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 236 lines 6.93 KB
Raw
1 import test from 'node:test';
2 import assert from 'node:assert/strict';
3 import { readFile } from 'node:fs/promises';
4 import worker, {
5 ApprovalReplayGuard as ExportedApprovalReplayGuard
6 } from '../src/index.js';
7 import { DurableApprovalReplayStore } from '../src/core/durable-replay-store.js';
8 import { ApprovalReplayGuard } from '../src/durable/approval-replay-guard.js';
9
10 function makeNamespace(handler) {
11 const ids = [];
12 return {
13 ids,
14 idFromName(name) {
15 ids.push(String(name));
16 return `id:${name}`;
17 },
18 get(id) {
19 return {
20 async fetch(request) {
21 return handler(id, request);
22 }
23 };
24 }
25 };
26 }
27
28 function makeState() {
29 const data = new Map();
30 let alarm = null;
31 return {
32 data,
33 get alarm() {
34 return alarm;
35 },
36 storage: {
37 async transaction(fn) {
38 return fn({
39 get: (key) => data.get(key),
40 put: (key, value) => {
41 data.set(key, value);
42 },
43 delete: (key) => data.delete(key)
44 });
45 },
46 async setAlarm(value) {
47 alarm = value;
48 },
49 async deleteAll() {
50 data.clear();
51 alarm = null;
52 }
53 }
54 };
55 }
56
57 test('DurableApprovalReplayStore fails closed without a Durable Object namespace', async () => {
58 const store = new DurableApprovalReplayStore();
59 await assert.rejects(
60 () =>
61 store.consume({
62 ownerId: '123',
63 nonce: 'abc',
64 expiresAt: Date.now() + 10_000
65 }),
66 /not configured/i
67 );
68 });
69
70 test('DurableApprovalReplayStore scopes one-time consumption by owner and nonce', async () => {
71 const namespace = makeNamespace(async (_id, request) => {
72 const body = await request.json();
73 return Response.json({ accepted: true, expiresAt: body.expiresAt });
74 });
75 const store = new DurableApprovalReplayStore({ namespace });
76
77 const accepted = await store.consume({
78 ownerId: '123',
79 nonce: 'nonce-1',
80 expiresAt: 42_000
81 });
82
83 assert.equal(accepted, true);
84 assert.deepEqual(namespace.ids, ['123:nonce-1']);
85 });
86
87 test('DurableApprovalReplayStore rejects a replay response from the Durable Object', async () => {
88 const namespace = makeNamespace(async () =>
89 Response.json({ accepted: false, reason: 'already_used' }, { status: 409 })
90 );
91 const store = new DurableApprovalReplayStore({ namespace });
92
93 const accepted = await store.consume({
94 ownerId: '123',
95 nonce: 'nonce-2',
96 expiresAt: 42_000
97 });
98 assert.equal(accepted, false);
99 });
100
101 test('ApprovalReplayGuard atomically accepts once and schedules cleanup', async () => {
102 const state = makeState();
103 const guard = new ApprovalReplayGuard(state);
104 const expiresAt = Date.now() + 60_000;
105 const request = () =>
106 new Request('https://approval-replay/consume', {
107 method: 'POST',
108 headers: { 'content-type': 'application/json' },
109 body: JSON.stringify({ expiresAt })
110 });
111
112 const first = await guard.fetch(request());
113 assert.equal(first.status, 200);
114 assert.deepEqual(await first.json(), { accepted: true });
115 assert.equal(state.data.get('used'), expiresAt);
116 assert.equal(state.alarm, expiresAt);
117
118 const second = await guard.fetch(request());
119 assert.equal(second.status, 409);
120 assert.deepEqual(await second.json(), {
121 accepted: false,
122 reason: 'already_used'
123 });
124 });
125
126 test('ApprovalReplayGuard fails closed for expired requests and alarm removes storage', async () => {
127 const state = makeState();
128 const guard = new ApprovalReplayGuard(state);
129 const response = await guard.fetch(
130 new Request('https://approval-replay/consume', {
131 method: 'POST',
132 headers: { 'content-type': 'application/json' },
133 body: JSON.stringify({ expiresAt: Date.now() - 1 })
134 })
135 );
136
137 assert.equal(response.status, 410);
138 assert.equal((await response.json()).accepted, false);
139
140 state.data.set('used', Date.now());
141 await guard.alarm();
142 assert.equal(state.data.size, 0);
143 });
144
145 test('Worker exports ApprovalReplayGuard and Wrangler declares SQLite durable binding', async () => {
146 assert.ok(worker && typeof worker.fetch === 'function');
147 assert.equal(ExportedApprovalReplayGuard, ApprovalReplayGuard);
148
149 const configText = await readFile(
150 new URL('../wrangler.jsonc', import.meta.url),
151 'utf8'
152 );
153 const config = JSON.parse(configText);
154 assert.ok(
155 config.durable_objects?.bindings?.some(
156 (binding) =>
157 binding.name === 'APPROVAL_REPLAY_GUARD' &&
158 binding.class_name === 'ApprovalReplayGuard'
159 )
160 );
161 assert.deepEqual(config.exports?.ApprovalReplayGuard, {
162 type: 'durable-object',
163 storage: 'sqlite'
164 });
165 });
166
167 test('DurableApprovalReplayStore persists owner-scoped second-factor pending state without exposing it in logs', async () => {
168 let stored = null;
169 const namespace = makeNamespace(async (_id, request) => {
170 const url = new URL(request.url);
171 if (request.method === 'POST' && url.pathname === '/second-factor/put') {
172 stored = await request.json();
173 return Response.json({ stored: true });
174 }
175 if (request.method === 'GET' && url.pathname === '/second-factor/get') {
176 return stored
177 ? Response.json({ pending: stored })
178 : Response.json({ pending: null }, { status: 404 });
179 }
180 if (request.method === 'POST' && url.pathname === '/second-factor/clear') {
181 stored = null;
182 return Response.json({ cleared: true });
183 }
184 return new Response('Not Found', { status: 404 });
185 });
186 const store = new DurableApprovalReplayStore({ namespace });
187 const expiresAt = Date.now() + 60_000;
188
189 await store.setSecondFactorPending({
190 ownerId: '123',
191 token: 'signed-token',
192 expiresAt
193 });
194 assert.deepEqual(await store.getSecondFactorPending({ ownerId: '123' }), {
195 token: 'signed-token',
196 expiresAt,
197 attempts: 0
198 });
199 await store.clearSecondFactorPending({ ownerId: '123' });
200 assert.equal(await store.getSecondFactorPending({ ownerId: '123' }), null);
201 assert.ok(namespace.ids.every((id) => id === '123:second-factor'));
202 });
203
204 test('ApprovalReplayGuard expires and clears second-factor pending state fail-closed', async () => {
205 const state = makeState();
206 const guard = new ApprovalReplayGuard(state);
207 const expiresAt = Date.now() + 60_000;
208
209 const put = await guard.fetch(
210 new Request('https://approval-replay/second-factor/put', {
211 method: 'POST',
212 headers: { 'content-type': 'application/json' },
213 body: JSON.stringify({ token: 'signed-token', expiresAt })
214 })
215 );
216 assert.equal(put.status, 200);
217
218 const get = await guard.fetch(
219 new Request('https://approval-replay/second-factor/get')
220 );
221 assert.equal(get.status, 200);
222 assert.deepEqual(await get.json(), {
223 pending: { token: 'signed-token', expiresAt, attempts: 0 }
224 });
225
226 const clear = await guard.fetch(
227 new Request('https://approval-replay/second-factor/clear', {
228 method: 'POST'
229 })
230 );
231 assert.equal(clear.status, 200);
232 const after = await guard.fetch(
233 new Request('https://approval-replay/second-factor/get')
234 );
235 assert.equal(after.status, 404);
236 });