Skip to content

Commit 6c957f9

Browse files
committed
java inline expectations prototype with tests
1 parent 3ae4cb2 commit 6c957f9

File tree

44 files changed

+577
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+577
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import java
2+
3+
external predicate queryResults(string relation, int row, int column, string data);
4+
5+
string actualLines() {
6+
exists(int i |
7+
queryResults("#select", i, _, _) and
8+
result =
9+
" | " +
10+
concat(int j, string cell | queryResults("#select", i, j, cell) | cell, " | " order by j) +
11+
" | "
12+
)
13+
}
14+
15+
predicate parsedExpectedResults(string filename, int line, string content) {
16+
exists(Javadoc doc |
17+
isEolComment(doc) and
18+
filename = doc.getLocation().getFile().getBaseName() and
19+
line = doc.getLocation().getStartLine() and
20+
line = doc.getLocation().getEndLine() and
21+
content = doc.getChild(0).getText().trim()
22+
)
23+
}
24+
25+
predicate expectError(string filename, int line) {
26+
exists(string content |
27+
parsedExpectedResults(filename, line, content) and content.indexOf("NOT OK") = 0
28+
)
29+
}
30+
31+
predicate expectPass(string filename, int line) {
32+
exists(string content |
33+
parsedExpectedResults(filename, line, content) and content.indexOf("OK") = 0
34+
)
35+
}
36+
37+
predicate parsedActualResults(string filename, int line, int colStart, int colEnd, string content) {
38+
exists(string s, string posString, string lineString |
39+
s = actualLines() and
40+
posString = s.substring(s.indexOf("|", 0, 0) + 1, s.indexOf("|", 1, 0)).trim() and
41+
filename = posString.substring(0, posString.indexOf(":", 0, 0)) and
42+
lineString = posString.substring(posString.indexOf(":", 0, 0) + 1, posString.indexOf(":", 1, 0)) and
43+
lineString = posString.substring(posString.indexOf(":", 2, 0) + 1, posString.indexOf(":", 3, 0)) and
44+
colStart =
45+
posString.substring(posString.indexOf(":", 1, 0) + 1, posString.indexOf(":", 2, 0)).toInt() and
46+
colEnd = posString.substring(posString.indexOf(":", 3, 0) + 1, posString.length()).toInt() and
47+
line = lineString.toInt() and
48+
content = s.substring(s.indexOf("|", 2, 0) + 1, s.indexOf("|", 3, 0)).trim()
49+
)
50+
}
51+
52+
predicate actualExpectedDiff(string type, string position, string error) {
53+
exists(string filename, int line, int colStart, int colEnd |
54+
parsedActualResults(filename, line, colStart, colEnd, error) and
55+
expectPass(filename, line) and
56+
type = "unexpected alert" and
57+
position = filename + ":" + line + ":" + colStart + ":" + line + ":" + colEnd
58+
)
59+
or
60+
exists(string filename, int line |
61+
expectError(filename, line) and
62+
not parsedActualResults(filename, line, _, _, _) and
63+
type = "expected alert" and
64+
position = filename + ":" + line and
65+
parsedExpectedResults(filename, line, error)
66+
)
67+
or
68+
exists(string filename, int line, string content |
69+
parsedExpectedResults(filename, line, content) and
70+
not expectPass(filename, line) and
71+
not expectError(filename, line) and
72+
type = "invalid inline expectation" and
73+
position = filename + ":" + line and
74+
error = content
75+
)
76+
}
77+
78+
from int line, string position, int column, string content
79+
where
80+
position = rank[line](string p | actualExpectedDiff(_, p, _) | p) and
81+
(
82+
column = 0 and content = position
83+
or
84+
column = 1 and actualExpectedDiff(content, position, _)
85+
or
86+
column = 2 and actualExpectedDiff(_, position, content)
87+
)
88+
select "#select", line, column, content
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
query predicate resultRelations(string name) { name = "#select" }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
query predicate learnEdits(string name) { none() }
2+
3+
select "#select", 1, 1, "foo"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
query predicate extraQuery(string name) { none() }
2+
3+
select "#select", 1, 1, "foo"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
query predicate resultRelations(string name, int arity) { name = "#select" and arity = 5 }
2+
3+
select "#select", 1, 1, "foo"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
select "#select", "1", 1, "foo"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
select "#select", 1, 1, ["foo", "bar"]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from int i, int j
2+
where
3+
i in [1, 2] and
4+
j in [1, 2] and
5+
not (i = 2 and j = 2)
6+
select "#select", i, j, "foo"

