Skip to content

Commit 887baf9

Browse files
committed
fix(runtime,desktop): harden Node 24 version parity, sidecar archive security, and Desktop PATH invariants
Signed-off-by: song <xinxinsong66@gmail.com>
1 parent 895970c commit 887baf9

10 files changed

Lines changed: 1164 additions & 139 deletions

File tree

‎apps/desktop/loopx-control-plane/src-tauri/src/services.rs‎

Lines changed: 67 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,11 +1224,12 @@ const NVM_ALIAS_DEPTH_LIMIT: usize = 8;
12241224
// accepted forms; anything else is a non-version alias name to follow.
12251225
#[cfg(unix)]
12261226
fn nvm_version(name: &str) -> Option<(u64, u64, u64)> {
1227-
let trimmed = name.trim().strip_prefix('v').unwrap_or(name.trim());
1227+
let trimmed = name.trim();
1228+
let trimmed = trimmed.strip_prefix('v').unwrap_or(trimmed);
12281229
let mut fields = trimmed.split('.');
1229-
let major = fields.next()?.parse().ok()?;
1230-
let minor = fields.next()?.parse().ok()?;
1231-
let patch = fields.next()?.parse().ok()?;
1230+
let major = parse_ascii_u64(fields.next()?)?;
1231+
let minor = parse_ascii_u64(fields.next()?)?;
1232+
let patch = parse_ascii_u64(fields.next()?)?;
12321233
fields.next().is_none().then_some((major, minor, patch))
12331234
}
12341235

@@ -1359,27 +1360,56 @@ fn bundled_node_candidates_for_app(app_executable: &Path, is_windows: bool) -> V
13591360
candidates
13601361
}
13611362

