Payload's colour-scheme client hints go to the admin only: withSecurityConfig outside withPayload
withPayload adds Accept-CH, Critical-CH and Vary: Sec-CH-Prefers-Color-Scheme for /:path*; Critical-CH makes
Chrome repeat the first document request of a visit to a public page. As the outer wrapper, withSecurityConfig
moves the three headers to /admin/:path* (adminClientHints: { route, sources } | false). Inside withPayload
nothing changes, so an unedited site behaves as before. Exports scopeColorSchemeClientHints,
adminClientHintSources, COLOR_SCHEME_CLIENT_HINT; doctor rule client-hints (info); reference site and starter
use the new order; e2e:built and the starter gate check it.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
2f6a81acbf
commit
9143c132a3
@@ -1,5 +1,6 @@
|
||||
import { expect, type Page, test } from '@playwright/test'
|
||||
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs'
|
||||
import { createServer, request as httpRequest } from 'node:http'
|
||||
import path from 'node:path'
|
||||
|
||||
import { attachmentOf, startSmtpSink } from './smtp-sink'
|
||||
@@ -203,6 +204,76 @@ test('security headers: CSP enforced, no X-Powered-By', async ({ request }) => {
|
||||
expect(headers['x-powered-by']).toBeUndefined()
|
||||
})
|
||||
|
||||
test.describe('client hints: Payload\'s colour-scheme headers go to the admin only', () => {
|
||||
const HINT = /sec-ch-prefers-color-scheme/i
|
||||
|
||||
test('no public page, route handler or static file carries them; the admin does', async ({ request }) => {
|
||||
const home = await request.get('/pl')
|
||||
const asset = (await home.text()).match(/\/_next\/static\/[^"'\\ ]+\.(?:js|css)/)?.[0]
|
||||
expect(asset, 'a static file linked from /pl').toBeTruthy()
|
||||
// FAILS when withPayload's site-wide rule is back (withSecurityConfig inside withPayload, or the option off)
|
||||
for (const path of ['/pl', '/en', '/pl/kontakt', '/en/contact', '/pl/blog', '/api/health', '/robots.txt', '/sitemap.xml', '/llms.txt', asset!]) {
|
||||
const res = await request.get(path)
|
||||
const headers = res.headers()
|
||||
expect(res.status(), path).toBe(200)
|
||||
expect(headers['accept-ch'], `${path}: Accept-CH`).toBeUndefined()
|
||||
expect(headers['critical-ch'], `${path}: Critical-CH`).toBeUndefined()
|
||||
expect(headers.vary ?? '', `${path}: Vary`).not.toMatch(HINT)
|
||||
}
|
||||
for (const path of ['/admin', '/admin/login']) {
|
||||
const headers = (await request.get(path, { maxRedirects: 0 })).headers()
|
||||
expect(headers['accept-ch'], `${path}: Accept-CH`).toMatch(HINT)
|
||||
expect(headers['critical-ch'], `${path}: Critical-CH`).toMatch(HINT)
|
||||
}
|
||||
})
|
||||
|
||||
test('Chrome asks for a public page once; the admin still gets the hint and renders the browser\'s scheme', async ({ baseURL, browser, request }) => {
|
||||
// A counting proxy in front of the site: what the SERVER receives. Chrome repeats a document request inside
|
||||
// its network stack when the answer names a critical hint it did not send; the page never sees that.
|
||||
const documents: { hint?: string; url: string }[] = []
|
||||
const site = new URL(baseURL!)
|
||||
const proxy = createServer((req, res) => {
|
||||
if (req.headers['sec-fetch-dest'] === 'document') {documents.push({ hint: req.headers['sec-ch-prefers-color-scheme'] as string | undefined, url: req.url! })}
|
||||
const upstream = httpRequest({ headers: req.headers, host: site.hostname, method: req.method, path: req.url, port: site.port }, (r) => {
|
||||
res.writeHead(r.statusCode!, r.headers)
|
||||
r.pipe(res)
|
||||
})
|
||||
upstream.on('error', () => res.destroy())
|
||||
req.pipe(upstream)
|
||||
})
|
||||
await new Promise<void>((resolve) => proxy.listen(0, '127.0.0.1', resolve))
|
||||
const origin = `http://localhost:${(proxy.address() as { port: number }).port}`
|
||||
try {
|
||||
for (const path of ['/pl', '/en/contact']) {
|
||||
const context = await browser.newContext() // a fresh profile: a first visit
|
||||
documents.length = 0
|
||||
await (await context.newPage()).goto(origin + path)
|
||||
expect(documents, `${path}: one document request, no hint asked for`).toEqual([{ hint: undefined, url: path }])
|
||||
await context.close()
|
||||
}
|
||||
// The panel: its first request teaches Chrome the hint, the repeated one carries it — as before the change.
|
||||
// (Chrome's network stack sends the system's scheme; Playwright's colorScheme emulation does not reach it.)
|
||||
const context = await browser.newContext()
|
||||
const page = await context.newPage()
|
||||
documents.length = 0
|
||||
const response = await page.goto(`${origin}/admin/login`)
|
||||
expect(documents).toEqual([{ hint: undefined, url: '/admin/login' }, { hint: expect.stringMatching(/^(dark|light)$/), url: '/admin/login' }])
|
||||
const sent = documents[1]!.hint!
|
||||
// the theme is in the server's HTML: the first paint is already right (no flash)
|
||||
expect(await response!.text()).toMatch(new RegExp(`<html[^>]*data-theme="${sent}"`))
|
||||
await expect(page.locator('html')).toHaveAttribute('data-theme', sent)
|
||||
await context.close()
|
||||
// and the server renders whichever scheme the hint names
|
||||
for (const scheme of ['dark', 'light']) {
|
||||
const html = await (await request.get('/admin/login', { headers: { 'Sec-CH-Prefers-Color-Scheme': scheme } })).text()
|
||||
expect(html, scheme).toMatch(new RegExp(`<html[^>]*data-theme="${scheme}"`))
|
||||
}
|
||||
} finally {
|
||||
proxy.close()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
test('a missing page is a 404 with the panel 404 content', async ({ page }) => {
|
||||
const res = await page.goto('/pl/nie-ma-takiej-strony')
|
||||
expect(res?.status()).toBe(404)
|
||||
|
||||
Reference in New Issue
Block a user