Skip to content

Commit 5f467d2

Browse files
committed
JS: recognize CSRF middleware from lusca package
1 parent 69962bd commit 5f467d2

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,15 @@ predicate hasCookieMiddleware(Express::RouteHandlerExpr expr, Express::RouteHand
3838
* // protected from CSRF
3939
* })
4040
* ```
41-
*
42-
* Currently the predicate only detects `csurf`-based protectors.
4341
*/
4442
DataFlow::CallNode csrfMiddlewareCreation() {
45-
exists (DataFlow::ModuleImportNode mod | result = mod.getACall() |
46-
mod.getPath() = "csurf"
43+
exists (DataFlow::SourceNode callee | result = callee.getACall() |
44+
callee = DataFlow::moduleImport("csurf")
45+
or
46+
callee = DataFlow::moduleImport("lusca") and
47+
result.getOptionArgument(0, "csrf").analyze().getABooleanValue() = true // any truthy value will enable CSRF
48+
or
49+
callee = DataFlow::moduleMember("lusca", "csrf")
4750
)
4851
}
4952

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
| MissingCsrfMiddlewareBad.js:7:9:7:22 | cookieParser() | This cookie middleware is serving a request handler $@ without CSRF protection. | MissingCsrfMiddlewareBad.js:10:26:11:1 | functio ... es) {\\n} | here |
22
| csurf_api_example.js:39:37:39:50 | cookieParser() | This cookie middleware is serving a request handler $@ without CSRF protection. | csurf_api_example.js:39:53:41:3 | functio ... e')\\n } | here |
33
| csurf_example.js:18:9:18:22 | cookieParser() | This cookie middleware is serving a request handler $@ without CSRF protection. | csurf_example.js:29:40:31:1 | functio ... sed')\\n} | here |
4+
| lusca_example.js:9:9:9:22 | cookieParser() | This cookie middleware is serving a request handler $@ without CSRF protection. | lusca_example.js:23:42:25:1 | functio ... sed')\\n} | here |
5+
| lusca_example.js:9:9:9:22 | cookieParser() | This cookie middleware is serving a request handler $@ without CSRF protection. | lusca_example.js:27:55:29:1 | functio ... sed')\\n} | here |
6+
| lusca_example.js:9:9:9:22 | cookieParser() | This cookie middleware is serving a request handler $@ without CSRF protection. | lusca_example.js:31:40:33:1 | functio ... sed')\\n} | here |
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var express = require('express')
2+
var cookieParser = require('cookie-parser')
3+
var bodyParser = require('body-parser')
4+
5+
var parseForm = bodyParser.urlencoded({ extended: false })
6+
var lusca = require('lusca');
7+
8+
var app = express()
9+
app.use(cookieParser())
10+
11+
app.post('/process', parseForm, lusca.csrf(), function (req, res) { // OK
12+
res.send('data is being processed')
13+
})
14+
15+
app.post('/process', parseForm, lusca({csrf:true}), function (req, res) { // OK
16+
res.send('data is being processed')
17+
})
18+
19+
app.post('/process', parseForm, lusca({csrf:{}}), function (req, res) { // OK
20+
res.send('data is being processed')
21+
})
22+
23+
app.post('/process', parseForm, lusca(), function (req, res) { // NOT OK - missing csrf option
24+
res.send('data is being processed')
25+
})
26+
27+
app.post('/process', parseForm, lusca({csrf: false}), function (req, res) { // NOT OK - csrf disabled
28+
res.send('data is being processed')
29+
})
30+
31+
app.post('/process_unsafe', parseForm, function (req, res) { // NOT OK
32+
res.send('data is being processed')
33+
})

0 commit comments

Comments
 (0)