1362-
fn parse_node_version(raw: &str) -> Option<((u64, u64, u64), bool)> {
1363+
fn parse_ascii_u64(raw: &str) -> Option<u64> {
1364+
if raw.is_empty() || !raw.bytes().all(|byte| byte.is_ascii_digit()) {
1365+
return None;
1366+
}
1367+
raw.parse::<u64>().ok()
1368+
}
1369+
1370+
fn is_valid_semver_identifiers(raw: &str) -> bool {
1371+
!raw.is_empty()
1372+
&& raw.split('.').all(|segment| {
1373+
!segment.is_empty()
1374+
&& segment
1375+
.bytes()
1376+
.all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
1377+
})
1378+
}
1379+
1380+
fn parse_node_version_parts(raw: &str) -> Option<((u64, u64, u64), Option<&str>)> {
13631381
let trimmed = raw.trim();
13641382
let without_v = trimmed.strip_prefix('v').unwrap_or(trimmed);
1365-
let without_build = without_v.split('+').next()?;
1366-
let (core, is_prerelease) = match without_build.split_once('-') {
1383+
let without_build = match without_v.split_once('+') {
1384+
Some((before_build, build)) => {
1385+
if !is_valid_semver_identifiers(build) {
1386+
return None;
1387+
}
1388+
before_build
1389+
}
1390+
None => without_v,
1391+
};
1392+
let (core, prerelease) = match without_build.split_once('-') {
13671393
Some((base, prerelease)) => {
1368-
if prerelease.is_empty() {
1394+
if !is_valid_semver_identifiers(prerelease) {
13691395
return None;
13701396
}
1371-
(base, true)
1397+
(base, Some(prerelease))
13721398
}
1373-
None => (without_build, false),
1399+
None => (without_build, None),
13741400
};
13751401
let mut fields = core.split('.');
1376-
let major = fields.next()?.parse::<u64>().ok()?;
1377-
let minor = fields.next()?.parse::<u64>().ok()?;
1378-
let patch = fields.next()?.parse::<u64>().ok()?;
1402+
let major = parse_ascii_u64(fields.next()?)?;
1403+
let minor = parse_ascii_u64(fields.next()?)?;
1404+
let patch = parse_ascii_u64(fields.next()?)?;
13791405
if fields.next().is_some() {
13801406
return None;
13811407
}
1382-
Some(((major, minor, patch), is_prerelease))
1408+
Some(((major, minor, patch), prerelease))
1409+
}
1410+
1411+
fn parse_node_version(raw: &str) -> Option<((u64, u64, u64), bool)> {
1412+
parse_node_version_parts(raw).map(|(triple, prerelease)| (triple, prerelease.is_some()))
13831413
}
13841414

13851415
fn is_supported_node_version(triple: (u64, u64, u64), is_prerelease: bool) -> bool {
@@ -1417,15 +1447,22 @@ fn node_executable_in_dir(directory: &Path) -> Option<PathBuf> {
14171447
}
14181448
}
14191449

1420-
fn probe_node_executable_version(executable: &Path) -> Option<((u64, u64, u64), bool)> {
1450+
fn probe_node_executable_banner(executable: &Path) -> Option<String> {
14211451
let mut probe = Command::new(executable);
14221452
probe.arg("--version");
1453+
// Ambient NODE_OPTIONS (e.g. IDE preloaders or legacy Node flags) must not
1454+
// cause a valid Node 24 binary version check to exit non-zero.
1455+
probe.env_remove("NODE_OPTIONS");
14231456
let output = timed_output(probe)?;
14241457
if !output.status.success() {
14251458
return None;
14261459
}
1427-
let text = std::str::from_utf8(&output.stdout).ok()?;
1428-
parse_node_version(text)
1460+
String::from_utf8(output.stdout).ok()
1461+
}
1462+
1463+
fn probe_node_executable_version(executable: &Path) -> Option<((u64, u64, u64), bool)> {
1464+
let banner = probe_node_executable_banner(executable)?;
1465+
parse_node_version(&banner)
14291466
}
14301467

14311468
fn probe_node_version_in_dir(directory: &Path) -> Option<((u64, u64, u64), bool)> {
@@ -1604,24 +1641,27 @@ fn promote_qualified_node_directory(paths: &mut Vec<PathBuf>) {
16041641
return;
16051642
}
16061643

1607-
let mut best: Option<(usize, (u64, u64, u64))> = None;
1644+
let mut best: Option<(usize, (u64, u64, u64), bool)> = None;
16081645
for (index, directory) in paths.iter().enumerate().skip(first_node_index + 1) {
16091646
let Some((version, is_prerelease)) = probe_node_version_in_dir(directory) else {
16101647
continue;
16111648
};
16121649
if !is_supported_node_version(version, is_prerelease) {
16131650
continue;
16141651
}
1652+
let is_stable = !is_prerelease;
16151653
let is_better = match best {
16161654
None => true,
1617-
Some((_, best_version)) => version > best_version,
1655+
Some((_, best_version, best_stable)) => {
1656+
(version, is_stable) > (best_version, best_stable)
1657+
}
16181658
};
16191659
if is_better {
1620-
best = Some((index, version));
1660+
best = Some((index, version, is_stable));
16211661
}
16221662
}
16231663

1624-
if let Some((best_index, _)) = best {
1664+
if let Some((best_index, _, _)) = best {
16251665
let promoted = paths.remove(best_index);
16261666
paths.insert(first_node_index, promoted);
16271667
}
@@ -2014,22 +2054,12 @@ fn node_environment_in_path(search_path: Option<&OsStr>) -> (bool, Option<String
20142054
let executable = resolve_executable_path("node", search_path);
20152055
let found = executable.is_some();
20162056
let version = executable.and_then(|node| {
2017-
let mut probe = Command::new(node);
2018-
probe.arg("--version");
2019-
timed_output(probe)
2020-
.filter(|output| output.status.success())
2021-
.and_then(|output| {
2022-
let text = std::str::from_utf8(&output.stdout)
2023-
.ok()?
2024-
.trim()
2025-
.strip_prefix('v')?;
2026-
let fields: Vec<_> = text.split('.').collect();
2027-
(fields.len() == 3
2028-
&& fields.iter().all(|field| {
2029-
!field.is_empty() && field.bytes().all(|byte| byte.is_ascii_digit())
2030-
}))
2031-
.then(|| text.to_owned())
2032-
})
2057+
let banner = probe_node_executable_banner(&node)?;
2058+
let ((major, minor, patch), prerelease) = parse_node_version_parts(&banner)?;
2059+
Some(match prerelease {
2060+
Some(tag) => format!("{major}.{minor}.{patch}-{tag}"),
2061+
None => format!("{major}.{minor}.{patch}"),
2062+
})
20332063
});
20342064
(found, version)
20352065
}

0 commit comments

Comments
 (0)