Stop a saved Rule from taking uploads it does not own.

A named path no longer moves another Rules file. Duplicate and Make a copy write their own copies of files they keep.
This commit is contained in:
adilallo committed 2026-11-24 18:52:33 -07:00
1 parent 9621fa4d7d
commit 94cee1dc5b
10 files changed
+757 -10

No files matched your search

+31 -6
View File
@@ -3,14 +3,19 @@ import { isValidUploadFileId } from "./uploadConstants";
const UPLOAD_PATH_RE =
/\/api\/uploads\/([0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12})/gi;
function stringifyUnknown(value: unknown): string | null {
try {
return JSON.stringify(value ?? null);
} catch {
return null;
}
}
/** UUID stems referenced as `/api/uploads/{id}` inside JSON. */
export function extractUploadIdsFromUnknown(value: unknown): string[] {
let text: string;
try {
text = JSON.stringify(value ?? null);
} catch {
return [];
}
const text = stringifyUnknown(value);
if (text == null) return [];
UPLOAD_PATH_RE.lastIndex = 0;
const ids = new Set<string>();
for (const match of text.matchAll(UPLOAD_PATH_RE)) {
const id = match[1]?.toLowerCase();
@@ -18,3 +23,23 @@ export function extractUploadIdsFromUnknown(value: unknown): string[] {
}
return [...ids];
}
/** Rewrite `/api/uploads/{id}` paths using `replacements` (old id → new id). */
export function rewriteUploadIdsInUnknown(
value: unknown,
replacements: ReadonlyMap<string, string>,
): unknown {
if (replacements.size === 0) return value;
const text = stringifyUnknown(value);
if (text == null) return value;
UPLOAD_PATH_RE.lastIndex = 0;
const rewritten = text.replace(UPLOAD_PATH_RE, (full, id: string) => {
const next = replacements.get(id.toLowerCase());
return next ? `/api/uploads/${next}` : full;
});
try {
return JSON.parse(rewritten) as unknown;
} catch {
return value;
}
}