Start every Rule private, with a rotating link and files that follow its readers.
New Rules stay off lists until the person who holds the pen turns on a share link, or the team features one. Uploaded files are readable only by those readers, stripped of location metadata, and deleted with the Rule, draft, or account.
This commit is contained in:
1 parent
642551064c
commit
5cbda35e32
50 files changed
+2429
-219
No files matched your search
@@ -1,30 +1,91 @@
|
||||
import { readFile } from "node:fs/promises";
|
||||
import { NextResponse } from "next/server";
|
||||
import { apiRoute } from "../../../../lib/server/apiRoute";
|
||||
import { prisma } from "../../../../lib/server/db";
|
||||
import { isDatabaseConfigured } from "../../../../lib/server/env";
|
||||
import {
|
||||
dbUnavailable,
|
||||
notFound,
|
||||
serverMisconfigured,
|
||||
} from "../../../../lib/server/responses";
|
||||
import { getReadablePublishedRule } from "../../../../lib/server/publishedRules";
|
||||
import { getSessionUser } from "../../../../lib/server/session";
|
||||
import { isValidUploadFileId } from "../../../../lib/server/uploads/uploadConstants";
|
||||
import { resolveUploadedFileById } from "../../../../lib/server/uploads/resolveUploadedFile";
|
||||
import { getUploadRootFromEnv } from "../../../../lib/server/uploads/uploadRoot";
|
||||
import { browserHoldsUploadGrant } from "../../../../lib/server/uploads/uploadGrantCookie";
|
||||
import { RULE_VISIBILITY } from "../../../../lib/ruleVisibility";
|
||||
|
||||
type RouteContext = { params: Promise<{ id: string }> };
|
||||
|
||||
/**
|
||||
* Public read for opaque upload ids (no auth). Unguessable UUID stem;
|
||||
* do not use for sensitive documents without revisiting policy.
|
||||
*/
|
||||
function cacheControlForVisibility(
|
||||
visibility: "private" | "shared_by_link" | "public_and_featured" | null,
|
||||
): string {
|
||||
if (visibility === RULE_VISIBILITY.public_and_featured) {
|
||||
return "public, max-age=3600";
|
||||
}
|
||||
return "private, no-store";
|
||||
}
|
||||
|
||||
export const GET = apiRoute<RouteContext>(
|
||||
"uploads.byId",
|
||||
async (_request, context) => {
|
||||
async (request, context) => {
|
||||
if (!getUploadRootFromEnv()) {
|
||||
return serverMisconfigured(
|
||||
"File uploads are not configured (UPLOAD_ROOT is unset).",
|
||||
);
|
||||
}
|
||||
if (!isDatabaseConfigured()) {
|
||||
return dbUnavailable();
|
||||
}
|
||||
|
||||
const { id } = await context.params;
|
||||
const resolved = await resolveUploadedFileById(id);
|
||||
if (!isValidUploadFileId(id)) {
|
||||
return notFound("Upload not found");
|
||||
}
|
||||
|
||||
const row = await prisma.upload.findUnique({
|
||||
where: { id: id.toLowerCase() },
|
||||
select: {
|
||||
id: true,
|
||||
userId: true,
|
||||
draftId: true,
|
||||
ruleId: true,
|
||||
rule: { select: { visibility: true } },
|
||||
draft: { select: { userId: true } },
|
||||
},
|
||||
});
|
||||
if (!row) {
|
||||
return notFound("Upload not found");
|
||||
}
|
||||
|
||||
const user = await getSessionUser();
|
||||
let allowed = false;
|
||||
let visibility: "private" | "shared_by_link" | "public_and_featured" | null =
|
||||
row.rule?.visibility ?? null;
|
||||
|
||||
if (row.ruleId) {
|
||||
const resolved = await getReadablePublishedRule({
|
||||
idOrSlug: row.ruleId,
|
||||
user,
|
||||
viaShareSlug: visibility === RULE_VISIBILITY.shared_by_link,
|
||||
requestPath: request.nextUrl.pathname,
|
||||
});
|
||||
allowed = resolved != null;
|
||||
visibility = resolved?.visibility ?? visibility;
|
||||
} else if (row.draftId) {
|
||||
allowed = Boolean(user && row.draft?.userId === user.id);
|
||||
} else if (user && row.userId === user.id) {
|
||||
allowed = true;
|
||||
} else if (!user) {
|
||||
allowed = await browserHoldsUploadGrant(row.id);
|
||||
}
|
||||
|
||||
if (!allowed) {
|
||||
return notFound("Upload not found");
|
||||
}
|
||||
|
||||
const resolved = await resolveUploadedFileById(row.id);
|
||||
if (!resolved) {
|
||||
return notFound("Upload not found");
|
||||
}
|
||||
@@ -34,7 +95,7 @@ export const GET = apiRoute<RouteContext>(
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": resolved.contentType,
|
||||
"Cache-Control": "public, max-age=31536000, immutable",
|
||||
"Cache-Control": cacheControlForVisibility(visibility),
|
||||
"X-Content-Type-Options": "nosniff",
|
||||
},
|
||||
});
|
||||
|
||||
@@ -11,6 +11,7 @@ import { getSessionUser } from "../../../lib/server/session";
|
||||
import { apiRoute } from "../../../lib/server/apiRoute";
|
||||
import { rateLimitKey } from "../../../lib/server/rateLimit";
|
||||
import { saveCreateFlowUpload } from "../../../lib/server/uploads/saveCreateFlowUpload";
|
||||
import { issueGuestUploadGrantCookie } from "../../../lib/server/uploads/uploadGrantCookie";
|
||||
import { getUploadRootFromEnv } from "../../../lib/server/uploads/uploadRoot";
|
||||
import {
|
||||
CREATE_FLOW_UPLOAD_MAX_BYTES,
|
||||
@@ -146,6 +147,7 @@ export const POST = apiRoute("uploads.post", async (request: NextRequest) => {
|
||||
const saved = await saveCreateFlowUpload({
|
||||
purpose: purposeRaw,
|
||||
buffer: buf,
|
||||
userId: user?.id ?? null,
|
||||
});
|
||||
|
||||
if ("error" in saved) {
|
||||
@@ -171,6 +173,14 @@ export const POST = apiRoute("uploads.post", async (request: NextRequest) => {
|
||||
);
|
||||
}
|
||||
|
||||
if (!user) {
|
||||
try {
|
||||
await issueGuestUploadGrantCookie(saved.id);
|
||||
} catch {
|
||||
// Cookie is best-effort; the file is already stored.
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
url: saved.urlPath,
|
||||
id: saved.id,
|
||||
|
||||
Reference in new issue
Block a user