@runeditrun/rer 0.2.70 → 0.2.71

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.
@@ -0,0 +1,25 @@
1
+ // Nested deploy/hook observers can see the same response. Retire its credential
2
+ // once, after the hook has finished using it, and share any retirement failure.
3
+ const retirements = new WeakMap();
4
+ export function retireOwnerClaimSession(fetch, response, origin) {
5
+ const cookies = response.headers.getSetCookie().map(cookie => cookie.split(';', 1)[0]);
6
+ if (!cookies.length) return;
7
+ if (!retirements.has(response)) retirements.set(response, signOut(fetch, origin, cookies.join('; ')));
8
+ return retirements.get(response);
9
+ }
10
+
11
+ async function signOut(fetch, origin, cookie) {
12
+ let response;
13
+ try {
14
+ response = await fetch(new URL('/auth/signout', origin), {
15
+ method: 'POST', redirect: 'manual', signal: AbortSignal.timeout(30000),
16
+ headers: { Origin: origin, 'Content-Type': 'application/json', Cookie: cookie }, body: '{}',
17
+ });
18
+ await response.body?.cancel();
19
+ if (response.status === 204 && !response.redirected) return;
20
+ } catch {}
21
+ const error = new Error('owner-claim-failed');
22
+ error.ownerSessionRetirement = true;
23
+ error.cause = `POST /auth/signout ${response ? `returned HTTP ${response.status}` : 'did not answer'}; the unused owner session could not be retired`;
24
+ throw error;
25
+ }
@@ -123,7 +123,7 @@ export async function deployCommand(context) {
123
123
  let deployed, claimed, linked = false, linkResult, step = 'custom deploy';
124
124
  const link = async (token, origin) => { if (!linked) { const state = await inspect(origin); linkResult = await linkDependencies?.(state?.origin ?? origin, token); linked = true; } return linkResult; };
125
125
  try {
126
- return await observeOwner({ paths: ['/auth/bootstrap', adapter.hooks?.owner?.path, setupManifest?.steps?.find(item => item.type === 'owner-claim')?.path].filter(Boolean), inspect, beforeClaim: link, addSecret, deployed: () => deployed, tokenKey: setupManifest?.steps?.find(item => item.type === 'owner-claim')?.tokenField }, async claims => {
126
+ const result = await observeOwner({ paths: ['/auth/bootstrap', adapter.hooks?.owner?.path, setupManifest?.steps?.find(item => item.type === 'owner-claim')?.path].filter(Boolean), inspect, beforeClaim: link, addSecret, deployed: () => deployed, tokenKey: setupManifest?.steps?.find(item => item.type === 'owner-claim')?.tokenField }, async claims => {
127
127
  observedClaims = claims;
128
128
  const saved = { ...process.env };
129
129
  try {
@@ -163,10 +163,11 @@ export async function deployCommand(context) {
163
163
  if (!linked) await linkDependencies?.(origin);
164
164
  step = 'verify hosted setup';
165
165
  await checkHostedSetup({ origin, claimed: claimed ?? state?.ownerClaim?.outcome ?? claims.at(-1)?.outcome, signal: context.signal });
166
- if (claimed) store.report(`${claimed === 'claimed' ? 'Claimed the hosted owner' : 'The hosted owner was already claimed and was kept'}${state ? '; the one-time setup token was removed' : ''}. Sign in by email at ${origin}.`);
167
166
  const { setupToken: _, ...result } = deployed;
168
167
  return { ...result, ...(claimed ? { owner: claimed } : {}), dependenciesLinked: true };
169
168
  });
169
+ if (claimed) store.report(`${claimed === 'claimed' ? 'Claimed the hosted owner' : 'The hosted owner was already claimed and was kept'}${recorded ? '; the one-time setup token was removed' : ''}. Sign in by email at ${result.origin}.`);
170
+ return result;
170
171
  } catch (error) {
171
172
  if (error instanceof DeployProductError) throw error;
172
173
  const again = deployAgain(options);
@@ -1,11 +1,12 @@
1
1
  import { observeReadiness, observedReadiness } from '../modules/deploy-kit/readiness-window.mjs';
2
2
  import { ownerClaimRequest } from './deploy-claim-request.mjs';
3
+ import { retireOwnerClaimSession } from './deploy-claim-session.mjs';
3
4
  // Observe the actual bootstrap exchange of a custom hook, including hooks that
4
5
  // call the shared owner helper themselves. Never issue a second claim after it.
5
6
  // The kit's deployment record, when the hook keeps one there, is checked before
6
7
  // linking with its live setup token; rer checks the origin the hook returns too.
7
8
  export async function observeOwner({ paths, inspect, beforeClaim, addSecret, deployed: knownDeployment, tokenKey }, run) {
8
- const fetch = globalThis.fetch, claims = [];
9
+ const fetch = globalThis.fetch, claims = [], sessions = [];
9
10
  globalThis.fetch = async (input, init = {}) => {
10
11
  const url = new URL(input instanceof Request ? input.url : input);
11
12
  let body;
@@ -23,6 +24,7 @@ export async function observeOwner({ paths, inspect, beforeClaim, addSecret, dep
23
24
  await beforeClaim?.(body.token, url.origin);
24
25
  const known = knownDeployment?.() ?? observedReadiness(url.origin);
25
26
  const response = await ownerClaimRequest(fetch, input, init, {deployed:known ?? deployed, tokenKey});
27
+ if (response.status === 303) sessions.push({ response, origin: url.origin });
26
28
  let code;
27
29
  if (response.status !== 303) {
28
30
  try { code = (await response.clone().json()).error; } catch {}
@@ -33,7 +35,10 @@ export async function observeOwner({ paths, inspect, beforeClaim, addSecret, dep
33
35
  return response;
34
36
  };
35
37
  try { return await observeReadiness(() => run(claims)); }
36
- finally { globalThis.fetch = fetch; }
38
+ finally {
39
+ globalThis.fetch = fetch;
40
+ await Promise.all(sessions.map(({ response, origin }) => retireOwnerClaimSession(fetch, response, origin)));
41
+ }
37
42
  }
38
43
 
39
44
  async function exchange(response,url,method) {
@@ -1,11 +1,12 @@
1
1
  import { ownerClaimRequest } from './deploy-claim-request.mjs';
2
+ import { retireOwnerClaimSession } from './deploy-claim-session.mjs';
2
3
  import { observeOwner, ownerExchangeFailure } from './deploy-observation.mjs';
3
4
  import { randomBytes } from 'node:crypto';
4
5
 
5
6
  // After a healthy deploy, the hosted app's first owner is the setup owner: rer
6
7
  // claims it with the one-time setup token through the seed's standard owner step
7
- // (Base operator-auth POST /auth/bootstrap), then removes that token from the
8
- // Worker and records the new Worker version. A Worker still holding a token rer
8
+ // (Base operator-auth POST /auth/bootstrap), signs out its unused session, then
9
+ // removes that token from the Worker and records the new Worker version. A Worker still holding a token rer
9
10
  // did not upload this run gets a fresh one first, unless the person supplied the
10
11
  // token (environment or --env-file) and inspection has not found it absent. A
11
12
  // seed with its own owner step function runs its own bootstrap; identity-provider
@@ -60,7 +61,7 @@ export async function claimHostedOwner({ hook, setupManifest, declaration, deplo
60
61
  } catch (cause) {
61
62
  const exchange = exchanges.lastExchange ?? exchanges.at(-1);
62
63
  const error = cause.message === 'owner-claim-failed' ? cause : ownerFailure(cause.message);
63
- if (exchange) { error.ownerExchange = exchange; error.cause = ownerExchangeFailure(exchange); }
64
+ if (exchange && !cause.ownerSessionRetirement) { error.ownerExchange = exchange; error.cause = ownerExchangeFailure(exchange); }
64
65
  throw error;
65
66
  }
66
67
  // Function hooks can keep existing owners without returning a value. Observe
@@ -76,7 +77,7 @@ export async function claimHostedOwner({ hook, setupManifest, declaration, deplo
76
77
  headers: { Origin: deployed.origin, 'Content-Type': 'application/json' }, body: JSON.stringify({ token, displayName, returnTo: hook.returnTo ?? '/' }) }, {deployed, tokenKey, signal});
77
78
  } catch (error) { throw ownerFailure(`POST ${new URL(hook.path ?? '/auth/bootstrap',deployed.origin).pathname}: ` + (['owner-claim-origin-mismatch','owner-claim-redirected','owner-claim-readiness-timeout'].includes(error.message) ? error.message : 'the hosted application did not answer')); }
78
79
  if (response.status !== 303) { try { const body = await response.json(); if (typeof body?.error === 'string' && /^[a-z_]{1,64}$/.test(body.error)) code = body.error; } catch {} }
79
- else await response.body?.cancel();
80
+ else { await retireOwnerClaimSession(fetch, response, deployed.origin); await response.body?.cancel(); }
80
81
  const outcome = response.status === 303 ? 'claimed' : response.status === 409 && code === 'already_initialized' ? 'kept' : undefined;
81
82
  const advice = code !== 'invalid_setup_token' ? '' : !secret ? `; the Worker holds another setup token: have the deploy hook return the one it deployed as setupToken, or set it as ${item.environment} with --env-file` : given ? `; the ${item.environment} you supplied is not the deployed setup token: leave it unset and rer issues a fresh one` : '';
82
83
  if (!outcome) throw ownerFailure(`POST ${new URL(hook.path ?? '/auth/bootstrap',deployed.origin).pathname} returned HTTP ${response.status} (${code})${advice}`);
@@ -4,6 +4,7 @@ import { readLegacyArchive } from '../modules/portability/format.mjs';
4
4
  import { recoverLegacyArchive } from '../modules/portability/legacy-files.mjs';
5
5
  import { legacyTargetState, requireLegacyEmpty } from '../modules/portability/legacy-safety.mjs';
6
6
  import { migrationReplay } from '../modules/portability/migration-history.mjs';
7
+ import { splitStatements } from '../modules/portability/sql-text.mjs';
7
8
  import { appliedMigrations as appliedMigrationNames } from '../modules/portability/bridge.mjs';
8
9
  import { join } from 'node:path';
9
10
  import { createHash } from 'node:crypto';
@@ -16,14 +17,16 @@ import { parseJsonc } from './jsonc.mjs';
16
17
  import { loadPolicy } from './portability-policy.mjs';
17
18
 
18
19
  // The migrations this installation applied, in its ledger's order, with each
19
- // file's hash, so a restore can place an older archive within them.
20
- async function appliedMigrations(root, db, binding, split) {
20
+ // file's hash, so a restore can place an older archive within them. Statements
21
+ // are split at SQLite's own boundaries (sql-text.mjs), not Wrangler's: its
22
+ // splitter can glue a trigger holding CASE ... END to the statements after it.
23
+ async function appliedMigrations(root, db, binding) {
21
24
  const config = parseJsonc(await readFile(join(root, 'wrangler.jsonc'), 'utf8'));
22
25
  const directory = join(root, config.d1_databases.find(entry => entry.binding === binding).migrations_dir ?? 'migrations');
23
26
  const names = await appliedMigrationNames(db) ?? [];
24
27
  return Promise.all(names.map(async name => {
25
28
  const bytes = await readFile(join(directory, name));
26
- return { name, statements: split(bytes.toString('utf8')), sha256: createHash('sha256').update(bytes).digest('hex') };
29
+ return { name, statements: splitStatements(bytes.toString('utf8')), sha256: createHash('sha256').update(bytes).digest('hex') };
27
30
  }));
28
31
  }
29
32
 
@@ -57,7 +60,7 @@ async function run(direction, { root, path, journal, binding, buckets, policy, p
57
60
  let proxy;
58
61
  try {
59
62
  proxy = await getPlatformProxy({ configPath, persist: { path: join(root, '.wrangler/state/v3') }, remoteBindings: false });
60
- const migrations=async()=>migrationReplay(await appliedMigrations(root,proxy.env[binding],binding,unstable_splitSqlQuery),policy);
63
+ const migrations=async()=>migrationReplay(await appliedMigrations(root,proxy.env[binding],binding),policy);
61
64
  const options={ db: proxy.env[binding], buckets: Object.fromEntries(Object.entries(buckets).map(([name, binding]) => [name, proxy.env[binding]])), policy, migrations };
62
65
  if(direction==='legacy-preflight') {
63
66
  await requireLegacyEmpty(options,await legacyTargetState(options.db,options.migrations));
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.2.71
4
+
5
+ - **Restores split migrations at SQLite's own statement boundaries (#344).** The portability classifier re-splits each migration chunk with sql-text.mjs splitStatements (the sqlite3_complete state machine: a trigger ends only at `; END ;`, so `CASE … END` never ends it) and counts only real table names, never keywords, function names, qualified columns, literals or comments; the secret-column check still sees every identifier. rer import's replay uses the same splitter. Slate's retained archive restores past 0038.
6
+ - **Import door archive and restore-check (#320).** Door grants gain archive and restore-check actions behind one additive door migration: archives survive plan and catch-up, erasure withdraws them and says so, and restore-check parses with zero native writes and returns the hash and counts. A catch-up of an unchanged vendor export plans zero changes.
7
+ - **Scheduled policy hardening (#339).** One canonical snapshot is validated, hashed, stored and executed; unknown actions are refused at approval and at execution; the cap counts every run-linked audit action; running rows past the 15-minute scheduled-handler limit expire atomically as failed/policy_expired with an audit row the owner sees.
8
+ - **/run fits 360 px (#343).** The /run header reflows at its cause, so a 360 px viewport has no sideways scroll; the one-line npx start and byte-exact Copy are unchanged.
9
+ - **Deploy revokes the bootstrap session (#327).** After owner hooks and before reporting success, rer deploy signs out the unused bootstrap session it created, so a fresh hosted install holds zero live sessions until the owner signs in.
10
+
3
11
  ## 0.2.70
4
12
 
5
13
  - **Archive restores judge later data statements by the table they write (#340).** A bounded INSERT/REPLACE/UPDATE/DELETE whose target is outside the restorable catalogue, and reaches none of it by trigger or FK action, is admitted whatever it reads; an INSERT…SELECT/VALUES into a catalogue table the archive lacks is replayed at finalize in migration order, in the same D1 batch, and recorded in bridged.replayed. Nook's retained archive restores past 0061.
@@ -26,7 +26,8 @@ retaining any later known sign-in, so missing historical sessions never establis
26
26
  immediate inactivity.
27
27
 
28
28
  Canonical JSON sorts object keys, preserves array order and rejects non-JSON
29
- values. SHA-256 covers the complete definition, including both bounds. The owner
29
+ values. The definition is snapshotted once; validation, action grants and SHA-256
30
+ all use that same snapshot, including both bounds. The owner
30
31
  reviews that exact hash; approval rereads the definition and rejects stale hashes.
31
32
  Approval stores the definition, actions, hash, owner id/authority version, session
32
33
  kind, approval time and revocation details. Reapproval atomically revokes its
@@ -60,7 +61,13 @@ remain committed. Existing final-owner and invitation/version triggers still app
60
61
 
61
62
  Admission is serialized per policy: another admitted run cannot overlap, and the
62
63
  minimum interval starts at the last admitted run's finish, including failed or
63
- refused admitted runs. Early ticks are refused with `policy_interval`. Consecutive
64
+ refused admitted runs. An admitted run still running beyond Cloudflare's
65
+ [15-minute scheduled-handler limit](https://developers.cloudflare.com/workers/platform/limits/#duration)
66
+ is atomically marked failed with `policy_expired` and a `policy:expired` audit
67
+ bound to its original run id. Its minimum interval is measured from admission,
68
+ so recovery cannot exceed the approved tick frequency; live runs still block
69
+ overlap. Expired capabilities cannot write or overwrite the expiry outcome.
70
+ Early ticks are refused with `policy_interval`. Consecutive
64
71
  identical unadmitted refusals for the same policy/version/approval/outcome coalesce
65
72
  into one history row with `repeats` occurrences and `lastSeenAt`. Admitted runs
66
73
  retain separate rows and action history.
@@ -6,11 +6,14 @@ function keys(value, allowed) { if (!plain(value) || Object.keys(value).some(key
6
6
  function canonical(value) {
7
7
  if (value === null || typeof value === 'string' || typeof value === 'boolean') return JSON.stringify(value);
8
8
  if (typeof value === 'number' && Number.isFinite(value)) return JSON.stringify(value);
9
- if (Array.isArray(value)) return '[' + value.map(canonical).join(',') + ']';
9
+ if (Array.isArray(value)) return '[' + Array.from(value, canonical).join(',') + ']';
10
10
  if (plain(value)) return '{' + Object.keys(value).sort().map(key => JSON.stringify(key) + ':' + canonical(value[key])).join(',') + '}';
11
11
  return invalid();
12
12
  }
13
13
  export async function policyVersion(input) {
14
+ const json = canonical(input);
15
+ if (json.length > 32768) invalid();
16
+ input = JSON.parse(json);
14
17
  keys(input, ['id', 'kind', 'parameters', 'actions', 'maxActionsPerRun', 'minIntervalMinutes']);
15
18
  if (!/^[a-z][a-z0-9-]{0,79}$/.test(input.id) || typeof input.id !== 'string' || typeof input.kind !== 'string' || !/^[a-z][a-z0-9:._-]{0,119}$/.test(input.kind) || !plain(input.parameters) || !Array.isArray(input.actions) || !input.actions.length || input.actions.length > 20) invalid();
16
19
  if (!Number.isSafeInteger(input.maxActionsPerRun) || input.maxActionsPerRun < 1 || input.maxActionsPerRun > 100 || !Number.isSafeInteger(input.minIntervalMinutes) || input.minIntervalMinutes < 1 || input.minIntervalMinutes > 525600) invalid();
@@ -23,10 +26,8 @@ export async function policyVersion(input) {
23
26
  if (operatorIds !== undefined && (!Array.isArray(operatorIds) || !operatorIds.length || operatorIds.length > 500 || operatorIds.some(id => typeof id !== 'string' || !id || id.length > 200) || new Set(operatorIds).size !== operatorIds.length)) invalid();
24
27
  if (inactiveDays !== undefined && (!Number.isSafeInteger(inactiveDays) || inactiveDays < 1 || inactiveDays > 36500)) invalid();
25
28
  }
26
- const json = canonical(input);
27
- if (json.length > 32768) invalid();
28
29
  const versionHash = [...new Uint8Array(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(json)))].map(byte => byte.toString(16).padStart(2, '0')).join('');
29
- return { definition: JSON.parse(json), versionHash, json, actionsJson: canonical(input.actions) };
30
+ return { definition: input, versionHash, json, actionsJson: canonical(input.actions) };
30
31
  }
31
32
  export async function readPolicy(definitions, id) {
32
33
  const policies = definitions();
@@ -0,0 +1,17 @@
1
+ // Cloudflare's scheduled-handler wall-clock limit:
2
+ // https://developers.cloudflare.com/workers/platform/limits/#duration
3
+ export const scheduledHandlerLimitMs = 15 * 60000;
4
+
5
+ export async function expireRuns(store, policyId, now) {
6
+ const stale = "policy_id=? AND admitted=1 AND status='running' AND started_at<?";
7
+ const args = [policyId, now - scheduledHandlerLimitMs];
8
+ // One transaction records each abandoned run exactly once before closing it.
9
+ await store.batch([
10
+ [`INSERT INTO oa_auth_audit(id,actor_id,action,value,created_at)
11
+ SELECT lower(hex(randomblob(16))),'policy:'||policy_id||'@'||version_hash,'policy:expired',
12
+ json_object('policyId',policy_id,'versionHash',version_hash,'approvalId',approval_id,
13
+ 'approvedBy',approved_by,'runId',id,'error','policy_expired'),?
14
+ FROM oa_policy_runs WHERE ${stale}`, [now, ...args]],
15
+ [`UPDATE oa_policy_runs SET status='failed',error='policy_expired',finished_at=?,last_seen_at=? WHERE ${stale}`, [now, now, ...args]],
16
+ ]);
17
+ }
@@ -1,3 +1,4 @@
1
+ import { scheduledHandlerLimitMs } from './expiry.mjs';
1
2
  // Identity, not a property of a request or session, selects this fence.
2
3
  const actors = new WeakMap();
3
4
  export function bindPolicyActor(actor, fence) { actors.set(actor, fence); }
@@ -20,8 +21,8 @@ export function actionFence(approval, runId, action, target, now, maxActions) {
20
21
  }
21
22
  return { sql: '(' + sql + ')', args };
22
23
  });
23
- return { sql: `${approvalGuard.sql} AND EXISTS(SELECT 1 FROM oa_policy_runs WHERE id=? AND approval_id=? AND status='running' AND admitted=1)
24
- AND (SELECT COUNT(*) FROM oa_auth_audit WHERE CASE WHEN json_valid(value) THEN json_extract(value,'$.runId') END=? AND action IN ('operators:deactivate','operators:reactivate'))<?
24
+ return { sql: `${approvalGuard.sql} AND EXISTS(SELECT 1 FROM oa_policy_runs WHERE id=? AND approval_id=? AND status='running' AND admitted=1 AND started_at>=?)
25
+ AND (SELECT COUNT(*) FROM oa_auth_audit WHERE CASE WHEN json_valid(value) THEN json_extract(value,'$.runId') END=?)<?
25
26
  AND role!='owner' AND status!='invited' AND id=? AND (${scopes.map(scope => scope.sql).join(' OR ') || '0'})`,
26
- args: [...approvalGuard.args, runId, approval.id, runId, maxActions, target.id, ...scopes.flatMap(scope => scope.args)] };
27
+ args: [...approvalGuard.args, runId, approval.id, now - scheduledHandlerLimitMs, runId, maxActions, target.id, ...scopes.flatMap(scope => scope.args)] };
27
28
  }
@@ -18,7 +18,7 @@ export async function runHistory(store, request, fence) {
18
18
  export async function finishRun(store, id, error, now) {
19
19
  const status = error?.status ?? 'completed', code = error?.code ?? null;
20
20
  await store.batch([
21
- ["UPDATE oa_policy_runs SET status=?,error=?,finished_at=?,last_seen_at=? WHERE id=?", [status, code, now, now, id]],
21
+ ["UPDATE oa_policy_runs SET status=?,error=?,finished_at=?,last_seen_at=? WHERE id=? AND status='running'", [status, code, now, now, id]],
22
22
  [`UPDATE oa_policy_runs SET repeats=repeats+1,last_seen_at=? WHERE rowid=(
23
23
  SELECT prior.rowid FROM oa_policy_runs prior JOIN oa_policy_runs tick ON tick.id=?
24
24
  WHERE tick.admitted=0 AND tick.status='refused' AND prior.rowid=(SELECT MAX(rowid) FROM oa_policy_runs WHERE rowid<tick.rowid AND policy_id=tick.policy_id)
@@ -3,6 +3,8 @@ import { writeOperatorStatus } from '../accounts/status.mjs';
3
3
  import { readPolicy } from './definition.mjs';
4
4
  import { actionFence, bindPolicyActor } from './fence.mjs';
5
5
  import { finishRun } from './history.mjs';
6
+ import { expireRuns } from './expiry.mjs';
7
+ const actionStatuses = Object.freeze({ 'operators:deactivate': 'inactive', 'operators:reactivate': 'active' });
6
8
 
7
9
  export function policyRunner({ definitions, store, clock, currentApproval, check, rolePolicy }) {
8
10
  return async function run(policyId, execute) {
@@ -14,7 +16,7 @@ export function policyRunner({ definitions, store, clock, currentApproval, check
14
16
  if (error instanceof AuthError && error.code === 'policy_changed' && approval) {
15
17
  await store.batch([
16
18
  ["UPDATE oa_policy_approvals SET revoked_at=?,revoke_reason='superseded_by_change' WHERE id=? AND revoked_at IS NULL", [clock(), approval.id]],
17
- ["UPDATE oa_policy_runs SET status='refused',error='policy_changed',finished_at=? WHERE id=?", [clock(), id]],
19
+ ["UPDATE oa_policy_runs SET status='refused',error='policy_changed',finished_at=? WHERE id=? AND status='running'", [clock(), id]],
18
20
  ]);
19
21
  }
20
22
  throw error;
@@ -24,9 +26,10 @@ export function policyRunner({ definitions, store, clock, currentApproval, check
24
26
  policy = await readPolicy(definitions, policyId); approval = await currentApproval(policyId);
25
27
  await store.run('UPDATE oa_policy_runs SET version_hash=?,approval_id=?,approved_by=? WHERE id=?', [policy.versionHash, approval?.id ?? null, approval?.ownerId ?? null, id]);
26
28
  await verify(policy);
29
+ await expireRuns(store, policyId, startedAt);
27
30
  const admission = await store.first(`UPDATE oa_policy_runs SET admitted=1 WHERE id=? AND NOT EXISTS(
28
31
  SELECT 1 FROM oa_policy_runs WHERE policy_id=? AND id!=? AND admitted=1
29
- AND (status='running' OR COALESCE(finished_at,started_at)>?)) RETURNING id`,
32
+ AND (status='running' OR COALESCE(CASE WHEN error='policy_expired' THEN started_at ELSE finished_at END,started_at)>?)) RETURNING id`,
30
33
  [id, policyId, id, startedAt - policy.definition.minIntervalMinutes * 60000]);
31
34
  if (!admission) deny('policy_interval', 429);
32
35
  let pending = null, actionError = null, committed = 0;
@@ -34,14 +37,14 @@ export function policyRunner({ definitions, store, clock, currentApproval, check
34
37
  if (!open || pending) deny('policy_run_closed', 409);
35
38
  const operation = (async () => {
36
39
  const current = await readPolicy(definitions, policyId), owner = await verify(current);
37
- if (!current.definition.actions.some(grant => grant.action === action)) deny('policy_action_denied', 403);
40
+ if (!Object.hasOwn(actionStatuses, action) || !current.definition.actions.some(grant => grant.action === action)) deny('policy_action_denied', 403);
38
41
  if (committed >= current.definition.maxActionsPerRun) deny('policy_action_limit', 429);
39
42
  const target = await store.operator(operatorId);
40
43
  if (!target) deny('operator_not_found', 404);
41
44
  if (!await rolePolicy({ actor: owner, action, target })) deny('policy_action_denied', 403);
42
45
  const actor = Object.freeze({});
43
46
  bindPolicyActor(actor, () => actionFence(approval, id, action, target, clock(), current.definition.maxActionsPerRun));
44
- const result = await writeOperatorStatus(store, actor, target, action === 'operators:deactivate' ? 'inactive' : 'active', clock(), {
47
+ const result = await writeOperatorStatus(store, actor, target, actionStatuses[action], clock(), {
45
48
  actor: capability.actor, action, value: { policyId, versionHash: policy.versionHash, approvalId: approval.id, approvedBy: approval.ownerId, runId: id, operatorId },
46
49
  });
47
50
  committed++; return result;
@@ -300,7 +300,17 @@ catalogue (excluded, derived or platform) and no trigger or foreign key action
300
300
  table, it changes no restored row and is allowed whatever it reads. Such a table
301
301
  then holds live rows the restore lacks, so a later statement that copies it into
302
302
  a catalogue table is refused. A statement this parser cannot bound to one table
303
- keeps the rule above. An archived table the archive holds no
303
+ keeps the rule above. Each chunk a runner's splitter hands over is first split
304
+ again where SQLite itself ends statements (`sql-text.mjs`, sqlite3_complete's
305
+ state machine): a trigger ends only at `; END ;`, so `CASE ... END||x` in its
306
+ body, quoted or commented semicolons never end it, and Wrangler's splitter
307
+ gluing such triggers to the statements after them changes nothing. `rer
308
+ import` replays the installation's migrations with that splitter too. A
309
+ statement names a table only through an identifier that can be one: SQL
310
+ keywords outside table position, function names (also table-valued ones such as
311
+ `pragma_index_list(...)`), the column of a qualified `t.column`, string and blob
312
+ literals, numbers and comments never count. Text Base cannot read to a
313
+ statement boundary, such as an unclosed block comment, is refused. An archived table the archive holds no
304
314
  rows of restores empty, so an `UPDATE` or `DELETE` of it changes nothing and is
305
315
  allowed unless it also names a table the archive holds rows of (which it could
306
316
  read into its writes); an `INSERT` into it, or any other statement naming it,
@@ -627,6 +637,7 @@ modules/portability/restore-safety.mjs
627
637
  modules/portability/bridge.mjs
628
638
  modules/portability/ledgers.mjs
629
639
  modules/portability/migration-statements.mjs
640
+ modules/portability/sql-text.mjs
630
641
  modules/portability/backfills.mjs
631
642
  modules/portability/legacy-safety.mjs
632
643
  modules/portability/export.mjs
@@ -1,6 +1,7 @@
1
1
  import { statement, quote, insist } from './storage.mjs';
2
2
  import { tokens, folded } from './generated.mjs';
3
3
  import { changesData, dataStatement, reachingTables, triggerBodies, schemaChange, statementNames } from './migration-statements.mjs';
4
+ import { splitStatements } from './sql-text.mjs';
4
5
 
5
6
  // The data statements of later migrations, judged by the tables they write
6
7
  // (bridge.mjs placement). Restored rows never pass through a later migration.
@@ -21,13 +22,17 @@ import { changesData, dataStatement, reachingTables, triggerBodies, schemaChange
21
22
  // - Everything else keeps today's rule (changesData), with the tables admitted
22
23
  // writes fill from archived rows counted as archived: copying one into a
23
24
  // catalogue table is refused, since the restore does not hold its live rows.
25
+ // Each chunk the runner's splitter handed over is split again at SQLite's own
26
+ // statement boundaries first (sql-text.mjs), so a trigger glued to the
27
+ // statements after it is judged as the statements it holds.
24
28
  // Returns the replays in migration order, or refuses with the first migration
25
29
  // (in `unapplied` order) holding a statement that cannot be admitted.
26
30
  export function laterData({applied,unapplied,replay,catalogue,archived,empty,tables,secrets}) {
27
- const statements=applied.flatMap(name=>replay.statements(name)),fold=list=>list.map(folded);
31
+ const split=name=>replay.statements(name).flatMap(splitStatements),fold=list=>list.map(folded);
32
+ const statements=applied.flatMap(split);
28
33
  const restorable=fold(catalogue),carried=fold(archived),absent=restorable.filter(name=>!carried.includes(name));
29
34
  const reached=reachingTables(statements,carried),writable=reachingTables(statements,restorable);
30
- const later=unapplied.flatMap(migration=>replay.statements(migration).map((sql,index)=>({migration,index,sql,data:dataStatement(sql)})));
35
+ const later=unapplied.flatMap(migration=>split(migration).map((sql,index)=>({migration,index,sql,data:dataStatement(sql)})));
31
36
  const known=fold(tables),outside=later.filter(s=>s.data && !writable.includes(s.data.target)),tainted=new Set();
32
37
  // Tables an admitted write fills from archived rows (directly, through its
33
38
  // target's triggers, or from another such table) hold rows the restore lacks.
@@ -57,7 +62,7 @@ function replayable({data},{table,statements,restorable,writable,carried,tables,
57
62
  if(!['insert','replace'].includes(data.verb) || data.star)return false;
58
63
  const reads=data.names.filter(name=>tables.includes(name) && name!==table);
59
64
  if(!reads.every(name=>carried.includes(name)))return false;
60
- if(reads.some(name=>Object.entries(secrets).some(([t,columns])=>folded(t)===name && columns.some(c=>data.names.includes(folded(c))))))return false;
65
+ if(reads.some(name=>Object.entries(secrets).some(([t,columns])=>folded(t)===name && columns.some(c=>data.words.includes(folded(c))))))return false;
61
66
  if(triggerBodies(statements,table).some(name=>writable.includes(name)))return false;
62
67
  // Foreign key actions: a row the replay deletes or rekeys (REPLACE, upsert) must reach no other catalogue table.
63
68
  return !reachingTables(statements,restorable.filter(name=>name!==table)).includes(table);
@@ -1,4 +1,5 @@
1
1
  import { tokens, identifier, folded } from './generated.mjs';
2
+ import { tableNames, splitStatements } from './sql-text.mjs';
2
3
 
3
4
  // Whether one migration statement can change rows a restore carries. Only
4
5
  // CREATE TABLE (not AS SELECT), CREATE INDEX and ALTER TABLE ... ADD COLUMN
@@ -18,8 +19,12 @@ import { tokens, identifier, folded } from './generated.mjs';
18
19
  // (no row fires a trigger either), so such a statement changes data only if it
19
20
  // names another reached table (one it could write, or read into its writes).
20
21
  // Any other statement naming an empty table still does: an INSERT would leave
21
- // rows the restore lacks.
22
+ // rows the restore lacks. A chunk holding several statements (sql-text.mjs
23
+ // splitStatements) changes data when any of them does.
22
24
  export function changesData(sql,reached,empty=[]) {
25
+ return splitStatements(sql).some(one=>statementChanges(one,reached,empty));
26
+ }
27
+ function statementChanges(sql,reached,empty) {
23
28
  const list=tokens(sql),words=list.map(t=>t.quoted?undefined:folded(t.text)),kind=kindOf(words);
24
29
  if(kind==='create table')return topLevelWords(list,words).includes('as');
25
30
  if(kind==='create index')return false;
@@ -34,15 +39,16 @@ export function changesData(sql,reached,empty=[]) {
34
39
  // DELETE/UPDATE CASCADE or SET) declared by a reached table: a write to such a
35
40
  // table during a later migration changes restorable rows the archive lacks. A
36
41
  // trigger counts when its statement names a reached table anywhere; one this
37
- // parser cannot bound adds every name it holds.
42
+ // parser cannot bound adds every table name it holds (sql-text.mjs tableNames:
43
+ // never a keyword, function, qualified column or literal).
38
44
  export function reachingTables(statements,restorable) {
39
- const reached=new Set(restorable.map(folded)),parsed=statements.map(sql=>tokens(sql)).map(list=>({list,words:list.map(t=>t.quoted?undefined:folded(t.text))}));
45
+ const reached=new Set(restorable.map(folded)),parsed=statements.flatMap(splitStatements).map(sql=>tokens(sql)).map(list=>({list,words:list.map(t=>t.quoted?undefined:folded(t.text))}));
40
46
  const triggers=parsed.filter(({words})=>kindOf(words)==='create trigger').map(({list})=>({list,on:createdTrigger(list)?.on}));
41
47
  const actions=parsed.filter(({words})=>['create table','alter table'].includes(kindOf(words)) && words.some((w,i)=>w==='cascade' || (w==='set' && ['delete','update'].includes(words[i-1])))).map(({list})=>referencing(list));
42
48
  for(let grew=true;grew;) {
43
49
  grew=false;
44
50
  const add=name=>{if(!reached.has(name)){reached.add(name);grew=true;}};
45
- for(const {list,on} of triggers)if(names(list,reached))(on===undefined?identifiers(list):[on]).forEach(add);
51
+ for(const {list,on} of triggers)if(names(list,reached))(on===undefined?tableNames(list):[on]).forEach(add);
46
52
  for(const {table,parents} of actions)if(table===undefined?parents.some(name=>reached.has(name)):reached.has(table))parents.forEach(add);
47
53
  }
48
54
  return [...reached];
@@ -63,7 +69,8 @@ function tableName(name) {
63
69
  // directly, after an optional WITH clause, with what a replay must know of it:
64
70
  // {verb, target, conflict (an OR clause's resolution, 'replace' for REPLACE),
65
71
  // columns (an explicit insert column list), upsert (ON CONFLICT ... DO UPDATE),
66
- // star (a * or t.* result column), names (every identifier it holds)}. Undefined
72
+ // star (a * or t.* result column), names (the table names it holds, tableNames),
73
+ // words (every identifier it holds, columns and keywords too)}. Undefined
67
74
  // for any other statement or one this parser cannot bound. Triggers on the
68
75
  // target and foreign key actions are the only further writes such a statement
69
76
  // makes; reachingTables accounts for both.
@@ -100,7 +107,7 @@ export function dataStatement(sql) {
100
107
  if(word(parts[i],'as'))i+=2;
101
108
  const star=parts.some((a,k)=>!a.quoted && /(^|\.)\*$/.test(a.text) && !(parts[k-1]?.text==='(' && parts[k+1]?.text===')'));
102
109
  const upsert=parts.some((a,k)=>word(a,'do') && word(parts[k+1],'update'));
103
- return {verb,target,conflict,columns:verb==='insert' || verb==='replace'?parts[i]?.text==='(':undefined,upsert,star,names:identifiers(parts)};
110
+ return {verb,target,conflict,columns:verb==='insert' || verb==='replace'?parts[i]?.text==='(':undefined,upsert,star,names:tableNames(parts),words:identifiers(parts)};
104
111
  }
105
112
  // The index after the parenthesized group opening at `start`, or the end when unbalanced.
106
113
  function closing(parts,start) {
@@ -108,15 +115,15 @@ function closing(parts,start) {
108
115
  for(let i=start;i<parts.length;i++){if(parts[i].quoted)continue;if(parts[i].text==='(')depth++;else if(parts[i].text===')' && !--depth)return i+1;}
109
116
  return parts.length;
110
117
  }
111
- // The names in the bodies of triggers on `table` that `statements` create, and
112
- // every name in a trigger this parser cannot bound that names the table.
118
+ // The table names in the bodies of triggers on `table` that `statements`
119
+ // create, and every one in a trigger this parser cannot bound that names the table.
113
120
  export function triggerBodies(statements,table) {
114
121
  const result=[];
115
- for(const list of statements.map(sql=>tokens(sql))) {
122
+ for(const list of statements.flatMap(splitStatements).map(sql=>tokens(sql))) {
116
123
  if(kindOf(list.map(t=>t.quoted?undefined:folded(t.text)))!=='create trigger')continue;
117
124
  const created=createdTrigger(list);
118
- if(!created){if(identifiers(list).includes(table))result.push(...identifiers(list));}
119
- else if(created.on===table)result.push(...identifiers(atoms(list).slice(created.begin+1)));
125
+ if(!created){if(tableNames(list).includes(table))result.push(...tableNames(list));}
126
+ else if(created.on===table)result.push(...tableNames(atoms(list).slice(created.begin+1)));
120
127
  }
121
128
  return result;
122
129
  }
@@ -130,10 +137,10 @@ export function schemaChange(sql) {
130
137
  if(kind==='alter table' && words.find((w,i)=>i>2 && ['add','rename','drop'].includes(w))==='add')return 'add column';
131
138
  if(kind==='create trigger' && createdTrigger(list) || kind==='drop trigger' && droppedTrigger(list))return 'trigger';
132
139
  }
133
- export const statementNames=sql=>identifiers(tokens(sql));
140
+ export const statementNames=sql=>tableNames(tokens(sql));
134
141
  const kindOf=words=>words.slice(0,4).filter(w=>!['temp','temporary','unique'].includes(w)).slice(0,2).join(' ');
135
142
  const identifiers=list=>list.flatMap(t=>t.quoted?(t.text.startsWith("'")?[]:[folded(identifier(t))]):folded(t.text).split(/[^a-z0-9_$\u0080-\uffff]+/).filter(Boolean));
136
- const names=(list,reached)=>identifiers(list).some(name=>reached.has(name));
143
+ const names=(list,reached)=>tableNames(list).some(name=>reached.has(name));
137
144
  // Tokens with each unquoted ';' split out, and trailing ones dropped.
138
145
  function atoms(list) {
139
146
  const result=list.flatMap(t=>t.quoted?[t]:t.text.split(/(;)/).filter(Boolean).map(text=>({text})));
@@ -0,0 +1,95 @@
1
+ import { tokens, identifier, folded } from './generated.mjs';
2
+
3
+ // Migration SQL split where SQLite itself ends statements: sqlite3_complete's
4
+ // state machine (https://github.com/sqlite/sqlite/blob/master/src/complete.c,
5
+ // public domain). A trigger ends only at `; END ;`, so CASE ... END (also
6
+ // `END||x`), quoted or commented semicolons and BEGIN ... END bodies never end
7
+ // one. Runners hand over chunks from their own splitter, which can glue a
8
+ // trigger to the statements after it; each chunk is split again here before
9
+ // it is classified. Statements are returned without their final semicolon. A
10
+ // trailing trigger with no `; END` is returned as it stands: the classifier
11
+ // cannot bound it (migration-statements.mjs) and judges it as unbounded.
12
+ const transitions=[
13
+ [1,0,2,3,4,2,2,2],// 0 invalid
14
+ [1,1,2,3,4,2,2,2],// 1 start
15
+ [1,2,2,2,2,2,2,2],// 2 normal
16
+ [1,3,3,2,4,2,2,2],// 3 explain
17
+ [1,4,2,2,2,4,5,2],// 4 create
18
+ [6,5,5,5,5,5,5,5],// 5 trigger
19
+ [6,6,5,5,5,5,5,7],// 6 semicolon in a trigger
20
+ [1,7,5,5,5,5,5,5],// 7 end in a trigger
21
+ ];// columns: semicolon, space, other, EXPLAIN, CREATE, TEMP/TEMPORARY, TRIGGER, END
22
+ const kinds={explain:3,create:4,temp:5,temporary:5,trigger:6,end:7};
23
+ export function splitStatements(sql) {
24
+ const result=[];let state=0,start=0,code=false;
25
+ for(const t of tokens(sql)) {
26
+ const pieces=t.quoted?[{text:t.text,at:t.start}]:[...t.text.matchAll(/[A-Za-z0-9_$\u0080-￿]+|[^A-Za-z0-9_$\u0080-￿]/g)].map(m=>({text:m[0],at:t.start+m.index}));
27
+ for(const {text,at} of pieces) {
28
+ const kind=t.quoted?2:text===';'?0:kinds[folded(text)]??2;
29
+ if(kind)code=true;
30
+ state=transitions[state][kind];
31
+ if(state!==1)continue;
32
+ if(code)result.push(sql.slice(start,at).trim());
33
+ start=at+1;code=false;state=0;
34
+ }
35
+ }
36
+ if(code)result.push(sql.slice(start).trim());
37
+ return result;
38
+ }
39
+
40
+ // SQLite's keywords (sqlite3_keyword_name), with the pseudo-tables a trigger
41
+ // or upsert names (NEW, OLD, excluded) and the boolean literals.
42
+ const keywords=new Set(`abort action add after all alter always analyze and as asc attach autoincrement before begin between by cascade case cast check collate column commit conflict constraint create cross current current_date current_time current_timestamp database default deferrable deferred delete desc detach distinct do drop each else end escape except exclude exclusive exists explain fail filter first following for foreign from full generated glob group groups having if ignore immediate in index indexed initially inner insert instead intersect into is isnull join key last left like limit match materialized natural no not nothing notnull null nulls of offset on or order others outer over partition plan pragma preceding primary query raise range recursive references regexp reindex release rename replace restrict returning right rollback row rows savepoint select set table temp temporary then ties to transaction trigger unbounded union unique update using vacuum values view virtual when where window with without new old excluded true false`.split(' '));
43
+ const pseudo=new Set(['new','old','excluded']);
44
+ // Words that can follow a keyword naming a table without being one: UPDATE OR,
45
+ // AFTER UPDATE ON/OF, DO UPDATE SET, TABLE IF [NOT] EXISTS.
46
+ const grammar=new Set(['or','on','of','set','if']);
47
+
48
+ // Every name in a token list (tokens()) that can be a table: identifiers in
49
+ // table position (after FROM, JOIN, INTO, UPDATE [OR ...], TABLE [IF [NOT]
50
+ // EXISTS], REFERENCES, and the ON of a CREATE INDEX or TRIGGER header), the
51
+ // qualifiers of a qualified name (`t` of t.column), and other bare
52
+ // identifiers. SQL keywords outside table position, function names (a name
53
+ // followed by `(`, also a table-valued one after FROM or JOIN), the column
54
+ // of a qualified reference, string and blob literals, numbers and comments
55
+ // never count. Names are folded.
56
+ export function tableNames(list) {
57
+ const lex=lexemes(list),result=[],header=headerOn(lex);
58
+ for(let i=0;i<lex.length;i++) {
59
+ if(!named(lex[i]))continue;
60
+ let j=i;
61
+ while(lex[j+1]?.text==='.' && named(lex[j+2]))j+=2;
62
+ const parts=lex.slice(i,j+1).filter((_,k)=>k%2===0),call=lex[j+1]?.text==='(',at=position(lex,i,header);
63
+ if(at && !(lex[i].kind==='word' && grammar.has(lex[i].value))){if(at==='target' || !call)result.push(parts.at(-1).value);}
64
+ else if(parts.length>1)result.push(...parts.slice(0,-1).filter(p=>!pseudo.has(p.value)).map(p=>p.value));
65
+ else if(!call && !(lex[i].kind==='word' && keywords.has(lex[i].value)) && !(folded(lex[i].text)==='x' && lex[i+1]?.kind==='string'))result.push(lex[i].value);
66
+ i=j;
67
+ }
68
+ return result;
69
+ }
70
+ function lexemes(list) {
71
+ return list.flatMap(t=>t.quoted?[{kind:t.text[0]==="'"?'string':'name',text:t.text,value:folded(identifier(t))}]:
72
+ [...t.text.matchAll(/(\d[\w$]*(?:\.\d*)?(?:[eE][+-]?\d+)?|\.\d+(?:[eE][+-]?\d+)?)|([A-Za-z_\u0080-￿][\w$\u0080-￿]*)|\S/g)]
73
+ .map(m=>m[1]!==undefined?{kind:'number',text:m[0]}:m[2]!==undefined?{kind:'word',text:m[0],value:folded(m[0])}:{kind:'symbol',text:m[0]}));
74
+ }
75
+ const named=a=>a?.kind==='name' || a?.kind==='word';
76
+ const is=(a,value)=>a?.kind==='word' && a.value===value;
77
+ // Whether the name starting at lex[i] is in table position: 'source' (FROM,
78
+ // JOIN: a name called there is a table-valued function), 'target' otherwise.
79
+ function position(lex,i,header) {
80
+ if(is(lex[i-1],'from') || is(lex[i-1],'join'))return 'source';
81
+ if(['into','references','update'].some(w=>is(lex[i-1],w)) || i-1===header)return 'target';
82
+ if(is(lex[i-2],'or') && ['update','insert'].some(w=>is(lex[i-3],w)))return 'target';
83
+ let k=i-1;
84
+ if(is(lex[k],'exists')){k--;if(is(lex[k],'not'))k--;if(!is(lex[k],'if'))return;k--;}
85
+ if(is(lex[k],'table'))return 'target';
86
+ }
87
+ // The index of the ON naming the table of a CREATE [UNIQUE] INDEX or CREATE
88
+ // [TEMP] TRIGGER statement: its first ON outside parentheses.
89
+ function headerOn(lex) {
90
+ const head=lex.slice(0,4).filter(a=>!['unique','temp','temporary'].some(w=>is(a,w)));
91
+ if(!is(head[0],'create') || !(is(head[1],'index') || is(head[1],'trigger')))return -1;
92
+ let depth=0;
93
+ for(let i=0;i<lex.length;i++){if(lex[i].text==='(')depth++;else if(lex[i].text===')')depth--;else if(!depth && is(lex[i],'on'))return i;else if(!depth && is(lex[i],'begin'))return -1;}
94
+ return -1;
95
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@runeditrun/rer",
3
- "version": "0.2.70",
3
+ "version": "0.2.71",
4
4
  "description": "The rer command: install, set up, run and launch Run Edit Run checkouts.",
5
5
  "license": "MIT",
6
6
  "type": "module",