|
1 | 1 | // Covers Doctor-only import of the retired exec approvals JSON file. |
| 2 | +import { createHash } from "node:crypto"; |
2 | 3 | import fs from "node:fs"; |
3 | 4 | import fsp from "node:fs/promises"; |
4 | 5 | import { afterEach, describe, expect, it } from "vitest"; |
@@ -165,41 +166,147 @@ describe("legacy exec approvals migration", () => { |
165 | 166 | expect(receipt(env)).toMatchObject({ removed_source: 1 }); |
166 | 167 | }); |
167 | 168 |
|
| 169 | + function legacyWithInvalidEntry(entry: unknown, agentKey = "private-marker-agent"): string { |
| 170 | + return JSON.stringify({ |
| 171 | + version: 1, |
| 172 | + socket: { path: "/private-marker/socket", token: "private-marker-token" }, |
| 173 | + agents: { |
| 174 | + first: { allowlist: ["/usr/bin/true"] }, |
| 175 | + [agentKey]: { allowlist: ["/usr/bin/true", entry] }, |
| 176 | + third: { security: "deny" }, |
| 177 | + }, |
| 178 | + }); |
| 179 | + } |
| 180 | + |
168 | 181 | it.each([ |
169 | | - { name: "invalid JSON", raw: "{malformed-secret-marker" }, |
170 | | - ...[ |
171 | | - { lastUsedAt: "now", lastUsedCommand: null }, |
172 | | - { lastUsedAt: null, lastUsedCommand: 42 }, |
173 | | - { lastUsedAt: null, argPattern: null }, |
174 | | - { lastUsedCommand: null, lastResolvedPath: null }, |
175 | | - ].map((metadata) => ({ |
176 | | - name: JSON.stringify(metadata), |
177 | | - raw: JSON.stringify({ |
178 | | - version: 1, |
179 | | - agents: { main: { allowlist: [{ pattern: "secret-marker", ...metadata }] } }, |
| 182 | + { |
| 183 | + name: "metadata in the second agent's second entry", |
| 184 | + raw: legacyWithInvalidEntry({ |
| 185 | + pattern: "/private-marker/pattern", |
| 186 | + id: "private-marker-id", |
| 187 | + commandText: "private-marker-command", |
| 188 | + lastUsedAt: "private-marker-time", |
| 189 | + lastUsedCommand: null, |
180 | 190 | }), |
| 191 | + problem: "agents entry #2.allowlist[1].lastUsedAt: expected a finite number", |
| 192 | + }, |
| 193 | + ...[ |
| 194 | + { metadata: { lastUsedAt: null, lastUsedCommand: 42 }, field: "lastUsedCommand" }, |
| 195 | + { metadata: { lastUsedAt: null, argPattern: null }, field: "argPattern" }, |
| 196 | + { metadata: { lastUsedCommand: null, lastResolvedPath: null }, field: "lastResolvedPath" }, |
| 197 | + ].map(({ metadata, field }) => ({ |
| 198 | + name: `invalid ${field} alongside historical null usage`, |
| 199 | + raw: legacyWithInvalidEntry({ pattern: "private-marker-pattern", ...metadata }), |
| 200 | + problem: `agents entry #2.allowlist[1].${field}: expected a string`, |
181 | 201 | })), |
182 | 202 | { |
183 | | - name: "null policy", |
| 203 | + name: "null policy alongside historical null usage", |
184 | 204 | raw: JSON.stringify({ |
185 | 205 | version: 1, |
186 | 206 | defaults: { security: null }, |
187 | | - agents: { main: { allowlist: [{ pattern: "secret-marker", lastUsedAt: null }] } }, |
| 207 | + agents: { main: { allowlist: [{ pattern: "private-marker", lastUsedAt: null }] } }, |
188 | 208 | }), |
| 209 | + problem: "defaults.security: expected a supported value", |
189 | 210 | }, |
190 | | - ])("preserves malformed $name and records a non-removal receipt", async ({ raw }) => { |
191 | | - const { env, stateDir, sourcePath } = useStateDir(); |
192 | | - await fsp.writeFile(sourcePath, raw, "utf8"); |
193 | | - |
194 | | - const result = await migrate({ env, stateDir }); |
195 | | - |
196 | | - expect(result.changes).toEqual([]); |
197 | | - expect(result.warnings[0]).toContain("Preserved malformed legacy exec approvals"); |
198 | | - expect(JSON.stringify(result)).not.toContain("secret-marker"); |
199 | | - expect(fs.readFileSync(sourcePath, "utf8")).toBe(raw); |
200 | | - expect(receipt(env)).toMatchObject({ removed_source: 0, source_record_count: 0 }); |
201 | | - expect(readExecApprovalsConfigRow(database(env))).toBeUndefined(); |
202 | | - }); |
| 211 | + { |
| 212 | + name: "hostile long keys, values, and multiple failures", |
| 213 | + raw: legacyWithInvalidEntry( |
| 214 | + { |
| 215 | + pattern: "/private-marker/pattern", |
| 216 | + lastUsedAt: { ["private-marker-value".repeat(1_000)]: "private-marker" }, |
| 217 | + lastUsedCommand: 42, |
| 218 | + }, |
| 219 | + "private-marker-\n\u001b[31m".repeat(1_000), |
| 220 | + ), |
| 221 | + problem: "agents entry #2.allowlist[1].lastUsedAt: expected a finite number", |
| 222 | + }, |
| 223 | + { |
| 224 | + name: "numeric-like agent key enumeration", |
| 225 | + raw: '{"version":1,"agents":{"20":{},"3":{"security":"private-marker"}}}', |
| 226 | + problem: "agents entry #1.security: expected a supported value", |
| 227 | + }, |
| 228 | + { |
| 229 | + name: "policy", |
| 230 | + raw: JSON.stringify({ version: 1, defaults: { security: "private-marker" } }), |
| 231 | + problem: "defaults.security: expected a supported value", |
| 232 | + }, |
| 233 | + { |
| 234 | + name: "socket token", |
| 235 | + raw: JSON.stringify({ version: 1, socket: { token: { "private-marker": true } } }), |
| 236 | + problem: "socket.token: expected a string", |
| 237 | + }, |
| 238 | + { |
| 239 | + name: "whole entry shape", |
| 240 | + raw: legacyWithInvalidEntry(42), |
| 241 | + problem: |
| 242 | + "agents entry #2.allowlist[1]: expected a non-empty string or an object with a non-empty pattern", |
| 243 | + }, |
| 244 | + { |
| 245 | + name: "blank string entry", |
| 246 | + raw: legacyWithInvalidEntry(" "), |
| 247 | + problem: "agents entry #2.allowlist[1]: expected a non-empty string", |
| 248 | + }, |
| 249 | + { |
| 250 | + name: "blank object pattern without a union wrapper", |
| 251 | + raw: legacyWithInvalidEntry({ pattern: " " }), |
| 252 | + problem: "agents entry #2.allowlist[1].pattern: expected a non-empty string", |
| 253 | + }, |
| 254 | + { |
| 255 | + name: "JSON syntax", |
| 256 | + raw: "{malformed-private-marker", |
| 257 | + problem: "invalid JSON syntax", |
| 258 | + }, |
| 259 | + { |
| 260 | + name: "UTF-8 encoding", |
| 261 | + raw: Buffer.concat([Buffer.from("private-marker"), Buffer.from([0xff])]), |
| 262 | + problem: "invalid UTF-8 encoding", |
| 263 | + }, |
| 264 | + ])( |
| 265 | + "diagnoses $name while preserving bytes and a non-removal receipt", |
| 266 | + async ({ raw, problem }) => { |
| 267 | + const { env, stateDir, sourcePath } = useStateDir(); |
| 268 | + const original = Buffer.from(raw); |
| 269 | + await fsp.writeFile(sourcePath, original); |
| 270 | + setTestEnvValue("OPENCLAW_STATE_DIR", stateDir); |
| 271 | + expect(() => loadExecApprovals()).toThrow(ExecApprovalsMigrationRequiredError); |
| 272 | + |
| 273 | + const result = await migrate({ env, stateDir }); |
| 274 | + |
| 275 | + expect(result.changes).toEqual([]); |
| 276 | + expect(result.warnings).toEqual([ |
| 277 | + `Preserved malformed legacy exec approvals for operator recovery. First problem: ${problem}. Repair exec-approvals.json locally, then rerun \`openclaw doctor --fix\` with the same OPENCLAW_STATE_DIR.`, |
| 278 | + ]); |
| 279 | + expect(result.warnings[0]?.length).toBeLessThan(400); |
| 280 | + expect(JSON.stringify(result)).not.toContain("private-marker"); |
| 281 | + expect(fs.readFileSync(sourcePath)).toEqual(original); |
| 282 | + expect(fs.existsSync(`${sourcePath}.doctor-importing`)).toBe(false); |
| 283 | + const sourceReceipt = receipt(env); |
| 284 | + expect(sourceReceipt).toMatchObject({ |
| 285 | + removed_source: 0, |
| 286 | + source_record_count: 0, |
| 287 | + status: "completed", |
| 288 | + source_size_bytes: original.length, |
| 289 | + }); |
| 290 | + const report = { |
| 291 | + source: "legacy-exec-approvals-json", |
| 292 | + target: "exec_approvals_config", |
| 293 | + decision: "malformed-legacy-preserved", |
| 294 | + sourceSha256: createHash("sha256").update(original).digest("hex"), |
| 295 | + sourceValid: false, |
| 296 | + importedRecordCount: 0, |
| 297 | + preservedSqliteRecordCount: 0, |
| 298 | + removesSource: false, |
| 299 | + }; |
| 300 | + expect(JSON.parse(sourceReceipt?.report_json ?? "null")).toEqual(report); |
| 301 | + expect(JSON.parse(runReceipt(env)?.report_json ?? "null")).toEqual(report); |
| 302 | + expect(JSON.stringify([sourceReceipt, runReceipt(env)])).not.toContain("private-marker"); |
| 303 | + expect(readExecApprovalsConfigRow(database(env))).toBeUndefined(); |
| 304 | + expect(() => loadExecApprovals()).toThrow(ExecApprovalsMigrationRequiredError); |
| 305 | + expect(await migrate({ env, stateDir })).toEqual(result); |
| 306 | + expect(fs.readFileSync(sourcePath)).toEqual(original); |
| 307 | + expect(() => loadExecApprovals()).toThrow(ExecApprovalsMigrationRequiredError); |
| 308 | + }, |
| 309 | + ); |
203 | 310 |
|
204 | 311 | it("removes a byte-identical source when canonical state already exists", async () => { |
205 | 312 | const { env, stateDir, sourcePath } = useStateDir(); |
|
0 commit comments