Skip to content

Commit 3dd227f

Browse files
committed
fix(config): escape single quotes and harden JS script tests
Escape single quotes in file paths interpolated into the Node.js eval script, preventing JS syntax errors for paths like `/user's projects/`. Also improve the raw-path-import test to structurally verify the import() argument rather than checking absence of a specific string literal.
1 parent 8c93aaa commit 3dd227f

1 file changed

Lines changed: 26 additions & 3 deletions

File tree

src/rcfile/javascript.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ pub fn from_javascript_path(file_path: &Path) -> Result<Rcfile, RcfileError> {
5858
}
5959

6060
fn build_nodejs_script(file_path: &Path) -> String {
61-
let escaped_file_path_for_nodejs = file_path.to_string_lossy().replace('\\', "\\\\");
61+
let escaped_file_path_for_nodejs = file_path
62+
.to_string_lossy()
63+
.replace('\\', "\\\\")
64+
.replace('\'', "\\'");
6265
format!(
6366
r#"
6467
const {{ pathToFileURL }} = require('node:url');
@@ -161,9 +164,15 @@ mod tests {
161164
fn script_does_not_import_raw_path() {
162165
let path = PathBuf::from("/some/path/syncpack.config.cjs");
163166
let script = build_nodejs_script(&path);
167+
// Extract the import() argument — should NOT be a bare string literal
168+
let import_call = script
169+
.find("import(")
170+
.expect("script should contain import()");
171+
let after_import = &script[import_call + "import(".len()..];
164172
assert!(
165-
!script.contains("import('/some/path/syncpack.config.cjs')"),
166-
"import() should not use a raw file path directly"
173+
after_import.starts_with("pathToFileURL("),
174+
"import() argument should be pathToFileURL(...), got: {}",
175+
&after_import[..after_import.find(')').unwrap_or(60)]
167176
);
168177
}
169178

@@ -187,6 +196,20 @@ mod tests {
187196
);
188197
}
189198

199+
#[test]
200+
fn script_escapes_single_quotes_in_paths() {
201+
let path = PathBuf::from("/user's projects/syncpack.config.cjs");
202+
let script = build_nodejs_script(&path);
203+
assert!(
204+
script.contains("/user\\'s projects/syncpack.config.cjs"),
205+
"single quotes in paths should be escaped to avoid JS syntax errors"
206+
);
207+
assert!(
208+
!script.contains("/user's projects/"),
209+
"unescaped single quote should not appear in the script"
210+
);
211+
}
212+
190213
#[test]
191214
fn loads_cjs_config_from_fixture() {
192215
let fixture_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))

0 commit comments

Comments
 (0)