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

+84 -2
View File
@@ -1,6 +1,67 @@
import { logger } from "../../logger";
import { prisma } from "../db";
import { extractUploadIdsFromUnknown } from "./extractUploadIds";
import { browserHoldsUploadGrant } from "./uploadGrantCookie";
type UploadClaimRow = {
id: string;
userId: string | null;
ruleId: string | null;
draftId: string | null;
};
async function loadUploadRows(ids: string[]): Promise<UploadClaimRow[]> {
if (ids.length === 0) return [];
return prisma.upload.findMany({
where: { id: { in: ids } },
select: { id: true, userId: true, ruleId: true, draftId: true },
});
}
/**
* A save may take a file only when it is unlinked (or already on this Rule)
* and was made by this account or browser.
*/
async function idsClaimableForRule(
rows: UploadClaimRow[],
opts: { ruleId: string; userId: string | null },
): Promise<string[]> {
const out: string[] = [];
for (const row of rows) {
if (row.ruleId != null && row.ruleId !== opts.ruleId) continue;
if (row.ruleId === opts.ruleId) {
out.push(row.id);
continue;
}
if (opts.userId != null && row.userId === opts.userId) {
out.push(row.id);
continue;
}
if (row.userId == null && (await browserHoldsUploadGrant(row.id))) {
out.push(row.id);
}
}
return out;
}
async function idsClaimableForDraft(
rows: UploadClaimRow[],
opts: { draftId: string; userId: string },
): Promise<string[]> {
const out: string[] = [];
for (const row of rows) {
if (row.ruleId != null) continue;
if (row.draftId != null && row.draftId !== opts.draftId) continue;
if (row.userId === opts.userId) {
out.push(row.id);
continue;
}
if (row.userId == null && (await browserHoldsUploadGrant(row.id))) {
out.push(row.id);
}
}
return out;
}
export async function linkUploadsToRule(opts: {
ruleId: string;
@@ -10,8 +71,19 @@ export async function linkUploadsToRule(opts: {
const ids = extractUploadIdsFromUnknown(opts.document);
if (ids.length === 0) return;
try {
const rows = await loadUploadRows(ids);
const claimable = await idsClaimableForRule(rows, opts);
if (claimable.length === 0) return;
await prisma.upload.updateMany({
where: { id: { in: ids } },
where: {
id: { in: claimable },
AND: [
{ OR: [{ ruleId: null }, { ruleId: opts.ruleId }] },
opts.userId
? { OR: [{ userId: opts.userId }, { userId: null }] }
: { userId: null },
],
},
data: {
ruleId: opts.ruleId,
draftId: null,
@@ -31,8 +103,18 @@ export async function linkUploadsToDraft(opts: {
const ids = extractUploadIdsFromUnknown(opts.payload);
if (ids.length === 0) return;
try {
const rows = await loadUploadRows(ids);
const claimable = await idsClaimableForDraft(rows, opts);
if (claimable.length === 0) return;
await prisma.upload.updateMany({
where: { id: { in: ids }, ruleId: null },
where: {
id: { in: claimable },
ruleId: null,
AND: [
{ OR: [{ draftId: null }, { draftId: opts.draftId }] },
{ OR: [{ userId: opts.userId }, { userId: null }] },
],
},
data: {
draftId: opts.draftId,
userId: opts.userId,