Personal Telegram developer console for GitHub

feat/studio-g0-foundation
audit/commit-drilldown audit/finalize-workspace-menu-security audit/ops-actionability audit/pr-menu-consolidation audit/status-menu-consolidation audit/telegram-menu-e2e audit/telegram-menu-e2e-wave2 chore/actions-node24-runtime cleanup/remove-diag design/cloudflare-ops-read docs/control-plane-2-7-design docs/guide-v2-1-sync docs/menu-audit-final-sync docs/phase2-acceptance-closeout docs/phase2-multifile-design feat/bootstrap-worker feat/cloudflare-ops-read feat/cp2-multifile-final feat/cp2-multifile-workspace feat/cp3-github-write-final feat/cp4-cloudflare-ops-final feat/cp5-cicd-control-plane feat/cp6-incident-manager feat/cp7-automation-engine feat/deep-file-browser feat/final-delivery-16-20 feat/github-advanced-read feat/in-app-guide-v2 feat/navigation-recovery-ux-v2 feat/ops-console-v2-1 feat/persistent-menu-code-input feat/phase2-multifile-workspace feat/phase2-multifile-workspace-v2 feat/plugin-foundation-v1 feat/safe-github-ops feat/self-register-webhook feat/studio-g0-foundation feat/telegram-main-inline-menu feat/telegram-pr-merge feat/ux-platform-8-12 feat/v3a-readonly-copilot feat/workspace-file-upload feature/mobile-repo-browser feature/multi-file-change-set fix/approval-expiry-feedback fix/callback-resilience-and-docs fix/ci-actions-pagination fix/cloudflare-fetch-binding fix/editor-request-body-cap fix/editor-safe-file-options fix/issues-permission-release-empty-ux fix/main-menu-callback-fallback fix/phase2-file-editor-selection fix/phase2-final-ux fix/phase2-workspace-ref-persistence fix/production-auto-deploy fix/provider-write-error-feedback fix/repository-id-navigation-fallback fix/repository-pr-navigation fix/telegram-callback-updates fix/telegram-runtime-diagnostics fix/workspace-editor-flow-v2 fix/workspace-recovery-validation hardening/ci-zero-test-telegram-token hardening/pr1-destructive-totp hardening/pr2-kill-switch main multifile-workspace nexa/edit-BwJEIIY3PW nexa/edit-IPHT8irO7b nexa/edit-b4Od3BIMYd p2-multifile phase2-20261005 phase2-20261005-a phase2-20261005-b phase2-20261005-c phase2-20261005-d phase2-20261005-e phase2-20261005-f phase2-20261005-g phase2-20261005-h phase2-20261005-i phase2-20261005-j phase2-20261005-k phase2-20261005-l phase2-20261005-m phase2-20261005-n phase2-20261005-o phase2-20261005-p phase2-20261005-q phase2-20261005-r phase2-20261005-s phase2-20261005-t phase2-20261005-u phase2-20261005-v phase2-20261005-w phase2-20261005-x phase2-20261005-y phase2-20261005-z phase2-acceptance-closeout phase2-add-file-hotfix phase2-change-set phase2-design-20261005 phase2-design-final phase2-implementation phase2-multifile-implementation phase2-multifile-spec phase2-spec-final phase2-workspace phase2/multifile-workspace plan/nexa-studio-v2-reviewed security/remove-telegram-setup spec/repository-workspace-editor v3a/multifile work/multifile
js 131 lines 4.34 KB
Raw
1 export class DurableApprovalReplayStore {
2 constructor({ namespace } = {}) {
3 this.namespace = namespace || null;
4 }
5 isConfigured() {
6 return Boolean(
7 this.namespace &&
8 typeof this.namespace.idFromName === 'function' &&
9 typeof this.namespace.get === 'function'
10 );
11 }
12 #stub(ownerId, nonce) {
13 if (!this.isConfigured())
14 throw new Error('Durable approval replay protection is not configured');
15 const owner = String(ownerId ?? '');
16 const nonceValue = String(nonce ?? '');
17 if (!owner || !nonceValue)
18 throw new Error('Invalid durable approval replay input');
19 return this.namespace.get(
20 this.namespace.idFromName(`${owner}:${nonceValue}`)
21 );
22 }
23 #secondFactorStub(ownerId) {
24 return this.#stub(ownerId, 'second-factor');
25 }
26 async put({ ownerId, nonce, expiresAt, payload } = {}) {
27 const expiry = Number(expiresAt);
28 if (!Number.isFinite(expiry) || !payload)
29 throw new Error('Invalid durable approval replay input');
30 const response = await this.#stub(ownerId, nonce).fetch(
31 new Request('https://approval-replay/prepare', {
32 method: 'POST',
33 headers: { 'content-type': 'application/json' },
34 body: JSON.stringify({ expiresAt: expiry, payload })
35 })
36 );
37 if (!response.ok)
38 throw new Error(
39 `Durable approval preparation failed (${response.status})`
40 );
41 return true;
42 }
43 async peek({ ownerId, nonce } = {}) {
44 const response = await this.#stub(ownerId, nonce).fetch(
45 new Request('https://approval-replay/peek')
46 );
47 if (!response.ok) return null;
48 const body = await response.json().catch(() => null);
49 return body?.payload || null;
50 }
51 async consume({ ownerId, nonce, expiresAt } = {}) {
52 const expiry = Number(expiresAt);
53 if (!Number.isFinite(expiry))
54 throw new Error('Invalid durable approval replay input');
55 const response = await this.#stub(ownerId, nonce).fetch(
56 new Request('https://approval-replay/consume', {
57 method: 'POST',
58 headers: { 'content-type': 'application/json' },
59 body: JSON.stringify({ expiresAt: expiry })
60 })
61 );
62 const body = await response.json().catch(() => null);
63 if (response.status === 409 || response.status === 410) return false;
64 if (!response.ok || body?.accepted !== true)
65 throw new Error(
66 `Durable approval replay protection failed (${response.status})`
67 );
68 return true;
69 }
70 async setSecondFactorPending({
71 ownerId,
72 token,
73 expiresAt,
74 attempts = 0
75 } = {}) {
76 const expiry = Number(expiresAt);
77 const attemptCount = Number(attempts);
78 if (
79 !String(ownerId ?? '') ||
80 !String(token || '') ||
81 !Number.isFinite(expiry) ||
82 expiry < Date.now() ||
83 !Number.isSafeInteger(attemptCount) ||
84 attemptCount < 0
85 ) {
86 throw new Error('Invalid second-factor pending state');
87 }
88 const response = await this.#secondFactorStub(ownerId).fetch(
89 new Request('https://approval-replay/second-factor/put', {
90 method: 'POST',
91 headers: { 'content-type': 'application/json' },
92 body: JSON.stringify({
93 token: String(token),
94 expiresAt: expiry,
95 attempts: attemptCount
96 })
97 })
98 );
99 if (!response.ok)
100 throw new Error(
101 `Durable second-factor state failed (${response.status})`
102 );
103 return true;
104 }
105 async getSecondFactorPending({ ownerId } = {}) {
106 if (!String(ownerId ?? '')) throw new Error('Invalid second-factor owner');
107 const response = await this.#secondFactorStub(ownerId).fetch(
108 new Request('https://approval-replay/second-factor/get')
109 );
110 if (response.status === 404 || response.status === 410) return null;
111 if (!response.ok)
112 throw new Error(
113 `Durable second-factor state failed (${response.status})`
114 );
115 const body = await response.json().catch(() => null);
116 return body?.pending || null;
117 }
118 async clearSecondFactorPending({ ownerId } = {}) {
119 if (!String(ownerId ?? '')) throw new Error('Invalid second-factor owner');
120 const response = await this.#secondFactorStub(ownerId).fetch(
121 new Request('https://approval-replay/second-factor/clear', {
122 method: 'POST'
123 })
124 );
125 if (!response.ok)
126 throw new Error(
127 `Durable second-factor clear failed (${response.status})`
128 );
129 return true;
130 }
131 }