java/ql/test/query-tests/Postprocessing/failing/OverlyLargeRangeQuery.expected

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: Security/CWE/CWE-020/OverlyLargeRange.ql
2+
postprocess: TestUtilities/JavaInlineExpectations.ql
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.regex.Pattern;
2+
3+
class SuspiciousRegexpRange {
4+
void test() {
5+
Pattern overlap1 = Pattern.compile("^[0-93-5]*$"); // NOT OK
6+
7+
Pattern overlap2 = Pattern.compile("[A-ZA-z]*"); // OK
8+
9+
Pattern isEmpty = Pattern.compile("^[z-a]*$"); // NOT OK
10+
11+
Pattern isAscii = Pattern.compile("^[\\x00-\\x7F]*$"); // NOT OK
12+
13+
Pattern printable = Pattern.compile("[!-~]*"); // OK - used to select most printable ASCII characters
14+
15+
Pattern codePoints = Pattern.compile("[^\\x21-\\x7E]|[[\\](){}<>/%]*"); // OK
16+
17+
Pattern NON_ALPHANUMERIC_REGEXP = Pattern.compile("([^\\#-~| |!])*"); // OK
18+
19+
Pattern smallOverlap = Pattern.compile("[0-9a-fA-f]*"); // NOT OK
20+
21+
Pattern weirdRange = Pattern.compile("[$-`]*"); // NOT OK
22+
23+
Pattern keywordOperator = Pattern.compile("[!\\~\\*\\/%+-<>\\^|=&]*"); // NOT OK
24+
25+
Pattern notYoutube = Pattern.compile("youtu.be/[a-z1-9.-_]+"); // NOT OK
26+
27+
Pattern numberToLetter = Pattern.compile("[7-F]*"); // NOT OK
28+
29+
Pattern overlapsWithClass1 = Pattern.compile("[0-9\\d]*"); // NOT OK
30+
31+
Pattern overlapsWithClass2 = Pattern.compile("[\\w,.-?:*+]*"); // NOT OK
32+
33+
Pattern nested = Pattern.compile("[[A-Za-z_][A-Za-z0-9._-]]*"); // OK, the dash it at the end
34+
35+
Pattern octal = Pattern.compile("[\000-\037\040-\045]*"); // OK
36+
}
37+
}

