Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/protect/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand Down
53 changes: 53 additions & 0 deletions tests/protect/local-mode-override-is-said.test.ts
Original file line number Diff line number Diff line change
@@ -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<typeof vi.spyOn>) =>
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([]);
});
});