Skip to content

Commit cd2c4d5

Browse files
committed
JS: Use post-processed inline test in MissingCsrfMiddleware
This query flags the cookie-parsing middleware in order to consolidate huge numbers of alerts into a single alert, which is more manageable. But simply annotating the cookie-parsing middleware with 'Alert' isn't a very useful, we want to annotate which middlewares are vulnerable.
1 parent e2fe74c commit cd2c4d5

File tree

9 files changed

+35
-34
lines changed

9 files changed

+35
-34
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
Security/CWE-352/MissingCsrfMiddleware.ql
1+
query: Security/CWE-352/MissingCsrfMiddleware.ql
2+
postprocess: utils/test/InlineExpectationsTestQuery.ql

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ var passport = require('passport');
44

55
var app = express();
66

7-
app.use(cookieParser());
7+
app.use(cookieParser()); // $ Alert
88
app.use(passport.authorize({ session: true }));
99

1010
app.post('/changeEmail', function (req, res) {
1111
let newEmail = req.cookies["newEmail"];
12-
});
12+
}); // $ RelatedLocation
1313

1414
(function () {
1515
var app = express();
1616

17-
app.use(cookieParser());
17+
app.use(cookieParser()); // $ Alert
1818
app.use(passport.authorize({ session: true }));
1919

2020
const errorCatch = (fn) =>
@@ -24,13 +24,13 @@ app.post('/changeEmail', function (req, res) {
2424

2525
app.post('/changeEmail', errorCatch(async function (req, res) {
2626
let newEmail = req.cookies["newEmail"];
27-
}));
27+
})); // $ RelatedLocation
2828
})
2929

3030
(function () {
3131
var app = express();
3232

33-
app.use(cookieParser());
33+
app.use(cookieParser()); // $ Alert
3434
app.use(passport.authorize({ session: true }));
3535

3636
const errorCatch = (fn) =>
@@ -40,9 +40,9 @@ app.post('/changeEmail', function (req, res) {
4040

4141
app.post('/changeEmail', errorCatch(async function (req, res) {
4242
let newEmail = req.cookies["newEmail"];
43-
}));
43+
})); // $ RelatedLocation
4444

4545
app.post('/doLoginStuff', errorCatch(async function (req, res) {
4646
req.session.user = loginStuff(req);
47-
}));
47+
})); // $ RelatedLocation
4848
})

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ function createApiRouter () {
3939
res.send('no csrf to get here')
4040
})
4141

42-
router.post('/getProfile_unsafe', cookieParser(), function (req, res) { // NOT OK - may use cookies
42+
router.post('/getProfile_unsafe', cookieParser(), function (req, res) { // $ Alert - may use cookies
4343
let newEmail = req.cookies["newEmail"];
4444
res.send('no csrf to get here')
45-
})
45+
}) // $ RelatedLocation
4646

4747
return router
4848
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var app = express()
1515

1616
// parse cookies
1717
// we need this because "cookie" is true in csrfProtection
18-
app.use(cookieParser())
18+
app.use(cookieParser()) // $ Alert
1919

