Skip to content

Commit e83673c

Browse files
owen-mcCopilot
andcommitted
Java: Preserve composite path guards across File and Path conversions
Allow localFlowToPathGuard to step backwards across String/File/Path representation conversions (including FileSystem.getPath, Path.toAbsolutePath, File.getAbsolutePath, and File.getAbsoluteFile), and allow flowsFromGuard to traverse File(parent, child) child arguments so precomputed derived paths remain sanitized when paired with a guard on the base value. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent a558c62 commit e83673c

3 files changed

Lines changed: 231 additions & 7 deletions

File tree

‎java/ql/lib/semmle/code/java/security/PathSanitizer.qll‎

Lines changed: 91 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,100 @@ private predicate localTaintFlowToPathGuard(Expr e, PathGuard g) {
6060
FlowToPathGuard::hasExprFlow(e, g.getCheckedExpr())
6161
}
6262

63+
/**
64+
* Holds if `n2` is a `String`, `File`, or `Path` representation of `n1` that
65+
* preserves path-traversal components.
66+
*/
67+
private predicate pathRepresentationStep(DataFlow::Node n1, DataFlow::Node n2) {
68+
exists(MethodCall mc, Method m |
69+
n2.asExpr() = mc and
70+
m = mc.getMethod()
71+
|
72+
n1.asExpr() = mc.getQualifier() and
73+
mc.getNumArgument() = 0 and
74+
(
75+
m.hasName("toString") and
76+
exists(RefType qt | qt = mc.getQualifier().getType().(RefType).getAnAncestor() |
77+
qt instanceof TypePath or
78+
qt instanceof TypeFile or
79+
qt instanceof TypeString
80+
)
81+
or
82+
m.getDeclaringType().getAnAncestor() instanceof TypePath and
83+
m.hasName(["toAbsolutePath", "toFile"])
84+
or
85+
m.getDeclaringType().getAnAncestor() instanceof TypeFile and
86+
m.hasName(["getAbsoluteFile", "getAbsolutePath", "getPath", "toPath"])
87+
)
88+
or
89+
n1.asExpr() = mc.getArgument(0) and
90+
m.getParameter(0).getType() instanceof TypeString and
91+
(
92+
mc.getNumArgument() = 1
93+
or
94+
mc.getNumArgument() = 2 and
95+
mc.getArgument(1).(ArrayCreationExpr).getFirstDimensionSize() = 0
96+
) and
97+
(
98+
m.getDeclaringType() instanceof TypePaths and m.hasName("get")
99+
or
100+
m.getDeclaringType() instanceof TypePath and m.hasName("of")
101+
or
102+
m.getDeclaringType().getAnAncestor() instanceof TypeFileSystem and m.hasName("getPath")
103+
)
104+
)
105+
or
106+
exists(ClassInstanceExpr cc |
107+
n2.asExpr() = cc and
108+
n1.asExpr() = cc.getArgument(0) and
109+
cc.getConstructedType().getAnAncestor() instanceof TypeFile and
110+
cc.getNumArgument() = 1 and
111+
cc.getCallee().getParameter(0).getType() instanceof TypeString
112+
)
113+
}
114+
63115
private predicate localFlowToPathGuard(DataFlow::Node n, PathGuard g) {
64116
n.asExpr() = g.getCheckedExpr()
65117
or
66118
exists(DataFlow::Node mid |
67119
localFlowToPathGuard(mid, g) and
68-
DataFlow::localFlowStep(n, mid)
120+
(
121+
DataFlow::localFlowStep(n, mid) or
122+
pathRepresentationStep(n, mid)
123+
)
124+
)
125+
}
126+
127+
private predicate fileConstructorChildStep(DataFlow::Node n1, DataFlow::Node n2) {
128+
exists(ConstructorCall cc |
129+
cc.getConstructedType().getAnAncestor() instanceof TypeFile and
130+
n1.asExpr() = cc.getArgument(1) and
131+
n2.asExpr() = cc
132+
)
133+
}
134+
135+
private predicate reachesPathGuard(DataFlow::Node n) {
136+
pathGuardNode(n)
137+
or
138+
exists(DataFlow::Node mid |
139+
reachesPathGuard(mid) and
140+
(
141+
TaintTracking::localTaintStep(n, mid) or
142+
fileConstructorChildStep(n, mid)
143+
)
144+
)
145+
}
146+
147+
private predicate flowsFromGuard(DataFlow::Node n, PathGuard g) {
148+
localFlowToPathGuard(n, g)
149+
or
150+
exists(DataFlow::Node mid |
151+
flowsFromGuard(mid, g) and
152+
reachesPathGuard(n) and
153+
(
154+
TaintTracking::localTaintStep(mid, n) or
155+
fileConstructorChildStep(mid, n)
156+
)
69157
)
70158
}
71159

