@runeditrun/rer 0.2.22 → 0.2.23
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/README.md +19 -0
- package/cli/bounded.mjs +5 -1
- package/cli/delivery-declarations.mjs +2 -2
- package/cli/deploy-inputs.mjs +4 -1
- package/cli/deploy.mjs +4 -2
- package/cli/deployment-record.mjs +52 -0
- package/cli/local.mjs +11 -3
- package/cli/managed-server.mjs +2 -1
- package/cli/output.mjs +1 -1
- package/cli/production-auth.mjs +7 -27
- package/cli/production-mail.mjs +72 -0
- package/cli/signin.mjs +11 -6
- package/global/CHANGELOG.md +9 -0
- package/modules/container/README.md +8 -3
- package/modules/container/build.mjs +1 -1
- package/modules/container/host.mjs +7 -4
- package/modules/container/websockets.mjs +54 -0
- package/modules/container/worker-engine.mjs +2 -2
- package/modules/demo/README.md +4 -0
- package/modules/demo/label.mjs +7 -4
- package/modules/demo/responses.mjs +1 -1
- package/modules/demo-host/README.md +5 -2
- package/modules/demo-host/capacity.mjs +2 -2
- package/modules/demo-host/worker.mjs +9 -5
- package/modules/deploy-kit/owner-signin.mjs +1 -1
- package/modules/mail/README.md +14 -5
- package/modules/mail/src/adapters/rer/contract.mjs +33 -5
- package/modules/mail/src/adapters/rer/index.mjs +4 -1
- package/modules/mail/src/core/transport.mjs +9 -3
- package/modules/operator-auth/README.md +5 -0
- package/modules/operator-auth/identity/email-link/index.mjs +5 -0
- package/onboarding/local/operation.mjs +4 -2
- package/onboarding/local/owner.mjs +9 -3
- package/onboarding/local/state-files.mjs +17 -0
- package/onboarding/process-diagnostics.mjs +25 -5
- package/package.json +1 -1
- package/scripts/publish-check/README.md +5 -2
- package/scripts/publish-check/content_identities.py +1 -1
- package/scripts/publish-check/json_identity.py +10 -1
- package/scripts/publish-check/public_hygiene.py +30 -4
package/cli/README.md
CHANGED
|
@@ -125,6 +125,14 @@ rebuild preview. A build that regenerates its own files declares them as
|
|
|
125
125
|
`generatedOutputs` on the command adapter (see the seed command contract); those
|
|
126
126
|
paths are not inputs, while any other file touched during the build is refused by name. Both use the same `.wrangler/state` and saved owner. Dev opens signed in through `RER_OPEN`, including with the legacy
|
|
127
127
|
`--login` flag. Stop before changing modes.
|
|
128
|
+
|
|
129
|
+
Local startup holds the checkout's exclusive operation lock until serving stops.
|
|
130
|
+
Its state-holder inspection selects only regular files under `.wrangler/state`,
|
|
131
|
+
including SQLite sidecars. Dev and demo give `lsof` three seconds to answer. A confirmed
|
|
132
|
+
holder is refused with its PID and process name. If inspection cannot answer,
|
|
133
|
+
dev (including demo) continues under the held checkout lock; another
|
|
134
|
+
cooperating launch cannot acquire it. This lock does not inventory unmanaged
|
|
135
|
+
processes. Setup, reset and preview still require their state inspection to complete.
|
|
128
136
|
Stop dev or preview before build, verification or data transfer; use the
|
|
129
137
|
documented `--stop` flag or Ctrl-C.
|
|
130
138
|
|
|
@@ -181,6 +189,17 @@ secrets after the provider verifies the owned deployment. Local setup and demo
|
|
|
181
189
|
do not require production mail credentials. Preview serves production output
|
|
182
190
|
and prints its ready address without a captured-mail sign-in flow.
|
|
183
191
|
|
|
192
|
+
The production preflight also reads the product's `artifact.specInputs` in
|
|
193
|
+
`rer-packaging.json`, `packaging.json` or `scripts/delivery/base/packaging.json`,
|
|
194
|
+
and `management/integration/deployment-spec.json`. Public inputs come from
|
|
195
|
+
`publicVariableNames` or fixed `publicVariables`; credentials come from
|
|
196
|
+
`secretNames` and `optionalSecretNames`. Native `SENDGRID_API_KEY` and
|
|
197
|
+
`RESEND_API_KEY` satisfy their respective providers. When `MAIL_PROVIDER` is
|
|
198
|
+
`later` or absent, a declared native provider key plus a declared `MAIL_FROM`
|
|
199
|
+
resolves the deferred choice for preflight. An explicitly selected provider
|
|
200
|
+
still requires its own credentials. Undeclared environment values do not count;
|
|
201
|
+
the product's deploy adapter remains responsible for applying these settings.
|
|
202
|
+
|
|
184
203
|
Idle timeout is `--idle-timeout SECONDS`, then `RER_SERVER_IDLE_SECONDS`, then
|
|
185
204
|
43200 seconds (12 hours). Only positive integer seconds are accepted. HTTP requests,
|
|
186
205
|
including assets, reset inactivity; a live request/stream prevents idle
|
package/cli/bounded.mjs
CHANGED
|
@@ -66,7 +66,11 @@ export async function boundedCommand(project,options,argv,out){
|
|
|
66
66
|
out.error(new RerError('cancelled',`Stopped by ${termination} after owned children were stopped.`,exitCode));
|
|
67
67
|
out.result(false,exitCode);
|
|
68
68
|
}else if(finalEvent&&finalEvent.exitCode===exitCode){
|
|
69
|
-
if(errorEvent)
|
|
69
|
+
if(errorEvent){
|
|
70
|
+
let deployment={};
|
|
71
|
+
try{deployment=JSON.parse(store.redact(JSON.stringify({environment:errorEvent.environment,availableDeployments:errorEvent.availableDeployments})));}catch{}
|
|
72
|
+
out.error(new RerError(errorEvent.code,store.redact(errorEvent.message),exitCode,deployment));
|
|
73
|
+
}
|
|
70
74
|
const {version,verb,event,ok,exitCode:childExit,...details}=finalEvent;
|
|
71
75
|
// Redaction can cut through a value that matches a credential; the outcome stands without its details.
|
|
72
76
|
let shown;try{shown=JSON.parse(store.redact(JSON.stringify(details)));}catch{shown={};}
|
|
@@ -4,7 +4,7 @@ import { contained } from './project.mjs';
|
|
|
4
4
|
|
|
5
5
|
// Source policy and its committed integration projection. Inspect declarations
|
|
6
6
|
// without requiring a build or executing the seed's packaging code.
|
|
7
|
-
const
|
|
7
|
+
export const deliveryDeclarationSources = [
|
|
8
8
|
['rer-packaging.json', value => value?.artifact?.specInputs],
|
|
9
9
|
['packaging.json', value => value?.artifact?.specInputs],
|
|
10
10
|
['scripts/delivery/base/packaging.json', value => value?.artifact?.specInputs],
|
|
@@ -13,7 +13,7 @@ const sources = [
|
|
|
13
13
|
|
|
14
14
|
export async function deliveryDeclarationClauses(root) {
|
|
15
15
|
const clauses = [];
|
|
16
|
-
for (const [path, select] of
|
|
16
|
+
for (const [path, select] of deliveryDeclarationSources) {
|
|
17
17
|
let text;
|
|
18
18
|
try { text = await readFile(await contained(root, path, { optional: true }), 'utf8'); }
|
|
19
19
|
catch (error) {
|
package/cli/deploy-inputs.mjs
CHANGED
|
@@ -39,7 +39,10 @@ export async function withDeployEnvironment(root, options, run) {
|
|
|
39
39
|
const { env } = await deployEnvironment({ root, options, declaration: {} });
|
|
40
40
|
const { createRedaction, credentialValues } = await shared('redaction.mjs');
|
|
41
41
|
const redaction = createRedaction();
|
|
42
|
-
|
|
42
|
+
// Provider names are public enum values. Masking "resend" would also hide
|
|
43
|
+
// RESEND_API_KEY in the preflight's instruction for the missing credential.
|
|
44
|
+
const publicProvider = (key, value) => key === 'MAIL_PROVIDER' && /^(resend|sendgrid|cloudflare|rer|later)$/.test(value);
|
|
45
|
+
redaction.addSecrets([...credentialValues(env), ...Object.entries(env).filter(([key, value]) => saved[key] !== value && !publicProvider(key, value)).map(([, value]) => value)]);
|
|
43
46
|
Object.assign(process.env, env);
|
|
44
47
|
try { return await run(redaction); }
|
|
45
48
|
catch (error) { if (typeof error?.message === 'string') error.message = redaction.redact(error.message); throw error; }
|
package/cli/deploy.mjs
CHANGED
|
@@ -13,6 +13,7 @@ import { checkHostedSetup, hostedSetupFailure } from './deploy-setup.mjs';
|
|
|
13
13
|
import { mutationReadinessFailure } from './deploy-readiness.mjs';
|
|
14
14
|
import { committedSource } from './deploy-source.mjs';
|
|
15
15
|
import { hookState, forgetHookState } from './deploy-hook-record.mjs';
|
|
16
|
+
import { deploymentRecord } from './deployment-record.mjs';
|
|
16
17
|
|
|
17
18
|
export function ownerDeploy(declarationPath, lifecycle) {
|
|
18
19
|
// rer verify serves the build with this declaration's vars and secrets.
|
|
@@ -29,6 +30,7 @@ export function ownerDeploy(declarationPath, lifecycle) {
|
|
|
29
30
|
const retry = options.remove ? `${again} --remove --yes` : options.setSecret ? `${again} --set-secret ${options.setSecret}` : options.unsetSecret ? `${again} --unset-secret ${options.unsetSecret}` : again;
|
|
30
31
|
if (change.length > 1 || change.length && options.dryRun) throw invalid(`Use ${change.join(', ')}${options.dryRun ? ' and --dry-run' : ''} on separate runs.`);
|
|
31
32
|
if (options.remove && !options.yes) throw invalid(`${again} --remove deletes this environment's Worker and every D1, R2, KV and queue rer created for it, with their data. Add --yes to confirm.`);
|
|
33
|
+
const recordedState = change.length ? await deploymentRecord(root, options) : undefined;
|
|
32
34
|
// A dry run reads the settings file too, so a refused key fails here and not only on the real deploy.
|
|
33
35
|
if (options.dryRun && options.envFile) await deployEnvironment({ root, options, declaration, localOnlyKeys });
|
|
34
36
|
if (!change.length) {
|
|
@@ -62,7 +64,7 @@ export function ownerDeploy(declarationPath, lifecycle) {
|
|
|
62
64
|
let step = 'deploy', deployed;
|
|
63
65
|
try {
|
|
64
66
|
// A custom hook's own state file, when its deploy named one.
|
|
65
|
-
const state = await hookState(root, environment);
|
|
67
|
+
const state = change.length ? recordedState : await hookState(root, environment);
|
|
66
68
|
if (options.remove) {
|
|
67
69
|
step = 'remove';
|
|
68
70
|
const result = await owner('remove', state ? ['--state', state] : []);
|
|
@@ -118,7 +120,7 @@ function failureText({ error, step, created, again, retry, secretChange, deploye
|
|
|
118
120
|
const input = /^missing-secret-([A-Z][A-Z0-9_]*)$/.exec(code)?.[1];
|
|
119
121
|
if (input) return `Deploy needs ${input}, which the deploy declaration lists as a secret and the Worker does not hold it. Set ${input} in the environment or in an --env-file file (an owned 0600 file of KEY=VALUE lines), then run the same command again. Nothing was uploaded.${created.length ? ` Resources created this attempt: ${created.join(', ')}.` : ''}`;
|
|
120
122
|
if (code === 'remove-incomplete') return `Removal stopped with ${error.remaining.join(', ')} still present${error.failed.length ? ` (${error.failed.map(item => `${item.resource}: ${item.code}`).join('; ')})` : ''}. Removed: ${error.removed.join(', ') || 'nothing'}. Confirmed owned R2 buckets are emptied automatically; unproved buckets are never purged. Run ${again} --remove --yes again to finish.`;
|
|
121
|
-
if (code === 'nothing-to-remove') return `
|
|
123
|
+
if (code === 'nothing-to-remove') return `The deployment record selected by ${again} has no resources that rer owns, so --remove deletes nothing.`;
|
|
122
124
|
if (code === 'owner-token-retirement-failed') return `The application is deployed at ${deployed?.origin}; owner ${error.owner}; setup token retirement failed (${redact(String(error.cause))}). Run ${again} --unset-secret ${error.tokenKey ?? 'SETUP_TOKEN'} using the same environment credentials, or run ${again} again to finish.`;
|
|
123
125
|
if (code === 'owner-claim-failed') return `The application is deployed at ${deployed?.origin}, but claiming its owner failed: ${redact(String(error.cause))}. Owner claim and setup token retirement are not confirmed; run ${again} again using the same environment credentials to finish.`;
|
|
124
126
|
const resource = diagnostic?.resource ?? (error.resource ? redact(String(error.resource)) : undefined);
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { access, readFile, readdir } from 'node:fs/promises';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { hookState } from './deploy-hook-record.mjs';
|
|
4
|
+
import { deployAgain } from './deploy-inputs.mjs';
|
|
5
|
+
import { contained } from './project.mjs';
|
|
6
|
+
import { RerError } from './errors.mjs';
|
|
7
|
+
|
|
8
|
+
const recordPath = environment => join('.rer', 'deploy', `${environment}.json`);
|
|
9
|
+
|
|
10
|
+
// Existing-deployment commands share the same hook pointer and default record.
|
|
11
|
+
// A first deploy still creates its record through the kit as before.
|
|
12
|
+
export async function deploymentRecord(root, options) {
|
|
13
|
+
const environment = options.environment ?? 'production';
|
|
14
|
+
const state = await hookState(root, environment);
|
|
15
|
+
const path = await contained(root, state ?? recordPath(environment), { optional: true });
|
|
16
|
+
try { await access(path); }
|
|
17
|
+
catch (error) {
|
|
18
|
+
if (error.code !== 'ENOENT') throw error;
|
|
19
|
+
const availableDeployments = (await environments(root)).map(environment => ({ environment, command: commandFor(options, environment) }));
|
|
20
|
+
const held = availableDeployments.length
|
|
21
|
+
? `It holds:\n${availableDeployments.map(item => ` ${item.environment}: ${item.command}`).join('\n')}`
|
|
22
|
+
: 'This checkout holds no deployment environments. Run it from the checkout that deployed it.';
|
|
23
|
+
const signin = options.verb === 'signin';
|
|
24
|
+
throw new RerError(signin ? 'safety_refusal' : 'command_failed', `No deployment of the ${environment} environment in this checkout. ${held}`, signin ? 4 : 1, { environment, availableDeployments });
|
|
25
|
+
}
|
|
26
|
+
return state;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async function environments(root) {
|
|
30
|
+
let entries;
|
|
31
|
+
try { entries = await readdir(await contained(root, '.rer/deploy', { optional: true })); }
|
|
32
|
+
catch (error) { if (error.code === 'ENOENT') return []; throw error; }
|
|
33
|
+
const names = new Set(entries.flatMap(name => /^([a-z][a-z0-9]*(?:-[a-z0-9]+)*)(?:\.hook)?\.json$/.exec(name)?.slice(1) ?? []).filter(name => name.length <= 32));
|
|
34
|
+
const held = await Promise.all([...names].map(async environment => {
|
|
35
|
+
try {
|
|
36
|
+
const state = await hookState(root, environment);
|
|
37
|
+
const path = await contained(root, state ?? recordPath(environment));
|
|
38
|
+
const record = JSON.parse(await readFile(path, 'utf8'));
|
|
39
|
+
return record?.kind === 'rer-deploy-owner-state' && record.environment === environment ? environment : undefined;
|
|
40
|
+
} catch { return undefined; } // An unrelated or broken file is not a held deployment.
|
|
41
|
+
}));
|
|
42
|
+
return held.filter(Boolean).sort();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function commandFor(options, environment) {
|
|
46
|
+
const again = deployAgain({ ...options, environment });
|
|
47
|
+
if (options.verb === 'signin') return again.replace(/^rer deploy/, 'rer signin');
|
|
48
|
+
if (options.remove) return `${again} --remove --yes`;
|
|
49
|
+
if (options.setSecret) return `${again} --set-secret ${options.setSecret}`;
|
|
50
|
+
if (options.unsetSecret) return `${again} --unset-secret ${options.unsetSecret}`;
|
|
51
|
+
return again;
|
|
52
|
+
}
|
package/cli/local.mjs
CHANGED
|
@@ -98,12 +98,20 @@ export async function reset(project, options, out) {
|
|
|
98
98
|
return { paths: inventory };
|
|
99
99
|
}
|
|
100
100
|
|
|
101
|
-
export async function refuseLegacyProcess(project,{reset=false,verb='dev'}={}) {
|
|
101
|
+
export async function refuseLegacyProcess(project,{reset=false,verb='dev',operation}={}) {
|
|
102
102
|
let selected;try{selected=await selectedOrigin(project,{});}catch(error){if(!reset)throw error;}
|
|
103
103
|
// Confirmed reset can recover malformed settings; checkout/state custody is
|
|
104
104
|
// still checked even when there is no usable saved origin.
|
|
105
|
-
const {localProcessConflict,inspectionContext}=await shared('process-diagnostics.mjs');
|
|
105
|
+
const {localProcessConflict,inspectionContext,uncheckedInspection}=await shared('process-diagnostics.mjs');
|
|
106
106
|
// The front door holds a door origin's port, not this checkout.
|
|
107
|
-
|
|
107
|
+
let conflict;
|
|
108
|
+
try{conflict=await localProcessConflict(project.root,selected?.door?undefined:selected?.savedOrigin,inspectionContext({verb}));}
|
|
109
|
+
catch(error){
|
|
110
|
+
// Startup already holds the exclusive checkout operation through teardown.
|
|
111
|
+
// An unanswered observation must not defeat that cooperating-launch proof.
|
|
112
|
+
// Setup/reset retain their destructive-operation inspection requirement.
|
|
113
|
+
if(operation?.token&&verb==='dev'&&uncheckedInspection(error))return {lockProof:true};
|
|
114
|
+
throw error;
|
|
115
|
+
}
|
|
108
116
|
if(conflict)throw refuse(`A local process holds ${conflict.reason}: PID ${conflict.pid} (${conflict.command}). Stop it with kill -TERM ${conflict.pid}, then retry. Use rer dev --stop or rer preview --stop for a registered server.`);
|
|
109
117
|
}
|
package/cli/managed-server.mjs
CHANGED
|
@@ -85,9 +85,10 @@ export async function managedServer(project,options,out,{clock={}}={}){
|
|
|
85
85
|
throw Error('Unknown management operation');
|
|
86
86
|
});
|
|
87
87
|
await stateSafety(project);
|
|
88
|
-
await refuseLegacyProcess(project,{verb:options.verb});
|
|
89
88
|
const {localOperation}=await shared('local/operation.mjs');
|
|
90
89
|
try{operation=await localOperation(project.root);}catch(error){throw refuse(error.message);}
|
|
90
|
+
const ownership=await refuseLegacyProcess(project,{verb:options.verb,operation});
|
|
91
|
+
if(ownership?.lockProof)out.progress('Local process inspection did not answer; continuing with the exclusive checkout launch lock.\n');
|
|
91
92
|
const {recoverInterrupted}=await shared('local/recovery.mjs');await recoverInterrupted(project.root,operation);
|
|
92
93
|
const {loadInstallerSeed}=await shared('setup-manifest.mjs');
|
|
93
94
|
const seed=project.adapter.setup?await loadInstallerSeed(project.root,project.adapter):undefined;project.setupManifest=seed?.manifest;
|
package/cli/output.mjs
CHANGED
|
@@ -19,7 +19,7 @@ export function output(options) {
|
|
|
19
19
|
else if (!['describe', 'check', 'dev', 'preview', 'setup'].includes(options.verb) || !ok) this.human(`${options.verb}: ${ok ? 'complete' : 'failed'} (exit ${exitCode})`);
|
|
20
20
|
},
|
|
21
21
|
error(error) {
|
|
22
|
-
if (structuredResult) this.event('error', { code: error.code, message: error.message, exitCode: error.exitCode });
|
|
22
|
+
if (structuredResult) this.event('error', { code: error.code, message: error.message, exitCode: error.exitCode, ...(error.availableDeployments ? { environment: error.environment, availableDeployments: error.availableDeployments } : {}) });
|
|
23
23
|
else write(2, error.message + '\n');
|
|
24
24
|
},
|
|
25
25
|
};
|
package/cli/production-auth.mjs
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { refuse } from './errors.mjs';
|
|
2
|
-
import { mailbox } from './parse.mjs';
|
|
3
2
|
import { main as owner } from '../modules/deploy-kit/owner-cli.mjs';
|
|
4
3
|
import { hookState } from './deploy-hook-record.mjs';
|
|
5
4
|
import { savedSetupAnswers } from './setup-answers.mjs';
|
|
6
|
-
import {
|
|
5
|
+
import { productionMail } from './production-mail.mjs';
|
|
7
6
|
|
|
8
7
|
// Validate the shared email/identity contract using the bindings that will reach
|
|
9
8
|
// the runtime. Independently authored authentication remains the adapter's job.
|
|
@@ -11,26 +10,14 @@ export async function requireProductionAuth({ root, declaration, env, setupManif
|
|
|
11
10
|
const dependencies = setupManifest?.dependencies?.filter(item => item.interface === 'oidc-provider@1') ?? [];
|
|
12
11
|
const bindings = { ...declaration.vars };
|
|
13
12
|
for (const secret of declaration.secrets ?? []) if (env[secret.environment]?.trim()) bindings[secret.binding] = env[secret.environment];
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
if (provider === 'rer') {
|
|
19
|
-
try { validateRerConfig({ relayUrl: bindings.MAIL_RELAY_URL }); } catch { relaySelected = false; mailSelected = false; }
|
|
20
|
-
}
|
|
21
|
-
// Secrets arrive under the seed's declared runtime binding, which need not
|
|
22
|
-
// be the shared facade's MAIL_API_KEY (for example native SendGrid auth).
|
|
23
|
-
const credentialKeys = ['MAIL_API_KEY', ...(provider === 'sendgrid' ? ['SENDGRID_API_KEY'] : [])];
|
|
24
|
-
const declaredCredentials = credentialKeys.filter(key => declaration.secrets?.some(item => item.binding === key));
|
|
25
|
-
const required = provider === 'cloudflare' ? ['MAIL_ACCOUNT_ID'] : [];
|
|
26
|
-
const hasMailSecrets = live => credentialKeys.some(key => bindings[key]?.trim() || live.includes(key))
|
|
27
|
-
&& required.every(key => bindings[key]?.trim() || live.includes(key));
|
|
28
|
-
if (mailSelected && hasMailSecrets([])) return;
|
|
29
|
-
if (mailSelected && !options.dryRun && declarationPath) {
|
|
13
|
+
const mail = await productionMail(root, declaration, env);
|
|
14
|
+
if (!setupManifest?.steps?.some(step => step.type === 'owner-claim') && !dependencies.length && !mail.declared) return;
|
|
15
|
+
if (mail.configured()) return;
|
|
16
|
+
if (mail.canInspect && !options.dryRun && declarationPath) {
|
|
30
17
|
try {
|
|
31
18
|
const state = await hookState(root, options.environment ?? 'production');
|
|
32
19
|
const existing = await owner(['inspect', '--declaration', declarationPath, '--environment', options.environment ?? 'production', ...(state ? ['--state', state] : [])], env, { cwd: root, localOnlyKeys: adapter?.localOnlyKeys });
|
|
33
|
-
if (
|
|
20
|
+
if (mail.configured(existing.liveSecrets)) return;
|
|
34
21
|
} catch (error) {
|
|
35
22
|
if (!['missing-owner-receipt', 'missing-input'].includes(error.message)) throw refuse(`Production sign-in could not verify the existing deployment and its mail secrets${/^[a-z][a-z0-9-]{0,80}$/.test(error.message)?` (${error.message})`:''}. Restore provider access and the owned deployment record before retrying.`);
|
|
36
23
|
}
|
|
@@ -41,12 +28,5 @@ export async function requireProductionAuth({ root, declaration, env, setupManif
|
|
|
41
28
|
if (dependencies.some(item => {
|
|
42
29
|
try { const value = answers?.[item.address] ?? bindings[item.address], issuer = new URL(value); return issuer.protocol === 'https:' && issuer.origin === value && !issuer.username && !issuer.password; } catch { return false; }
|
|
43
30
|
})) return;
|
|
44
|
-
|
|
45
|
-
...(!['resend', 'sendgrid', 'cloudflare', 'rer'].includes(provider) ? ['MAIL_PROVIDER (resend, sendgrid, cloudflare or rer)'] : []),
|
|
46
|
-
...(!mailbox(bindings.MAIL_FROM) ? ['MAIL_FROM'] : []),
|
|
47
|
-
...(!credentialKeys.some(key => bindings[key]?.trim()) ? [declaredCredentials.join(' or ') || credentialKeys.join(' or ')] : []),
|
|
48
|
-
...required.filter(key => !bindings[key]?.trim()),
|
|
49
|
-
...(!relaySelected ? ['MAIL_RELAY_URL (assigned HTTPS relay URL)'] : []),
|
|
50
|
-
];
|
|
51
|
-
throw refuse(`Production sign-in is not configured. Configure outbound email; missing or invalid: ${missing.join(', ')}. Or select and approve an Auth SSO install through the declared oidc-provider@1 setup dependency. Local dev and demo use automatic RER_OPEN sign-in. Nothing was built or uploaded.`);
|
|
31
|
+
throw refuse(`Production sign-in is not configured. Configure outbound email; missing or invalid: ${mail.missing}. Or select and approve an Auth SSO install through the declared oidc-provider@1 setup dependency. Local dev and demo use automatic RER_OPEN sign-in. Nothing was built or uploaded.`);
|
|
52
32
|
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { contained } from './project.mjs';
|
|
3
|
+
import { refuse } from './errors.mjs';
|
|
4
|
+
import { mailbox } from './parse.mjs';
|
|
5
|
+
import { deliveryDeclarationSources } from './delivery-declarations.mjs';
|
|
6
|
+
import { validateRerConfig } from '../modules/mail/src/adapters/rer/contract.mjs';
|
|
7
|
+
|
|
8
|
+
const providers = ['resend', 'sendgrid', 'cloudflare', 'rer'];
|
|
9
|
+
const nativeCredential = { sendgrid: 'SENDGRID_API_KEY', resend: 'RESEND_API_KEY' };
|
|
10
|
+
const present = value => typeof value === 'string' && Boolean(value.trim());
|
|
11
|
+
|
|
12
|
+
// Packaging can declare inputs that a product's deploy adapter adds to the
|
|
13
|
+
// root declaration later. Read that source contract, never arbitrary ambient
|
|
14
|
+
// keys or the adapter's executable source, before any build or provider write.
|
|
15
|
+
export async function productionMail(root, declaration, env) {
|
|
16
|
+
const bindings = { ...declaration.vars }, secrets = new Set(), publicInputs = new Set(), declaredProviders = new Set();
|
|
17
|
+
for (const secret of declaration.secrets ?? []) {
|
|
18
|
+
secrets.add(secret.binding);
|
|
19
|
+
if (present(env[secret.environment])) bindings[secret.binding] = env[secret.environment];
|
|
20
|
+
}
|
|
21
|
+
for (const [path, select] of deliveryDeclarationSources) {
|
|
22
|
+
let text;
|
|
23
|
+
try { text = await readFile(await contained(root, path, { optional: true }), 'utf8'); }
|
|
24
|
+
catch (error) { if (error.code === 'ENOENT') continue; throw error; }
|
|
25
|
+
let spec;
|
|
26
|
+
try { spec = select(JSON.parse(text)); }
|
|
27
|
+
catch { throw refuse(`Restore a valid mail deployment declaration at ${path}. Nothing was built or uploaded.`); }
|
|
28
|
+
if (!spec) continue;
|
|
29
|
+
for (const name of spec.publicVariableNames ?? []) publicInputs.add(name);
|
|
30
|
+
for (const [name, value] of Object.entries(spec.publicVariables ?? {})) {
|
|
31
|
+
if (!(name in bindings) || name === 'MAIL_PROVIDER' && bindings[name] === 'later') bindings[name] = value;
|
|
32
|
+
}
|
|
33
|
+
for (const name of [...(spec.secretNames ?? []), ...(spec.optionalSecretNames ?? [])]) {
|
|
34
|
+
secrets.add(name);
|
|
35
|
+
// A root secret's explicit environment-to-binding mapping wins.
|
|
36
|
+
if (!declaration.secrets?.some(secret => secret.binding === name) && present(env[name])) bindings[name] = env[name];
|
|
37
|
+
}
|
|
38
|
+
for (const provider of spec.mail?.providers ?? []) if (providers.includes(provider)) declaredProviders.add(provider);
|
|
39
|
+
}
|
|
40
|
+
for (const name of publicInputs) if (present(env[name])) bindings[name] = env[name];
|
|
41
|
+
const provider = bindings.MAIL_PROVIDER;
|
|
42
|
+
const deferred = !provider || provider === 'later';
|
|
43
|
+
const choices = new Set(declaredProviders);
|
|
44
|
+
if (deferred) for (const [name, key] of Object.entries(nativeCredential)) if (secrets.has(key)) choices.add(name);
|
|
45
|
+
const selected = deferred ? [...choices] : providers.includes(provider) ? [provider] : [];
|
|
46
|
+
const routes = selected.map(provider => {
|
|
47
|
+
const credentialKeys = deferred && nativeCredential[provider] && !declaredProviders.has(provider) ? [nativeCredential[provider]]
|
|
48
|
+
: ['MAIL_API_KEY', ...(nativeCredential[provider] ? [nativeCredential[provider]] : [])];
|
|
49
|
+
const declaredCredentials = credentialKeys.filter(key => secrets.has(key));
|
|
50
|
+
return live => {
|
|
51
|
+
let relaySelected = true;
|
|
52
|
+
if (provider === 'rer') try { validateRerConfig({ relayUrl: bindings.MAIL_RELAY_URL }); } catch { relaySelected = false; }
|
|
53
|
+
const has = key => present(bindings[key]) || secrets.has(key) && live.includes(key);
|
|
54
|
+
return [
|
|
55
|
+
...(!mailbox(bindings.MAIL_FROM) ? ['MAIL_FROM'] : []),
|
|
56
|
+
...(!credentialKeys.some(has) ? [declaredCredentials.join(' or ') || credentialKeys.join(' or ')] : []),
|
|
57
|
+
...(provider === 'cloudflare' && !has('MAIL_ACCOUNT_ID') ? ['MAIL_ACCOUNT_ID'] : []),
|
|
58
|
+
...(!relaySelected ? ['MAIL_RELAY_URL (assigned HTTPS relay URL)'] : []),
|
|
59
|
+
];
|
|
60
|
+
};
|
|
61
|
+
});
|
|
62
|
+
return {
|
|
63
|
+
declared: 'MAIL_PROVIDER' in bindings || publicInputs.has('MAIL_PROVIDER') || secrets.has('MAIL_API_KEY') || selected.length > 0,
|
|
64
|
+
configured: (live = []) => routes.some(missing => missing(live).length === 0),
|
|
65
|
+
canInspect: mailbox(bindings.MAIL_FROM) && routes.length > 0,
|
|
66
|
+
missing: routes.length ? routes.map(missing => missing([]).join(', ')).join(' or ') : [
|
|
67
|
+
'MAIL_PROVIDER (resend, sendgrid, cloudflare or rer)',
|
|
68
|
+
...(!mailbox(bindings.MAIL_FROM) ? ['MAIL_FROM'] : []),
|
|
69
|
+
'MAIL_API_KEY',
|
|
70
|
+
].join(', '),
|
|
71
|
+
};
|
|
72
|
+
}
|
package/cli/signin.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { declarationFor } from './deploy-command.mjs';
|
|
2
2
|
import { deployEnvironment, deployAgain } from './deploy-inputs.mjs';
|
|
3
|
-
import {
|
|
4
|
-
import { missing, refuse, RerError } from './errors.mjs';
|
|
3
|
+
import { deploymentRecord } from './deployment-record.mjs';
|
|
4
|
+
import { invalid, missing, refuse, RerError } from './errors.mjs';
|
|
5
5
|
import { shared } from './shared.mjs';
|
|
6
6
|
import { main as owner } from '../modules/deploy-kit/owner-cli.mjs';
|
|
7
7
|
import { deployDiagnostic, providerRefusal } from '../modules/deploy-kit/diagnostics.mjs';
|
|
@@ -15,25 +15,30 @@ export async function signinCommand(project, options, out, { fetch } = {}) {
|
|
|
15
15
|
if (project.profile !== 'first-party' || typeof project.adapter?.deploy !== 'function') throw refuse('rer signin needs a seed with an owner-account deploy declaration.');
|
|
16
16
|
const root = project.root, environment = options.environment ?? 'production', again = deployAgain(options).replace(/^rer deploy/, 'rer signin');
|
|
17
17
|
const { path, value: declaration } = await declarationFor(root, project.adapter);
|
|
18
|
+
const state = await deploymentRecord(root, { ...options, verb: 'signin' });
|
|
18
19
|
const { env } = await deployEnvironment({ root, options, declaration, localOnlyKeys: project.adapter.localOnlyKeys ?? [] });
|
|
19
20
|
if (['CLOUDFLARE_ACCOUNT_ID', 'CLOUDFLARE_API_TOKEN'].some(name => !env[name]?.trim())) throw missing('rer signin uses the Cloudflare credentials that deployed this environment: set CLOUDFLARE_ACCOUNT_ID and CLOUDFLARE_API_TOKEN in the environment or an --env-file, then run it again.');
|
|
20
21
|
const { createRedaction, credentialValues } = await shared('redaction.mjs');
|
|
21
22
|
const redaction = createRedaction();
|
|
22
23
|
redaction.addSecrets([...credentialValues(env), env.CLOUDFLARE_ACCOUNT_ID]);
|
|
23
|
-
const state = await hookState(root, environment);
|
|
24
24
|
let result;
|
|
25
25
|
try { result = await owner(['signin', '--declaration', path, '--environment', environment, ...(state ? ['--state', state] : [])], env, { cwd: root, localOnlyKeys: project.adapter.localOnlyKeys ?? [], ...(fetch ? { fetch } : {}) }); }
|
|
26
|
-
catch (error) { throw failure(error, { environment, again, redact: redaction.redact }); }
|
|
26
|
+
catch (error) { throw failure(error, { environment, again, adapter: project.adapter, redact: redaction.redact }); }
|
|
27
27
|
const minutes = Math.round((result.expiresAt - Date.now()) / 60000);
|
|
28
28
|
if (!options.json) out.human(`One-time sign-in link for the owner ${result.email}. It works once, only for that owner, and expires in ${minutes} minutes. rer does not save it; open it now:\n${result.link}`);
|
|
29
29
|
return { owner: result.email, expiresAt: result.expiresAt, ...(options.json ? { link: result.link } : {}) };
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
function failure(error, { environment, again, redact }) {
|
|
32
|
+
function failure(error, { environment, again, adapter, redact }) {
|
|
33
33
|
const code = deployDiagnostic(error, redact)?.code ?? error.message;
|
|
34
34
|
if (code === 'missing-owner-receipt') return refuse(`This checkout has no completed rer deployment of the ${environment} environment. Run rer signin from the checkout that deployed it (rer deploy --environment ${environment}).`);
|
|
35
35
|
if (code === 'owned-resource-replaced') return refuse(`The ${environment} Worker is no longer the one this checkout deployed, so rer will not sign anyone in to it. Run rer deploy --environment ${environment} first.`);
|
|
36
|
-
if (code === 'owner-signin-unsupported')
|
|
36
|
+
if (code === 'owner-signin-unsupported') {
|
|
37
|
+
const path = adapter.signInPath;
|
|
38
|
+
if (path !== undefined && (typeof path !== 'string' || path.length > 2048 || !path.startsWith('/') || path.startsWith('//') || /[\\\s\u0000-\u001f\u007f-\u009f]/u.test(path))) return invalid('The adapter signInPath must be a same-origin browser path such as /login. Correct the seed declaration, then run rer signin again.');
|
|
39
|
+
const destination = new URL(path ?? '/setup', error.origin).href;
|
|
40
|
+
return refuse(redact(`The ${environment} deployment signs owners in through the seed's own sign-in flow. ${path ? `Open ${destination} to sign in.` : `Open ${destination} for the seed's owner recovery.`} rer signin cannot issue a link for this seed.`));
|
|
41
|
+
}
|
|
37
42
|
if (code === 'owner-signin-ambiguous') return refuse(`More than one D1 database of the ${environment} deployment holds an owner sign-in table, so rer cannot tell which one signs in the owner. Nothing was written.`);
|
|
38
43
|
if (code === 'owner-not-claimed') return refuse(`No owner has claimed the ${environment} deployment yet. Run rer deploy --environment ${environment}: it claims the owner. Nothing was written.`);
|
|
39
44
|
const refusal = providerRefusal(error, redact);
|
package/global/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.2.23
|
|
4
|
+
|
|
5
|
+
- Managed mail (`rer` provider) works from Cloudflare Workers. Mail transport always fetches with `redirect: 'manual'`, because Workers reject `redirect: 'error'`, which made every relay send end as `transport_uncertain` without a request. A relay 3xx is never followed and is reported as `rejected` (`provider_redirected`).
|
|
6
|
+
- The `rer` relay adapter accepts `Auto-Submitted: auto-generated`, which Base's sign-in and invitation mail sets, and does not transmit it: the platform relay sets it on every message. It maps `List-Unsubscribe` and one-click `List-Unsubscribe-Post` to the relay's `listUnsubscribe` and `listUnsubscribePost` fields. Values the relay would refuse are refused before sending, and every other header is still refused.
|
|
7
|
+
|
|
8
|
+
### Upgrade notes
|
|
9
|
+
|
|
10
|
+
- Mail transport now passes `redirect: 'manual'` to an injected `fetch`. A seed that wrapped fetch for the `rer` relay to get around the Workers redirect error must remove that wrapper when it refreshes `modules/mail` or pins 0.2.23. A wrapper that insists on `redirect: 'error'` (as Spool's `src/auth/mail-fetch.server.mjs` does) would turn every send into `uncertain`. A seed that dropped `Auto-Submitted` for the relay can send it again.
|
|
11
|
+
|
|
3
12
|
## 0.2.21
|
|
4
13
|
|
|
5
14
|
- Demo removal retries transient Cloudflare provider failures while preserving the resource ownership checks.
|
|
@@ -27,9 +27,14 @@ Other Cloudflare modules have no admitted host adapters; raw sockets would also
|
|
|
27
27
|
bypass the host's routed fetch egress. Native `email()` has no configured host
|
|
28
28
|
delivery, `caches.default` has no declared container cache contract, and the host
|
|
29
29
|
disables request CF metadata (`cf: false`). The module import rule admits native
|
|
30
|
-
ES module loading, not arbitrary calls carrying module strings.
|
|
31
|
-
|
|
32
|
-
WebSocket
|
|
30
|
+
ES module loading, not arbitrary calls carrying module strings.
|
|
31
|
+
|
|
32
|
+
The host forwards WebSocket upgrades through the same ingress and app session
|
|
33
|
+
checks as HTTP. An app response with status 101 and a `WebSocketPair` is bridged
|
|
34
|
+
in both directions, preserving text and binary messages, selected protocols,
|
|
35
|
+
cookies and close codes. Refused upgrades retain their HTTP response; other
|
|
36
|
+
upgrade protocols receive 400. Hosted demo routing selects the visitor's own
|
|
37
|
+
container before forwarding the upgrade, including on declared secondary hosts.
|
|
33
38
|
|
|
34
39
|
The workerd source boundary permits Durable Object definitions, including export
|
|
35
40
|
aliases and indirect inheritance. Unbound definitions create no namespace. The
|
|
@@ -85,7 +85,7 @@ async function buildImage(root, { output = join(root,'.rer/container/image'), en
|
|
|
85
85
|
}
|
|
86
86
|
const base = resolve(import.meta.dirname,'../..');
|
|
87
87
|
for (const path of ['modules/container','modules/portability','modules/d1-transport','modules/deploy-kit','cli/scheduled.mjs','cli/jsonc.mjs','modules/integrations/core/public-address.ts']) { await mkdir(join(output,'runtime',path,'..'),{recursive:true}); await cp(join(base,path),join(output,'runtime',path),{recursive:true,filter:path => !path.includes('/tests/')}); }
|
|
88
|
-
const dependencies = engine === 'workerd' ? { miniflare:'5.20260907.0-alpha' } : {};
|
|
88
|
+
const dependencies = { ws:'8.21.0', ...(engine === 'workerd' ? { miniflare:'5.20260907.0-alpha' } : {}) };
|
|
89
89
|
await writeFile(join(output,'runtime/package.json'),JSON.stringify({private:true,type:'module',dependencies}));
|
|
90
90
|
await writeFile(join(output,'Dockerfile'),`FROM node:26.8.1-bookworm-slim\nRUN apt-get update && apt-get install -y --no-install-recommends util-linux ca-certificates && apt-get clean\nRUN mkdir -p /private /archive\nWORKDIR /runtime\nCOPY runtime/package.json ./package.json\nRUN npm install --omit=dev --no-audit --no-fund\nCOPY runtime/ /runtime/\nCOPY app/ /app/\nENV NODE_ENV=production PORT=8080 WRANGLER_SEND_METRICS=false\nEXPOSE 8080\nVOLUME /data\nENTRYPOINT ["sh", "/runtime/modules/container/entrypoint.sh"]\n`);
|
|
91
91
|
return { output,manifest };
|
|
@@ -3,13 +3,14 @@ import { readFile, mkdir } from 'node:fs/promises';
|
|
|
3
3
|
import { join } from 'node:path';
|
|
4
4
|
import { recordVolumeOwner } from './lock.mjs';
|
|
5
5
|
import { incoming, outgoing } from './ingress.mjs';
|
|
6
|
+
import { attachWebSockets } from './websockets.mjs';
|
|
6
7
|
import { parseCron, cronMatches } from '../../cli/scheduled.mjs';
|
|
7
8
|
import { superviseRunner } from './runner.mjs';
|
|
8
9
|
export async function startHost({ root = '/app', data = '/data', settings = process.env, port = Number(process.env.PORT ?? 8080), edge, engineVersion = process.version, automaticTicks=true, runnerEnabled=true } = {}) {
|
|
9
10
|
await mkdir(data, { recursive:true });
|
|
10
11
|
const manifest = JSON.parse(await readFile(join(root, 'container.json'), 'utf8'));
|
|
11
12
|
const release = await recordVolumeOwner(data);
|
|
12
|
-
let engine, server, timer, idleTimer, runner, closing, lastMinute = -1, lastRequest=Date.now();
|
|
13
|
+
let engine, server, timer, idleTimer, runner, closing, closeWebSockets, lastMinute = -1, lastRequest=Date.now();
|
|
13
14
|
try {
|
|
14
15
|
const origins = settings.RER_INGRESS_ORIGINS === undefined ? undefined : JSON.parse(settings.RER_INGRESS_ORIGINS);
|
|
15
16
|
if (origins !== undefined && (!Array.isArray(origins) || origins.some(value => { try { const url = new URL(value); return typeof value !== 'string' || url.protocol !== 'https:' || url.origin !== value; } catch { return true; } }))) throw new Error('Invalid ingress origins');
|
|
@@ -17,10 +18,11 @@ export async function startHost({ root = '/app', data = '/data', settings = proc
|
|
|
17
18
|
engine = await create({ root,data,manifest,settings,edge });
|
|
18
19
|
const schedules = (manifest.config.triggers?.crons ?? []).map(cron => [cron,parseCron(cron)]);
|
|
19
20
|
const tick = async time => { for (const [cron, expression] of schedules) if (cronMatches(expression, new Date(time))) await engine.scheduled({ cron,scheduledTime:time }); await engine.drain(); };
|
|
21
|
+
const requestFrom = req => incoming(req, { trustedProxy:settings.TRUSTED_PROXY, origin:settings.RER_INGRESS_ORIGIN, origins, maxRequestBytes:manifest.maxRequestBytes });
|
|
20
22
|
server = createServer(async (req, res) => {
|
|
21
23
|
try {
|
|
22
24
|
lastRequest=Date.now();
|
|
23
|
-
const request = await
|
|
25
|
+
const request = await requestFrom(req);
|
|
24
26
|
let response = await engine.fetch(request);
|
|
25
27
|
if (new URL(request.url).pathname === '/health' && response.ok) {
|
|
26
28
|
const body = await response.json(); const deadJobs = engine.dead();
|
|
@@ -29,14 +31,15 @@ export async function startHost({ root = '/app', data = '/data', settings = proc
|
|
|
29
31
|
outgoing(response, res);
|
|
30
32
|
} catch (cause) { res.statusCode = cause.status ?? 500; res.end('Container request failed'); console.error('Container request failed:', cause.name); }
|
|
31
33
|
});
|
|
34
|
+
closeWebSockets = attachWebSockets(server, { engine, incoming:requestFrom, activity:() => { lastRequest=Date.now(); } });
|
|
32
35
|
await new Promise((resolve, reject) => { server.once('error', reject); server.listen(port,'0.0.0.0',resolve); });
|
|
33
36
|
let ticking=false;
|
|
34
37
|
if(automaticTicks){timer = setInterval(async () => { if(ticking)return;ticking=true;const minute = Math.floor(Date.now()/60000); try { if (minute !== lastMinute) { lastMinute = minute; await tick(minute * 60000); } else await engine.drain(); } catch { console.error('Container scheduled event failed'); }finally{ticking=false;} },1000); timer.unref();}
|
|
35
|
-
const close = () => closing ??= (async () => { clearInterval(timer); clearInterval(idleTimer); await runner?.close(); await new Promise(resolve => server.close(resolve)); await engine.close(); await release(); })();
|
|
38
|
+
const close = () => closing ??= (async () => { clearInterval(timer); clearInterval(idleTimer); await runner?.close(); closeWebSockets(); await new Promise(resolve => server.close(resolve)); await engine.close(); await release(); })();
|
|
36
39
|
const idle=Number(settings.RER_CONTAINER_IDLE_SECONDS);if(Number.isFinite(idle)&&idle>0){idleTimer=setInterval(()=>{if(Date.now()-lastRequest>=idle*1000)void close();},Math.min(1000,idle*1000));idleTimer.unref();}
|
|
37
40
|
if(runnerEnabled)runner=superviseRunner({root,manifest,settings,failed(){console.error('Required container runner stopped');void close().finally(()=>{process.exitCode=1;});}});
|
|
38
41
|
return { engine,manifest,server,tick,close };
|
|
39
|
-
} catch (cause) { clearInterval(timer); clearInterval(idleTimer); await runner?.close(); server?.close(); await engine?.close(); await release(); throw cause; }
|
|
42
|
+
} catch (cause) { clearInterval(timer); clearInterval(idleTimer); await runner?.close(); closeWebSockets?.(); server?.close(); await engine?.close(); await release(); throw cause; }
|
|
40
43
|
}
|
|
41
44
|
if (process.argv[1] === import.meta.filename) {
|
|
42
45
|
let host;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { ServerResponse } from 'node:http';
|
|
2
|
+
import { WebSocketServer } from 'ws';
|
|
3
|
+
import { outgoing } from './ingress.mjs';
|
|
4
|
+
|
|
5
|
+
// Keep the Worker response's cookies and selected protocol. ws owns the wire
|
|
6
|
+
// handshake (including key validation) and never negotiates compression here.
|
|
7
|
+
const handshakeHeaders = new Set(['connection', 'upgrade', 'sec-websocket-accept', 'sec-websocket-extensions', 'content-length', 'transfer-encoding']);
|
|
8
|
+
export function attachWebSockets(server, { engine, incoming, activity }) {
|
|
9
|
+
const responses = new WeakMap(), sockets = new Set();
|
|
10
|
+
const bridge = new WebSocketServer({ noServer:true, handleProtocols:() => false });
|
|
11
|
+
bridge.on('headers', (headers, req) => {
|
|
12
|
+
const response = responses.get(req); responses.delete(req);
|
|
13
|
+
for (const [name, value] of response.headers) if (!handshakeHeaders.has(name) && name !== 'set-cookie') headers.push(`${name}: ${value}`);
|
|
14
|
+
for (const cookie of response.headers.getSetCookie()) headers.push(`set-cookie: ${cookie}`);
|
|
15
|
+
});
|
|
16
|
+
const refuse = (req, socket, response) => {
|
|
17
|
+
const res = new ServerResponse(req); res.assignSocket(socket);
|
|
18
|
+
res.shouldKeepAlive = false;
|
|
19
|
+
const headers = new Headers(response.headers); headers.set('connection', 'close');
|
|
20
|
+
outgoing(new Response(response.body, { status:response.status, headers }), res);
|
|
21
|
+
};
|
|
22
|
+
server.on('upgrade', async (req, socket, head) => {
|
|
23
|
+
sockets.add(socket); socket.once('close', () => sockets.delete(socket));
|
|
24
|
+
// A peer may disappear while ingress or the Worker is still dispatching.
|
|
25
|
+
socket.on('error', () => socket.destroy());
|
|
26
|
+
let pair, coupled = false;
|
|
27
|
+
const abandon = () => { if (pair && !coupled) { try { pair.accept(); pair.close(1001, 'Client disconnected'); } catch {} } };
|
|
28
|
+
socket.once('close', abandon);
|
|
29
|
+
try {
|
|
30
|
+
activity();
|
|
31
|
+
if (req.headers.upgrade?.toLowerCase() !== 'websocket') return refuse(req, socket, new Response('Unsupported upgrade', { status:400 }));
|
|
32
|
+
const request = await incoming(req); request.headers.set('upgrade', 'websocket');
|
|
33
|
+
const response = await engine.fetch(request);
|
|
34
|
+
pair = response.webSocket;
|
|
35
|
+
if (socket.destroyed) { abandon(); return; }
|
|
36
|
+
if (response.status !== 101) return refuse(req, socket, response);
|
|
37
|
+
if (!pair || !engine.coupleWebSocket) return refuse(req, socket, new Response('Invalid WebSocket response', { status:502 }));
|
|
38
|
+
responses.set(req, response);
|
|
39
|
+
bridge.handleUpgrade(req, socket, head, ws => {
|
|
40
|
+
coupled = true;
|
|
41
|
+
ws.on('message', activity); ws.on('ping', activity); ws.on('pong', activity);
|
|
42
|
+
pair.addEventListener('message', activity);
|
|
43
|
+
// Miniflare's coupling preserves text/binary messages, queued app sends,
|
|
44
|
+
// close codes and abrupt disconnects on its returned WebSocketPair.
|
|
45
|
+
engine.coupleWebSocket(ws, pair).catch(() => { ws.terminate(); try { pair.close(1011, 'Bridge failed'); } catch {} });
|
|
46
|
+
});
|
|
47
|
+
} catch (cause) {
|
|
48
|
+
abandon();
|
|
49
|
+
if (!socket.destroyed) refuse(req, socket, new Response('Container upgrade failed', { status:cause.status ?? 500 }));
|
|
50
|
+
console.error('Container upgrade failed:', cause.name);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
return () => { for (const socket of sockets) socket.destroy(); bridge.close(); };
|
|
54
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Miniflare, convertV4MiniflareOptions } from 'miniflare';
|
|
1
|
+
import { Miniflare, convertV4MiniflareOptions, coupleWebSocket } from 'miniflare';
|
|
2
2
|
import { readdir } from 'node:fs/promises';
|
|
3
3
|
import { join, relative } from 'node:path';
|
|
4
4
|
import { rootCertificates } from 'node:tls';
|
|
@@ -27,7 +27,7 @@ export async function workerEngine({ root, data, manifest, settings, edge }) {
|
|
|
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); }
|
|
28
28
|
for (const bucket of config.r2_buckets ?? []) objects[bucket.binding] = await mf.getR2Bucket(bucket.binding, 'app');
|
|
29
29
|
const worker = await mf.getWorker('app');
|
|
30
|
-
return { mf, databases, objects, app:worker,
|
|
30
|
+
return { mf, databases, objects, app:worker, coupleWebSocket,
|
|
31
31
|
fetch(request) { const headers = new Headers(request.headers); headers.set('x-rer-original-url', request.url); return mf.dispatchFetch(request.url, { method:request.method,headers,body:request.body,duplex:'half',redirect:'manual' }); },
|
|
32
32
|
scheduled:event => worker.scheduled(event), async drain() {}, dead:() => 0, close:() => mf.dispose(),
|
|
33
33
|
};
|
package/modules/demo/README.md
CHANGED
|
@@ -18,6 +18,10 @@ Production build/deploy refuses that setting; ordinary builds keep real effects.
|
|
|
18
18
|
provider response should supply its own exact success shape this way.
|
|
19
19
|
- Mail in local dev and demo is silently suppressed. There is no outbox or
|
|
20
20
|
blocked-send UI. Logs contain only the mocked request's method and host.
|
|
21
|
+
- The configured owner's authenticated session supplies step-up proof inside
|
|
22
|
+
its own demo, including owner invitations and role changes. The demo guard
|
|
23
|
+
must be active; ordinary hosted instances still require real proof. Each
|
|
24
|
+
visitor's separate session secret and store reject other visitors' cookies.
|
|
21
25
|
- `seed(env, { ownerEmail, now })` runs once after the owner exists and must
|
|
22
26
|
be idempotent. Its only table is the demo's throwaway `rer_demo` marker.
|
|
23
27
|
- Base adds ` Demo` to the document title, including client-side navigation.
|
package/modules/demo/label.mjs
CHANGED
|
@@ -1,23 +1,26 @@
|
|
|
1
1
|
import { allow, sha256 } from '../operator-auth/csp.mjs';
|
|
2
2
|
|
|
3
3
|
// CSS leaves React's text alone during hydration. The title observer handles
|
|
4
|
-
// client-side route changes, whose head manager writes a fresh title.
|
|
4
|
+
// client-side route changes, whose head manager writes a fresh title. The script
|
|
5
|
+
// is one constant text (so one constant CSP hash); the suffix reaches it as data
|
|
6
|
+
// on the label's style element, never as code.
|
|
7
|
+
const SCRIPT = "(()=>{const suffix=document.querySelector('style[data-rer-label]').dataset.rerLabel,update=()=>{const title=document.title.replace(/ (?:Demo|Dev)$/,'')+' '+suffix;if(document.title!==title)document.title=title};update();new MutationObserver(update).observe(document.head,{childList:true,subtree:true,characterData:true})})()";
|
|
8
|
+
|
|
5
9
|
export async function decorateLabel(response, mode) {
|
|
6
10
|
if (!response.body || !response.headers.get('content-type')?.toLowerCase().startsWith('text/html')) return response;
|
|
7
11
|
const suffix = mode === 'Demo' ? 'Demo' : 'Dev';
|
|
8
12
|
const style = `[data-rer-name]::after{content:" ${suffix}"}`;
|
|
9
|
-
const script = `(()=>{const suffix=${JSON.stringify(suffix)},update=()=>{const title=document.title.replace(/ (?:Demo|Dev)$/,'')+' '+suffix;if(document.title!==title)document.title=title};update();new MutationObserver(update).observe(document.head,{childList:true,subtree:true,characterData:true})})()`;
|
|
10
13
|
const html = await response.text();
|
|
11
14
|
if (html.includes('data-rer-label=')) return new Response(html, response);
|
|
12
15
|
const headers = new Headers(response.headers);
|
|
13
16
|
let policy = headers.get('content-security-policy');
|
|
14
17
|
if (policy) {
|
|
15
18
|
policy = allow(policy, 'style-src', await sha256(style));
|
|
16
|
-
headers.set('content-security-policy', allow(policy, 'script-src', await sha256(
|
|
19
|
+
headers.set('content-security-policy', allow(policy, 'script-src', await sha256(SCRIPT)));
|
|
17
20
|
}
|
|
18
21
|
headers.delete('content-length');
|
|
19
22
|
headers.delete('etag');
|
|
20
|
-
const addition = `<style data-rer-label="${suffix}">${style}</style><script>${
|
|
23
|
+
const addition = `<style data-rer-label="${suffix}">${style}</style><script>${SCRIPT}</script>`;
|
|
21
24
|
const titled = html.replace(/(<title\b[^>]*>)([\s\S]*?)(<\/title>)/i, (_, open, title, close) => `${open}${title.replace(/ (?:Demo|Dev)$/, '')} ${suffix}${close}`);
|
|
22
25
|
const body = /<\/head\s*>/i.test(titled) ? titled.replace(/<\/head\s*>/i, `${addition}</head>`) : addition + titled;
|
|
23
26
|
return new Response(body, { status: response.status, statusText: response.statusText, headers });
|
|
@@ -7,7 +7,7 @@ export async function effectSuccess(request) {
|
|
|
7
7
|
if (host === 'api.resend.com') return json({ id });
|
|
8
8
|
if (host === 'api.twilio.com') return json({ sid: `SM${crypto.randomUUID().replaceAll('-', '')}`, status: 'queued', error_code: null, error_message: null }, 201);
|
|
9
9
|
if (host === 'api.postmarkapp.com') return json({ ErrorCode: 0, Message: 'OK', MessageID: id, SubmittedAt: new Date().toISOString() });
|
|
10
|
-
if (host.endsWith('mailgun.net')) return json({ id: `<${id}@demo.invalid>`, message: 'Queued. Thank you.' });
|
|
10
|
+
if (host === 'mailgun.net' || host.endsWith('.mailgun.net')) return json({ id: `<${id}@demo.invalid>`, message: 'Queued. Thank you.' });
|
|
11
11
|
if (host === 'slack.com') return json({ ok: true, channel: 'demo', ts: `${Math.floor(Date.now() / 1000)}.000001`, message: { type: 'message', text: 'Message sent' } });
|
|
12
12
|
if (host === 'hooks.slack.com' || host.endsWith('.webhook.office.com') || host.endsWith('.logic.azure.com')) return new Response(host === 'hooks.slack.com' ? 'ok' : '1');
|
|
13
13
|
if (host === 'api.stripe.com') {
|
|
@@ -45,7 +45,8 @@ start page.
|
|
|
45
45
|
storage transaction, excluding diagnostics and expired rows. A lost start
|
|
46
46
|
handoff expires after one minute; a successful claim extends the reservation
|
|
47
47
|
to the demo's end. Refusals log the held ids as SHA-256 fingerprints (also
|
|
48
|
-
logged with each start's correlation), never visitor bearer cookies.
|
|
48
|
+
logged with each start's correlation), never visitor bearer cookies. The
|
|
49
|
+
refusal's response correlation also identifies that held-place snapshot.
|
|
49
50
|
- `image.mjs`, `prepare.mjs`, `template.mjs`, `entrypoint.sh`: the demo image
|
|
50
51
|
build migrates fresh workerd persistence with the packaged engine and closes
|
|
51
52
|
it into `/template`, with no network and no app requests. The entrypoint
|
|
@@ -91,7 +92,9 @@ correlation. Every attempt logs its timestamp, elapsed time, duration and
|
|
|
91
92
|
HTTP status or fixed error category, including successful probes. Exit records
|
|
92
93
|
include the numeric platform exit code when supplied. A separate random
|
|
93
94
|
`x-rer-demo-correlation` response header joins these events without disclosing
|
|
94
|
-
the visitor's bearer cookie.
|
|
95
|
+
the visitor's bearer cookie. Early refusals, including capacity, rate-limit,
|
|
96
|
+
Origin and human-check refusals, also carry a correlation and log their status.
|
|
97
|
+
Worker observability also enables the platform's
|
|
95
98
|
private Container stdout/stderr logs; historical stdout is not exposed by the
|
|
96
99
|
Container lifecycle API. Read those logs with an account principal granted
|
|
97
100
|
Workers Observability Write, and filter by the application and boot interval.
|
|
@@ -18,7 +18,7 @@ export class DemoCapacity extends DurableObject {
|
|
|
18
18
|
}
|
|
19
19
|
async alarm() { await this.ctx.storage.deleteAll(); }
|
|
20
20
|
|
|
21
|
-
async acquire(id, ends, max) {
|
|
21
|
+
async acquire(id, ends, max, correlation) {
|
|
22
22
|
return this.ctx.storage.transaction(async storage => {
|
|
23
23
|
const now = Date.now(), places = [...await storage.list()]
|
|
24
24
|
.filter(([key, until]) => /^[0-9a-f]{64}$/.test(key) && typeof until === 'number');
|
|
@@ -28,7 +28,7 @@ export class DemoCapacity extends DurableObject {
|
|
|
28
28
|
if (held.length >= max) {
|
|
29
29
|
// Fingerprints identify held reservations without logging bearer cookies.
|
|
30
30
|
const heldIds = await Promise.all(held.map(async ([key, until]) => ({ id: await fingerprint(key), ends: until })));
|
|
31
|
-
console.log(JSON.stringify({ event: 'demo_capacity_refused', max, held: heldIds }));
|
|
31
|
+
console.log(JSON.stringify({ event: 'demo_capacity_refused', correlation, max, held: heldIds }));
|
|
32
32
|
return false;
|
|
33
33
|
}
|
|
34
34
|
await storage.put(id, ends);
|
|
@@ -41,7 +41,10 @@ async function human(env, token, ip) {
|
|
|
41
41
|
|
|
42
42
|
const wantsJSON = request => request.headers.get('accept') === 'application/json';
|
|
43
43
|
function refusal(request, env, status, notice, headers = {}) {
|
|
44
|
-
|
|
44
|
+
const correlation = headers['x-rer-demo-correlation'] ?? crypto.randomUUID();
|
|
45
|
+
headers = { 'cache-control': 'no-store', ...headers, 'x-rer-demo-correlation': correlation };
|
|
46
|
+
report(correlation, Date.now(), 'refused', { status });
|
|
47
|
+
return wantsJSON(request) ? Response.json({ notice }, { status, headers }) : startPage(env, { status, notice, headers });
|
|
45
48
|
}
|
|
46
49
|
|
|
47
50
|
function progress(request, env, id, state) {
|
|
@@ -69,9 +72,10 @@ async function status(request, env) {
|
|
|
69
72
|
}
|
|
70
73
|
|
|
71
74
|
async function start(request, env, ctx) {
|
|
72
|
-
const
|
|
75
|
+
const correlation = crypto.randomUUID();
|
|
76
|
+
const refuse = (status, notice, headers = {}) => refusal(request, env, status, notice, { 'x-rer-demo-correlation': correlation, ...headers });
|
|
73
77
|
if (request.headers.get('origin') !== env.DEMO_ORIGIN) return refuse(403, 'Start the demo from this page.');
|
|
74
|
-
let demo
|
|
78
|
+
let demo;
|
|
75
79
|
const started = Date.now();
|
|
76
80
|
try {
|
|
77
81
|
const ip = request.headers.get('cf-connecting-ip') ?? '';
|
|
@@ -89,10 +93,10 @@ async function start(request, env, ctx) {
|
|
|
89
93
|
const capacity = env.CAPACITY.get(env.CAPACITY.idFromName('capacity'));
|
|
90
94
|
// A lost handoff occupies a place for at most a minute. The object extends
|
|
91
95
|
// it to the demo's end only after health and owner admission succeed.
|
|
92
|
-
if (!await bounded(capacity.acquire(id.toString(), Date.now() + 60_000, Number(env.DEMO_MAX_INSTANCES)), 2000)) {
|
|
96
|
+
if (!await bounded(capacity.acquire(id.toString(), Date.now() + 60_000, Number(env.DEMO_MAX_INSTANCES), correlation), 2000)) {
|
|
93
97
|
return refuse(503, 'Every demo is in use right now. Try again in a few minutes.');
|
|
94
98
|
}
|
|
95
|
-
demo = env.DEMO.get(id);
|
|
99
|
+
demo = env.DEMO.get(id);
|
|
96
100
|
const state = await bounded(demo.begin({ correlation, origin: env.DEMO_ORIGIN, originVar: env.DEMO_ORIGIN_VAR, ends, endsLocal: localTime(ends, request.cf?.timezone), settings: JSON.parse(env.DEMO_SETTINGS) }), 2000);
|
|
97
101
|
return progress(request, env, id, { ...state, correlation });
|
|
98
102
|
} catch (error) {
|
|
@@ -18,7 +18,7 @@ export async function ownerSignin({ api, target, statePath, clock = Date.now })
|
|
|
18
18
|
const ids = Object.values(state.owned.d1).filter(entry => entry.status === 'created' && entry.id).map(entry => entry.id);
|
|
19
19
|
const holding = [];
|
|
20
20
|
for (const id of ids) if ((await query(api, id, "SELECT name FROM sqlite_master WHERE type='table' AND name='oa_owner_signins'"))[0].results.length) holding.push(id);
|
|
21
|
-
|
|
21
|
+
if (!holding.length) throw Object.assign(new Error('owner-signin-unsupported'), { origin: target.origin });
|
|
22
22
|
assert(holding.length === 1, 'owner-signin-ambiguous');
|
|
23
23
|
const token = randomBytes(32).toString('base64url'), id = randomUUID(), now = clock();
|
|
24
24
|
// The recorded owner: the claimed (bootstrap) owner first, as operator-auth's own owner() reads it.
|
package/modules/mail/README.md
CHANGED
|
@@ -148,17 +148,26 @@ await sender.send({
|
|
|
148
148
|
|
|
149
149
|
Pass `relayUrl` only for `rer`. It must be the assigned HTTPS URL, without URL
|
|
150
150
|
credentials, query or fragment. The adapter sends one POST with Bearer
|
|
151
|
-
authentication and `redirect: '
|
|
151
|
+
authentication and `redirect: 'manual'` (Cloudflare Workers reject
|
|
152
|
+
`redirect: 'error'`); a 3xx is never followed. Its JSON contains one mailbox `to`,
|
|
152
153
|
`subject`, `text` and optional nonempty `html`. RER owns the sender and
|
|
153
154
|
installation identity; `from` and `deliveryId` are not transmitted. Subjects
|
|
154
155
|
must be nonempty, without newlines and at most 200 characters. Plain text is
|
|
155
156
|
required; each body is limited to 65,536 characters and the JSON to 128 KiB.
|
|
156
|
-
|
|
157
|
-
|
|
157
|
+
|
|
158
|
+
The relay sets `Auto-Submitted: auto-generated` on every message, so the
|
|
159
|
+
adapter accepts that header and does not transmit it; other `Auto-Submitted`
|
|
160
|
+
values are refused. `List-Unsubscribe` (one or two `<https:…>`/`<mailto:…>`
|
|
161
|
+
entries, at most 2,000 characters) travels as `listUnsubscribe`, and
|
|
162
|
+
`List-Unsubscribe-Post: List-Unsubscribe=One-Click` as `listUnsubscribePost:
|
|
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.
|
|
158
166
|
|
|
159
167
|
HTTP 202 returns `accepted` with no provider message ID. HTTP 400, 401, 402,
|
|
160
|
-
403 and 413 return `rejected
|
|
161
|
-
`
|
|
168
|
+
403 and 413 return `rejected`, and so does any 3xx (`provider_redirected`);
|
|
169
|
+
429 returns `rate_limited`; 502 and 503 return `uncertain`. Network failures
|
|
170
|
+
are also `uncertain`.
|
|
162
171
|
The usual application delivery policy decides whether to retry or stop; the
|
|
163
172
|
adapter makes no retries and never reads or logs provider error bodies.
|
|
164
173
|
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { fail } from '../../core/errors.mjs';
|
|
2
2
|
|
|
3
|
+
// The relay's own List-Unsubscribe rule: one or two <https:…>/<mailto:…> entries.
|
|
4
|
+
const listEntries = /^[ \t]*<([^<>\s]+)>[ \t]*(?:,[ \t]*<([^<>\s]+)>[ \t]*)?$/;
|
|
5
|
+
const relayMailbox = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/;
|
|
6
|
+
|
|
3
7
|
export function validateRerConfig(config) {
|
|
4
8
|
let url;
|
|
5
9
|
try { url = new URL(config.relayUrl); } catch { fail('rer_relay_url_required'); }
|
|
@@ -8,14 +12,38 @@ export function validateRerConfig(config) {
|
|
|
8
12
|
url.href !== config.relayUrl) fail('invalid_rer_relay_url');
|
|
9
13
|
}
|
|
10
14
|
|
|
15
|
+
/** Relay request fields for the headers it supports; it sets Auto-Submitted: auto-generated itself. */
|
|
16
|
+
export function rerHeaderFields(headers = {}) {
|
|
17
|
+
const fields = {};
|
|
18
|
+
for (const [name, value] of Object.entries(headers)) {
|
|
19
|
+
const lower = name.toLowerCase();
|
|
20
|
+
if (lower === 'auto-submitted') {
|
|
21
|
+
if (value.trim().toLowerCase() !== 'auto-generated') fail('unsupported_provider_setting');
|
|
22
|
+
} else if (lower === 'list-unsubscribe') fields.listUnsubscribe = value;
|
|
23
|
+
else if (lower === 'list-unsubscribe-post') {
|
|
24
|
+
if (value !== 'List-Unsubscribe=One-Click') fail('invalid_list_unsubscribe_post');
|
|
25
|
+
fields.listUnsubscribePost = true;
|
|
26
|
+
} else fail('unsupported_provider_setting');
|
|
27
|
+
}
|
|
28
|
+
let https = false;
|
|
29
|
+
if (fields.listUnsubscribe !== undefined) {
|
|
30
|
+
const entries = fields.listUnsubscribe.length <= 2000 && fields.listUnsubscribe.match(listEntries);
|
|
31
|
+
if (!entries) fail('invalid_list_unsubscribe');
|
|
32
|
+
for (const entry of entries.slice(1).filter(Boolean)) {
|
|
33
|
+
if (/^https:\/\//i.test(entry)) https = true;
|
|
34
|
+
else if (!/^mailto:/i.test(entry) || !relayMailbox.test(entry.slice(7))) fail('invalid_list_unsubscribe');
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (fields.listUnsubscribePost && !https) fail('invalid_list_unsubscribe_post');
|
|
38
|
+
return fields;
|
|
39
|
+
}
|
|
40
|
+
|
|
11
41
|
export function validateRerMessage(message) {
|
|
12
42
|
if (message.to.length !== 1) fail('rer_one_recipient_required');
|
|
13
43
|
// The relay cannot preserve these features. Refuse them rather than silently
|
|
14
|
-
// dropping recipients, attachments
|
|
15
|
-
if (['cc', 'bcc', 'replyTo', 'attachments'
|
|
16
|
-
|
|
17
|
-
fail('unsupported_provider_setting');
|
|
18
|
-
}
|
|
44
|
+
// dropping recipients, attachments or threading.
|
|
45
|
+
if (['cc', 'bcc', 'replyTo', 'attachments'].some(field => message[field] !== undefined)) fail('unsupported_provider_setting');
|
|
46
|
+
rerHeaderFields(message.headers);
|
|
19
47
|
if (!message.subject.trim() || message.subject.length > 200) fail('invalid_subject');
|
|
20
48
|
if (!message.text?.trim() || message.text.length > 65_536 ||
|
|
21
49
|
(message.html !== undefined && message.html.length > 65_536)) fail('invalid_body');
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { validateConfig, validateMessage, parseAddress } from '../../core/validation.mjs';
|
|
2
2
|
import { fail } from '../../core/errors.mjs';
|
|
3
|
-
import { requestProvider, classifyHttpStatus, sendResult } from '../../core/transport.mjs';
|
|
3
|
+
import { requestProvider, redirected, classifyHttpStatus, sendResult } from '../../core/transport.mjs';
|
|
4
|
+
import { rerHeaderFields } from './contract.mjs';
|
|
4
5
|
|
|
5
6
|
/** One Managed relay submission. RER owns the sender and installation identity. */
|
|
6
7
|
export async function sendRer(config, message, dependencies = {}) {
|
|
@@ -12,6 +13,7 @@ export async function sendRer(config, message, dependencies = {}) {
|
|
|
12
13
|
subject: message.subject,
|
|
13
14
|
text: message.text,
|
|
14
15
|
...(message.html ? { html: message.html } : {}),
|
|
16
|
+
...rerHeaderFields(message.headers),
|
|
15
17
|
});
|
|
16
18
|
if (new TextEncoder().encode(body).byteLength > 128 * 1024) fail('rer_message_too_large');
|
|
17
19
|
const response = await requestProvider(config.relayUrl, {
|
|
@@ -21,6 +23,7 @@ export async function sendRer(config, message, dependencies = {}) {
|
|
|
21
23
|
body,
|
|
22
24
|
}, dependencies);
|
|
23
25
|
if (response === null) return sendResult('rer', 'uncertain', null, 'transport_uncertain');
|
|
26
|
+
if (response === redirected) return sendResult('rer', 'rejected', null, 'provider_redirected');
|
|
24
27
|
// Do not consume provider bodies: they may contain credentials or personal data.
|
|
25
28
|
if (response.status === 202) return sendResult('rer', 'accepted');
|
|
26
29
|
const status = classifyHttpStatus(response.status) ?? 'uncertain';
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { validateSendResult } from './validation.mjs';
|
|
2
2
|
|
|
3
|
-
/**
|
|
3
|
+
/** What a caller that asked for redirect: 'error' receives for any 3xx. */
|
|
4
|
+
export const redirected = Object.freeze({ redirected: true });
|
|
5
|
+
|
|
6
|
+
/** One bounded request. Fetch exceptions never escape with credential/body text.
|
|
7
|
+
* Workers reject redirect: 'error', so every request is manual and never follows. */
|
|
4
8
|
export async function requestProvider(url, init, dependencies = {}) {
|
|
5
9
|
const timeout = dependencies.timeoutMilliseconds ?? 15_000;
|
|
6
10
|
let timer;
|
|
@@ -8,8 +12,10 @@ export async function requestProvider(url, init, dependencies = {}) {
|
|
|
8
12
|
const deadline = new Promise(resolve => { timer = setTimeout(() => resolve(null), timeout); });
|
|
9
13
|
return await Promise.race([
|
|
10
14
|
Promise.resolve().then(() => (dependencies.fetch ?? globalThis.fetch)(url, {
|
|
11
|
-
...init, redirect:
|
|
12
|
-
})).then(response => response instanceof Response ?
|
|
15
|
+
...init, redirect: 'manual', signal: AbortSignal.timeout(timeout),
|
|
16
|
+
})).then(response => !(response instanceof Response) ? null
|
|
17
|
+
: init.redirect === 'error' && (response.type === 'opaqueredirect' || response.status >= 300 && response.status < 400) ? redirected
|
|
18
|
+
: response, () => null),
|
|
13
19
|
deadline,
|
|
14
20
|
]);
|
|
15
21
|
} catch { return null; }
|
|
@@ -425,6 +425,11 @@ owner invitation cancellation, and owner activation or deactivation. Unchanged
|
|
|
425
425
|
roles/statuses and non-owner status changes retain their usual authorization.
|
|
426
426
|
The existing version fences and last-active-owner protection still apply after
|
|
427
427
|
the proof succeeds.
|
|
428
|
+
With the `RER_DEMO=1` demo guard active, the configured owner's authenticated
|
|
429
|
+
session supplies this proof inside its own visitor container because mail is
|
|
430
|
+
mocked. Authentication still checks the container's own session secret and
|
|
431
|
+
store; another visitor's session is refused. Ordinary hosted instances keep
|
|
432
|
+
requiring the real proof described above.
|
|
428
433
|
Creating a server-declared owner dev persona also requires the signed-in owner's
|
|
429
434
|
fresh local proof in addition to the checkout key. Its creation repeats the actor
|
|
430
435
|
session/version fence in the database write.
|
|
@@ -59,6 +59,11 @@ export function createEmailLinkLifecycle({ config: input, clock = Date.now, mail
|
|
|
59
59
|
// Authentication already required the local key gate for this session.
|
|
60
60
|
// Dev confirms actions directly rather than sending an unusable mail link.
|
|
61
61
|
if (config.local && actor.kind === 'dev') return;
|
|
62
|
+
// The demo's mail is mocked. Its configured owner can confirm directly,
|
|
63
|
+
// after authentication against this visitor's isolated session store.
|
|
64
|
+
// Another demo's cookie cannot authenticate here; ordinary runtimes never
|
|
65
|
+
// install the demo guard, even if a caller supplies demo request headers.
|
|
66
|
+
if (demoEffectsEnabled() && actor.operator.role === 'owner' && actor.operator.email === config.OWNER_EMAIL) return;
|
|
62
67
|
if (!await store.steppedUp(actor, clock() - STEP_UP_SECONDS * 1000, clock())) deny('step_up_required', 403);
|
|
63
68
|
}
|
|
64
69
|
const authenticate = request => store.authenticate(request);
|
|
@@ -3,12 +3,14 @@ import { link, lstat, open, readFile, rename, unlink } from 'node:fs/promises'
|
|
|
3
3
|
import { join } from 'node:path'
|
|
4
4
|
import { privateDirectory } from './files.mjs'
|
|
5
5
|
import { processStart } from './process-table.mjs'
|
|
6
|
-
import {processName} from '../process-diagnostics.mjs'
|
|
6
|
+
import {processName,inspectionContext,uncheckedInspection} from '../process-diagnostics.mjs'
|
|
7
7
|
|
|
8
8
|
const busy = async (path) => {
|
|
9
9
|
let owner;try{owner=JSON.parse(await readFile(path,'utf8'));}catch{}
|
|
10
10
|
const pid=Number.isSafeInteger(owner?.pid)&&owner.pid>0?owner.pid:undefined;
|
|
11
|
-
|
|
11
|
+
let command='process';
|
|
12
|
+
if(pid)try{command=await processName(pid,inspectionContext({budgetMs:1000}));}catch(error){if(!uncheckedInspection(error))throw error;}
|
|
13
|
+
const identity=pid?`PID ${pid} (${command})`:'an unidentified process';
|
|
12
14
|
return Object.assign(new Error(`An active local operation owns the state: ${identity}. Stop it with rer dev --stop or rer preview --stop; for an unmanaged process, stop PID ${pid ?? 'shown in the lock'} before retrying.`), {code: 'LOCAL_OPERATION_BUSY', ownerPid: pid});
|
|
13
15
|
}
|
|
14
16
|
const graceMs = 10000
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import {readdir,lstat,realpath} from 'node:fs/promises';
|
|
2
2
|
import {basename,join} from 'node:path';
|
|
3
|
-
|
|
3
|
+
|
|
4
|
+
// Importing the owner-name helper must not load the database engine or emit
|
|
5
|
+
// its runtime warning before the launcher registers its worker.
|
|
6
|
+
async function openDatabase(path,options={}){
|
|
7
|
+
const {DatabaseSync}=await import('node:sqlite');
|
|
8
|
+
return new DatabaseSync(path,options);
|
|
9
|
+
}
|
|
4
10
|
|
|
5
11
|
// The owner the zero-setup `rer dev` gives a checkout before its real owner is known.
|
|
6
12
|
export function zeroSetupOwner(checkout){
|
|
@@ -21,7 +27,7 @@ export async function localOwners(root){
|
|
|
21
27
|
const found=[];
|
|
22
28
|
for(const path of await databases(root)){
|
|
23
29
|
let db,owners=[];
|
|
24
|
-
try{db=
|
|
30
|
+
try{db=await openDatabase(path,{readOnly:true});
|
|
25
31
|
const tables=new Set(db.prepare("SELECT name FROM sqlite_master WHERE type='table'").all().map(row=>row.name));
|
|
26
32
|
if(tables.has('oa_operators'))owners=db.prepare("SELECT email FROM oa_operators WHERE role='owner' AND status='active'").all();
|
|
27
33
|
}finally{db?.close();}
|
|
@@ -47,7 +53,7 @@ export async function assertLocalOwner(root,email,failure=message=>Error(message
|
|
|
47
53
|
// retires sign-in links issued to the placeholder.
|
|
48
54
|
export async function replaceLocalOwner(root,from,to,failure=message=>Error(message)){
|
|
49
55
|
for(const path of await databases(root)){
|
|
50
|
-
const db=
|
|
56
|
+
const db=await openDatabase(path);
|
|
51
57
|
try{
|
|
52
58
|
const tables=new Set(db.prepare("SELECT name FROM sqlite_master WHERE type='table'").all().map(row=>row.name));
|
|
53
59
|
if(!tables.has('oa_operators'))continue;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import {readdir} from 'node:fs/promises';
|
|
2
|
+
import {join} from 'node:path';
|
|
3
|
+
|
|
4
|
+
// Select only regular files in canonical state, including SQLite sidecars.
|
|
5
|
+
// Do not follow links into other checkouts or pass directories to lsof.
|
|
6
|
+
export async function stateFiles(directory) {
|
|
7
|
+
let entries;
|
|
8
|
+
try{entries=await readdir(directory,{withFileTypes:true});}
|
|
9
|
+
catch(error){if(error.code==='ENOENT')return [];throw error;}
|
|
10
|
+
const files=[];
|
|
11
|
+
for(const entry of entries){
|
|
12
|
+
const path=join(directory,entry.name);
|
|
13
|
+
if(entry.isDirectory())files.push(...await stateFiles(path));
|
|
14
|
+
else if(entry.isFile())files.push(path);
|
|
15
|
+
}
|
|
16
|
+
return files;
|
|
17
|
+
}
|
|
@@ -6,14 +6,15 @@ import {homedir} from 'node:os';
|
|
|
6
6
|
import {isIP} from 'node:net';
|
|
7
7
|
import {setTimeout as delay} from 'node:timers/promises';
|
|
8
8
|
import {commandFailure,approvedFailure} from './failures.mjs';
|
|
9
|
+
import {stateFiles} from './local/state-files.mjs';
|
|
9
10
|
const exec=promisify(execFile);
|
|
10
11
|
// macOS supplies lsof in /usr/sbin, which a command PATH may omit.
|
|
11
12
|
// Diagnose sockets with the system utility even when that PATH is constrained.
|
|
12
13
|
const lsof=process.platform==='darwin'?'/usr/sbin/lsof':'lsof';
|
|
13
14
|
// One monotonic budget covers every command in an ownership decision.
|
|
14
|
-
export const inspectionContext=(details={})=>({deadline:performance.now()+
|
|
15
|
+
export const inspectionContext=({budgetMs=15000,...details}={})=>({deadline:performance.now()+budgetMs,budgetMs,...details});
|
|
15
16
|
export const uncheckedInspection=error=>approvedFailure(error)?.ownershipUnchecked===true;
|
|
16
|
-
const run=async(command,args,context=inspectionContext(),{maxBuffer=1024*1024,noMatch=false}={})=>{
|
|
17
|
+
const run=async(command,args,context=inspectionContext(),{maxBuffer=1024*1024,noMatch=false,positiveResult}={})=>{
|
|
17
18
|
const invocation=[command,...args].join(' ').replaceAll(homedir(),'~');
|
|
18
19
|
let failure;
|
|
19
20
|
for(let attempt=0;attempt<2;attempt++){
|
|
@@ -22,6 +23,9 @@ const run=async(command,args,context=inspectionContext(),{maxBuffer=1024*1024,no
|
|
|
22
23
|
const timeout=attempt===0?Math.min(3000,remaining):remaining;
|
|
23
24
|
try{return (await exec(command,args,{timeout,maxBuffer})).stdout;}
|
|
24
25
|
catch(error){
|
|
26
|
+
// A selected state file positively observed before a timeout still has a
|
|
27
|
+
// holder. Partial output can never establish the absence of a holder.
|
|
28
|
+
if(typeof error.stdout==='string'&&positiveResult?.(error.stdout))return error.stdout;
|
|
25
29
|
// A quiet lsof exit 1 means no match, including a selected PID exiting
|
|
26
30
|
// while other selected PIDs still return records. Diagnostic output instead
|
|
27
31
|
// means inspection failed, even if lsof also returned partial records.
|
|
@@ -31,7 +35,7 @@ const run=async(command,args,context=inspectionContext(),{maxBuffer=1024*1024,no
|
|
|
31
35
|
}
|
|
32
36
|
const subject=context.port?`who uses port ${context.port}`:'local process ownership';
|
|
33
37
|
const cause=failure?.startsWith('timed out')||performance.now()>=context.deadline?'in time (system busy)':'because the system process check failed';
|
|
34
|
-
throw commandFailure(`Could not check ${subject} ${cause}. Run rer ${context.verb??'dev'} again. No other owner was identified.\nOwnership check: ${invocation} ${failure??'total inspection deadline exhausted'} (at most 2 attempts; shared total bound 15000 ms).`,{ownershipUnchecked:true});
|
|
38
|
+
throw commandFailure(`Could not check ${subject} ${cause}. Run rer ${context.verb??'dev'} again. No other owner was identified.\nOwnership check: ${invocation} ${failure??'total inspection deadline exhausted'} (at most 2 attempts; shared total bound ${context.budgetMs??15000} ms).`,{ownershipUnchecked:true});
|
|
35
39
|
};
|
|
36
40
|
|
|
37
41
|
export async function processName(pid,context=inspectionContext()){
|
|
@@ -46,6 +50,7 @@ function records(text,socketTypes=false){
|
|
|
46
50
|
let record,family;const rows=[];
|
|
47
51
|
for(const line of text.split('\n')){
|
|
48
52
|
if(line[0]==='p'){record={pid:Number(line.slice(1)),names:[],...(socketTypes?{sockets:[]}: {})};family=undefined;rows.push(record);}
|
|
53
|
+
else if(line[0]==='c'&&record)record.command=basename(line.slice(1)).replace(/[^a-zA-Z0-9._-]/g,'').slice(0,80);
|
|
49
54
|
else if(line[0]==='f')family=undefined;
|
|
50
55
|
else if(line[0]==='t')family=['IPv4','IPv6'].includes(line.slice(1))?line.slice(1):undefined;
|
|
51
56
|
else if(line[0]==='n'&&record){const name=line.slice(1);record.names.push(name);if(socketTypes)record.sockets.push({name,family});}
|
|
@@ -192,8 +197,23 @@ export async function localProcessConflict(root,savedOrigin,context=inspectionCo
|
|
|
192
197
|
let held;
|
|
193
198
|
if(hasState){
|
|
194
199
|
if(process.platform==='linux'){for(const pid of await linuxPids())if(!lineage.has(pid)&&(await descriptors(pid)).some(path=>path.startsWith(state+'/'))){held={pid};break;}}
|
|
195
|
-
else
|
|
196
|
-
|
|
200
|
+
else {
|
|
201
|
+
const files=await stateFiles(state);
|
|
202
|
+
// Path selection needs stat, so -b (which skips stat) is inappropriate here.
|
|
203
|
+
// No system-wide open-file output, and no query at all for empty state.
|
|
204
|
+
if(files.length){
|
|
205
|
+
const budgetMs=context.verb==='dev'?3000:context.budgetMs??15000;
|
|
206
|
+
const stateContext=inspectionContext({verb:context.verb,budgetMs,deadline:Math.min(context.deadline,performance.now()+budgetMs)});
|
|
207
|
+
const holder=text=>records(text).find(row=>!lineage.has(row.pid)&&row.names.some(path=>path.startsWith(state+'/')));
|
|
208
|
+
held=holder(await run(lsof,['-nP','-w','-Fpcn','--',...files],stateContext,{positiveResult:holder}));
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
if(held){
|
|
212
|
+
// A slow optional label cannot discard a positively observed holder.
|
|
213
|
+
let command=held.command;
|
|
214
|
+
if(!command)try{command=await processName(held.pid,context);}catch(error){if(!uncheckedInspection(error))throw error;}
|
|
215
|
+
return {pid:held.pid,command:command||'process',reason:'the canonical local state'};
|
|
216
|
+
}
|
|
197
217
|
}
|
|
198
218
|
let all;
|
|
199
219
|
try{all=await listeners(undefined,context);}
|
package/package.json
CHANGED
|
@@ -63,8 +63,11 @@ public report. Authoring's integrity checks remain unchanged.
|
|
|
63
63
|
against every declared file's bytes and regular-file mode in the same scanned
|
|
64
64
|
tree. Historical matches use only that commit's blobs; present files cannot
|
|
65
65
|
rescue a historical mismatch. Working-tree preimages reject linked paths.
|
|
66
|
-
|
|
67
|
-
|
|
66
|
+
Copy records use the same proof at custom JSON paths.
|
|
67
|
+
The captured secret must occur unambiguously inside the exact proved JSON value;
|
|
68
|
+
a native fragment ending partway through a digest qualifies too. The same value
|
|
69
|
+
in another field remains a finding. There is no filename, hash-value or
|
|
70
|
+
comma-delimited credential exemption.
|
|
68
71
|
|
|
69
72
|
`checksum_maps.py` classifies a file-name to digest map (`'0005_operator_auth.sql':
|
|
70
73
|
'<sha256>'`, in JSON, JavaScript, YAML or similar) in any file outside the
|
|
@@ -24,7 +24,7 @@ class ContentIdentities:
|
|
|
24
24
|
pointers = set(module_pins(path, document.value))
|
|
25
25
|
pointers.update(source_pointers(path, document.value))
|
|
26
26
|
pointers.update(controller_pointers(document.value))
|
|
27
|
-
if
|
|
27
|
+
if read_regular is not None:
|
|
28
28
|
pointers.update(copy_pointers(document.value, read_regular))
|
|
29
29
|
self.allowed[path] = pointers
|
|
30
30
|
|
|
@@ -67,13 +67,22 @@ class Document:
|
|
|
67
67
|
def matches(self, hit, pointers):
|
|
68
68
|
if hit.get('RuleID') not in ('generic-api-key', 'hex-api-key'):
|
|
69
69
|
return False
|
|
70
|
+
secret = hit.get('Secret', '').encode('utf-8')
|
|
71
|
+
if not secret:
|
|
72
|
+
return False
|
|
70
73
|
located = set()
|
|
71
74
|
for left, right in match_spans(self.data, hit):
|
|
75
|
+
start = self.data.find(secret, left, right)
|
|
76
|
+
if start < 0 or self.data.find(secret, start + 1, right) >= 0:
|
|
77
|
+
continue
|
|
72
78
|
for pointer in pointers:
|
|
73
79
|
span = self.spans.get(pointer)
|
|
74
80
|
if span:
|
|
75
81
|
a, b, value = span
|
|
76
|
-
|
|
82
|
+
# Native fragments can truncate a digest capture. Bind the
|
|
83
|
+
# captured secret's bytes, including a partial capture, to
|
|
84
|
+
# this exact unescaped JSON value.
|
|
85
|
+
if (a + 1 <= start and start + len(secret) <= b - 1
|
|
77
86
|
and self.data[a + 1:b - 1] == value.encode('ascii')):
|
|
78
87
|
located.add((a, b))
|
|
79
88
|
return len(located) == 1
|
|
@@ -12,11 +12,12 @@ from png_text import text_chunks
|
|
|
12
12
|
from media import sample_spans
|
|
13
13
|
from c2pa import certificate_spans
|
|
14
14
|
URI_AUTHORITY = re.compile(r'''//(?P<authority>[^/?#\s<>"']+)''')
|
|
15
|
+
ACTION_REFERENCE = re.compile(r'''uses:\s*["']?[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+@[^\s"']+''')
|
|
15
16
|
SCHEME_CHARACTERS = frozenset('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+.-')
|
|
16
17
|
# Match raw, complete commit trailer lines; decoded or embedded prose cannot
|
|
17
18
|
# acquire trailer privileges. Only the address span is omitted from email checks.
|
|
18
19
|
COAUTHOR = re.compile(
|
|
19
|
-
rb'^Co-Authored-By:[ \t]+[^<>\r\n]*[^<>\s][ \t]+<(?P<address>'
|
|
20
|
+
rb'^Co-Authored-By:[ \t]+(?=[^ \t])[^<>\r\n]*[^<>\s][ \t]+<(?P<address>'
|
|
20
21
|
rb'noreply@(?:anthropic|github)\.com|'
|
|
21
22
|
rb'[0-9]+\+[a-z0-9](?:[a-z0-9]|-(?=[a-z0-9])){0,38}@users\.noreply\.github\.com'
|
|
22
23
|
rb')>[ \t]*\r?$', re.I | re.M)
|
|
@@ -27,7 +28,7 @@ def without_public_coauthor_addresses(data):
|
|
|
27
28
|
# A quoted attribution followed by ordinary prose stays ordinary body text.
|
|
28
29
|
tail = data.rstrip(b' \t\r\n')
|
|
29
30
|
block = re.split(rb'\r?\n[ \t]*\r?\n', tail)[-1]
|
|
30
|
-
if not all(re.fullmatch(rb'[A-Za-z0-9-]+:[ \t]
|
|
31
|
+
if not all(re.fullmatch(rb'[A-Za-z0-9-]+:[ \t][^\r\n]+\r?', line)
|
|
31
32
|
for line in block.split(b'\n')):
|
|
32
33
|
return data
|
|
33
34
|
offset = len(tail) - len(block)
|
|
@@ -74,11 +75,36 @@ def private_emails(text, config):
|
|
|
74
75
|
email_text = re.sub(r'(?i)\b(?a:git@github\.com:)(?=[A-Za-z0-9]|\$\{[A-Za-z_$][A-Za-z0-9_.$]*\})', '', email_text)
|
|
75
76
|
# A package manager's patch filename joins the name and version with an at sign.
|
|
76
77
|
version = r'(?:v?[0-9]+(?:\.[0-9]+)*(?:-[A-Za-z0-9-]+)?|[a-f0-9]{40})(?:\.patch)?(?![A-Za-z0-9_.-])'
|
|
77
|
-
email_text =
|
|
78
|
-
|
|
78
|
+
email_text = without_action_references(email_text)
|
|
79
|
+
# Only the first character of a package token can change the outcome.
|
|
80
|
+
# Keep the outer boundary for scopes preceded by punctuation (x.@scope/pkg).
|
|
81
|
+
email_text = re.sub(r'(?<![A-Za-z0-9])(?:@[A-Za-z0-9_.-]+/)?(?<![A-Za-z0-9_.-])[A-Za-z0-9_.-]+(?:/[A-Za-z0-9_.-]+)?@' + version, '', email_text)
|
|
79
82
|
return [match.group() for match in EMAIL.finditer(email_text) if not public_email(match.group(), config)]
|
|
80
83
|
|
|
81
84
|
|
|
85
|
+
def without_action_references(text):
|
|
86
|
+
pieces, offset = [], 0
|
|
87
|
+
for match in ACTION_REFERENCE.finditer(text):
|
|
88
|
+
# Locate the literal first, then walk each whitespace gap once. A
|
|
89
|
+
# multiline ^\s*-?\s* retries and repartitions long blank-line runs.
|
|
90
|
+
start = match.start()
|
|
91
|
+
while start > offset and text[start - 1].isspace():
|
|
92
|
+
start -= 1
|
|
93
|
+
if start > offset and text[start - 1] == '-':
|
|
94
|
+
start -= 1
|
|
95
|
+
while start > offset and text[start - 1].isspace():
|
|
96
|
+
start -= 1
|
|
97
|
+
if start > 0 and text[start - 1] != '\n':
|
|
98
|
+
newline = text.find('\n', start, match.start())
|
|
99
|
+
if newline < 0:
|
|
100
|
+
continue
|
|
101
|
+
start = newline + 1
|
|
102
|
+
pieces.append(text[offset:start])
|
|
103
|
+
offset = match.end()
|
|
104
|
+
pieces.append(text[offset:])
|
|
105
|
+
return ''.join(pieces)
|
|
106
|
+
|
|
107
|
+
|
|
82
108
|
# A stored host name is lower case, upper case or capitalised, label by label.
|
|
83
109
|
# Bytes that merely decode to text, such as compressed pixels, rarely are.
|
|
84
110
|
HOST_LABEL = re.compile(r'(?:[a-z0-9-]+|[A-Z0-9-]+|[A-Z][a-z0-9-]*)')
|