Closed
Description
Bug: location function does not validate potentially malicious URLs
Environment information
Version: express 5.0.0-beta.3
Platform: Linux
Node.js version: 18.x
Any other relevant information: N/A
What steps will reproduce the bug?
- Create an Express application
- Set up a route that uses
res.location()
with a malicious URL - Send a request to this route and observe the response headers
const express = require('express');
const app = express();
app.get('/redirect', (req, res) => {
// Set a malicious URL in the Location header
res.location('javascript:alert("XSS attack")');
res.status(302).send('Redirecting...');
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});
// Using curl to test:
// $ curl -i http://localhost:3000/redirect
//
// The response headers will contain:
// Location: javascript:alert("XSS attack")
What is the expected behavior?
The location function should validate the URL scheme and reject or sanitize non-HTTP(S) URLs, especially those with potentially dangerous schemes like javascript:, data.
What happens instead?
The function sets the malicious URL directly in the Location header without any validation:
HTTP/1.1 302 Found
Location: javascript:alert("XSS attack")
...
This creates a security vulnerability as browsers following this redirect might execute the JavaScript code, leading to Cross-Site Scripting (XSS) attacks.
Source of the issue
The issue is in the location function in lib/response.js.