Skip to content

Commit e931e6e

Browse files
committed
fix: relax redirects validation
Fixes #5337 1. checking specific characters will always be error prone 2. checking matching paths is no longer relevant with `/*` which matches them all 3. added check to validate url or path with `new URL()`
1 parent ee37447 commit e931e6e

File tree

2 files changed

+11
-40
lines changed

2 files changed

+11
-40
lines changed

apps/builder/app/builder/features/project-settings/section-redirects.tsx

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import {
2222
} from "@webstudio-is/sdk";
2323
import { useRef, useState, type ChangeEvent } from "react";
2424
import { flushSync } from "react-dom";
25-
import { matchPathnamePattern } from "~/builder/shared/url-pattern";
2625
import { $pages, $publishedOrigin } from "~/shared/nano-states";
2726
import { serverSyncStore } from "~/shared/sync";
2827
import { getExistingRoutePaths, sectionSpacing } from "./utils";
@@ -82,32 +81,9 @@ export const SectionRedirects = () => {
8281

8382
const validateNewPath = (newPath: string): string[] => {
8483
const newPathValidationResult = ProjectNewRedirectPath.safeParse(newPath);
85-
86-
if (newPathValidationResult.success === true) {
87-
/*
88-
This is the new path, that users want to redirect to.
89-
If the new path doesn't exist, it's not a valid redirect.
90-
*/
91-
92-
if (newPath === "/") {
93-
return [];
94-
}
95-
96-
if (newPath.startsWith("/") === true) {
97-
let matched = false;
98-
for (const pattern of existingPaths) {
99-
if (matchPathnamePattern(pattern, newPath)) {
100-
matched = true;
101-
}
102-
}
103-
if (matched === false) {
104-
return ["This path doesn't exist in the project"];
105-
}
106-
}
107-
84+
if (newPathValidationResult.success) {
10885
return [];
10986
}
110-
11187
return newPathValidationResult.error.format()._errors;
11288
};
11389

packages/sdk/src/schema/pages.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -152,21 +152,16 @@ const ProjectMeta = z.object({
152152
});
153153
export type ProjectMeta = z.infer<typeof ProjectMeta>;
154154

155-
export const ProjectNewRedirectPath = PagePath.or(
156-
z.string().refine((data) => {
157-
// Users should be able to redirect from any old-path to the home page in the new project.
158-
if (data === "/") {
159-
return true;
160-
}
161-
162-
try {
163-
new URL(data);
164-
return true;
165-
} catch {
166-
return false;
167-
}
168-
}, "Must be a valid URL")
169-
);
155+
export const ProjectNewRedirectPath = z.string().refine((data) => {
156+
// Users should be able to redirect from any old-path to the home page in the new project.
157+
try {
158+
// can be relative and absolute paths
159+
new URL(data, "http://url.com");
160+
return true;
161+
} catch {
162+
return false;
163+
}
164+
}, "Must be a valid URL");
170165

171166
export const PageRedirect = z.object({
172167
old: OldPagePath,

0 commit comments

Comments
 (0)