f101bde6a8
The image is built once with sentinel values
(ENV NEXT_PUBLIC_SELF_HOSTED=__NEXT_PUBLIC_SELF_HOSTED__) that
docker-entrypoint.sh seds into .next at container start. Comparing a flag in
place defeats that: the bundler inlines the sentinel, the minifier folds
"__NEXT_PUBLIC_SELF_HOSTED__" === 'true' to false and eliminates the branch, so
both the variable name and the sentinel disappear and sed has nothing left to
replace. The flag is then permanently false whatever the operator configures.
Diagnosed against a running self-hosted instance: the compiled gate read
function r(){return"true"!==process.env.FORCE_PAYWALL
&&"true"===process.env.DISABLE_PAYWALL}
with the isSelfHosted() branch gone. The un-prefixed FORCE_PAYWALL /
DISABLE_PAYWALL survived precisely because they are never inlined, and
NODE_ENV === 'development' was folded away by the same mechanism. The one
place the flag still worked, getSessionTimeoutConfig(env = process.env), reads
it off a parameter the bundler cannot fold.
Consequence: every Docker self-host ran with the entitlement paywall live, so
ai, bank_sync, skatteverket and email_send went dark 30 days after company
creation when the seeded trial grants expired. Nothing surfaced it, because
dev and the Vercel build both have real env values and never reproduce it.
Analytics, forced MFA, BankID and the hosted upload ceiling read the same flag
and were wrong in the same direction.
Flags are now read as values through lib/env/public-flags, which keeps the
sentinel in the output as a live string literal and defers the comparison to
runtime. flagEnabled uses a Set lookup rather than ===, which a minifier could
fold if it ever inlined the helper.
Guarded twice, because the source fix alone would not have caught this:
- check:guards folded-public-flag fails any in-place NEXT_PUBLIC_* comparison
(AST, no baseline, verified to fire on a probe file);
- docker-publish asserts the sentinels survive the built image, which is the
only artifact where the failure is observable.
npm test 14999 passed, npm run lint 0 errors, npm run check:guards clean.
Signed-off-by: Bjorn Bergenheim <29535152+bjornbergenheim@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
54 lines
1.9 KiB
TypeScript
54 lines
1.9 KiB
TypeScript
import { describe, it, expect, afterEach, vi } from 'vitest'
|
|
import { flagEnabled, isSelfHosted } from '../public-flags'
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllEnvs()
|
|
})
|
|
|
|
describe('flagEnabled', () => {
|
|
it('is true only for the exact string "true"', () => {
|
|
expect(flagEnabled('true')).toBe(true)
|
|
expect(flagEnabled('false')).toBe(false)
|
|
expect(flagEnabled('')).toBe(false)
|
|
expect(flagEnabled(undefined)).toBe(false)
|
|
})
|
|
|
|
it('does not accept near-misses that would silently switch a flag on', () => {
|
|
// An operator typing TRUE/1/yes gets the documented default, not a guess.
|
|
expect(flagEnabled('TRUE')).toBe(false)
|
|
expect(flagEnabled('True')).toBe(false)
|
|
expect(flagEnabled('1')).toBe(false)
|
|
expect(flagEnabled('yes')).toBe(false)
|
|
expect(flagEnabled(' true')).toBe(false)
|
|
})
|
|
|
|
it('treats an unsubstituted Docker sentinel as off', () => {
|
|
// The image is built with __NEXT_PUBLIC_SELF_HOSTED__ and the entrypoint
|
|
// seds the real value in. If substitution ever fails, the flag must read
|
|
// off rather than matching some truthy heuristic.
|
|
expect(flagEnabled('__NEXT_PUBLIC_SELF_HOSTED__')).toBe(false)
|
|
})
|
|
|
|
it('reads the value the entrypoint substituted', () => {
|
|
expect(flagEnabled('true')).toBe(true)
|
|
})
|
|
})
|
|
|
|
describe('isSelfHosted', () => {
|
|
it('follows NEXT_PUBLIC_SELF_HOSTED at call time, not at module load', () => {
|
|
vi.stubEnv('NEXT_PUBLIC_SELF_HOSTED', 'true')
|
|
expect(isSelfHosted()).toBe(true)
|
|
|
|
// Same module instance, different env: proves the read is not frozen into
|
|
// a module-level constant, which is what lets the Docker entrypoint's
|
|
// runtime substitution take effect at all.
|
|
vi.stubEnv('NEXT_PUBLIC_SELF_HOSTED', 'false')
|
|
expect(isSelfHosted()).toBe(false)
|
|
})
|
|
|
|
it('is false when the variable is absent (hosted is the default)', () => {
|
|
vi.stubEnv('NEXT_PUBLIC_SELF_HOSTED', undefined)
|
|
expect(isSelfHosted()).toBe(false)
|
|
})
|
|
})
|