Skip to content

Commit 7fe3d41

Browse files
authored
WEB-64 Validation of paths (#110)
2 parents b95c4cf + 024f6a1 commit 7fe3d41

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

src/lib/actions.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,45 @@ import type {
2323
} from "./types";
2424
import { wrapAction } from "./wrap-action";
2525

26+
const ROUTE_SEGMENT_RE = /^[a-z0-9_-]+$/;
27+
2628
function assertValidRoutePath(path: string): void {
29+
if (path.trim() !== path) {
30+
throw new Error("Path cannot have leading or trailing whitespace");
31+
}
32+
33+
if (path.length === 0) {
34+
throw new Error("Path cannot be empty");
35+
}
36+
2737
if (!path.startsWith("/")) {
2838
throw new Error("Path must start with /");
2939
}
40+
41+
if (path === "/") {
42+
return;
43+
}
44+
45+
if (path.endsWith("/")) {
46+
throw new Error("Path must not end with / (except root /)");
47+
}
48+
49+
if (path.includes("//")) {
50+
throw new Error("Path must not contain consecutive slashes");
51+
}
52+
53+
if (path !== path.toLowerCase()) {
54+
throw new Error("Path must be lowercase");
55+
}
56+
57+
const segments = path.split("/").slice(1);
58+
for (const segment of segments) {
59+
if (!ROUTE_SEGMENT_RE.test(segment)) {
60+
throw new Error(
61+
"Each path segment may only contain lowercase letters, numbers, hyphens, and underscores"
62+
);
63+
}
64+
}
3065
}
3166

3267
export async function createDocumentAction(

0 commit comments

Comments
 (0)