Personal Telegram developer console for GitHub
feat: use durable replay protection for approvals
davoodmehraban89 committed
Oct 3, 2026 at 06:31 UTC
7ea79fe520c3580b5ce4cf929b2ea5bbef633461
1 file changed
+18
-10
src/core/approval.js
+18
-10
@@ -1,9 +1,5 @@
1
const encoder = new TextEncoder();
2
-
3
-// Shared within a Worker isolate so approval replay state survives across
4
-// separate NexaDevBot instances created for individual webhook requests.
5
-// A durable cross-isolate store is wired in the hardening phase.
6
-const sharedUsedNonces = new Set();
2
+const SIGNING_DOMAIN = 'nexa-approval-v1';
3
4
const DEFAULT_CODECS = new Map([
5
['github.workflow.run.rerun_failed', {
@@ -72,20 +68,24 @@ export class ApprovalService {
68
ttlMs = 120_000,
69
maxTokenLength = 56,
70
signatureBytes = 12,
75
- replayStore = sharedUsedNonces
71
+ replayStore = null
72
} = {}) {
73
this.secret = String(secret || '');
74
this.now = now;
75
this.ttlMs = ttlMs;
76
this.maxTokenLength = maxTokenLength;
77
this.signatureBytes = signatureBytes;
82
- if (!replayStore || typeof replayStore.has !== 'function' || typeof replayStore.add !== 'function') {
78
+ if (replayStore && typeof replayStore.consume !== 'function') {
79
throw new Error('Approval replay store is invalid');
80
}
81
this.replayStore = replayStore;
82
this.keyPromise = null;
83
}
84
85
+ isReplayProtected() {
86
+ return Boolean(this.replayStore && typeof this.replayStore.consume === 'function');
87
+ }
88
+
89
async issue(payload = {}) {
90
this.#assertSecret();
91
const issuedAt = this.now();
@@ -145,8 +145,16 @@ export class ApprovalService {
145
146
async consume(token, expectations = {}) {
147
const payload = await this.verify(token, expectations);
148
- if (this.replayStore.has(payload.nonce)) throw new Error('Approval token already used');
149
- this.replayStore.add(payload.nonce);
148
+ if (!this.isReplayProtected()) {
149
+ throw new Error('Durable approval replay protection is not configured');
150
+ }
151
+
152
+ const accepted = await this.replayStore.consume({
153
+ ownerId: payload.ownerId,
154
+ nonce: payload.nonce,
155
+ expiresAt: payload.expiresAt
156
+ });
157
+ if (!accepted) throw new Error('Approval token already used');
158
return payload;
159
}
160
@@ -213,7 +221,7 @@ export class ApprovalService {
221
const signature = await crypto.subtle.sign(
222
'HMAC',
223
key,
216
- encoder.encode(`${ownerId}|${body}`)
224
+ encoder.encode(`${SIGNING_DOMAIN}|${ownerId}|${body}`)
225
);
226
return new Uint8Array(signature);
227
}