Skip to content

Commit ecf0e08

Browse files
committed
Rust: Add some more path injection test case variants.
1 parent 8436f00 commit ecf0e08

File tree

1 file changed

+43
-4
lines changed
  • rust/ql/test/query-tests/security/CWE-022/src

1 file changed

+43
-4
lines changed

rust/ql/test/query-tests/security/CWE-022/src/main.rs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,45 @@ fn tainted_path_handler_folder_good(Query(file_path): Query<String>) -> Result<S
3434

3535
//#[handler]
3636
fn tainted_path_handler_folder_almost_good1(
37-
Query(file_path): Query<String>, // $ MISSING: Source=remote4
37+
Query(file_path): Query<String>, // $ MISSING: Source=remote2
3838
) -> Result<String> {
3939
let public_path = PathBuf::from("/var/www/public_html");
4040
let file_path = public_path.join(PathBuf::from(file_path));
4141
// BAD: the path could still contain `..` and escape the public folder
4242
if !file_path.starts_with(public_path) {
4343
return Err(Error::from_status(StatusCode::BAD_REQUEST));
4444
}
45-
fs::read_to_string(file_path).map_err(InternalServerError) // $ path-injection-sink MISSING: Alert[rust/path-injection]=remote4 -- we cannot resolve the `join` call above, because it needs a `PathBuf -> Path` `Deref`
45+
fs::read_to_string(file_path).map_err(InternalServerError) // $ path-injection-sink MISSING: Alert[rust/path-injection]=remote2 -- we cannot resolve the `join` call above, because it needs a `PathBuf -> Path` `Deref`
46+
}
47+
48+
//#[handler]
49+
fn tainted_path_handler_folder_good_simpler(Query(file_path): Query<String>) -> Result<String> {
50+
let public_path = "/var/www/public_html";
51+
let file_path = Path::new(&file_path);
52+
let file_path = file_path.canonicalize().unwrap();
53+
// GOOD: ensure that the path stays within the public folder
54+
if !file_path.starts_with(public_path) {
55+
return Err(Error::from_status(StatusCode::BAD_REQUEST));
56+
}
57+
fs::read_to_string(file_path).map_err(InternalServerError) // $ path-injection-sink
58+
}
59+
60+
//#[handler]
61+
fn tainted_path_handler_folder_almost_good1_simpler(
62+
Query(file_path): Query<String>, // $ MISSING: Source=remote3
63+
) -> Result<String> {
64+
let public_path = "/var/www/public_html";
65+
let file_path = Path::new(&file_path);
66+
// BAD: the path could still contain `..` and escape the public folder
67+
if !file_path.starts_with(public_path) {
68+
return Err(Error::from_status(StatusCode::BAD_REQUEST));
69+
}
70+
fs::read_to_string(file_path).map_err(InternalServerError) // $ path-injection-sink MISSING: Alert[rust/path-injection]=remote3
4671
}
4772

4873
//#[handler]
4974
fn tainted_path_handler_folder_almost_good2(
50-
Query(file_path): Query<String>, // $ MISSING: Source=remote5
75+
Query(file_path): Query<String>, // $ MISSING: Source=remote4
5176
) -> Result<String> {
5277
let public_path = PathBuf::from("/var/www/public_html");
5378
let file_path = public_path.join(PathBuf::from(file_path));
@@ -56,7 +81,21 @@ fn tainted_path_handler_folder_almost_good2(
5681
if file_path.starts_with(public_path) {
5782
return Err(Error::from_status(StatusCode::BAD_REQUEST));
5883
}
59-
fs::read_to_string(file_path).map_err(InternalServerError) // $ path-injection-sink MISSING: Alert[rust/path-injection]=remote5 -- we cannot resolve the `join` call above, because it needs a `PathBuf -> Path` `Deref`
84+
fs::read_to_string(file_path).map_err(InternalServerError) // $ path-injection-sink MISSING: Alert[rust/path-injection]=remote4 -- we cannot resolve the `join` call above, because it needs a `PathBuf -> Path` `Deref`
85+
}
86+
87+
//#[handler]
88+
fn tainted_path_handler_folder_almost_good3(
89+
Query(file_path): Query<String>, // $ MISSING: Source=remote5
90+
) -> Result<String> {
91+
let public_path = "/var/www/public_html";
92+
let file_path = Path::new(&file_path);
93+
// BAD: the starts_with check is ineffective before canonicalization, the path could still contain `..`
94+
if !file_path.starts_with(public_path) {
95+
return Err(Error::from_status(StatusCode::BAD_REQUEST));
96+
}
97+
let file_path = file_path.canonicalize().unwrap();
98+
fs::read_to_string(file_path).map_err(InternalServerError) // $ path-injection-sink MISSING: Alert[rust/path-injection]=remote5
6099
}
61100

62101
fn sinks(path1: &Path, path2: &Path) {

0 commit comments

Comments
 (0)