Skip to content

Commit db74ea3

Browse files
authored
Image optimization endpoint redirects to underlying image URL if the signature is not the latest. (#2665)
1 parent 99579ac commit db74ea3

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

.changeset/tender-bags-guess.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'gitbook': minor
3+
---
4+
5+
Image optimization endpoint redirects to underlying image URL if the signature is not the latest.

packages/gitbook/src/app/(global)/~gitbook/image/route.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { NextRequest } from 'next/server';
22

3-
import { isSignatureVersion, SignatureVersion, verifyImageSignature } from '@/lib/image-signatures';
3+
import {
4+
CURRENT_SIGNATURE_VERSION,
5+
isSignatureVersion,
6+
SignatureVersion,
7+
verifyImageSignature,
8+
} from '@/lib/image-signatures';
49
import { resizeImage, CloudflareImageOptions, checkIsSizableImageURL } from '@/lib/images';
510
import { parseImageAPIURL } from '@/lib/urls';
611

@@ -39,6 +44,10 @@ export async function GET(request: NextRequest) {
3944
return new Response(`Invalid signature "${signature ?? ''}" for "${url}"`, { status: 400 });
4045
}
4146

47+
if (signatureVersion !== CURRENT_SIGNATURE_VERSION) {
48+
return Response.redirect(url, 302);
49+
}
50+
4251
// Cloudflare-specific options are in the cf object.
4352
const options: CloudflareImageOptions = {
4453
fit: 'scale-down',

packages/gitbook/src/lib/image-signatures.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ import { host } from './links';
1111
*/
1212
export type SignatureVersion = '0' | '1' | '2';
1313

14+
/**
15+
* The current version of the signature.
16+
*/
17+
export const CURRENT_SIGNATURE_VERSION: SignatureVersion = '2';
18+
1419
/**
1520
* A mapping of signature versions to signature functions.
1621
*/
@@ -48,7 +53,7 @@ export function generateImageSignature(input: string): {
4853
version: SignatureVersion;
4954
} {
5055
const result = generateSignatureV2(input);
51-
return { signature: result, version: '2' };
56+
return { signature: result, version: CURRENT_SIGNATURE_VERSION };
5257
}
5358

5459
// Reused buffer for FNV-1a hashing in the v2 algorithm

0 commit comments

Comments
 (0)