Skip to content

Commit 8075063

Browse files
authored
fix(install): reject commit-less git checkouts (#113809)
* fix(install): reject commit-less git checkouts * test(install): fix incomplete-checkout coverage * fix(install): pin incomplete checkout validation * test(install): update pinned checkout stub * fix(install): require checkout HEAD commit object
1 parent c1e31f1 commit 8075063

6 files changed

Lines changed: 122 additions & 0 deletions

File tree

‎scripts/install-cli.sh‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,6 +1186,11 @@ install_openclaw_from_git() {
11861186
ensure_pnpm
11871187
ensure_pnpm_binary_for_scripts
11881188

1189+
if [[ -d "$repo_dir/.git" ]] &&
1190+
! git --git-dir="$repo_dir/.git" --work-tree="$repo_dir" rev-parse --verify --quiet 'HEAD^{commit}' >/dev/null 2>&1; then
1191+
fail "Git checkout has no commit: ${repo_dir}. Move or remove this incomplete checkout, then retry."
1192+
fi
1193+
11891194
if [[ -d "$repo_dir/.git" ]]; then
11901195
:
11911196
elif [[ -d "$repo_dir" ]]; then

‎scripts/install.ps1‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,24 @@ function Install-OpenClaw {
14591459
}
14601460

14611461
# Install OpenClaw from GitHub
1462+
function Assert-GitCheckoutHasCommit {
1463+
param([string]$RepoDir)
1464+
1465+
$gitDir = Join-Path $RepoDir ".git"
1466+
if (-not (Test-Path -LiteralPath $gitDir -PathType Container)) {
1467+
return
1468+
}
1469+
1470+
$hasHead = $false
1471+
try {
1472+
git "--git-dir=$gitDir" "--work-tree=$RepoDir" rev-parse --verify --quiet "HEAD^{commit}" 2>$null | Out-Null
1473+
$hasHead = ($LASTEXITCODE -eq 0)
1474+
} catch {}
1475+
if (-not $hasHead) {
1476+
throw "Git checkout has no commit: $RepoDir. Move or remove this incomplete checkout, then retry."
1477+
}
1478+
}
1479+
14621480
function Install-OpenClawFromGit {
14631481
param(
14641482
[string]$RepoDir,
@@ -1471,6 +1489,7 @@ function Install-OpenClawFromGit {
14711489
$repoUrl = "https://github.com/openclaw/openclaw.git"
14721490
Write-Host "[*] Installing OpenClaw from GitHub ($repoUrl)..." -ForegroundColor Yellow
14731491

1492+
Assert-GitCheckoutHasCommit -RepoDir $RepoDir
14741493
if (-not (Test-Path $RepoDir)) {
14751494
git clone $repoUrl $RepoDir
14761495
}

‎scripts/install.sh‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2418,6 +2418,21 @@ checkout_git_openclaw_ref() {
24182418
return 1
24192419
}
24202420

2421+
validate_git_checkout_head() {
2422+
local repo_dir="$1"
2423+
2424+
if [[ ! -d "$repo_dir/.git" ]]; then
2425+
return 0
2426+
fi
2427+
if git --git-dir="$repo_dir/.git" --work-tree="$repo_dir" rev-parse --verify --quiet 'HEAD^{commit}' >/dev/null 2>&1; then
2428+
return 0
2429+
fi
2430+
2431+
ui_error "Git checkout has no commit: ${repo_dir}"
2432+
ui_info "Move or remove this incomplete checkout, then retry the installer."
2433+
return 1
2434+
}
2435+
24212436
git_install_lockfile_flag() {
24222437
local repo_dir="$1"
24232438
local ref="$2"
@@ -2873,6 +2888,7 @@ install_openclaw_from_git() {
28732888
ensure_pnpm
28742889
ensure_pnpm_binary_for_scripts
28752890

2891+
validate_git_checkout_head "$repo_dir" || return 1
28762892
if [[ ! -d "$repo_dir" ]]; then
28772893
mkdir -p "$(dirname "$repo_dir")"
28782894
run_quiet_step "Cloning OpenClaw" git clone "$repo_url" "$repo_dir"

‎test/scripts/install-cli.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,38 @@ function linkRequiredShellTools(bin: string) {
4141
describe("install-cli.sh", () => {
4242
const script = readFileSync(SCRIPT_PATH, "utf8");
4343

44+
it("rejects a git checkout without a commit before updating it", () => {
45+
const result = runInstallCliShell(`
46+
set -euo pipefail
47+
source "${SCRIPT_PATH}"
48+
tmp="$(mktemp -d)"
49+
repo="$tmp/repo"
50+
mkdir -p "$repo/.git"
51+
ensure_git() { :; }
52+
ensure_pnpm() { :; }
53+
ensure_pnpm_binary_for_scripts() { :; }
54+
git() {
55+
[[ "$1" == "--git-dir=$repo/.git" ]] &&
56+
[[ "$2" == "--work-tree=$repo" ]] &&
57+
[[ "$3" == "rev-parse" ]] &&
58+
[[ "$4" == "--verify" ]] &&
59+
[[ "$5" == "--quiet" ]] &&
60+
[[ "$6" == "HEAD^{commit}" ]] &&
61+
return 1
62+
return 99
63+
}
64+
65+
set +e
66+
(install_openclaw_from_git "$repo")
67+
status="$?"
68+
set -e
69+
[[ "$status" -eq 1 ]]
70+
[[ -d "$repo/.git" ]]
71+
`);
72+
73+
expect(result.status).toBe(0);
74+
});
75+
4476
it("bounds stalled curl downloads and propagates timeout failures", () => {
4577
const result = runInstallCliShell(`
4678
set -euo pipefail

‎test/scripts/install-ps1.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,19 @@ describe("install.ps1 failure handling", () => {
657657
expect(gitInstallBody).not.toContain("NPM_CONFIG_SCRIPT_SHELL");
658658
});
659659

660+
it("rejects a git checkout without a commit before updating it", () => {
661+
const guardBody = extractFunctionBody(source, "Assert-GitCheckoutHasCommit");
662+
const gitInstallBody = extractFunctionBody(source, "Install-OpenClawFromGit");
663+
664+
expect(guardBody).toContain('"--git-dir=$gitDir"');
665+
expect(guardBody).toContain('"--work-tree=$RepoDir"');
666+
expect(guardBody).toContain('rev-parse --verify --quiet "HEAD^{commit}"');
667+
expect(guardBody).toContain("Git checkout has no commit");
668+
expect(guardBody).not.toContain("Remove-Item");
669+
expect(guardBody).not.toContain("Move-Item");
670+
expect(gitInstallBody).toContain("Assert-GitCheckoutHasCommit -RepoDir $RepoDir");
671+
});
672+
660673
it("runs Windows command shims from a Windows-local cwd", () => {
661674
const commandSafeBody = extractFunctionBody(source, "Invoke-CommandFromWindowsSafeDirectory");
662675
const npmCommandBody = extractFunctionBody(source, "Invoke-NpmCommand");

‎test/scripts/install-sh.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,9 @@ NODE
323323
ui_success() { :; }
324324
ui_error() { printf 'error:%s\\n' "$*"; }
325325
git() {
326+
if [[ "$1" == "--git-dir=$repo/.git" && "$2" == "--work-tree=$repo" && "$3" == "rev-parse" && "$6" == "HEAD^{commit}" ]]; then
327+
return 0
328+
fi
326329
if [[ "$1" == "-C" && "$3" == "status" ]]; then
327330
return 0
328331
fi
@@ -344,6 +347,40 @@ NODE
344347
expect(result.stdout).toContain("/repo/dist/entry.js --version");
345348
});
346349

350+
it("rejects a git checkout without a commit without modifying it", () => {
351+
const result = runInstallShell(`
352+
set -euo pipefail
353+
source "${SCRIPT_PATH}"
354+
tmp="$(mktemp -d)"
355+
parent="$tmp/parent"
356+
repo="$parent/repo"
357+
git -C "$tmp" init -q parent
358+
git -C "$parent" config user.email [email protected]
359+
git -C "$parent" config user.name test
360+
touch "$parent/seed"
361+
git -C "$parent" add seed
362+
git -C "$parent" commit -qm seed
363+
mkdir -p "$repo"
364+
git -C "$repo" init -q
365+
printf 'ref: refs/heads/main\\n' > "$repo/.git/HEAD"
366+
mkdir -p "$repo/.git/refs/heads"
367+
printf '1111111111111111111111111111111111111111\\n' > "$repo/.git/refs/heads/main"
368+
printf 'keep\\n' > "$repo/local.txt"
369+
ui_info() { :; }
370+
ui_error() { :; }
371+
372+
set +e
373+
validate_git_checkout_head "$repo"
374+
status="$?"
375+
set -e
376+
[[ "$status" -eq 1 ]]
377+
[[ -f "$repo/local.txt" ]]
378+
[[ -d "$repo/.git" ]]
379+
`);
380+
381+
expect(result.status).toBe(0);
382+
});
383+
347384
it("accepts GNU and musl Linux shells in OS detection", () => {
348385
expect(script).toContain('[[ "$OSTYPE" == "linux"* ]]');
349386
expect(script).not.toContain('[[ "$OSTYPE" == "linux-gnu"* ]]');

0 commit comments

Comments
 (0)