java/ql/test/query-tests/Postprocessing/passing/OverlyLargeRangeQuery.expected

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: Security/CWE/CWE-020/OverlyLargeRange.ql
2+
postprocess: TestUtilities/JavaInlineExpectations.ql
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.regex.Pattern;
2+
3+
class SuspiciousRegexpRange {
4+
void test() {
5+
Pattern overlap1 = Pattern.compile("^[0-93-5]*$"); // NOT OK
6+
7+
Pattern overlap2 = Pattern.compile("[A-ZA-z]*"); // NOT OK
8+
9+
Pattern isEmpty = Pattern.compile("^[z-a]*$"); // NOT OK
10+
11+
Pattern isAscii = Pattern.compile("^[\\x00-\\x7F]*$"); // OK
12+
13+
Pattern printable = Pattern.compile("[!-~]*"); // OK - used to select most printable ASCII characters
14+
15+
Pattern codePoints = Pattern.compile("[^\\x21-\\x7E]|[[\\](){}<>/%]*"); // OK
16+
17+
Pattern NON_ALPHANUMERIC_REGEXP = Pattern.compile("([^\\#-~| |!])*"); // OK
18+
19+
Pattern smallOverlap = Pattern.compile("[0-9a-fA-f]*"); // NOT OK
20+
21+
Pattern weirdRange = Pattern.compile("[$-`]*"); // NOT OK
22+
23+
Pattern keywordOperator = Pattern.compile("[!\\~\\*\\/%+-<>\\^|=&]*"); // NOT OK
24+
25+
Pattern notYoutube = Pattern.compile("youtu.be/[a-z1-9.-_]+"); // NOT OK
26+
27+
Pattern numberToLetter = Pattern.compile("[7-F]*"); // NOT OK
28+
29+
Pattern overlapsWithClass1 = Pattern.compile("[0-9\\d]*"); // NOT OK
30+
31+
Pattern overlapsWithClass2 = Pattern.compile("[\\w,.-?:*+]*"); // NOT OK
32+
33+
Pattern nested = Pattern.compile("[[A-Za-z_][A-Za-z0-9._-]]*"); // OK, the dash it at the end
34+
35+
Pattern octal = Pattern.compile("[\000-\037\040-\045]*"); // OK
36+
}
37+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| SuspiciousRegexpRange.java:11 | expected alert | NOT OK |
2+
| SuspiciousRegexpRange.java:7:49:7:51 | unexpected alert | Suspicious character range that overlaps with A-Z in the same character class, and is equivalent to [A-Z\\[\\\\\\]^_`a-z]. |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: Security/CWE/CWE-020/OverlyLargeRange.ql
2+
postprocess: TestUtilities/JavaInlineExpectations.ql
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.regex.Pattern;
2+
3+
class SuspiciousRegexpRange {
4+
void test() {
5+
Pattern overlap1 = Pattern.compile("^[0-93-5]*$"); // NOT OK
6+
7+
Pattern overlap2 = Pattern.compile("[A-ZA-z]*"); // OK
8+
9+
Pattern isEmpty = Pattern.compile("^[z-a]*$"); // NOT OK
10+
11+
Pattern isAscii = Pattern.compile("^[\\x00-\\x7F]*$"); // NOT OK
12+
13+
Pattern printable = Pattern.compile("[!-~]*"); // OK - used to select most printable ASCII characters
14+
15+
Pattern codePoints = Pattern.compile("[^\\x21-\\x7E]|[[\\](){}<>/%]*"); // OK
16+
17+
Pattern NON_ALPHANUMERIC_REGEXP = Pattern.compile("([^\\#-~| |!])*"); // OK
18+
19+
Pattern smallOverlap = Pattern.compile("[0-9a-fA-f]*"); // NOT OK
20+
21+
Pattern weirdRange = Pattern.compile("[$-`]*"); // NOT OK
22+
23+
Pattern keywordOperator = Pattern.compile("[!\\~\\*\\/%+-<>\\^|=&]*"); // NOT OK
24+
25+
Pattern notYoutube = Pattern.compile("youtu.be/[a-z1-9.-_]+"); // NOT OK
26+
27+
Pattern numberToLetter = Pattern.compile("[7-F]*"); // NOT OK
28+
29+
Pattern overlapsWithClass1 = Pattern.compile("[0-9\\d]*"); // NOT OK
30+
31+
Pattern overlapsWithClass2 = Pattern.compile("[\\w,.-?:*+]*"); // NOT OK
32+
33+
Pattern nested = Pattern.compile("[[A-Za-z_][A-Za-z0-9._-]]*"); // OK, the dash it at the end
34+
35+
Pattern octal = Pattern.compile("[\000-\037\040-\045]*"); // OK
36+
}
37+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| SuspiciousRegexpRange.java:13 | invalid inline expectation | IS OK - used to select most printable ASCII characters |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: Security/CWE/CWE-020/OverlyLargeRange.ql
2+
postprocess: TestUtilities/JavaInlineExpectations.ql
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.regex.Pattern;
2+
3+
class SuspiciousRegexpRange {
4+
void test() {
5+
Pattern overlap1 = Pattern.compile("^[0-93-5]*$"); // NOT OK
6+
7+
Pattern overlap2 = Pattern.compile("[A-ZA-z]*"); // NOT OK
8+
9+
Pattern isEmpty = Pattern.compile("^[z-a]*$"); // NOT OK
10+
11+
Pattern isAscii = Pattern.compile("^[\\x00-\\x7F]*$"); // OK
12+
13+
Pattern printable = Pattern.compile("[!-~]*"); // IS OK - used to select most printable ASCII characters
14+
15+
Pattern codePoints = Pattern.compile("[^\\x21-\\x7E]|[[\\](){}<>/%]*"); // OK
16+
17+
Pattern NON_ALPHANUMERIC_REGEXP = Pattern.compile("([^\\#-~| |!])*"); // OK
18+
19+
Pattern smallOverlap = Pattern.compile("[0-9a-fA-f]*"); // NOT OK
20+
21+
Pattern weirdRange = Pattern.compile("[$-`]*"); // NOT OK
22+
23+
Pattern keywordOperator = Pattern.compile("[!\\~\\*\\/%+-<>\\^|=&]*"); // NOT OK
24+
25+
Pattern notYoutube = Pattern.compile("youtu.be/[a-z1-9.-_]+"); // NOT OK
26+
27+
Pattern numberToLetter = Pattern.compile("[7-F]*"); // NOT OK
28+
29+
Pattern overlapsWithClass1 = Pattern.compile("[0-9\\d]*"); // NOT OK
30+
31+
Pattern overlapsWithClass2 = Pattern.compile("[\\w,.-?:*+]*"); // NOT OK
32+
33+
Pattern nested = Pattern.compile("[[A-Za-z_][A-Za-z0-9._-]]*"); // OK, the dash it at the end
34+
35+
Pattern octal = Pattern.compile("[\000-\037\040-\045]*"); // OK
36+
}
37+
}

