🎥 Make videos programmatically with React
finalize presign URL
Jonny Burger3bbe0c34da90
diff --git a/packages/docs/docs/lambda/presignurl.md b/packages/docs/docs/lambda/presignurl.mdindex 455a481b4e..b0d1ffb0ca 100644--- a/packages/docs/docs/lambda/presignurl.md+++ b/packages/docs/docs/lambda/presignurl.md@@ -12,10 +12,11 @@ Returns a presigned url for public access of an object stored in Remotion's S3 bimport { presignUrl } from "@remotion/lambda";const url = await presignUrl({- region: "us-east-1",- bucketName: "remotionlambda-c7fsl3d",- objectKey: "assets/sample.png",- checkIfObjectExists: true,+ region: "us-east-1",+ bucketName: "remotionlambda-c7fsl3d",+ objectKey: "assets/sample.png",+ checkIfObjectExists: true,+ expiresInSeconds: 900,});console.log(url); // `null` if object doesn't exist@@ -41,9 +42,10 @@ They key that uniquely identifies the object stored in the bucket.Whether the function should check if the object exists in the bucket before generating the presigned url. Default false.-### `expiresIn`+### `expiresInSeconds`-The number of seconds before the presigned URL expires. Default 120.+The number of seconds before the presigned URL expires.+Must be an integer and `>=1` and `<=604800` (maximum of 7 days)## See alsodiff --git a/packages/lambda/src/api/deploy-site.ts b/packages/lambda/src/api/deploy-site.tsindex ae55c5a78b..22392310ea 100644--- a/packages/lambda/src/api/deploy-site.ts+++ b/packages/lambda/src/api/deploy-site.ts@@ -2,11 +2,12 @@ import {Internals, WebpackOverrideFn} from 'remotion';import {deleteSite} from '../api/delete-site';import {AwsRegion} from '../pricing/aws-regions';import {bundleSite} from '../shared/bundle-site';-import {getSitesKey, REMOTION_BUCKET_PREFIX} from '../shared/constants';+import {getSitesKey} from '../shared/constants';import {getAccountId} from '../shared/get-account-id';import {makeS3ServeUrl} from '../shared/make-s3-url';import {randomHash} from '../shared/random-hash';import {validateAwsRegion} from '../shared/validate-aws-region';+import {validateBucketName} from '../shared/validate-bucketname';import {validateSiteName} from '../shared/validate-site-name';import {bucketExistsInRegion} from './bucket-exists';import {enableS3Website} from './enable-s3-website';@@ -47,20 +48,9 @@ export const deploySite = async ({region,}: DeploySiteInput): DeploySiteOutput => {validateAwsRegion(region);- if (!bucketName.startsWith(REMOTION_BUCKET_PREFIX)) {- throw new Error(- `The bucketName parameter must start with ${REMOTION_BUCKET_PREFIX}.`- );- }-- if (typeof siteName !== 'string' && typeof siteName !== 'undefined') {- throw new TypeError(- 'The `projectName` argument must be a string if provided.'- );- }+ validateBucketName(bucketName);const siteId = siteName ?? randomHash();-validateSiteName(siteId);const bucketExists = await bucketExistsInRegion({diff --git a/packages/lambda/src/api/get-s3-object.ts b/packages/lambda/src/api/get-s3-object.tsindex 1f06eae1e8..237dee737e 100644--- a/packages/lambda/src/api/get-s3-object.ts+++ b/packages/lambda/src/api/get-s3-object.ts@@ -2,7 +2,7 @@ import {GetObjectCommand} from '@aws-sdk/client-s3';import {Readable} from 'stream';import {AwsRegion} from '../pricing/aws-regions';import {getS3Client} from '../shared/aws-clients';-import {REMOTION_BUCKET_PREFIX} from '../shared/constants';+import {validateBucketName} from '../shared/validate-bucketname';export type GetS3ObjectInput = {region: AwsRegion;@@ -23,11 +23,7 @@ export const getS3Object = async ({bucketName,objectKey,}: GetS3ObjectInput): Promise<Readable> => {- if (!bucketName.startsWith(REMOTION_BUCKET_PREFIX)) {- throw new Error(- `The bucketName parameter must start with ${REMOTION_BUCKET_PREFIX}.`- );- }+ validateBucketName(bucketName);const s3Client = getS3Client(region);diff --git a/packages/lambda/src/api/presign-url.ts b/packages/lambda/src/api/presign-url.tsindex 77b163fb9d..756103cf2e 100644--- a/packages/lambda/src/api/presign-url.ts+++ b/packages/lambda/src/api/presign-url.ts@@ -2,14 +2,15 @@ import {GetObjectCommand, HeadObjectCommand} from '@aws-sdk/client-s3';import {getSignedUrl} from '@aws-sdk/s3-request-presigner';import {AwsRegion} from '../pricing/aws-regions';import {getS3Client} from '../shared/aws-clients';-import {REMOTION_BUCKET_PREFIX} from '../shared/constants';+import {validateBucketName} from '../shared/validate-bucketname';+import {validatePresignExpiration} from '../shared/validate-presign-expiration';export type PresignURLInput = {region: AwsRegion;bucketName: string;objectKey: string;checkIfObjectExists?: boolean;- expiresIn?: number;+ expiresInSeconds: number;};/**@@ -27,24 +28,21 @@ export const presignUrl = async ({bucketName,objectKey,checkIfObjectExists = false,- expiresIn = 120,+ expiresInSeconds,}: PresignURLInput): Promise<string | null> => {- if (!bucketName.startsWith(REMOTION_BUCKET_PREFIX)) {- throw new Error(- `The bucketName parameter must start with ${REMOTION_BUCKET_PREFIX}.`- );- }+ validateBucketName(bucketName);+ validatePresignExpiration(expiresInSeconds);const s3Client = getS3Client(region);- const params = {- Bucket: bucketName,- Key: objectKey,- };-if (checkIfObjectExists) {try {- await s3Client.send(new HeadObjectCommand(params));+ await s3Client.send(+ new HeadObjectCommand({+ Bucket: bucketName,+ Key: objectKey,+ })+ );} catch (err) {if ((err as {name: string}).name === 'NotFound') {return null;@@ -54,10 +52,13 @@ export const presignUrl = async ({}}- const objCommand = new GetObjectCommand(params);+ const objCommand = new GetObjectCommand({+ Bucket: bucketName,+ Key: objectKey,+ });const publicUrl = await getSignedUrl(s3Client, objCommand, {- expiresIn,+ expiresIn: expiresInSeconds,});return publicUrl;diff --git a/packages/lambda/src/shared/validate-bucketname.ts b/packages/lambda/src/shared/validate-bucketname.tsnew file mode 100644index 0000000000..a9bc0041be--- /dev/null+++ b/packages/lambda/src/shared/validate-bucketname.ts@@ -0,0 +1,15 @@+import {REMOTION_BUCKET_PREFIX} from './constants';++export const validateBucketName = (bucketName: unknown) => {+ if (typeof bucketName !== 'string') {+ throw new TypeError(+ `'bucketName' must be a string, but is ${JSON.stringify(bucketName)}`+ );+ }++ if (!bucketName.startsWith(REMOTION_BUCKET_PREFIX)) {+ throw new Error(+ `The bucketName parameter must start with ${REMOTION_BUCKET_PREFIX}.`+ );+ }+};diff --git a/packages/lambda/src/shared/validate-presign-expiration.ts b/packages/lambda/src/shared/validate-presign-expiration.tsnew file mode 100644index 0000000000..caabaef6c3--- /dev/null+++ b/packages/lambda/src/shared/validate-presign-expiration.ts@@ -0,0 +1,46 @@+export const MAX_PRESIGN_EXPIRATION = 604800;+export const MIN_PRESIGN_EXPIRATION = 1;++export const validatePresignExpiration = (presignExpiration: unknown) => {+ if (typeof presignExpiration === 'undefined' || presignExpiration === null) {+ return;+ }++ if (typeof presignExpiration !== 'number') {+ throw new TypeError(+ `'expiresIn' should be a number, but is ${JSON.stringify(+ presignExpiration+ )}`+ );+ }++ if (Number.isNaN(presignExpiration)) {+ throw new TypeError(`'expiresIn' should not be NaN, but is NaN`);+ }++ if (!Number.isFinite(presignExpiration)) {+ throw new TypeError(+ `'expiresIn' should be finite but is ${presignExpiration}`+ );+ }++ if (presignExpiration % 1 !== 0) {+ throw new TypeError(+ `'expiresIn' should be an integer but is ${JSON.stringify(+ presignExpiration+ )}`+ );+ }++ if (presignExpiration > MAX_PRESIGN_EXPIRATION) {+ throw new TypeError(+ `The 'expiresIn' parameter must be less or equal than ${MAX_PRESIGN_EXPIRATION} (7 days) as enforced by AWS`+ );+ }++ if (presignExpiration < MIN_PRESIGN_EXPIRATION) {+ throw new TypeError(+ `The 'expiresIn' parameter must be greater or equal than ${MIN_PRESIGN_EXPIRATION}`+ );+ }+};diff --git a/packages/lambda/src/shared/validate-site-name.ts b/packages/lambda/src/shared/validate-site-name.tsindex 7b5d99db71..4d4c1406e8 100644--- a/packages/lambda/src/shared/validate-site-name.ts+++ b/packages/lambda/src/shared/validate-site-name.ts@@ -1,4 +1,16 @@-export const validateSiteName = (siteName: string) => {+export const validateSiteName = (siteName: unknown) => {+ if (typeof siteName === 'undefined') {+ return;+ }++ if (typeof siteName !== 'string') {+ throw new TypeError(+ `The 'projectName' argument must be a string if provided, but is ${JSON.stringify(+ siteName+ )}`+ );+ }+if (!siteName.match(/([0-9a-zA-Z-!_.*'()]+)/g)) {throw new Error("The `siteName` must match the RegExp `/([0-9a-zA-Z-!_.*'()]+)/g`. You passed: " +