Conversation
|
| serveStaticFile(req, res, filePath); | ||
| } else if (stats.isDirectory() && !parsedUrl.pathname.endsWith('/')) { | ||
| // Redirect to URL with trailing slash for directories | ||
| res.writeHead(301, { 'Location': req.url + '/' }); |
Check warning
Code scanning / CodeQL
Server-side URL redirect Medium
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 6 months ago
The best way to fix this problem is to avoid using the raw req.url in the redirect and instead construct a canonical, relative URL path based solely on validated input. Specifically, when redirecting a user to the trailing slash version of a directory, parse and reconstruct the pathname—without including any potentially harmful user-supplied query strings or full URLs. The correct approach is to use the parsed pathname (from url.parse(req.url, true)) and ensure the result is a relative path (not an absolute URL). Additionally, you may want to re-attach the original query parameters (if any), to preserve search parameters when redirecting, but do so via encoding.
In practical terms, replace res.writeHead(301, { 'Location': req.url + '/' }); with a construct that starts with the URL-parsed pathname, appends the trailing slash, and (optionally) preserves the query string, ensuring the entire result is a relative path. All code changes can be made directly in serve-proxy.js at line 302, using standard Node.js modules you are already importing.
| @@ -299,7 +299,11 @@ | ||
| serveStaticFile(req, res, filePath); | ||
| } else if (stats.isDirectory() && !parsedUrl.pathname.endsWith('/')) { | ||
| // Redirect to URL with trailing slash for directories | ||
| res.writeHead(301, { 'Location': req.url + '/' }); | ||
| let redirectPath = parsedUrl.pathname + '/'; | ||
| if (parsedUrl.search) { | ||
| redirectPath += parsedUrl.search; | ||
| } | ||
| res.writeHead(301, { 'Location': redirectPath }); | ||
| res.end(); | ||
| } else { | ||
| serveStaticFile(req, res, filePath); |



No description provided.