Skip to content

config(io): add OPENCLAW_SKIP_STATE_DIR_HARDEN opt-out for multi-uid sidecar deployments (AI-assisted) - #71205

Closed
chrislangston wants to merge 2 commits into
openclaw:mainfrom
chrislangston:fix/openclaw-skip-state-dir-harden
Closed

chrislangston wants to merge 2 commits into
openclaw:mainfrom
chrislangston:fix/openclaw-skip-state-dir-harden

Conversation

@chrislangston

Copy link
Copy Markdown

Summary

  • Problem: tightenStateDirPermissionsIfNeeded in src/config/io.ts unconditionally calls chmod(configDir, 0o700) on every config save when the config dir matches the state dir. Multi-uid shared-volume deployments (e.g. Fly Machines running a bridge + gateway sidecar pair under different uids on a group-writable setgid state dir) get their sibling-uid access stripped — the dir mode drops from 2770 owner:shared-group to 0700 owner:none-for-group, and the sibling process (bridge, uid 1001) can no longer traverse the state dir to read/write the files it needs (bridge-ready sentinel, etc.). That cascades into a workspace-bootstrap retry storm; each retry triggers another config save, which re-hardens and re-locks the sibling out.
  • Why it matters: This is the top blocker for OpenClaw gateway + sidecar deployments that use shared state via a setgid clawshared group for cross-uid access. Without an opt-out, the only workarounds are patching the chmod call out with a require-shim at runtime, or moving the config to a subdirectory so configDir !== stateDir — both of which are fragile.
  • What changed: Added an env-var opt-out (OPENCLAW_SKIP_STATE_DIR_HARDEN=1). When set, tightenStateDirPermissionsIfNeeded returns early before stat/chmod. Everything else (the win32 skip, the configDir !== stateDir skip, the 0o077-already-zero skip) is untouched. Default behavior is preserved — this is strictly opt-in.
  • What did NOT change (scope boundary): No changes to the actual chmod mode, the default path, or any other call site. The existing "tightens world-writable state dir…" test still passes unchanged, which is the regression guard for the default path.

Change Type (select all)

  • Bug fix
  • Feature (opt-out env var)
  • Refactor required for the fix
  • Docs
  • Security hardening
  • Chore/infra

Scope (select all touched areas)

  • Gateway / orchestration
  • Skills / tool execution
  • Auth / tokens
  • Memory / storage
  • Integrations
  • API / contracts
  • UI / DX
  • CI/CD / infra

Linked Issue/PR

  • Closes #
  • Related #
  • This PR fixes a bug or regression

No existing issue — surfaced while debugging a downstream multi-uid sidecar deployment. Happy to open a tracking issue first if maintainers prefer.

Root Cause

  • Root cause: tightenStateDirPermissionsIfNeeded was designed for the single-uid home-directory case (~/.openclaw). It doesn't account for deployments where the state dir is a setgid group-writable shared volume between two uids, which is a common Fly Machine / Docker sidecar pattern.
  • Missing detection / guardrail: No env-var escape hatch, unlike peer OPENCLAW_SKIP_* / OPENCLAW_ALLOW_* flags (OPENCLAW_SKIP_BROWSER_CONTROL_SERVER, OPENCLAW_ALLOW_MULTI_GATEWAY, OPENCLAW_SKIP_CHANNELS, etc.) that exist for the same class of concern in other subsystems.
  • Contributing context: src/infra/bonjour.ts and src/infra/path-env.ts already use the isTruthyEnvValue(process.env.OPENCLAW_*) pattern for similar behavioral opt-outs; this PR follows the same idiom.

Regression Test Plan

  • Coverage level that should have caught this:
    • Unit test
    • Seam / integration test
    • End-to-end test
    • Existing coverage already sufficient
  • Target test or file: src/config/io.write-config.test.ts
  • Scenario the test should lock in: When OPENCLAW_SKIP_STATE_DIR_HARDEN=1 is set, a state dir pre-created with 0o2770 (setgid + group-rwx, as a multi-uid deployment would mount it) retains mode 0o2770 after a config write — not 0o700.
  • Why this is the smallest reliable guardrail: The default path has an existing companion test ("tightens world-writable state dir when writing the default config", L115) that still passes unchanged. The new test is a direct mirror with the env var flipped, so any regression that re-introduces unconditional chmod would fail this specific case.
  • Existing test that already covers this (if any): None for the opt-out path; the default-behavior test is the sibling case.

User-visible / Behavior Changes

  • New env var: OPENCLAW_SKIP_STATE_DIR_HARDEN. When set to a truthy value (1/on/true/yes), tightenStateDirPermissionsIfNeeded returns without stat'ing or chmod'ing the config dir. Default behavior is unchanged when the var is unset.

