Skip to content

Update site redirect regex for validation #3216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/tidy-rings-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"gitbook": patch
"gitbook-v2": patch
---

Update the regex for validating site redirect
6 changes: 5 additions & 1 deletion packages/gitbook/src/components/SitePage/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion packages/gitbook/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
);
}

/**
Expand Down