Skip to content

Commit 596386e

Browse files
wpfleger96npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7
andauthored
feat(node): add Windows managed Node.js fallback (win-x64 + win-arm64) (#2661)
Signed-off-by: Will Pfleger <[email protected]> Co-authored-by: npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7 <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@buzz.block.builderlab.xyz>
1 parent 269ef35 commit 596386e

2 files changed

Lines changed: 330 additions & 36 deletions

File tree

‎desktop/src-tauri/src/commands/agent_discovery/managed_node.rs‎

Lines changed: 304 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,27 @@ const MANAGED_NODE_ARTIFACT: Option<ManagedNodeArtifact> = Some(ManagedNodeArtif
4343
sha256: "4786d00c4d259d3ff0b2328307f764ef3ced65f2d6e9502d433e68d66238509d",
4444
});
4545

46+
#[cfg(all(target_os = "windows", target_arch = "x86_64"))]
47+
const MANAGED_NODE_ARTIFACT: Option<ManagedNodeArtifact> = Some(ManagedNodeArtifact {
48+
platform: "win-x64",
49+
filename: "node-v24.11.0-win-x64.zip",
50+
sha256: "1054540bce22b54ec7e50ebc078ec5d090700a77657607a58f6a64df21f49fdd",
51+
});
52+
53+
#[cfg(all(target_os = "windows", target_arch = "aarch64"))]
54+
const MANAGED_NODE_ARTIFACT: Option<ManagedNodeArtifact> = Some(ManagedNodeArtifact {
55+
platform: "win-arm64",
56+
filename: "node-v24.11.0-win-arm64.zip",
57+
sha256: "12d3b1aa9696b7411e115a4fa2aef57f95560b5ee16bb62cd69843e535ec72be",
58+
});
59+
4660
#[cfg(not(any(
4761
all(target_os = "macos", target_arch = "aarch64"),
4862
all(target_os = "macos", target_arch = "x86_64"),
4963
all(target_os = "linux", target_arch = "x86_64"),
50-
all(target_os = "linux", target_arch = "aarch64")
64+
all(target_os = "linux", target_arch = "aarch64"),
65+
all(target_os = "windows", target_arch = "x86_64"),
66+
all(target_os = "windows", target_arch = "aarch64")
5167
)))]
5268
const MANAGED_NODE_ARTIFACT: Option<ManagedNodeArtifact> = None;
5369

