Skip to content

Commit c405b78

Browse files
committed
fix: address review feedback from Copilot and CodeRabbit
- computeFileDigest() now throws when blueprint file is missing instead of silently returning null (prevents integrity-bypass when caller provides blueprintPath but file doesn't exist) - Sanitize the snapshot-level openclaw.json copy, not just the sandbox-bundle copy (secrets no longer persist in snapshot directory) - Also sanitize external config copy in snapshot directory - Wrap restore-time digest computation in try/catch for proper error reporting - Add tests: missing blueprint file fails snapshot creation, snapshot directory itself is sanitized
1 parent 0f9fc11 commit c405b78

2 files changed

Lines changed: 93 additions & 4 deletions

File tree

‎nemoclaw/src/commands/migration-state.test.ts‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,82 @@ describe("commands/migration-state", () => {
742742
}
743743
expect(bundle.manifest.blueprintDigest).toBeNull();
744744
});
745+
746+
it("fails when blueprintPath is provided but file is missing", () => {
747+
const logger = makeLogger();
748+
addDir("/home/user/.openclaw");
749+
addFile("/home/user/.openclaw/openclaw.json", JSON.stringify({ version: 1 }));
750+
751+
const hostState: HostOpenClawState = {
752+
exists: true,
753+
homeDir: "/home/user",
754+
stateDir: "/home/user/.openclaw",
755+
configDir: "/home/user/.openclaw",
756+
configPath: "/home/user/.openclaw/openclaw.json",
757+
workspaceDir: null,
758+
extensionsDir: null,
759+
skillsDir: null,
760+
hooksDir: null,
761+
externalRoots: [],
762+
warnings: [],
763+
errors: [],
764+
hasExternalConfig: false,
765+
};
766+
767+
// /test/nonexistent.yaml does not exist in store
768+
const bundle = createSnapshotBundle(hostState, logger, {
769+
persist: false,
770+
blueprintPath: "/test/nonexistent.yaml",
771+
});
772+
expect(bundle).toBeNull();
773+
expect(logger.error).toHaveBeenCalled();
774+
});
775+
776+
it("sanitizes credentials in the snapshot directory itself (not just sandbox-bundle)", () => {
777+
const logger = makeLogger();
778+
addDir("/home/user/.openclaw");
779+
addFile(
780+
"/home/user/.openclaw/openclaw.json",
781+
JSON.stringify({
782+
version: 1,
783+
gateway: { auth: { token: "secret123" } },
784+
nvidia: { apiKey: "nvapi-test-key" },
785+
}),
786+
);
787+
788+
const hostState: HostOpenClawState = {
789+
exists: true,
790+
homeDir: "/home/user",
791+
stateDir: "/home/user/.openclaw",
792+
configDir: "/home/user/.openclaw",
793+
configPath: "/home/user/.openclaw/openclaw.json",
794+
workspaceDir: null,
795+
extensionsDir: null,
796+
skillsDir: null,
797+
hooksDir: null,
798+
externalRoots: [],
799+
warnings: [],
800+
errors: [],
801+
hasExternalConfig: false,
802+
};
803+
804+
const bundle = createSnapshotBundle(hostState, logger, { persist: false });
805+
if (bundle === null) {
806+
expect.unreachable("bundle should not be null");
807+
return;
808+
}
809+
810+
// Check the snapshot-level openclaw.json (not sandbox-bundle)
811+
const snapshotConfigEntry = store.get(bundle.snapshotDir + "/openclaw/openclaw.json");
812+
if (!snapshotConfigEntry || !snapshotConfigEntry.content) {
813+
expect.unreachable("snapshot config entry should exist with content");
814+
return;
815+
}
816+
const snapshotConfig = JSON.parse(snapshotConfigEntry.content);
817+
expect(snapshotConfig).not.toHaveProperty("gateway");
818+
expect(snapshotConfig.nvidia.apiKey).toBe("[STRIPPED_BY_MIGRATION]");
819+
expect(snapshotConfig.version).toBe(1);
820+
});
745821
});
746822

747823
// -------------------------------------------------------------------------

‎nemoclaw/src/commands/migration-state.ts‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -549,8 +549,10 @@ function sanitizeConfigFile(configPath: string): void {
549549
chmodSync(configPath, 0o600);
550550
}
551551

552-
function computeFileDigest(filePath: string): string | null {
553-
if (!existsSync(filePath)) return null;
552+
function computeFileDigest(filePath: string): string {
553+
if (!existsSync(filePath)) {
554+
throw new Error(`Blueprint file not found: ${filePath}`);
555+
}
554556
return createHash("sha256").update(readFileSync(filePath)).digest("hex");
555557
}
556558

@@ -684,13 +686,14 @@ export function createSnapshotBundle(
684686
mkdirSync(parentDir, { recursive: true });
685687
const snapshotStateDir = path.join(parentDir, "openclaw");
686688
copyDirectory(hostState.stateDir, snapshotStateDir, { stripCredentials: true });
689+
sanitizeConfigFile(path.join(snapshotStateDir, "openclaw.json"));
687690

688691
if (hostState.configPath && hostState.hasExternalConfig) {
689692
const configSnapshotDir = path.join(parentDir, "config");
690693
mkdirSync(configSnapshotDir, { recursive: true });
691694
const configSnapshotPath = path.join(configSnapshotDir, "openclaw.json");
692695
copyFileSync(hostState.configPath, configSnapshotPath);
693-
chmodSync(configSnapshotPath, 0o600);
696+
sanitizeConfigFile(configSnapshotPath);
694697
}
695698

696699
const externalRoots: MigrationExternalRoot[] = [];
@@ -849,7 +852,17 @@ export function restoreSnapshotToHost(
849852
logger.error("Snapshot manifest has invalid blueprintDigest. Refusing to restore.");
850853
return false;
851854
}
852-
const currentDigest = options?.blueprintPath ? computeFileDigest(options.blueprintPath) : null;
855+
let currentDigest: string | null = null;
856+
try {
857+
currentDigest = options?.blueprintPath ? computeFileDigest(options.blueprintPath) : null;
858+
} catch (err: unknown) {
859+
logger.error(
860+
`Failed to read blueprint for digest verification: ${
861+
err instanceof Error ? err.message : String(err)
862+
}`,
863+
);
864+
return false;
865+
}
853866
if (!currentDigest) {
854867
logger.error(
855868
"Snapshot contains a blueprintDigest but no blueprint is available for verification. " +

0 commit comments

Comments
 (0)