@runeditrun/rer 0.2.25 → 0.2.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cli/demo-host.mjs CHANGED
@@ -1,4 +1,5 @@
1
1
  import { cp, mkdir, readFile, rm, writeFile } from 'node:fs/promises';
2
+ import { randomBytes } from 'node:crypto';
2
3
  import { createRequire } from 'node:module';
3
4
  import { dirname, join } from 'node:path';
4
5
  import { contained } from './project.mjs';
@@ -6,14 +7,18 @@ import { invalid, missing, refuse, RerError } from './errors.mjs';
6
7
  import { imageFolder } from './container.mjs';
7
8
  import { prepareDemoImage } from '../modules/demo-host/image.mjs';
8
9
  import { DEMO_FILES, INSTANCE_TYPES, demoPlan } from '../modules/demo-host/plan.mjs';
10
+ import { awaitCertificates } from '../modules/demo-host/certificate.mjs';
11
+ import { MEASUREMENT_COOKIE, MEASUREMENT_HEADER, TESTING_SECRET } from '../modules/demo-host/measurement.mjs';
9
12
 
10
13
  // `rer deploy --demo [--dry-run]`: the seed's hosted demo at demo.<domain>
11
14
  // (modules/demo-host), from deploy-demo.json. It writes .rer/demo-host/: the
12
15
  // front Worker, its wrangler.json and the container image folder (workerd
13
16
  // engine). --dry-run stops there, so `wrangler dev` can serve that folder with
14
17
  // Docker; a deploy runs `wrangler deploy` on it with the Turnstile secret and
