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

Allow only valid values for theme query string #2745

Merged
merged 3 commits into from
Jan 14, 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
5 changes: 5 additions & 0 deletions .changeset/loud-files-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'gitbook': patch
---

Allow only good values for theme query parameter. Avoid having a 500 error when we pass an invalid value.
12 changes: 12 additions & 0 deletions packages/gitbook/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,18 @@ export async function getSiteData(
};
}

/**
* Validate that the customization settings passed are valid.
*/
export function validateSerializedCustomization(raw: string): boolean {
try {
rison.decode_object(raw);
return true;
} catch {
return false;
}
}

/**
* Get the customization settings for a space from the API.
*/
Expand Down
8 changes: 5 additions & 3 deletions packages/gitbook/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { ContentAPITokenPayload, GitBookAPI } from '@gitbook/api';
import { ContentAPITokenPayload, CustomizationThemeMode, GitBookAPI } from '@gitbook/api';
import { setTag, setContext } from '@sentry/nextjs';
import assertNever from 'assert-never';
import jwt from 'jsonwebtoken';
import type { ResponseCookie } from 'next/dist/compiled/@edge-runtime/cookies';
import { NextResponse, NextRequest } from 'next/server';
import hash from 'object-hash';
import rison from 'rison';

import {
PublishedContentWithCache,
Expand All @@ -17,6 +18,7 @@ import {
DEFAULT_API_ENDPOINT,
getPublishedContentSite,
getSiteData,
validateSerializedCustomization,
} from '@/lib/api';
import { race } from '@/lib/async';
import { buildVersion } from '@/lib/build';
Expand Down Expand Up @@ -259,12 +261,12 @@ export async function middleware(request: NextRequest) {
}

const customization = url.searchParams.get('customization');
if (customization) {
if (customization && validateSerializedCustomization(customization)) {
headers.set('x-gitbook-customization', customization);
gregberge marked this conversation as resolved.
Show resolved Hide resolved
}

const theme = url.searchParams.get('theme');
if (theme) {
if (theme === CustomizationThemeMode.Dark || theme === CustomizationThemeMode.Light) {
headers.set('x-gitbook-theme', theme);
}

Expand Down
Loading