Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 72012a9

Browse files
authored
Merge pull request #375 from esben-semmle/js/limit-directive-sizes
Approved by xiemaisi
2 parents 47096bb + 8f635e6 commit 72012a9

6 files changed

Lines changed: 28 additions & 7 deletions

File tree

javascript/ql/src/Expressions/UnknownDirective.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ from Directive d
1414
where not d instanceof KnownDirective and
1515
// but exclude attribute top-levels: `<a href="javascript:'some-attribute-string'">`
1616
not (d.getParent() instanceof CodeInAttribute)
17-
select d, "Unknown directive: '" + d.getDirectiveText() + "'."
17+
select d, "Unknown directive: '" + truncate(d.getDirectiveText(), 20, " ... (truncated)") + "'."

javascript/ql/src/semmle/javascript/Util.qll

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,25 @@ string capitalize(string s) {
1212
result = s.charAt(0).toUpperCase() + s.suffix(1)
1313
}
1414

15-
/**
16-
* Gets the pluralization for `n` occurrences of `noun`.
17-
*
18-
* For example, the pluralization of `"function"` for `n = 2` is `"functions"`.
19-
*/
15+
/**
16+
* Gets the pluralization for `n` occurrences of `noun`.
17+
*
18+
* For example, the pluralization of `"function"` for `n = 2` is `"functions"`.
19+
*/
2020
bindingset[noun, n]
2121
string pluralize(string noun, int n) {
2222
if n = 1 then
2323
result = noun
2424
else
2525
result = noun + "s"
26-
}
26+
}
27+
28+
/**
29+
* Gets `str` or a truncated version of `str` with `explanation` appended if its length exceeds `maxLength`.
30+
*
31+
* For example, the truncation of `"long_string"` for `maxLength = 5` and explanation `" ..."` is `"long_ ..."`.
32+
*/
33+
bindingset[str, maxLength, explanation]
34+
string truncate(string str, int maxLength, string explanation) {
35+
if str.length() > maxLength then result = str.prefix(maxLength) + explanation else result = str
36+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| y | | X | XX | XXy |
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import semmle.javascript.Util
2+
3+
select truncate("X", 0, "y"), truncate("", 2, "y"), truncate("X", 2, "y"), truncate("XX", 2, "y"), truncate("XXX", 2, "y")

javascript/ql/test/query-tests/Expressions/UnknownDirective/UnknownDirective.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@
1111
| UnknownDirective.js:12:5:12:17 | "use struct;" | Unknown directive: 'use struct;'. |
1212
| UnknownDirective.js:13:5:13:17 | "Use Strict"; | Unknown directive: 'Use Strict'. |
1313
| UnknownDirective.js:14:5:14:14 | "use bar"; | Unknown directive: 'use bar'. |
14+
| UnknownDirective.js:38:5:38:17 | "[0, 0, 0];"; | Unknown directive: '[0, 0, 0];'. |
15+
| UnknownDirective.js:39:5:39:65 | "[0, 0, ... , 0];"; | Unknown directive: '[0, 0, 0, 0, 0, 0, 0 ... (truncated)'. |

javascript/ql/test/query-tests/Expressions/UnknownDirective/UnknownDirective.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,8 @@ function good() {
3333
"deps foo"; // OK
3434
"deps bar"; // OK
3535
}
36+
37+
function data() {
38+
"[0, 0, 0];"; // NOT OK
39+
"[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];"; // NOT OK
40+
}

0 commit comments

Comments
 (0)