Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: percent-encode object keys in CloudFront signed URL policies

Richie McIlroyeb671a805b58
diff --git a/apps/web/__tests__/unit/cloudfront-signed-url.test.ts b/apps/web/__tests__/unit/cloudfront-signed-url.test.ts
new file mode 100644
index 000000000..1d6004954
--- /dev/null
+++ b/apps/web/__tests__/unit/cloudfront-signed-url.test.ts
@@ -0,0 +1,85 @@
+import { generateKeyPairSync } from "node:crypto";
+import { describe, expect, it } from "vitest";
+
+import {
+ createCloudFrontSignedUrl,
+ getCloudFrontObjectUrl,
+} from "../../../../packages/web-backend/src/S3Buckets/CloudFrontSignedUrl";
+
+const { privateKey } = generateKeyPairSync("rsa", {
+ modulusLength: 2048,
+ privateKeyEncoding: { type: "pkcs1", format: "pem" },
+ publicKeyEncoding: { type: "spki", format: "pem" },
+});
+
+const bucketUrl = "https://cdn.example.com";
+
+const sign = (key: string) =>
+ createCloudFrontSignedUrl({
+ bucketUrl,
+ key,
+ keyPairId: "K2JCJMDEHXQW5F",
+ privateKey,
+ expiresAt: 1_900_000_000,
+ });
+
+const decodePolicy = (signedUrl: string) => {
+ const encoded = new URL(signedUrl).searchParams.get("Policy") ?? "";
+ const base64 = encoded
+ .replace(/-/g, "+")
+ .replace(/_/g, "=")
+ .replace(/~/g, "/");
+ return JSON.parse(Buffer.from(base64, "base64").toString("utf8")) as {
+ Statement: { Resource: string }[];
+ };
+};
+
+const resourceOf = (signedUrl: string) =>
+ decodePolicy(signedUrl).Statement[0]?.Resource;
+
+const baseOf = (signedUrl: string) =>
+ signedUrl.slice(0, signedUrl.indexOf("?"));
+
+describe("createCloudFrontSignedUrl", () => {
+ it("signs ordinary keys for the exact object URL", () => {
+ const signedUrl = sign("user/video/result.mp4");
+
+ expect(resourceOf(signedUrl)).toBe(
+ "https://cdn.example.com/user/video/result.mp4",
+ );
+ expect(baseOf(signedUrl)).toBe(resourceOf(signedUrl));
+ });
+
+ it("does not leave CloudFront wildcards in the policy resource", () => {
+ for (const key of [
+ "user/video/*",
+ "user/video/?",
+ "user/video/*.mp4",
+ "user/video/a?b/*",
+ ]) {
+ const signedUrl = sign(key);
+ const resource = resourceOf(signedUrl) ?? "";
+
+ expect(resource).not.toMatch(/[*?]/);
+ expect(baseOf(signedUrl)).toBe(resource);
+ }
+ });
+
+ it("percent-encodes wildcard and reserved characters per path segment", () => {
+ expect(getCloudFrontObjectUrl(bucketUrl, "user/video/a*b?c d#e%f")).toBe(
+ "https://cdn.example.com/user/video/a%2Ab%3Fc%20d%23e%25f",
+ );
+ expect(getCloudFrontObjectUrl(bucketUrl, "user/video/(1)!'.mp4")).toBe(
+ "https://cdn.example.com/user/video/%281%29%21%27.mp4",
+ );
+ });
+
+ it("keeps the returned URL aligned with the signed resource for encoded keys", () => {
+ const signedUrl = sign("user/video/my clip (1).mp4");
+
+ expect(resourceOf(signedUrl)).toBe(
+ "https://cdn.example.com/user/video/my%20clip%20%281%29.mp4",
+ );
+ expect(baseOf(signedUrl)).toBe(resourceOf(signedUrl));
+ });
+});
diff --git a/packages/web-backend/src/S3Buckets/CloudFrontSignedUrl.ts b/packages/web-backend/src/S3Buckets/CloudFrontSignedUrl.ts
new file mode 100644
index 000000000..9085482db
--- /dev/null
+++ b/packages/web-backend/src/S3Buckets/CloudFrontSignedUrl.ts
@@ -0,0 +1,48 @@
+import * as CloudFrontPresigner from "@aws-sdk/cloudfront-signer";
+
+const encodeKeySegment = (segment: string) =>
+ encodeURIComponent(segment).replace(
+ /[!'()*]/g,
+ (char) => `%${char.charCodeAt(0).toString(16).toUpperCase()}`,
+ );
+
+export const getCloudFrontObjectUrl = (bucketUrl: string, key: string) =>
+ `${bucketUrl}/${key.split("/").map(encodeKeySegment).join("/")}`;
+
+export const createCloudFrontSignedUrl = ({
+ bucketUrl,
+ key,
+ keyPairId,
+ privateKey,
+ expiresAt,
+}: {
+ bucketUrl: string;
+ key: string;
+ keyPairId: string;
+ privateKey: string;
+ expiresAt: number;
+}) => {
+ const policy = {
+ Statement: [
+ {
+ Resource: getCloudFrontObjectUrl(bucketUrl, key),
+ Condition: {
+ DateLessThan: {
+ "AWS:EpochTime": expiresAt,
+ },
+ },
+ },
+ ],
+ };
+
+ // No `url` here on purpose: the signer re-encodes `url` with
+ // encodeURIComponent(decodeURIComponent(segment)), which turns %2A back into
+ // a literal `*`. Without `url` it uses the policy Resource verbatim, so the
+ // returned URL matches the signed Resource byte for byte, and CloudFront
+ // compares against the raw request URL.
+ return CloudFrontPresigner.getSignedUrl({
+ keyPairId,
+ privateKey,
+ policy: JSON.stringify(policy),
+ });
+};
diff --git a/packages/web-backend/src/S3Buckets/index.ts b/packages/web-backend/src/S3Buckets/index.ts
index 49ecb912e..3141206bc 100644
--- a/packages/web-backend/src/S3Buckets/index.ts
+++ b/packages/web-backend/src/S3Buckets/index.ts
@@ -1,5 +1,4 @@
import * as S3 from "@aws-sdk/client-s3";
-import * as CloudFrontPresigner from "@aws-sdk/cloudfront-signer";
import { decrypt } from "@cap/database/crypto";
import type { Organisation, S3Bucket, User } from "@cap/web-domain";
import type { RequestPresigningArguments } from "@smithy/types";
@@ -7,6 +6,7 @@ import { Config, Effect, Layer, Option } from "effect";
import { AwsCredentials } from "../Aws.ts";
import { Database } from "../Database.ts";
+import { createCloudFrontSignedUrl } from "./CloudFrontSignedUrl.ts";
import { createS3BucketAccess } from "./S3BucketAccess.ts";
import { S3BucketClientProvider } from "./S3BucketClientProvider.ts";
import { S3BucketsRepo } from "./S3BucketsRepo.ts";
@@ -107,29 +107,15 @@ export class S3Buckets extends Effect.Service<S3Buckets>()("S3Buckets", {
key: string,
signingArgs?: RequestPresigningArguments,
) => {
- const url = `${cloudfrontEnvs.bucketUrl}/${key}`;
const expiresIn = signingArgs?.expiresIn ?? 3600;
- const expires = Math.floor((Date.now() + expiresIn * 1000) / 1000);
-
- const policy = {
- Statement: [
- {
- Resource: url,
- Condition: {
- DateLessThan: {
- "AWS:EpochTime": Math.floor(expires),
- },
- },
- },
- ],
- };
return Effect.succeed(
- CloudFrontPresigner.getSignedUrl({
- url,
+ createCloudFrontSignedUrl({
+ bucketUrl: cloudfrontEnvs.bucketUrl,
+ key,
keyPairId: cloudfrontEnvs.keypairId,
privateKey: cloudfrontEnvs.privateKey,
- policy: JSON.stringify(policy),
+ expiresAt: Math.floor((Date.now() + expiresIn * 1000) / 1000),
}),
);
};