Personal Telegram developer console for GitHub

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