Skip to content

Commit 3f29206

Browse files
authored
Update site redirect regex for validation (#3216)
1 parent 5e975ab commit 3f29206

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

.changeset/tidy-rings-talk.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"gitbook": patch
3+
"gitbook-v2": patch
4+
---
5+
6+
Update the regex for validating site redirect

packages/gitbook/src/components/SitePage/fetch.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ async function resolvePage(context: GitBookSiteContext, params: PagePathParams |
6969

7070
// If a page still can't be found, we try with the API, in case we have a redirect at site level.
7171
const redirectPathname = withLeadingSlash(rawPathname);
72-
if (/^\/[a-zA-Z0-9-_.\/]+[a-zA-Z0-9-_.]$/.test(redirectPathname)) {
72+
if (
73+
/^\/(?:[A-Za-z0-9\-._~]|%[0-9A-Fa-f]{2})+(?:\/(?:[A-Za-z0-9\-._~]|%[0-9A-Fa-f]{2})+)*$/.test(
74+
redirectPathname
75+
)
76+
) {
7377
const redirectSources = new Set<string>([
7478
// Test the pathname relative to the root
7579
// For example hello/world -> section/variant/hello/world

packages/gitbook/src/lib/api.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,12 @@ export const getComputedDocument = cache({
765765
* Mimic the validation done on source server-side to reduce API usage.
766766
*/
767767
function validateSiteRedirectSource(source: string) {
768-
return source.length <= 512 && /^\/[a-zA-Z0-9-_.\\/]+[a-zA-Z0-9-_.]$/.test(source);
768+
return (
769+
source.length <= 512 &&
770+
/^\/(?:[A-Za-z0-9\-._~]|%[0-9A-Fa-f]{2})+(?:\/(?:[A-Za-z0-9\-._~]|%[0-9A-Fa-f]{2})+)*$/.test(
771+
source
772+
)
773+
);
769774
}
770775

771776
/**

0 commit comments

Comments
 (0)