Skip to content

Commit ce030fd

Browse files
authored
v2: Fix redirect to strip jwt_token in url-host mode (#3014)
1 parent 9ba8783 commit ce030fd

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

packages/gitbook-v2/src/middleware.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,12 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
141141
// Make sure the URL is clean of any va token after a successful lookup
142142
// The token is stored in a cookie that is set on the redirect response
143143
//
144-
const requestURLWithoutToken = normalizeVisitorAuthURL(
145-
mode === 'url' ? requestURL : siteURL
146-
);
147-
if (requestURLWithoutToken.toString() !== requestURL.toString()) {
144+
const incomingURL = mode === 'url' ? requestURL : siteURL;
145+
const requestURLWithoutToken = normalizeVisitorAuthURL(incomingURL);
146+
if (
147+
requestURLWithoutToken !== incomingURL &&
148+
requestURLWithoutToken.toString() !== incomingURL.toString()
149+
) {
148150
return writeResponseCookies(
149151
NextResponse.redirect(requestURLWithoutToken.toString()),
150152
cookies

packages/gitbook/src/lib/visitor-token.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,13 @@ export function getVisitorAuthCookieValue(basePath: string, token: string): stri
151151
* Normalize the URL by removing the visitor authentication token from the query parameters (if present).
152152
*/
153153
export function normalizeVisitorAuthURL(url: URL): URL {
154-
const withoutVAParam = new URL(url);
155-
withoutVAParam.searchParams.delete(VISITOR_AUTH_PARAM);
156-
return withoutVAParam;
154+
if (url.searchParams.has(VISITOR_AUTH_PARAM)) {
155+
const withoutVAParam = new URL(url);
156+
withoutVAParam.searchParams.delete(VISITOR_AUTH_PARAM);
157+
return withoutVAParam;
158+
}
159+
160+
return url;
157161
}
158162

159163
/**

0 commit comments

Comments
 (0)