Skip to content

[UPDATE QUERY] JavaScript: track req.session across calls in js/session-fixation, add js/rate-limit-spoofable-key (CWE-384, CWE-307) - #367

Draft
felickz with Copilot wants to merge 4 commits into
mainfrom
copilot/fix-332622516-1410885174-2c292797-c057-453f-9b3a-28ee66a315f0
Draft

felickz with Copilot wants to merge 4 commits into
mainfrom
copilot/fix-332622516-1410885174-2c292797-c057-453f-9b3a-28ee66a315f0

Conversation

Copilot AI commented Oct 31, 2026 •

Copy link
Copy Markdown

📝 Query Update

  • Query File: languages/javascript/custom/src/SessionFixation/SessionFixation.ql (custom copy of the stock js/session-fixation); new: languages/javascript/custom/src/RateLimitSpoofableKey/RateLimitSpoofableKey.ql (js/rate-limit-spoofable-key)
  • Language: javascript
  • Update Type: enhanced detection

🎯 Changes

What Changed

I rebuilt each of the 6 linked findings from the real source files and ran the shipped queries on them first. Only 2 were actual misses:

Finding Stock query result Change
NodeGoat CWE-384 Already reported by js/session-fixation. That query is only in security-extended, not the default suite. Regression test
nodejs-goof CWE-384 Missed: req.session is passed to a helper, and the helper does the write Extend js/session-fixation
NodeGoat, nodejs-goof, Goatlin CWE-307 Already reported by js/missing-rate-limiting (default suite). The alert sits on the route registration, not on the handler. Regression tests
juice-shop CWE-307 Missed: no query looks at how the limiter computes its key New query
  • js/session-fixation (custom copy, same id)
    • Follows req.session into other functions it is passed to, so both a session write and a regenerate call inside a helper are found. Otherwise the logic matches the stock query.
  • js/rate-limit-spoofable-key (new, path-problem)
    • Targets express-rate-limit and express-slow-down limiters created with rateLimit(...), new RateLimit(...) or slowDown(...).
    • Sources, read from the request passed to keyGenerator (also followed into helper functions):
      • client-IP headers (x-forwarded-for, x-real-ip, forwarded, cf-connecting-ip, …) read via req.headers[...], req.get(...) or req.header(...);
      • req.ip and req.ips, only when the app that installs the limiter (or a parent app of the router that does) calls app.enable('trust proxy') or set('trust proxy', true).
    • Sink: the value returned by keyGenerator.
    • req.headers[...] is matched only by its lower-case name. Node lower-cases header keys, so headers['X-Forwarded-For'] is always undefined and is not reported.
  • Tests: new test dirs MissingRateLimiting/, SessionFixation/, RateLimitSpoofableKey/ and RateLimitSpoofableKeyTrustedProxy/, each with safe counterparts that must not alert. The test pack now depends on codeql/javascript-queries so the stock query can be tested directly.
  • Docs: .qhelp with good and bad examples for both queries, plus a change note.

Why This Change

The AI scan reported these 6 findings and the default-setup scan did not. The point was to separate real query gaps from cases the stock queries already report, then close the real gaps.

Impact

  • js/session-fixation: more true positives on login routes that pass the session to a helper.
  • New alerts for spoofable rate-limit keys. Expected volume is low, since the query only looks at limiters with an explicit keyGenerator.

🔍 Before vs. After

Previous Behavior

// nodejs-goof: not reported
exports.loginHandler = (req, res) => { /* auth ok */ adminLoginSuccess(req.session, res); };
function adminLoginSuccess(session, res) { session.loggedIn = 1; }

// juice-shop: not reported
app.enable('trust proxy')
app.use('/rest/user/reset-password', rateLimit({
  keyGenerator ({ headers, ip }) { return headers['X-Forwarded-For'] ?? ip }
}))

New Behavior

  • Reported: app.post('/login', routes.loginHandler). Not reported when the helper calls session.regenerate(...).
  • Reported: the juice-shop keyGenerator return. The alert points at ip: the X-Forwarded-For lookup is always undefined, so the key is req.ip, which comes from the client's X-Forwarded-For because the app trusts every proxy.
  • Not reported:
    • req.ip with set('trust proxy', 1);
    • req.ip in a different app with no trust proxy setting;
    • non-IP headers such as x-api-key;
    • req.socket.remoteAddress;
    • user-id keys.

🧪 Testing

  • New/updated test cases added
  • All tests pass
  • Regression tests validated
  • No unintended side effects

