Skip to content

Commit 9c1a9c7

Browse files
Pr0metheangithub-advanced-security[bot]gemini-code-assist[bot]
authored
docs(examples): Fix code scanning alert no. 228: Uncontrolled data used in path expression (#534)
* docs(example): Potential fix for code scanning alert no. 228: Uncontrolled data used in path expression Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> Signed-off-by: Chris Hennick <4961925+Pr0methean@users.noreply.github.com> * Fix variable-shadowing alert Signed-off-by: Chris Hennick <4961925+Pr0methean@users.noreply.github.com> * Apply suggestion from @gemini-code-assist[bot] Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Signed-off-by: Chris Hennick <4961925+Pr0methean@users.noreply.github.com> --------- Signed-off-by: Chris Hennick <4961925+Pr0methean@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent a12eaaf commit 9c1a9c7

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

examples/write-large-file.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,25 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
1212
#[cfg(feature = "_deflate-any")]
1313
{
1414
let filename = &*args[1];
15-
// Ensure that the filename has no path separators or parent directory references
16-
if filename.contains("..") || filename.contains('/') || filename.contains('\\') {
17-
return Err("Invalid filename: path separators or '..' are not allowed".into());
15+
// Ensure that the filename is non-empty and has no path separators or parent directory references.
16+
// WARNING: This check is not sufficient to prevent TOCTOU (Time-of-check to time-of-use)
17+
// race conditions. An attacker could create a symbolic link with a "safe" name that
18+
// points to a sensitive file. When `File::create` is called, it will follow the
19+
// symlink and may overwrite the target file. This is example code and is not
20+
// intended for use in production.
21+
let trimmed_filename = filename.trim();
22+
if trimmed_filename.is_empty()
23+
|| trimmed_filename.contains("..")
24+
|| trimmed_filename.contains('/')
25+
|| trimmed_filename.contains('\\')
26+
{
27+
return Err("Invalid filename: must be a non-empty simple file name without path separators or '..'".into());
1828
}
1929
use std::io::Write;
2030

2131
use zip::write::SimpleFileOptions;
2232

23-
let file = std::fs::File::create(filename)?;
33+
let file = std::fs::File::create(trimmed_filename)?;
2434
let mut zip = zip::ZipWriter::new(file);
2535

2636
let options = SimpleFileOptions::default()

0 commit comments

Comments
 (0)