From fffe6d83c30226004eccdb46622a23cd0646816c Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Wed, 28 Oct 2026 00:25:21 -0500 Subject: [PATCH] fix(id): make sanitize_file_name enforce job-directory containment (#1595) --- pkgs/id/src/world_compile.rs | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/pkgs/id/src/world_compile.rs b/pkgs/id/src/world_compile.rs index 936b0f2af9..64758d0812 100644 --- a/pkgs/id/src/world_compile.rs +++ b/pkgs/id/src/world_compile.rs @@ -13,7 +13,7 @@ //! world platform declares none), and the cache lives inside the job //! directory so concurrent jobs cannot interfere. -use std::path::{Path, PathBuf}; +use std::path::{Component, Path, PathBuf}; use std::process::Stdio; use std::time::Duration; @@ -503,8 +503,13 @@ fn rewrite_platform_path(main: &str) -> Result { } fn sanitize_file_name(name: &str) -> Result { + // The name must be exactly one normal path component: no parent + // references, no separators, no root. (Splitting on '.' cannot + // detect ".." — its parts are maximal non-dot runs and never + // contain a dot.) + let mut components = Path::new(name).components(); ensure!( - name.split('.').all(|part| part != "." && part != ".."), + matches!(components.next(), Some(Component::Normal(_))) && components.next().is_none(), "file name {name:?} must stay inside the job directory" ); Ok(name.to_owned()) @@ -597,6 +602,7 @@ mod tests { validate_spec(&spec(vec![("main.roc", "app [x] {}")])).unwrap(); assert!(validate_spec(&spec(vec![("lib.roc", "x")])).is_err()); assert!(validate_spec(&spec(vec![("main.roc", "x"), ("../evil.roc", "y")])).is_err()); + assert!(validate_spec(&spec(vec![("main.roc", "x"), (".hidden", "y")])).is_err()); let too_big = spec(vec![("main.roc", &"x".repeat(MAX_COMPILE_FILE_BYTES + 1))]); assert!(validate_spec(&too_big).is_err()); @@ -607,6 +613,28 @@ mod tests { assert!(validate_spec(&spec(many)).is_err()); } + #[test] + fn sanitize_file_name_refuses_traversal() { + for name in [ + "..", + ".", + "", + "../evil.roc", + "../../evil.roc", + "a/../../evil.roc", + "sub/Foo.roc", + "/etc/passwd", + ] { + assert!( + sanitize_file_name(name).is_err(), + "sanitize_file_name({name:?}) must refuse" + ); + } + for name in ["main.roc", "Helper.roc", "a..b.roc", "foo-bar_baz.roc"] { + assert_eq!(sanitize_file_name(name).unwrap(), name); + } + } + #[test] fn duplicate_file_names_are_refused() { // A duplicated helper: staging writes both to the same job path, so