Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
fix: cloudfront AccessDenied for downloads with renamed filename
Marc Seitz1e88cf872703
diff --git a/lib/utils/download-document.ts b/lib/utils/download-document.tsindex daccca59..eb546917 100644--- a/lib/utils/download-document.ts+++ b/lib/utils/download-document.ts@@ -1,23 +1,12 @@/**- * Trigger a document download from one of the link-scoped download- * endpoints (`/api/links/download` or `/api/links/download/dataroom-document`).+ * Trigger a document download from one of the link-scoped download endpoints.*- * The server returns one of two response shapes:- *- * 1. A buffered binary response (currently only watermarked PDFs that need- * mupdf annotation) with `Content-Disposition` set to the desired- * filename. We turn it into a blob + `<a download>` to save it.- *- * 2. A JSON response of the form `{ downloadUrl, fileName }`. The URL is- * a CloudFront / S3 presigned URL with `Content-Disposition: attachment`- * baked into the object metadata, so the browser will save it directly.- * We hand it to a hidden iframe instead of `<a download>` because the- * `download` attribute is silently ignored for cross-origin URLs in- * Chromium/Firefox/Safari, which would otherwise navigate the top-level- * page to the CloudFront URL.- *- * `fallbackFileName` is used only when the buffered response is missing- * its `Content-Disposition` header (shouldn't happen in practice).+ * Handles two response shapes:+ * 1. Buffered binary (watermarked PDFs) -> save as blob via `<a download>`.+ * 2. JSON `{ downloadUrl }` -> hand to a hidden iframe. We can't use+ * `<a download>` here because the `download` attribute is ignored for+ * cross-origin URLs, which would turn the click into a top-level+ * navigation to the CloudFront URL.*/export async function downloadFromLinkEndpoint({endpoint,diff --git a/pages/api/file/s3/get-presigned-get-url.ts b/pages/api/file/s3/get-presigned-get-url.tsindex a52953ef..75a75059 100644--- a/pages/api/file/s3/get-presigned-get-url.ts+++ b/pages/api/file/s3/get-presigned-get-url.ts@@ -65,42 +65,57 @@ export default async function handler(const { client, config } = await getTeamS3ClientAndConfig(teamId);if (config.distributionHost) {- // CloudFront signed URLs DO honor `response-content-disposition` if S3- // is the origin and the distribution forwards that query param. The- // catch is that `@aws-sdk/cloudfront-signer` mangles the encoding when- // it parses the URL, so the resulting URL fails with AccessDenied.- // Workaround: set the param via URL.searchParams (so it's part of the- // signed policy) and then re-set it on the signed output to fix the- // encoding.- // See https://obviy.us/blog/cloudfront-signed-disposition/const distributionUrl = new URL(key,`https://${config.distributionHost}`,);- if (responseContentDisposition) {- distributionUrl.searchParams.set(- "response-content-disposition",- responseContentDisposition,- );++ if (!responseContentDisposition) {+ const url = getCloudfrontSignedUrl({+ url: distributionUrl.toString(),+ keyPairId: `${config.distributionKeyId}`,+ privateKey: `${config.distributionKeyContents}`,+ dateLessThan: new Date(Date.now() + expiration).toISOString(),+ });++ return res.status(200).json({ url });}- const signed = getCloudfrontSignedUrl({+ // Use a custom policy (wildcard resource) when overriding+ // Content-Disposition. The RFC 5987 `filename*=UTF-8''...` syntax+ // contains `''`, which the canned-policy path can't sign correctly:+ // the signer leaves `'` literal (encodeURIComponent), but browsers+ // re-encode it to `%27` on send, so the URL no longer matches what+ // was signed and CloudFront returns AccessDenied. Signing the+ // Policy JSON instead of the URL bytes sidesteps the mismatch.+ distributionUrl.searchParams.set(+ "response-content-disposition",+ responseContentDisposition,+ );++ const resourceBase = `https://${config.distributionHost}${distributionUrl.pathname}`;+ const policy = JSON.stringify({+ Statement: [+ {+ Resource: `${resourceBase}?*`,+ Condition: {+ DateLessThan: {+ "AWS:EpochTime": Math.floor(+ (Date.now() + expiration) / ONE_SECOND,+ ),+ },+ },+ },+ ],+ });++ const url = getCloudfrontSignedUrl({url: distributionUrl.toString(),+ policy,keyPairId: `${config.distributionKeyId}`,privateKey: `${config.distributionKeyContents}`,- dateLessThan: new Date(Date.now() + expiration).toISOString(),});- let url = signed;- if (responseContentDisposition) {- const fixed = new URL(signed);- fixed.searchParams.set(- "response-content-disposition",- responseContentDisposition,- );- url = fixed.href;- }-return res.status(200).json({ url });}