Personal Telegram developer console for GitHub
feat(cp4): add compact durable approval for exact Cloudflare version
davoodmehraban89 committed
Oct 5, 2026 at 06:06 UTC
1366d87a8c9dfea352b49e38d956cfe757c1c8a2
1 file changed
+17
-7
src/core/approval.js
+17
-7
@@ -2,6 +2,7 @@ const encoder = new TextEncoder();
2
const SIGNING_DOMAIN = 'nexa-approval-v1';
3
const INTENT_ID_PATTERN = /^[A-Za-z0-9_-]{8}$/;
4
const FULL_SHA_PATTERN = /^[a-f0-9]{40}$/i;
5
+const VERSION_ID_PATTERN = /^[0-9a-f-]{36}$/i;
6
const DEFAULT_CODECS = new Map([
7
['github.workflow.run.rerun_failed', { code: 'r', pluginId: 'github', risk: 'WRITE', target: 'run' }],
8
['github.workflow.run.cancel', { code: 'c', pluginId: 'github', risk: 'WRITE', target: 'run' }],
@@ -25,12 +26,20 @@ export class ApprovalService {
26
const normalized = { v: 1, pluginId: payload.pluginId, actionId: payload.actionId, risk: payload.risk, ownerId: String(payload.ownerId ?? ''), input: payload.input ?? {}, issuedAt, expiresAt, nonce };
27
if (!normalized.ownerId) throw new Error('Approval owner is required');
28
let body;
28
- if (normalized.actionId === 'github.pull_request.merge') {
29
- const { repoId, prNumber, headSha } = normalized.input || {};
30
- if (normalized.pluginId !== 'github' || normalized.risk !== 'DESTRUCTIVE' || !Number.isSafeInteger(Number(repoId)) || !Number.isSafeInteger(Number(prNumber)) || !FULL_SHA_PATTERN.test(String(headSha || ''))) throw new Error('Invalid pull request approval target');
29
+ if (normalized.actionId === 'github.pull_request.merge' || normalized.actionId === 'cloudflare.worker.deploy_version') {
30
+ let code = '';
31
+ if (normalized.actionId === 'github.pull_request.merge') {
32
+ const { repoId, prNumber, headSha } = normalized.input || {};
33
+ if (normalized.pluginId !== 'github' || normalized.risk !== 'DESTRUCTIVE' || !Number.isSafeInteger(Number(repoId)) || !Number.isSafeInteger(Number(prNumber)) || !FULL_SHA_PATTERN.test(String(headSha || ''))) throw new Error('Invalid pull request approval target');
34
+ code = 'm';
35
+ } else {
36
+ const { versionId } = normalized.input || {};
37
+ if (normalized.pluginId !== 'cloudflare' || normalized.risk !== 'DESTRUCTIVE' || !VERSION_ID_PATTERN.test(String(versionId || ''))) throw new Error('Invalid Cloudflare approval target');
38
+ code = 'c';
39
+ }
40
if (!this.replayStore || typeof this.replayStore.put !== 'function' || typeof this.replayStore.peek !== 'function') throw new Error('Durable approval context storage is not configured');
41
await this.replayStore.put({ ownerId: normalized.ownerId, nonce, expiresAt, payload: normalized });
33
- body = `s.m.${toBase36Int(expiresAt)}.${nonce}`;
42
+ body = `s.${code}.${toBase36Int(expiresAt)}.${nonce}`;
43
} else body = this.#encodeBody(normalized);
44
const signature = await this.#sign(normalized.ownerId, body); const token = `${body}.${bytesToBase64Url(signature.slice(0, this.signatureBytes))}`;
45
if (token.length > this.maxTokenLength) throw new Error('Approval token too large for callback data'); return token;
@@ -41,11 +50,12 @@ export class ApprovalService {
50
try { suppliedSignature = base64UrlToBytes(signaturePart); } catch { throw new Error('Invalid approval signature'); }
51
const expectedSignature = (await this.#sign(ownerId, body)).slice(0, this.signatureBytes); if (!constantTimeEqual(suppliedSignature, expectedSignature)) throw new Error('Invalid approval signature or owner mismatch');
52
let payload;
44
- if (body.startsWith('s.m.')) {
45
- const state = body.split('.'); if (state.length !== 4) throw new Error('Invalid approval payload'); const expiresAt = fromBase36Int(state[2]); const nonce = state[3]; if (expiresAt < this.now()) throw new Error('Approval token expired');
53
+ if (body.startsWith('s.m.') || body.startsWith('s.c.')) {
54
+ const state = body.split('.'); if (state.length !== 4) throw new Error('Invalid approval payload'); const code = state[1]; const expiresAt = fromBase36Int(state[2]); const nonce = state[3]; if (expiresAt < this.now()) throw new Error('Approval token expired');
55
if (!this.replayStore || typeof this.replayStore.peek !== 'function') throw new Error('Durable approval context storage is not configured');
56
payload = await this.replayStore.peek({ ownerId, nonce }); if (!payload) throw new Error('Approval context is unavailable');
48
- if (payload.expiresAt !== expiresAt || payload.nonce !== nonce || payload.ownerId !== ownerId || payload.actionId !== 'github.pull_request.merge') throw new Error('Approval context mismatch');
57
+ const expectedAction = code === 'm' ? 'github.pull_request.merge' : code === 'c' ? 'cloudflare.worker.deploy_version' : '';
58
+ if (!expectedAction || payload.expiresAt !== expiresAt || payload.nonce !== nonce || payload.ownerId !== ownerId || payload.actionId !== expectedAction) throw new Error('Approval context mismatch');
59
} else payload = this.#decodeBody(body, ownerId);
60
if (payload.expiresAt < this.now()) throw new Error('Approval token expired'); if (expectations.pluginId && payload.pluginId !== expectations.pluginId) throw new Error('Approval plugin mismatch'); if (expectations.actionId && payload.actionId !== expectations.actionId) throw new Error('Approval action mismatch'); if (expectations.risk && payload.risk !== expectations.risk) throw new Error('Approval risk mismatch'); return payload;
61
}