Diagram

Before (multi-uid shared-volume setup):
  start.sh chowns /home/node/.openclaw to node:clawshared mode 2770
    → bridge (uid 1001, clawshared) can traverse + write
    → gateway (uid 1000, clawshared) can traverse + write
  gateway writes config → tightenStateDirPermissionsIfNeeded → chmod 0o700
    → mode drops to 0o700 owner=node, no group access
    → bridge (not owner) can no longer traverse → EACCES cascade

After (same setup, with OPENCLAW_SKIP_STATE_DIR_HARDEN=1):
  start.sh chowns /home/node/.openclaw to node:clawshared mode 2770
  gateway writes config → tightenStateDirPermissionsIfNeeded → env guard → return
    → mode stays at 2770 → bridge retains group-rwx access

Security Impact

  • New permissions/capabilities? No. The env var only skips an existing chmod; it cannot widen perms.
  • Secrets/tokens handling changed? No.
  • New/changed network calls? No.
  • Command/tool execution surface changed? No.
  • Data access scope changed? No.
  • Mitigation: The opt-out is strictly opt-in. Deployments that set the env var are explicitly accepting responsibility for setting the state dir mode themselves (typically via a setgid 2770 group-writable directory, as the added test exercises). The default path is unchanged and still hardens to 0o700.

Repro + Verification

Environment

  • OS: Linux (Debian in Docker — Fly Machines)
  • Runtime/container: Docker container with split uids (gateway=1000, bridge=1001) sharing clawshared group (gid 1002) and a Fly Volume mounted at /home/node/.openclaw with setgid 2770 node:clawshared
  • Model/provider: N/A for repro
  • Integration/channel (if any): N/A
  • Relevant config (redacted): OPENCLAW_SKIP_STATE_DIR_HARDEN=1 + GATEWAY_URL=ws://127.0.0.1:18789

Steps to reproduce the original bug (without this PR)

  1. Provision a shared state dir: mkdir -p /srv/state && chown node:clawshared /srv/state && chmod 2770 /srv/state
  2. Run gateway as uid 1000 with HOME=/srv — state dir resolves to /srv/state, matches configDir
  3. Run a second process as uid 1001 that also needs to read/write files under /srv/state via the clawshared group
  4. Observe: after the gateway's first config save, /srv/state is mode 0o700, the uid-1001 process hits EACCES opening anything inside

Expected (with this PR)

With OPENCLAW_SKIP_STATE_DIR_HARDEN=1 set in the gateway's environment, /srv/state retains mode 0o2770 after gateway config writes, and the uid-1001 sibling continues to access files via group membership.

Actual

Verified via new unit test "leaves state dir mode alone when OPENCLAW_SKIP_STATE_DIR_HARDEN=1" — test passes: pre-set 0o2770 mode is preserved through writeConfigFile.

Evidence

  • pnpm exec vitest run src/config/io.write-config.test.ts — all 12 tests pass (11 existing + 1 new)
  • pnpm build — clean

