diff --git a/src/protect/runtime.js b/src/protect/runtime.js index 3f9378d..b5b87f4 100644 --- a/src/protect/runtime.js +++ b/src/protect/runtime.js @@ -448,6 +448,23 @@ export async function createProtection(options = {}) { // Mode is mutable so a Patchstack for JavaScript refresh can flip dry-run ↔ block when SaaS enables production. // Precedence: PATCHSTACK_MODE env (local override) > API enforcement > options.mode > dry-run. let mode = resolveMode(options, bundle); + // Said once per pairing, in the app's own log: a mode set in the environment wins over the one chosen + // on the platform, and nothing on the platform shows that it did. + const warnedModeOverrides = new Set(); + const noteModeOverride = (delivered) => { + const local = typeof process !== 'undefined' ? process.env?.PATCHSTACK_MODE : undefined; + const chosen = delivered?.enforcement; + if ((local !== 'block' && local !== 'dry-run') || (chosen !== 'block' && chosen !== 'dry-run') || local === chosen) return; + if (typeof process !== 'undefined' && process.env?.NEXT_PHASE === 'phase-production-build') return; + const pairing = `${local}>${chosen}`; + if (warnedModeOverrides.has(pairing)) return; + warnedModeOverrides.add(pairing); + console.warn( + `[patchstack] PATCHSTACK_MODE=${local} is set in this environment, so the guard runs in ${local}. ` + + `The mode chosen in the Patchstack dashboard (${chosen}) is not applied. Remove PATCHSTACK_MODE to follow the dashboard.`, + ); + }; + noteModeOverride(bundle); // Rule-derived runtime state. Held in `let` bindings the guard methods below close over, so a // refresh (see the loop near the end) can hot-swap the engines by reassigning them — the // egress interception and the protection object itself stay in place, no re-install. @@ -2148,6 +2165,7 @@ export async function createProtection(options = {}) { synchronization = next.synchronization ?? null; if (next.source?.ok) lastCheckedAt = Date.now(); mode = resolveMode(options, next); + noteModeOverride(next); applyBundle(next); noteRuleSource(next.source); if (next.source?.ok !== false) recovery?.stop(); diff --git a/tests/protect/local-mode-override-is-said.test.ts b/tests/protect/local-mode-override-is-said.test.ts new file mode 100644 index 0000000..74b6016 --- /dev/null +++ b/tests/protect/local-mode-override-is-said.test.ts @@ -0,0 +1,53 @@ +import { afterEach, describe, expect, it, vi } from 'vitest'; +import { createProtection } from '../../src/protect/runtime.js'; + +// A mode set in the environment wins over the one chosen on the platform. The app's log says so, once, +// because nothing on the platform shows that the choice made there is not the one in force. + +const bundle = (enforcement: string) => + ({ bundle: { firewall: [], whitelists: [], whitelist_keys: {}, enforcement }, etag: 'W/"1"', buildId: null, source: 'site:x@' }) as never; + +let protection: any; + +afterEach(async () => { + await protection?.stop(); + protection = undefined; + delete process.env.PATCHSTACK_MODE; + vi.restoreAllMocks(); +}); + +const overrideWarnings = (warn: ReturnType) => + warn.mock.calls.map((call) => String(call[0])).filter((line) => line.includes('PATCHSTACK_MODE=')); + +describe('a local mode that overrides the platform', () => { + it('is said in the log, naming both modes', async () => { + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); + process.env.PATCHSTACK_MODE = 'dry-run'; + + protection = await createProtection({ reportDetections: false, rules: bundle('block') }); + + expect(protection.mode).toBe('dry-run'); + const lines = overrideWarnings(warn); + expect(lines).toHaveLength(1); + expect(lines[0]).toContain('PATCHSTACK_MODE=dry-run'); + expect(lines[0]).toContain('dashboard (block) is not applied'); + }); + + it('is not said when both agree', async () => { + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); + process.env.PATCHSTACK_MODE = 'block'; + + protection = await createProtection({ reportDetections: false, rules: bundle('block') }); + + expect(overrideWarnings(warn)).toEqual([]); + }); + + it('is not said when nothing is set locally', async () => { + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); + + protection = await createProtection({ reportDetections: false, rules: bundle('block') }); + + expect(protection.mode).toBe('block'); + expect(overrideWarnings(warn)).toEqual([]); + }); +});