Skip to content

🎥 Make videos programmatically with React

validate site names

Jonny Burger6eb0dc187c58
diff --git a/packages/lambda/src/api/deploy-site.ts b/packages/lambda/src/api/deploy-site.ts
index 4683f91df4..58587d9c9a 100644
--- a/packages/lambda/src/api/deploy-site.ts
+++ b/packages/lambda/src/api/deploy-site.ts
@@ -7,6 +7,7 @@ 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 {validateSiteName} from '../shared/validate-site-name';
import {bucketExistsInRegion} from './bucket-exists';
import {enableS3Website} from './enable-s3-website';
import {uploadDir, UploadDirProgress} from './upload-dir';
@@ -58,6 +59,10 @@ export const deploySite = async ({
);
}
+ const siteId = siteName ?? randomHash();
+
+ validateSiteName(siteId);
+
const bucketExists = await bucketExistsInRegion({
bucketName,
region,
@@ -67,8 +72,6 @@ export const deploySite = async ({
throw new Error(`No bucket with the name ${bucketName} exists`);
}
- const siteId = siteName ?? randomHash();
-
const subFolder = getSitesKey(siteId);
await deleteSite({
bucketName,
diff --git a/packages/lambda/src/api/get-sites.ts b/packages/lambda/src/api/get-sites.ts
index 88dbf36af2..966a021b2c 100644
--- a/packages/lambda/src/api/get-sites.ts
+++ b/packages/lambda/src/api/get-sites.ts
@@ -39,7 +39,9 @@ export const getSites = async ({
});
for (const file of ls) {
- const siteKeyMatch = file.Key?.match(/sites\/([0-9a-zA-Z]+)\/(.*)$/);
+ const siteKeyMatch = file.Key?.match(
+ /sites\/([0-9a-zA-Z-!_.*'()]+)\/(.*)$/
+ );
if (!siteKeyMatch) {
throw new Error(
`A file was found in the bucket "${bucket.name}" with the key ${file.Key} which is an unexpected folder structure. Delete this file.`
diff --git a/packages/lambda/src/cli/commands/sites/create.ts b/packages/lambda/src/cli/commands/sites/create.ts
index 20f8614c5d..c5005544bc 100644
--- a/packages/lambda/src/cli/commands/sites/create.ts
+++ b/packages/lambda/src/cli/commands/sites/create.ts
@@ -4,6 +4,7 @@ import path from 'path';
import {deploySite} from '../../../api/deploy-site';
import {getOrCreateBucket} from '../../../api/get-or-create-bucket';
import {BINARY_NAME} from '../../../shared/constants';
+import {validateSiteName} from '../../../shared/validate-site-name';
import {parsedLambdaCli} from '../../args';
import {getAwsRegion} from '../../get-aws-region';
import {
@@ -46,6 +47,11 @@ export const sitesCreateSubcommand = async (args: string[]) => {
quit(1);
}
+ const desiredSiteName = parsedLambdaCli['site-name'] ?? undefined;
+ if (desiredSiteName !== undefined) {
+ validateSiteName(desiredSiteName);
+ }
+
const progressBar = CliInternals.createOverwriteableCliOutput(
CliInternals.quietFlagProvided()
);
@@ -100,8 +106,7 @@ export const sitesCreateSubcommand = async (args: string[]) => {
const {serveUrl, siteName} = await deploySite({
entryPoint: absoluteFile,
- // TODO: Make better
- siteName: parsedLambdaCli['site-name'] ?? undefined,
+ siteName: desiredSiteName,
bucketName,
options: {
onBundleProgress: (progress: number) => {
diff --git a/packages/lambda/src/shared/validate-site-name.ts b/packages/lambda/src/shared/validate-site-name.ts
new file mode 100644
index 0000000000..7b5d99db71
--- /dev/null
+++ b/packages/lambda/src/shared/validate-site-name.ts
@@ -0,0 +1,9 @@
+export const validateSiteName = (siteName: string) => {
+ if (!siteName.match(/([0-9a-zA-Z-!_.*'()]+)/g)) {
+ throw new Error(
+ "The `siteName` must match the RegExp `/([0-9a-zA-Z-!_.*'()]+)/g`. You passed: " +
+ siteName +
+ '. Check for invalid characters.'
+ );
+ }
+};