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
| 1 | import test from 'node:test'; |
| 2 | import assert from 'node:assert/strict'; |
| 3 | import { DurableWorkspaceStoreClient } from '../src/core/workspace-store.js'; |
| 4 | import { WorkspaceStore } from '../src/durable/workspace-store.js'; |
| 5 | import { KillSwitchGuidedNexaDevBot } from '../src/kill-switch-bot.js'; |
| 6 | import { GitHubService } from '../src/github.js'; |
| 7 | import { |
| 8 | createWorkspaceController, |
| 9 | commitWorkspaceIntent |
| 10 | } from '../src/plugins/github/workspace.js'; |
| 11 | import { |
| 12 | WORKSPACE_EDITOR_LIMITS, |
| 13 | digestText |
| 14 | } from '../src/core/workspace-policy.js'; |
| 15 | import { |
| 16 | makeReplayState, |
| 17 | makeReplayNamespace |
| 18 | } from './helpers/replay-namespace.js'; |
| 19 | |
| 20 | const api = await import('../src/studio-api.js').catch(() => ({})); |
| 21 | const BOT_TOKEN = '123456:studio-test-token'; |
| 22 | const OWNER = '42'; |
| 23 | const NOW = Date.parse('2026-10-26T12:00:00Z'); |
| 24 | const HEAD = 'a'.repeat(40); |
| 25 | const TREE = 'b'.repeat(40); |
| 26 | const BLOB = 'c'.repeat(40); |
| 27 | |
| 28 | async function signedInitData({ userId = 42, age = 0 } = {}) { |
| 29 | const fields = new URLSearchParams({ |
| 30 | auth_date: String(Math.floor(NOW / 1000) - age), |
| 31 | user: JSON.stringify({ id: userId, first_name: 'Owner' }) |
| 32 | }); |
| 33 | const encode = (value) => new TextEncoder().encode(value); |
| 34 | const sign = async (keyBytes, value) => { |
| 35 | const key = await crypto.subtle.importKey( |
| 36 | 'raw', |
| 37 | keyBytes, |
| 38 | { name: 'HMAC', hash: 'SHA-256' }, |
| 39 | false, |
| 40 | ['sign'] |
| 41 | ); |
| 42 | return new Uint8Array(await crypto.subtle.sign('HMAC', key, encode(value))); |
| 43 | }; |
| 44 | const check = [...fields.entries()] |
| 45 | .sort(([a], [b]) => a.localeCompare(b)) |
| 46 | .map(([key, value]) => `${key}=${value}`) |
| 47 | .join('\n'); |
| 48 | const signature = await sign( |
| 49 | await sign(encode('WebAppData'), BOT_TOKEN), |
| 50 | check |
| 51 | ); |
| 52 | fields.set( |
| 53 | 'hash', |
| 54 | [...signature].map((byte) => byte.toString(16).padStart(2, '0')).join('') |
| 55 | ); |
| 56 | return fields.toString(); |
| 57 | } |
| 58 | |
| 59 | function fixture() { |
| 60 | let currentTime = NOW; |
| 61 | const states = new Map(); |
| 62 | const namespace = { |
| 63 | idFromName: (id) => String(id), |
| 64 | get(id) { |
| 65 | if (!states.has(id)) states.set(id, makeReplayState()); |
| 66 | return { |
| 67 | fetch: (request) => new WorkspaceStore(states.get(id)).fetch(request) |
| 68 | }; |
| 69 | } |
| 70 | }; |
| 71 | const workspaces = new DurableWorkspaceStoreClient({ |
| 72 | namespace, |
| 73 | now: () => currentTime |
| 74 | }); |
| 75 | const calls = []; |
| 76 | const repository = { |
| 77 | id: '123', |
| 78 | name: 'repo', |
| 79 | fullName: 'owner/repo', |
| 80 | defaultBranch: 'main', |
| 81 | private: true |
| 82 | }; |
| 83 | const github = { |
| 84 | async listRepositories() { |
| 85 | calls.push('repos'); |
| 86 | return [repository]; |
| 87 | }, |
| 88 | async getRepository(repoId) { |
| 89 | calls.push('repo'); |
| 90 | return { ...repository, id: String(repoId) }; |
| 91 | }, |
| 92 | async listFiles(repoId, path, options) { |
| 93 | calls.push(['tree', repoId, path, options]); |
| 94 | return Array.from({ length: 35 }, (_, i) => ({ |
| 95 | name: `file-${i}.js`, |
| 96 | path: path ? `${path}/file-${i}.js` : `file-${i}.js`, |
| 97 | type: 'file', |
| 98 | size: 7, |
| 99 | sha: BLOB |
| 100 | })); |
| 101 | }, |
| 102 | async getBranchSnapshot() { |
| 103 | calls.push('snapshot'); |
| 104 | return { headSha: HEAD, treeSha: TREE }; |
| 105 | }, |
| 106 | async getFileAtRef(repoId, path, ref) { |
| 107 | calls.push(['file', repoId, path, ref]); |
| 108 | return { path, content: 'before\n', size: 7, sha: BLOB }; |
| 109 | }, |
| 110 | async createGitBlob() { |
| 111 | throw new Error('Unexpected provider write'); |
| 112 | }, |
| 113 | async createGitTree() { |
| 114 | throw new Error('Unexpected provider write'); |
| 115 | }, |
| 116 | async createGitCommit() { |
| 117 | throw new Error('Unexpected provider write'); |
| 118 | }, |
| 119 | async createGitRef() { |
| 120 | throw new Error('Unexpected provider write'); |
| 121 | } |
| 122 | }; |
| 123 | const env = { |
| 124 | TELEGRAM_BOT_TOKEN: BOT_TOKEN, |
| 125 | OWNER_TELEGRAM_ID: OWNER, |
| 126 | APPROVAL_SIGNING_SECRET: 'studio-approval-test-secret', |
| 127 | WORKSPACE_STORE: namespace, |
| 128 | APPROVAL_REPLAY_GUARD: makeReplayNamespace() |
| 129 | }; |
| 130 | const bot = new KillSwitchGuidedNexaDevBot(env); |
| 131 | Object.assign(bot.github, github); |
| 132 | bot.approvals.now = () => currentTime; |
| 133 | bot.audit.sink = () => {}; |
| 134 | const messages = []; |
| 135 | bot.telegram.sendMessage = async (...args) => { |
| 136 | messages.push(args); |
| 137 | return { ok: true }; |
| 138 | }; |
| 139 | return { |
| 140 | env, |
| 141 | github, |
| 142 | workspaces, |
| 143 | bot, |
| 144 | calls, |
| 145 | messages, |
| 146 | states, |
| 147 | now: () => currentTime, |
| 148 | advance: (ms) => { |
| 149 | currentTime += ms; |
| 150 | } |
| 151 | }; |
| 152 | } |
| 153 | |
| 154 | async function request( |
| 155 | f, |
| 156 | path = '', |
| 157 | { method = 'GET', body, headers = {}, auth = {} } = {} |
| 158 | ) { |
| 159 | assert.equal( |
| 160 | typeof api.handleStudioApiRequest, |
| 161 | 'function', |
| 162 | 'Studio API handler exists' |
| 163 | ); |
| 164 | return api.handleStudioApiRequest( |
| 165 | new Request(`https://studio.test/api/v1/studio${path}`, { |
| 166 | method, |
| 167 | headers: { |
| 168 | 'x-telegram-init-data': await signedInitData(auth), |
| 169 | ...headers |
| 170 | }, |
| 171 | ...(body !== undefined |
| 172 | ? { body: typeof body === 'string' ? body : JSON.stringify(body) } |
| 173 | : {}) |
| 174 | }), |
| 175 | f |
| 176 | ); |
| 177 | } |
| 178 | |
| 179 | async function create(f) { |
| 180 | const tree = await (await request(f, '/tree?repoId=123')).json(); |
| 181 | const response = await request(f, '/workspaces', { |
| 182 | method: 'POST', |
| 183 | body: { refId: tree.items[0].refId } |
| 184 | }); |
| 185 | assert.equal(response.status, 200); |
| 186 | return (await response.json()).workspace; |
| 187 | } |
| 188 | |
| 189 | test('Studio authenticates signed, fresh owner initData before touching providers or durable objects on every route', async () => { |
| 190 | const routes = [ |
| 191 | ['', 'GET'], |
| 192 | ['/repos', 'GET'], |
| 193 | ['/tree?repoId=123', 'GET'], |
| 194 | ['/workspaces', 'POST'], |
| 195 | ['/workspaces/W123456789', 'GET'], |
| 196 | ['/workspaces/W123456789/files', 'PUT'], |
| 197 | ['/workspaces/W123456789/files', 'POST'], |
| 198 | ['/workspaces/W123456789/diff', 'GET'], |
| 199 | ['/workspaces/W123456789/message', 'PUT'], |
| 200 | ['/workspaces/W123456789/approval', 'POST'], |
| 201 | ['/workspaces/W123456789', 'DELETE'] |
| 202 | ]; |
| 203 | for (const auth of [{ userId: 99 }, { age: 3601 }]) { |
| 204 | for (const [path, method] of routes) { |
| 205 | const f = fixture(); |
| 206 | const response = await request(f, path, { |
| 207 | method, |
| 208 | auth, |
| 209 | body: method === 'GET' ? undefined : {} |
| 210 | }); |
| 211 | assert.equal(response.status, 403, `${method} ${path}`); |
| 212 | assert.deepEqual(f.calls, []); |
| 213 | assert.equal(f.states.size, 0); |
| 214 | assert.equal(f.messages.length, 0); |
| 215 | } |
| 216 | } |
| 217 | const f = fixture(); |
| 218 | const missing = await request(f, '/repos', { |
| 219 | headers: { 'x-telegram-init-data': '' } |
| 220 | }); |
| 221 | assert.equal(missing.status, 403); |
| 222 | assert.deepEqual(f.calls, []); |
| 223 | }); |
| 224 | |
| 225 | test('Studio bootstrap, complete repository listing and directory refs expose only structured owner-scoped data', async () => { |
| 226 | const f = fixture(); |
| 227 | const bootstrap = await (await request(f)).json(); |
| 228 | assert.equal(bootstrap.ok, true); |
| 229 | assert.equal(bootstrap.user.name, 'Owner'); |
| 230 | assert.deepEqual(bootstrap.limits, WORKSPACE_EDITOR_LIMITS); |
| 231 | assert.equal(typeof bootstrap.version, 'string'); |
| 232 | const repositories = await (await request(f, '/repos')).json(); |
| 233 | assert.equal(repositories.repositories[0].fullName, 'owner/repo'); |
| 234 | const tree = await (await request(f, '/tree?repoId=123&path=src')).json(); |
| 235 | assert.equal(tree.items.length, 35); |
| 236 | assert.equal(tree.path, 'src'); |
| 237 | assert.equal(tree.repository.id, '123'); |
| 238 | assert.match(tree.items[34].refId, /^[A-Za-z0-9_-]{10}$/); |
| 239 | assert.deepEqual( |
| 240 | await f.workspaces.resolveNavigationRef({ |
| 241 | ownerId: '99', |
| 242 | refId: tree.items[0].refId |
| 243 | }), |
| 244 | null |
| 245 | ); |
| 246 | }); |
| 247 | |
| 248 | test('Studio creates pinned workspaces and refuses expired or foreign refs without provider reads', async () => { |
| 249 | const f = fixture(); |
| 250 | const workspace = await create(f); |
| 251 | assert.equal(workspace.baseHeadSha, HEAD); |
| 252 | assert.equal(workspace.files[0].draftText, 'before\n'); |
| 253 | assert.equal(workspace.files[0].revision, 0); |
| 254 | assert.equal(Object.hasOwn(workspace.files[0], 'originalText'), false); |
| 255 | assert.equal(Object.hasOwn(workspace, 'originalText'), false); |
| 256 | const foreign = await f.workspaces.createNavigationRefs({ |
| 257 | ownerId: '99', |
| 258 | refs: [{ repoId: 123, path: 'secret.js', type: 'file' }] |
| 259 | }); |
| 260 | f.calls.length = 0; |
| 261 | const denied = await request(f, '/workspaces', { |
| 262 | method: 'POST', |
| 263 | body: { refId: foreign[0].refId } |
| 264 | }); |
| 265 | assert.equal(denied.status, 404); |
| 266 | assert.deepEqual(f.calls, []); |
| 267 | const [ref] = await f.workspaces.createNavigationRefs({ |
| 268 | ownerId: OWNER, |
| 269 | ttlMs: 1, |
| 270 | refs: [{ repoId: 123, path: 'old.js', type: 'file' }] |
| 271 | }); |
| 272 | f.advance(2); |
| 273 | assert.equal( |
| 274 | ( |
| 275 | await request(f, '/workspaces', { |
| 276 | method: 'POST', |
| 277 | body: { refId: ref.refId } |
| 278 | }) |
| 279 | ).status, |
| 280 | 404 |
| 281 | ); |
| 282 | assert.deepEqual(f.calls, []); |
| 283 | }); |
| 284 | |
| 285 | test('Studio enforces If-Match, membership, edit policy, diff, normalized messages and immediate deletion', async () => { |
| 286 | const f = fixture(); |
| 287 | const workspace = await create(f); |
| 288 | const base = `/workspaces/${workspace.id}`; |
| 289 | assert.equal( |
| 290 | ( |
| 291 | await request(f, `${base}/files`, { |
| 292 | method: 'PUT', |
| 293 | body: { path: workspace.path, text: 'after\n' } |
| 294 | }) |
| 295 | ).status, |
| 296 | 428 |
| 297 | ); |
| 298 | assert.equal( |
| 299 | ( |
| 300 | await request(f, `${base}/files`, { |
| 301 | method: 'PUT', |
| 302 | headers: { 'If-Match': '0' }, |
| 303 | body: { path: 'missing.js', text: 'after\n' } |
| 304 | }) |
| 305 | ).status, |
| 306 | 400 |
| 307 | ); |
| 308 | const update = await request(f, `${base}/files`, { |
| 309 | method: 'PUT', |
| 310 | headers: { 'If-Match': '"0"' }, |
| 311 | body: { path: workspace.path, text: 'after\n' } |
| 312 | }); |
| 313 | assert.equal(update.status, 200); |
| 314 | assert.equal((await update.json()).workspace.files[0].revision, 1); |
| 315 | assert.equal( |
| 316 | ( |
| 317 | await request(f, `${base}/files`, { |
| 318 | method: 'PUT', |
| 319 | headers: { 'If-Match': '0' }, |
| 320 | body: { path: workspace.path, text: 'stale\n' } |
| 321 | }) |
| 322 | ).status, |
| 323 | 409 |
| 324 | ); |
| 325 | assert.equal( |
| 326 | ( |
| 327 | await request(f, `${base}/files`, { |
| 328 | method: 'PUT', |
| 329 | headers: { 'If-Match': '1' }, |
| 330 | body: { path: workspace.path, text: 'x'.repeat(20001) } |
| 331 | }) |
| 332 | ).status, |
| 333 | 422 |
| 334 | ); |
| 335 | const diff = await (await request(f, `${base}/diff`)).json(); |
| 336 | assert.equal(diff.ok, true); |
| 337 | assert.match(JSON.stringify(diff.diff), /after/); |
| 338 | const message = await ( |
| 339 | await request(f, `${base}/message`, { |
| 340 | method: 'PUT', |
| 341 | body: { message: ' edit\n file ' } |
| 342 | }) |
| 343 | ).json(); |
| 344 | assert.equal(message.workspace.commitMessage, 'edit file'); |
| 345 | assert.equal((await request(f, `/workspaces/W123456789`)).status, 404); |
| 346 | assert.equal((await request(f, base, { method: 'DELETE' })).status, 200); |
| 347 | assert.equal((await request(f, base)).status, 404); |
| 348 | }); |
| 349 | |
| 350 | test('Studio workspace file refs are scoped to the same workspace and pinned repository', async () => { |
| 351 | const f = fixture(); |
| 352 | const first = await create(f); |
| 353 | const second = await create(f); |
| 354 | f.calls.length = 0; |
| 355 | assert.equal( |
| 356 | (await request(f, `/tree?repoId=456&workspaceId=${first.id}`)).status, |
| 357 | 400 |
| 358 | ); |
| 359 | assert.deepEqual(f.calls, []); |
| 360 | const tree = await ( |
| 361 | await request(f, `/tree?repoId=123&path=src&workspaceId=${first.id}`) |
| 362 | ).json(); |
| 363 | const refId = tree.items[0].refId; |
| 364 | const saved = await f.workspaces.resolveNavigationRef({ |
| 365 | ownerId: OWNER, |
| 366 | refId |
| 367 | }); |
| 368 | assert.equal(saved.type, 'workspace-add'); |
| 369 | assert.equal(saved.workspaceId, first.id); |
| 370 | assert.equal( |
| 371 | ( |
| 372 | await request(f, `/workspaces/${second.id}/files`, { |
| 373 | method: 'POST', |
| 374 | body: { refId } |
| 375 | }) |
| 376 | ).status, |
| 377 | 400 |
| 378 | ); |
| 379 | const added = await ( |
| 380 | await request(f, `/workspaces/${first.id}/files`, { |
| 381 | method: 'POST', |
| 382 | body: { refId } |
| 383 | }) |
| 384 | ).json(); |
| 385 | assert.equal(added.workspace.files.length, 2); |
| 386 | assert.equal(added.workspace.files[1].path, 'src/file-0.js'); |
| 387 | assert.equal(f.calls.at(-1)[3], HEAD); |
| 388 | }); |
| 389 | |
| 390 | test('Studio concurrent file additions enforce the durable file limit and return a bounded policy response', async () => { |
| 391 | const f = fixture(); |
| 392 | const workspace = await create(f); |
| 393 | const refs = await f.workspaces.createNavigationRefs({ |
| 394 | ownerId: OWNER, |
| 395 | refs: Array.from({ length: 8 }, (_, i) => ({ |
| 396 | repoId: 123, |
| 397 | workspaceId: workspace.id, |
| 398 | path: `extra-${i}.js`, |
| 399 | type: 'workspace-add' |
| 400 | })) |
| 401 | }); |
| 402 | const add = (refId) => |
| 403 | request(f, `/workspaces/${workspace.id}/files`, { |
| 404 | method: 'POST', |
| 405 | body: { refId } |
| 406 | }); |
| 407 | for (const ref of refs.slice(0, 6)) |
| 408 | assert.equal((await add(ref.refId)).status, 200); |
| 409 | const responses = await Promise.all( |
| 410 | refs.slice(6).map((ref) => add(ref.refId)) |
| 411 | ); |
| 412 | assert.deepEqual(responses.map((result) => result.status).sort(), [200, 422]); |
| 413 | const saved = await f.workspaces.getWorkspace({ |
| 414 | ownerId: OWNER, |
| 415 | workspaceId: workspace.id |
| 416 | }); |
| 417 | assert.equal(saved.files.length, WORKSPACE_EDITOR_LIMITS.maxFiles); |
| 418 | }); |
| 419 | |
| 420 | test('Studio approval seals immutable intent and sends only a signed approval to the fixed private owner without executing writes', async () => { |
| 421 | const f = fixture(); |
| 422 | const workspace = await create(f); |
| 423 | const base = `/workspaces/${workspace.id}`; |
| 424 | await request(f, `${base}/files`, { |
| 425 | method: 'PUT', |
| 426 | headers: { 'If-Match': '0' }, |
| 427 | body: { path: workspace.path, text: 'after\n' } |
| 428 | }); |
| 429 | await request(f, `${base}/message`, { |
| 430 | method: 'PUT', |
| 431 | body: { message: 'edit file' } |
| 432 | }); |
| 433 | assert.equal( |
| 434 | (await request(f, `${base}/approval`, { method: 'POST', body: {} })).status, |
| 435 | 400 |
| 436 | ); |
| 437 | assert.equal(f.messages.length, 0); |
| 438 | assert.equal( |
| 439 | ( |
| 440 | await f.workspaces.getWorkspace({ |
| 441 | ownerId: OWNER, |
| 442 | workspaceId: workspace.id |
| 443 | }) |
| 444 | ).currentIntentId, |
| 445 | null |
| 446 | ); |
| 447 | const { review } = await (await request(f, `${base}/diff`)).json(); |
| 448 | for (const message of [undefined, null, 7]) { |
| 449 | assert.equal( |
| 450 | ( |
| 451 | await request(f, `${base}/approval`, { |
| 452 | method: 'POST', |
| 453 | body: { review, message } |
| 454 | }) |
| 455 | ).status, |
| 456 | 400 |
| 457 | ); |
| 458 | } |
| 459 | f.calls.length = 0; |
| 460 | const response = await request(f, `${base}/approval`, { |
| 461 | method: 'POST', |
| 462 | body: { review, message: ' edit\nfile ' } |
| 463 | }); |
| 464 | assert.equal(response.status, 200); |
| 465 | const body = await response.json(); |
| 466 | assert.equal(body.approvalRequested, true); |
| 467 | assert.equal(body.workspaceId, workspace.id); |
| 468 | assert.equal(body.targetBranch, `nexa/edit-${workspace.id}`); |
| 469 | assert.equal(body.baseHeadSha, HEAD); |
| 470 | assert.equal(body.expiresAt, NOW + 120_000); |
| 471 | assert.equal(JSON.stringify(body).includes('approve:'), false); |
| 472 | assert.deepEqual(f.calls, []); |
| 473 | assert.equal(f.messages.length, 1); |
| 474 | assert.equal(f.messages[0][0], OWNER); |
| 475 | assert.equal(f.messages[0][1].includes('after'), false); |
| 476 | const button = f.messages[0][2].inline_keyboard |
| 477 | .flat() |
| 478 | .find((value) => value.callback_data?.startsWith('approve:')); |
| 479 | assert.ok(button); |
| 480 | const approved = await f.bot.approvals.verify(button.callback_data.slice(8), { |
| 481 | ownerId: OWNER |
| 482 | }); |
| 483 | assert.equal(approved.actionId, 'github.workspace.commit_branch'); |
| 484 | assert.equal(approved.risk, 'WRITE'); |
| 485 | const sealed = await f.workspaces.getWorkspace({ |
| 486 | ownerId: OWNER, |
| 487 | workspaceId: workspace.id |
| 488 | }); |
| 489 | assert.equal(approved.input.intentId, sealed.currentIntentId); |
| 490 | const intent = await f.workspaces.getIntent({ |
| 491 | ownerId: OWNER, |
| 492 | intentId: sealed.currentIntentId |
| 493 | }); |
| 494 | assert.equal(intent.baseHeadSha, HEAD); |
| 495 | assert.equal(intent.state, 'READY'); |
| 496 | assert.equal(intent.commitMessageDigest, await digestText('edit file')); |
| 497 | assert.equal( |
| 498 | ( |
| 499 | await request(f, `${base}/approval`, { |
| 500 | method: 'POST', |
| 501 | body: { review, chatId: '-100', approved: true } |
| 502 | }) |
| 503 | ).status, |
| 504 | 400 |
| 505 | ); |
| 506 | assert.equal(f.messages.length, 1); |
| 507 | }); |
| 508 | |
| 509 | test('Studio rejects oversized and malformed bodies and hides provider errors and draft source', async () => { |
| 510 | const f = fixture(); |
| 511 | assert.equal( |
| 512 | (await request(f, '/workspaces', { method: 'POST', body: '{' })).status, |
| 513 | 400 |
| 514 | ); |
| 515 | assert.equal( |
| 516 | ( |
| 517 | await request(f, '/workspaces', { |
| 518 | method: 'POST', |
| 519 | body: 'x'.repeat(5000) |
| 520 | }) |
| 521 | ).status, |
| 522 | 413 |
| 523 | ); |
| 524 | f.github.listRepositories = async () => { |
| 525 | throw new Error('github_pat_secret draft source'); |
| 526 | }; |
| 527 | const response = await request(f, '/repos'); |
| 528 | assert.equal(response.status, 502); |
| 529 | assert.equal((await response.text()).includes('github_pat_secret'), false); |
| 530 | }); |
| 531 | |
| 532 | test('Studio approval delivery and metadata failures return bounded errors without leaking the token or source', async () => { |
| 533 | for (const failAt of ['delivery', 'metadata']) { |
| 534 | const f = fixture(); |
| 535 | const workspace = await create(f); |
| 536 | const base = `/workspaces/${workspace.id}`; |
| 537 | await request(f, `${base}/files`, { |
| 538 | method: 'PUT', |
| 539 | headers: { 'If-Match': '0' }, |
| 540 | body: { path: workspace.path, text: 'after\n' } |
| 541 | }); |
| 542 | await request(f, `${base}/message`, { |
| 543 | method: 'PUT', |
| 544 | body: { message: 'edit file' } |
| 545 | }); |
| 546 | const { review } = await (await request(f, `${base}/diff`)).json(); |
| 547 | if (failAt === 'delivery') |
| 548 | f.bot.telegram.sendMessage = async () => { |
| 549 | throw new Error('secret-token source-text'); |
| 550 | }; |
| 551 | else |
| 552 | f.bot.router.dispatchCallback = async () => ({ requiresApproval: false }); |
| 553 | const response = await request(f, `${base}/approval`, { |
| 554 | method: 'POST', |
| 555 | body: { review, message: 'edit file' } |
| 556 | }); |
| 557 | assert.equal(response.status, failAt === 'delivery' ? 502 : 503); |
| 558 | const body = await response.text(); |
| 559 | assert.equal(body.includes('secret-token'), false); |
| 560 | assert.equal(body.includes('source-text'), false); |
| 561 | } |
| 562 | }); |
| 563 | |
| 564 | test('Studio approval rejects an older reviewed diff after another tab saves and requires a fresh diff', async () => { |
| 565 | const f = fixture(); |
| 566 | const workspace = await create(f); |
| 567 | const base = `/workspaces/${workspace.id}`; |
| 568 | const save = (revision, text) => |
| 569 | request(f, `${base}/files`, { |
| 570 | method: 'PUT', |
| 571 | headers: { 'If-Match': String(revision) }, |
| 572 | body: { path: workspace.path, text } |
| 573 | }); |
| 574 | await save(0, 'reviewed A\n'); |
| 575 | await request(f, `${base}/message`, { |
| 576 | method: 'PUT', |
| 577 | body: { message: 'edit file' } |
| 578 | }); |
| 579 | const oldDiff = await (await request(f, `${base}/diff`)).json(); |
| 580 | await save(1, 'unreviewed B\n'); |
| 581 | f.calls.length = 0; |
| 582 | const stale = await request(f, `${base}/approval`, { |
| 583 | method: 'POST', |
| 584 | body: { review: oldDiff.review, message: 'edit file' } |
| 585 | }); |
| 586 | assert.equal(stale.status, 409); |
| 587 | assert.equal(f.messages.length, 0); |
| 588 | assert.deepEqual(f.calls, []); |
| 589 | const currentDiff = await (await request(f, `${base}/diff`)).json(); |
| 590 | assert.deepEqual(currentDiff.review, { |
| 591 | baseHeadSha: HEAD, |
| 592 | files: [{ path: workspace.path, revision: 2 }] |
| 593 | }); |
| 594 | const fresh = await request(f, `${base}/approval`, { |
| 595 | method: 'POST', |
| 596 | body: { review: currentDiff.review, message: 'edit file' } |
| 597 | }); |
| 598 | assert.equal(fresh.status, 200); |
| 599 | assert.equal(f.messages.length, 1); |
| 600 | }); |
| 601 | |
| 602 | test('Studio review comparison is atomic when a save races intent sealing', async () => { |
| 603 | const f = fixture(); |
| 604 | const workspace = await create(f); |
| 605 | const base = `/workspaces/${workspace.id}`; |
| 606 | await request(f, `${base}/files`, { |
| 607 | method: 'PUT', |
| 608 | headers: { 'If-Match': '0' }, |
| 609 | body: { path: workspace.path, text: 'reviewed A\n' } |
| 610 | }); |
| 611 | await request(f, `${base}/message`, { |
| 612 | method: 'PUT', |
| 613 | body: { message: 'edit file' } |
| 614 | }); |
| 615 | const { review } = await (await request(f, `${base}/diff`)).json(); |
| 616 | const seal = f.workspaces.sealIntent.bind(f.workspaces); |
| 617 | f.workspaces.sealIntent = async (input) => { |
| 618 | await f.workspaces.updateWorkspaceFileDraft({ |
| 619 | ownerId: OWNER, |
| 620 | workspaceId: workspace.id, |
| 621 | path: workspace.path, |
| 622 | text: 'racing B\n', |
| 623 | digest: 'new-digest', |
| 624 | expectedRevision: 1 |
| 625 | }); |
| 626 | return seal(input); |
| 627 | }; |
| 628 | const response = await request(f, `${base}/approval`, { |
| 629 | method: 'POST', |
| 630 | body: { review, message: 'edit file' } |
| 631 | }); |
| 632 | assert.equal(response.status, 409); |
| 633 | assert.equal(f.messages.length, 0); |
| 634 | assert.equal( |
| 635 | ( |
| 636 | await f.workspaces.getWorkspace({ |
| 637 | ownerId: OWNER, |
| 638 | workspaceId: workspace.id |
| 639 | }) |
| 640 | ).currentIntentId, |
| 641 | null |
| 642 | ); |
| 643 | }); |
| 644 | |
| 645 | test('Studio approval binds the displayed commit message when another tab changes it during sealing', async () => { |
| 646 | const f = fixture(); |
| 647 | const workspace = await create(f); |
| 648 | const base = `/workspaces/${workspace.id}`; |
| 649 | await request(f, `${base}/files`, { |
| 650 | method: 'PUT', |
| 651 | headers: { 'If-Match': '0' }, |
| 652 | body: { path: workspace.path, text: 'reviewed file\n' } |
| 653 | }); |
| 654 | await request(f, `${base}/message`, { |
| 655 | method: 'PUT', |
| 656 | body: { message: 'reviewed message A' } |
| 657 | }); |
| 658 | const { review } = await (await request(f, `${base}/diff`)).json(); |
| 659 | const seal = f.workspaces.sealIntent.bind(f.workspaces); |
| 660 | f.workspaces.sealIntent = async (input) => { |
| 661 | await f.workspaces.setCommitMessage({ |
| 662 | ownerId: OWNER, |
| 663 | workspaceId: workspace.id, |
| 664 | message: 'unreviewed message B', |
| 665 | digest: await digestText('unreviewed message B') |
| 666 | }); |
| 667 | return seal(input); |
| 668 | }; |
| 669 | const response = await request(f, `${base}/approval`, { |
| 670 | method: 'POST', |
| 671 | body: { review, message: 'reviewed message A' } |
| 672 | }); |
| 673 | assert.equal(response.status, 409); |
| 674 | assert.equal(f.messages.length, 0); |
| 675 | const current = await f.workspaces.getWorkspace({ |
| 676 | ownerId: OWNER, |
| 677 | workspaceId: workspace.id |
| 678 | }); |
| 679 | assert.equal(current.currentIntentId, null); |
| 680 | assert.equal(current.commitMessage, 'unreviewed message B'); |
| 681 | }); |
| 682 | |
| 683 | test('Studio approval rejects a message changed after its displayed value was saved', async () => { |
| 684 | const f = fixture(); |
| 685 | const workspace = await create(f); |
| 686 | const base = `/workspaces/${workspace.id}`; |
| 687 | await request(f, `${base}/files`, { |
| 688 | method: 'PUT', |
| 689 | headers: { 'If-Match': '0' }, |
| 690 | body: { path: workspace.path, text: 'reviewed file\n' } |
| 691 | }); |
| 692 | await request(f, `${base}/message`, { |
| 693 | method: 'PUT', |
| 694 | body: { message: 'reviewed message A' } |
| 695 | }); |
| 696 | const { review } = await (await request(f, `${base}/diff`)).json(); |
| 697 | await request(f, `${base}/message`, { |
| 698 | method: 'PUT', |
| 699 | body: { message: 'unreviewed message B' } |
| 700 | }); |
| 701 | const issued = []; |
| 702 | const issue = f.bot.approvals.issue.bind(f.bot.approvals); |
| 703 | f.bot.approvals.issue = async (payload) => { |
| 704 | const token = await issue(payload); |
| 705 | issued.push(token); |
| 706 | return token; |
| 707 | }; |
| 708 | f.calls.length = 0; |
| 709 | const response = await request(f, `${base}/approval`, { |
| 710 | method: 'POST', |
| 711 | body: { review, message: 'reviewed message A' } |
| 712 | }); |
| 713 | assert.equal(response.status, 409); |
| 714 | assert.equal((await response.text()).includes('unreviewed message B'), false); |
| 715 | assert.equal(issued.length, 0); |
| 716 | assert.equal(f.messages.length, 0); |
| 717 | assert.deepEqual(f.calls, []); |
| 718 | const current = await f.workspaces.getWorkspace({ |
| 719 | ownerId: OWNER, |
| 720 | workspaceId: workspace.id |
| 721 | }); |
| 722 | assert.equal(current.currentIntentId, null); |
| 723 | assert.equal(current.state, 'DRAFT'); |
| 724 | assert.equal(current.commitMessage, 'unreviewed message B'); |
| 725 | }); |
| 726 | |
| 727 | test('Studio approval rejects an unreviewed proposed message even if the current message is restored', async () => { |
| 728 | const f = fixture(); |
| 729 | const workspace = await create(f); |
| 730 | const base = `/workspaces/${workspace.id}`; |
| 731 | await request(f, `${base}/files`, { |
| 732 | method: 'PUT', |
| 733 | headers: { 'If-Match': '0' }, |
| 734 | body: { path: workspace.path, text: 'reviewed file\n' } |
| 735 | }); |
| 736 | await request(f, `${base}/message`, { |
| 737 | method: 'PUT', |
| 738 | body: { message: 'reviewed message A' } |
| 739 | }); |
| 740 | const { review } = await (await request(f, `${base}/diff`)).json(); |
| 741 | await request(f, `${base}/message`, { |
| 742 | method: 'PUT', |
| 743 | body: { message: 'unreviewed message B' } |
| 744 | }); |
| 745 | const seal = f.workspaces.sealIntent.bind(f.workspaces); |
| 746 | f.workspaces.sealIntent = async (input) => { |
| 747 | await f.workspaces.setCommitMessage({ |
| 748 | ownerId: OWNER, |
| 749 | workspaceId: workspace.id, |
| 750 | message: 'reviewed message A', |
| 751 | digest: await digestText('reviewed message A') |
| 752 | }); |
| 753 | return seal(input); |
| 754 | }; |
| 755 | f.calls.length = 0; |
| 756 | const response = await request(f, `${base}/approval`, { |
| 757 | method: 'POST', |
| 758 | body: { review, message: 'reviewed message A' } |
| 759 | }); |
| 760 | assert.equal(response.status, 409); |
| 761 | assert.equal(f.messages.length, 0); |
| 762 | assert.deepEqual(f.calls, []); |
| 763 | const current = await f.workspaces.getWorkspace({ |
| 764 | ownerId: OWNER, |
| 765 | workspaceId: workspace.id |
| 766 | }); |
| 767 | assert.equal(current.currentIntentId, null); |
| 768 | assert.equal(current.commitMessage, 'reviewed message A'); |
| 769 | }); |
| 770 | |
| 771 | test('Studio approval keeps the exact sealed intent when another tab seals a replacement', async () => { |
| 772 | const f = fixture(); |
| 773 | const workspace = await create(f); |
| 774 | const base = `/workspaces/${workspace.id}`; |
| 775 | await request(f, `${base}/files`, { |
| 776 | method: 'PUT', |
| 777 | headers: { 'If-Match': '0' }, |
| 778 | body: { path: workspace.path, text: 'reviewed file\n' } |
| 779 | }); |
| 780 | await request(f, `${base}/message`, { |
| 781 | method: 'PUT', |
| 782 | body: { message: 'reviewed message A' } |
| 783 | }); |
| 784 | const { review } = await (await request(f, `${base}/diff`)).json(); |
| 785 | const seal = f.workspaces.sealIntent.bind(f.workspaces); |
| 786 | let reviewedIntent; |
| 787 | f.workspaces.sealIntent = async (input) => { |
| 788 | reviewedIntent = await seal(input); |
| 789 | f.workspaces.sealIntent = seal; |
| 790 | await f.workspaces.setCommitMessage({ |
| 791 | ownerId: OWNER, |
| 792 | workspaceId: workspace.id, |
| 793 | message: 'unreviewed message B', |
| 794 | digest: await digestText('unreviewed message B') |
| 795 | }); |
| 796 | await createWorkspaceController(f).sealWorkspaceIntent({ |
| 797 | ownerId: OWNER, |
| 798 | workspaceId: workspace.id |
| 799 | }); |
| 800 | return reviewedIntent; |
| 801 | }; |
| 802 | f.calls.length = 0; |
| 803 | const response = await request(f, `${base}/approval`, { |
| 804 | method: 'POST', |
| 805 | body: { review, message: 'reviewed message A' } |
| 806 | }); |
| 807 | assert.equal(response.status, 200); |
| 808 | assert.equal(f.messages.length, 1); |
| 809 | const button = f.messages[0][2].inline_keyboard |
| 810 | .flat() |
| 811 | .find((value) => value.callback_data?.startsWith('approve:')); |
| 812 | const approved = await f.bot.approvals.verify(button.callback_data.slice(8), { |
| 813 | ownerId: OWNER |
| 814 | }); |
| 815 | assert.equal(approved.input.intentId, reviewedIntent.id); |
| 816 | assert.equal( |
| 817 | reviewedIntent.commitMessageDigest, |
| 818 | await digestText('reviewed message A') |
| 819 | ); |
| 820 | const current = await f.workspaces.getWorkspace({ |
| 821 | ownerId: OWNER, |
| 822 | workspaceId: workspace.id |
| 823 | }); |
| 824 | assert.notEqual(current.currentIntentId, reviewedIntent.id); |
| 825 | await assert.rejects( |
| 826 | commitWorkspaceIntent({ |
| 827 | ...f, |
| 828 | ownerId: OWNER, |
| 829 | repoId: 123, |
| 830 | intentId: reviewedIntent.id |
| 831 | }), |
| 832 | /Workspace material changed/i |
| 833 | ); |
| 834 | assert.deepEqual(f.calls, []); |
| 835 | }); |
| 836 | |
| 837 | test('Studio rejects unsafe owner configuration, malformed paths and foreign workspace identifiers', async () => { |
| 838 | const f = fixture(); |
| 839 | f.env.OWNER_TELEGRAM_ID = '-100'; |
| 840 | assert.equal((await request(f, '/repos')).status, 503); |
| 841 | assert.deepEqual(f.calls, []); |
| 842 | assert.equal(f.states.size, 0); |
| 843 | f.env.OWNER_TELEGRAM_ID = OWNER; |
| 844 | for (const path of ['../secret', '/secret', 'src//a', 'src\\a', 'src\0a']) { |
| 845 | assert.equal( |
| 846 | (await request(f, `/tree?repoId=123&path=${encodeURIComponent(path)}`)) |
| 847 | .status, |
| 848 | 400 |
| 849 | ); |
| 850 | } |
| 851 | assert.deepEqual(f.calls, []); |
| 852 | const foreign = await f.workspaces.createWorkspace({ |
| 853 | ownerId: '99', |
| 854 | repoId: 123, |
| 855 | path: 'foreign.js', |
| 856 | originalText: 'private', |
| 857 | originalDigest: 'digest', |
| 858 | baseBranch: 'main', |
| 859 | baseHeadSha: HEAD, |
| 860 | baseTreeSha: TREE |
| 861 | }); |
| 862 | for (const [method, suffix, body] of [ |
| 863 | ['GET', '', undefined], |
| 864 | ['DELETE', '', undefined], |
| 865 | ['PUT', '/files', { path: 'foreign.js', text: 'attacker' }], |
| 866 | ['POST', '/approval', {}] |
| 867 | ]) { |
| 868 | assert.equal( |
| 869 | ( |
| 870 | await request(f, `/workspaces/${foreign.id}${suffix}`, { |
| 871 | method, |
| 872 | body, |
| 873 | headers: { 'If-Match': '0' } |
| 874 | }) |
| 875 | ).status, |
| 876 | 404 |
| 877 | ); |
| 878 | } |
| 879 | assert.equal( |
| 880 | ( |
| 881 | await f.workspaces.getWorkspace({ |
| 882 | ownerId: '99', |
| 883 | workspaceId: foreign.id |
| 884 | }) |
| 885 | ).draftText, |
| 886 | 'private' |
| 887 | ); |
| 888 | }); |
| 889 | |
| 890 | test('GitHub repository listing traverses all Link pages at per_page=100 without leaking credentials to another origin', async () => { |
| 891 | const originalFetch = globalThis.fetch; |
| 892 | const requested = []; |
| 893 | globalThis.fetch = async (url) => { |
| 894 | requested.push(String(url)); |
| 895 | const page = new URL(url).searchParams.get('page'); |
| 896 | return Response.json( |
| 897 | [{ id: page === '2' ? 2 : 1, name: 'repo', full_name: 'owner/repo' }], |
| 898 | { |
| 899 | headers: |
| 900 | page === '2' |
| 901 | ? {} |
| 902 | : { |
| 903 | Link: '<https://api.github.com/user/repos?per_page=100&page=2>; rel="next"' |
| 904 | } |
| 905 | } |
| 906 | ); |
| 907 | }; |
| 908 | try { |
| 909 | const repos = await new GitHubService('github_pat_test').listRepositories(); |
| 910 | assert.equal(repos.length, 2); |
| 911 | assert.equal(new URL(requested[0]).searchParams.get('per_page'), '100'); |
| 912 | assert.equal(requested.length, 2); |
| 913 | requested.length = 0; |
| 914 | globalThis.fetch = async (url) => { |
| 915 | requested.push(String(url)); |
| 916 | return Response.json([], { |
| 917 | headers: { |
| 918 | Link: '<https://attacker.test/user/repos?page=2>; rel="next"' |
| 919 | } |
| 920 | }); |
| 921 | }; |
| 922 | await assert.rejects( |
| 923 | () => new GitHubService('github_pat_test').listRepositories(), |
| 924 | /pagination|page/i |
| 925 | ); |
| 926 | assert.equal(requested.length, 1); |
| 927 | } finally { |
| 928 | globalThis.fetch = originalFetch; |
| 929 | } |
| 930 | }); |