Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions scripts/dc/validate.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ try {
const semver = createRequire(fs.realpathSync(npm))('semver');
const manifest = read(path.join(root, 'package.json'));
check(manifest.dependencies[name] === expected);
const main = path.resolve(root, key, pkg.main);
check(main.startsWith(path.join(root, key) + '/') && fs.statSync(main).isFile());
const executable = pkg.bin === undefined ? pkg.main : pkg.bin?.['desktop-commander'];
check(typeof executable === 'string' && executable.length > 0 && !path.isAbsolute(executable));
const main = path.resolve(root, key, executable);
const packagePrefix = path.join(root, key) + '/';
check(main.startsWith(packagePrefix) && fs.realpathSync(main).startsWith(packagePrefix) && fs.statSync(main).isFile());
const lock = read(path.join(root, 'package-lock.json'));
const hidden = read(path.join(root, 'node_modules/.package-lock.json'));
const entry = lock.packages[key];
Expand All @@ -52,7 +55,9 @@ try {
check(hidden.packages[key].version === expected && hidden.packages[key].integrity === entry.integrity);
if (pkg._integrity) check(pkg._integrity === entry.integrity);
check(typeof pkg.engines?.node === 'string' && semver.satisfies(process.version, pkg.engines.node));
for (const dependency of Object.values(lock.packages)) {
for (const [relative, dependency] of Object.entries(lock.packages)) {
// Cross-platform optional entries can exist in npm lockfiles without being installed.
if (dependency.optional && relative && !fs.existsSync(path.join(root, relative))) continue;
if (dependency.engines?.node) check(semver.satisfies(process.version, dependency.engines.node));
}
const walk = dir => {
Expand Down
50 changes: 27 additions & 23 deletions scripts/update/lib/dc_local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,36 @@ update_dc_local() {
result_warn "WSL" "Desktop Commander" "Stable upstream version unavailable; installed $installed retained"
elif dc_newer_version "$latest" "$installed"; then
stage="$HOME/.local/share/ixatu-mcp-staged/$latest"
stage_root="${stage%/*}"
candidate="$stage/node_modules/@wonderwhy-er/desktop-commander/package.json"
if [[ ! -f "$candidate" ]]; then
mkdir -p "$(dirname "$stage")"
tmp="$(mktemp -d "$(dirname "$stage")/.dc-stage.XXXXXXXX")"
if npm install --prefix "$tmp" --save-exact --package-lock --ignore-scripts --no-audit --no-fund "@wonderwhy-er/desktop-commander@$latest" >"$LOG_DIR/wsl-desktop-commander.log" 2>&1 &&
[[ "$(node -p "require(process.argv[1]).version" "$tmp/node_modules/@wonderwhy-er/desktop-commander/package.json" 2>/dev/null)" == "$latest" ]]; then
mv "$tmp" "$stage"
else
result_warn "WSL" "Desktop Commander" "Staging failed; installed $installed retained; see log"
if [[ -L "$stage_root" || (-e "$stage_root" && (! -d "$stage_root" || ! -O "$stage_root")) ]] || ! install -d -m 700 -- "$stage_root"; then
result_warn "WSL" "Desktop Commander" "Unsafe staging directory; installed $installed retained"
else
if [[ ! -f "$candidate" ]]; then
tmp="$(mktemp -d "$(dirname "$stage")/.dc-stage.XXXXXXXX")"
if npm install --prefix "$tmp" --save-exact --package-lock --ignore-scripts --no-audit --no-fund "@wonderwhy-er/desktop-commander@$latest" >"$LOG_DIR/wsl-desktop-commander.log" 2>&1 &&
[[ "$(node -p "require(process.argv[1]).version" "$tmp/node_modules/@wonderwhy-er/desktop-commander/package.json" 2>/dev/null)" == "$latest" ]]; then
mv "$tmp" "$stage"
else
result_warn "WSL" "Desktop Commander" "Staging failed; installed $installed retained; see log"
fi
[[ -d "$tmp" ]] && rm -rf "$tmp"
fi
[[ -d "$tmp" ]] && rm -rf "$tmp"
fi
if [[ -f "$candidate" ]] && [[ "$(node -p "require(process.argv[1]).version" "$candidate" 2>/dev/null)" == "$latest" ]]; then
activation_log="$LOG_DIR/wsl-dc-activate.log"
if [[ "${DOTFILES_MCP_AUTO_ACTIVATE:-1}" != 0 ]] &&
"${DOTFILES_ROOT:-$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd)}/bin/dotfiles-dc-activate" "$latest" --updater-apply >"$activation_log" 2>&1; then
tool_snapshot_add "Desktop Commander" "$installed" "$(dc_local_version)"
result_info "WSL" "Desktop Commander" "INSTALLED/ACTIVATED $latest; verified; see activation log"
else
activation_status=ACTIVATION_PENDING
if [[ "${DOTFILES_MCP_AUTO_ACTIVATE:-1}" != 0 && -f "$activation_log" ]]; then
if grep -q 'manual.*recovery' "$activation_log"; then
activation_status='ROLLBACK FAILED: manual recovery required'
elif grep -q ROLLBACK "$activation_log"; then activation_status=ROLLBACK; fi
if [[ -f "$candidate" ]] && [[ "$(node -p "require(process.argv[1]).version" "$candidate" 2>/dev/null)" == "$latest" ]]; then
activation_log="$LOG_DIR/wsl-dc-activate.log"
if [[ "${DOTFILES_MCP_AUTO_ACTIVATE:-1}" != 0 ]] &&
"${DOTFILES_ROOT:-$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd)}/bin/dotfiles-dc-activate" "$latest" --updater-apply >"$activation_log" 2>&1; then
tool_snapshot_add "Desktop Commander" "$installed" "$(dc_local_version)"
result_info "WSL" "Desktop Commander" "INSTALLED/ACTIVATED $latest; verified; see activation log"
else
activation_status=ACTIVATION_PENDING
if [[ "${DOTFILES_MCP_AUTO_ACTIVATE:-1}" != 0 && -f "$activation_log" ]]; then
if grep -q 'manual.*recovery' "$activation_log"; then
activation_status='ROLLBACK FAILED: manual recovery required'
elif grep -q ROLLBACK "$activation_log"; then activation_status=ROLLBACK; fi
fi
result_warn "WSL" "Desktop Commander" "PREPARED $latest; ACTIVE $(dc_local_version); $activation_status; see activation log"
fi
result_warn "WSL" "Desktop Commander" "PREPARED $latest; ACTIVE $(dc_local_version); $activation_status; see activation log"
fi
fi
else
Expand Down
20 changes: 19 additions & 1 deletion scripts/update/update-wsl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,25 @@ run_apt() {
record_version_transition "WSL" "Azure CLI" "$azure_cli_before" "$azure_cli_before"
return 0
fi
run_step "WSL" "APT update" "${LOG_DIR}/wsl-apt-update.log" sudo apt-get update -y
local apt_service apt_state automatic_update=""
if command -v systemctl >/dev/null 2>&1; then
for apt_service in apt-daily.service apt-daily-upgrade.service; do
if apt_state="$(systemctl show "$apt_service" --property=ActiveState --value 2>/dev/null)"; then
case "$apt_state" in
active | activating | reloading)
automatic_update="$apt_service is $apt_state; automatic APT maintenance in progress"
break
;;
esac
fi
done
fi
if [[ -n "$automatic_update" ]]; then
skip "APT update: $automatic_update"
result_skip "WSL" "APT update" "$automatic_update"
else
run_step "WSL" "APT update" "${LOG_DIR}/wsl-apt-update.log" sudo apt-get update -y
fi
run_step "WSL" "APT upgrade" "${LOG_DIR}/wsl-apt-upgrade.log" sudo apt-get upgrade -y
run_step "WSL" "APT autoremove" "${LOG_DIR}/wsl-apt-autoremove.log" sudo apt-get autoremove -y
if ! command -v az >/dev/null 2>&1; then
Expand Down
75 changes: 75 additions & 0 deletions tests/bats/system/dc-activate.bats
Original file line number Diff line number Diff line change
Expand Up @@ -408,3 +408,78 @@ MOCK
[ "$status" -eq 0 ]
[[ "$output" == *'ACTIVE 1.2.0'* ]]
}

# Exercise validation directly: candidate code and service operations must not run.
validate_executable() {
local executable="$1"
node - "$STAGE" "$executable" <<'JS'
const fs = require('fs');
const [root, executable] = process.argv.slice(2);
const file = root + '/node_modules/@wonderwhy-er/desktop-commander/package.json';
const pkg = JSON.parse(fs.readFileSync(file));
// A valid main must not hide an invalid bin declaration.
pkg.bin = {'desktop-commander': executable};
if (executable === 'dist/index.js') delete pkg.main;
fs.writeFileSync(file, JSON.stringify(pkg));
JS
run node "$REPO/scripts/dc/validate.cjs" stage "$STAGE" 0.3.4 "$(command -v npm)"
}

@test "DC validator accepts bin-only package without executing candidate code" {
printf 'throw new Error("candidate executed");\n' >"$STAGE/node_modules/@wonderwhy-er/desktop-commander/dist/index.js"
validate_executable dist/index.js
[ "$status" -eq 0 ]
[ ! -e "$DC_TEST/systemctl" ]
}

@test "DC validator refuses unsafe or absent bin even with valid main fallback" {
for executable in '../../../package.json' /etc/passwd dist/missing.js ''; do
validate_executable "$executable"
[ "$status" -ne 0 ]
done
[ ! -e "$DC_TEST/systemctl" ]
}

@test "DC validator refuses bin symlink outside package and missing executable metadata" {
local package="$STAGE/node_modules/@wonderwhy-er/desktop-commander"
ln -s "$STAGE/package.json" "$package/dist/escape.js"
validate_executable dist/escape.js
[ "$status" -ne 0 ]
node - "$package/package.json" <<'JS'
const fs = require('fs');
const file = process.argv[2], pkg = JSON.parse(fs.readFileSync(file));
delete pkg.bin;
delete pkg.main;
fs.writeFileSync(file, JSON.stringify(pkg));
JS
run node "$REPO/scripts/dc/validate.cjs" stage "$STAGE" 0.3.4 "$(command -v npm)"
[ "$status" -ne 0 ]
[ ! -e "$DC_TEST/systemctl" ]
}

@test "DC validator ignores absent optional Windows engines, but rejects them if installed" {
# npm lockfiles include platform-specific optional entries absent from Linux WSL.
node - "$STAGE" <<'JS'
const fs = require('fs');
const root = process.argv[2];
const path = root + '/package-lock.json';
const lock = JSON.parse(fs.readFileSync(path));
lock.packages['node_modules/@img/sharp-win32-ia32'] = {
version: '0.35.5', optional: true, os: ['win32'], cpu: ['ia32'],
engines: {node: '^20.9.0'}
};
fs.writeFileSync(path, JSON.stringify(lock));
JS
run node "$REPO/scripts/dc/validate.cjs" stage "$STAGE" 0.3.4 "$(command -v npm)"
[ "$status" -eq 0 ]
mkdir -p "$STAGE/node_modules/@img/sharp-win32-ia32"
run node "$REPO/scripts/dc/validate.cjs" stage "$STAGE" 0.3.4 "$(command -v npm)"
[ "$status" -ne 0 ]
}

@test "DC updater corrects writable owned staging parent before activating" {
chmod 775 "$HOME/.local/share/ixatu-mcp-staged"
dc_updater 0.3.4
[[ "$output" == *'INSTALLED/ACTIVATED 0.3.4'* ]]
[ "$(stat -c %a "$HOME/.local/share/ixatu-mcp-staged")" -eq 700 ]
}
85 changes: 85 additions & 0 deletions tests/bats/system/update-workflow.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2397,3 +2397,88 @@ EOF
run grep -Eq '^ups:' "${DOTFILES_DIR}/Makefile" "${DOTFILES_DIR}"/*.mk
[[ "${status}" -ne 0 ]]
}

# Only allow fake sudo/systemctl/az and basic shell utilities in this runner.
run_apt_preflight() {
local daily="$1" upgrade="${2:-inactive}" mock="${3:-0}" apt_exit="${4:-0}"
local stub_dir="${TEST_TEMP_DIR}/apt-bin" runner="${TEST_TEMP_DIR}/apt-runner"
mkdir -p "$stub_dir"
for cmd in bash mkdir dirname date tee tr head sed grep cat perl; do
ln -sf "$(command -v "$cmd")" "$stub_dir/$cmd"
done
cat >"$stub_dir/systemctl" <<'MOCK'
#!/usr/bin/env bash
printf '%s\n' "$*" >>"$APT_TEST_DIR/systemctl.log"
[[ "$1" == show && "$3" == --property=ActiveState && "$4" == --value ]] || exit 99
state="$DAILY_STATE"
[[ "$2" != apt-daily-upgrade.service ]] || state="$UPGRADE_STATE"
[[ "$state" != unavailable ]] || exit 1
printf '%s\n' "$state"
MOCK
[[ "$daily" != missing ]] || rm "$stub_dir/systemctl"
cat >"$stub_dir/sudo" <<'MOCK'
#!/usr/bin/env bash
printf '%s\n' "$*" >>"$APT_TEST_DIR/sudo.log"
[[ "$1" == apt-get ]] || exit 99
[[ "$2" != update ]] || exit "$APT_UPDATE_EXIT"
exit 0
MOCK
cat >"$stub_dir/az" <<'MOCK'
#!/usr/bin/env bash
printf '2.79.0\n'
MOCK
chmod +x "$stub_dir/sudo" "$stub_dir/az"
[[ ! -f "$stub_dir/systemctl" ]] || chmod +x "$stub_dir/systemctl"
cat >"$runner" <<'MOCK'
#!/usr/bin/env bash
set -euo pipefail
set -- --section none
source "$APT_REPO/scripts/update/update-wsl.sh"
run_apt
MOCK
run env PATH="$stub_dir" APT_REPO="$DOTFILES_DIR" APT_TEST_DIR="$TEST_TEMP_DIR" \
DAILY_STATE="$daily" UPGRADE_STATE="$upgrade" APT_UPDATE_EXIT="$apt_exit" \
DOTFILES_UPDATE_MOCK="$mock" DOTFILES_UPDATE_RUN_DIR="$TEST_TEMP_DIR/apt-run" bash "$runner"
[ "$status" -eq 0 ]
}

@test "APT preflight skips duplicate update for active activating or reloading automatic services" {
for service in daily upgrade; do
for state in active activating reloading; do
rm -f "$TEST_TEMP_DIR/sudo.log" "$TEST_TEMP_DIR/systemctl.log"
if [[ "$service" == daily ]]; then run_apt_preflight "$state"; else run_apt_preflight inactive "$state"; fi
grep -q $'SKIP\tWSL\tAPT update\t.*apt-daily' "$TEST_TEMP_DIR/apt-run/wsl-results.tsv"
grep -q "$state" "$TEST_TEMP_DIR/apt-run/wsl-results.tsv"
! grep -q 'apt-get update' "$TEST_TEMP_DIR/sudo.log"
grep -qx 'apt-get upgrade -y' "$TEST_TEMP_DIR/sudo.log"
grep -qx 'apt-get autoremove -y' "$TEST_TEMP_DIR/sudo.log"
[ "$(wc -l <"$TEST_TEMP_DIR/sudo.log")" -eq 2 ]
grep -q $'Azure CLI\t2.79.0\t2.79.0\tunchanged' "$TEST_TEMP_DIR/apt-run/tool-snapshot.tsv"
! grep -Eq 'stop|start|disable|kill' "$TEST_TEMP_DIR/systemctl.log"
done
done
}

@test "APT preflight keeps normal update when services inactive unavailable or systemctl absent" {
for state in inactive failed unavailable missing; do
rm -f "$TEST_TEMP_DIR/sudo.log"
run_apt_preflight "$state"
grep -qx 'apt-get update -y' "$TEST_TEMP_DIR/sudo.log"
grep -q $'OK\tWSL\tAPT update\t' "$TEST_TEMP_DIR/apt-run/wsl-results.tsv"
done
}

@test "APT preflight preserves unrelated update errors and continues upgrade and autoremove" {
run_apt_preflight inactive inactive 0 100
grep -q $'FAIL\tWSL\tAPT update\texit 100;' "$TEST_TEMP_DIR/apt-run/wsl-results.tsv"
grep -qx 'apt-get upgrade -y' "$TEST_TEMP_DIR/sudo.log"
grep -qx 'apt-get autoremove -y' "$TEST_TEMP_DIR/sudo.log"
}

@test "APT mock bypasses preflight and sudo and preserves Azure CLI snapshot" {
run_apt_preflight activating active 1
[ ! -e "$TEST_TEMP_DIR/systemctl.log" ]
[ ! -e "$TEST_TEMP_DIR/sudo.log" ]
grep -q $'OK\tWSL\tAPT\tmocked' "$TEST_TEMP_DIR/apt-run/wsl-results.tsv"
grep -q $'Azure CLI\t2.79.0\t2.79.0\tunchanged' "$TEST_TEMP_DIR/apt-run/tool-snapshot.tsv"
}
Loading