TL;DR: keep the bucket private, keep snapshot ownership in the application database, and issue a short-lived signed download URL only after four checks: authenticated user, tenant membership, snapshot state, and object-key match. That adds one small authorization endpoint, but it prevents a copied object key from becoming a cross-tenant restore path. For an edtech backup system, I take that trade every time.
| Design | Access-control boundary | Delivery work | Best fit |
|---|---|---|---|
| Application streams every download | App checks every byte request | Highest | Extra inspection or transformation |
| App authorizes, then signs a private object URL | App checks once; storage validates the signature | Low | Routine snapshot delivery |
| Public object with an unguessable key | Possession of the key | Lowest | Not tenant backups |
Recommendation: use application authorization followed by a short-lived signed URL. Keep streaming as the runner-up for cases that require per-byte policy or content transformation. Never treat an opaque key as authorization.
How should Node.js upload a private document to object storage?
A private bucket blocks anonymous reads. It does not decide whether teacher A may restore school B's selected snapshot. That decision lives in the application because the application knows the user, tenant, role, retention state, and requested snapshot.
The dangerous shortcut is accepting an object key from the client and signing it directly. An attacker who learns another key has bypassed the tenant lookup. The signer did exactly what it was asked to do. The application asked the wrong question.
Make the database record authoritative instead. A restore request supplies a snapshot ID. The server loads that record within the caller's tenant, verifies that the snapshot is ready, and uses the stored object key. It then checks that the stored key belongs to that same snapshot before asking the adapter for a signed URL. This sequence matters because the browser knows the requested snapshot, while only the server knows its current owner and storage location. Returning the same missing response for a missing row and a tenant mismatch keeps the lookup from becoming a tenant-discovery tool. The audit record captures the decision by stable IDs, not by copying the signed query string. The document bytes can then travel directly from private object storage to the authorized client.
Four checks.
No key comes from the browser.
This also keeps storage metadata boring. Use opaque identifiers such as tenant_7f3 and snap_a91, not student names, email addresses, course titles, or school names. The database can map those identifiers to business records under the normal authorization model.
The two criteria that decide the design
The first criterion is where authorization can be revoked. A signed URL is a time-limited capability: anyone holding it can use it until its validity ends, subject to the conditions enforced by the storage service. AWS documents that presigned URLs use the permissions of the principal that created them and can be reused until expiration. So expiry is part of the access model, not a cleanup detail. Keep it aligned with the restore workflow, and never log the full URL.
The second criterion is delivery simplicity. Once authorization succeeds, object storage can serve the document without routing PDF or DOCX bytes through the Node.js process. That removes an application data path, but it does not remove application policy. The API still owns the tenant check, snapshot selection, audit event, and decision to mint the capability.
I benchmark the boundary, not a synthetic GET in isolation: authorization lookup latency, signing latency, time to first byte, and failed-restore rate. A storage benchmark that skips the tenant query measures the easy half. It also hides the glue developers will debug in production.
A small TypeScript boundary
The storage adapter needs two operations for this path. The policy stays outside it. No provider SDK types leak into the handler.
type Snapshot = {
id: string;
tenantId: string;
objectKey: string;
state: "ready" | "creating" | "expired";
fileName: string;
};
type RestoreActor = {
userId: string;
tenantId: string;
canRestoreBackups: boolean;
};
interface SnapshotRepository {
findById(id: string): Promise<Snapshot | null>;
}
interface PrivateObjectStore {
createSignedDownload(input: {
key: string;
expiresInSeconds: number;
downloadName: string;
}): Promise<string>;
}
interface AuditLog {
record(event: {
action: "snapshot.download_authorized";
actorId: string;
tenantId: string;
snapshotId: string;
}): Promise<void>;
}
export async function authorizeSnapshotDownload(
actor: RestoreActor,
snapshotId: string,
snapshots: SnapshotRepository,
objects: PrivateObjectStore,
audit: AuditLog,
): Promise<{ url: string; expiresInSeconds: number }> {
if (!actor.canRestoreBackups) throw new Error("forbidden");
const snapshot = await snapshots.findById(snapshotId);
if (!snapshot) throw new Error("not_found");
if (snapshot.tenantId !== actor.tenantId) throw new Error("not_found");
if (snapshot.state !== "ready") throw new Error("snapshot_unavailable");
const expiresInSeconds = 300;
const url = await objects.createSignedDownload({
key: snapshot.objectKey,
expiresInSeconds,
downloadName: snapshot.fileName,
});
await audit.record({
action: "snapshot.download_authorized",
actorId: actor.userId,
tenantId: actor.tenantId,
snapshotId: snapshot.id,
});
return { url, expiresInSeconds };
}
The not_found response for both a missing snapshot and a tenant mismatch avoids confirming that another tenant's snapshot ID exists. The 300-second value is a policy choice in this example, not a universal optimum. Measure restore startup time and set the window deliberately.
Upload follows the same ownership rule. The application chooses an opaque object key, records the tenant and user association in its database, and marks the snapshot ready only after the object operation succeeds. PDF and DOCX are display names or accepted media policy; neither file extension grants trust. A backup is restorable only when the database state and stored object agree.
Tests should try the ugly paths: a valid user with another tenant's snapshot ID, a ready database row whose object is unavailable, an expired snapshot, a reused signed URL after its window, and two restore requests racing against retention cleanup. Record authorization outcomes by stable IDs. Do not record signed query strings.
When the runner-up is better
This design has a real limitation: access cannot be revoked instantly after a signed URL has been issued. A five-minute URL remains a usable capability for its valid window. The trade-off is explicit. If policy changes must interrupt an active restore, choose application streaming instead.
Stream through the application when the download must be transformed, scanned during delivery, watermarked, or stopped according to a policy that changes inside the signed window. The app pays for that control by remaining in the byte path. Backpressure, range requests, process memory, and client disconnects now matter. Test them.
A storage-native signed URL is better when the file is already final and the authorization decision can remain valid for a brief, explicit interval. Both major object-storage models documented by AWS and Google Cloud support time-limited signed access, although their credentials and signing procedures differ. Put those differences behind the adapter and test the contract against the deployment target.
Do not build a switchboard of configuration flags. One adapter, one expiry policy, one database ownership check, and one integration test for a real PDF plus one for a real DOCX are enough to expose most boundary mistakes. Add knobs only after a concrete requirement appears. Config is debt with a friendly name.
The final decision rule is blunt: if revocation must take effect during an active transfer, stream; otherwise authorize once and sign briefly. Either way, the database owns tenant access and object storage owns bytes. Keeping those jobs separate makes a selected snapshot restore understandable, testable, and hard to cross-wire.
Top comments (0)