Skip to content

Commit a68bb9f

Browse files
committed
JS: Ignore calls and csrf/captcha access
1 parent b1ec3e1 commit a68bb9f

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

javascript/ql/src/Security/CWE-352/MissingCsrfMiddleware.ql

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,22 @@
1212

1313
import javascript
1414

15+
/** Gets the string `session` or `cookies`, the parts of `req` containing cookie data. */
16+
string sessionOrCookies() {
17+
result = "session" or result = "cookies"
18+
}
19+
1520
/** Gets a data flow node that flows to the base of an access to `cookies` or `session`. */
1621
private DataFlow::SourceNode nodeLeadingToCookieAccess(DataFlow::TypeBackTracker t) {
1722
t.start() and
18-
exists(string name | name = "session" or name = "cookies" |
19-
exists(result.getAPropertyRead(name))
23+
exists(DataFlow::PropRead value |
24+
value = result.getAPropertyRead(sessionOrCookies()).getAPropertyRead() and
25+
26+
// Ignore accesses to values that are part of a CSRF or captcha check
27+
not value.getPropertyName().regexpMatch("(?i).*(csrf|xsrf|captcha).*") and
28+
29+
// Ignore calls like `req.session.save()`
30+
not value = any(DataFlow::InvokeNode call).getCalleeNode()
2031
)
2132
or
2233
exists(DataFlow::TypeBackTracker t2 |

javascript/ql/test/query-tests/Security/CWE-352/unused_cookies.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,13 @@ app.post('/doSomethingElse', (req, res) => { // OK - doesn't actually use cookie
1717
res.end('Ok');
1818
});
1919

20+
app.post('/doWithCaptcha', (req, res) => { // OK - attacker can't guess the captcha value either
21+
if (req.session['captcha'] !== req.query['captcha']) {
22+
res.end("You guessed wrong, that 'u' was actually a 'U'. Try again.");
23+
return;
24+
}
25+
somethingElse(req.query['data']);
26+
res.end('Ok');
27+
});
28+
2029
app.listen();

0 commit comments

Comments
 (0)