Skip to content

Commit b1ec3e1

Browse files
committed
JS: Add test and dont check predecessors
1 parent 406c6eb commit b1ec3e1

File tree

8 files changed

+41
-6
lines changed

8 files changed

+41
-6
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ where
109109
// the handler wouldn't work. However, if we can't find the cookie middleware, it
110110
// indicates that our middleware model is too incomplete, so in that case we
111111
// don't trust it to detect the presence of CSRF middleware either.
112-
getARouteUsingCookies().flowsToExpr(handler.getPreviousMiddleware*()) and
112+
getARouteUsingCookies().flowsToExpr(handler) and
113113
hasCookieMiddleware(handler, cookie) and
114114

115115
// Only flag the first cookie parser registered first.
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
| 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 |
2-
| 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 |
3-
| 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:40:29:1 | functio ... sed')\\n} | here |
1+
| MissingCsrfMiddlewareBad.js:7:9:7:22 | cookieParser() | This cookie middleware is serving a request handler $@ without CSRF protection. | MissingCsrfMiddlewareBad.js:10:26:12:1 | functio ... il"];\\n} | here |
2+
| csurf_api_example.js:42:37:42:50 | cookieParser() | This cookie middleware is serving a request handler $@ without CSRF protection. | csurf_api_example.js:42:53:45:3 | functio ... e')\\n } | here |
3+
| csurf_example.js:18:9:18:22 | cookieParser() | This cookie middleware is serving a request handler $@ without CSRF protection. | csurf_example.js:31:40:34: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:26:42:29: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:31:40:34:1 | functio ... sed')\\n} | here |
6+
| unused_cookies.js:6:9:6:22 | cookieParser() | This cookie middleware is serving a request handler $@ without CSRF protection. | unused_cookies.js:8:34:13:1 | (req, r ... Ok');\\n} | here |

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ app.use(cookieParser())
88
app.use(passport.authorize({ session: true }))
99

1010
app.post('/changeEmail', function (req, res) {
11+
let newEmail = req.cookies["newEmail"];
1112
})

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ app.use(passport.authorize({ session: true }))
1010
app.use(csrf({ cookie:true }))
1111

1212
app.post('/changeEmail', function (req, res) {
13+
let newEmail = req.cookies["newEmail"];
1314
})

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,26 @@ app.use(cookieParser())
2121
app.use(csrf({ cookie: true }))
2222

2323
app.get('/form', function (req, res) {
24+
let newEmail = req.cookies["newEmail"];
2425
// pass the csrfToken to the view
2526
res.render('send', { csrfToken: req.csrfToken() })
2627
})
2728

2829
app.post('/process', function (req, res) { // OK
30+
let newEmail = req.cookies["newEmail"];
2931
res.send('csrf was required to get here')
3032
})
3133

3234
function createApiRouter () {
3335
var router = new express.Router()
3436

3537
router.post('/getProfile', function (req, res) { // OK - cookies are not parsed
38+
let newEmail = req.cookies["newEmail"];
3639
res.send('no csrf to get here')
3740
})
3841

3942
router.post('/getProfile_unsafe', cookieParser(), function (req, res) { // NOT OK - may use cookies
43+
let newEmail = req.cookies["newEmail"];
4044
res.send('no csrf to get here')
4145
})
4246

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@ var app = express()
1818
app.use(cookieParser())
1919

2020
app.get('/form', csrfProtection, function (req, res) { // OK
21+
let newEmail = req.cookies["newEmail"];
2122
// pass the csrfToken to the view
2223
res.render('send', { csrfToken: req.csrfToken() })
2324
})
2425

2526
app.post('/process', parseForm, csrfProtection, function (req, res) { // OK
27+
let newEmail = req.cookies["newEmail"];
2628
res.send('data is being processed')
2729
})
2830

2931
app.post('/process_unsafe', parseForm, function (req, res) { // NOT OK
32+
let newEmail = req.cookies["newEmail"];
3033
res.send('data is being processed')
3134
})

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,26 @@ var app = express()
99
app.use(cookieParser())
1010

1111
app.post('/process', parseForm, lusca.csrf(), function (req, res) { // OK
12+
let newEmail = req.cookies["newEmail"];
1213
res.send('data is being processed')
1314
})
1415

1516
app.post('/process', parseForm, lusca({csrf:true}), function (req, res) { // OK
17+
let newEmail = req.cookies["newEmail"];
1618
res.send('data is being processed')
1719
})
1820

1921
app.post('/process', parseForm, lusca({csrf:{}}), function (req, res) { // OK
22+
let newEmail = req.cookies["newEmail"];
2023
res.send('data is being processed')
2124
})
2225

2326
app.post('/process', parseForm, lusca(), function (req, res) { // NOT OK - missing csrf option
27+
let newEmail = req.cookies["newEmail"];
2428
res.send('data is being processed')
2529
})
2630

2731
app.post('/process_unsafe', parseForm, function (req, res) { // NOT OK
32+
let newEmail = req.cookies["newEmail"];
2833
res.send('data is being processed')
2934
})
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
let express = require('express');
2+
let cookieParser = require('cookie-parser');
3+
4+
let app = express();
5+
6+
app.use(cookieParser());
7+
8+
app.post('/doSomethingTerrible', (req, res) => { // NOT OK - uses cookies
9+
if (req.cookies['secret'] === app.secret) {
10+
somethingTerrible();
11+
}
12+
res.end('Ok');
13+
});
14+
15+
app.post('/doSomethingElse', (req, res) => { // OK - doesn't actually use cookies
16+
somethingElse(req.query['data']);
17+
res.end('Ok');
18+
});
19+
20+
app.listen();

0 commit comments

Comments
 (0)