Personal Telegram developer console for GitHub

test: define approval token security contract

davoodmehraban89 committed Oct 3, 2026 at 05:23 UTC ac7cd800cd4ca602fa5682d835ace735e4324efb
1 file changed +105
test/approval.test.js new
+105
@@ -0,0 +1,105 @@
1 +import test from 'node:test';
2 +import assert from 'node:assert/strict';
3 +
4 +const approvalModule = await import('../src/core/approval.js').catch(() => null);
5 +
6 +test('ApprovalService module exists', () => {
7 + assert.ok(approvalModule, 'ApprovalService module must exist');
8 + assert.equal(typeof approvalModule?.ApprovalService, 'function');
9 +});
10 +
11 +test('ApprovalService issues and verifies a signed owner-bound token', async () => {
12 + assert.ok(approvalModule?.ApprovalService, 'ApprovalService module must exist');
13 + let now = 1_700_000_000_000;
14 + const service = new approvalModule.ApprovalService({
15 + secret: 'test-signing-secret',
16 + now: () => now,
17 + ttlMs: 60_000
18 + });
19 +
20 + const token = await service.issue({
21 + pluginId: 'github',
22 + actionId: 'github.workflow.run.rerun_failed',
23 + risk: 'WRITE',
24 + ownerId: '42',
25 + input: { repoId: 7, runId: 9 }
26 + });
27 +
28 + const payload = await service.verify(token, {
29 + ownerId: '42',
30 + pluginId: 'github',
31 + actionId: 'github.workflow.run.rerun_failed',
32 + risk: 'WRITE'
33 + });
34 +
35 + assert.equal(payload.v, 1);
36 + assert.equal(payload.ownerId, '42');
37 + assert.equal(payload.pluginId, 'github');
38 + assert.equal(payload.actionId, 'github.workflow.run.rerun_failed');
39 + assert.deepEqual(payload.input, { repoId: 7, runId: 9 });
40 + assert.equal(typeof payload.nonce, 'string');
41 + assert.ok(payload.nonce.length >= 8);
42 + assert.equal(payload.issuedAt, now);
43 + assert.equal(payload.expiresAt, now + 60_000);
44 +});
45 +
46 +test('ApprovalService rejects tampered, expired and mismatched tokens', async () => {
47 + assert.ok(approvalModule?.ApprovalService, 'ApprovalService module must exist');
48 + let now = 1_700_000_000_000;
49 + const service = new approvalModule.ApprovalService({
50 + secret: 'test-signing-secret',
51 + now: () => now,
52 + ttlMs: 1000
53 + });
54 + const token = await service.issue({
55 + pluginId: 'github',
56 + actionId: 'github.workflow.run.cancel',
57 + risk: 'WRITE',
58 + ownerId: '42',
59 + input: { repoId: 7, runId: 9 }
60 + });
61 +
62 + await assert.rejects(() => service.verify(`${token}x`, { ownerId: '42' }), /invalid approval/i);
63 + await assert.rejects(() => service.verify(token, { ownerId: '99' }), /owner/i);
64 + await assert.rejects(() => service.verify(token, { actionId: 'github.workflow.run.rerun_failed' }), /action/i);
65 + now += 2000;
66 + await assert.rejects(() => service.verify(token, { ownerId: '42' }), /expired/i);
67 +});
68 +
69 +test('ApprovalService consume is one-time and rejects replay', async () => {
70 + assert.ok(approvalModule?.ApprovalService, 'ApprovalService module must exist');
71 + const service = new approvalModule.ApprovalService({
72 + secret: 'test-signing-secret',
73 + now: () => 1_700_000_000_000
74 + });
75 + const token = await service.issue({
76 + pluginId: 'github',
77 + actionId: 'github.workflow.run.cancel',
78 + risk: 'WRITE',
79 + ownerId: '42',
80 + input: { repoId: 7, runId: 9 }
81 + });
82 +
83 + const first = await service.consume(token, { ownerId: '42' });
84 + assert.equal(first.actionId, 'github.workflow.run.cancel');
85 + await assert.rejects(() => service.consume(token, { ownerId: '42' }), /already used/i);
86 +});
87 +
88 +test('ApprovalService fails closed for missing secret and oversized callback token', async () => {
89 + assert.ok(approvalModule?.ApprovalService, 'ApprovalService module must exist');
90 + const missing = new approvalModule.ApprovalService({ secret: '' });
91 + await assert.rejects(() => missing.issue({ pluginId: 'github' }), /secret/i);
92 +
93 + const service = new approvalModule.ApprovalService({
94 + secret: 'test-signing-secret',
95 + maxTokenLength: 64,
96 + now: () => 1_700_000_000_000
97 + });
98 + await assert.rejects(() => service.issue({
99 + pluginId: 'github',
100 + actionId: 'github.workflow.run.cancel',
101 + risk: 'WRITE',
102 + ownerId: '42',
103 + input: { text: 'x'.repeat(200) }
104 + }), /too large/i);
105 +});