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
19 changes: 19 additions & 0 deletions field-test/hosted-startup.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,22 @@ dependency activity and outbound calls. A separate loader experiment could
observe which package versions really load, then evaluate version-specific
patches. Package replacement has its own compatibility and rollback
requirements and is outside this fixture.

## Live Linux smoke test

For a dedicated sandbox site, supply `PATCHSTACK_SITE_UUID` and
`PATCHSTACK_API_KEY` through the Linux runner's environment. From the same
checkout used to build the tarball, run either Fastify fixture with
`PS_FASTIFY_LIVE=1`.

```sh
PS_FASTIFY_LIVE=1 npm run test:frameworks -- fastify-startup-esm "$tarball"
PS_FASTIFY_LIVE=1 npm run test:frameworks -- fastify-startup-cjs "$tarball"
```

This uploads the fixture's package inventory and input map, starts Fastify in
dry-run mode, and checks a signed activation response over loopback HTTP. It
compares the uploaded build ID with a different local ID, and checks the
service's verdict in both cases. Use a sandbox site reserved for this fixture
because each run uploads an app map. Rule delivery and a matching build do not
establish that this fixture has any generated rules to enforce.
37 changes: 37 additions & 0 deletions field-test/runtime-attachment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Runtime attachment options

| Host boundary | What it can cover | Next test |
| --- | --- | --- |
| Node module loader | Known package factories in apps whose startup the host controls. The Fastify pilot attaches after body parsing and before the handler. | Test coexisting instrumentation and a managed Linux service restart. |
| Node HTTP diagnostics | Compare which HTTP requests Node observed with those that traversed a Connect adapter. HTTP/2 has separate events. | Use counters to identify missing coverage. Keep enforcement at a hook that controls the request or response. |
| Node HTTP listener or outbound transport | An early listener can work with URL and headers. The stock runtime can screen supported outbound calls. | Validate HTTP/2, upgrades, and framework body ownership separately before adding enforcement at a core listener. |
| Host proxy or network policy | Request and response policy at a TLS-terminating proxy. Network policy can constrain process egress independently. | Test tenant identity at the trusted proxy boundary and compare it with in-process reporting. |
| Native Fetch handler | Wrap the handler in Bun, Deno, Workers, or another runtime that passes a Web Request to the app. | Start with the native handler, then add tests for the runtime's route tables, streaming and outbound APIs. |

