diff --git a/.changeset/tidy-rings-talk.md b/.changeset/tidy-rings-talk.md new file mode 100644 index 000000000..75ac0e7f8 --- /dev/null +++ b/.changeset/tidy-rings-talk.md @@ -0,0 +1,6 @@ +--- +"gitbook": patch +"gitbook-v2": patch +--- + +Update the regex for validating site redirect diff --git a/packages/gitbook/src/components/SitePage/fetch.ts b/packages/gitbook/src/components/SitePage/fetch.ts index 6ca4e5529..732c36072 100644 --- a/packages/gitbook/src/components/SitePage/fetch.ts +++ b/packages/gitbook/src/components/SitePage/fetch.ts @@ -69,7 +69,11 @@ async function resolvePage(context: GitBookSiteContext, params: PagePathParams | // If a page still can't be found, we try with the API, in case we have a redirect at site level. const redirectPathname = withLeadingSlash(rawPathname); - if (/^\/[a-zA-Z0-9-_.\/]+[a-zA-Z0-9-_.]$/.test(redirectPathname)) { + if ( + /^\/(?:[A-Za-z0-9\-._~]|%[0-9A-Fa-f]{2})+(?:\/(?:[A-Za-z0-9\-._~]|%[0-9A-Fa-f]{2})+)*$/.test( + redirectPathname + ) + ) { const redirectSources = new Set([ // Test the pathname relative to the root // For example hello/world -> section/variant/hello/world diff --git a/packages/gitbook/src/lib/api.ts b/packages/gitbook/src/lib/api.ts index ca3ce3b1f..310074450 100644 --- a/packages/gitbook/src/lib/api.ts +++ b/packages/gitbook/src/lib/api.ts @@ -765,7 +765,12 @@ export const getComputedDocument = cache({ * Mimic the validation done on source server-side to reduce API usage. */ function validateSiteRedirectSource(source: string) { - return source.length <= 512 && /^\/[a-zA-Z0-9-_.\\/]+[a-zA-Z0-9-_.]$/.test(source); + return ( + source.length <= 512 && + /^\/(?:[A-Za-z0-9\-._~]|%[0-9A-Fa-f]{2})+(?:\/(?:[A-Za-z0-9\-._~]|%[0-9A-Fa-f]{2})+)*$/.test( + source + ) + ); } /**