-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathhmac.ts
More file actions
332 lines (292 loc) · 10.5 KB
/
hmac.ts
File metadata and controls
332 lines (292 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import { expect } from 'chai';
import * as sinon from 'sinon';
import {
calculateHMAC,
calculateHMACSubject,
calculateRequestHMAC,
calculateRequestHeaders,
verifyResponse,
} from '../src/hmac';
import * as sjcl from '@bitgo/sjcl';
import { createSecretKey } from 'crypto';
// Mock Date.now for consistent timestamp values
const MOCK_TIMESTAMP = 1672531200000; // Example timestamp (e.g., Jan 1, 2023, 00:00:00 UTC)
describe('HMAC Utility Functions', () => {
let clock;
before(() => {
clock = sinon.useFakeTimers(MOCK_TIMESTAMP);
});
after(() => {
clock.restore();
});
describe('calculateHMAC', () => {
it('should calculate the correct HMAC for a given key and message', () => {
const key = 'test-key';
const message = 'test-message';
const expectedHmac = 'f8c2bb87c17608c9038eab4e92ef2775e42629c939d6fd3390d42f80af6bb712';
expect(calculateHMAC(key, message)).to.equal(expectedHmac);
});
it('should accept a Buffer key (BinaryLike) and match the string result', () => {
const keyBuffer = Buffer.from('test-key', 'utf8');
const message = Buffer.from('test-message');
const expectedHmac = 'f8c2bb87c17608c9038eab4e92ef2775e42629c939d6fd3390d42f80af6bb712';
expect(calculateHMAC(keyBuffer, message)).to.equal(expectedHmac);
});
it('should accept a KeyObject key and match the string result', () => {
const keyObject = createSecretKey(Buffer.from('test-key', 'utf8'));
const message = 'test-message';
const expectedHmac = 'f8c2bb87c17608c9038eab4e92ef2775e42629c939d6fd3390d42f80af6bb712';
expect(calculateHMAC(keyObject, message)).to.equal(expectedHmac);
});
});
describe('calculateHMACSubject', () => {
it('should calculate the correct subject for a request', () => {
const expectedSubject = 'GET|1672531200000|3.0|/api/test?query=123|body-content';
expect(
calculateHMACSubject({
urlPath: '/api/test?query=123',
text: 'body-content',
timestamp: MOCK_TIMESTAMP,
method: 'get',
authVersion: 3,
})
).to.equal(expectedSubject);
});
it('should calculate the correct subject for a request with a trailing ? when useOriginalPath is true', () => {
const expectedSubject = 'GET|1672531200000|3.0|/api/test?|body-content';
expect(
calculateHMACSubject(
{
urlPath: '/api/test?',
text: 'body-content',
timestamp: MOCK_TIMESTAMP,
method: 'get',
authVersion: 3,
},
true
)
).to.equal(expectedSubject);
});
it('should calculate the correct subject for a request with a trailing ? when useOriginalPath is false', () => {
const expectedSubject = 'GET|1672531200000|3.0|/api/test|body-content';
expect(
calculateHMACSubject({
urlPath: '/api/test?',
text: 'body-content',
timestamp: MOCK_TIMESTAMP,
method: 'get',
authVersion: 3,
})
).to.equal(expectedSubject);
});
it('should include statusCode for a response', () => {
const expectedSubject = 'GET|1672531200000|/api/test|200|response-body';
expect(
calculateHMACSubject({
urlPath: '/api/test',
text: 'response-body',
timestamp: MOCK_TIMESTAMP,
statusCode: 200,
method: 'get',
authVersion: 3,
})
).to.equal(expectedSubject);
});
it('should not include undefined for a response when text is undefined', () => {
const expectedSubject = 'GET|1672531200000|/api/test|200|';
expect(
calculateHMACSubject({
urlPath: '/api/test',
text: undefined as unknown as string,
timestamp: MOCK_TIMESTAMP,
statusCode: 200,
method: 'get',
authVersion: 3,
})
).to.equal(expectedSubject);
});
it('should handle Buffer text input and return a Buffer for requests', () => {
const buffer = Buffer.from('binary-data-content');
const result = calculateHMACSubject({
urlPath: '/api/test',
text: buffer,
timestamp: MOCK_TIMESTAMP,
method: 'get',
authVersion: 3,
});
expect(Buffer.isBuffer(result)).to.be.true;
// Check the content structure
const expectedPrefix = 'GET|1672531200000|3.0|/api/test|';
const prefixBuffer = Buffer.from(expectedPrefix, 'utf8');
// Manually reconstruct the expected buffer to compare
const expectedBuffer = Buffer.concat([prefixBuffer, buffer]);
expect(result).to.deep.equal(expectedBuffer);
});
it('should handle Buffer undefined text input and return a Buffer for requests', () => {
const buffer = undefined as unknown as Buffer;
const result = calculateHMACSubject({
urlPath: '/api/test',
text: buffer,
timestamp: MOCK_TIMESTAMP,
method: 'get',
authVersion: 3,
});
expect(Buffer.isBuffer(result)).to.be.false;
const expectedSubject = 'GET|1672531200000|3.0|/api/test|';
expect(result).to.deep.equal(expectedSubject);
});
it('should handle Buffer text input and return a Buffer for responses', () => {
const buffer = Buffer.from('binary-response-data');
const result = calculateHMACSubject({
urlPath: '/api/test',
text: buffer,
timestamp: MOCK_TIMESTAMP,
statusCode: 200,
method: 'get',
authVersion: 3,
});
expect(Buffer.isBuffer(result)).to.be.true;
// Check the content structure
const expectedPrefix = 'GET|1672531200000|/api/test|200|';
const prefixBuffer = Buffer.from(expectedPrefix, 'utf8');
// Manually reconstruct the expected buffer to compare
const expectedBuffer = Buffer.concat([prefixBuffer, buffer]);
expect(result).to.deep.equal(expectedBuffer);
});
});
describe('calculateRequestHMAC', () => {
it('should calculate the correct HMAC for a request', () => {
const expectedHmac = '56b7c2bb722ebfa55600a0201af42ad5cd926340d9df5735005d91db452386d1';
expect(
calculateRequestHMAC({
url: '/api/test',
text: 'request-body',
timestamp: MOCK_TIMESTAMP,
token: 'test-token',
method: 'post',
authVersion: 3,
})
).to.equal(expectedHmac);
});
});
describe('calculateRequestHeaders', () => {
it('should calculate the correct headers with HMAC', () => {
const headers = calculateRequestHeaders({
url: '/api/test',
text: 'request-body',
token: 'test-token',
method: 'post',
authVersion: 3,
});
const hashDigest = sjcl.hash.sha256.hash('test-token');
const tokenHash = sjcl.codec.hex.fromBits(hashDigest);
expect(headers).to.include({
hmac: headers.hmac, // Verify hmac exists
timestamp: MOCK_TIMESTAMP,
tokenHash,
});
});
});
describe('verifyResponse', () => {
it('should verify the HMAC and timestamp validity', () => {
const result = verifyResponse({
url: '/api/test',
statusCode: 200,
text: 'response-body',
timestamp: MOCK_TIMESTAMP,
token: 'test-token',
hmac: 'a16c08b1fa8bff1e2e58d1831855e1745361f78bd6eb6e18b5b7ee17ae0a3bb7',
method: 'post',
authVersion: 3,
});
expect(result).to.include({
isValid: true,
expectedHmac: 'a16c08b1fa8bff1e2e58d1831855e1745361f78bd6eb6e18b5b7ee17ae0a3bb7',
isInResponseValidityWindow: true,
});
});
it('should return invalid if HMAC does not match', () => {
const result = verifyResponse({
url: '/api/test',
statusCode: 200,
text: 'response-body',
timestamp: MOCK_TIMESTAMP,
token: 'wrong-token',
hmac: 'invalid-hmac',
method: 'post',
authVersion: 3,
});
expect(result.isValid).to.be.false;
});
it('should return invalid if timestamp is outside the validity window (backwards check)', () => {
const result = verifyResponse({
url: '/api/test',
statusCode: 200,
text: 'response-body',
timestamp: MOCK_TIMESTAMP - 1000 * 60 * 10, // 10 minutes in the past
token: 'test-token',
hmac: '8f6a2d183e4c4f2bd2023202486e1651292c84573a31b3829d394f1763a6ec6c',
method: 'post',
authVersion: 3,
});
expect(result.isInResponseValidityWindow).to.be.false;
});
it('should return invalid if timestamp is outside the validity window (forwards check)', () => {
const result = verifyResponse({
url: '/api/test',
statusCode: 200,
text: 'response-body',
timestamp: MOCK_TIMESTAMP + 1000 * 60 * 2, // 2 minutes in the future
token: 'test-token',
hmac: '8f6a2d183e4c4f2bd2023202486e1651292c84573a31b3829d394f1763a6ec6c',
method: 'post',
authVersion: 3,
});
expect(result.isInResponseValidityWindow).to.be.false;
});
it('should verify if timestamp is inside the forward validity window', () => {
const result = verifyResponse({
url: '/api/test',
statusCode: 200,
text: 'response-body',
timestamp: MOCK_TIMESTAMP + 1000 * 30, // 30 seconds in the future
token: 'test-token',
hmac: '5e08c494691951ee45a6fc0e3fbfce3a76fcb5b9ee37e9bf9f2ac66690466dc7',
method: 'post',
authVersion: 3,
});
expect(result).to.include({
isValid: true,
isInResponseValidityWindow: true,
});
});
it('should verify response with Buffer data', () => {
const responseData = Buffer.from('binary-response-data');
// First create an HMAC for this binary data
const signatureSubject = calculateHMACSubject({
urlPath: '/api/test',
text: responseData,
timestamp: MOCK_TIMESTAMP,
statusCode: 200,
method: 'post',
authVersion: 3,
});
const token = 'test-token';
const expectedHmac = calculateHMAC(token, signatureSubject);
// Now verify using the generated HMAC
const result = verifyResponse({
url: '/api/test',
statusCode: 200,
text: responseData, // Use binary data here
timestamp: MOCK_TIMESTAMP,
token: token,
hmac: expectedHmac,
method: 'post',
authVersion: 3,
});
expect(result.isValid).to.be.true;
expect(result.expectedHmac).to.equal(expectedHmac);
expect(Buffer.isBuffer(result.signatureSubject)).to.be.true;
});
});
});