Human Verification

  • Verified scenarios:
    • New test passes; existing "tightens world-writable state dir…" test still passes (default-path regression guard)
    • Reviewed src/infra/env.ts isTruthyEnvValue to confirm accepted truthy values match existing peer vars
    • Confirmed env-var naming matches OPENCLAW_SKIP_* / OPENCLAW_ALLOW_* convention used elsewhere (see src/infra/bonjour.ts:29, src/infra/path-env.ts:87, src/entry.respawn.ts:38)
  • Edge cases checked:
    • Unset env var → default behavior preserved (existing test passes)
    • Truthy value ("1") → chmod skipped, mode preserved (new test passes)
    • Falsy value ("0", "false", "no") → isTruthyEnvValue returns false, default behavior (covered by existing test's empty-env case)
  • What I did not verify:
    • End-to-end on a real Fly Machine with the env var set in the live fly.toml (handled separately in the downstream project; outside this PR's scope)
    • Windows platform path (short-circuited by the existing process.platform === "win32" check above the new one)

Review Conversations

  • I replied to or resolved every bot review conversation I addressed in this PR.
  • I left unresolved only the conversations that still need reviewer or maintainer judgment.

Compatibility / Migration

  • Backward compatible? Yes. Default behavior (env var unset) is unchanged.
  • Config/env changes? Yes — new OPENCLAW_SKIP_STATE_DIR_HARDEN env var (opt-in only).
  • Migration needed? No.

Risks and Mitigations

  • Risk: A user sets OPENCLAW_SKIP_STATE_DIR_HARDEN=1 on a single-uid deployment and leaves their state dir world-writable.
    • Mitigation: The opt-out is explicitly opt-in. The existing 0o077-already-zero check still applies — a dir that's already 0o700 stays 0o700 regardless. The feature is scoped to deployments that are explicitly managing state-dir perms themselves.
  • Risk: Env-var name collision.
    • Mitigation: Grep confirms no existing OPENCLAW_SKIP_STATE_DIR_HARDEN identifier in the repo.

AI-assisted

  • This PR was drafted with Claude (Anthropic). Final code, test, and PR copy reviewed by a human before submission.

@greptile-apps

greptile-apps Bot commented Apr 24, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds a OPENCLAW_SKIP_STATE_DIR_HARDEN env-var opt-out to tightenStateDirPermissionsIfNeeded in src/config/io.ts, allowing multi-uid shared-volume deployments to skip the chmod(configDir, 0o700) call that otherwise strips group access on every config write. The change is minimal, well-scoped, and follows the established isTruthyEnvValue / OPENCLAW_SKIP_* convention; the existing hardening test still passes unchanged and a new companion test guards the opt-out path.

Confidence Score: 5/5

Safe to merge; the change is strictly opt-in and leaves the default hardening path untouched.

All findings are P2 style suggestions (missing logAcceptedEnvOption call for observability). The logic is correct, the test is solid, and no existing behavior is altered when the env var is unset.

No files require special attention.

Prompt To Fix All With AI
This is a comment left during a code review.
Path: src/config/io.ts
Line: 209-211

Comment:
Other `OPENCLAW_*` env-var opt-outs call `logAcceptedEnvOption` (from `src/infra/env.ts`) so operators can confirm via logs that the env var was read and honoured. Without this call, a misconfigured container (e.g. env var misspelled or not injected) will silently fall back to the hardening path with no observable signal, making it harder to diagnose the original `EACCES` cascade.

```suggestion
  if (isTruthyEnvValue(params.env.OPENCLAW_SKIP_STATE_DIR_HARDEN)) {
    logAcceptedEnvOption({
      key: "OPENCLAW_SKIP_STATE_DIR_HARDEN",
      description: "skipping state-dir chmod hardening for multi-uid shared-volume deployment",
    });
    return;
  }
```

How can I resolve this? If you propose a fix, please make it concise.

Reviews (1): Last reviewed commit: "config(io): add OPENCLAW_SKIP_STATE_DIR_..." | Re-trigger Greptile

Comment thread src/config/io.ts
Comment on lines +209 to +211
if (isTruthyEnvValue(params.env.OPENCLAW_SKIP_STATE_DIR_HARDEN)) {
return;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Other OPENCLAW_* env-var opt-outs call logAcceptedEnvOption (from src/infra/env.ts) so operators can confirm via logs that the env var was read and honoured. Without this call, a misconfigured container (e.g. env var misspelled or not injected) will silently fall back to the hardening path with no observable signal, making it harder to diagnose the original EACCES cascade.

Suggested change
if (isTruthyEnvValue(params.env.OPENCLAW_SKIP_STATE_DIR_HARDEN)) {
return;
}
if (isTruthyEnvValue(params.env.OPENCLAW_SKIP_STATE_DIR_HARDEN)) {
logAcceptedEnvOption({
key: "OPENCLAW_SKIP_STATE_DIR_HARDEN",
description: "skipping state-dir chmod hardening for multi-uid shared-volume deployment",
});
return;
}
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/config/io.ts
Line: 209-211

Comment:
Other `OPENCLAW_*` env-var opt-outs call `logAcceptedEnvOption` (from `src/infra/env.ts`) so operators can confirm via logs that the env var was read and honoured. Without this call, a misconfigured container (e.g. env var misspelled or not injected) will silently fall back to the hardening path with no observable signal, making it harder to diagnose the original `EACCES` cascade.

```suggestion
  if (isTruthyEnvValue(params.env.OPENCLAW_SKIP_STATE_DIR_HARDEN)) {
    logAcceptedEnvOption({
      key: "OPENCLAW_SKIP_STATE_DIR_HARDEN",
      description: "skipping state-dir chmod hardening for multi-uid shared-volume deployment",
    });
    return;
  }
```

How can I resolve this? If you propose a fix, please make it concise.

@chrislangston

Copy link
Copy Markdown
Author

Addressed in d500b555e — added logAcceptedEnvOption call so a misspelled or missing-injection env var is visible in logs instead of silently falling back to the default hardening path. Good catch, thank you.

@clawsweeper

clawsweeper Bot commented Apr 29, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for the context here. I swept through the related work, and this is now duplicate or superseded.

Keep open, but this PR is not merge-ready: the proposed opt-out only bypasses one helper, while current main still hardens the config directory through the atomic writer with dirMode: 0o700. It needs a repaired permission-policy shape, maintainer security review, and real multi-UID deployment proof before merge.

Canonical path: Close this stale PR. The latest review rated it F, the branch still lacks merge-ready proof, and there has been no human follow-up after the durable review.

So I’m closing this here because the remaining work is already tracked in the canonical issue.

Review details

Best possible solution:

Close this stale PR. The latest review rated it F, the branch still lacks merge-ready proof, and there has been no human follow-up after the durable review.

Do we have a high-confidence way to reproduce the issue?

Yes for the patch defect: source inspection shows that after this new guard would return, writeConfigFile still calls replaceFileAtomic with dirMode: 0o700, and fs-safe chmods the directory. I did not run a live multi-UID sidecar deployment.

Is this the best way to solve the issue?

No. The env guard is a plausible direction, but this implementation is too narrow for current main; the best fix must cover the atomic writer boundary or use a broader maintainer-approved state permission policy.

Security review:

Security review needs attention: The diff changes a local state-directory permission hardening boundary, and the opt-out is incomplete against current main's atomic write path.

  • [medium] Incomplete state-dir hardening opt-out — src/config/io.ts:266
    The env var disables one chmod helper while replaceFileAtomic({ dirMode: 0o700 }) can still tighten the same directory, so operators may believe they preserved group access while another security-sensitive path changes it.
    Confidence: 0.93

AGENTS.md: found and applied where relevant.

What I checked:

  • stale F-rated PR: PR was opened 2026-04-24T18:33:39Z, is older than 60 days, and the latest review rated it F.
  • proof blocker: real behavior proof is mock_only and proof tier is F, so this branch is not merge-ready without contributor follow-up.
  • no human follow-up: live comments and timeline hydrated by apply contain no non-automation activity after the ClawSweeper review.

Likely related people:

  • steipete: Peter Steinberger authored the state-dir hardening commit and the later fs-safe extraction PR that now routes config writes through replaceFileAtomic({ dirMode: 0o700 }). (role: introduced hardening and fs-safe boundary; confidence: high; commits: 7e3787517feb, 538605ff44d2; files: src/config/io.ts, src/config/io.write-config.test.ts, src/infra/replace-file.ts)
  • vincentkoc: Vincent Koc authored the merged exec-approvals fsGroup fix that explicitly kept this config/state-dir permission policy separate from the exec-specific fix. (role: adjacent permission-regression contributor; confidence: medium; commits: 4d6593642e5a; files: src/infra/exec-approvals.ts, src/infra/exec-approvals-store.test.ts)

Codex review notes: model internal, reasoning high; reviewed against 8ed6c78b7891.

@chrislangston
chrislangston force-pushed the fix/openclaw-skip-state-dir-harden branch from c153b83 to bd4e8a9 Compare April 29, 2026 14:23
@katerinkaivanova

Copy link
Copy Markdown

FYI: a third codepath with the same unconditional chmod-on-state-dir
pattern was reported in issue #83619, this time in exec-approvals
(src/infra/exec-approvals.ts:ensureDir), regressed in #77907.
If OPENCLAW_SKIP_STATE_DIR_HARDEN is extended to gate that call too, a
single env-var would cover all three known codepaths in multi-uid runtimes.

@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. labels May 20, 2026
@clawsweeper

clawsweeper Bot commented May 20, 2026

Copy link
Copy Markdown
Contributor

ClawSweeper PR egg

🎁 Pass real behavior proof to wake the egg and unlock a hatchable treat.

Where did the egg go?
  • The egg game starts only after the PR passes the real-behavior proof check.
  • Before that, no creature or rarity is rolled. The treat waits for real proof.
  • This is still just collectible flavor: proof affects review readiness, not creature quality.

@openclaw-barnacle openclaw-barnacle Bot added the triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. label May 20, 2026
@openclaw-barnacle

Copy link
Copy Markdown

This pull request has been automatically marked as stale due to inactivity.
Please add updates or it will be closed.

@openclaw-barnacle openclaw-barnacle Bot added the stale Marked as stale due to inactivity label Jun 14, 2026
@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Jun 17, 2026
@openclaw-barnacle openclaw-barnacle Bot removed the stale Marked as stale due to inactivity label Jun 17, 2026
@clawsweeper clawsweeper Bot added merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. and removed merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. labels Jun 19, 2026
@clawsweeper

clawsweeper Bot commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

ClawSweeper applied the proposed close for this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. P1 High-priority user-facing bug, regression, or broken workflow. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: XS status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants