Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

Restrict some prefixes for page paths (#462)

Roman Pominov6542a20e47d5
diff --git a/apps/designer/.gitignore b/apps/designer/.gitignore
index 60d6321f0b..92b1c7ada8 100644
--- a/apps/designer/.gitignore
+++ b/apps/designer/.gitignore
@@ -7,4 +7,5 @@
/public/build
/api/index.js
/api/index.js.map
-/public/uploads
\ No newline at end of file
+/public/uploads
+/public/s/uploads
diff --git a/apps/designer/app/routes/rest/pages.$projectId.ts b/apps/designer/app/routes/rest/pages.$projectId.ts
index d5b054de1f..af8f10ab16 100644
--- a/apps/designer/app/routes/rest/pages.$projectId.ts
+++ b/apps/designer/app/routes/rest/pages.$projectId.ts
@@ -1,6 +1,6 @@
import { LoaderFunction, ActionFunction } from "@remix-run/node";
import { db } from "@webstudio-is/project/server";
-import { type Pages, utils } from "@webstudio-is/project";
+import { type Pages, utils, pathValidators } from "@webstudio-is/project";
import { zfd } from "zod-form-data";
import { z } from "zod";
import { sentryException } from "~/shared/sentry";
@@ -21,18 +21,13 @@ const nonEmptyString = z
const commonPageInput = {
name: nonEmptyString,
- path: nonEmptyString
- .refine((path) => path !== "/", "Can't be just a /")
- .transform((path) => (path.endsWith("/") ? path.slice(0, -1) : path))
- .transform((path) => path.replace(/\/\//g, "/"))
- .refine(
- (path) => path === "" || path.startsWith("/"),
- "Must start with a /"
- )
- .refine(
- (path) => /^[-_a-z0-9\\/]*$/.test(path),
- "Only a-z, 0-9, -, _ and / are allowed"
- ),
+ path: pathValidators(
+ nonEmptyString
+ .transform((path) =>
+ path.endsWith("/") && path !== "/" ? path.slice(0, -1) : path
+ )
+ .transform((path) => path.replace(/\/\//g, "/"))
+ ),
title: z.string().optional(),
description: z.string().optional(),
} as const;
diff --git a/packages/asset-uploader/src/constants.ts b/packages/asset-uploader/src/constants.ts
index 6228cc05c7..043f4686ad 100644
--- a/packages/asset-uploader/src/constants.ts
+++ b/packages/asset-uploader/src/constants.ts
@@ -1,3 +1,3 @@
-export const DEFAULT_UPLOAD_PATH = "public/uploads";
+export const DEFAULT_UPLOAD_PATH = "public/s/uploads";
// By default using Vercel's limit https://vercel.com/docs/concepts/limits/overview#serverless-function-payload-size-limit
export const MAX_UPLOAD_SIZE = "4.5";
diff --git a/packages/asset-uploader/src/utils/get-asset-path.test.ts b/packages/asset-uploader/src/utils/get-asset-path.test.ts
index 0e3563cd4c..b69c311de4 100644
--- a/packages/asset-uploader/src/utils/get-asset-path.test.ts
+++ b/packages/asset-uploader/src/utils/get-asset-path.test.ts
@@ -39,7 +39,7 @@ describe("getAssetPath", () => {
name: "test.png",
location: Location.FS,
})
- ).toBe("/uploads/test.png");
+ ).toBe("/s/uploads/test.png");
});
test("return local path with custom path", () => {
diff --git a/packages/project/src/db/types.ts b/packages/project/src/db/types.ts
index 2a6c35d324..aa79e10007 100644
--- a/packages/project/src/db/types.ts
+++ b/packages/project/src/db/types.ts
@@ -1,4 +1,4 @@
-import { z } from "zod";
+import { z, type ZodType } from "zod";
import {
Project as BaseProject,
Build as DbBuild,
@@ -24,12 +24,16 @@ const HomePage = z.object({
.refine((path) => path === "", "Home page path must be empty"),
});
-const Page = z.object({
- ...commonPageFields,
- path: z
- .string()
- .refine((path) => path !== "" && path !== "/", "Can't be empty")
- .refine((path) => path.startsWith("/"), "Must start with a /")
+export const pathValidators = (
+ baseValidator: ZodType<string>
+): ZodType<string> =>
+ baseValidator
+ .refine((path) => path !== "", "Can't be empty")
+ .refine((path) => path !== "/", "Can't be just a /")
+ .refine(
+ (path) => path === "" || path.startsWith("/"),
+ "Must start with a /"
+ )
.refine((path) => path.endsWith("/") === false, "Can't end with a /")
.refine(
(path) => path.includes("//") === false,
@@ -38,7 +42,22 @@ const Page = z.object({
.refine(
(path) => /^[-_a-z0-9\\/]*$/.test(path),
"Only a-z, 0-9, -, _ and / are allowed"
- ),
+ )
+ .refine(
+ // We use /s for our system stuff like /s/css or /s/uploads
+ (path) => path !== "/s" && path.startsWith("/s/") === false,
+ "/s prefix is reserved for the system"
+ )
+ .refine(
+ // Remix serves build artefacts like JS bundles from /build
+ // And we cannot customize it due to bug in Remix: https://github.com/remix-run/remix/issues/2933
+ (path) => path !== "/build" && path.startsWith("/build/") === false,
+ "/build prefix is reserved for the system"
+ );
+
+const Page = z.object({
+ ...commonPageFields,
+ path: pathValidators(z.string()),
});
export type Page = z.infer<typeof Page>;