Skip to content

Commit f36accf

Browse files
committed
only report clear-text cookies for sensitive cookies
1 parent 53b4337 commit f36accf

File tree

4 files changed

+24
-18
lines changed

4 files changed

+24
-18
lines changed

javascript/ql/lib/semmle/javascript/frameworks/CookieLibraries.qll

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ private module JsCookie {
7373
this.getOptionArgument(2, CookieWrites::secure()).mayHaveBooleanValue(true)
7474
}
7575

76-
override predicate isSensitive() { none() } // TODO: Maybe it can be sensitive?
76+
override predicate isSensitive() {
77+
HeuristicNames::nameIndicatesSensitiveData(any(string s |
78+
this.getArgument(0).mayHaveStringValue(s)
79+
), _)
80+
}
7781

7882
override predicate isHttpOnly() { none() } // js-cookie is browser side library and doesn't support HttpOnly
7983
}

javascript/ql/src/Security/CWE-614/InsecureCookie.ql

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212

1313
import javascript
1414

15+
// TODO: Rename to ClearTextCookie?
1516
from DataFlow::Node node
16-
// TODO: Only for sensitive cookies? (e.g. auth cookies)
17-
where exists(CookieWrites::CookieWrite cookie | cookie = node | not cookie.isSecure())
17+
where
18+
exists(CookieWrites::CookieWrite cookie | cookie = node |
19+
cookie.isSensitive() and not cookie.isSecure()
20+
)
1821
select node, "Cookie is added to response without the 'secure' flag being set to true"

javascript/ql/test/query-tests/Security/CWE-614/InsecureCookies.expected

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
| tst-cleartextCookie.js:5:5:10:10 | res.coo ... }) | Cookie is added to response without the 'secure' flag being set to true |
2-
| tst-cleartextCookie.js:20:5:20:40 | res.coo ... ptions) | Cookie is added to response without the 'secure' flag being set to true |
3-
| tst-cleartextCookie.js:35:1:35:48 | js_cook ... alse }) | Cookie is added to response without the 'secure' flag being set to true |
4-
| tst-cleartextCookie.js:44:37:44:48 | "type=ninja" | Cookie is added to response without the 'secure' flag being set to true |
5-
| tst-cleartextCookie.js:64:38:64:49 | "type=ninja" | Cookie is added to response without the 'secure' flag being set to true |
6-
| tst-cleartextCookie.js:64:52:64:72 | "langua ... script" | Cookie is added to response without the 'secure' flag being set to true |
7-
| tst-cleartextCookie.js:94:60:94:80 | "langua ... script" | Cookie is added to response without the 'secure' flag being set to true |
2+
| tst-cleartextCookie.js:20:5:20:43 | res.coo ... ptions) | Cookie is added to response without the 'secure' flag being set to true |
3+
| tst-cleartextCookie.js:35:1:35:52 | js_cook ... alse }) | Cookie is added to response without the 'secure' flag being set to true |
4+
| tst-cleartextCookie.js:44:37:44:51 | "authKey=ninja" | Cookie is added to response without the 'secure' flag being set to true |
5+
| tst-cleartextCookie.js:64:38:64:52 | "authKey=ninja" | Cookie is added to response without the 'secure' flag being set to true |
6+
| tst-cleartextCookie.js:94:60:94:72 | "authKey=foo" | Cookie is added to response without the 'secure' flag being set to true |
87
| tst-cleartextCookie.js:104:9:107:2 | session ... T OK\\n}) | Cookie is added to response without the 'secure' flag being set to true |
98
| tst-cleartextCookie.js:109:9:112:2 | session ... T OK\\n}) | Cookie is added to response without the 'secure' flag being set to true |
109
| tst-cleartextCookie.js:114:9:117:2 | session ... T OK\\n}) | Cookie is added to response without the 'secure' flag being set to true |

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

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

44
app.get('/a', function (req, res, next) {
5-
res.cookie('name', 'value',
5+
res.cookie('authkey', 'value',
66
{
77
maxAge: 9000000000,
88
httpOnly: true,
@@ -17,7 +17,7 @@ app.get('/b', function (req, res, next) {
1717
httpOnly: true,
1818
secure: false // NOT OK
1919
}
20-
res.cookie('name', 'value', options);
20+
res.cookie('authKey', 'value', options);
2121
res.end('ok')
2222
})
2323

@@ -32,16 +32,16 @@ app.get('/c', function (req, res, next) {
3232
})
3333

3434
const js_cookie = require('js-cookie')
35-
js_cookie.set('key', 'value', { secure: false }); // NOT OK
36-
js_cookie.set('key', 'value', { secure: true }); // OK
35+
js_cookie.set('authKey', 'value', { secure: false }); // NOT OK
36+
js_cookie.set('authKey', 'value', { secure: true }); // OK
3737

3838
const http = require('http');
3939

4040
function test1() {
4141
const server = http.createServer((req, res) => {
4242
res.setHeader('Content-Type', 'text/html');
4343
// BAD
44-
res.setHeader("Set-Cookie", "type=ninja");
44+
res.setHeader("Set-Cookie", "authKey=ninja");
4545
res.writeHead(200, { 'Content-Type': 'text/plain' });
4646
res.end('ok');
4747
});
@@ -60,8 +60,8 @@ function test2() {
6060
function test3() {
6161
const server = http.createServer((req, res) => {
6262
res.setHeader('Content-Type', 'text/html');
63-
// BAD
64-
res.setHeader("Set-Cookie", ["type=ninja", "language=javascript"]);
63+
// BAD (and good, TODO: Move to separate lines)
64+
res.setHeader("Set-Cookie", ["authKey=ninja", "language=javascript"]);
6565
res.writeHead(200, { 'Content-Type': 'text/plain' });
6666
res.end('ok');
6767
});
@@ -90,8 +90,8 @@ function test5() {
9090
function test6() {
9191
const server = http.createServer((req, res) => {
9292
res.setHeader('Content-Type', 'text/html');
93-
// BAD
94-
res.setHeader("Set-Cookie", ["type=ninja; secure", "language=javascript"]);
93+
// BAD (and good. TODO: Move to separate lines)
94+
res.setHeader("Set-Cookie", ["type=ninja; secure", "authKey=foo"]);
9595
res.writeHead(200, { 'Content-Type': 'text/plain' });
9696
res.end('ok');
9797
});

0 commit comments

Comments
 (0)