fix(metrics): handle malformed URLs gracefully in metrics endpoint
Wrap URL parsing in try/catch to prevent server crashes from malformed requests (invalid percent-encoding, problematic Host headers, etc.). Returns 400 Bad Request instead of crashing the process.
This commit is contained in:
@@ -1222,8 +1222,22 @@ export class MetricsServer {
|
|||||||
|
|
||||||
this.server = createServer(async (req: IncomingMessage, res: ServerResponse) => {
|
this.server = createServer(async (req: IncomingMessage, res: ServerResponse) => {
|
||||||
// Parse URL to handle querystrings and trailing slashes
|
// Parse URL to handle querystrings and trailing slashes
|
||||||
const parsedUrl = new URL(req.url || '/', `http://${req.headers.host || 'localhost'}`)
|
// Wrapped in try/catch to handle malformed URLs gracefully
|
||||||
const pathname = parsedUrl.pathname.replace(/\/+$/, '') || '/' // Normalize trailing slashes
|
let pathname: string
|
||||||
|
try {
|
||||||
|
const parsedUrl = new URL(req.url || '/', `http://${req.headers.host || 'localhost'}`)
|
||||||
|
pathname = parsedUrl.pathname.replace(/\/+$/, '') || '/'
|
||||||
|
} catch {
|
||||||
|
// Malformed URL - return 400 Bad Request
|
||||||
|
res.writeHead(400, { 'Content-Type': 'application/json' })
|
||||||
|
res.end(JSON.stringify({
|
||||||
|
error: 'Bad Request',
|
||||||
|
message: 'Malformed URL',
|
||||||
|
timestamp: new Date().toISOString()
|
||||||
|
}))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const configPath = this.config.path.replace(/\/+$/, '') || '/'
|
const configPath = this.config.path.replace(/\/+$/, '') || '/'
|
||||||
|
|
||||||
if (pathname === configPath && req.method === 'GET') {
|
if (pathname === configPath && req.method === 'GET') {
|
||||||
|
|||||||
Reference in New Issue
Block a user