diff --git a/tests/httpsServer.test.cjs b/tests/httpsServer.test.cjs index 65acceda94..7d810cd25d 100644 --- a/tests/httpsServer.test.cjs +++ b/tests/httpsServer.test.cjs @@ -51,7 +51,14 @@ test('the redirect keeps only the path, and names a host the browser asked for o test('pages and websocket upgrades over TLS, through the existing HTTP server; a renewed certificate without a restart', { skip: !HAS_OPENSSL && 'needs openssl' }, async () => { const dir = fs.mkdtempSync(path.join(ROOT, '.tools', 'tmp', 'https-')); const first = selfSigned(dir, 'first.example'); - const app = http.createServer((req, res) => res.end(`served ${req.url} encrypted=${!!req.socket.encrypted}`)); + // The page is named by comparison, never by echoing req.url back: a + // response that reflects the request is the reflected-XSS shape (CodeQL + // #583), even in a test (tests/noReflectedRequest.test.cjs). + const app = http.createServer((req, res) => { + const page = req.url === '/hello' ? '/hello' : 'another page'; + res.setHeader('Content-Type', 'text/plain; charset=utf-8'); + res.end(`served ${page} encrypted=${!!req.socket.encrypted}`); + }); let upgraded = false; app.on('upgrade', (req, socket) => { upgraded = true; socket.end('HTTP/1.1 101 Switching Protocols\r\n\r\n'); }); const env = { HTTPS_PORT: '1', HTTPS_CERT_FILE: first.cert, HTTPS_KEY_FILE: first.key }; diff --git a/tests/noReflectedRequest.test.cjs b/tests/noReflectedRequest.test.cjs new file mode 100644 index 0000000000..8dccf1a1e4 --- /dev/null +++ b/tests/noReflectedRequest.test.cjs @@ -0,0 +1,49 @@ +'use strict'; + +// CodeQL js/reflected-xss (alert #583): a test's HTTP server answered with +// `served ${req.url} ...`, sending the request back as the body. Harmless in a +// test, and exactly the shape of reflected XSS anywhere else, so the shape is +// kept out of the whole tree: no res.end/res.write/res.send writes a request's +// url, query, params, headers or body straight into the response. +// Run: node tests/noReflectedRequest.test.cjs + +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const { execFileSync } = require('node:child_process'); + +const ROOT = path.join(__dirname, '..'); +// A write call whose argument mentions the request's own data directly. +const REFLECTS = /\bres\.(end|write|send)\(\s*[^;]*\breq\.(url|originalUrl|query|params|headers|body)\b/; + +function findings(files) { + const out = []; + for (const file of files) { + const lines = fs.readFileSync(path.join(ROOT, file), 'utf8').split('\n'); + lines.forEach((line, i) => { + if (/^\s*(\/\/|\*)/.test(line)) return; + if (REFLECTS.test(line)) out.push(`${file}:${i + 1}: ${line.trim()}`); + }); + } + return out; +} + +const tracked = execFileSync('git', ['ls-files', '--', '*.js', '*.cjs', '*.mjs'], { cwd: ROOT }).toString() + .split('\n').filter(f => f && !f.startsWith('_build/') && !f.includes('node_modules/') + && f !== 'tests/noReflectedRequest.test.cjs' && fs.existsSync(path.join(ROOT, f))); +assert.deepEqual(findings(tracked), [], 'a response reflects the request'); +console.log(` ok - ${tracked.length} files: no response reflects the request`); + +// The detector finds the alert's own shape, and leaves a fixed answer alone. +const tmp = path.join(ROOT, '.tools', 'tmp', `reflect-${process.pid}`); +fs.mkdirSync(tmp, { recursive: true }); +try { + const rel = name => path.relative(ROOT, path.join(tmp, name)); + fs.writeFileSync(path.join(tmp, 'bad.js'), 'http.createServer((req, res) => res.end(`served ${req.url}`));\n'); + fs.writeFileSync(path.join(tmp, 'good.js'), "const page = req.url === '/hello' ? '/hello' : 'other';\nres.end(`served ${page}`);\n"); + assert.equal(findings([rel('bad.js')]).length, 1, 'the #583 shape is caught'); + assert.equal(findings([rel('good.js')]).length, 0, 'a fixed answer chosen by comparison passes'); +} finally { + fs.rmSync(tmp, { recursive: true, force: true }); +} +console.log(' ok - the detector catches a reflected request and passes a fixed answer');