@@ -74,12 +162,8 @@ private predicate localFlowToPathGuard(DataFlow::Node n, PathGuard g) {
74162
* `previousGuard` are connected by local taint flow, possibly from a common value.
75163
*/
76164
private predicate checksRelatedValue(Expr e, PathGuard previousGuard) {
77-
localTaintFlowToPathGuard(e, previousGuard)
78-
or
79-
exists(DataFlow::Node prev |
80-
localFlowToPathGuard(prev, previousGuard) and
81-
FlowToPathGuard::hasFlow(prev, DataFlow::exprNode(e))
82-
)
165+
localTaintFlowToPathGuard(e, previousGuard) or
166+
flowsFromGuard(DataFlow::exprNode(e), previousGuard)
83167
}
84168

85169
private class AllowedPrefixGuard extends PathGuard instanceof MethodCall {

‎java/ql/test/library-tests/pathsanitizer/Test.java‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import java.io.File;
22
import java.net.URI;
3+
import java.nio.file.FileSystems;
34
import java.nio.file.Path;
45
import java.nio.file.Paths;
56
import android.net.Uri;
@@ -192,6 +193,8 @@ public void allowListGuard() throws Exception {
192193
sink(source); // $ hasTaintFlow
193194
}
194195
{
196+
// Lossy `File.getName()` does not link `baseName` back to precomputed `resolved`
197+
// (note: `sink(source)` is still sanitized because `localTaintFlowToPathGuard` allows taint steps).
195198
String source = (String) source();
196199
String baseName = new File(source).getName();
197200
Path resolved = Paths.get("/safe/sub").resolve(source);
@@ -210,6 +213,53 @@ public void allowListGuard() throws Exception {
210213
sink(resolved); // $ hasTaintFlow
211214
}
212215
}
216+
{
217+
Path source = (Path) source();
218+
Path resolved = Paths.get("/safe").resolve(source);
219+
if (!source.toString().contains("..") && resolved.startsWith("/safe")) {
220+
sink(source); // Safe
221+
sink(resolved); // Safe
222+
} else {
223+
sink(source); // $ hasTaintFlow
224+
sink(resolved); // $ hasTaintFlow
225+
}
226+
}
227+
{
228+
File source = (File) source();
229+
String str = source.getPath();
230+
Path resolved = Paths.get("/safe").resolve(source.toPath());
231+
if (!str.contains("..") && resolved.startsWith("/safe")) {
232+
sink(source); // Safe
233+
sink(resolved); // Safe
234+
} else {
235+
sink(source); // $ hasTaintFlow
236+
sink(resolved); // $ hasTaintFlow
237+
}
238+
}
239+
{
240+
File source = (File) source();
241+
String absPath = source.getAbsoluteFile().getAbsolutePath();
242+
Path resolved = Paths.get("/safe").resolve(source.toPath());
243+
if (!absPath.contains("..") && resolved.startsWith("/safe")) {
244+
sink(source); // Safe
245+
sink(resolved); // Safe
246+
} else {
247+
sink(source); // $ hasTaintFlow
248+
sink(resolved); // $ hasTaintFlow
249+
}
250+
}
251+
{
252+
File dir = new File("/safe");
253+
String name = (String) source();
254+
File file = new File(dir, name);
255+
if (!name.contains("..") && file.toPath().startsWith(dir.toPath())) {
256+
sink(file); // Safe
257+
sink(name); // $ hasTaintFlow
258+
} else {
259+
sink(file); // $ hasTaintFlow
260+
sink(name); // $ hasTaintFlow
261+
}
262+
}
213263
{
214264
String source = (String) source();
215265
if (!source.contains("..")) {
@@ -434,6 +484,60 @@ public void dotDotCheckGuard() throws Exception {
434484
sink(trimmed); // $ hasTaintFlow
435485
}
436486
}
487+
{
488+
String source = (String) source();
489+
String target = source + "/file";
490+
if (Paths.get(source).startsWith("/safe") && !target.contains("..")) {
491+
sink(source); // Safe
492+
sink(target); // Safe
493+
} else {
494+
sink(source); // $ hasTaintFlow
495+
sink(target); // $ hasTaintFlow
496+
}
497+
}
498+
{
499+
Path source = (Path) source();
500+
String str = source.toString();
501+
if (source.toString().startsWith("/safe") && !str.contains("..")) {
502+
sink(source); // Safe
503+
sink(str); // Safe
504+
} else {
505+
sink(source); // $ hasTaintFlow
506+
sink(str); // $ hasTaintFlow
507+
}
508+
}
509+
{
510+
String source = (String) source();
511+
Path checked = FileSystems.getDefault().getPath(source);
512+
String target = source + "/file";
513+
if (checked.startsWith("/safe") && !target.contains("..")) {
514+
sink(source); // Safe
515+
sink(target); // Safe
516+
} else {
517+
sink(source); // $ hasTaintFlow
518+
sink(target); // $ hasTaintFlow
519+
}
520+
}
521+
{
522+
String source = (String) source();
523+
Path checked = FileSystems.getDefault().getPath(source, "/safe");
524+
String target = source + "/etc";
525+
if (checked.startsWith("/safe") && !target.contains("..")) {
526+
sink(target); // $ hasTaintFlow
527+
}
528+
}
529+
{
530+
Path source = (Path) source();
531+
Path abs = source.toAbsolutePath();
532+
Path target = source.resolve("file");
533+
if (abs.startsWith("/safe") && !target.toString().contains("..")) {
534+
sink(source); // Safe
535+
sink(target); // Safe
536+
} else {
537+
sink(source); // $ hasTaintFlow
538+
sink(target); // $ hasTaintFlow
539+
}
540+
}
437541
}
438542

439543
private void blockListGuardValidation(String path) throws Exception {

‎java/ql/test/library-tests/pathsanitizer/TestKt.kt‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import java.io.File
22
import java.net.URI
3+
import java.nio.file.FileSystems
34
import java.nio.file.Path
45
import java.nio.file.Paths
56
import android.net.Uri
@@ -205,6 +206,41 @@ class TestKt {
205206
sink(resolved) // $ hasTaintFlow
206207
}
207208
}
209+
run {
210+
val source = source() as Path?
211+
val resolved: Path = Paths.get("/safe").resolve(source!!)
212+
if (!source.toString().contains("..") && resolved.startsWith("/safe")) {
213+
sink(source) // Safe
214+
sink(resolved) // Safe
215+
} else {
216+
sink(source) // $ hasTaintFlow
217+
sink(resolved) // $ hasTaintFlow
218+
}
219+
}
220+
run {
221+
val dir = File("/safe")
222+
val name = source() as String?
223+
val file = File(dir, name!!)
224+
if (!name.contains("..") && file.toPath().startsWith(dir.toPath())) {
225+
sink(file) // Safe
226+
sink(name) // $ hasTaintFlow
227+
} else {
228+
sink(file) // $ hasTaintFlow
229+
sink(name) // $ hasTaintFlow
230+
}
231+
}
232+
run {
233+
val source = source() as String?
234+
val checked: Path = FileSystems.getDefault().getPath(source!!).toAbsolutePath()
235+
val target = source + "/file"
236+
if (checked.startsWith("/safe") && !target.contains("..")) {
237+
sink(source) // Safe
238+
sink(target) // Safe
239+
} else {
240+
sink(source) // $ hasTaintFlow
241+
sink(target) // $ hasTaintFlow
242+
}
243+
}
208244
run {
209245
val source = source() as String?
210246
if (!source!!.contains("..")) {

0 commit comments

Comments
 (0)