Files
raucao 023f5b39bf
CI / Test (pull_request) Successful in 43s
Release Drafter / Update release notes draft (pull_request) Successful in 3s
Fix strfry README and drop deployment specifics
The new README described the directory as mostly Node ESM and called the
pre-existing Deno policies "legacy", even though ldap-policy.ts is a shared
module reused by the new member bridge and strfry-policy.ts is the members
relay's current write policy. It also baked in internal deployment details
(Chef, strfry-1, /etc/*.conf paths, the staging relay's ZeroTier IP).

Document every file with its correct runtime (Deno vs Node 24 LTS), drop the
"legacy" label, and genericize the deployment examples. Cross-link the README
from docs/dev/nostr.md, and replace the remaining internal host/IP references
in sync-policy.mjs and its test with neutral placeholders.
2026-11-18 13:51:48 +04:00

113 lines
3.6 KiB
JavaScript
Executable File

#!/usr/bin/env node
// Fail-closed strfry write policy for the members relay's approved-content sync.
//
// This policy only accepts events that arrived through `strfry sync` from the
// dedicated staging relay. Everything else is rejected, including events with
// the wrong source type or the wrong source URL, so that a misconfigured or
// compromised sync source can never write into the production relay.
//
// Reads strfry JSONL input messages from stdin and writes one
// {"id","action","msg"} JSON object per line to stdout.
//
// See README.md for the env vars and how strfry invokes this.
import readline from 'node:readline';
import { pathToFileURL } from 'node:url';
const NAME = 'sync-policy';
export function createState(env = process.env) {
return {
config: {
stagingRelayUrl: typeof env.STAGING_RELAY_URL === 'string' ? env.STAGING_RELAY_URL.trim() : '',
},
};
}
function reject(id, msg) {
return { id, action: 'reject', msg };
}
function describeSource(msg) {
return `sourceType=${JSON.stringify(msg && msg.sourceType)} sourceInfo=${JSON.stringify(msg && msg.sourceInfo)}`;
}
export function decide(msg, state) {
const id = msg && msg.event && typeof msg.event.id === 'string' ? msg.event.id : '';
const expected = state.config.stagingRelayUrl;
if (msg === null || typeof msg !== 'object') {
return reject(id, 'malformed input');
}
if (msg.type !== 'new') {
return reject(id, `unexpected request type: ${String(msg.type)} (${describeSource(msg)})`);
}
if (expected === '') {
return reject(id, `source not allowed: STAGING_RELAY_URL is not configured (${describeSource(msg)})`);
}
if (msg.sourceType !== 'Sync') {
return reject(id, `source not allowed: expected sourceType=${JSON.stringify('Sync')} got ${JSON.stringify(msg.sourceType)} (${describeSource(msg)})`);
}
if (msg.sourceInfo !== expected) {
return reject(id, `source not allowed: expected sourceInfo=${JSON.stringify(expected)} got ${JSON.stringify(msg.sourceInfo)} (${describeSource(msg)})`);
}
return { id, action: 'accept', msg: '' };
}
function writeResult(output, result) {
output.write(`${JSON.stringify(result)}\n`);
}
export async function run({ input = process.stdin, output = process.stdout, error = process.stderr, env = process.env, state = undefined } = {}) {
const policyState = state ?? createState(env);
if (policyState.config.stagingRelayUrl === '') {
error.write(`[${NAME}] warning: STAGING_RELAY_URL is not set; rejecting all events\n`);
} else {
error.write(`[${NAME}] accepting sourceType=Sync sourceInfo=${JSON.stringify(policyState.config.stagingRelayUrl)}\n`);
}
const rl = readline.createInterface({ input, crlfDelay: Infinity });
for await (const line of rl) {
if (line.trim() === '') continue;
let msg;
try {
msg = JSON.parse(line);
} catch (e) {
error.write(`[${NAME}] malformed JSON: ${e.message}\n`);
writeResult(output, reject('', 'malformed JSON'));
continue;
}
let result;
try {
result = decide(msg, policyState);
} catch (e) {
error.write(`[${NAME}] unexpected error: ${e.stack || e.message}\n`);
result = reject(msg && msg.event && typeof msg.event.id === 'string' ? msg.event.id : '', 'internal error');
}
if (result.action !== 'accept') {
error.write(`[${NAME}] reject id=${result.id} reason=${result.msg}\n`);
}
writeResult(output, result);
}
}
const isMain = process.argv[1] !== undefined && import.meta.url === pathToFileURL(process.argv[1]).href;
if (isMain) {
run().catch((e) => {
process.stderr.write(`[${NAME}] fatal: ${e.stack || e.message}\n`);
process.exit(1);
});
}