fix(restic): validate lock diagnostics object IDs

This commit is contained in:
Nicolas Meienberger 2026-06-03 18:47:04 +02:00
parent 333c11986d
commit edab1b231b
No known key found for this signature in database

View file

@ -1,5 +1,6 @@
import { logger, safeExec } from "../node"; import { logger, safeExec } from "../node";
import { toMessage } from "../utils"; import { toMessage } from "../utils";
import { safeJsonParse } from "../utils/json";
import { ResticLockError } from "./error"; import { ResticLockError } from "./error";
import { addCommonArgs } from "./helpers/add-common-args"; import { addCommonArgs } from "./helpers/add-common-args";
import { buildEnv } from "./helpers/build-env"; import { buildEnv } from "./helpers/build-env";
@ -34,6 +35,14 @@ export const isResticLockFailure = (error: unknown) => {
return LOCK_ERROR_PATTERNS.some((pattern) => pattern.test(message)); return LOCK_ERROR_PATTERNS.some((pattern) => pattern.test(message));
}; };
const RESTIC_LOCK_ID_PATTERN = /^[a-f0-9]{64}$/i;
const addLockId = (ids: Set<string>, candidate: unknown) => {
if (typeof candidate === "string" && RESTIC_LOCK_ID_PATTERN.test(candidate)) {
ids.add(candidate);
}
};
const parseLockIds = (stdout: string) => { const parseLockIds = (stdout: string) => {
const ids = new Set<string>(); const ids = new Set<string>();
@ -41,16 +50,13 @@ const parseLockIds = (stdout: string) => {
const trimmed = line.trim(); const trimmed = line.trim();
if (!trimmed) continue; if (!trimmed) continue;
const jsonId = trimmed.match(/"id"\s*:\s*"([^"]+)"/)?.[1]; const parsed = safeJsonParse<{ id?: unknown }>(trimmed);
if (jsonId) { if (parsed) {
ids.add(jsonId); addLockId(ids, parsed.id);
continue; continue;
} }
const hexId = trimmed.match(/[a-f0-9]{64}/i)?.[0]; addLockId(ids, trimmed);
if (hexId) {
ids.add(hexId);
}
} }
return [...ids].slice(0, 20); return [...ids].slice(0, 20);
@ -76,7 +82,7 @@ const inspectResticLocks = async (config: RepositoryConfig, organizationId: stri
for (const lockId of lockIds) { for (const lockId of lockIds) {
const catResult = await safeExec({ const catResult = await safeExec({
command: "restic", command: "restic",
args: [...baseArgs, "cat", "lock", lockId], args: [...baseArgs, "cat", "lock", "--", lockId],
env, env,
timeout: 15_000, timeout: 15_000,
}); });