ai-secure-audit 0.0.14 → 0.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/package.json +1 -1
- package/rules/express-route-no-rate-limit.yml +2 -0
- package/rules/express-session-lookup-plain-object.yml +2 -0
- package/rules/express-vote-route-no-identity.yml +2 -0
- package/rules/js-code-from-text.js +136 -0
- package/rules/js-code-from-text.yml +81 -0
- package/rules/js-html-from-user-text.js +206 -0
- package/rules/js-html-from-user-text.yml +143 -0
- package/rules/nestjs-route-no-throttle.yml +2 -0
- package/rules/sql-built-from-parameter.js +115 -0
- package/rules/sql-built-from-parameter.yml +81 -0
- package/rules/sql-built-from-request.ts +196 -0
- package/rules/sql-built-from-request.yml +103 -0
- package/src/cli.js +6 -2
- package/src/coverage.js +100 -5
- package/src/findings.js +1 -1
- package/src/live/ports.js +136 -0
- package/src/live.js +30 -7
- package/src/report-simple.js +1 -0
- package/src/report.js +2 -1
- package/src/scan.js +4 -1
- package/src/scanners/opengrep.js +44 -1
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
> Status: 0.0.x on npm runs the basic check with `npx ai-secure-audit`: secrets, libraries with known holes and, from
|
|
4
4
|
> 0.0.5, code rules for routes anyone can repeat without limit and, from 0.0.9, for session checks that a built-in
|
|
5
5
|
> name such as `constructor` passes. From 0.0.10 it also checks Dockerfiles with trivy (Compose files are not read yet,
|
|
6
|
-
> and the report says so), and from 0.0.11 it warns when a Docker build would copy these results into the image, and from 0.0.13 `--report simple` writes a short to-do list in plain words (`report-simple.md`) and `live` checks one host you list in `targets.yml`: its TLS with testssl.sh, and with `--zap` its web headers and cookies with OWASP ZAP in Docker. `live` has only been tried on servers on the same machine and a GitHub test runner, never on a real remote site, and it does not run on Windows (TLS part) or without Docker (ZAP part). From 0.0.14 a secret in a file that was moved with `git mv` is no longer called "not committed yet" (that wording told people they could skip rotating a key that is already in git). v0.1 is not released yet.
|
|
6
|
+
> and the report says so), and from 0.0.11 it warns when a Docker build would copy these results into the image, and from 0.0.13 `--report simple` writes a short to-do list in plain words (`report-simple.md`) and `live` checks one host you list in `targets.yml`: its TLS with testssl.sh, and with `--zap` its web headers and cookies with OWASP ZAP in Docker. `live` has only been tried on servers on the same machine and a GitHub test runner, never on a real remote site, and it does not run on Windows (TLS part) or without Docker (ZAP part). From 0.0.15 `live --ports` also checks which of 34 well-known risky ports (databases, Docker API, RDP, SMB, dev ports) accept a connection from the computer that runs it, and says what the address shows (a loopback or private address is not the internet). From 0.0.14 a secret in a file that was moved with `git mv` is no longer called "not committed yet" (that wording told people they could skip rotating a key that is already in git). From 0.0.16 code rules also look for SQL built from request values or from a db helper's parameter, text people type put into HTML without escaping, and text run as code (`eval`, `new Function`, `vm`), and the report names what the code rules did not read: languages with no rules, folders named `test`, `*.min.js`, and files the code checker could not read or read only in part. v0.1 is not released yet.
|
|
7
7
|
>
|
|
8
8
|
> **มือใหม่เริ่มตรงนี้:** [learn/](learn/) ควรตรวจอะไรก่อน เพราะอะไร และแก้ยังไง (ภาษาไทย)
|
|
9
9
|
>
|
|
@@ -93,7 +93,7 @@ SHA-256 checksums. The check reads only your project folder and never uploads yo
|
|
|
93
93
|
| Passwords, API keys and tokens in the code, in files not committed yet, in git history and in build output (v0.1) | gitleaks | MIT |
|
|
94
94
|
| Libraries with known vulnerabilities (v0.2) | osv-scanner (offline database) | Apache-2.0 |
|
|
95
95
|
| Dockerfile and config mistakes (0.0.10): Dockerfile, Kubernetes, Helm, Terraform, CloudFormation, Azure ARM, Ansible. trivy does not read Docker Compose files; the report names any it found as not checked | trivy config | Apache-2.0 |
|
|
96
|
-
| Code mistakes AI-generated apps often make (see below), one rule at a time: sign-in, sign-up and join routes with no limit on repeats (Express 0.0.5, NestJS 0.0.6), vote and like routes that never check who is voting (0.0.6), session, token or admin checks that a built-in name like `constructor` passes (0.0.9); the rest planned | opengrep + this kit's own rules | LGPL-2.1 (rules: Apache-2.0) |
|
|
96
|
+
| Code mistakes AI-generated apps often make (see below), one rule at a time: sign-in, sign-up and join routes with no limit on repeats (Express 0.0.5, NestJS 0.0.6), vote and like routes that never check who is voting (0.0.6), session, token or admin checks that a built-in name like `constructor` passes (0.0.9), SQL built from request values or a db helper's parameter, text people type put into HTML without escaping, and text run as code with `eval`, `new Function` or `vm` (0.0.16); the rest planned | opengrep + this kit's own rules | LGPL-2.1 (rules: Apache-2.0) |
|
|
97
97
|
|
|
98
98
|
Common mistakes the kit's own rules look for: sign-in, sign-up and join routes that anyone can call without limit
|
|
99
99
|
(Express and NestJS), vote routes that never check who is voting, session checks done on a plain object (a cookie
|
package/package.json
CHANGED
|
@@ -9,6 +9,8 @@ rules:
|
|
|
9
9
|
handler) and lock out after repeated failures. If a limiter is applied somewhere this rule cannot see (another
|
|
10
10
|
file, the reverse proxy), confirm it actually covers this route.
|
|
11
11
|
metadata:
|
|
12
|
+
# Shown in the report's list of what the code rules look for.
|
|
13
|
+
summary: 'sign-in, sign-up, join or code routes with no rate limit (Express)'
|
|
12
14
|
category: code
|
|
13
15
|
cwe: 'CWE-307: Improper Restriction of Excessive Authentication Attempts'
|
|
14
16
|
asvs: 'OWASP ASVS 5.0 V6.3.1 (brute force), V2.4.1 (anti-automation)'
|
|
@@ -9,6 +9,8 @@ rules:
|
|
|
9
9
|
or an object made with Object.create(null), check with Object.hasOwn first, or look up a hash of the token
|
|
10
10
|
(which also keeps raw tokens out of storage).
|
|
11
11
|
metadata:
|
|
12
|
+
# Shown in the report's list of what the code rules look for.
|
|
13
|
+
summary: 'session, token or admin checks that look a request value up in a plain object (Express)'
|
|
12
14
|
category: code
|
|
13
15
|
cwe: 'CWE-287: Improper Authentication'
|
|
14
16
|
asvs: 'OWASP ASVS 5.0 V7.2.1 (session tokens verified by a trusted back end)'
|
|
@@ -9,6 +9,8 @@ rules:
|
|
|
9
9
|
identity on the server, and add a per-IP limit. Remembering "already voted" in the browser (localStorage, a
|
|
10
10
|
cookie the server does not check) does not stop a script.
|
|
11
11
|
metadata:
|
|
12
|
+
# Shown in the report's list of what the code rules look for.
|
|
13
|
+
summary: 'vote, like or rating routes that never check who sends them (Express)'
|
|
12
14
|
category: code
|
|
13
15
|
cwe: 'CWE-799: Improper Control of Interaction Frequency'
|
|
14
16
|
asvs: 'OWASP ASVS 5.0 V2.4.1 (anti-automation)'
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
// Test cases for js-code-from-text. Run: opengrep scan --test rules/
|
|
2
|
+
// Written before the rule. Text that is run as code: eval, new Function, setTimeout with a string, Node's vm.
|
|
3
|
+
const vm = require('vm');
|
|
4
|
+
const express = require('express');
|
|
5
|
+
const app = express();
|
|
6
|
+
|
|
7
|
+
// --- eval with anything but a literal ---
|
|
8
|
+
|
|
9
|
+
app.post('/calc', (req, res) => {
|
|
10
|
+
// ruleid: js-code-from-text
|
|
11
|
+
res.json({ result: eval(req.body.expression) });
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
function profileName(user) {
|
|
15
|
+
const code = user.username.substring(2, user.username.length - 1);
|
|
16
|
+
// ruleid: js-code-from-text
|
|
17
|
+
return eval(code);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function onMessage(msg) {
|
|
21
|
+
// ruleid: js-code-from-text
|
|
22
|
+
window.eval(msg.data);
|
|
23
|
+
// ruleid: js-code-from-text
|
|
24
|
+
globalThis.eval(`run(${msg.data})`);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// --- new Function with a body built from data ---
|
|
28
|
+
|
|
29
|
+
function makeFilter(rule) {
|
|
30
|
+
// ruleid: js-code-from-text
|
|
31
|
+
return new Function('row', 'return ' + rule.expr);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function makeFormula(f) {
|
|
35
|
+
// ruleid: js-code-from-text
|
|
36
|
+
return Function('x', f.body);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// --- setTimeout or setInterval given a string ---
|
|
40
|
+
|
|
41
|
+
function later(name) {
|
|
42
|
+
// ruleid: js-code-from-text
|
|
43
|
+
setTimeout('greet("' + name + '")', 100);
|
|
44
|
+
// ruleid: js-code-from-text
|
|
45
|
+
setInterval(`tick(${name})`, 1000);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// --- Node vm: it is not a sandbox ---
|
|
49
|
+
|
|
50
|
+
app.post('/b2b', (req, res) => {
|
|
51
|
+
const sandbox = { safeEval: require('notevil'), orderLinesData: req.body.orderLinesData };
|
|
52
|
+
vm.createContext(sandbox);
|
|
53
|
+
// ruleid: js-code-from-text
|
|
54
|
+
vm.runInContext('safeEval(orderLinesData)', sandbox, { timeout: 2000 });
|
|
55
|
+
res.end();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
app.post('/script', (req, res) => {
|
|
59
|
+
// ruleid: js-code-from-text
|
|
60
|
+
const out = vm.runInNewContext(req.body.script, {});
|
|
61
|
+
// ruleid: js-code-from-text
|
|
62
|
+
const s = new vm.Script(req.body.source);
|
|
63
|
+
// ruleid: js-code-from-text
|
|
64
|
+
vm.runInThisContext(`(${req.body.fn})`);
|
|
65
|
+
res.json({ out, s });
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// --- Not run as code ---
|
|
69
|
+
|
|
70
|
+
function safe(text, rows) {
|
|
71
|
+
// ok: js-code-from-text
|
|
72
|
+
const data = JSON.parse(text);
|
|
73
|
+
// ok: js-code-from-text
|
|
74
|
+
const two = eval('1 + 1');
|
|
75
|
+
// ok: js-code-from-text
|
|
76
|
+
window.eval('init()');
|
|
77
|
+
// ok: js-code-from-text
|
|
78
|
+
const dynamicImport = new Function('specifier', 'return import(specifier)');
|
|
79
|
+
// ok: js-code-from-text
|
|
80
|
+
setTimeout(() => rows.push(data), 100);
|
|
81
|
+
// ok: js-code-from-text
|
|
82
|
+
setTimeout(refresh, 1000);
|
|
83
|
+
// ok: js-code-from-text
|
|
84
|
+
setInterval(function () { refresh(); }, 1000);
|
|
85
|
+
return [two, dynamicImport];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function parseInVm(data, sandbox) {
|
|
89
|
+
// vm used only to put a time limit on a parser: the code is fixed and does not evaluate the data.
|
|
90
|
+
// ok: js-code-from-text
|
|
91
|
+
return vm.runInContext('JSON.stringify(yaml.load(data))', sandbox, { timeout: 2000 });
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function browser(page, el) {
|
|
95
|
+
// ok: js-code-from-text
|
|
96
|
+
await page.evaluate(() => document.title);
|
|
97
|
+
// ok: js-code-from-text
|
|
98
|
+
const v = el.evaluate('//a', el);
|
|
99
|
+
return v;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Redis EVAL runs a Lua script on the Redis server, not JavaScript.
|
|
103
|
+
async function counter(redisClient, luaScript, key) {
|
|
104
|
+
// ok: js-code-from-text
|
|
105
|
+
return redisClient.eval(luaScript, 1, key);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// A compiled vm.Script is run with a context, not with code.
|
|
109
|
+
function runCompiled(ctx) {
|
|
110
|
+
const script = new vm.Script('total + 1');
|
|
111
|
+
// ok: js-code-from-text
|
|
112
|
+
return script.runInContext(ctx);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function laterParts(name, fnName) {
|
|
116
|
+
// ruleid: js-code-from-text
|
|
117
|
+
setTimeout('greet' + name, 10);
|
|
118
|
+
// ruleid: js-code-from-text
|
|
119
|
+
setTimeout(fnName + '()', 10);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// --- A function given to setTimeout is not a string, even if it builds strings inside ---
|
|
123
|
+
function poll(el, name) {
|
|
124
|
+
// ok: js-code-from-text
|
|
125
|
+
setTimeout(() => { el.textContent = 'Hi ' + name; }, 0);
|
|
126
|
+
// ok: js-code-from-text
|
|
127
|
+
setInterval(function () { el.title = `${name} is online`; }, 1000);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Other functions given a built string are not timers.
|
|
131
|
+
function logs(name) {
|
|
132
|
+
// ok: js-code-from-text
|
|
133
|
+
console.log('Hi ' + name);
|
|
134
|
+
// ruleid: js-code-from-text
|
|
135
|
+
window.setTimeout('show(' + name + ')', 5);
|
|
136
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
rules:
|
|
2
|
+
- id: js-code-from-text
|
|
3
|
+
languages: [javascript, typescript]
|
|
4
|
+
severity: ERROR
|
|
5
|
+
message: >-
|
|
6
|
+
Text is run as JavaScript code here (eval, new Function, setTimeout or setInterval with a string, or Node's vm).
|
|
7
|
+
If any part of that text can come from a person (a form, a URL, a saved profile, a file they upload), they can
|
|
8
|
+
run their own code on your server or in other people's browsers: read secrets and files, change data, or take
|
|
9
|
+
over the machine. Do not run text as code: parse data with JSON.parse, compute with ordinary functions, and pass
|
|
10
|
+
a function to setTimeout instead of a string. Node's vm module is not a security boundary (its own docs say so),
|
|
11
|
+
so moving eval into vm does not make it safe.
|
|
12
|
+
metadata:
|
|
13
|
+
category: code
|
|
14
|
+
# Shown in the report's list of what the code rules look for.
|
|
15
|
+
summary: 'text run as code (eval, new Function, setTimeout with a string, Node vm)'
|
|
16
|
+
cwe: "CWE-95: Improper Neutralization of Directives in Dynamically Evaluated Code ('Eval Injection')"
|
|
17
|
+
asvs: 'OWASP ASVS 5.0 V1.3.2 (no eval or dynamic code execution)'
|
|
18
|
+
confidence: HIGH
|
|
19
|
+
limitations: >-
|
|
20
|
+
Reports every eval of something that is not a fixed string, whether or not a person can reach it (a server
|
|
21
|
+
that evals a sum it made itself is reported too: replace it anyway). vm code that is a fixed string is
|
|
22
|
+
reported only when it calls an eval-like function itself. Does not see code run through other libraries
|
|
23
|
+
(a template engine compiling user templates, a sandbox package).
|
|
24
|
+
pattern-either:
|
|
25
|
+
# 1. eval of anything but a string literal.
|
|
26
|
+
- patterns:
|
|
27
|
+
- pattern: eval($X)
|
|
28
|
+
- pattern-not: eval("...")
|
|
29
|
+
- patterns:
|
|
30
|
+
- pattern: $W.eval($X, ...)
|
|
31
|
+
- pattern-not: $W.eval("...", ...)
|
|
32
|
+
# Only the global eval: redisClient.eval(script, ...) runs Lua on Redis, not JavaScript.
|
|
33
|
+
- metavariable-regex:
|
|
34
|
+
metavariable: $W
|
|
35
|
+
regex: ^(window|globalThis|global|self|win)$
|
|
36
|
+
# 2. new Function / Function whose body (the last argument) is not a string literal.
|
|
37
|
+
- patterns:
|
|
38
|
+
- pattern-either:
|
|
39
|
+
- pattern: new Function(..., $BODY)
|
|
40
|
+
- pattern: Function(..., $BODY)
|
|
41
|
+
- pattern-not: new Function(..., "...")
|
|
42
|
+
- pattern-not: Function(..., "...")
|
|
43
|
+
# 3. setTimeout or setInterval given a string that is built from data. The first argument itself must be the
|
|
44
|
+
# string: a function that builds strings inside it is not matched.
|
|
45
|
+
- patterns:
|
|
46
|
+
- pattern-either:
|
|
47
|
+
- pattern: $TIMER("..." + $Y, ...)
|
|
48
|
+
- pattern: $TIMER($Y + "...", ...)
|
|
49
|
+
- pattern: $TIMER(`...${$Y}...`, ...)
|
|
50
|
+
- pattern: $W.$TIMER("..." + $Y, ...)
|
|
51
|
+
- pattern: $W.$TIMER($Y + "...", ...)
|
|
52
|
+
- pattern: $W.$TIMER(`...${$Y}...`, ...)
|
|
53
|
+
- metavariable-regex:
|
|
54
|
+
metavariable: $TIMER
|
|
55
|
+
regex: ^(setTimeout|setInterval)$
|
|
56
|
+
# 4. Node vm: code that is not a fixed string ...
|
|
57
|
+
- patterns:
|
|
58
|
+
- pattern-either:
|
|
59
|
+
- pattern: $VM.runInContext($CODE, ...)
|
|
60
|
+
- pattern: $VM.runInNewContext($CODE, ...)
|
|
61
|
+
- pattern: $VM.runInThisContext($CODE, ...)
|
|
62
|
+
- pattern: $VM.compileFunction($CODE, ...)
|
|
63
|
+
- pattern: new $VM.Script($CODE, ...)
|
|
64
|
+
- metavariable-regex:
|
|
65
|
+
metavariable: $VM
|
|
66
|
+
regex: (?i)^\w*vm$
|
|
67
|
+
- pattern-not: $VM.$RUN("...", ...)
|
|
68
|
+
- pattern-not: new $VM.Script("...", ...)
|
|
69
|
+
# ... or fixed code that evals something itself (vm.runInContext('safeEval(data)', sandbox)).
|
|
70
|
+
- patterns:
|
|
71
|
+
- pattern-either:
|
|
72
|
+
- pattern: $VM.runInContext("$CODE", ...)
|
|
73
|
+
- pattern: $VM.runInNewContext("$CODE", ...)
|
|
74
|
+
- pattern: $VM.runInThisContext("$CODE", ...)
|
|
75
|
+
- pattern: new $VM.Script("$CODE", ...)
|
|
76
|
+
- metavariable-regex:
|
|
77
|
+
metavariable: $VM
|
|
78
|
+
regex: (?i)^\w*vm$
|
|
79
|
+
- metavariable-regex:
|
|
80
|
+
metavariable: $CODE
|
|
81
|
+
regex: (?i).*(eval|Function)\s*\(
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
// Test cases for js-html-from-user-text. Run: opengrep scan --test rules/
|
|
2
|
+
const esc = (s) => String(s).replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`);
|
|
3
|
+
|
|
4
|
+
// Comment board: other people's comments rendered as HTML.
|
|
5
|
+
function renderComments(list, comments) {
|
|
6
|
+
// ruleid: js-html-from-user-text
|
|
7
|
+
list.innerHTML = comments.map((c) => `<li><b>${c.authorName}</b> ${c.text}</li>`).join('');
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// Shop reviews, destructured fields.
|
|
11
|
+
function renderReviews(box, reviews) {
|
|
12
|
+
// ruleid: js-html-from-user-text
|
|
13
|
+
box.innerHTML = reviews.map(({ title, stars }) => `<h3>${title}</h3><span>${stars}</span>`).join('');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// Chat: a message added at the end.
|
|
17
|
+
function addMessage(log, msg) {
|
|
18
|
+
// ruleid: js-html-from-user-text
|
|
19
|
+
log.insertAdjacentHTML('beforeend', '<p>' + msg.message + '</p>');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Profile: a field shown on its own.
|
|
23
|
+
function showBio(el, profile) {
|
|
24
|
+
// ruleid: js-html-from-user-text
|
|
25
|
+
el.innerHTML = profile.bio;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// A search box echoed back.
|
|
29
|
+
function echoSearch(out, input) {
|
|
30
|
+
// ruleid: js-html-from-user-text
|
|
31
|
+
out.innerHTML = `No results for <i>${input.value}</i>`;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// jQuery.
|
|
35
|
+
function renderTask(task) {
|
|
36
|
+
// ruleid: js-html-from-user-text
|
|
37
|
+
$('#tasks').append(`<li>${task.description}</li>`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Server page built from the query string.
|
|
41
|
+
app.get('/hello', (req, res) => {
|
|
42
|
+
// ruleid: js-html-from-user-text
|
|
43
|
+
res.send(`<h1>Hello ${req.query.name}</h1>`);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// Escaped first.
|
|
47
|
+
function renderCommentsSafe(list, comments) {
|
|
48
|
+
// ok: js-html-from-user-text
|
|
49
|
+
list.innerHTML = comments.map((c) => `<li><b>${esc(c.authorName)}</b> ${esc(c.text)}</li>`).join('');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Set as text, not HTML.
|
|
53
|
+
function showBioSafe(el, profile) {
|
|
54
|
+
// ok: js-html-from-user-text
|
|
55
|
+
el.textContent = profile.bio;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Only fixed markup, numbers and the app's own helpers.
|
|
59
|
+
function renderCounts(bar, s, icon) {
|
|
60
|
+
// ok: js-html-from-user-text
|
|
61
|
+
bar.innerHTML = `${icon('alert')}<span>${s.total} votes</span><span>${s.percent}%</span>`;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// A class name or tag name is not text people type.
|
|
65
|
+
function renderBadge(el, item) {
|
|
66
|
+
// ok: js-html-from-user-text
|
|
67
|
+
el.innerHTML = `<span class="${item.className}">${item.count}</span>`;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// lit-html escapes values in its html tag.
|
|
71
|
+
function renderLit(el, user) {
|
|
72
|
+
// ok: js-html-from-user-text
|
|
73
|
+
el.innerHTML = html`<p>${user.name}</p>`;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Cleared.
|
|
77
|
+
function clear(el) {
|
|
78
|
+
// ok: js-html-from-user-text
|
|
79
|
+
el.innerHTML = '';
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// React.
|
|
83
|
+
function Post({ post }) {
|
|
84
|
+
// ruleid: js-html-from-user-text
|
|
85
|
+
return <div className="body" dangerouslySetInnerHTML={{ __html: post.content }} />;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function PostSafe({ post }) {
|
|
89
|
+
// ok: js-html-from-user-text
|
|
90
|
+
return <div className="body" dangerouslySetInnerHTML={{ __html: DOMPurify.sanitize(post.content) }} />;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function Static() {
|
|
94
|
+
// ok: js-html-from-user-text
|
|
95
|
+
return <div dangerouslySetInnerHTML={{ __html: '<b>fixed</b>' }} />;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function Text({ post }) {
|
|
99
|
+
// ok: js-html-from-user-text
|
|
100
|
+
return <div>{post.content}</div>;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// The app's own tab labels and a dialog body the caller built: bare variables are not matched.
|
|
104
|
+
function renderTabs(root, TABS, body) {
|
|
105
|
+
// ok: js-html-from-user-text
|
|
106
|
+
root.innerHTML = TABS.map(([k, label]) => `<button data-tab="${k}">${label}</button>`).join('') + `<div>${body}</div>`;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// --- Known false findings (todook): the rule judges by the field's name, so it cannot tell these from user text ---
|
|
110
|
+
// A boolean flag named text, put in an ARIA state attribute that only takes true or false.
|
|
111
|
+
function toggles(c) {
|
|
112
|
+
// todook: js-html-from-user-text
|
|
113
|
+
return `<button aria-pressed="${c.text}">text</button><button aria-checked="${c.title}">title</button>`;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Labels the app wrote itself, in an array of object literals in the same file.
|
|
117
|
+
const STEPS = [{ title: 'Name the event', go: 'Open settings' }, { title: 'Add topics', go: 'Open topics' }];
|
|
118
|
+
function steps() {
|
|
119
|
+
// todook: js-html-from-user-text
|
|
120
|
+
return STEPS.map((x) => `<li><b>${x.title}</b><button>${x.go}</button></li>`).join('');
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// --- One template with nested templates is one finding, at the line where it starts ---
|
|
124
|
+
function card(u, x) {
|
|
125
|
+
// ruleid: js-html-from-user-text
|
|
126
|
+
return `<section class="card">
|
|
127
|
+
<p>${x.on ? `<b>on</b>` : ''}</p>
|
|
128
|
+
${x.more ? `<div class="more">more</div>` : ''}
|
|
129
|
+
<i>${u.name}</i>
|
|
130
|
+
</section>`;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// --- Angular: bypassSecurityTrust* turns Angular's own escaping off for the value ---
|
|
134
|
+
class SearchResult {
|
|
135
|
+
show(sanitizer, row, queryParam) {
|
|
136
|
+
// ruleid: js-html-from-user-text
|
|
137
|
+
row.description = sanitizer.bypassSecurityTrustHtml(row.description);
|
|
138
|
+
// ruleid: js-html-from-user-text
|
|
139
|
+
this.searchValue = this.sanitizer.bypassSecurityTrustHtml(queryParam);
|
|
140
|
+
// ruleid: js-html-from-user-text
|
|
141
|
+
this.link = this.sanitizer.bypassSecurityTrustUrl(row.url);
|
|
142
|
+
// ruleid: js-html-from-user-text
|
|
143
|
+
this.frame = this.sanitizer.bypassSecurityTrustResourceUrl(this.videoUrl);
|
|
144
|
+
// ok: js-html-from-user-text
|
|
145
|
+
this.badge = this.sanitizer.bypassSecurityTrustHtml('<b>new</b>');
|
|
146
|
+
// ok: js-html-from-user-text
|
|
147
|
+
this.body = this.sanitizer.bypassSecurityTrustHtml(DOMPurify.sanitize(row.body));
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// --- An escaping tag named html is not jQuery's .html(): opengrep reads html`...` as a call ---
|
|
152
|
+
// Imported by destructuring, which makes opengrep read html`...` as a method call on the module.
|
|
153
|
+
function stage(g) {
|
|
154
|
+
const { html } = require('./lib/html');
|
|
155
|
+
// ok: js-html-from-user-text
|
|
156
|
+
return html`<div class="stage">
|
|
157
|
+
${media(g, { alt: g.link ? '' : 'Cover of ' + g.title })}
|
|
158
|
+
<h2>${g.title}</h2>
|
|
159
|
+
</div>`;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// jQuery on a stored selection or a $-named variable is still jQuery.
|
|
163
|
+
function jq($list, row) {
|
|
164
|
+
// ruleid: js-html-from-user-text
|
|
165
|
+
$list.html('<li>' + row.name + '</li>');
|
|
166
|
+
// ruleid: js-html-from-user-text
|
|
167
|
+
jQuery('#out').prepend('<b>' + row.comment + '</b>');
|
|
168
|
+
// ruleid: js-html-from-user-text
|
|
169
|
+
$('#who').html('<i>' + row.username + '</i>');
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// --- A component or function that takes a record is not itself the HTML: only a template inside it can be ---
|
|
173
|
+
const Panel = ({ title, items }) => {
|
|
174
|
+
const head = 'Panel';
|
|
175
|
+
// ruleid: js-html-from-user-text
|
|
176
|
+
const html = `<h2>${items[0].title}</h2>`;
|
|
177
|
+
return html;
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
// --- XML is not a web page ---
|
|
181
|
+
function invoiceXml(inv) {
|
|
182
|
+
// ok: js-html-from-user-text
|
|
183
|
+
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
184
|
+
<Invoice><Buyer>${inv.customer_name}</Buyer></Invoice>`;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// A record field in an arrow, but the template has no tag or the field is escaped.
|
|
188
|
+
function labels(rows) {
|
|
189
|
+
// ok: js-html-from-user-text
|
|
190
|
+
const plain = rows.map(({ title }) => `Title: ${title}`);
|
|
191
|
+
// ok: js-html-from-user-text
|
|
192
|
+
const safe = rows.map(({ title }) => `<li>${esc(title)}</li>`);
|
|
193
|
+
return [plain, safe];
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// --- A value handed to another function first: that function may escape it (a page() that escapes the title) ---
|
|
197
|
+
app.get('/book', (req, res) => {
|
|
198
|
+
// ok: js-html-from-user-text
|
|
199
|
+
res.send(page(res, `Booking ${settings.serviceName}`, '<h1>Book</h1>'));
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
// A list joined into markup by concatenation is still the markup itself.
|
|
203
|
+
function names(el, rows) {
|
|
204
|
+
// ruleid: js-html-from-user-text
|
|
205
|
+
el.innerHTML = rows.map(({ name }) => '<li>' + name).join('');
|
|
206
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
rules:
|
|
2
|
+
- id: js-html-from-user-text
|
|
3
|
+
languages: [javascript, typescript]
|
|
4
|
+
severity: WARNING
|
|
5
|
+
message: >-
|
|
6
|
+
Text that people type (a name, comment, note, title or message) is put into HTML without escaping. If someone
|
|
7
|
+
types <img src=x onerror=...> as their name, the browser runs it for everyone who opens the page: it can read
|
|
8
|
+
what they see, act as them, or send their session away. Escape the text before it goes into HTML (a small
|
|
9
|
+
esc() that replaces & < > " ' with entities), or set it with textContent instead of innerHTML. If the field
|
|
10
|
+
must hold formatted HTML, clean it with DOMPurify first.
|
|
11
|
+
metadata:
|
|
12
|
+
category: code
|
|
13
|
+
cwe: "CWE-79: Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting')"
|
|
14
|
+
asvs: 'OWASP ASVS 5.0 V1.2.1 (output encoding for the HTML context)'
|
|
15
|
+
# Shown in the report's list of what the code rules look for.
|
|
16
|
+
summary: 'text people type (names, comments, titles) put into HTML without escaping, React dangerouslySetInnerHTML, Angular bypassSecurityTrust*'
|
|
17
|
+
confidence: MEDIUM
|
|
18
|
+
limitations: >-
|
|
19
|
+
Judges by the field's name (name, title, text, comment, note, description, message, ...), so it misses text
|
|
20
|
+
kept under other names and can flag a field the server already escaped before saving. A bare variable is
|
|
21
|
+
matched only when it is taken out of a record in an arrow function's parameters. Does not follow a value
|
|
22
|
+
through other variables or functions. Raw output in .vue, .svelte and template files (EJS, Handlebars, Pug)
|
|
23
|
+
is not checked by this rule.
|
|
24
|
+
patterns:
|
|
25
|
+
- pattern-either:
|
|
26
|
+
# 1. A template literal that builds HTML (it has a tag) with a user-text field placed straight in it,
|
|
27
|
+
# wherever it goes: helper functions usually return the markup and the caller sets innerHTML.
|
|
28
|
+
# esc(x.name) or String(x.name).replace(...) is a call, so it is not matched. The finding is at the line
|
|
29
|
+
# where the template that holds the field starts, so a template with nested templates is one finding.
|
|
30
|
+
- patterns:
|
|
31
|
+
- pattern: $T
|
|
32
|
+
# Cheap test first: $T is a template literal itself (not a call or function around one), it has a
|
|
33
|
+
# tag so it is HTML, and it is not an XML document. Only then look for a field inside it.
|
|
34
|
+
- metavariable-regex:
|
|
35
|
+
metavariable: $T
|
|
36
|
+
regex: &html_template '(?s)`(?!\s*<\?xml).*?<[a-zA-Z]'
|
|
37
|
+
- metavariable-pattern:
|
|
38
|
+
metavariable: $T
|
|
39
|
+
patterns:
|
|
40
|
+
- pattern: "`...${$O.$F}...`"
|
|
41
|
+
- metavariable-regex:
|
|
42
|
+
metavariable: $F
|
|
43
|
+
regex: &fields (?i)^((?!class|tag|node|host|path|icon|event|field|prop|key|type|method|font|attr|css|var|func|fn|param)[a-z_]*(name|title|text|message|msg|comment|note|desc|description|content|bio|caption|review|feedback|reply|answer|question|reason|address|intro|greeting|subject|nickname|username)s?|value|query|about|innertext|textcontent)$
|
|
44
|
+
# A tagged template (lit-html `html`, a sanitizing tag) does its own escaping. opengrep 1.33.1 already
|
|
45
|
+
# reads a tagged template as a call, so this changes nothing there; kept in case another version does not.
|
|
46
|
+
- pattern-not-inside: $TAG`...`
|
|
47
|
+
# 1b. A field taken out of a record: rows.map(({ title }) => `...${title}...`). Only an arrow whose body is the
|
|
48
|
+
# template itself; a bare variable on its own is not matched (in real code it is as often a UI string or
|
|
49
|
+
# prebuilt HTML).
|
|
50
|
+
- patterns:
|
|
51
|
+
- pattern: "({..., $F, ...}) => $BODY"
|
|
52
|
+
- metavariable-regex:
|
|
53
|
+
metavariable: $F
|
|
54
|
+
regex: *fields
|
|
55
|
+
- metavariable-regex:
|
|
56
|
+
metavariable: $BODY
|
|
57
|
+
regex: *html_template
|
|
58
|
+
- metavariable-pattern:
|
|
59
|
+
metavariable: $BODY
|
|
60
|
+
patterns:
|
|
61
|
+
- pattern: "`...${$F}...`"
|
|
62
|
+
# 2. Put into an HTML sink by concatenation or a template without a tag.
|
|
63
|
+
- patterns:
|
|
64
|
+
- pattern-either:
|
|
65
|
+
- pattern: $EL.innerHTML = $X
|
|
66
|
+
- pattern: $EL.outerHTML = $X
|
|
67
|
+
- pattern: $EL.innerHTML += $X
|
|
68
|
+
- pattern: $EL.insertAdjacentHTML($POS, $X)
|
|
69
|
+
- pattern: document.write($X)
|
|
70
|
+
- pattern: document.writeln($X)
|
|
71
|
+
# jQuery only: $(...), jQuery(...) or a variable named with a leading $. opengrep reads a tagged
|
|
72
|
+
# template html`...` as a call named html, so a bare $JQ.html would match escaping tags.
|
|
73
|
+
- patterns:
|
|
74
|
+
- pattern-either:
|
|
75
|
+
- pattern: $JQ.html($X)
|
|
76
|
+
- pattern: $JQ.append($X)
|
|
77
|
+
- pattern: $JQ.prepend($X)
|
|
78
|
+
- pattern-either:
|
|
79
|
+
- pattern: $(...).$M($X)
|
|
80
|
+
- pattern: jQuery(...).$M($X)
|
|
81
|
+
- patterns:
|
|
82
|
+
- pattern: $JQ.$M($X)
|
|
83
|
+
- metavariable-regex:
|
|
84
|
+
metavariable: $JQ
|
|
85
|
+
regex: ^\$\w+$
|
|
86
|
+
- patterns:
|
|
87
|
+
- pattern: $RES.send($X)
|
|
88
|
+
- metavariable-regex:
|
|
89
|
+
metavariable: $RES
|
|
90
|
+
regex: ^(res|response)$
|
|
91
|
+
# $X is the markup itself, or a list joined into markup (rows.map(...).join('')). A value handed to
|
|
92
|
+
# another function first (page(title, body), layout(...)) is not searched: that function may escape it.
|
|
93
|
+
- metavariable-regex:
|
|
94
|
+
metavariable: $X
|
|
95
|
+
regex: '(?s)(?![\w$.]+\s*\()|[\w$.]*\.(?:map|join|concat|reduce|filter)\s*\('
|
|
96
|
+
- metavariable-pattern:
|
|
97
|
+
metavariable: $X
|
|
98
|
+
patterns:
|
|
99
|
+
- pattern-either:
|
|
100
|
+
# A template with a tag is already found by 1.
|
|
101
|
+
- patterns:
|
|
102
|
+
- pattern: "`...${$O.$F}...`"
|
|
103
|
+
- pattern-not-regex: '<[a-zA-Z]'
|
|
104
|
+
- pattern: $S + $O.$F
|
|
105
|
+
- pattern: $O.$F + $S
|
|
106
|
+
- pattern: "({..., $F, ...}) => $S + $F"
|
|
107
|
+
- metavariable-regex:
|
|
108
|
+
metavariable: $F
|
|
109
|
+
regex: *fields
|
|
110
|
+
- pattern-not: $EL.innerHTML = $TAG`...`
|
|
111
|
+
- pattern-not: $RES.send($TAG`...`)
|
|
112
|
+
# 3. The field on its own.
|
|
113
|
+
- patterns:
|
|
114
|
+
- pattern-either:
|
|
115
|
+
- pattern: $EL.innerHTML = $O.$F
|
|
116
|
+
- pattern: $EL.outerHTML = $O.$F
|
|
117
|
+
- pattern: $EL.insertAdjacentHTML($POS, $O.$F)
|
|
118
|
+
- pattern: document.write($O.$F)
|
|
119
|
+
- metavariable-regex:
|
|
120
|
+
metavariable: $F
|
|
121
|
+
regex: *fields
|
|
122
|
+
# 4. React: anything but a literal or a sanitizer's result in __html.
|
|
123
|
+
- patterns:
|
|
124
|
+
- pattern: '<$TAGNAME dangerouslySetInnerHTML={{__html: $V}} />'
|
|
125
|
+
- pattern-not: '<$TAGNAME dangerouslySetInnerHTML={{__html: "..."}} />'
|
|
126
|
+
- pattern-not: '<$TAGNAME dangerouslySetInnerHTML={{__html: $SAN.sanitize(...)}} />'
|
|
127
|
+
- pattern-not: '<$TAGNAME dangerouslySetInnerHTML={{__html: sanitize(...)}} />'
|
|
128
|
+
- pattern-not: '<$TAGNAME dangerouslySetInnerHTML={{__html: sanitizeHtml(...)}} />'
|
|
129
|
+
- pattern-not: '<$TAGNAME dangerouslySetInnerHTML={{__html: xss(...)}} />'
|
|
130
|
+
# 5. Angular: bypassSecurityTrust* turns Angular's own escaping off for the value. Anything but a literal or
|
|
131
|
+
# a sanitizer's result is reported, whatever the field is called.
|
|
132
|
+
- patterns:
|
|
133
|
+
- pattern-either:
|
|
134
|
+
- pattern: $S.bypassSecurityTrustHtml($V)
|
|
135
|
+
- pattern: $S.bypassSecurityTrustScript($V)
|
|
136
|
+
- pattern: $S.bypassSecurityTrustStyle($V)
|
|
137
|
+
- pattern: $S.bypassSecurityTrustUrl($V)
|
|
138
|
+
- pattern: $S.bypassSecurityTrustResourceUrl($V)
|
|
139
|
+
- pattern-not: $S.$BYPASS("...")
|
|
140
|
+
- pattern-not: $S.$BYPASS($SAN.sanitize(...))
|
|
141
|
+
- pattern-not: $S.$BYPASS(sanitize(...))
|
|
142
|
+
- pattern-not: $S.$BYPASS(sanitizeHtml(...))
|
|
143
|
+
|
|
@@ -9,6 +9,8 @@ rules:
|
|
|
9
9
|
low limit, and lock out after repeated failures. If a limit is set somewhere this rule cannot see (the reverse
|
|
10
10
|
proxy), confirm it actually covers this route.
|
|
11
11
|
metadata:
|
|
12
|
+
# Shown in the report's list of what the code rules look for.
|
|
13
|
+
summary: 'sign-in, sign-up, join or code routes with no throttle (NestJS)'
|
|
12
14
|
category: code
|
|
13
15
|
cwe: 'CWE-307: Improper Restriction of Excessive Authentication Attempts'
|
|
14
16
|
asvs: 'OWASP ASVS 5.0 V6.3.1 (brute force), V2.4.1 (anti-automation)'
|