-
Notifications
You must be signed in to change notification settings - Fork 3k
fix(security): harden post-merge state handling #8074
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9d00850
fix(shields): bind timer recovery to its config target
cv 3e3040a
fix(security): bind migration scrubbing to installed bytes
cv dd7ccd7
fix(security): address post-merge hardening review
cv fcd128c
merge(main): refresh #8074 with current main
cv 353ac18
merge(main): refresh #8074 with current main
cv fb05698
merge(main): refresh #8074 with current main
cv 053d6c1
docs(shields): clarify mutation ordering
cv e96aa39
fix(security): complete hardening review remediation
cv ea43bfb
test(security): satisfy hardening check gates
cv ceffeec
docs(security): document lock rollback recovery
cv 731de54
merge(security): refresh #8074 on current main
cv 662d4ad
merge: refresh PR #8074 from main
cv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { | ||
| existsSync, | ||
| mkdirSync, | ||
| mkdtempSync, | ||
| readdirSync, | ||
| readFileSync, | ||
| rmSync, | ||
| statSync, | ||
| symlinkSync, | ||
| writeFileSync, | ||
| } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import path from "node:path"; | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
| import type { PluginLogger } from "../index.js"; | ||
| import { | ||
| cleanupSnapshotBundle, | ||
| createSnapshotBundle, | ||
| type HostOpenClawState, | ||
| setConfigValue, | ||
| } from "./migration-state.js"; | ||
|
|
||
| const roots: string[] = []; | ||
|
|
||
| function makeHome(): string { | ||
| const home = mkdtempSync(path.join(tmpdir(), "nemoclaw-migration-state-security-")); | ||
| roots.push(home); | ||
| return home; | ||
| } | ||
|
|
||
| function makeLogger(): PluginLogger { | ||
| return { | ||
| info: vi.fn(), | ||
| warn: vi.fn(), | ||
| error: vi.fn(), | ||
| debug: vi.fn(), | ||
| }; | ||
| } | ||
|
|
||
| function makeHostState(homeDir: string, configPath: string): HostOpenClawState { | ||
| const stateDir = path.join(homeDir, ".openclaw"); | ||
| return { | ||
| exists: true, | ||
| homeDir, | ||
| stateDir, | ||
| configDir: stateDir, | ||
| configPath, | ||
| workspaceDir: null, | ||
| extensionsDir: null, | ||
| skillsDir: null, | ||
| hooksDir: null, | ||
| externalRoots: [], | ||
| warnings: [], | ||
| errors: [], | ||
| hasExternalConfig: false, | ||
| }; | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| for (const root of roots.splice(0)) { | ||
| rmSync(root, { force: true, recursive: true }); | ||
| } | ||
| }); | ||
|
|
||
| describe("migration-state prepared config security", () => { | ||
| it("installs a mode-0600 config after scrubbing contextual secrets in memory", () => { | ||
| const home = makeHome(); | ||
| const stateDir = path.join(home, ".openclaw"); | ||
| const configPath = path.join(stateDir, "openclaw.json"); | ||
| mkdirSync(stateDir, { recursive: true }); | ||
| writeFileSync( | ||
| configPath, | ||
| JSON.stringify({ | ||
| gateway: { auth: { token: "must-not-migrate" } }, | ||
| metadata: { | ||
| environmentAssignment: "GITHUB_TOKEN=opaque-secret-value-123", | ||
| camelAssignment: "apiKey=opaque-secret-value-123", | ||
| model: "keep-me", | ||
| }, | ||
| }), | ||
| ); | ||
|
|
||
| const bundle = createSnapshotBundle(makeHostState(home, configPath), makeLogger(), { | ||
| persist: false, | ||
| }); | ||
| expect(bundle).not.toBeNull(); | ||
| if (bundle === null) return; | ||
|
|
||
| const preparedConfigPath = path.join(bundle.preparedStateDir, "openclaw.json"); | ||
| const preparedConfig = JSON.parse(readFileSync(preparedConfigPath, "utf-8")) as { | ||
| gateway?: unknown; | ||
| metadata: Record<string, string>; | ||
| }; | ||
| expect(preparedConfig.gateway).toBeUndefined(); | ||
| expect(preparedConfig.metadata).toEqual({ | ||
| environmentAssignment: "[STRIPPED_BY_MIGRATION]", | ||
| camelAssignment: "[STRIPPED_BY_MIGRATION]", | ||
| model: "keep-me", | ||
| }); | ||
| expect(statSync(preparedConfigPath).mode & 0o777).toBe(0o600); | ||
|
|
||
| cleanupSnapshotBundle(bundle); | ||
| }); | ||
|
|
||
| it.runIf(process.platform !== "win32")( | ||
| "rejects an in-tree config symlink without touching its external target", | ||
| () => { | ||
| const home = makeHome(); | ||
| const stateDir = path.join(home, ".openclaw"); | ||
| const configPath = path.join(stateDir, "openclaw.json"); | ||
| const externalConfigPath = path.join(home, "external-openclaw.json"); | ||
| const original = JSON.stringify({ external: "must-remain" }); | ||
| mkdirSync(stateDir, { recursive: true }); | ||
| writeFileSync(externalConfigPath, original, { mode: 0o640 }); | ||
| const originalMode = statSync(externalConfigPath).mode & 0o777; | ||
| symlinkSync(externalConfigPath, configPath); | ||
| const logger = makeLogger(); | ||
|
|
||
| const bundle = createSnapshotBundle(makeHostState(home, configPath), logger, { | ||
| persist: false, | ||
| }); | ||
|
|
||
| expect(bundle).toBeNull(); | ||
| expect(logger.error).toHaveBeenCalled(); | ||
| expect(readFileSync(externalConfigPath, "utf-8")).toBe(original); | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| expect(statSync(externalConfigPath).mode & 0o777).toBe(originalMode); | ||
| const stagingDir = path.join(home, ".nemoclaw", "staging"); | ||
| expect(existsSync(stagingDir) ? readdirSync(stagingDir) : []).toEqual([]); | ||
| }, | ||
| ); | ||
| }); | ||
|
|
||
| describe("migration-state config path security", () => { | ||
| const expectPrototypeClean = (): void => { | ||
| const probe: Record<string, unknown> = {}; | ||
| for (const key of ["polluted", "isAdmin", "bar"]) { | ||
| expect(Object.prototype.hasOwnProperty.call(Object.prototype, key)).toBe(false); | ||
| expect(probe[key]).toBeUndefined(); | ||
| } | ||
| }; | ||
|
|
||
| it.each([ | ||
| "__proto__", | ||
| "constructor", | ||
| "prototype", | ||
| ])("rejects unsafe path segment: %s", (segment) => { | ||
| const doc: Record<string, unknown> = {}; | ||
| expect(() => { | ||
| setConfigValue(doc, `${segment}.polluted`, "true"); | ||
| }).toThrow(/Unsafe config path segment/); | ||
| expectPrototypeClean(); | ||
| }); | ||
|
|
||
| it("rejects __proto__ in nested position", () => { | ||
| const doc: Record<string, unknown> = {}; | ||
| expect(() => { | ||
| setConfigValue(doc, "agents.__proto__.isAdmin", "true"); | ||
| }).toThrow(/Unsafe config path segment/); | ||
| expectPrototypeClean(); | ||
| }); | ||
|
|
||
| it.each([ | ||
| "foo.prototype.bar", | ||
| "foo.constructor.bar", | ||
| ])("rejects unsafe segment in nested path: %s", (configPath) => { | ||
| const doc: Record<string, unknown> = {}; | ||
| expect(() => { | ||
| setConfigValue(doc, configPath, "true"); | ||
| }).toThrow(/Unsafe config path segment/); | ||
| expectPrototypeClean(); | ||
| }); | ||
|
|
||
| it("allows simple top-level keys", () => { | ||
| const doc: Record<string, unknown> = {}; | ||
| setConfigValue(doc, "theme", "dark"); | ||
| expect(doc.theme).toBe("dark"); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.