fix(shields): stop leaking the permissive runtime temp dir on failed shields down - #7965
fix(shields): stop leaking the permissive runtime temp dir on failed shields down#7965harjothkhara wants to merge 7 commits into
Conversation
📝 WalkthroughWalkthroughThe shields-down flow defers policy resolution until application. Runtime permissive policies are created when needed, and temporary directories are cleaned up after application or auto-restore timer startup failure. Tests cover filesystem-path merging and both cleanup paths. ChangesRuntime policy cleanup
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related PRs
Suggested labels: Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/lib/shields/flow.test.ts`:
- Around line 894-914: Update the test case “applies the merged permissive
policy and removes its temp directory once applied” to assert that
appliedPolicyBody contains the read_only filesystem path “/etc” in addition to
the existing “/opt/hermes” read_write assertion. Keep the existing merged-policy
and temporary-directory cleanup checks unchanged.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: 989a570c-6caf-4e2d-9b06-a8cb5166af2a
📒 Files selected for processing (2)
src/lib/shields/flow.test.tssrc/lib/shields/index.ts
PR Review Advisor — No blocking findings reportedAdvisor assessment: No blocking advisor findings reported Model lanes
4 terminology differences from the second opinionAdvisory only. These are normalized differences from the primary terminology receipt.
4 additional E2E selections from the second opinionAdvisory only. The primary lane did not select these E2E jobs or targets.
Second-opinion terminology and E2E selections are advisory. They do not change the primary assessment or E2E / PR Gate. 1 semantic terminology decisionTerminology decisions are advisory. They affect the assessment only when a separate finding identifies concrete semantic impact.
E2E guidanceAdvisory only. E2E / PR Gate selects and runs jobs independently. Recommended E2E: 1 optional E2E recommendation
This automated review informs maintainers. Warnings and suggestions do not require a response. A maintainer decides whether to merge. |
…ailed shields down buildRuntimePermissivePolicy writes the merged permissive policy into a 0700 permissive runtime temp directory, but only the policy-apply try/finally removed it. Two early exits sit between the build and that apply — a fork/authorize failure returning "Cannot start auto-restore timer", and a saveShieldsState failure rethrowing — and both left the directory behind. A live openshell policy always carries filesystem_policy paths, so the merge runs on effectively every real shields down. Build the merged policy inside the apply try/finally instead. Policy resolution still happens up front so an unknown policy name fails before any mutation, but nothing is written to the permissive runtime temp directory until the only owner that can release it is in scope. Closes NVIDIA#7964 Signed-off-by: harjoth <harjoth.khara@gmail.com>
dfe71a0 to
1fe18e8
Compare
The merged-policy test only checked the read_write path, so a regression that dropped read_only paths would still pass. The static baseline carries no filesystem paths, so both can only come from the live merge. Refs: NVIDIA#7964 Signed-off-by: harjoth <harjoth.khara@gmail.com>
|
✨ Thanks for the PR. This fixes a temp directory leak in Related open PRs: Related open issues: |
Signed-off-by: Carlos Villela <cvillela@nvidia.com>
Signed-off-by: Carlos Villela <cvillela@nvidia.com>
Security reviewVerdict: PASS Reviewed commit SHA This change defers creation of the merged permissive-policy temporary file until the existing apply-and-cleanup scope owns it. The policy bytes, transition locks, recovery authority, and sandbox configuration behavior remain unchanged. The nine-category review found no security issue. Merge still requires all repository gates and human approval. FindingsNo findings. Detailed analysis
Files reviewedChanged files:
Supporting contracts inspected:
|
|
Closing as superseded by #7966, which merged into main and resolves the same #7964 leak in the same Shields flow. The merged implementation routes timer-start failure, state-write failure, and policy-application completion through one temporary-policy cleanup owner. Its regression coverage also proves a runtime policy directory is created during the failed transition and absent afterward. Because the reported behavior and all affected exits are now covered on main, carrying this conflicting duplicate forward would not add an independent fix. Thank you @harjothkhara and @cv for the implementation and test work here. |
Summary
shields downbuilds the merged permissive policy into a 0700 permissive runtime temp directory, but only the policy-applytry/finallyremoved it. Two early exits sit between the build and that apply, so a failed transition left the directory behind. This builds the merged policy inside the applytry/finallyinstead, so the directory is never created before the only owner that can release it is in scope.Related Issue
Fixes #7964
Changes
src/lib/shields/index.ts:shieldsDownWithoutHostLocknow stores a function that resolves the policy source and calls it inside the policy-applytry. Policy-source validation still happens before mutation, so an unknown policy name fails before any mutation. The two early exits between the old build site and policy application, the auto-restore timer failure and thesaveShieldsStatefailure, can no longer leak the directory. A crash in that window also leaves no directory because policy materialization has not started.src/lib/shields/flow.test.ts: adds alivePolicyYamlharness option and two tests. The harness previously returned a live policy with nofilesystem_policy, sobuildRuntimePermissivePolicyalways returned the static base path and the merge path was never exercised here.This adds no abstraction, configuration, fallback, migration, or compatibility path. The local resolver function replaces two local variables that held the same result eagerly.
I chose this over wrapping the whole creation-through-apply region in one
try/finally. That shape works too, but it re-indents about 120 lines of the timer and state-commit block, and #7749 is already touching this file. Deferring the build also closes the window rather than only adding a release point.buildRuntimePermissivePolicyguards all of its I/O internally and degrades to the base path, so moving the call introduces no new failure mode at the new site.Type of Change
Quality Gates
7f940aad8f9bd62890c075062d38dc9c6e1fc994against base SHA4cd4d64fe67143b57707f874afa0b9d269dfeff2; no findings.Documentation Writer Review
no-docs-neededsrc/lib/shields/index.tschanges only when an internal permissive-policy temporary directory is materialized and removed, andsrc/lib/shields/flow.test.tsadds regression coverage. Command output, errors, flags, configuration, applied policy bytes, and documented workflows remain unchanged at commit7f940aad8f9bd62890c075062d38dc9c6e1fc994. Changed comments and test titles were reviewed againstdocs/AGENTS.md,WRITING.md, and the controlled word list. No Fern source changed, so a docs build is not applicable.Verification
7f940aad8f9bd62890c075062d38dc9c6e1fc994; base SHA4cd4d64fe67143b57707f874afa0b9d269dfeff2; E2E / PR Gate is pending for this commit SHA.Signed-off-by:line and every commit appears asVerifiedin GitHubpre-commit,commit-msg, andpre-pushhooks passed, ornpm run validate:prpassed after refreshingorigin/mainwhen hooks were skipped or unavailablenpm run validate:prpassed.npm testfor broad runtime/test-harness changes;npm run checkfor repo-wide validation/coverage changes — command/result: not applicable. Two files change, one function and one test file, with no runtime or test-harness surface shared beyond them.npm run docsbuilds without warnings (doc changes only)Red-green evidence
Before the fix, the timer-failure test reports the leftover directory:
After the fix both tests pass. The success-path test passes before and after; it is there to prove the merged policy is still what gets applied, which was the main risk of moving the build.
Signed-off-by: harjoth harjoth.khara@gmail.com
Summary by CodeRabbit
Bug Fixes
Tests