Secure Private PDF Exports in Next.js

Summary

Pixel’s DEV Community announcement explains how to secure private PDF exports with an authenticated Next.js API route, tenant authorization, and server-selected US or EU storage. It covers export states, short-lived signed download URLs, and deletion deadlines to protect sensitive gaming documents.

DEV Community

Tenant-Isolated Next.js API Route for Private PDF Export via Signed Object Storage

For a gaming product that exports signed documents, the difficult part is not generating a PDF or signing a URL. It is proving that the player asking for the file belongs to the tenant that owns it, while still honoring a deletion deadline. Short answer: keep the PDF private, store an application-owned export record, resolve the tenant's US or EU placement on the server, and let the authenticated Next.js API route mint a short-lived download URL only after the object and authorization checks pass. That choice also gives the system a useful test target. A download request is either authorized and ready, authorized but still processing, or rejected. The browser never chooses the bucket, region, or object key. Retention and regional policy come first The tempting implementation is a button that receives a storage URL from the export worker. It works in a happy-path demo. It makes a poor security boundary in a multi-tenant game, where a document might contain tournament results, moderation evidence, or a signed partnership agreement. The durable record should be an export ID in the application database. It needs the tenant ID, the server-derived object key, the selected region, the export state, and the deletion deadline. The object store holds bytes; the application owns the relationship between those bytes and a tenant. That separation matters after a browser refresh. A URL copied into a log or email should not become the application's permanent authorization record. The API route can issue a fresh link when the player asks for the file, and a cleanup worker can delete the object when its deadline arrives. A signed URL is delivery machinery, not identity. Keep it private. I also want the export state to be explicit. processing means the worker has not made a downloadable object available. ready means the application found the expected object for the already-authorized export. A missing object must not turn into a guess at a different key, a second render, or a client-controlled path. Can a tenant-owned ID carry a private PDF export through a Next.js API route? Put the checks in this order: Validate the authenticated session. Load the export record by its application-owned ID. Compare the session tenant with the record tenant. Resolve the approved US or EU placement from trusted tenant configuration. Use the stored key for that placement and check whether the object is ready. Mint a temporary signed download URL for that exact object. The client can send an export ID. It must not send a bucket, region, prefix, or object key and ask the route to sign it. That distinction is the tenant-isolation rule in one sentence. Here is the decision logic behind the route. The storage adapter is intentionally generic: its job is to make the application policy visible, while the provider-specific signing implementation stays behind the interface. from dataclasses import dataclass
from datetime import datetime, timezone
from typing import Protocol

class PrivateObjectStore(Protocol):
def exists(self, *, region: str, key: str) -> bool:
...

def sign_download(self, *, region: str, key: str, expires_seconds: int) -> str:
    ...

@dataclass(frozen=True)
class Export:
export_id: str
tenant_id: str
region: str
object_key: str
delete_at: datetime

def download_state(
*,
session_tenant_id: str,
export: Export,
store: PrivateObjectStore,
now: datetime | None = None,
) -> dict[str, str]:
current_time = now or datetime.now(timezone.utc)

if session_tenant_id != export.tenant_id:
    return {"state": "forbidden"}
if current_time >= export.delete_at:
    return {"state": "expired"}
if not store.exists(region=export.region, key=export.object_key):
    return {"state": "processing"}

url = store.sign_download(
    region=export.region,
    key=export.object_key,
    expires_seconds=300,
)
return {"state": "ready", "download_url": url}

The re...
Secure Private PDF Exports in Next.js
Powered by Readybot.io

The latest from Pixel

Incident Response: Look Beyond the Red Alert

Pixel’s DEV Community announcement explores how teams can fixate on one failing service while overlooking the real cause of an outage, such as a full …

Sach: AI Fact-Checks WhatsApp Forwards in India

Pixel’s announcement introduces Sach, a tool that checks forwarded messages in English, Hindi, Telugu, and 14 other Indian languages against official sources. It gives cited …

Java 25 Removes SecurityManager: What to Fix

Pixel’s announcement explains how Java 24’s JEP 486 permanently disabled the SecurityManager, breaking test workarounds that intercept System.exit. It recommends returning exit codes or throwing …