15
- // the demo's record as the version message. `--remove --yes` removes what that
16
- // record proves is the demo's (modules/demo-host/remove.mjs).
18
+ // the demo's record as the version message, then waits for every demo host's
19
+ // certificate. On Turnstile's testing secret (a test host) it also sets the
20
+ // measurement token starts must carry, and says where it is. `--remove --yes` removes
21
+ // what that record proves is the demo's (modules/demo-host/remove.mjs).
17
22
  export async function demoHostCommand(project, options, store, signal, out, operation) {
18
23
  const read = async path => { try { return JSON.parse(await readFile(await contained(project.root, path), 'utf8')); } catch (error) { if (error instanceof RerError) throw error; throw invalid(`Missing or malformed ${path}.`); } };
19
24
  const app = await read(project.adapter.deploy?.declaration ?? 'deploy.json'), declaration = await read('deploy-demo.json');
@@ -23,11 +28,13 @@ export async function demoHostCommand(project, options, store, signal, out, oper
23
28
  catch (error) { throw invalid(`The demo deploy declaration is invalid (${error.message}). deploy-demo.json holds {"version": 1, "domain": "<run site apex>", "name": "<product name>"}, optionally "maxInstances", "instanceType" (${INSTANCE_TYPES.join(', ')}; default basic), "ownerEmail" (the demo owner's address; default owner@demo.invalid) and plain "settings".`); }
24
29
  if (options.remove) return removeDemo(plan, store, signal);
25
30
  if (process.env.RER_DEMO_OPERATOR_TOKEN && (process.env.RER_DEMO_OPERATOR_TOKEN.length < 32 || process.env.RER_DEMO_OPERATOR_TOKEN.length > 512)) throw invalid('RER_DEMO_OPERATOR_TOKEN must contain 32 to 512 characters.');
31
+ const supplied = process.env.RER_DEMO_MEASUREMENT_TOKEN?.trim();
32
+ if (supplied && (supplied.length < 32 || supplied.length > 512 || /[\s;,]/.test(supplied))) throw invalid('RER_DEMO_MEASUREMENT_TOKEN must contain 32 to 512 characters, with no spaces, semicolons or commas.');
26
33
  const folder = join(project.root, '.rer', 'demo-host');
27
34
  await rm(folder, { recursive: true, force: true });
28
35
  await mkdir(folder, { recursive: true, mode: 0o700 });
29
36
  await store.step('Building the demo container image (workerd engine)');
30
- await imageFolder(project, operation, { ...app, secrets: [...(app.secrets ?? []), { binding: 'RER_DEMO_OPERATOR_TOKEN' }, { binding: 'TURNSTILE_SECRET_KEY' }] }, { output: join(folder, 'image'), engine: 'workerd' });
37
+ await imageFolder(project, operation, { ...app, secrets: [...(app.secrets ?? []), { binding: 'RER_DEMO_OPERATOR_TOKEN' }, { binding: 'RER_DEMO_MEASUREMENT_TOKEN' }, { binding: 'TURNSTILE_SECRET_KEY' }] }, { output: join(folder, 'image'), engine: 'workerd' });
31
38
  await prepareDemoImage(join(folder, 'image'));
32
39
  for (const file of DEMO_FILES) await cp(new URL(`../modules/demo-host/${file}`, import.meta.url), join(folder, file));
33
40
  await writeFile(join(folder, 'wrangler.json'), JSON.stringify(plan.wrangler, null, 2) + '\n');
@@ -43,15 +50,34 @@ export async function demoHostCommand(project, options, store, signal, out, oper
43
50
  const names = ['CLOUDFLARE_ACCOUNT_ID', 'CLOUDFLARE_API_TOKEN', 'TURNSTILE_SITE_KEY', 'TURNSTILE_SECRET_KEY'];
44
51
  if (names.some(name => !process.env[name]?.trim())) throw missing(`${names.join(', ')} are required; see DEMO.md "Hosting the demo".`);
45
52
  if (!options.json) for (const line of human) out.human(line);
53
+ // Production hosts keep the real human check and never take a measurement token.
54
+ const measurement = TESTING_SECRET.test(process.env.TURNSTILE_SECRET_KEY.trim()) ? supplied || randomBytes(32).toString('base64url') : null;
46
55
  const secrets = join(folder, '.secrets.json');
47
- await writeFile(secrets, JSON.stringify({ TURNSTILE_SECRET_KEY: process.env.TURNSTILE_SECRET_KEY.trim(), ...(process.env.RER_DEMO_OPERATOR_TOKEN ? { RER_DEMO_OPERATOR_TOKEN: process.env.RER_DEMO_OPERATOR_TOKEN } : {}) }), { mode: 0o600 });
56
+ await writeFile(secrets, JSON.stringify({ TURNSTILE_SECRET_KEY: process.env.TURNSTILE_SECRET_KEY.trim(), ...(process.env.RER_DEMO_OPERATOR_TOKEN ? { RER_DEMO_OPERATOR_TOKEN: process.env.RER_DEMO_OPERATOR_TOKEN } : {}),
57
+ ...(measurement ? { RER_DEMO_MEASUREMENT_TOKEN: measurement } : {}) }), { mode: 0o600 });
48
58
  try {
49
59
  const require = createRequire(join(project.root, 'package.json'));
50
60
  const manifest = require.resolve('wrangler/package.json');
51
61
  const wrangler = join(dirname(manifest), JSON.parse(await readFile(manifest, 'utf8')).bin.wrangler);
52
62
  await store.run('Deploying the demo Worker and its container image', process.execPath, [wrangler, 'deploy', '--config', 'wrangler.json', '--secrets-file', '.secrets.json', '--message', plan.record], { cwd: folder, signal });
53
63
  } finally { await rm(secrets, { force: true }); }
54
- return { dryRun: false, deployed: true, origin: plan.origin, demo: summary };
64
+ await store.step(`Waiting for the certificate on ${plan.hostnames.join(', ')}`);
65
+ try { await awaitCertificates({ hostnames: plan.hostnames, report: line => store.report(line) }); }
66
+ catch (error) {
67
+ if (error.message !== 'demo-certificate-timeout') throw error;
68
+ throw new RerError('deploy_refused', `The demo is deployed, but its certificate is not active 10 minutes later (${error.hosts.join('; ')}). Run the deploy again to wait again; nothing needs removing.`, 1);
69
+ }
70
+ let measured;
71
+ if (measurement) {
72
+ // Output redaction masks credentials, so a token rer made goes to a private file.
73
+ const file = '.rer/demo-host/measurement-token';
74
+ if (!supplied) await writeFile(join(project.root, file), measurement + '\n', { mode: 0o600 });
75
+ measured = { cookie: MEASUREMENT_COOKIE, header: MEASUREMENT_HEADER, token: supplied ? 'RER_DEMO_MEASUREMENT_TOKEN' : file };
76
+ const how = `send it as the cookie ${MEASUREMENT_COOKIE}=<token> on ${plan.hostname} or the header ${MEASUREMENT_HEADER}: <token>`;
77
+ if (options.json) out.event('demo-measurement', measured);
78
+ else out.human(`Test host: it starts demos only with the measurement token in ${supplied ? 'RER_DEMO_MEASUREMENT_TOKEN' : file}; ${how}.`);
79
+ }
80
+ return { dryRun: false, deployed: true, origin: plan.origin, demo: summary, ...(measured ? { measurement: measured } : {}) };
55
81
  }
56
82
 
57
83
  async function removeDemo(plan, store, signal) {
@@ -0,0 +1,23 @@
1
+ import { readFile } from 'node:fs/promises';
2
+ import { deliveryDeclarationSources } from './delivery-declarations.mjs';
3
+ import { sourcePath } from '../modules/delivery-producer/migrations.mjs';
4
+ import { usesManagedMail } from '../modules/delivery-producer/mail.mjs';
5
+
6
+ // Managed-capable seeds can retain direct-provider adapters. Only declared
7
+ // mail credentials receive stand-ins; other application secrets still need
8
+ // their own deploy declaration, even when packaging lists them.
9
+ const credentials = new Set(['MAIL_API_KEY', 'MAIL_PROVIDER_API_KEY', 'RESEND_API_KEY', 'SENDGRID_API_KEY']);
10
+
11
+ export async function hostedMailCredentials(root) {
12
+ const names = new Set();
13
+ for (const [candidate, select] of deliveryDeclarationSources) {
14
+ const path = await sourcePath(root, candidate, { optional: true });
15
+ if (!path) continue;
16
+ const spec = select(JSON.parse(await readFile(path, 'utf8')));
17
+ if (!spec || !usesManagedMail(spec.mail)) continue;
18
+ for (const name of [...(spec.secretNames ?? []), ...(spec.optionalSecretNames ?? [])]) {
19
+ if (credentials.has(name)) names.add(name);
20
+ }
21
+ }
22
+ return [...names];
23
+ }
@@ -8,6 +8,7 @@ import { publicAssetPaths } from '../modules/deploy-kit/artifact.mjs';
8
8
  import { migrate } from '../modules/container/migrations.mjs';
9
9
  import { readMigrationRewrites, withRewrites } from '../modules/deploy-kit/migrations.mjs';
10
10
  import { RerError } from './errors.mjs';
11
+ import { hostedMailCredentials } from './hosted-mail.mjs';
11
12
 
12
13
  // Serve the production build the way rer deploy hosts it (assets behind the
13
14
  // Worker, deploy identity bound, a public https origin) on loopback, with no
@@ -40,8 +41,8 @@ async function miniflareFor(root) {
40
41
  const standIn = (item, field) => typeof item.verifyStandIn === 'string' && item.verifyStandIn ? item.verifyStandIn : field?.kind === 'email' ? 'owner@example.com' : typeof field?.default === 'string' && field.default ? field.default : randomBytes(24).toString('hex');
41
42
  const links = html => [...html.matchAll(/(?:src|href)=["'](\/[^"'#?\s]+)/g)].map(match => match[1].slice(1)).filter(path => !path.startsWith('/') && /\.[A-Za-z0-9]+$/.test(path));
42
43
 
43
- /** The build served as rer deploy hosts it, with the vars, origin and (stand-in) secrets of its
44
- * deploy declaration. `fetch(path, init)` answers for the deployed origin. */
44
+ /** The build served as rer deploy hosts it, with declared vars, origin and
45
+ * stand-in deploy/Managed mail secrets. `fetch(path, init)` uses the deployed origin. */
45
46
  export async function serveHosted(root, { declaration: declarationPath = 'deploy.json' } = {}) {
46
47
  const build = join(root, 'dist/server'), client = join(root, 'dist/client');
47
48
  let config;
@@ -56,6 +57,9 @@ export async function serveHosted(root, { declaration: declarationPath = 'deploy
56
57
  const modules = [entry, ...(await files(build)).map(path => join(build, path)).filter(path => path !== entry && moduleTypes[extname(path)])].map(path => ({ type: moduleTypes[extname(path)], path }));
57
58
  const identity = { sourceSha: createHash('sha1').update(randomBytes(16)).digest('hex'), fingerprint: createHash('sha256').update(randomBytes(16)).digest('hex'), versionId: crypto.randomUUID() };
58
59
  const secrets = Object.fromEntries((declaration.secrets ?? []).map(item => [item.binding, standIn(item, manifest.fields?.find(field => field.name === item.binding))]));
60
+ for (const binding of await hostedMailCredentials(root)) {
61
+ if (!Object.hasOwn(secrets, binding)) secrets[binding] = standIn({}, manifest.fields?.find(field => field.name === binding));
62
+ }
59
63
  const mf = new Miniflare(convertV4MiniflareOptions({ host: '127.0.0.1', port: 0, cf: false, workers: [{ name: 'ingress', modules: true, script: ingress, serviceBindings: { APP: 'app' } }, {
60
64
  name: 'app', modules, modulesRoot: build, compatibilityDate: config.compatibility_date, compatibilityFlags: config.compatibility_flags,
61
65
  bindings: { ...config.vars, ...declaration.vars, ...secrets, ...(declaration.originVar ? { [declaration.originVar]: ORIGIN } : {}),
@@ -1,5 +1,20 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.2.26
4
+
5
+ - The `rer` mail adapter carries `List-Id`, `List-Archive`, `List-Help`, `In-Reply-To` and `References` to the Managed relay as its `listId`, `listArchive`, `listHelp`, `inReplyTo` and `references` fields, and refuses invalid values before sending. Other raw headers stay refused.
6
+ - Hosted output takes Managed-mail credential stand-ins from the seed's mail spec when `mail.providers` includes `rer`, so a Managed seed no longer has to list them in `deploy.json`. `export/SEED-COMMAND-CONTRACT.md` documents the own-host and Managed contract.
7
+ - In a hosted demo, the app's fetches to its own demo hosts (primary and declared secondary) are served by its own container as the same visitor, with no network egress. Every other destination still goes through the demo egress.
8
+ - `rer deploy --demo` waits up to 10 minutes for each demo hostname to present a verified TLS certificate, and fails naming what each host last answered.
9
+ - Demo hosts on Turnstile testing keys start demos only when the start carries the deploy's measurement token, as the cookie `rer_demo_measure` or the header `x-rer-demo-measurement`. `rer deploy --demo` uses `RER_DEMO_MEASUREMENT_TOKEN` when it is set; otherwise it writes a token per deploy to `.rer/demo-host/measurement-token` (0600) and prints that path, not the token.
10
+ - Demo image builds accept `MAIL_RELAY_URL` as public Managed-mail configuration, alongside `MAIL_PROVIDER` and `MAIL_FROM`.
11
+
12
+ ### Upgrade notes
13
+
14
+ - Update the exact rer package pin, or refresh vendored operating commands, to receive these changes.
15
+ - A demo deployed to a test host (Turnstile testing keys) needs its measurement token to start demos: set the repository secret `RER_DEMO_MEASUREMENT_TOKEN`, which `run-demo.yml` passes to `rer deploy --demo`, and send it with each measurement start.
16
+ - Remove `MAIL_RELAY_URL` from `deploy.json` secrets and keep it as a public spec or Wrangler variable.
17
+
3
18
  ## 0.2.25
4
19
 
5
20
  - Invitations stay valid for seven days (`invitationSeconds`), separately from sign-in and step-up links, which keep their short lifetime.
@@ -35,6 +35,11 @@ in both directions, preserving text and binary messages, selected protocols,
35
35
  cookies and close codes. Refused upgrades retain their HTTP response; other
36
36
  upgrade protocols receive 400. Hosted demo routing selects the visitor's own
37
37
  container before forwarding the upgrade, including on declared secondary hosts.
38
+ In a hosted demo (`RER_DEMO=1` with `RER_INGRESS_ORIGIN`), the workerd demo
39
+ egress serves the app's own fetch of any of its ingress origins through this
40
+ container's ingress, never the network: the front admits only the visitor's
41
+ cookie, so this is how the app reads its own public pages. Other destinations
42
+ keep the demo egress and its refusals.
38
43
 
39
44
  The workerd source boundary permits Durable Object definitions, including export
40
45
  aliases and indirect inheritance. Unbound definitions create no namespace. The
@@ -68,10 +73,10 @@ SQLite stays on the Docker host's local filesystem; host-shared checkout mounts
68
73
  are not used for database storage. Back up with export before deleting a volume. Build
69
74
  uses the ordinary private-file custody and command admission boundary. Secrets
70
75
  are provided at runtime, never baked into the image or transferred in archives.
71
- Managed mail's `MAIL_PROVIDER` and `MAIL_FROM` are public configuration. Keep
72
- them in the mail spec's public variables and Wrangler `vars`, and remove them
73
- from `deploy.json` secrets. For migration, the image builder accepts those two
74
- legacy secret declarations when the managed-mail spec declares them public,
76
+ Managed mail's `MAIL_PROVIDER`, `MAIL_FROM` and `MAIL_RELAY_URL` are public
77
+ configuration. Keep them in the mail spec's public variables and Wrangler `vars`,
78
+ and remove them from `deploy.json` secrets. For migration, the image builder
79
+ accepts those legacy secret declarations when the managed-mail spec declares them public,
75
80
  and prints that correction. Demo runtime settings override their image defaults.
76
81
  API keys, tokens and other secrets in image variables remain refused.
77
82
 
@@ -6,6 +6,18 @@ const transports = new Set([
6
6
  ]);
7
7
  const mailPorts = new Set(['25', '465', '587', '2525']);
8
8
 
9
+ // The demo's own hosts (RER_INGRESS_ORIGIN and RER_INGRESS_ORIGINS, from
10
+ // deploy-demo.json) reach the front only with the visitor's cookie, so the
11
+ // app's own read of one would leave the container and meet the start page.
12
+ // SELF is this container's ingress: the same visitor's demo, no network.
13
+ const STRIPPED = ['cf-connecting-ip', 'x-forwarded-host', 'x-forwarded-proto', 'x-forwarded-for', 'forwarded', 'x-rer-original-url', 'x-rer-ingress-origin'];
14
+ function local(request, url) {
15
+ const headers = new Headers(request.headers);
16
+ for (const name of STRIPPED) headers.delete(name);
17
+ headers.set('cf-connecting-ip', '127.0.0.1'); headers.set('x-rer-original-url', url.href);
18
+ return new Request(request, { headers, redirect: 'manual' });
19
+ }
20
+
9
21
  function check(url) {
10
22
  const host = url.hostname.toLowerCase().replace(/\.$/, '');
11
23
  if (transports.has(host) || mailPorts.has(url.port)) throw new TypeError('Demo direct messaging transport refused');
@@ -17,10 +29,11 @@ export default {
17
29
  const redirect = request.redirect;
18
30
  for (let hops = 0; ; hops++) {
19
31
  const url = new URL(request.url);
20
- check(url);
32
+ const own = JSON.parse(env.RER_DEMO_ORIGINS ?? '[]').includes(url.origin);
33
+ if (!own) check(url);
21
34
  // Check every redirect before contacting its destination, including when
22
35
  // an otherwise public URL redirects into a messaging provider.
23
- const response = await env.NETWORK.fetch(new Request(request.clone(), { redirect: 'manual' }));
36
+ const response = own ? await env.SELF.fetch(local(request.clone(), url)) : await env.NETWORK.fetch(new Request(request.clone(), { redirect: 'manual' }));
24
37
  const location = response.headers.get('location');
25
38
  if (![301, 302, 303, 307, 308].includes(response.status) || location === null || redirect === 'manual') return response;
26
39
  await response.body?.cancel();
@@ -18,7 +18,7 @@ export async function checkImageVariables(root,vars,secrets) {
18
18
  const names=spec.publicVariableNames ?? Object.keys(spec.publicVariables ?? {});
19
19
  const publicVariables=Object.fromEntries(Object.entries(vars).filter(([name])=>names.includes(name)));
20
20
  validateMailSpec({...spec,publicVariables,secretNames:spec.secretNames ?? []});
21
- for (const name of ['MAIL_PROVIDER','MAIL_FROM']) if (typeof publicVariables[name] === 'string') publicMail.add(name);
21
+ for (const name of ['MAIL_PROVIDER','MAIL_FROM','MAIL_RELAY_URL']) if (typeof publicVariables[name] === 'string') publicMail.add(name);
22
22
  }
23
23
  for (const {binding} of overlaps) if (!publicMail.has(binding) || privateNames.has(binding)) {
24
24
  throw new Error('Secret declared as an image variable: '+binding);
@@ -20,8 +20,8 @@ export async function workerEngine({ root, data, manifest, settings, edge }) {
20
20
  const mf = new Miniflare(convertV4MiniflareOptions({ host:'127.0.0.1',port:0,cf:false,resourcePersistencePath:data,unsafeLocalExplorer:false,
21
21
  workers:[{ name:'ingress',modules:true,script:`export default {fetch(request,env){const observed=request.headers.get('x-rer-original-url');if(!observed)return new Response(null,{status:400});const url=new URL(observed),headers=new Headers(request.headers);headers.delete('x-rer-original-url');headers.set('host',url.host);return env.APP.fetch(new Request(url,{method:request.method,headers,redirect:'manual',...(!['GET','HEAD'].includes(request.method)?{body:request.body}:{})}))}}`,serviceBindings:{ APP:'app' } }, app,
22
22
  { name:'egress',modules:[{ type:'ESModule',path:join(import.meta.dirname,'demo-egress.mjs') }],modulesRoot:import.meta.dirname,
23
- bindings:{ RER_DEMO:settings.RER_DEMO ?? '' },
24
- serviceBindings:{ NETWORK:{ network:{ allow:['public'],tlsOptions:{ trustBrowserCas:true,trustedCertificates:[...rootCertificates] } } } } }],
23
+ bindings:{ RER_DEMO:settings.RER_DEMO ?? '', RER_DEMO_ORIGINS:JSON.stringify(settings.RER_DEMO === '1' && settings.RER_INGRESS_ORIGIN ? [settings.RER_INGRESS_ORIGIN, ...JSON.parse(settings.RER_INGRESS_ORIGINS ?? '[]')] : []) },
24
+ serviceBindings:{ SELF:'ingress', NETWORK:{ network:{ allow:['public'],tlsOptions:{ trustBrowserCas:true,trustedCertificates:[...rootCertificates] } } } } }],
25
25
  }));
26
26
  await mf.ready; const databases = {}, objects = {};
27
27
  for (const [binding, plan] of Object.entries(manifest.migrations)) { const db = await mf.getD1Database(binding, 'app'); databases[binding] = db; await migrate(db, plan); await ensurePortabilitySchema(db, manifest.portabilityMigrations); }
@@ -26,8 +26,11 @@ start page.
26
26
  held (at most 1.5 seconds) until the start changes, so the page learns of the
27
27
  sign-in at once; a slow status read answers pending. Each admission logs
28
28
  `demo_admitted` with the request's user agent and network owner, so a held
29
- place can be joined to whoever started it (automated browsers pass
30
- Turnstile's always-pass testing keys on test hosts). A request
29
+ place can be joined to whoever started it. On Turnstile's testing secret (a
30
+ test host) a start must also carry the deploy's measurement token
31
+ (`measurement.mjs`: cookie `rer_demo_measure` or header
32
+ `x-rer-demo-measurement`), since automated browsers pass those keys; a
33
+ test host without a token admits nobody. A request
31
34
  with that cookie goes to the visitor's Durable Object; nothing else reaches
32
35
  a container.
33
36
  - `container.mjs`: `DemoContainer`, one per visitor (`newUniqueId`, checked
@@ -48,6 +51,10 @@ start page.
48
51
  exact idle/absolute deadline without keeping a timer in flight. Requests
49
52
  check both deadlines before refreshing activity. A container found stopped
50
53
  ends the demo and answers the ended page (410).
54
+ - `certificate.mjs`: after `wrangler deploy`, the CLI waits for each demo
55
+ hostname's certificate, a TLS handshake that verifies for that name at the
56
+ address public DNS over HTTPS gives (10 minutes at most), and logs each
57
+ attempt. A failed or unverified handshake never counts as active.
51
58
  - `capacity.mjs`: `DemoCapacity` counts unexpired reservation rows inside a
52
59
  storage transaction, excluding diagnostics and expired rows. A lost start
53
60
  handoff expires after one minute; a successful claim extends the reservation
@@ -0,0 +1,56 @@
1
+ import { connect } from 'node:tls';
2
+
3
+ // A new Custom Domain answers TLS only once Cloudflare has issued its
4
+ // certificate: about 153 s on a fresh zone (dm-lanyard-1, October 2026), with
5
+ // browsers seeing ERR_SSL_VERSION_OR_CIPHER_MISMATCH meanwhile. After the
6
+ // deploy, wait on every demo hostname for a handshake that verifies the
7
+ // certificate for that name, so the deploy's readback and first visitors
8
+ // meet a working host. Addresses come from public DNS over HTTPS, so a local
9
+ // resolver's cached "no such host" for a new name cannot stall the wait. A
10
+ // failed or unverified handshake is never success: the wait ends in an error
11
+ // naming the last thing each host answered.
12
+ export async function awaitCertificates({ hostnames, report, resolve = publicAddress, port = 443, ca, timeout = 10 * 60_000, interval = 10_000, attemptTimeout = 10_000 }) {
13
+ const deadline = Date.now() + timeout, pending = new Set(hostnames), last = new Map();
14
+ for (let attempt = 1; ; attempt++) {
15
+ for (const hostname of pending) {
16
+ let seen;
17
+ try {
18
+ const address = await resolve(hostname, attemptTimeout);
19
+ if (!address) seen = 'not in public DNS yet';
20
+ else {
21
+ const certificate = await handshake({ address, hostname, port, ca, timeout: attemptTimeout });
22
+ pending.delete(hostname);
23
+ report(`TLS on ${hostname} verified (${certificate.protocol}, certificate valid to ${certificate.validTo}).`);
24
+ continue;
25
+ }
26
+ } catch (error) { seen = error.code ?? error.message; }
27
+ last.set(hostname, seen);
28
+ report(`Waiting for the certificate on ${hostname}: ${seen} (attempt ${attempt}).`);
29
+ }
30
+ if (!pending.size) return;
31
+ if (Date.now() + interval >= deadline) {
32
+ throw Object.assign(new Error('demo-certificate-timeout'), { hosts: [...pending].map(hostname => `${hostname}: ${last.get(hostname)}`) });
33
+ }
34
+ await new Promise(done => setTimeout(done, interval));
35
+ }
36
+ }
37
+
38
+ function handshake({ address, hostname, port, ca, timeout }) {
39
+ return new Promise((resolve, reject) => {
40
+ const socket = connect({ host: address, port, servername: hostname, rejectUnauthorized: true, ...(ca ? { ca } : {}), timeout });
41
+ socket.once('secureConnect', () => {
42
+ const certificate = socket.getPeerCertificate(), protocol = socket.getProtocol(), authorized = socket.authorized;
43
+ socket.destroy();
44
+ if (!authorized) reject(Object.assign(new Error('certificate not verified'), { code: socket.authorizationError ?? 'UNVERIFIED' }));
45
+ else resolve({ protocol, validTo: certificate.valid_to });
46
+ });
47
+ socket.once('timeout', () => { socket.destroy(); reject(Object.assign(new Error('handshake timed out'), { code: 'ETIMEDOUT' })); });
48
+ socket.once('error', reject);
49
+ });
50
+ }
51
+
52
+ async function publicAddress(hostname, timeout) {
53
+ const response = await fetch(`https://cloudflare-dns.com/dns-query?name=${encodeURIComponent(hostname)}&type=A`, { headers: { accept: 'application/dns-json' }, signal: AbortSignal.timeout(timeout) });
54
+ if (!response.ok) throw new Error(`public DNS answered ${response.status}`);
55
+ return (await response.json()).Answer?.find(record => record.type === 1)?.data ?? null;
56
+ }
@@ -0,0 +1,19 @@
1
+ import { equalSecret } from './operator.mjs';
2
+
3
+ // A test host runs on one of Cloudflare's Turnstile testing secrets, which pass
4
+ // (or fail) every visitor alike, so automated browsers took real places within
5
+ // minutes of a new certificate (gr-base-164). There a start must carry the
6
+ // deploy's measurement token, which `rer deploy --demo` hands the operator
7
+ // (RER_DEMO_MEASUREMENT_TOKEN, or .rer/demo-host/measurement-token): the cookie
8
+ // rer_demo_measure or the header x-rer-demo-measurement. A host with
9
+ // a real Turnstile secret never asks for it.
10
+ export const TESTING_SECRET = /^[123]x0{31}AA$/;
11
+ export const MEASUREMENT_COOKIE = 'rer_demo_measure', MEASUREMENT_HEADER = 'x-rer-demo-measurement';
12
+
13
+ export const testHost = env => TESTING_SECRET.test(env.TURNSTILE_SECRET_KEY ?? '');
14
+
15
+ /** Whether a start on a test host carries this deploy's token; a test host without one admits nobody. */
16
+ export async function measured(request, env) {
17
+ const cookie = (request.headers.get('cookie') ?? '').split(';').map(part => part.trim()).find(part => part.startsWith(`${MEASUREMENT_COOKIE}=`))?.slice(MEASUREMENT_COOKIE.length + 1);
18
+ return equalSecret(env.RER_DEMO_MEASUREMENT_TOKEN, request.headers.get(MEASUREMENT_HEADER) ?? cookie);
19
+ }
@@ -2,16 +2,21 @@ import { startPage } from './pages.mjs';
2
2
 
3
3
  // This credential belongs to the demo HOST operator, never a demo's signed-in
4
4
  // owner. It is supplied only as an Authorization header, never a URL/cookie.
5
- export async function operatorPage(request, env, correlation) {
6
- const expected = env.RER_DEMO_OPERATOR_TOKEN;
7
- const authorization = request.headers.get('authorization') ?? '';
8
- const supplied = authorization.startsWith('Bearer ') ? authorization.slice(7) : '';
9
- if (!expected || expected.length < 32 || !supplied || supplied.length > 512) return new Response('Unauthorized', { status: 401 });
5
+ /** A supplied credential against its secret, compared in constant time over their digests. */
6
+ export async function equalSecret(expected, supplied) {
7
+ if (!expected || expected.length < 32 || !supplied || supplied.length > 512) return false;
10
8
  const digest = value => crypto.subtle.digest('SHA-256', new TextEncoder().encode(value));
11
9
  const [a, b] = await Promise.all([digest(expected), digest(supplied)]);
12
10
  let unequal = 0; const av = new Uint8Array(a), bv = new Uint8Array(b);
13
11
  for (let i = 0; i < av.length; i++) unequal |= av[i] ^ bv[i];
14
- if (unequal) return new Response('Unauthorized', { status: 401 });
12
+ return unequal === 0;
13
+ }
14
+
15
+ export async function operatorPage(request, env, correlation) {
16
+ const expected = env.RER_DEMO_OPERATOR_TOKEN;
17
+ const authorization = request.headers.get('authorization') ?? '';
18
+ const supplied = authorization.startsWith('Bearer ') ? authorization.slice(7) : '';
19
+ if (!await equalSecret(expected, supplied)) return new Response('Unauthorized', { status: 401 });
15
20
  const record = await env.CAPACITY.get(env.CAPACITY.idFromName('diagnostics')).diagnostic(correlation);
16
21
  return startPage(env, { status: record ? 200 : 404, notice: record ? `Operator diagnostic: ${JSON.stringify(record)}` : 'No retained diagnostic for this start.' });
17
22
  }
@@ -9,7 +9,7 @@ import { siteDeclaration } from '../deploy-kit/site.mjs';
9
9
  * running the seed's own image (`rer build --platform container`, workerd
10
10
  * engine), deployed by `wrangler deploy` with the image beside it. */
11
11
 
12
- export const DEMO_FILES = ['worker.mjs', 'container.mjs', 'startup.mjs', 'placement.mjs', 'capacity.mjs', 'claim.mjs', 'pages.mjs', 'start-script.mjs', 'diagnostics.mjs', 'operator.mjs'];
12
+ export const DEMO_FILES = ['worker.mjs', 'container.mjs', 'startup.mjs', 'placement.mjs', 'capacity.mjs', 'claim.mjs', 'pages.mjs', 'start-script.mjs', 'diagnostics.mjs', 'operator.mjs', 'measurement.mjs'];
13
13
  const FRONT_SET = /^(?:RER_.*|SESSION_SECRET|SETUP_TOKEN|OWNER_EMAIL)$/;
14
14
  const SECRET = /SECRET|TOKEN|PASSWORD|_KEY$/;
15
15
  // Cloudflare Containers' predefined instance types, smallest first (developers.cloudflare.com/containers/platform-details/limits).
@@ -6,6 +6,7 @@
6
6
  // show its progress and failures; a plain form post gets the 303 or the page. Every request with that cookie goes to the visitor's
7
7
  // container; nothing else on this host is reachable.
8
8
  import { operatorPage } from './operator.mjs';
9
+ import { measured, testHost } from './measurement.mjs';
9
10
  import { bounded, category, report } from './diagnostics.mjs';
10
11
  import { startPage, demoCookie, clearCookie } from './pages.mjs';
11
12
 
@@ -80,6 +81,7 @@ async function start(request, env, ctx) {
80
81
  const correlation = crypto.randomUUID();
81
82
  const refuse = (status, notice, headers = {}) => refusal(request, env, status, notice, { 'x-rer-demo-correlation': correlation, ...headers });
82
83
  if (request.headers.get('origin') !== env.DEMO_ORIGIN) return refuse(403, 'Start the demo from this page.');
84
+ if (testHost(env) && !await measured(request, env)) return refuse(403, 'This is a test host: it starts demos only for its operator\'s measurement runs.');
83
85
  let demo;
84
86
  const started = Date.now();
85
87
  try {
@@ -161,8 +161,24 @@ values are refused. `List-Unsubscribe` (one or two `<https:…>`/`<mailto:…>`
161
161
  entries, at most 2,000 characters) travels as `listUnsubscribe`, and
162
162
  `List-Unsubscribe-Post: List-Unsubscribe=One-Click` as `listUnsubscribePost:
163
163
  true`, which requires an HTTPS entry. The structured `unsubscribe` request
164
- assembles the same headers. Other recipients, attachments, reply-to and every
165
- other header are unsupported and fail before dispatch.
164
+ assembles the same headers.
165
+
166
+ `List-Id`, `List-Archive`, `List-Help`, `In-Reply-To` and `References` travel as
167
+ `listId`, `listArchive`, `listHelp`, `inReplyTo` and `references`, respectively.
168
+ They match platform's relay validation at `098ba40` (`src/managed-mail/headers.ts`):
169
+ each is a string of at most 2,000 characters, without newlines or controls.
170
+ `List-Id` accepts an optional ASCII phrase or quoted display name followed by an
171
+ angle-bracketed ASCII dot-atom identifier containing a dot (at most 255
172
+ characters). `List-Archive` and `List-Help` accept one or two angle-bracketed
173
+ HTTPS or mailto URLs, using the relay's URL and mailbox checks. These three
174
+ headers allow separator tabs. `In-Reply-To` accepts exactly one `<local@domain>`
175
+ message ID with ASCII dot-atoms or a bracketed domain literal. `References`
176
+ accepts 1–20 such IDs separated by ASCII spaces, without leading/trailing
177
+ spaces, tabs or commas. Invalid values fail locally with `invalid_list_id`,
178
+ `invalid_list_archive`, `invalid_list_help`, `invalid_in_reply_to` or
179
+ `invalid_references`. Values are preserved; raw headers are never forwarded.
180
+ Other recipients, attachments, reply-to and every other header are unsupported
181
+ and fail before dispatch.
166
182
 
167
183
  HTTP 202 returns `accepted` with no provider message ID. HTTP 400, 401, 402,
168
184
  403 and 413 return `rejected`, and so does any 3xx (`provider_redirected`);
@@ -1,4 +1,5 @@
1
1
  import { fail } from '../../core/errors.mjs';
2
+ import { rerListHeaderField } from './list-headers.mjs';
2
3
 
3
4
  // The relay's own List-Unsubscribe rule: one or two <https:…>/<mailto:…> entries.
4
5
  const listEntries = /^[ \t]*<([^<>\s]+)>[ \t]*(?:,[ \t]*<([^<>\s]+)>[ \t]*)?$/;
@@ -17,7 +18,9 @@ export function rerHeaderFields(headers = {}) {
17
18
  const fields = {};
18
19
  for (const [name, value] of Object.entries(headers)) {
19
20
  const lower = name.toLowerCase();
20
- if (lower === 'auto-submitted') {
21
+ const field = rerListHeaderField(lower, value);
22
+ if (field) fields[field] = value;
23
+ else if (lower === 'auto-submitted') {
21
24
  if (value.trim().toLowerCase() !== 'auto-generated') fail('unsupported_provider_setting');
22
25
  } else if (lower === 'list-unsubscribe') fields.listUnsubscribe = value;
23
26
  else if (lower === 'list-unsubscribe-post') {
@@ -41,7 +44,7 @@ export function rerHeaderFields(headers = {}) {
41
44
  export function validateRerMessage(message) {
42
45
  if (message.to.length !== 1) fail('rer_one_recipient_required');
43
46
  // The relay cannot preserve these features. Refuse them rather than silently
44
- // dropping recipients, attachments or threading.
47
+ // dropping recipients or attachments.
45
48
  if (['cc', 'bcc', 'replyTo', 'attachments'].some(field => message[field] !== undefined)) fail('unsupported_provider_setting');
46
49
  rerHeaderFields(message.headers);
47
50
  if (!message.subject.trim() || message.subject.length > 200) fail('invalid_subject');
@@ -0,0 +1,44 @@
1
+ import { fail } from '../../core/errors.mjs';
2
+
3
+ // Match platform 098ba4034b80c1d704cf96398a41da94f2b879af:
4
+ // src/managed-mail/headers.ts:3-8,21-40,44-50 and src/platform/mail.ts:20.
5
+ const atom = "[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+";
6
+ const dotAtom = atom + '(?:\\.' + atom + ')*';
7
+ const word = atom + '|"(?:[\\x20-\\x21\\x23-\\x5b\\x5d-\\x7e]|\\\\[\\x20-\\x7e])*"';
8
+ const listIdPattern = new RegExp('^[ \\t]*(?:(?:' + word + ')(?:[ \\t]+(?:' + word + '))*[ \\t]*)?<(' + atom + '(?:\\.' + atom + ')+)>[ \\t]*$');
9
+ const messageIdPattern = new RegExp('^<' + dotAtom + '@(?:' + dotAtom + '|\\[[\\x21-\\x5a\\x5e-\\x7e]*\\])>$');
10
+ const controls = /[\r\n\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/;
11
+ const listEntries = /^[ \t]*<([^<>\s]+)>[ \t]*(?:,[ \t]*<([^<>\s]+)>[ \t]*)?$/;
12
+ const mailbox = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/;
13
+ const fields = new Map([
14
+ ['list-id', ['listId', 'invalid_list_id']],
15
+ ['list-archive', ['listArchive', 'invalid_list_archive']],
16
+ ['list-help', ['listHelp', 'invalid_list_help']],
17
+ ['in-reply-to', ['inReplyTo', 'invalid_in_reply_to']],
18
+ ['references', ['references', 'invalid_references']],
19
+ ]);
20
+
21
+ /** Validate a supported list/thread header and return its relay field, or null. */
22
+ export function rerListHeaderField(name, value) {
23
+ const mapping = fields.get(name.toLowerCase());
24
+ if (!mapping) return null;
25
+ const [field, code] = mapping;
26
+ if (typeof value !== 'string' || value.length > 2000 || controls.test(value)) fail(code);
27
+ if (field === 'listId') {
28
+ const match = listIdPattern.exec(value);
29
+ if (!match || match[1].length > 255) fail(code);
30
+ } else if (field === 'listArchive' || field === 'listHelp') {
31
+ const entries = value.match(listEntries);
32
+ if (!entries) fail(code);
33
+ for (const entry of entries.slice(1).filter(Boolean)) {
34
+ let url;
35
+ try { url = new URL(entry); } catch { fail(code); }
36
+ if (/^https:\/\//i.test(entry) && url.protocol === 'https:' && url.hostname && !entry.includes('\\')) continue;
37
+ if (url.protocol !== 'mailto:' || !mailbox.test(url.pathname)) fail(code);
38
+ }
39
+ } else {
40
+ const ids = field === 'references' ? value.split(/ +/) : [value];
41
+ if (!value || ids.length > (field === 'references' ? 20 : 1) || ids.some(id => !messageIdPattern.test(id))) fail(code);
42
+ }
43
+ return field;
44
+ }
@@ -1,6 +1,7 @@
1
1
  import { fail } from './errors.mjs';
2
2
  import { validateMessagingConfig, validateMessagingResult, identity, rfc3339 } from '../adapters/messaging/contract.mjs';
3
3
  import { validateRerConfig, validateRerMessage } from '../adapters/rer/contract.mjs';
4
+ import { rerListHeaderField } from '../adapters/rer/list-headers.mjs';
4
5
 
5
6
  const providers = ['sendgrid', 'resend', 'cloudflare', 'rer', 'development', 'messaging'];
6
7
  const controls = /[\x00-\x1f\x7f]/;
@@ -62,7 +63,7 @@ export function parseAddress(value) {
62
63
  return name ? { email, name } : { email };
63
64
  }
64
65
 
65
- export function validateHeaders(headers = {}) {
66
+ export function validateHeaders(headers = {}, provider) {
66
67
  record(headers, 'invalid_headers');
67
68
  const names = new Set();
68
69
  for (const name of Reflect.ownKeys(headers)) {
@@ -70,6 +71,8 @@ export function validateHeaders(headers = {}) {
70
71
  const lower = name.toLowerCase();
71
72
  if (reservedHeaders.has(lower) || names.has(lower)) fail('reserved_or_duplicate_header');
72
73
  names.add(lower);
74
+ // These relay grammars allow separator tabs and report field-specific errors.
75
+ if (provider === 'rer' && rerListHeaderField(lower, headers[name])) continue;
73
76
  bounded(headers[name], 8192, 'invalid_header_value', true);
74
77
  }
75
78
  return headers;
@@ -128,7 +131,7 @@ export function validateMessage(message, provider) {
128
131
  bounded(message.subject, 998, 'invalid_subject', true);
129
132
  for (const field of ['text', 'html']) if (message[field] !== undefined && typeof message[field] !== 'string') fail('invalid_body');
130
133
  if (!message.text?.trim() && !message.html?.trim()) fail('body_required');
131
- if (message.headers !== undefined) validateHeaders(message.headers);
134
+ if (message.headers !== undefined) validateHeaders(message.headers, provider);
132
135
  if (message.attachments !== undefined) {
133
136
  if (!Array.isArray(message.attachments)) fail('invalid_attachments');
134
137
  for (const attachment of message.attachments) validateAttachment(attachment);
@@ -26,7 +26,7 @@ export function createMailSender(config, dependencies = {}) {
26
26
  validateMessage(message, configured.provider);
27
27
  let snapshot;
28
28
  try { snapshot = structuredClone(message); } catch { fail('invalid_message'); }
29
- const prepared = applyUnsubscribeHeaders(snapshot);
29
+ const prepared = applyUnsubscribeHeaders(snapshot, configured.provider);
30
30
  validateMessage(prepared, configured.provider);
31
31
  return validateSendResult(await transport(configured, prepared, host), configured.provider);
32
32
  },
@@ -104,8 +104,8 @@ function addOrCheckHeader(headers, name, expected) {
104
104
  headers[name] = expected;
105
105
  }
106
106
 
107
- function assemble(message) {
108
- validateMessage(message);
107
+ function assemble(message, provider) {
108
+ validateMessage(message, provider);
109
109
 
110
110
  const headers = { ...(message.headers ?? {}) };
111
111
  validateExistingListHeaders(headers);
@@ -127,14 +127,14 @@ function assemble(message) {
127
127
 
128
128
  const prepared = { ...message, headers };
129
129
  delete prepared.unsubscribe;
130
- validateMessage(prepared);
130
+ validateMessage(prepared, provider);
131
131
  return prepared;
132
132
  }
133
133
 
134
134
  /** Return a copy with validated unsubscribe headers assembled and the request consumed. */
135
- export function applyUnsubscribeHeaders(message) {
135
+ export function applyUnsubscribeHeaders(message, provider) {
136
136
  try {
137
- return assemble(message);
137
+ return assemble(message, provider);
138
138
  } catch (error) {
139
139
  if (error instanceof MailValidationError) throw error;
140
140
  fail('invalid_message');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@runeditrun/rer",
3
- "version": "0.2.25",
3
+ "version": "0.2.26",
4
4
  "description": "The rer command: install, set up, run and launch Run Edit Run checkouts.",
5
5
  "license": "MIT",
6
6
  "type": "module",