Skip to content

Commit 54f3b27

Browse files
owen-mcCopilot
andcommitted
Use inline expectations with second test
Co-authored-by: Copilot <copilot@github.com>
1 parent dca7046 commit 54f3b27

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
Security/CWE/CWE-023/PartialPathTraversal.ql
1+
query: Security/CWE/CWE-023/PartialPathTraversal.ql
2+
postprocess:
3+
- utils/test/PrettyPrintModels.ql
4+
- utils/test/InlineExpectationsTestQuery.ql

java/ql/test/query-tests/security/CWE-023/semmle/tests/PartialPathTraversalTest.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010

1111
public class PartialPathTraversalTest {
1212
public void esapiExample(File parent) throws IOException {
13-
if (!dir().getCanonicalPath().startsWith(parent.getCanonicalPath())) { // $ Alert[java/partial-path-traversal-from-remote]
13+
if (!dir().getCanonicalPath().startsWith(parent.getCanonicalPath())) { // $ Alert[java/partial-path-traversal-from-remote] Alert[java/partial-path-traversal]
1414
throw new IOException("Invalid directory: " + dir().getCanonicalPath());
1515
}
1616
}
1717

1818
@SuppressWarnings("ResultOfMethodCallIgnored")
1919
void foo1(File parent) throws IOException {
20-
(dir().getCanonicalPath()).startsWith((parent.getCanonicalPath())); // $ Alert[java/partial-path-traversal-from-remote]
20+
(dir().getCanonicalPath()).startsWith((parent.getCanonicalPath())); // $ Alert[java/partial-path-traversal-from-remote] Alert[java/partial-path-traversal]
2121
}
2222

2323
void foo2(File parent) throws IOException {
@@ -29,42 +29,42 @@ void foo2(File parent) throws IOException {
2929

3030
void foo3(File parent) throws IOException {
3131
String parentPath = parent.getCanonicalPath();
32-
if (!dir().getCanonicalPath().startsWith(parentPath)) { // $ Alert[java/partial-path-traversal-from-remote]
32+
if (!dir().getCanonicalPath().startsWith(parentPath)) { // $ Alert[java/partial-path-traversal-from-remote] Alert[java/partial-path-traversal]
3333
throw new IOException("Invalid directory: " + dir().getCanonicalPath());
3434
}
3535
}
3636

3737
void foo4() throws IOException {
38-
if (!dir().getCanonicalPath().startsWith("/usr" + "/dir")) { // $ Alert[java/partial-path-traversal-from-remote]
38+
if (!dir().getCanonicalPath().startsWith("/usr" + "/dir")) { // $ Alert[java/partial-path-traversal-from-remote] Alert[java/partial-path-traversal]
3939
throw new IOException("Invalid directory: " + dir().getCanonicalPath());
4040
}
4141
}
4242

4343
void foo5(File parent) throws IOException {
4444
String canonicalPath = dir().getCanonicalPath();
45-
if (!canonicalPath.startsWith(parent.getCanonicalPath())) { // $ Alert[java/partial-path-traversal-from-remote]
45+
if (!canonicalPath.startsWith(parent.getCanonicalPath())) { // $ Alert[java/partial-path-traversal-from-remote] Alert[java/partial-path-traversal]
4646
throw new IOException("Invalid directory: " + dir().getCanonicalPath());
4747
}
4848
}
4949

5050
void foo6(File parent) throws IOException {
5151
String canonicalPath = dir().getCanonicalPath();
52-
if (!canonicalPath.startsWith(parent.getCanonicalPath())) { // $ Alert[java/partial-path-traversal-from-remote]
52+
if (!canonicalPath.startsWith(parent.getCanonicalPath())) { // $ Alert[java/partial-path-traversal-from-remote] Alert[java/partial-path-traversal]
5353
throw new IOException("Invalid directory: " + dir().getCanonicalPath());
5454
}
5555
String canonicalPath2 = dir().getCanonicalPath();
56-
if (!canonicalPath2.startsWith(parent.getCanonicalPath())) { // $ Alert[java/partial-path-traversal-from-remote]
56+
if (!canonicalPath2.startsWith(parent.getCanonicalPath())) { // $ Alert[java/partial-path-traversal-from-remote] Alert[java/partial-path-traversal]
5757
throw new IOException("Invalid directory: " + dir().getCanonicalPath());
5858
}
5959
}
6060

6161
void foo7(File dir, File parent) throws IOException {
6262
String canonicalPath = dir().getCanonicalPath();
6363
String canonicalPath2 = dir().getCanonicalPath();
64-
if (!canonicalPath.startsWith(parent.getCanonicalPath())) { // $ Alert[java/partial-path-traversal-from-remote]
64+
if (!canonicalPath.startsWith(parent.getCanonicalPath())) { // $ Alert[java/partial-path-traversal-from-remote] Alert[java/partial-path-traversal]
6565
throw new IOException("Invalid directory: " + dir().getCanonicalPath());
6666
}
67-
if (!canonicalPath2.startsWith(parent.getCanonicalPath())) { // $ Alert[java/partial-path-traversal-from-remote]
67+
if (!canonicalPath2.startsWith(parent.getCanonicalPath())) { // $ Alert[java/partial-path-traversal-from-remote] Alert[java/partial-path-traversal]
6868
throw new IOException("Invalid directory: " + dir().getCanonicalPath());
6969
}
7070
}
@@ -75,7 +75,7 @@ File getChild() {
7575

7676
void foo8(File parent) throws IOException {
7777
String canonicalPath = getChild().getCanonicalPath();
78-
if (!canonicalPath.startsWith(parent.getCanonicalPath())) {
78+
if (!canonicalPath.startsWith(parent.getCanonicalPath())) { // $ Alert[java/partial-path-traversal-from-remote] Alert[java/partial-path-traversal]
7979
throw new IOException("Invalid directory: " + getChild().getCanonicalPath());
8080
}
8181
}
@@ -94,18 +94,18 @@ void foo10(File parent) throws IOException {
9494

9595
void foo11(File parent) throws IOException {
9696
String parentCanonical = parent.getCanonicalPath();
97-
if (!dir().getCanonicalPath().startsWith(parentCanonical)) { // $ Alert[java/partial-path-traversal-from-remote]
97+
if (!dir().getCanonicalPath().startsWith(parentCanonical)) { // $ Alert[java/partial-path-traversal-from-remote] Alert[java/partial-path-traversal]
9898
throw new IOException("Invalid directory: " + dir().getCanonicalPath());
9999
}
100100
}
101101

102102
void foo12(File parent) throws IOException {
103103
String parentCanonical = parent.getCanonicalPath();
104104
String parentCanonical2 = parent.getCanonicalPath();
105-
if (!dir().getCanonicalPath().startsWith(parentCanonical)) { // $ Alert[java/partial-path-traversal-from-remote]
105+
if (!dir().getCanonicalPath().startsWith(parentCanonical)) { // $ Alert[java/partial-path-traversal-from-remote] Alert[java/partial-path-traversal]
106106
throw new IOException("Invalid directory: " + dir().getCanonicalPath());
107107
}
108-
if (!dir().getCanonicalPath().startsWith(parentCanonical2)) { // $ Alert[java/partial-path-traversal-from-remote]
108+
if (!dir().getCanonicalPath().startsWith(parentCanonical2)) { // $ Alert[java/partial-path-traversal-from-remote] Alert[java/partial-path-traversal]
109109
throw new IOException("Invalid directory: " + dir().getCanonicalPath());
110110
}
111111
}
@@ -173,7 +173,7 @@ void foo18(File dir, File parent, boolean branch) throws IOException {
173173

174174
void foo19(File parent) throws IOException {
175175
String parentCanonical = parent.getCanonicalPath() + "/potato";
176-
if (!dir().getCanonicalPath().startsWith(parentCanonical)) { // $ Alert[java/partial-path-traversal-from-remote]
176+
if (!dir().getCanonicalPath().startsWith(parentCanonical)) { // $ Alert[java/partial-path-traversal-from-remote] Alert[java/partial-path-traversal]
177177
throw new IOException("Invalid directory: " + dir().getCanonicalPath());
178178
}
179179
}
@@ -191,7 +191,7 @@ InputStream foo20() {
191191
String filePath = sb.toString();
192192
File encodedFile = new File(filePath);
193193
try {
194-
if (!encodedFile.getCanonicalPath().startsWith(cacheDir.getCanonicalPath())) { // $ Alert[java/partial-path-traversal-from-remote]
194+
if (!encodedFile.getCanonicalPath().startsWith(cacheDir.getCanonicalPath())) { // $ Alert[java/partial-path-traversal-from-remote] Alert[java/partial-path-traversal]
195195
return null;
196196
}
197197
return Files.newInputStream(encodedFile.toPath());
@@ -209,7 +209,7 @@ void foo21(File parent) throws IOException {
209209

210210
void foo22(File dir2, File parent, boolean conditional) throws IOException {
211211
String canonicalPath = conditional ? dir().getCanonicalPath() : dir2.getCanonicalPath();
212-
if (!canonicalPath.startsWith(parent.getCanonicalPath())) { // $ Alert[java/partial-path-traversal-from-remote]
212+
if (!canonicalPath.startsWith(parent.getCanonicalPath())) { // $ Alert[java/partial-path-traversal-from-remote] Alert[java/partial-path-traversal]
213213
throw new IOException("Invalid directory: " + dir().getCanonicalPath());
214214
}
215215
}

0 commit comments

Comments
 (0)