@@ -180,7 +196,12 @@ fn install_managed_node_runtime(
180196
extract_managed_node_archive(&archive_path, &temp_dir, artifact.filename)?;
181197
let _ = std::fs::remove_file(&archive_path);
182198

183-
let extracted_dir = temp_dir.join(artifact.filename.trim_end_matches(".tar.gz"));
199+
let extracted_dir = temp_dir.join(
200+
artifact
201+
.filename
202+
.trim_end_matches(".tar.gz")
203+
.trim_end_matches(".zip"),
204+
);
184205
let source_dir = if extracted_dir.is_dir() {
185206
extracted_dir
186207
} else {
@@ -277,22 +298,106 @@ fn extract_managed_node_archive(
277298
dest_dir: &std::path::Path,
278299
filename: &str,
279300
) -> Result<(), String> {
280-
if !filename.ends_with(".tar.gz") {
281-
return Err(format!("unsupported managed Node.js archive: {filename}"));
282-
}
283-
let file =
284-
std::fs::File::open(archive_path).map_err(|e| format!("open Node.js archive: {e}"))?;
285-
let decoder = flate2::read::GzDecoder::new(file);
286-
let mut archive = tar::Archive::new(decoder);
287-
validate_managed_node_archive_entries(&mut archive)?;
288-
289-
let file = std::fs::File::open(archive_path)
290-
.map_err(|e| format!("open Node.js archive for extraction: {e}"))?;
291-
let decoder = flate2::read::GzDecoder::new(file);
292-
let mut archive = tar::Archive::new(decoder);
293-
archive
294-
.unpack(dest_dir)
295-
.map_err(|e| format!("extract Node.js archive: {e}"))
301+
if filename.ends_with(".tar.gz") {
302+
let file =
303+
std::fs::File::open(archive_path).map_err(|e| format!("open Node.js archive: {e}"))?;
304+
let decoder = flate2::read::GzDecoder::new(file);
305+
let mut archive = tar::Archive::new(decoder);
306+
validate_managed_node_archive_entries(&mut archive)?;
307+
308+
let file = std::fs::File::open(archive_path)
309+
.map_err(|e| format!("open Node.js archive for extraction: {e}"))?;
310+
let decoder = flate2::read::GzDecoder::new(file);
311+
let mut archive = tar::Archive::new(decoder);
312+
archive
313+
.unpack(dest_dir)
314+
.map_err(|e| format!("extract Node.js archive: {e}"))
315+
} else if filename.ends_with(".zip") {
316+
let file =
317+
std::fs::File::open(archive_path).map_err(|e| format!("open Node.js archive: {e}"))?;
318+
let mut archive =
319+
zip::ZipArchive::new(file).map_err(|e| format!("read Node.js zip archive: {e}"))?;
320+
validate_managed_node_zip_entries(&archive)?;
321+
extract_managed_node_zip(&mut archive, dest_dir)
322+
} else {
323+
Err(format!("unsupported managed Node.js archive: {filename}"))
324+
}
325+
}
326+
327+
/// Validate ZIP entry names using platform-neutral string logic.
328+
///
329+
/// `std::path::Path` is intentionally avoided: its `is_absolute()` and
330+
/// `Component` parsing use BUILD-HOST grammar, so `/etc/passwd` is not
331+
/// `is_absolute()` on Windows (no drive prefix), causing the check to lie on
332+
/// the platform this guard exists to protect. Instead we apply pure string
333+
/// rules that produce identical results on every host:
334+
///
335+
/// - Unix-rooted: starts with `/`
336+
/// - Windows-rooted: starts with `\`, has a drive prefix (`X:`), or is UNC
337+
/// (`\\` / `//`)
338+
/// - Traversal: any component that is `..` when split on EITHER `/` or `\`
339+
fn validate_managed_node_zip_entries(
340+
archive: &zip::ZipArchive<std::fs::File>,
341+
) -> Result<(), String> {
342+
for i in 0..archive.len() {
343+
let name = archive
344+
.name_for_index(i)
345+
.ok_or_else(|| format!("Node.js zip entry {i}: missing name"))?;
346+
347+
// Absolute-path checks (platform-neutral).
348+
if name.starts_with('/') || name.starts_with('\\') {
349+
return Err(format!("Node.js zip contains absolute path: {name}"));
350+
}
351+
// Drive prefix: one ASCII letter followed by ':'
352+
if name.len() >= 2 && name.as_bytes()[1] == b':' && name.as_bytes()[0].is_ascii_alphabetic()
353+
{
354+
return Err(format!("Node.js zip contains absolute path: {name}"));
355+
}
356+
// UNC prefix: // or \\ (covered by starts_with checks above for \\,
357+
// and // is caught by starts_with('/') then a second '/' — belt + suspenders).
358+
// (Already caught by the starts_with checks above; explicit for clarity.)
359+
360+
// Traversal: split on both separators and check each component.
361+
let has_traversal = name.split(['/', '\\']).any(|component| component == "..");
362+
if has_traversal {
363+
return Err(format!("Node.js zip contains path traversal: {name}"));
364+
}
365+
}
366+
Ok(())
367+
}
368+
369+
fn extract_managed_node_zip(
370+
archive: &mut zip::ZipArchive<std::fs::File>,
371+
dest_dir: &std::path::Path,
372+
) -> Result<(), String> {
373+
for i in 0..archive.len() {
374+
let mut entry = archive
375+
.by_index(i)
376+
.map_err(|e| format!("Node.js zip entry {i}: {e}"))?;
377+
let outpath = match entry.enclosed_name() {
378+
Some(p) => dest_dir.join(p),
379+
None => {
380+
return Err(format!(
381+
"Node.js zip contains unsafe path: {}",
382+
entry.name()
383+
))
384+
}
385+
};
386+
if entry.is_dir() {
387+
std::fs::create_dir_all(&outpath)
388+
.map_err(|e| format!("create dir in Node.js zip: {e}"))?;
389+
} else {
390+
if let Some(parent) = outpath.parent() {
391+
std::fs::create_dir_all(parent)
392+
.map_err(|e| format!("create parent dir in Node.js zip: {e}"))?;
393+
}
394+
let mut out = std::fs::File::create(&outpath)
395+
.map_err(|e| format!("create file in Node.js zip: {e}"))?;
396+
std::io::copy(&mut entry, &mut out)
397+
.map_err(|e| format!("extract file in Node.js zip: {e}"))?;
398+
}
399+
}
400+
Ok(())
296401
}
297402

298403
fn validate_managed_node_archive_entries<R: std::io::Read>(
@@ -325,15 +430,36 @@ fn validate_managed_node_archive_entries<R: std::io::Read>(
325430
}
326431

327432
fn verify_node_tree(dir: &std::path::Path) -> Result<(), String> {
328-
let node = dir.join("bin").join("node");
329-
let npm = dir.join("bin").join("npm");
330-
if !node.is_file() {
331-
return Err("Node.js archive missing bin/node".to_string());
433+
#[cfg(windows)]
434+
{
435+
// Windows zip layout: node.exe + npm.cmd + npm (POSIX sh shim) at archive root
436+
let node = dir.join("node.exe");
437+
let npm_cmd = dir.join("npm.cmd");
438+
let npm = dir.join("npm");
439+
if !node.is_file() {
440+
return Err("Node.js archive missing node.exe".to_string());
441+
}
442+
if !npm_cmd.is_file() {
443+
return Err("Node.js archive missing npm.cmd".to_string());
444+
}
445+
if !npm.is_file() {
446+
return Err("Node.js archive missing npm".to_string());
447+
}
448+
Ok(())
332449
}
333-
if !npm.is_file() {
334-
return Err("Node.js archive missing bin/npm".to_string());
450+
#[cfg(not(windows))]
451+
{
452+
// Unix tarball layout: bin/node + bin/npm
453+
let node = dir.join("bin").join("node");
454+
let npm = dir.join("bin").join("npm");
455+
if !node.is_file() {
456+
return Err("Node.js archive missing bin/node".to_string());
457+
}
458+
if !npm.is_file() {
459+
return Err("Node.js archive missing bin/npm".to_string());
460+
}
461+
Ok(())
335462
}
336-
Ok(())
337463
}
338464

339465
// ── managed npm adapter installs ──────────────────────────────────────────────
@@ -466,4 +592,157 @@ mod tests {
466592
"'/tmp/Buzz'\\''s Node'"
467593
);
468594
}
595+
596+
// ── zip validation tests ──────────────────────────────────────────────────
597+
598+
/// Build an in-memory zip archive with the supplied entry names and return
599+
/// a temporary file containing it (zip::ZipArchive requires Seek).
600+
fn make_zip_with_entries(entry_names: &[&str]) -> tempfile::NamedTempFile {
601+
let mut buf: Vec<u8> = Vec::new();
602+
{
603+
let mut writer = zip::ZipWriter::new(std::io::Cursor::new(&mut buf));
604+
let opts = zip::write::SimpleFileOptions::default();
605+
for name in entry_names {
606+
writer.start_file(*name, opts).unwrap();
607+
}
608+
writer.finish().unwrap();
609+
}
610+
let mut tmp = tempfile::NamedTempFile::new().unwrap();
611+
std::io::Write::write_all(&mut tmp, &buf).unwrap();
612+
tmp
613+
}
614+
615+
#[test]
616+
fn test_validate_zip_accepts_normal_entries() {
617+
let tmp = make_zip_with_entries(&[
618+
"node-v24.11.0-win-x64/node.exe",
619+
"node-v24.11.0-win-x64/npm.cmd",
620+
"node-v24.11.0-win-x64/npm",
621+
]);
622+
let file = std::fs::File::open(tmp.path()).unwrap();
623+
let archive = zip::ZipArchive::new(file).unwrap();
624+
assert!(validate_managed_node_zip_entries(&archive).is_ok());
625+
}
626+
627+
#[test]
628+
fn test_validate_zip_rejects_absolute_path() {
629+
let tmp = make_zip_with_entries(&["/etc/passwd"]);
630+
let file = std::fs::File::open(tmp.path()).unwrap();
631+
let archive = zip::ZipArchive::new(file).unwrap();
632+
let err = validate_managed_node_zip_entries(&archive).unwrap_err();
633+
assert!(
634+
err.contains("absolute path"),
635+
"expected 'absolute path' in: {err}"
636+
);
637+
}
638+
639+
#[test]
640+
fn test_validate_zip_rejects_path_traversal() {
641+
let tmp = make_zip_with_entries(&["../../../etc/passwd"]);
642+
let file = std::fs::File::open(tmp.path()).unwrap();
643+
let archive = zip::ZipArchive::new(file).unwrap();
644+
let err = validate_managed_node_zip_entries(&archive).unwrap_err();
645+
assert!(
646+
err.contains("path traversal"),
647+
"expected 'path traversal' in: {err}"
648+
);
649+
}
650+
651+
#[test]
652+
fn test_validate_zip_rejects_backslash_rooted() {
653+
// Windows-style absolute path using backslash — must reject on every host.
654+
let tmp = make_zip_with_entries(&["\\Windows\\system32\\evil.dll"]);
655+
let file = std::fs::File::open(tmp.path()).unwrap();
656+
let archive = zip::ZipArchive::new(file).unwrap();
657+
let err = validate_managed_node_zip_entries(&archive).unwrap_err();
658+
assert!(
659+
err.contains("absolute path"),
660+
"expected 'absolute path' in: {err}"
661+
);
662+
}
663+
664+
#[test]
665+
fn test_validate_zip_rejects_drive_prefix() {
666+
// Windows drive-letter absolute path — must reject on every host.
667+
let tmp = make_zip_with_entries(&["C:\\evil\\payload.exe"]);
668+
let file = std::fs::File::open(tmp.path()).unwrap();
669+
let archive = zip::ZipArchive::new(file).unwrap();
670+
let err = validate_managed_node_zip_entries(&archive).unwrap_err();
671+
assert!(
672+
err.contains("absolute path"),
673+
"expected 'absolute path' in: {err}"
674+
);
675+
}
676+
677+
#[test]
678+
fn test_validate_zip_rejects_backslash_traversal() {
679+
// Path traversal using Windows separator — must reject on every host.
680+
let tmp = make_zip_with_entries(&["node-v24.11.0-win-x64\\..\\..\\evil"]);
681+
let file = std::fs::File::open(tmp.path()).unwrap();
682+
let archive = zip::ZipArchive::new(file).unwrap();
683+
let err = validate_managed_node_zip_entries(&archive).unwrap_err();
684+
assert!(
685+
err.contains("path traversal"),
686+
"expected 'path traversal' in: {err}"
687+
);
688+
}
689+
690+
// ── verify_node_tree layout tests ─────────────────────────────────────────
691+
692+
#[test]
693+
fn test_verify_node_tree_unix_layout_passes() {
694+
let tmp = tempfile::TempDir::new().unwrap();
695+
let bin = tmp.path().join("bin");
696+
std::fs::create_dir_all(&bin).unwrap();
697+
std::fs::write(bin.join("node"), b"").unwrap();
698+
std::fs::write(bin.join("npm"), b"").unwrap();
699+
// On non-Windows the unix branch is active — this must pass.
700+
#[cfg(not(windows))]
701+
assert!(verify_node_tree(tmp.path()).is_ok());
702+
// On Windows the windows branch is active — unix layout must fail.
703+
#[cfg(windows)]
704+
assert!(verify_node_tree(tmp.path()).is_err());
705+
}
706+
707+
#[test]
708+
fn test_verify_node_tree_unix_layout_missing_npm_fails() {
709+
let tmp = tempfile::TempDir::new().unwrap();
710+
let bin = tmp.path().join("bin");
711+
std::fs::create_dir_all(&bin).unwrap();
712+
std::fs::write(bin.join("node"), b"").unwrap();
713+
// npm intentionally absent
714+
#[cfg(not(windows))]
715+
{
716+
let err = verify_node_tree(tmp.path()).unwrap_err();
717+
assert!(err.contains("bin/npm"), "err: {err}");
718+
}
719+
}
720+
721+
#[test]
722+
fn test_verify_node_tree_windows_layout_passes() {
723+
let tmp = tempfile::TempDir::new().unwrap();
724+
std::fs::write(tmp.path().join("node.exe"), b"").unwrap();
725+
std::fs::write(tmp.path().join("npm.cmd"), b"").unwrap();
726+
std::fs::write(tmp.path().join("npm"), b"").unwrap();
727+
// On Windows the windows branch is active — this must pass.
728+
#[cfg(windows)]
729+
assert!(verify_node_tree(tmp.path()).is_ok());
730+
// On non-Windows the unix branch is active — windows-layout root files
731+
// don't satisfy bin/node + bin/npm, so this must fail.
732+
#[cfg(not(windows))]
733+
assert!(verify_node_tree(tmp.path()).is_err());
734+
}
735+
736+
#[test]
737+
fn test_verify_node_tree_windows_layout_missing_npm_shim_fails() {
738+
let tmp = tempfile::TempDir::new().unwrap();
739+
std::fs::write(tmp.path().join("node.exe"), b"").unwrap();
740+
std::fs::write(tmp.path().join("npm.cmd"), b"").unwrap();
741+
// npm POSIX shim intentionally absent
742+
#[cfg(windows)]
743+
{
744+
let err = verify_node_tree(tmp.path()).unwrap_err();
745+
assert!(err.contains("npm"), "err: {err}");
746+
}
747+
}
469748
}

0 commit comments

Comments
 (0)