The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add Add magic-byte checks to R2 upload (#4539)
Kiran K7ab748576fde
diff --git a/apps/web/.env.example b/apps/web/.env.exampleindex c3ac59d0bc..5c26914520 100644--- a/apps/web/.env.example+++ b/apps/web/.env.example@@ -116,6 +116,9 @@ STORAGE_BASE_URL=STORAGE_PUBLIC_BUCKET=STORAGE_PRIVATE_BUCKET=+# Shared secret for Cloudflare Workers that call Next.js webhooks+CLOUDFLARE_WORKER_WEBHOOK_SECRET=+# Used for internal monitoring & paging# You can remove this by removing `DUB_SLACK_HOOK_CRON` and `DUB_SLACK_HOOK_LINKS` from the codebaseDUB_SLACK_HOOK_CRON=diff --git a/apps/web/app/(ee)/api/cloudflare/webhook/r2-object-created/route.ts b/apps/web/app/(ee)/api/cloudflare/webhook/r2-object-created/route.tsnew file mode 100644index 0000000000..cbbec37452--- /dev/null+++ b/apps/web/app/(ee)/api/cloudflare/webhook/r2-object-created/route.ts@@ -0,0 +1,62 @@+import { assertEnv } from "@/lib/assert-env";+import { logger, toErrorFields } from "@/lib/axiom/server";+import {+ processR2ObjectCreated,+ r2ObjectCreatedNotificationSchema,+} from "@/lib/storage/process-r2-object-created";+import { timingSafeCompare } from "@/lib/webhook/timing-safe-compare";+import { toErrorMessage } from "@dub/utils";+import { logAndRespond } from "app/(ee)/api/cron/utils";+import { NextResponse } from "next/server";+import * as z from "zod/v4";++export const dynamic = "force-dynamic";++const r2ObjectCreatedWebhookBodySchema = z.object({+ notification: r2ObjectCreatedNotificationSchema,+});++function verifyWorkerSecret(req: Request) {+ const webhookSecret = assertEnv("CLOUDFLARE_WORKER_WEBHOOK_SECRET");++ const auth = req.headers.get("authorization") || "";+ const token = auth.startsWith("Bearer ") ? auth.slice("Bearer ".length) : "";++ return timingSafeCompare(token, webhookSecret);+}++// POST /api/cloudflare/webhook/r2-object-created+// Called by the Cloudflare queue consumer Worker (thin forwarder).+export async function POST(req: Request) {+ try {+ if (!verifyWorkerSecret(req)) {+ return NextResponse.json(+ {+ error: "Unauthorized",+ },+ {+ status: 401,+ },+ );+ }++ const { notification } = r2ObjectCreatedWebhookBodySchema.parse(+ await req.json(),+ );++ const result = await processR2ObjectCreated(notification);++ return logAndRespond(result);+ } catch (error) {+ logger.error("storage.r2_validate_failed", {+ ...toErrorFields(error),+ });++ return NextResponse.json(+ {+ error: toErrorMessage(error, "Internal server error"),+ },+ { status: 500 },+ );+ }+}diff --git a/apps/web/lib/actions/partners/program-resources/get-program-resource-upload-url.ts b/apps/web/lib/actions/partners/program-resources/get-program-resource-upload-url.tsindex 329e45e291..698514a36e 100644--- a/apps/web/lib/actions/partners/program-resources/get-program-resource-upload-url.ts+++ b/apps/web/lib/actions/partners/program-resources/get-program-resource-upload-url.ts@@ -5,6 +5,8 @@ import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-progrimport { createSignedUploadUrl } from "@/lib/storage/create-signed-upload-url";import { signedUploadInputSchema } from "@/lib/storage/schemas";import { validateSignedUpload } from "@/lib/storage/validate-signed-upload";+import { assertRateLimit } from "@/lib/upstash/assert-rate-limit";+import { RATELIMIT_POLICIES } from "@/lib/upstash/ratelimit-policies";import { nanoid } from "@dub/utils";import slugify from "@sindresorhus/slugify";import * as z from "zod/v4";@@ -25,7 +27,7 @@ const schema = z.object({export const getProgramResourceUploadUrlAction = authActionClient.inputSchema(schema).action(async ({ ctx, parsedInput }) => {- const { workspace } = ctx;+ const { workspace, user } = ctx;const { resourceType, name, extension, contentType, contentLength } =parsedInput;@@ -53,6 +55,11 @@ export const getProgramResourceUploadUrlAction = authActionClientpolicy: uploadPolicy,});+ await assertRateLimit({+ policy: RATELIMIT_POLICIES.workspaceFileUpload,+ identifier: [workspace.id, user.id],+ });+const { signedUrl, destinationUrl } = await createSignedUploadUrl({key,contentType,diff --git a/apps/web/lib/actions/partners/upload-campaign-image.ts b/apps/web/lib/actions/partners/upload-campaign-image.tsindex da2a8a5964..cc3581ff32 100644--- a/apps/web/lib/actions/partners/upload-campaign-image.ts+++ b/apps/web/lib/actions/partners/upload-campaign-image.ts@@ -4,6 +4,8 @@ import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-progrimport { createSignedUploadUrl } from "@/lib/storage/create-signed-upload-url";import { signedUploadInputSchema } from "@/lib/storage/schemas";import { validateSignedUpload } from "@/lib/storage/validate-signed-upload";+import { assertRateLimit } from "@/lib/upstash/assert-rate-limit";+import { RATELIMIT_POLICIES } from "@/lib/upstash/ratelimit-policies";import { nanoid } from "@dub/utils";import * as z from "zod/v4";import { authActionClient } from "../safe-action";@@ -17,7 +19,7 @@ const inputSchema = z.object({export const uploadCampaignImageAction = authActionClient.inputSchema(inputSchema).action(async ({ ctx, parsedInput }) => {- const { workspace } = ctx;+ const { workspace, user } = ctx;const { contentType, contentLength } = parsedInput;throwIfNoPermission({@@ -33,6 +35,11 @@ export const uploadCampaignImageAction = authActionClientpolicy: "programCampaignImages",});+ await assertRateLimit({+ policy: RATELIMIT_POLICIES.workspaceFileUpload,+ identifier: [workspace.id, user.id],+ });+const { key, signedUrl, destinationUrl } = await createSignedUploadUrl({key: `programs/${programId}/emails/image_${nanoid(10)}`,contentType,diff --git a/apps/web/lib/actions/partners/upload-lander-image.ts b/apps/web/lib/actions/partners/upload-lander-image.tsindex afac2e1d0c..62a8e7d802 100644--- a/apps/web/lib/actions/partners/upload-lander-image.ts+++ b/apps/web/lib/actions/partners/upload-lander-image.ts@@ -4,6 +4,8 @@ import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-progrimport { createSignedUploadUrl } from "@/lib/storage/create-signed-upload-url";import { signedUploadInputSchema } from "@/lib/storage/schemas";import { validateSignedUpload } from "@/lib/storage/validate-signed-upload";+import { assertRateLimit } from "@/lib/upstash/assert-rate-limit";+import { RATELIMIT_POLICIES } from "@/lib/upstash/ratelimit-policies";import { nanoid } from "@dub/utils";import * as z from "zod/v4";import { authActionClient } from "../safe-action";@@ -17,7 +19,7 @@ const inputSchema = z.object({export const uploadLanderImageAction = authActionClient.inputSchema(inputSchema).action(async ({ ctx, parsedInput }) => {- const { workspace } = ctx;+ const { workspace, user } = ctx;const { contentType, contentLength } = parsedInput;throwIfNoPermission({@@ -33,6 +35,11 @@ export const uploadLanderImageAction = authActionClientpolicy: "programLanderImages",});+ await assertRateLimit({+ policy: RATELIMIT_POLICIES.workspaceFileUpload,+ identifier: [workspace.id, user.id],+ });+const { key, signedUrl, destinationUrl } = await createSignedUploadUrl({key: `programs/${programId}/lander/image_${nanoid(10)}`,contentType,diff --git a/apps/web/lib/storage.ts b/apps/web/lib/storage.tsindex 6958d38b7d..3527d71963 100644--- a/apps/web/lib/storage.ts+++ b/apps/web/lib/storage.ts@@ -49,7 +49,9 @@ class StorageClient {}const headers = {- "Content-Length": uploadBody.size.toString(),+ "Content-Length": String(+ uploadBody instanceof Blob ? uploadBody.size : uploadBody.byteLength,+ ),...opts?.headers,};@@ -104,6 +106,69 @@ class StorageClient {}}+ // Fetch object metadata (Content-Type, size, eTag) without downloading the body+ async head({ key, bucket = "public" }: { key: string; bucket?: BucketType }) {+ const response = await this.client.fetch(+ `${process.env.STORAGE_ENDPOINT}/${this._getBucketName(bucket)}/${key}`,+ {+ method: "HEAD",+ },+ );++ if (response.status === 404) {+ return null;+ }++ if (!response.ok) {+ throw new Error(+ `storage.head failed (${response.status}): ${response.statusText}`,+ );+ }++ const contentLengthHeader = response.headers.get("content-length");++ return {+ contentType: response.headers.get("content-type"),+ contentLength: contentLengthHeader+ ? Number.parseInt(contentLengthHeader, 10)+ : null,+ eTag: response.headers.get("etag"),+ };+ }++ // Read the first N bytes of an object (for magic-byte sniffing)+ async getBytes({+ key,+ bucket = "public",+ length = 4100,+ }: {+ key: string;+ bucket?: BucketType;+ length?: number;+ }) {+ const response = await this.client.fetch(+ `${process.env.STORAGE_ENDPOINT}/${this._getBucketName(bucket)}/${key}`,+ {+ method: "GET",+ headers: {+ Range: `bytes=0-${Math.max(length - 1, 0)}`,+ },+ },+ );++ if (response.status === 404) {+ return null;+ }++ if (!response.ok && response.status !== 206) {+ throw new Error(+ `storage.getBytes failed (${response.status}): ${response.statusText}`,+ );+ }++ return new Uint8Array(await response.arrayBuffer());+ }+async getSignedUrl({key,method,@@ -261,7 +326,11 @@ class StorageClient {const maxRedirects = 5;let currentUrl = url;- for (let redirectCount = 0; redirectCount <= maxRedirects; redirectCount++) {+ for (+ let redirectCount = 0;+ redirectCount <= maxRedirects;+ redirectCount+++ ) {await this.assertSafeUrl(currentUrl);const response = await fetchWithTimeout(currentUrl, {diff --git a/apps/web/lib/storage/magic-bytes.ts b/apps/web/lib/storage/magic-bytes.tsnew file mode 100644index 0000000000..70e3cd9bea--- /dev/null+++ b/apps/web/lib/storage/magic-bytes.ts@@ -0,0 +1,339 @@+/**+ * Magic-byte helpers for user-upload validation.+ * Used by Next.js processR2ObjectCreated (Worker only forwards queue messages).+ */++export const USER_UPLOAD_KEY_PREFIXES = [+ "programs/",+ "integration-screenshots/",+ "program-logos/",+ "messages/",+ "resumes/",+] as const;++export const DANGEROUS_CONTENT_TYPES = [+ "text/html",+ "application/xhtml+xml",+ "text/javascript",+ "application/javascript",+ "application/x-javascript",+ "text/jscript",+] as const;++const ZIP_FAMILY_CONTENT_TYPES = [+ "application/zip",+ "application/x-zip-compressed",+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document",+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",+] as const;++// Legacy .doc / .xls share OLE Compound File magic (not ZIP/OOXML)+const OLE_FAMILY_CONTENT_TYPES = [+ "application/msword",+ "application/vnd.ms-excel",+] as const;++const TEXT_CONTENT_TYPES = ["text/plain", "text/csv"] as const;++// Unambiguous magic when R2 omits Content-Type (exclude SVG/ZIP/OLE — need declared type)+const SAFE_MAGIC_ONLY_CONTENT_TYPES = [+ "image/jpeg",+ "image/png",+ "image/gif",+ "image/webp",+ "image/avif",+ "application/pdf",+] as const;++function isMissingOrGenericContentType(+ contentType: string | null | undefined,+): boolean {+ const normalized = normalizeContentType(contentType);+ return !normalized || normalized === "application/octet-stream";+}++export function isUserUploadKey(key: string): boolean {+ return USER_UPLOAD_KEY_PREFIXES.some((prefix) => key.startsWith(prefix));+}++export function normalizeContentType(+ contentType: string | null | undefined,+): string | null {+ if (!contentType) {+ return null;+ }++ const base = contentType.split(";")[0]?.trim().toLowerCase() ?? null;+ if (!base) {+ return null;+ }++ if (base === "image/jpg") {+ return "image/jpeg";+ }++ return base;+}++export function isDangerousContentType(+ contentType: string | null | undefined,+): boolean {+ const normalized = normalizeContentType(contentType);+ if (!normalized) {+ return false;+ }++ return (DANGEROUS_CONTENT_TYPES as readonly string[]).includes(normalized);+}++function startsWithBytes(bytes: Uint8Array, signature: number[]): boolean {+ if (bytes.length < signature.length) {+ return false;+ }++ return signature.every((value, index) => bytes[index] === value);+}++function indexOfBytes(haystack: Uint8Array, needle: number[]): number {+ outer: for (let i = 0; i <= haystack.length - needle.length; i++) {+ for (let j = 0; j < needle.length; j++) {+ if (haystack[i + j] !== needle[j]) {+ continue outer;+ }+ }+ return i;+ }+ return -1;+}++function looksLikeSvg(bytes: Uint8Array): boolean {+ const text = new TextDecoder("utf-8", { fatal: false })+ .decode(bytes)+ .trimStart()+ .toLowerCase();++ if (text.includes("<html") || text.includes("<!doctype html")) {+ return false;+ }++ return (+ text.includes("<svg") || (text.startsWith("<?xml") && text.includes("<svg"))+ );+}++/**+ * Detect MIME from the start of a file. Returns null when unknown.+ */+export function detectMimeFromMagicBytes(bytes: Uint8Array): string | null {+ if (bytes.length === 0) {+ return null;+ }++ // JPEG+ if (startsWithBytes(bytes, [0xff, 0xd8, 0xff])) {+ return "image/jpeg";+ }++ // PNG+ if (+ startsWithBytes(bytes, [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])+ ) {+ return "image/png";+ }++ // GIF+ if (+ startsWithBytes(bytes, [0x47, 0x49, 0x46, 0x38, 0x37, 0x61]) ||+ startsWithBytes(bytes, [0x47, 0x49, 0x46, 0x38, 0x39, 0x61])+ ) {+ return "image/gif";+ }++ // WebP: RIFF....WEBP+ if (+ startsWithBytes(bytes, [0x52, 0x49, 0x46, 0x46]) &&+ bytes.length >= 12 &&+ bytes[8] === 0x57 &&+ bytes[9] === 0x45 &&+ bytes[10] === 0x42 &&+ bytes[11] === 0x50+ ) {+ return "image/webp";+ }++ // AVIF / HEIC family: ....ftyp+ if (+ bytes.length >= 12 &&+ bytes[4] === 0x66 &&+ bytes[5] === 0x74 &&+ bytes[6] === 0x79 &&+ bytes[7] === 0x70+ ) {+ const brand = String.fromCharCode(+ bytes[8],+ bytes[9],+ bytes[10],+ bytes[11],+ ).toLowerCase();+ if (brand.startsWith("avif") || brand.startsWith("avis")) {+ return "image/avif";+ }+ }++ if (startsWithBytes(bytes, [0x25, 0x50, 0x44, 0x46])) {+ return "application/pdf";+ }++ // OLE Compound File (legacy .doc / .xls)+ if (+ startsWithBytes(bytes, [0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1])+ ) {+ return "application/x-ole-storage";+ }++ // ZIP / OOXML+ if (startsWithBytes(bytes, [0x50, 0x4b, 0x03, 0x04])) {+ return "application/zip";+ }++ // SVG (text)+ if (looksLikeSvg(bytes)) {+ return "image/svg+xml";+ }++ // HTML sniff (for explicit mismatch detection)+ const htmlMarkers = [+ [0x3c, 0x68, 0x74, 0x6d, 0x6c], // <html+ [0x3c, 0x48, 0x54, 0x4d, 0x4c], // <HTML+ [0x3c, 0x21, 0x44, 0x4f, 0x43, 0x54, 0x59, 0x50, 0x45], // <!DOCTYPE+ ];+ for (const marker of htmlMarkers) {+ if (+ indexOfBytes(bytes.slice(0, Math.min(bytes.length, 512)), marker) !== -1+ ) {+ return "text/html";+ }+ }++ return null;+}++export function mimeMatchesContentType({+ detectedMime,+ contentType,+}: {+ detectedMime: string | null;+ contentType: string | null | undefined;+}): boolean {+ const declared = normalizeContentType(contentType);+ if (!declared) {+ return false;+ }++ // TXT/CSV have no reliable magic; allow when declared and body is not sniffed+ // as something else (e.g. HTML would set detectedMime and fail below).+ if (+ (TEXT_CONTENT_TYPES as readonly string[]).includes(declared) &&+ !detectedMime+ ) {+ return true;+ }++ if (!detectedMime) {+ return false;+ }++ if (declared === detectedMime) {+ return true;+ }++ // OOXML / zip containers share PK magic+ if (+ detectedMime === "application/zip" &&+ (ZIP_FAMILY_CONTENT_TYPES as readonly string[]).includes(declared)+ ) {+ return true;+ }++ // Legacy Office binaries share OLE Compound File magic+ if (+ detectedMime === "application/x-ole-storage" &&+ (OLE_FAMILY_CONTENT_TYPES as readonly string[]).includes(declared)+ ) {+ return true;+ }++ return false;+}++export type QuarantineDecision =+ | { action: "skip"; reason: string }+ | { action: "allow"; reason: string }+ | {+ action: "quarantine";+ reason: string;+ detectedMime: string | null;+ contentType: string | null;+ };++export function decideQuarantine({+ key,+ contentType,+ bytes,+}: {+ key: string;+ contentType: string | null | undefined;+ bytes: Uint8Array;+}): QuarantineDecision {+ if (!isUserUploadKey(key)) {+ return {+ action: "skip",+ reason: "non_user_upload_prefix",+ };+ }++ const normalized = normalizeContentType(contentType);++ if (isDangerousContentType(normalized)) {+ return {+ action: "quarantine",+ reason: "dangerous_content_type",+ detectedMime: detectMimeFromMagicBytes(bytes),+ contentType: normalized,+ };+ }++ const detectedMime = detectMimeFromMagicBytes(bytes);++ if (+ mimeMatchesContentType({+ detectedMime,+ contentType: normalized,+ })+ ) {+ return {+ action: "allow",+ reason: "magic_bytes_match",+ };+ }++ // R2 sometimes omits Content-Type or stores octet-stream; trust unambiguous magic+ if (+ isMissingOrGenericContentType(normalized) &&+ detectedMime &&+ (SAFE_MAGIC_ONLY_CONTENT_TYPES as readonly string[]).includes(detectedMime)+ ) {+ return {+ action: "allow",+ reason: "safe_magic_missing_content_type",+ };+ }++ return {+ action: "quarantine",+ reason: detectedMime ? "magic_bytes_mismatch" : "unknown_magic_bytes",+ detectedMime,+ contentType: normalized,+ };+}diff --git a/apps/web/lib/storage/process-r2-object-created.ts b/apps/web/lib/storage/process-r2-object-created.tsnew file mode 100644index 0000000000..81a29f897a--- /dev/null+++ b/apps/web/lib/storage/process-r2-object-created.ts@@ -0,0 +1,134 @@+import { logger } from "@/lib/axiom/server";+import { storage } from "@/lib/storage";+import { decideQuarantine } from "@/lib/storage/magic-bytes";+import * as z from "zod/v4";++const CREATE_ACTIONS = new Set([+ "PutObject",+ "CopyObject",+ "CompleteMultipartUpload",+]);++export const r2ObjectCreatedNotificationSchema = z+ .object({+ account: z.string().optional(),+ action: z.string(),+ bucket: z.string().optional(),+ object: z+ .object({+ key: z.string().optional(),+ size: z.number().optional(),+ eTag: z.string().optional(),+ })+ .optional(),+ eventTime: z.string().optional(),+ })+ .passthrough();++type R2ObjectCreatedNotification = z.infer<+ typeof r2ObjectCreatedNotificationSchema+>;++type ProcessR2ObjectCreatedResult =+ | { status: "skipped"; reason: string; key?: string }+ | { status: "allowed"; reason: string; key: string }+ | {+ status: "removed";+ reason: string;+ key: string;+ contentType: string | null;+ detectedMime: string | null;+ };++// Validate magic bytes for an R2 object-create notification; delete on mismatch+export async function processR2ObjectCreated(+ notification: R2ObjectCreatedNotification,+): Promise<ProcessR2ObjectCreatedResult> {+ const key = notification.object?.key;+ const action = notification.action;++ if (!key) {+ return {+ status: "skipped",+ reason: "missing_key",+ };+ }++ if (action && !CREATE_ACTIONS.has(action)) {+ return {+ status: "skipped",+ reason: "non_create_action",+ key,+ };+ }++ const head = await storage.head({ key, bucket: "public" });+ if (!head) {+ return {+ status: "skipped",+ reason: "object_not_found",+ key,+ };+ }++ const bytes = await storage.getBytes({+ key,+ bucket: "public",+ length: 4100,+ });++ if (!bytes) {+ return {+ status: "skipped",+ reason: "object_not_found",+ key,+ };+ }++ const decision = decideQuarantine({+ key,+ contentType: head.contentType,+ bytes,+ });++ if (decision.action === "skip") {+ return {+ status: "skipped",+ reason: decision.reason,+ key,+ };+ }++ if (decision.action === "allow") {+ return {+ status: "allowed",+ reason: decision.reason,+ key,+ };+ }++ await storage.delete({+ key,+ bucket: "public",+ });++ logger.warn("storage.r2_magic_bytes_mismatch", {+ key,+ contentType: decision.contentType,+ detectedMime: decision.detectedMime,+ reason: decision.reason,+ action,+ size: notification.object?.size ?? head.contentLength,+ eTag: notification.object?.eTag ?? head.eTag,+ });++ await logger.flush();++ return {+ status: "removed",+ reason: decision.reason,+ key,+ contentType: decision.contentType,+ detectedMime: decision.detectedMime,+ };+}diff --git a/apps/web/tests/misc/magic-bytes.test.ts b/apps/web/tests/misc/magic-bytes.test.tsnew file mode 100644index 0000000000..3c987429e3--- /dev/null+++ b/apps/web/tests/misc/magic-bytes.test.ts@@ -0,0 +1,265 @@+import {+ decideQuarantine,+ detectMimeFromMagicBytes,+ isUserUploadKey,+ mimeMatchesContentType,+} from "@/lib/storage/magic-bytes";+import { describe, expect, it } from "vitest";++function bytes(...values: number[]) {+ return new Uint8Array(values);+}++describe("detectMimeFromMagicBytes", () => {+ it("detects JPEG", () => {+ expect(detectMimeFromMagicBytes(bytes(0xff, 0xd8, 0xff, 0xe0))).toBe(+ "image/jpeg",+ );+ });++ it("detects PNG", () => {+ expect(+ detectMimeFromMagicBytes(+ bytes(0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a),+ ),+ ).toBe("image/png");+ });++ it("detects PDF", () => {+ expect(+ detectMimeFromMagicBytes(bytes(0x25, 0x50, 0x44, 0x46, 0x2d, 0x31)),+ ).toBe("application/pdf");+ });++ it("detects HTML", () => {+ const html = new TextEncoder().encode("<!DOCTYPE html><html>");+ expect(detectMimeFromMagicBytes(html)).toBe("text/html");+ });++ it("detects OLE compound storage", () => {+ expect(+ detectMimeFromMagicBytes(+ bytes(0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1, 0x00),+ ),+ ).toBe("application/x-ole-storage");+ });+});++describe("mimeMatchesContentType", () => {+ it("matches jpeg aliases", () => {+ expect(+ mimeMatchesContentType({+ detectedMime: "image/jpeg",+ contentType: "image/jpg",+ }),+ ).toBe(true);+ });++ it("allows zip magic for docx", () => {+ expect(+ mimeMatchesContentType({+ detectedMime: "application/zip",+ contentType:+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document",+ }),+ ).toBe(true);+ });++ it("does not treat zip magic as legacy msword", () => {+ expect(+ mimeMatchesContentType({+ detectedMime: "application/zip",+ contentType: "application/msword",+ }),+ ).toBe(false);+ });++ it("allows OLE magic for msword and xls", () => {+ expect(+ mimeMatchesContentType({+ detectedMime: "application/x-ole-storage",+ contentType: "application/msword",+ }),+ ).toBe(true);+ expect(+ mimeMatchesContentType({+ detectedMime: "application/x-ole-storage",+ contentType: "application/vnd.ms-excel",+ }),+ ).toBe(true);+ });++ it("allows text types with no detected magic", () => {+ expect(+ mimeMatchesContentType({+ detectedMime: null,+ contentType: "text/plain",+ }),+ ).toBe(true);+ expect(+ mimeMatchesContentType({+ detectedMime: null,+ contentType: "text/csv",+ }),+ ).toBe(true);+ });++ it("rejects png declared as html body", () => {+ expect(+ mimeMatchesContentType({+ detectedMime: "text/html",+ contentType: "image/png",+ }),+ ).toBe(false);+ });+});++describe("decideQuarantine", () => {+ it("skips non user-upload prefixes", () => {+ const decision = decideQuarantine({+ key: "customers/avatar.png",+ contentType: "image/png",+ bytes: bytes(0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a),+ });+ expect(decision.action).toBe("skip");+ });++ it("allows matching PNG on programs/", () => {+ expect(isUserUploadKey("programs/prog_x/emails/image_abc")).toBe(true);+ const decision = decideQuarantine({+ key: "programs/prog_x/emails/image_abc",+ contentType: "image/png",+ bytes: bytes(0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a),+ });+ expect(decision).toMatchObject({+ action: "allow",+ reason: "magic_bytes_match",+ });+ });++ it("quarantines HTML uploaded as image/png", () => {+ const html = new TextEncoder().encode("<!DOCTYPE html><html>xss</html>");+ const decision = decideQuarantine({+ key: "integration-screenshots/abc",+ contentType: "image/png",+ bytes: html,+ });+ expect(decision.action).toBe("quarantine");+ if (decision.action === "quarantine") {+ expect(decision.detectedMime).toBe("text/html");+ expect(decision.reason).toBe("magic_bytes_mismatch");+ }+ });++ it("quarantines dangerous content types", () => {+ const decision = decideQuarantine({+ key: "program-logos/logo",+ contentType: "text/html",+ bytes: bytes(0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a),+ });+ expect(decision.action).toBe("quarantine");+ if (decision.action === "quarantine") {+ expect(decision.reason).toBe("dangerous_content_type");+ }+ });++ it("allows text/plain and text/csv under programs/", () => {+ const plain = decideQuarantine({+ key: "programs/prog_x/files/notes-abcd.txt",+ contentType: "text/plain",+ bytes: new TextEncoder().encode("hello partner resources"),+ });+ expect(plain).toMatchObject({+ action: "allow",+ reason: "magic_bytes_match",+ });++ const csv = decideQuarantine({+ key: "programs/prog_x/files/export-abcd.csv",+ contentType: "text/csv",+ bytes: new TextEncoder().encode("name,email\nAda,[email protected]\n"),+ });+ expect(csv).toMatchObject({+ action: "allow",+ reason: "magic_bytes_match",+ });+ });++ it("quarantines HTML declared as text/plain", () => {+ const html = new TextEncoder().encode("<!DOCTYPE html><html>xss</html>");+ const decision = decideQuarantine({+ key: "programs/prog_x/files/notes-abcd.txt",+ contentType: "text/plain",+ bytes: html,+ });+ expect(decision.action).toBe("quarantine");+ if (decision.action === "quarantine") {+ expect(decision.detectedMime).toBe("text/html");+ expect(decision.reason).toBe("magic_bytes_mismatch");+ }+ });++ it("allows legacy doc/xls OLE under programs/", () => {+ const ole = bytes(0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1, 0x00);+ expect(+ decideQuarantine({+ key: "programs/prog_x/files/brief-abcd.doc",+ contentType: "application/msword",+ bytes: ole,+ }),+ ).toMatchObject({+ action: "allow",+ reason: "magic_bytes_match",+ });+ expect(+ decideQuarantine({+ key: "programs/prog_x/files/sheet-abcd.xls",+ contentType: "application/vnd.ms-excel",+ bytes: ole,+ }),+ ).toMatchObject({+ action: "allow",+ reason: "magic_bytes_match",+ });+ });++ it("allows safe magic when Content-Type is missing or octet-stream", () => {+ const png = bytes(0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a);+ expect(+ decideQuarantine({+ key: "programs/prog_x/emails/image_abc",+ contentType: null,+ bytes: png,+ }),+ ).toMatchObject({+ action: "allow",+ reason: "safe_magic_missing_content_type",+ });+ expect(+ decideQuarantine({+ key: "programs/prog_x/emails/image_abc",+ contentType: "application/octet-stream",+ bytes: png,+ }),+ ).toMatchObject({+ action: "allow",+ reason: "safe_magic_missing_content_type",+ });+ });++ it("quarantines zip/svg when Content-Type is missing", () => {+ const zip = decideQuarantine({+ key: "programs/prog_x/files/archive-abcd.zip",+ contentType: null,+ bytes: bytes(0x50, 0x4b, 0x03, 0x04),+ });+ expect(zip.action).toBe("quarantine");++ const svg = decideQuarantine({+ key: "programs/prog_x/logos/mark-abcd.svg",+ contentType: null,+ bytes: new TextEncoder().encode('<svg xmlns="http://www.w3.org/2000/svg"/>'),+ });+ expect(svg.action).toBe("quarantine");+ });+});diff --git a/packages/workers/r2-object-created/README.md b/packages/workers/r2-object-created/README.mdnew file mode 100644index 0000000000..3bf816925b--- /dev/null+++ b/packages/workers/r2-object-created/README.md@@ -0,0 +1,26 @@+# r2-object-created Worker (thin forwarder)++Cloudflare Queue **push** consumer that forwards each R2 `object-create` notification to Next.js. Magic-byte validation, delete-on-mismatch, and Axiom logging run in the app.++## Quick Edit++1. Paste [`worker.js`](./worker.js) into Workers Quick Edit → Deploy.+2. Settings → Bindings / Variables / Secrets (below).+3. Confirm public R2 bucket **Event notification** `object-create` → matching queue.++## Bindings++| Type | Name | Value |+| ----------------- | ---------------------------------- | --------------------------------------------------------- |+| Queues (consumer) | — | `r2-object-created` or `-dev` |+| Variable | `DUB_WEBHOOK_URL` | `https://<host>/api/cloudflare/webhook/r2-object-created` |+| Secret | `CLOUDFLARE_WORKER_WEBHOOK_SECRET` | Same as Next.js `CLOUDFLARE_WORKER_WEBHOOK_SECRET` |++No R2 or Axiom bindings on the Worker — Next.js uses `STORAGE_*` and Axiom already.++## Flow++```text+R2 object-create → Queue → Worker → POST /api/cloudflare/webhook/r2-object-created+ → magic-byte check → delete public object on mismatch → Axiom+```diff --git a/packages/workers/r2-object-created/worker.js b/packages/workers/r2-object-created/worker.jsnew file mode 100644index 0000000000..003bf8ce00--- /dev/null+++ b/packages/workers/r2-object-created/worker.js@@ -0,0 +1,72 @@+/**+ *+ * Thin Cloudflare Worker: queue push → forward each R2 notification to Next.js.+ * Magic-byte validation + delete-on-mismatch live in Next.js, not here.+ *+ * - Variable DUB_WEBHOOK_URL e.g. https://app.dub.co/api/cloudflare/webhook/r2-object-created+ * - Secret CLOUDFLARE_WORKER_WEBHOOK_SECRET (same value as Next.js env)+ */++function parseBody(body) {+ if (typeof body === "string") {+ return JSON.parse(body);+ }++ if (body && typeof body === "object") {+ return body;+ }++ throw new Error("Unexpected queue message body type");+}++async function forwardToNextjs(env, notification) {+ const url = env.DUB_WEBHOOK_URL;+ const secret = env.CLOUDFLARE_WORKER_WEBHOOK_SECRET;++ if (!url || !secret) {+ throw new Error(+ "DUB_WEBHOOK_URL and CLOUDFLARE_WORKER_WEBHOOK_SECRET must be set",+ );+ }++ const response = await fetch(url, {+ method: "POST",+ headers: {+ Authorization: `Bearer ${secret}`,+ "Content-Type": "application/json",+ },+ body: JSON.stringify({ notification }),+ });++ if (!response.ok) {+ const text = await response.text();+ throw new Error(+ `Next.js webhook failed (${response.status}): ${text.slice(0, 500)}`,+ );+ }++ return response.json().catch(() => ({}));+}++export default {+ async fetch() {+ return new Response("r2-object-created forwarder ok");+ },++ async queue(batch, env) {+ for (const message of batch.messages) {+ try {+ const notification = parseBody(message.body);+ const result = await forwardToNextjs(env, notification);+ console.info("forwarded r2 notification", {+ key: notification?.object?.key,+ result,+ });+ message.ack();+ } catch (error) {+ console.error("forward failed", error);+ message.retry();+ }+ }+ },+};