Node's [module hooks](https://nodejs.org/api/module.html#moduleregisterhooksoptions)
support host startup integrations with explicit arguments. Node's
[diagnostics channels](https://nodejs.org/api/diagnostics_channel.html#built-in-channels)
publish HTTP lifecycle events. Package-load inventory and any future
version-specific package replacement need their own rules, provenance and
rollback tests.

Deno documents [synchronous loader hooks](https://docs.deno.com/runtime/reference/loader_hooks/)
and [native Fetch serving](https://docs.deno.com/runtime/fundamentals/http_server/).
Bun has [runtime plugins](https://bun.com/docs/runtime/plugins) and
[a native HTTP handler](https://bun.com/docs/runtime/http/server). Workers
provide a [Fetch entry point](https://developers.cloudflare.com/workers/runtime-apis/handlers/fetch/).
Bundled apps need their deployment or framework integration because the original
package may never reach a runtime loader.

The local smoke test exercises a synthetic module and real loopback HTTP with
the Connect build for each runtime. It does not authenticate to a rules service.
Run it after building:

```sh
bun scripts/fetch-runtime-consumer.mjs
deno run --no-prompt --allow-net=127.0.0.1 --allow-env --allow-read=. scripts/fetch-runtime-consumer.mjs
```

Run the Deno test in an isolated shell without application secrets. Its
environment and filesystem permissions let the guard load its configuration and
code. Its network permission limits the test to loopback.
27 changes: 27 additions & 0 deletions scripts/fastify-startup-live.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { installFastifyHook } from './preload.mjs';

assert.equal(process.env.PS_FASTIFY_LIVE, '1');
assert.equal(process.platform, 'linux');
assert.equal(process.env.NODE_OPTIONS, undefined);
const deployment = JSON.parse(readFileSync(process.env.PATCHSTACK_DEPLOYMENT_FILE, 'utf8'));
const { protection } = await import('@patchstack/connect/register');
const expectMatch = process.env.PS_FASTIFY_EXPECT_MATCH === '1';
const deadline = Date.now() + 20_000;
while (expectMatch && !protection.synchronization.matched && Date.now() < deadline) {
await new Promise(done => setTimeout(done, 1000));
if (Date.now() < deadline) await protection.refresh();
}
const status = protection.synchronization;
assert.equal(status.source.origin, 'api');
assert.equal(status.source.ok, true);
assert.equal(status.siteUuid, deployment.siteUuid);
assert.equal(status.buildId, deployment.buildId);
assert.equal(status.matched, expectMatch);
const { bundle } = JSON.parse(readFileSync('.patchstack/patchstack-rules.json', 'utf8'));
console.log('Fastify live pilot ' + JSON.stringify({
matched: status.matched, mapRules: status.mapRules, delivered: bundle.firewall.length,
scoped: status.rules.scoped, scopedBlocking: status.rules.scopedBlocking,
}));
installFastifyHook(protection);
25 changes: 16 additions & 9 deletions scripts/fastify-startup-preload.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,25 @@ const fastify = requireFromEntry(factoryPath);
const factoryUrl = pathToFileURL(factoryPath).href;
const facadeUrl = new URL('./facade.mjs', import.meta.url).href;

let protection;

export function createGuardedApp(...options) {
return fastify(...options).register(patchstackFastify);
return fastify(...options).register(patchstackFastify, { protection });
}
Object.assign(createGuardedApp, fastify);
createGuardedApp.fastify = createGuardedApp;
createGuardedApp.default = createGuardedApp;

registerHooks({
resolve(specifier, context, nextResolve) {
const resolved = nextResolve(specifier, context);
if (specifier !== 'fastify') return resolved;
if (resolved.url !== factoryUrl) throw new Error('The Fastify startup fixture only covers one installed copy.');
return { url: facadeUrl, format: 'module', shortCircuit: true };
},
});
export function installFastifyHook(liveProtection) {
protection = liveProtection;
registerHooks({
resolve(specifier, context, nextResolve) {
const resolved = nextResolve(specifier, context);
if (specifier !== 'fastify') return resolved;
if (resolved.url !== factoryUrl) throw new Error('The Fastify startup fixture only covers one installed copy.');
return { url: facadeUrl, format: 'module', shortCircuit: true };
},
});
}

if (process.env.PS_FASTIFY_LIVE !== '1') installFastifyHook();
71 changes: 71 additions & 0 deletions scripts/fetch-runtime-consumer.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import assert from 'node:assert/strict';

const name = typeof Bun === 'undefined' ? 'deno' : 'bun';
assert.ok(name === 'bun' || typeof Deno !== 'undefined', 'Run this fixture with Bun or Deno.');
const dist = new URL('../dist/', import.meta.url);
const { createProtection } = await import(new URL(name === 'deno' ? 'protect.edge.js' : 'protect.js', dist));

const specifier = 'fixture:connect-runtime';
if (name === 'bun') {
Bun.plugin({
name: 'Connect runtime fixture',
setup(build) {
build.onResolve({ filter: /^connect-runtime$/, namespace: 'fixture' }, ({ path }) => ({
path, namespace: 'fixture',
}));
build.onLoad({ filter: /^connect-runtime$/, namespace: 'fixture' }, () => ({
exports: { tag: 'fixture-ordinary' }, loader: 'object',
}));
},
});
} else {
const { registerHooks } = await import('node:module');
const virtual = new URL('./connect-fake-fixture.mjs', import.meta.url).href;
registerHooks({
resolve(request, context, next) {
return request === specifier ? { url: virtual, shortCircuit: true } : next(request, context);
},
load(url, context, next) {
return url === virtual
? { format: 'module', source: 'export const tag = "fixture-ordinary"', shortCircuit: true }
: next(url, context);
},
});
}
const { tag } = await import(specifier);
assert.equal(tag, 'fixture-ordinary');

const protection = await createProtection({
mode: 'block',
rules: { firewall: [
{ id: 'synthetic-request', rule_v2: [{ parameter: 'post.label', match: { type: 'equals', value: 'fixture-block' } }] },
{ id: 'synthetic-response', phase: 'response', action: 'redact',
rule_v2: [{ parameter: 'response.body', match: { type: 'contains', value: 'fixture-private' } }] },
], whitelists: [], whitelist_keys: {} },
});
const handle = protection.fetch(async request =>
Response.json({ body: await request.json(), sample: 'fixture-private' }));
const options = { hostname: '127.0.0.1', port: 0 };
const server = name === 'bun' ? Bun.serve({ ...options, fetch: handle }) : Deno.serve({ ...options, onListen() {} }, handle);
try {
const port = name === 'bun' ? server.port : server.addr.port;
const post = label => fetch(`http://127.0.0.1:${port}/fixture`, {
method: 'POST', signal: AbortSignal.timeout(2000),
headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ label }),
});
const allowed = await post(tag);
assert.equal(allowed.status, 200);
const result = await allowed.json();
assert.deepEqual(result.body, { label: tag });
assert.equal(typeof result.sample, 'string');
assert.doesNotMatch(result.sample, /fixture-private/);
const blocked = await post('fixture-block');
assert.equal(blocked.status, 403);
await blocked.text();
const version = name === 'bun' ? Bun.version : Deno.version.deno;
console.log(`PASS ${name} ${version}: module hook and native Fetch request/response screening`);
} finally {
if (name === 'bun') await server.stop(true);
else await server.shutdown();
await protection.stop();
}
72 changes: 64 additions & 8 deletions scripts/framework-consumers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { createServer } from 'node:net';
import { tmpdir } from 'node:os';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { ACTIVATION_HEADER, activationChallenge, verifyActivationResponse } from '../src/protect/activation.js';

const output = `({ label: body.label, sample: 'fixture-private' })`;
const fetchRoute = `async ({ request }) => {
Expand Down Expand Up @@ -167,6 +168,7 @@ app.get('/stream', async (request, reply) => {
.send(Readable.from(chunks()));
});
app.post('/fixture', async ({ body }) => ${output});
app.options('/fixture', async () => ({ ok: true }));
app.listen({ port: Number(process.env.PORT), host: '127.0.0.1' });
` },
rules: 'patchstack/rules.json',
Expand All @@ -188,24 +190,39 @@ for (const format of ['esm', 'cjs']) {
const [name, tarball] = process.argv.slice(2);
assert.ok(Object.hasOwn(fixtures, name) && tarball, `Usage: node scripts/framework-consumers.mjs <${Object.keys(fixtures).join('|')}> <tarball>`);
const fixture = fixtures[name];
const live = process.env.PS_FASTIFY_LIVE === '1';
const siteUuid = process.env.PATCHSTACK_SITE_UUID;
const apiKey = process.env.PATCHSTACK_API_KEY;
if (live) {
assert.ok(fixture.preload && process.platform === 'linux', 'Live mode requires a Linux Fastify startup fixture.');
assert.ok(siteUuid && apiKey, 'Live mode needs a dedicated sandbox site and API key.');
}
const cwd = mkdtempSync(join(tmpdir(), 'ps-framework-'));
const env = Object.fromEntries(Object.entries(process.env).filter(([key]) => !key.startsWith('PATCHSTACK_') && key !== 'NODE_OPTIONS'));
const env = Object.fromEntries(Object.entries(process.env).filter(([key]) => !key.startsWith('PATCHSTACK_') && key !== 'NODE_OPTIONS' && !key.startsWith('PS_FASTIFY_')));
Object.assign(env, { ASTRO_TELEMETRY_DISABLED: '1', NUXT_TELEMETRY_DISABLED: '1', CI: '1', HOST: '127.0.0.1' });
const put = (file, source) => {
mkdirSync(dirname(join(cwd, file)), { recursive: true });
writeFileSync(join(cwd, file), source);
};
function run(command, args) {
const child = spawnSync(command, args, { cwd, env, encoding: 'utf8', timeout: 180_000 });
function run(command, args, childEnv = env) {
const child = spawnSync(command, args, { cwd, env: childEnv, encoding: 'utf8', timeout: 180_000 });
assert.equal(child.status, 0, [child.error?.message, child.stdout, child.stderr].join('\n').replaceAll(cwd, '<fixture>'));
}
async function check(protectedApp) {
async function check(protectedApp, liveBuild) {
const listener = createServer().listen(0, '127.0.0.1');
await once(listener, 'listening');
const port = listener.address().port;
await new Promise(done => listener.close(done));
const processEnv = { ...env, PORT: String(port), ORIGIN: `http://127.0.0.1:${port}` };
const flags = protectedApp && fixture.preload ? ['--import', fixture.preload] : [];
if (liveBuild) {
Object.assign(processEnv, {
PS_FASTIFY_LIVE: '1', PS_FASTIFY_EXPECT_MATCH: liveBuild.matched ? '1' : '0',
PATCHSTACK_SITE_UUID: siteUuid, PATCHSTACK_API_KEY: apiKey,
PATCHSTACK_DEPLOYMENT_FILE: liveBuild.file, PATCHSTACK_ENVIRONMENT: 'sandbox', PATCHSTACK_MODE: 'dry-run',
});
}
const preload = liveBuild ? './host/live.mjs' : fixture.preload;
const flags = protectedApp && preload ? ['--import', preload] : [];
const app = spawn(process.execPath, [...flags, ...fixture.start], { cwd, env: processEnv, stdio: ['ignore', 'pipe', 'pipe'] });
let log = '';
for (const stream of [app.stdout, app.stderr]) stream.on('data', part => { log += part; });
Expand All @@ -215,13 +232,34 @@ async function check(protectedApp) {
});
try {
let normal;
for (let attempt = 0; attempt < 200 && app.exitCode === null; attempt++) {
for (let attempt = 0; attempt < (liveBuild ? 350 : 200) && app.exitCode === null; attempt++) {
try { normal = await post('ordinary'); break; } catch { await new Promise(done => setTimeout(done, 100)); }
}
assert.equal(normal?.status, 200, log.replaceAll(cwd, '<fixture>'));
const result = await normal.json();
assert.equal(result.label, 'ordinary');
assert.equal(typeof result.sample, 'string');
if (liveBuild) {
const reported = log.split('\n').find(line => line.startsWith('Fastify live pilot '));
assert.ok(reported, 'The host startup hook did not report SaaS rule delivery.');
const url = processEnv.ORIGIN + '/fixture';
const challenge = await activationChallenge(apiKey, url);
const probe = await fetch(url, {
method: 'OPTIONS', redirect: 'error', headers: { [ACTIVATION_HEADER]: challenge },
signal: AbortSignal.timeout(3000),
});
assert.equal(probe.status, 200);
const status = await verifyActivationResponse(apiKey, challenge, await probe.text(), probe.headers.get(ACTIVATION_HEADER));
const { buildId } = JSON.parse(readFileSync(join(cwd, liveBuild.file), 'utf8'));
assert.ok(status, 'Fastify did not return a signed activation response.');
assert.equal(status.siteUuid, siteUuid);
assert.equal(status.buildId, buildId);
assert.equal(status.matched, liveBuild.matched);
assert.equal(status.source.ok, true);
assert.equal(status.source.origin, 'api');
console.log(reported);
return;
}
if (protectedApp) assert.doesNotMatch(result.sample, /fixture-private/);
else assert.equal(result.sample, 'fixture-private');
assert.equal((await post('fixture-block')).status, protectedApp ? 403 : 200);
Expand Down Expand Up @@ -270,6 +308,9 @@ try {
if (fixture.preload) {
mkdirSync(join(cwd, 'host'), { recursive: true });
copyFileSync(fileURLToPath(new URL('./fastify-startup-preload.mjs', import.meta.url)), join(cwd, fixture.preload));
if (live) {
copyFileSync(fileURLToPath(new URL('./fastify-startup-live.mjs', import.meta.url)), join(cwd, 'host/live.mjs'));
}
put('host/facade.mjs', `import { createGuardedApp } from './preload.mjs';
export { createGuardedApp as fastify, createGuardedApp as default, createGuardedApp as 'module.exports' };
export const { errorCodes, LogController } = createGuardedApp;`);
Expand All @@ -288,11 +329,26 @@ export const { errorCodes, LogController } = createGuardedApp;`);
rule_v2: [{ parameter: 'response.header.x-fixture', match: { type: 'regex', value: '/fixture-stream-private/' } }] },
], whitelists: [], whitelist_keys: {} }));
if (fixture.build) run(process.execPath, fixture.build);
await check(true);
if (live) {
// A separate local bundle would make a broken live policy look healthy.
put(fixture.rules, JSON.stringify({ firewall: [], whitelists: [], whitelist_keys: {} }));
const deployment = 'host/deployment.json';
run(process.execPath, [join(packageDir, 'dist/cli.js'), 'prepare-deploy', '--out', deployment], {
...env, PATCHSTACK_SITE_UUID: siteUuid, PATCHSTACK_API_KEY: apiKey, PATCHSTACK_ENVIRONMENT: 'sandbox',
});
await check(true, { file: deployment, matched: true });
const unmatched = JSON.parse(readFileSync(join(cwd, deployment), 'utf8'));
unmatched.buildId = (unmatched.buildId[0] === '0' ? '1' : '0') + unmatched.buildId.slice(1);
put('host/unmatched.json', JSON.stringify(unmatched));
await check(true, { file: 'host/unmatched.json', matched: false });
} else {
await check(true);
}
if (fixture.preload) {
for (const [file, source] of Object.entries(fixture.files)) assert.equal(readFileSync(join(cwd, file), 'utf8'), source);
}
console.log(`PASS ${name}: HTTP request screening, normal body parsing and response redaction${fixture.stream ? ' including stream headers' : ''}`);
if (live) console.log(`PASS ${name}: Linux live-rule and build-identity checks with unchanged application source`);
else console.log(`PASS ${name}: HTTP request screening, normal body parsing and response redaction${fixture.stream ? ' including stream headers' : ''}`);
} finally {
rmSync(cwd, { recursive: true, force: true });
}
3 changes: 3 additions & 0 deletions src/protect/install/template-upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ const PREVIOUS: Record<string, string | readonly string[]> = {
'fastify-plugin.ts': [
'5d606cb8f8f2e6fe35b9d377c5568c5a2090a81af9f0acb219ebc15a713792e9',
'1b995704a608397fd6e2fe47b4e46461299cb444110b2988fd9ebedee9804d05',
'877d491e982ad43b3f3ab7f70293fd959662d929714b90304a44a9030be745d6',
],
'fastify-plugin.js': [
'03327d437513571d53554ca34c84ba170ebeebb5e89c2602ae340fb82178ab48',
'71bcd3699bad8fba0fa347332c6739219d2e3cf7a594a85442fe853f23ab5d53',
'18ea5c583badf23d4b7d893d1ed3246962286b1ce930b5cdc3abbee5b4c5df55',
],
'fastify-plugin.cjs': [
'b3754bf023ebb5f2a22e0a95cfe74bb783034ec3db9ce61a3e7202237e09e1dd',
'fd90ef3a4be1234307e3b631d1ac7e898b24a025043c928b7ae48189f19af45d',
'88fa3ceb74548b68b1236bc7fa417ed4f7132a8888e7009cd7e7af649e991286',
],
'guard.ts': '270e29769f5a7b3ab70ee39970be5a4e150e1b3949877687dca1ed83596afae7',
'sveltekit-hooks.ts': '0a68389ac9018dc0ab5805356ad78301f039cb4c701bf642e7f986cc959da990',
Expand Down
Loading
Loading