2020
app.get('/form', csrfProtection, function (req, res) { // OK
2121
let newEmail = req.cookies["newEmail"];
@@ -28,7 +28,7 @@ app.post('/process', parseForm, csrfProtection, function (req, res) { // OK
2828
res.send('data is being processed')
2929
})
3030

31-
app.post('/process_unsafe', parseForm, function (req, res) { // NOT OK
31+
app.post('/process_unsafe', parseForm, function (req, res) {
3232
let newEmail = req.cookies["newEmail"];
3333
res.send('data is being processed')
34-
})
34+
}) // $ RelatedLocation

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const fastify = require('fastify')
22

33
const app = fastify();
44

5-
app.register(require('fastify-cookie'));
5+
app.register(require('fastify-cookie')); // $ Alert
66
app.register(require('fastify-csrf'));
77

88
app.route({
@@ -17,10 +17,10 @@ app.route({
1717
app.route({
1818
method: 'POST',
1919
path: '/',
20-
handler: async (req, reply) => { // NOT OK - lacks CSRF protection
20+
handler: async (req, reply) => { // lacks CSRF protection
2121
req.session.blah;
2222
return req.body
23-
}
23+
} // $ RelatedLocation
2424
})
2525

2626

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const fp = require('fastify-plugin');
44
const app = fastify();
55

66
function plugin(app) {
7-
app.register(require('fastify-cookie'));
7+
app.register(require('fastify-cookie')); // $ Alert
88
app.register(require('fastify-csrf'));
99
}
1010
app.register(fp(plugin));
@@ -21,10 +21,10 @@ app.route({
2121
app.route({
2222
method: 'POST',
2323
path: '/',
24-
handler: async (req, reply) => { // NOT OK - lacks CSRF protection
24+
handler: async (req, reply) => { // lacks CSRF protection
2525
req.session.blah;
2626
return req.body
27-
}
27+
} // $ RelatedLocation
2828
})
2929

3030

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var parseForm = bodyParser.urlencoded({ extended: false })
66
var lusca = require('lusca');
77

88
var app = express()
9-
app.use(cookieParser())
9+
app.use(cookieParser()) // $ Alert
1010

1111
app.post('/process', parseForm, lusca.csrf(), function (req, res) { // OK
1212
let newEmail = req.cookies["newEmail"];
@@ -23,12 +23,12 @@ app.post('/process', parseForm, lusca({csrf:{}}), function (req, res) { // OK
2323
res.send('data is being processed')
2424
})
2525

26-
app.post('/process', parseForm, lusca(), function (req, res) { // NOT OK - missing csrf option
26+
app.post('/process', parseForm, lusca(), function (req, res) { // missing csrf option
2727
let newEmail = req.cookies["newEmail"];
2828
res.send('data is being processed')
29-
})
29+
}) // $ RelatedLocation
3030

31-
app.post('/process_unsafe', parseForm, function (req, res) { // NOT OK
31+
app.post('/process_unsafe', parseForm, function (req, res) {
3232
let newEmail = req.cookies["newEmail"];
3333
res.send('data is being processed')
34-
})
34+
}) // $ RelatedLocation

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ const cookieParser = require('cookie-parser')
33
const csrf = require('csurf')
44

55
const app = express()
6-
app.use(cookieParser())
6+
app.use(cookieParser()) // $ Alert
77

8-
app.post('/unsafe', (req, res) => { // NOT OK
8+
app.post('/unsafe', (req, res) => {
99
req.cookies.x;
10-
});
10+
}); // $ RelatedLocation
1111

1212
function middlewares() {
1313
return express.Router()

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ let cookieParser = require('cookie-parser');
33

44
let app = express();
55

6-
app.use(cookieParser());
6+
app.use(cookieParser()); // $ Alert
77

8-
app.post('/doSomethingTerrible', (req, res) => { // NOT OK - uses cookies
8+
app.post('/doSomethingTerrible', (req, res) => { // uses cookies
99
if (req.cookies['secret'] === app.secret) {
1010
somethingTerrible();
1111
}
1212
res.end('Ok');
13-
});
13+
}); // $ RelatedLocation
1414

1515
app.post('/doSomethingElse', (req, res) => { // OK - doesn't actually use cookies
1616
somethingElse(req.query['data']);
@@ -26,14 +26,14 @@ app.post('/doWithCaptcha', (req, res) => { // OK - attacker can't guess the capt
2626
res.end('Ok');
2727
});
2828

29-
app.post('/user', (req, res) => { // NOT OK - access to req.user is unprotected
29+
app.post('/user', (req, res) => { // access to req.user is unprotected
3030
somethingElse(req.user.name);
3131
res.end('Ok');
32-
});
32+
}); // $ RelatedLocation
3333

34-
app.post('/session', (req, res) => { // NOT OK - access to req.session is unprotected
34+
app.post('/session', (req, res) => { // access to req.session is unprotected
3535
somethingElse(req.session.name);
3636
res.end('Ok');
37-
});
37+
}); // $ RelatedLocation
3838

3939
app.listen();

0 commit comments

Comments
 (0)