Skip to content
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