Update site redirect regex for validation (#3216)

This commit is contained in:
Taran Vohra
2025-05-07 12:42:28 +05:30
committed by GitHub
parent 5e975ab95b
commit 3f292065cd
3 changed files with 17 additions and 2 deletions
+6
View File
@@ -0,0 +1,6 @@
---
"gitbook": patch
"gitbook-v2": patch
---
Update the regex for validating site redirect
@@ -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<string>([
// Test the pathname relative to the root
// For example hello/world -> section/variant/hello/world
+6 -1
View File
@@ -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
)
);
}
/**