Skip to content

Commit 2d08a6e

Browse files
authored
Return not found when decodeURIComponent from params fail (#3863)
1 parent d539409 commit 2d08a6e

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

packages/gitbook/src/app/utils.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { type SiteURLData, fetchSiteContextByURLLookup, getBaseContext } from '@
33
import { getDynamicCustomizationSettings } from '@/lib/customization';
44
import type { SiteAPIToken } from '@gitbook/api';
55
import { jwtDecode } from 'jwt-decode';
6-
import { forbidden } from 'next/navigation';
6+
import { forbidden, notFound } from 'next/navigation';
77
import rison from 'rison';
88

99
export type RouteParamMode = 'url-host' | 'url';
@@ -80,14 +80,27 @@ export async function getDynamicSiteContext(params: RouteLayoutParams) {
8080
* Get the decoded page path from the params.
8181
*/
8282
export function getPagePathFromParams(params: RouteParams) {
83-
const decoded = decodeURIComponent(params.pagePath);
84-
return decoded;
83+
// If decoding the param fails, return a 404 instead of crashing
84+
try {
85+
const decoded = decodeURIComponent(params.pagePath);
86+
return decoded;
87+
} catch (error) {
88+
console.error(
89+
`Returning 404 after failing to decode page path ${params.pagePath}: ${error}`
90+
);
91+
notFound();
92+
}
8593
}
8694

8795
function getSiteURLFromParams(params: RouteLayoutParams) {
88-
const decoded = decodeURIComponent(params.siteURL);
89-
const url = new URL(`https://${decoded}`);
90-
return url;
96+
try {
97+
const decoded = decodeURIComponent(params.siteURL);
98+
const url = new URL(`https://${decoded}`);
99+
return url;
100+
} catch (error) {
101+
console.error(`Returning 404 after failing to decode site URL ${params.siteURL}: ${error}`);
102+
notFound();
103+
}
91104
}
92105

93106
function getModeFromParams(mode: string): RouteParamMode {
@@ -102,6 +115,13 @@ function getModeFromParams(mode: string): RouteParamMode {
102115
* Get the decoded site data from the params.
103116
*/
104117
function getSiteURLDataFromParams(params: RouteLayoutParams): SiteURLData {
105-
const decoded = decodeURIComponent(params.siteData);
106-
return rison.decode(decoded);
118+
try {
119+
const decoded = decodeURIComponent(params.siteData);
120+
return rison.decode(decoded);
121+
} catch (error) {
122+
console.error(
123+
`Returning 404 after failing to decode site data ${params.siteData}: ${error}`
124+
);
125+
notFound();
126+
}
107127
}

0 commit comments

Comments
 (0)