📊 Impact Analysis

  • Compatibility: backward compatible. The custom js/session-fixation reports everything the stock query reports, plus the new cases.
  • Performance: neutral. Each query adds a small interprocedural trace scoped to one route handler or one key generator.
  • Accuracy: improved recall. Known limits of js/rate-limit-spoofable-key, which is why its precision is medium:
    • It matches headers by name, so it can't tell when a reverse proxy overwrites X-Real-IP and makes it trustworthy.
    • Limiters left on the default key (no keyGenerator) are not checked.
    • A limiter that can't be tied to an app falls back to whether any app in the codebase trusts all proxies.

📋 Checklist

  • Query compiles without errors
  • Documentation updated
  • Tests updated with new expectations
  • Metadata updated if needed

🔗 References

…ey and missing-rate-limiting regression tests

Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(session({ secret: 'keyboard cat' }));
const routes = require("./routes");

MongoClient.connect("mongodb://localhost/db", (err, db) => {
app.use(session({ secret: "secret", saveUninitialized: true, resave: true }));

const router = express.Router();

router.get('/accounts/:username/profile', auth, (req, res) => { // $ Alert

app.get("/login", sessionHandler.displayLoginPage);
app.post("/login", sessionHandler.handleLoginRequest); // $ Alert
app.post("/safe-login", sessionHandler.handleSafeLoginRequest);

var app = express();
app.get('/login', routes.login);
app.post('/login', routes.loginHandler); // $ Alert
const sessionHandler = new SessionHandler(db);

app.get("/login", sessionHandler.displayLoginPage);
app.post("/login", sessionHandler.handleLoginRequest); // $ Alert

// GOOD: the login handler is rate-limited.
var loginLimiter = rateLimit({ windowMs: 15 * 60 * 1000, max: 5 });
app.post('/limited-login', loginLimiter, routes.loginHandler);

var app = express();
app.get('/login', routes.login);
app.post('/login', routes.loginHandler); // $ Alert
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(session({ secret: 'keyboard cat' }));
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(session({ secret: 'keyboard cat' }));
…nd match lower-case header names

Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
Copilot AI changed the title [WIP] Copilot Request [UPDATE QUERY] JavaScript: track req.session across calls in js/session-fixation, add js/rate-limit-spoofable-key (CWE-384, CWE-307) Oct 31, 2026
Copilot AI requested a review from felickz October 31, 2026 06:46
@felickz
felickz requested a balanced review from Copilot November 2, 2026 18:39

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The query loses limiter and application trust context, causing false positives for shared key generators and sub-app trust overrides, plus false negatives for unresolved router mounts.

3 open issues
What changed in this PR

This PR addresses the confirmed detection gaps from issue #358 in the JavaScript custom CodeQL pack, while adding regression tests for cases already covered by stock queries.

Changes:

  • Extends session-fixation detection to follow session objects into helper functions.
  • Adds a query for rate-limit keys derived from spoofable client-address values.
  • Adds positive and negative fixtures, query help, examples, and a change note.
File Description
languages/​javascript/​custom/​test/​SessionFixation/​SessionFixation.qlref Runs the custom session-fixation query.
languages/​javascript/​custom/​test/​SessionFixation/​SessionFixation.expected Records expected session-fixation alerts.
languages/​javascript/​custom/​test/​SessionFixation/​package.json Sets CommonJS fixture semantics.
languages/​javascript/​custom/​test/​SessionFixation/​nodegoat/​user-dao.js Provides callback-based authentication fixtures.
languages/​javascript/​custom/​test/​SessionFixation/​nodegoat/​server.js Connects session middleware and routes.
languages/​javascript/​custom/​test/​SessionFixation/​nodegoat/​routes/​session.js Covers direct session writes and regeneration.
languages/​javascript/​custom/​test/​SessionFixation/​nodegoat/​routes/​index.js Registers vulnerable and safe login routes.
languages/​javascript/​custom/​test/​SessionFixation/​goof/​routes.js Covers session writes and regeneration in helpers.
languages/​javascript/​custom/​test/​SessionFixation/​goof/​app.js Registers helper-based login fixtures.
languages/​javascript/​custom/​test/​RateLimitSpoofableKeyTrustedProxy/​RateLimitSpoofableKey.qlref Runs the spoofable-key query.
languages/​javascript/​custom/​test/​RateLimitSpoofableKeyTrustedProxy/​RateLimitSpoofableKey.expected Records the raw-header alert.
languages/​javascript/​custom/​test/​RateLimitSpoofableKeyTrustedProxy/​package.json Sets CommonJS fixture semantics.
languages/​javascript/​custom/​test/​RateLimitSpoofableKeyTrustedProxy/​app.js Covers fixed-hop trust and header casing.
languages/​javascript/​custom/​test/​RateLimitSpoofableKey/​server.ts Covers trust-all settings and nested routers.
languages/​javascript/​custom/​test/​RateLimitSpoofableKey/​RateLimitSpoofableKey.qlref Runs the spoofable-key query.
languages/​javascript/​custom/​test/​RateLimitSpoofableKey/​RateLimitSpoofableKey.expected Records alerts and data-flow paths.
languages/​javascript/​custom/​test/​RateLimitSpoofableKey/​package.json Sets CommonJS fixture semantics.
languages/​javascript/​custom/​test/​RateLimitSpoofableKey/​other-app.js Adds a separate non-trusting application.
languages/​javascript/​custom/​test/​RateLimitSpoofableKey/​limiters.js Covers limiter APIs, helpers, and safe keys.
languages/​javascript/​custom/​test/​qlpack.yml Adds stock queries as a test dependency.
languages/​javascript/​custom/​test/​MissingRateLimiting/​package.json Sets CommonJS fixture semantics.
languages/​javascript/​custom/​test/​MissingRateLimiting/​nodegoat/​user-dao.js Provides database-backed login validation.
languages/​javascript/​custom/​test/​MissingRateLimiting/​nodegoat/​server.js Wires the NodeGoat-style application.
languages/​javascript/​custom/​test/​MissingRateLimiting/​nodegoat/​routes/​session.js Provides constructor-defined authentication handlers.
languages/​javascript/​custom/​test/​MissingRateLimiting/​nodegoat/​routes/​index.js Registers an unthrottled login route.
languages/​javascript/​custom/​test/​MissingRateLimiting/​MissingRateLimiting.qlref Runs the stock missing-rate-limiting query.
languages/​javascript/​custom/​test/​MissingRateLimiting/​MissingRateLimiting.expected Records three stock-query alerts.
languages/​javascript/​custom/​test/​MissingRateLimiting/​goof/​routes.js Provides database-backed administrator login.
languages/​javascript/​custom/​test/​MissingRateLimiting/​goof/​mongoose-db.js Defines the authentication database model.
languages/​javascript/​custom/​test/​MissingRateLimiting/​goof/​app.js Compares throttled and unthrottled login routes.
languages/​javascript/​custom/​test/​MissingRateLimiting/​goatlin/​routes/​accounts.js Compares throttled and unthrottled authentication middleware.
languages/​javascript/​custom/​test/​MissingRateLimiting/​goatlin/​models/​account.js Defines the account lookup model.
languages/​javascript/​custom/​test/​MissingRateLimiting/​goatlin/​middleware/​auth.js Provides Basic authentication with database access.
languages/​javascript/​custom/​test/​MissingRateLimiting/​goatlin/​app.js Mounts the account router.
languages/​javascript/​custom/​src/​SessionFixation/​SessionFixation.ql Tracks session objects across function calls.
languages/​javascript/​custom/​src/​SessionFixation/​SessionFixation.qhelp Documents session-fixation detection and remediation.
languages/​javascript/​custom/​src/​SessionFixation/​examples/​SessionFixationGood.js Demonstrates regeneration before authentication writes.
languages/​javascript/​custom/​src/​SessionFixation/​examples/​SessionFixationBad.js Demonstrates session reuse through a helper.
languages/​javascript/​custom/​src/​RateLimitSpoofableKey/​RateLimitSpoofableKey.ql Detects spoofable values flowing into limiter keys.
languages/​javascript/​custom/​src/​RateLimitSpoofableKey/​RateLimitSpoofableKey.qhelp Explains spoofable keys and proxy configuration.
languages/​javascript/​custom/​src/​RateLimitSpoofableKey/​examples/​RateLimitSpoofableKeyGood.js Demonstrates a fixed-hop proxy configuration.
languages/​javascript/​custom/​src/​RateLimitSpoofableKey/​examples/​RateLimitSpoofableKeyBad.js Demonstrates a client-controlled limiter key.
languages/​javascript/​custom/​src/​change-notes/​2026-10-31-session-fixation-rate-limit-key.md Summarizes both query additions.

Review effort: Balanced

Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

4 participants