How to Secure Object Storage Service: Pre-Signed User File Uploads and Downloads
Short answer: use an object-storage service that can issue short-lived pre-signed URLs, but make tenant identity part of the authorization decision before any URL is created. The URL is a temporary capability, not an acc
Short answer: use an object-storage service that can issue short-lived pre-signed URLs, but make tenant identity part of the authorization decision before any URL is created. The URL is a temporary capability, not an access-control system. For logistics receipts, the durable design is a server-owned object key, a tenant-scoped policy check, an upload record, and an audit trail that preserves the original file.
Here is the decision matrix I use for a one-person SaaS:
| Decision | Prefer | Why |
|---|---|---|
| Browser sends file bytes | Direct upload with a pre-signed URL | The application server handles authorization, not every byte |
| Browser downloads a receipt | A new pre-signed URL per request | Access can expire and be checked against current membership |
| Object naming | Server-generated tenant-scoped key | User input cannot choose another tenant's path |
| Audit requirement | Immutable original plus metadata record | A thumbnail or parsed result is not the evidence file |
Keep the original.
My recommendation is to keep the storage interface small and put the important policy in your application. Ship the first version weekly. Outsource the undifferentiated byte transfer to object storage, while retaining ownership of identity, tenant boundaries, and audit events. That usually produces more revenue per hour than building a proxy endpoint that streams every receipt through your server.
What should an object storage service support for pre-signed URLs?
A pre-signed URL should grant one narrow operation on one object for a short period. It should not grant a user a directory, a tenant, or a reusable session. The signing service must choose the object key, HTTP method, content type constraints, and expiry. The browser receives the resulting URL only after the application has authenticated the user and checked the tenant relationship.
The crucial boundary is before signing. A request such as tenantId=acme is only input. It is not proof that the caller belongs to Acme. Resolve the tenant from the authenticated session, check the user's role, then derive the key from that server-side tenant identifier.
For uploads, create a pending receipt row before issuing the URL. Store the expected tenant, object key, size limit, media type, uploader, and expiry. After the upload, verify that the object exists and that its metadata matches the pending row before marking the receipt complete. A client-side success message is not evidence that the original reached storage.
Which isolation rules matter most for a logistics receipt system?
There are two rules worth refusing to compromise on.
First, every object key must have a server-generated tenant prefix. A useful shape is tenants/{tenantId}/receipts/{receiptId}/original, where tenantId and receiptId are opaque identifiers generated by the service. Do not use a filename as an identifier. Names contain slashes, Unicode, collisions, and occasionally an attempt to escape the intended namespace.
Second, every read must repeat authorization. Do not treat possession of a receipt ID as permission to download it. Look up the receipt, compare its tenant to the authenticated tenant, verify the user's role, and only then sign a download URL. This matters after an employee leaves a carrier account: an old link may expire, but current authorization still has to be checked when a new one is issued.
I initially treated the upload URL as the hard part. The harder bug class is the download path, because a convenient GET /receipts/:id handler can accidentally become a cross-tenant oracle. The fix is architectural: the storage adapter receives an already-authorized object reference, never a raw tenant or filename from the browser.
A small TypeScript flow that preserves the boundary
The following interfaces keep storage mechanics behind an application-owned contract. The signing implementation can use any service that supports pre-signed operations.
type ReceiptIntent = {
tenantId: string;
receiptId: string;
objectKey: string;
contentType: string;
maxBytes: number;
};
type StorageSigner = {
signUpload(input: {
key: string;
contentType: string;
maxBytes: number;
expiresInSeconds: number;
}): Promise<{ url: string; expiresAt: string }>;
signDownload(input: {
key: string;
expiresInSeconds: number;
}): Promise<{ url: string; expiresAt: string }>;
};
function receiptKey(tenantId: string, receiptId: string): string {
return `tenants/${tenantId}/receipts/${receiptId}/original`;
}
async function createUploadIntent(
signer: StorageSigner,
actor: { tenantId: string; canUploadReceipts: boolean },
input: { receiptId: string; contentType: string },
): Promise<ReceiptIntent & { uploadUrl: string; expiresAt: string }> {
if (!actor.canUploadReceipts) {
throw new Error("receipt upload is not allowed");
}
const intent: ReceiptIntent = {
tenantId: actor.tenantId,
receiptId: input.receiptId,
objectKey: receiptKey(actor.tenantId, input.receiptId),
contentType: input.contentType,
maxBytes: 10 * 1024 * 1024,
};
const signed = await signer.signUpload({
key: intent.objectKey,
contentType: intent.contentType,
maxBytes: intent.maxBytes,
expiresInSeconds: 300,
});
return { ...intent, uploadUrl: signed.url, expiresAt: signed.expiresAt };
}
The five-minute expiry is an example policy, not a universal answer. Choose it from the expected upload time, network conditions, and risk of a leaked capability. The service should also reject a completed receipt whose stored size, media type, or checksum fails the pending intent. Keep that validation in the completion transaction so a retry cannot produce two competing originals.
A download flow is shorter because the object already exists, but it still begins with the database authorization check:
async function createDownloadUrl(
signer: StorageSigner,
actor: { tenantId: string; canReadReceipts: boolean },
receipt: { tenantId: string; objectKey: string },
): Promise<string> {
if (!actor.canReadReceipts || receipt.tenantId !== actor.tenantId) {
throw new Error("receipt is not accessible");
}
const signed = await signer.signDownload({
key: receipt.objectKey,
expiresInSeconds: 120,
});
return signed.url;
}
Do not log the full URL. Query strings commonly contain the signature. Log the receipt ID, tenant ID, actor ID, operation, object key hash, and authorization result instead. That gives an auditor a useful event without turning application logs into a second copy of the capability.
What should be tested before shipping?
The highest-value tests are cross-tenant tests, not happy-path upload tests. Create two tenants, issue a receipt in each, and assert that a member of Tenant A cannot obtain a download URL for Tenant B. Repeat with a receipt ID supplied by the client, a changed filename, and a stale membership record. Test that an expired upload cannot be completed and that a content-type mismatch leaves the receipt pending.
Run these checks in CI with a fake signer and a storage integration test. The fake signer should record the key, expiry, and operation so the test can assert policy without exposing real credentials. In a deployment, monitor signing failures, abandoned pending intents, object verification failures, and authorization denials by tenant-safe dimensions. Avoid putting raw filenames or signed URLs into metrics labels.
The runner-up architecture is a server-side upload and download proxy. It can be the better choice when a legacy client cannot perform direct uploads, when a compliance boundary requires every byte to pass through an inspection service, or when the application must transform content synchronously. It costs application bandwidth and adds a scaling path, but those constraints can be the right trade. A pre-signed URL is not automatically superior; it is superior when direct transfer fits the trust and inspection model.
The limitation is clear: a direct pre-signed transfer is a poor fit when the service must inspect or rewrite every byte before storage. In that case, accept the proxy's latency and bandwidth cost, and choose the architecture that enforces the required control. The useful decision is about tenant isolation and evidence handling, not about making one transfer pattern win everywhere.
For the normal browser receipt flow, the decision rule is simple: authorize in the application, sign one narrowly scoped operation, verify the resulting object, and keep the original under a tenant-owned key. That is enough separation to ship without making storage the center of the product.
Sources
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.