From 7d835bdc499bfe573bd70532bd617f9c43009e16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Smoli=C5=84ski?= Date: Thu, 22 Oct 2026 17:35:08 +0200 Subject: [PATCH] Locale cookie honours consent saved under a bumped policy version The proxy parsed the consent cookie as version 1, so after an editor bumped the policy version NEXT_LOCALE was never saved. It now accepts the stored record's own version; policyVersion pins one. Co-Authored-By: Claude Opus 5.5 --- src/modules/i18n/createProxy.ts | 2 ++ src/modules/i18n/localeMiddleware.ts | 22 +++++++++++++++++- test/localeConsentVersion.test.ts | 34 ++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 test/localeConsentVersion.test.ts diff --git a/src/modules/i18n/createProxy.ts b/src/modules/i18n/createProxy.ts index a689744..c987357 100644 --- a/src/modules/i18n/createProxy.ts +++ b/src/modules/i18n/createProxy.ts @@ -49,6 +49,8 @@ type CreateProxyOptions = { * requested with `Accept: text/markdown` is rewritten to its Markdown version. */ markdown?: boolean | string + /** Consent policy version to require for saving the locale cookie (createLocaleMiddleware). */ + policyVersion?: number } /** diff --git a/src/modules/i18n/localeMiddleware.ts b/src/modules/i18n/localeMiddleware.ts index 05d58b7..94b4627 100644 --- a/src/modules/i18n/localeMiddleware.ts +++ b/src/modules/i18n/localeMiddleware.ts @@ -37,6 +37,13 @@ type CreateLocaleMiddlewareArgs = { consentCookieName?: string /** Cookie name for the locale choice. Defaults to LOCALE_COOKIE_NAME. */ cookieName?: string + /** + * Cookie Settings → Policy version to require. The proxy can't read the + * panel, so by default a stored consent counts under its OWN version — + * reading it as version 1 meant that after an editor bumped the policy the + * locale cookie was never saved again. Pin it to require that version. + */ + policyVersion?: number } /** @@ -74,17 +81,30 @@ function firstSegment(pathname: string): string { * return res * } */ +/** The policy version a consent cookie was saved under (undefined when unreadable). */ +function storedVersion(raw: string | undefined): number | undefined { + if (!raw) {return undefined} + try { + const version = (JSON.parse(decodeURIComponent(raw)) as { version?: unknown }).version + return typeof version === 'number' ? version : undefined + } catch { + return undefined + } +} + export function createLocaleMiddleware({ config, consentCategory = 'functional', consentCookieName = CONSENT_COOKIE, cookieName = LOCALE_COOKIE_NAME, + policyVersion, }: CreateLocaleMiddlewareArgs) { // Whether the locale cookie may be written: 'necessary' is always granted; // 'functional' (default) requires the visitor to have consented. function mayPersistLocale(request: MiddlewareRequest): boolean { if (consentCategory === 'necessary') {return true} - const consent = parseConsent(request.cookies.get(consentCookieName)?.value) + const raw = request.cookies.get(consentCookieName)?.value + const consent = parseConsent(raw, policyVersion ?? storedVersion(raw)) return consent?.[consentCategory] === true } diff --git a/test/localeConsentVersion.test.ts b/test/localeConsentVersion.test.ts new file mode 100644 index 0000000..1afa8ea --- /dev/null +++ b/test/localeConsentVersion.test.ts @@ -0,0 +1,34 @@ +import { describe, expect, test } from 'vitest' + +import { CONSENT_COOKIE, serializeConsent } from '../src/modules/consent/storage.js' +import { createLocaleMiddleware } from '../src/modules/i18n/localeMiddleware.js' +import { LOCALE_COOKIE_NAME } from '../src/modules/i18n/negotiateLocale.js' + +const config = { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'PL' }, { code: 'en', label: 'EN' }] } as const +const state = { analytics: false, functional: true, marketing: false, necessary: true } + +function request(path: string, cookies: Record) { + const url = new URL(`https://acme.pl${path}`) + return { + cookies: { get: (name: string) => (name in cookies ? { value: cookies[name] } : undefined) }, + headers: new Headers(), + nextUrl: { clone: () => new URL(url), pathname: url.pathname, search: url.search }, + url: url.toString(), + } +} + +describe('locale cookie and the consent policy version', () => { + const switchToEn = (version: number, policyVersion?: number) => + createLocaleMiddleware({ config, ...(policyVersion ? { policyVersion } : {}) })( + request('/en/o-nas', { [CONSENT_COOKIE]: decodeURIComponent(serializeConsent(state, version)), [LOCALE_COOKIE_NAME]: 'pl' }) as never, + ) + + test('a consent saved under a bumped policy version still lets the locale be saved', () => { + expect(switchToEn(3).cookie).toMatchObject({ name: LOCALE_COOKIE_NAME, value: 'en' }) + }) + + test('policyVersion pins the version the proxy requires', () => { + expect(switchToEn(3, 4).cookie).toBeUndefined() + expect(switchToEn(4, 4).cookie).toMatchObject({ value: 'en' }) + }) +})