From 9a648b049824eff116353f38f6a4e3c78f5f79b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Smoli=C5=84ski?= Date: Wed, 25 Nov 2026 01:04:14 +0100 Subject: [PATCH] The plugin's version is a constant: no package.json among a site's public assets pluginVersion() named package.json relative to its own module. A bundler takes such a file for an asset: a Turbopack build of a site copied the plugin's whole package.json (dependency lists, scripts, the registry and repository addresses; no secret) to /_next/static/media/package..json, served to anyone. Webpack kept it on the server side. The version now comes from src/version.ts (scripts/write-version.mjs, run by the `version` script; a test fails when it differs from package.json). A smoke test of the built site: nothing under .next/static is a package manifest. Co-Authored-By: Claude Fable 5.2 --- package.json | 3 ++- reference/e2e/smoke.spec.ts | 20 ++++++++++++++++++++ scripts/write-version.mjs | 13 +++++++++++++ src/modules/doctor/status.ts | 10 +++------- src/version.ts | 10 ++++++++++ test/version.test.ts | 32 ++++++++++++++++++++++++++++++++ 6 files changed, 80 insertions(+), 8 deletions(-) create mode 100644 scripts/write-version.mjs create mode 100644 src/version.ts create mode 100644 test/version.test.ts diff --git a/package.json b/package.json index 92d6950..89b8574 100644 --- a/package.json +++ b/package.json @@ -127,7 +127,8 @@ "test:e2e": "playwright test", "test:int": "vitest run", "test:watch": "vitest", - "typecheck": "tsc --noEmit -p . && tsc -p test" + "typecheck": "tsc --noEmit -p . && tsc -p test", + "version": "node scripts/write-version.mjs && git add src/version.ts" }, "dependencies": { "busboy": "1.6.0", diff --git a/reference/e2e/smoke.spec.ts b/reference/e2e/smoke.spec.ts index 32dee65..037d2de 100644 --- a/reference/e2e/smoke.spec.ts +++ b/reference/e2e/smoke.spec.ts @@ -1,4 +1,6 @@ import { expect, type Page, test } from '@playwright/test' +import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs' +import path from 'node:path' import { attachmentOf, startSmtpSink } from './smtp-sink' import { totp } from './totp' @@ -176,6 +178,24 @@ test('SEO and GEO routes respond', async ({ request }) => { expect(await mcp.text()).toContain('get_page') }) +// A bundler turns a file that server code names relative to its own module into an ASSET. Turbopack published the +// plugin's whole package.json that way (dependency lists, scripts, the registry address) under /_next/static/media/. +test('no package.json is published: nothing under .next/static holds one, and the obvious addresses answer 404', async ({ request }) => { + const statics = path.resolve('.next/static') + const walk = (dir: string): string[] => (existsSync(dir) ? readdirSync(dir).flatMap((name) => { + const file = path.join(dir, name) + return statSync(file).isDirectory() ? walk(file) : [file] + }) : []) + const files = walk(statics) + expect(files.length, 'the built site\'s .next/static (run from the site folder)').toBeGreaterThan(0) + // by name, and by content (a manifest of a package: its dependency lists) + const manifests = files.filter((file) => /package[^/]*\.json$/.test(file) || (file.endsWith('.json') && /"(?:devDependencies|peerDependencies|publishConfig)"\s*:/.test(readFileSync(file, 'utf8')))) + expect(manifests.map((file) => path.relative(statics, file))).toEqual([]) + for (const address of ['/package.json', '/_next/package.json', '/_next/static/package.json', '/_next/static/media/package.json']) { + expect((await request.get(address)).status(), address).toBe(404) + } +}) + test('security headers: CSP enforced, no X-Powered-By', async ({ request }) => { const headers = (await request.get('/pl')).headers() expect(headers['content-security-policy']).toBeTruthy() diff --git a/scripts/write-version.mjs b/scripts/write-version.mjs new file mode 100644 index 0000000..571007d --- /dev/null +++ b/scripts/write-version.mjs @@ -0,0 +1,13 @@ +#!/usr/bin/env node +// Writes the version from package.json into src/version.ts (see the comment there). +// Runs as the `version` script of package.json; by hand: node scripts/write-version.mjs +import { readFileSync, writeFileSync } from 'node:fs' + +const root = new URL('../', import.meta.url) +const { version } = JSON.parse(readFileSync(new URL('package.json', root), 'utf8')) +const file = new URL('src/version.ts', root) +const before = readFileSync(file, 'utf8') +const after = before.replace(/export const PLUGIN_VERSION = '[^']*'/, `export const PLUGIN_VERSION = '${version}'`) +if (after === before && !before.includes(`'${version}'`)) {throw new Error('src/version.ts: the PLUGIN_VERSION line was not found')} +writeFileSync(file, after) +console.log(`src/version.ts → ${version}`) diff --git a/src/modules/doctor/status.ts b/src/modules/doctor/status.ts index b971e0b..a548ad3 100644 --- a/src/modules/doctor/status.ts +++ b/src/modules/doctor/status.ts @@ -1,10 +1,10 @@ import type { BasePayload, Endpoint, PayloadRequest } from 'payload' import { timingSafeEqual } from 'node:crypto' -import { readFileSync } from 'node:fs' import type { DoctorStatus } from './types.js' +import { PLUGIN_VERSION } from '../../version.js' import { openSignUpCollections, staffCollectionsOnOwnRoles, staffCollectionsOpenToOthers } from '../access/selfRegistration.js' import { countPendingCookies } from '../consent/scanner/approval.js' import { COOKIE_REVIEW_SLUG } from '../consent/scanner/collection.js' @@ -13,13 +13,9 @@ import { countAccountsWithoutTotp } from '../security/totp/setupRequired.js' import { clientIp, consumeRateLimit, tooManyRequests } from '../shared/rateLimit.js' import { turnstileSecretFromEnv } from '../turnstile/config.js' -/** The plugin's own version (package.json next to dist/ and src/). */ +/** The plugin's own version — a constant (src/version.ts), never a file read at run time. */ export function pluginVersion(): string { - try { - return (JSON.parse(readFileSync(new URL('../../../package.json', import.meta.url), 'utf8')) as { version: string }).version - } catch { - return 'unknown' - } + return PLUGIN_VERSION } type CollectionLike = { fields?: Array<{ name?: string }>; slug: string } diff --git a/src/version.ts b/src/version.ts new file mode 100644 index 0000000..b12905d --- /dev/null +++ b/src/version.ts @@ -0,0 +1,10 @@ +/** + * The plugin's own version, as a constant in the code. + * + * Not read from package.json at run time: naming that file relative to the module (a URL built + * from the module's own address) in server code makes a bundler treat the file as an ASSET — Turbopack copied the plugin's whole + * package.json (dependency lists, scripts, the registry address) into the site's public + * `/_next/static/media/`. Written by `node scripts/write-version.mjs` (the `version` script of + * package.json runs it on `pnpm version …`); test/version.test.ts fails when the two differ. + */ +export const PLUGIN_VERSION = '1.16.7' diff --git a/test/version.test.ts b/test/version.test.ts new file mode 100644 index 0000000..c18cc7e --- /dev/null +++ b/test/version.test.ts @@ -0,0 +1,32 @@ +import { readdirSync, readFileSync, statSync } from 'node:fs' +import path from 'node:path' +import { describe, expect, test } from 'vitest' + +import { pluginVersion } from '../src/modules/doctor/status.js' +import { PLUGIN_VERSION } from '../src/version.js' + +const root = path.resolve(import.meta.dirname, '..') +const walk = (dir: string): string[] => readdirSync(dir).flatMap((name) => { + const file = path.join(dir, name) + return statSync(file).isDirectory() ? walk(file) : [file] +}) + +describe('the plugin version', () => { + test('src/version.ts says what package.json says (node scripts/write-version.mjs after a bump)', () => { + const { version } = JSON.parse(readFileSync(path.join(root, 'package.json'), 'utf8')) as { version: string } + expect(PLUGIN_VERSION).toBe(version) + expect(pluginVersion()).toBe(version) + }) + + // A file named through `new URL(…, import.meta.url)` (or imported) in code a site bundles becomes an ASSET of + // that site: Turbopack put the plugin's whole package.json into the public /_next/static/media/. + test('no code a site bundles names package.json as a module-relative file', () => { + const cli = /^src\/modules\/(?:starter|doctor\/(?:cli|project|run|probe|rules))/ // run by the bin scripts, on the project's own disk + const offenders = walk(path.join(root, 'src')) + .filter((file) => /\.(?:ts|tsx|mjs|js)$/.test(file)) + .map((file) => ({ code: readFileSync(file, 'utf8'), rel: path.relative(root, file) })) + .filter(({ code, rel }) => (!cli.test(rel) && /new URL\([^)]*import\.meta\.url/.test(code)) || /(?:from|import\()\s*['"][^'"]*package\.json['"]/.test(code)) + .map(({ rel }) => rel) + expect(offenders).toEqual([]) + }) +})