Responses had no CSP, the 48-hour download token never changed, and short share slugs could be guessed without a miss limit.
175 lines
4.7 KiB
TypeScript
175 lines
4.7 KiB
TypeScript
import type { User } from "@prisma/client";
|
|
import { RULE_VISIBILITY, type RuleVisibility } from "../ruleVisibility";
|
|
import {
|
|
emailInitials,
|
|
type RuleShareSettings,
|
|
type SharePerson,
|
|
} from "../ruleShareTypes";
|
|
import { formatShareUrlDisplay, shareCopyUrl } from "../sharePaths";
|
|
import { prisma } from "./db";
|
|
import { isRuleAdminEmail } from "./ruleAdmins";
|
|
import { allocateShareSlug } from "./shareSlug";
|
|
|
|
function toShareSettings(opts: {
|
|
visibility: RuleVisibility;
|
|
shareSlug: string | null;
|
|
title: string;
|
|
origin: string;
|
|
ruleId: string;
|
|
ownerEmail: string;
|
|
readers: SharePerson[];
|
|
}): RuleShareSettings {
|
|
const shareUrl = shareCopyUrl({
|
|
origin: opts.origin,
|
|
ruleId: opts.ruleId,
|
|
shareSlug: opts.shareSlug,
|
|
visibility: opts.visibility,
|
|
});
|
|
const shareUrlDisplay =
|
|
opts.shareSlug &&
|
|
(opts.visibility === RULE_VISIBILITY.shared_by_link ||
|
|
opts.visibility === RULE_VISIBILITY.public_and_featured)
|
|
? formatShareUrlDisplay(opts.origin, opts.shareSlug)
|
|
: null;
|
|
return {
|
|
visibility: opts.visibility,
|
|
shareSlug: opts.shareSlug,
|
|
shareUrl,
|
|
shareUrlDisplay,
|
|
title: opts.title,
|
|
ownerEmail: opts.ownerEmail,
|
|
ownerInitials: emailInitials(opts.ownerEmail),
|
|
readers: opts.readers,
|
|
copyEnabled: true,
|
|
};
|
|
}
|
|
|
|
export async function getOwnedRuleShareSettings(opts: {
|
|
ruleId: string;
|
|
user: User;
|
|
origin: string;
|
|
}): Promise<RuleShareSettings | "not_found" | "forbidden"> {
|
|
const row = await prisma.publishedRule.findUnique({
|
|
where: { id: opts.ruleId },
|
|
select: {
|
|
id: true,
|
|
title: true,
|
|
userId: true,
|
|
visibility: true,
|
|
shareSlug: true,
|
|
user: { select: { email: true } },
|
|
stakeholders: {
|
|
select: { email: true },
|
|
orderBy: [{ invitedAt: "asc" }, { id: "asc" }],
|
|
},
|
|
},
|
|
});
|
|
if (!row) return "not_found";
|
|
const isOwner = row.userId === opts.user.id;
|
|
const isAdmin = isRuleAdminEmail(opts.user.email);
|
|
if (!isOwner && !isAdmin) return "forbidden";
|
|
const ownerEmail = (
|
|
row.user?.email ?? opts.user.email
|
|
)
|
|
.trim()
|
|
.toLowerCase();
|
|
const readers: SharePerson[] = row.stakeholders
|
|
.map((s) => s.email.trim().toLowerCase())
|
|
.filter((email) => email.length > 0 && email !== ownerEmail)
|
|
.map((email) => ({
|
|
email,
|
|
initials: emailInitials(email),
|
|
role: "reader" as const,
|
|
}));
|
|
return toShareSettings({
|
|
visibility: row.visibility,
|
|
shareSlug: row.shareSlug,
|
|
title: row.title,
|
|
origin: opts.origin,
|
|
ruleId: row.id,
|
|
ownerEmail,
|
|
readers,
|
|
});
|
|
}
|
|
|
|
export async function patchOwnedRuleShare(opts: {
|
|
ruleId: string;
|
|
user: User;
|
|
origin: string;
|
|
visibility: RuleVisibility;
|
|
communityAgreed?: boolean;
|
|
}): Promise<
|
|
| { ok: true; settings: RuleShareSettings }
|
|
| { ok: false; reason: "not_found" | "forbidden" | "community_agreed" }
|
|
> {
|
|
const row = await prisma.publishedRule.findUnique({
|
|
where: { id: opts.ruleId },
|
|
select: {
|
|
id: true,
|
|
userId: true,
|
|
visibility: true,
|
|
shareSlug: true,
|
|
},
|
|
});
|
|
if (!row) return { ok: false, reason: "not_found" };
|
|
|
|
const isOwner = row.userId === opts.user.id;
|
|
const isAdmin = isRuleAdminEmail(opts.user.email);
|
|
if (!isOwner && !isAdmin) return { ok: false, reason: "forbidden" };
|
|
|
|
if (
|
|
opts.visibility === RULE_VISIBILITY.public_and_featured &&
|
|
!isAdmin
|
|
) {
|
|
return { ok: false, reason: "forbidden" };
|
|
}
|
|
|
|
if (
|
|
opts.visibility === RULE_VISIBILITY.shared_by_link &&
|
|
row.visibility !== RULE_VISIBILITY.shared_by_link &&
|
|
row.visibility !== RULE_VISIBILITY.public_and_featured &&
|
|
opts.communityAgreed !== true
|
|
) {
|
|
return { ok: false, reason: "community_agreed" };
|
|
}
|
|
|
|
if (!isOwner && opts.visibility !== RULE_VISIBILITY.public_and_featured) {
|
|
return { ok: false, reason: "forbidden" };
|
|
}
|
|
|
|
let shareSlug = row.shareSlug;
|
|
if (opts.visibility === RULE_VISIBILITY.private) {
|
|
shareSlug = null;
|
|
} else if (
|
|
opts.visibility === RULE_VISIBILITY.shared_by_link &&
|
|
(row.visibility === RULE_VISIBILITY.private || shareSlug == null)
|
|
) {
|
|
shareSlug = await allocateShareSlug();
|
|
}
|
|
|
|
const turningLinkOff =
|
|
opts.visibility === RULE_VISIBILITY.private &&
|
|
row.visibility !== RULE_VISIBILITY.private;
|
|
|
|
await prisma.publishedRule.update({
|
|
where: { id: row.id },
|
|
data: {
|
|
visibility: opts.visibility,
|
|
shareSlug,
|
|
...(turningLinkOff
|
|
? { exportTokenVersion: { increment: 1 } }
|
|
: {}),
|
|
},
|
|
});
|
|
|
|
const settings = await getOwnedRuleShareSettings({
|
|
ruleId: row.id,
|
|
user: opts.user,
|
|
origin: opts.origin,
|
|
});
|
|
if (settings === "not_found" || settings === "forbidden") {
|
|
return { ok: false, reason: settings };
|
|
}
|
|
return { ok: true, settings };
|
|
}
|