Skip to content

Commit a17d152

Browse files
authored
Merge branch 'js-team-sprint' into priv-file-polish
2 parents 6b0adf1 + bfb2e9d commit a17d152

File tree

8 files changed

+95
-18
lines changed

8 files changed

+95
-18
lines changed

change-notes/1.25/analysis-javascript.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
| Unsafe expansion of self-closing HTML tag (`js/unsafe-html-expansion`) | security, external/cwe/cwe-079, external/cwe/cwe-116 | Highlights potential XSS vulnerabilities caused by unsafe expansion of self-closing HTML tags. |
3838
| Unsafe shell command constructed from library input (`js/shell-command-constructed-from-input`) | correctness, security, external/cwe/cwe-078, external/cwe/cwe-088 | Highlights potential command injections due to a shell command being constructed from library inputs. Results are shown on LGTM by default. |
3939
| Exposure of private files (`js/exposure-of-private-files`) | security, external/cwe/cwe-200 | Highlights servers that serve private files. Results are shown on LGTM by default. |
40+
| Creating biased random numbers from a cryptographically secure source (`js/biased-cryptographic-random`) | security, external/cwe/cwe-327 | Highlights mathematical operations on cryptographically secure numbers that can create biased results. Results are shown on LGTM by default. |
4041
| Storage of sensitive information in build artifact (`js/build-artifact-leak`) | security, external/cwe/cwe-312 | Highlights storage of sensitive information in build artifacts. Results are shown on LGTM by default. |
4142
| Improper code sanitization (`js/bad-code-sanitization`) | security, external/cwe/cwe-094, external/cwe/cwe-079, external/cwe/cwe-116 | Highlights string concatenation where code is constructed without proper sanitization. Results are shown on LGTM by default. |
4243

javascript/ql/src/Security/CWE-327/BadRandomness.qhelp

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,62 @@
44
<qhelp>
55
<overview>
66
<p>
7-
Placeholder
7+
Generating secure random numbers can be an important part of creating a
8+
secure software system. This can be done using APIs that create
9+
cryptographically secure random numbers.
10+
</p>
11+
<p>
12+
However, using some mathematical operations on these cryptographically
13+
secure random numbers can create biased results, where some outcomes
14+
are more likely than others.
15+
Such biased results can make it easier for an attacker to guess the random
16+
numbers, and thereby break the security of the software system.
817
</p>
9-
1018
</overview>
1119
<recommendation>
12-
1320
<p>
14-
Placeholder.
21+
Be very careful not to introduce bias when performing mathematical operations
22+
on cryptographically secure random numbers.
23+
</p>
24+
<p>
25+
If possible, avoid performing mathematical operations on cryptographically secure
26+
random numbers at all, and use a preexisting library instead.
1527
</p>
16-
1728
</recommendation>
1829
<example>
19-
2030
<p>
21-
Placeholder
31+
The example below uses the modulo operator to create an array of 10 random digits
32+
using random bytes as the source for randomness.
2233
</p>
34+
<sample src="examples/bad-random.js" />
35+
<p>
36+
The random byte is a uniformly random value between 0 and 255, and thus the result
37+
from using the modulo operator is slightly more likely to be between 0 and 5 than
38+
between 6 and 9.
39+
</p>
40+
<p>
41+
The issue has been fixed in the code below by using a library that correctly generates
42+
cryptographically secure random values.
43+
</p>
44+
<sample src="examples/bad-random-fixed.js" />
45+
<p>
46+
Alternatively, the issue can be fixed by fixing the math in the original code.
47+
In the code below the random byte is discarded if the value is greater than or equal to 250.
48+
Thus the modulo operator is used on a uniformly random number between 0 and 249, which
49+
results in a uniformly random digit between 0 and 9.
50+
</p>
51+
<sample src="examples/bad-random-fixed2.js" />
2352

2453
</example>
2554

