From f238d59b31decfce1ecd593ddacf6f853a27071d Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 2 Dec 2026 23:20:51 -0800 Subject: [PATCH] Tests: quote temp paths as script strings in the evalFile refusal tests script_eval_file_outside_scope_refused and script_eval_file_keeps_the_refusal_when_no_uri_form_is_read put the temp folder path into a single-quoted script string without escaping it. A path with an apostrophe, backslash or line break ended the string, and the run failed with a syntax error instead of the refusal under test. The tests now write the path as a JSON-escaped string literal, and their folder names contain an apostrophe so the quoting is exercised. --- crates/engine/src/tests/script_include.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/tests/script_include.rs b/crates/engine/src/tests/script_include.rs index 8c4276cfce..3ab8f8be82 100644 --- a/crates/engine/src/tests/script_include.rs +++ b/crates/engine/src/tests/script_include.rs @@ -24,6 +24,11 @@ fn write(path: &Path, text: &str) { std::fs::write(path, text).unwrap(); } +/// `text` as a quoted script string literal, with its quotes, backslashes and line breaks escaped. +fn js_string(text: &str) -> String { + serde_json::to_string(text).unwrap() +} + /// Runs `main/main.jsx` in `base`, with the text `text`. fn run(base: &Path, text: &str) -> Result { let main = base.join("main/main.jsx"); @@ -443,11 +448,12 @@ fn script_file_name_names_the_file_of_the_code_that_reads_it() { #[test] fn script_eval_file_outside_scope_refused() { - let base = folder("evaloutside"); + // The apostrophe in the folder name would end a quoted script string. + let base = folder("eval'outside"); write(&base.join("outside.jsx"), "1;"); - let absolute = base.join("outside.jsx").to_string_lossy().replace('\\', "/"); + let absolute = js_string(&base.join("outside.jsx").to_string_lossy().replace('\\', "/")); let script = format!( - "var out = [];\ntry {{ $.evalFile('../outside.jsx'); }} catch (e) {{ out.push(String(e)); }}\ntry {{ $.evalFile('{absolute}'); }} catch (e) {{ out.push(String(e)); }}\nvc.log(out);" + "var out = [];\ntry {{ $.evalFile('../outside.jsx'); }} catch (e) {{ out.push(String(e)); }}\ntry {{ $.evalFile({absolute}); }} catch (e) {{ out.push(String(e)); }}\nvc.log(out);" ); let r = run(&base, &script).unwrap(); let out = r["log"][0].as_array().unwrap(); @@ -466,10 +472,10 @@ fn script_eval_file_outside_scope_refused() { #[cfg(not(windows))] #[test] fn script_eval_file_keeps_the_refusal_when_no_uri_form_is_read() { - let base = folder("evaluri"); + let base = folder("eval'uri"); write(&base.join("out%20side.jsx"), "1;"); let absolute = base.join("out%20side.jsx").to_string_lossy().into_owned(); - let script = format!("try {{ $.evalFile('{absolute}'); }} catch (e) {{ vc.log(String(e)); }}"); + let script = format!("try {{ $.evalFile({}); }} catch (e) {{ vc.log(String(e)); }}", js_string(&absolute)); let r = run(&base, &script).unwrap(); let e = r["log"][0].as_str().unwrap_or_default().to_string(); assert!(e.starts_with(&format!("Error: script file read refused: {absolute}")), "{e}");