java/ql/test/query-tests/Postprocessing/postprocessingBug1/OverlyLargeRangeQuery.expected

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: Security/CWE/CWE-020/OverlyLargeRange.ql
2+
postprocess: TestUtilities/ProblematicJavaInlineExpectations1.ql
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.regex.Pattern;
2+
3+
class SuspiciousRegexpRange {
4+
void test() {
5+
Pattern overlap1 = Pattern.compile("^[0-93-5]*$"); // NOT OK
6+
7+
Pattern overlap2 = Pattern.compile("[A-ZA-z]*"); // NOT OK
8+
9+
Pattern isEmpty = Pattern.compile("^[z-a]*$"); // NOT OK
10+
11+
Pattern isAscii = Pattern.compile("^[\\x00-\\x7F]*$"); // OK
12+
13+
Pattern printable = Pattern.compile("[!-~]*"); // OK - used to select most printable ASCII characters
14+
15+
Pattern codePoints = Pattern.compile("[^\\x21-\\x7E]|[[\\](){}<>/%]*"); // OK
16+
17+
Pattern NON_ALPHANUMERIC_REGEXP = Pattern.compile("([^\\#-~| |!])*"); // OK
18+
19+
Pattern smallOverlap = Pattern.compile("[0-9a-fA-f]*"); // NOT OK
20+
21+
Pattern weirdRange = Pattern.compile("[$-`]*"); // NOT OK
22+
23+
Pattern keywordOperator = Pattern.compile("[!\\~\\*\\/%+-<>\\^|=&]*"); // NOT OK
24+
25+
Pattern notYoutube = Pattern.compile("youtu.be/[a-z1-9.-_]+"); // NOT OK
26+
27+
Pattern numberToLetter = Pattern.compile("[7-F]*"); // NOT OK
28+
29+
Pattern overlapsWithClass1 = Pattern.compile("[0-9\\d]*"); // NOT OK
30+
31+
Pattern overlapsWithClass2 = Pattern.compile("[\\w,.-?:*+]*"); // NOT OK
32+
33+
Pattern nested = Pattern.compile("[[A-Za-z_][A-Za-z0-9._-]]*"); // OK, the dash it at the end
34+
35+
Pattern octal = Pattern.compile("[\000-\037\040-\045]*"); // OK
36+
}
37+
}

java/ql/test/query-tests/Postprocessing/postprocessingBug2/OverlyLargeRangeQuery.expected

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: Security/CWE/CWE-020/OverlyLargeRange.ql
2+
postprocess: TestUtilities/ProblematicJavaInlineExpectations2.ql
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.regex.Pattern;
2+
3+
class SuspiciousRegexpRange {
4+
void test() {
5+
Pattern overlap1 = Pattern.compile("^[0-93-5]*$"); // NOT OK
6+
7+
Pattern overlap2 = Pattern.compile("[A-ZA-z]*"); // NOT OK
8+
9+
Pattern isEmpty = Pattern.compile("^[z-a]*$"); // NOT OK
10+
11+
Pattern isAscii = Pattern.compile("^[\\x00-\\x7F]*$"); // OK
12+
13+
Pattern printable = Pattern.compile("[!-~]*"); // OK - used to select most printable ASCII characters
14+
15+
Pattern codePoints = Pattern.compile("[^\\x21-\\x7E]|[[\\](){}<>/%]*"); // OK
16+
17+
Pattern NON_ALPHANUMERIC_REGEXP = Pattern.compile("([^\\#-~| |!])*"); // OK
18+
19+
Pattern smallOverlap = Pattern.compile("[0-9a-fA-f]*"); // NOT OK
20+
21+
Pattern weirdRange = Pattern.compile("[$-`]*"); // NOT OK
22+
23+
Pattern keywordOperator = Pattern.compile("[!\\~\\*\\/%+-<>\\^|=&]*"); // NOT OK
24+
25+
Pattern notYoutube = Pattern.compile("youtu.be/[a-z1-9.-_]+"); // NOT OK
26+
27+
Pattern numberToLetter = Pattern.compile("[7-F]*"); // NOT OK
28+
29+
Pattern overlapsWithClass1 = Pattern.compile("[0-9\\d]*"); // NOT OK
30+
31+
Pattern overlapsWithClass2 = Pattern.compile("[\\w,.-?:*+]*"); // NOT OK
32+
33+
Pattern nested = Pattern.compile("[[A-Za-z_][A-Za-z0-9._-]]*"); // OK, the dash it at the end
34+
35+
Pattern octal = Pattern.compile("[\000-\037\040-\045]*"); // OK
36+
}
37+
}

java/ql/test/query-tests/Postprocessing/postprocessingBug3/OverlyLargeRangeQuery.expected

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: Security/CWE/CWE-020/OverlyLargeRange.ql
2+
postprocess: TestUtilities/ProblematicJavaInlineExpectations3.ql

0 commit comments

Comments
 (0)