55+
2656
<references>
27-
<li>NIST, FIPS 140 Annex a: <a href="http://csrc.nist.gov/publications/fips/fips140-2/fips1402annexa.pdf"> Approved Security Functions</a>.</li>
28-
<li>NIST, SP 800-131A: <a href="http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar1.pdf"> Transitions: Recommendation for Transitioning the Use of Cryptographic Algorithms and Key Lengths</a>.</li>
57+
<li>Stack Overflow: <a href="https://stackoverflow.com/questions/3956478/understanding-randomness">Understanding “randomness”</a>.</li>
58+
<li>OWASP: <a href="https://owasp.org/www-community/vulnerabilities/Insecure_Randomness">Insecure Randomness</a>.</li>
2959
<li>OWASP: <a
3060
href="https://cheatsheetseries.owasp.org/cheatsheets/Cryptographic_Storage_Cheat_Sheet.html#rule---use-strong-approved-authenticated-encryption">Rule
3161
- Use strong approved cryptographic algorithms</a>.
3262
</li>
33-
<li>Stack Overflow: <a href="https://stackoverflow.com/questions/3956478/understanding-randomness">Understanding “randomness”</a>.</li>
3463
</references>
3564

3665
</qhelp>

javascript/ql/src/Security/CWE-327/BadRandomness.ql

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @name Creating biased random numbers from cryptographically secure source.
2+
* @name Creating biased random numbers from a cryptographically secure source.
33
* @description Some mathematical operations on random numbers can cause bias in
44
* the results and compromise security.
55
* @kind problem
@@ -132,6 +132,18 @@ DataFlow::Node goodRandom(DataFlow::SourceNode source) {
132132
result = goodRandom(DataFlow::TypeTracker::end(), source)
133133
}
134134

135+
/**
136+
* Gets a node that is passed to a rounding function from `Math`, using type-backtracker `t`.
137+
*/
138+
DataFlow::Node isRounded(DataFlow::TypeBackTracker t) {
139+
t.start() and
140+
result = DataFlow::globalVarRef("Math").getAMemberCall(["round", "floor", "ceil"]).getArgument(0)
141+
or
142+
exists(DataFlow::TypeBackTracker t2 | t2 = t.smallstep(result, isRounded(t2)))
143+
or
144+
InsecureRandomness::isAdditionalTaintStep(result, isRounded(t.continue()))
145+
}
146+
135147
/**
136148
* Gets a node that that produces a biased result from otherwise cryptographically secure random numbers produced by `source`.
137149
*/
@@ -153,10 +165,7 @@ DataFlow::Node badCrypto(string description, DataFlow::SourceNode source) {
153165
goodRandom(source).asExpr() = div.getLeftOperand() and
154166
description = "division and rounding the result" and
155167
not div.getRightOperand() = isPowerOfTwoMinusOne().asExpr() and // division by (2^n)-1 most of the time produces a uniformly random number between 0 and 1.
156-
DataFlow::globalVarRef("Math")
157-
.getAMemberCall(["round", "floor", "ceil"])
158-
.getArgument(0)
159-
.asExpr() = div
168+
div = isRounded(DataFlow::TypeBackTracker::end()).asExpr()
160169
)
161170
or
162171
// modulo - only bad if not by a power of 2 - and the result is not checked for bias
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const cryptoRandomString = require('crypto-random-string');
2+
3+
const digits = cryptoRandomString({length: 10, type: 'numeric'});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const crypto = require('crypto');
2+
3+
const digits = [];
4+
while (digits.length < 10) {
5+
const byte = crypto.randomBytes(1)[0];
6+
if (byte >= 250) {
7+
continue;
8+
}
9+
digits.push(byte % 10); // OK
10+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const crypto = require('crypto');
2+
3+
const digits = [];
4+
for (let i = 0; i < 10; i++) {
5+
digits.push(crypto.randomBytes(1)[0] % 10); // NOT OK
6+
}

javascript/ql/test/query-tests/Security/CWE-327/BadRandomness.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@
1313
| bad-random.js:85:11:85:35 | goodRan ... Random2 | Using addition on a $@ produces biased results. | bad-random.js:84:23:84:38 | secureRandom(10) | cryptographically secure random number |
1414
| bad-random.js:87:16:87:24 | bad + bad | Using addition on a $@ produces biased results. | bad-random.js:83:23:83:38 | secureRandom(10) | cryptographically secure random number |
1515
| bad-random.js:87:16:87:24 | bad + bad | Using addition on a $@ produces biased results. | bad-random.js:84:23:84:38 | secureRandom(10) | cryptographically secure random number |
16+
| bad-random.js:90:29:90:54 | secureR ... / 25.6 | Using division and rounding the result on a $@ produces biased results. | bad-random.js:90:29:90:44 | secureRandom(10) | cryptographically secure random number |
17+
| bad-random.js:96:29:96:58 | crypto. ... ] / 100 | Using division and rounding the result on a $@ produces biased results. | bad-random.js:96:29:96:49 | crypto. ... ytes(1) | cryptographically secure random number |
18+
| bad-random.js:118:17:118:45 | crypto. ... 0] % 10 | Using modulo on a $@ produces biased results. | bad-random.js:118:17:118:37 | crypto. ... ytes(1) | cryptographically secure random number |

javascript/ql/test/query-tests/Security/CWE-327/bad-random.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,13 @@ var bad = goodRandom1 + goodRandom2; // NOT OK
8787
var dontFlag = bad + bad; // OK - the operands have already been flagged - but flagged anyway due to us not detecting that [INCONSISTENCY].
8888

8989
var good = secureRandom(10)[0] / 0xff; // OK - result is not rounded.
90-
var good = Math.ceil(0.5 - (secureRandom(10)[0] / 25.6)); // NOT OK - division generally introduces bias - but not flagged due to not looking through nested arithmetic [INCONSISTENCY].
90+
var good = Math.ceil(0.5 - (secureRandom(10)[0] / 25.6)); // NOT OK - division generally introduces bias - but not flagged due to not looking through nested arithmetic.
9191

9292
var good = (crypto.randomBytes(1)[0] << 8) + crypto.randomBytes(3)[0]; // OK - bit shifts are usually used to construct larger/smaller numbers,
9393

9494
var good = Math.floor(max * (crypto.randomBytes(1)[0] / 0xff)); // OK - division by 0xff (255) gives a uniformly random number between 0 and 1.
9595

96-
var bad = Math.floor(max * (crypto.randomBytes(1)[0] / 100)); // NOT OK - division by 100 gives bias - but not flagged due to not looking through nested arithmetic [INCONSISTENCY].
96+
var bad = Math.floor(max * (crypto.randomBytes(1)[0] / 100)); // NOT OK - division by 100 gives bias - but not flagged due to not looking through nested arithmetic.
9797

9898
var crb = crypto.randomBytes(4);
9999
var cryptoRand = 0x01000000 * crb[0] + 0x00010000 * crb[1] + 0x00000100 * crb[2] + 0x00000001 * crb[3]; // OK - producing a larger number from smaller numbers.
@@ -110,4 +110,20 @@ var a = crypto.randomBytes(10);
110110
var good = ((a[i] & 31) * 0x1000000000000) + (a[i + 1] * 0x10000000000) + (a[i + 2] * 0x100000000) + (a[i + 3] * 0x1000000) + (a[i + 4] << 16) + (a[i + 5] << 8) + a[i + 6]; // OK - generating a large number from smaller bytes.
111111
var good = (a[i] * 0x100000000) + a[i + 6]; // OK - generating a large number from smaller bytes.
112112
var good = (a[i + 2] * 0x10000000) + a[i + 6]; // OK - generating a large number from smaller bytes.
113-
var foo = 0xffffffffffff + 0xfffffffffff + 0xffffffffff + 0xfffffffff + 0xffffffff + 0xfffffff + 0xffffff
113+
var foo = 0xffffffffffff + 0xfffffffffff + 0xffffffffff + 0xfffffffff + 0xffffffff + 0xfffffff + 0xffffff
114+
115+
// Bad documentation example:
116+
const digits = [];
117+
for (let i = 0; i < 10; i++) {
118+
digits.push(crypto.randomBytes(1)[0] % 10); // NOT OK
119+
}
120+
121+
// Good documentation example:
122+
const digits = [];
123+
while (digits.length < 10) {
124+
const byte = crypto.randomBytes(1)[0];
125+
if (byte >= 250) {
126+
continue;
127+
}
128+
digits.push(byte % 10); // OK
129+
}

0 commit comments

Comments
 (0)