-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SignedAccess.spec.js
458 lines (373 loc) · 20.1 KB
/
SignedAccess.spec.js
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
'use strict'
const crypto = require('node:crypto')
const SignedAccess = require('../src/SignedAccess.js')
const key = '9x,9Ku9TrUL'
const signedAccess = new SignedAccess(key)
describe('constructor', () => {
test('type guards', () => {
['xpto', 0, false, null].forEach(input => expect(() => new SignedAccess(key, { algorithm: input })).toThrowError(new TypeError('Invalid algorithm')));
['xpto', '', 0, Infinity, NaN, false, null].forEach(input => expect(() => new SignedAccess(key, { ttl: input })).toThrow('Invalid ttl'));
[false, null, undefined].forEach(input => expect(() => new SignedAccess(input)).toThrowError(new TypeError('Invalid key')))
expect(() => new SignedAccess(undefined, {
algorithm: null,
ttl: null
})).toThrowError(new AggregateError([
new TypeError('Invalid key'),
new TypeError('Invalid algorithm'),
new Error('Invalid ttl')
], 'Invalid arguments'))
})
test('default values', () => {
const signedAccess = new SignedAccess(key)
expect(signedAccess.algorithm).toBe('sha512')
expect(signedAccess.ttl).toBe(86400)
expect(signedAccess.key).toBe(key)
})
test('custom values', () => {
const signedAccess = new SignedAccess(key, {
algorithm: 'md5',
ttl: 1
})
expect(signedAccess.algorithm).toBe('md5')
expect(signedAccess.ttl).toBe(1)
expect(signedAccess.key).toBe(key)
signedAccess.algorithm = 'sha256'
signedAccess.ttl = 3600
signedAccess.key = 'abc'
expect(signedAccess.algorithm).toBe('sha256')
expect(signedAccess.ttl).toBe(3600)
expect(signedAccess.key).toBe('abc')
})
})
describe('signURL', () => {
const url = 'https://github.com/JadsonLucena/SignedAccess.js?foo=bar&expires=anything&ip=anything&method=anything&nonce=anything&prefix=anything&signature=anything#id'
test('type guards', () => {
[undefined, 0, false, null].forEach(input => expect(() => signedAccess.signURL(input)).toThrowError(new TypeError('Invalid URL')));
['xpto', 0, false, null].forEach(input => expect(() => signedAccess.signURL(url, { algorithm: input })).toThrowError(new TypeError('Invalid algorithm')));
['tomorrow', 0, false, null].forEach(input => expect(() => signedAccess.signURL(url, { ttl: input })).toThrow('Invalid ttl'));
['127.000.000.001', '127.0.0.1/24', 'fhqwhgads', 127001, 0, false, null].forEach(input => expect(() => signedAccess.signURL(url, { remoteAddress: input })).toThrow('Invalid remoteAddress'));
[0, false, null].forEach(input => expect(() => signedAccess.signURL(url, { key: input })).toThrowError(new TypeError('Invalid key')));
['GETTER', 0, false, null].forEach(input => expect(() => signedAccess.signURL(url, { accessControlAllowMethods: input })).toThrow('Invalid accessControlAllowMethods'));
[0, false, null].forEach(input => expect(() => signedAccess.signURL(url, { nonce: input })).toThrowError(new TypeError('Invalid nonce')));
['/github/', 0, false, null].forEach(input => expect(() => signedAccess.signURL(url, { pathname: input })).toThrow('Invalid pathname'))
expect(() => signedAccess.signURL(undefined, {
accessControlAllowMethods: null,
algorithm: null,
key: null,
nonce: null,
pathname: null,
remoteAddress: null,
ttl: null
})).toThrowError(new AggregateError([
new TypeError('Invalid URL'),
new Error('Invalid accessControlAllowMethods'),
new TypeError('Invalid algorithm'),
new TypeError('Invalid nonce'),
new Error('Invalid pathname'),
new Error('Invalid remoteAddress'),
new Error('Invalid ttl')
], 'Invalid arguments'))
})
test('default values', () => {
let signedURL = signedAccess.signURL(url)
signedURL = new URL(signedURL)
const searchParams = signedURL.searchParams
const querys = Array.from(searchParams.keys())
const URLOriginal = new URL(url)
expect(signedURL.origin).toBe(URLOriginal.origin)
expect(signedURL.pathname).toBe(URLOriginal.pathname)
expect(signedURL.hash).toBe(URLOriginal.hash)
expect(querys.length).toBe(3)
expect(querys).toContain('foo')
expect(querys).toContain('expires')
expect(querys).not.toContain('ip') // reserved searchParams
expect(querys).not.toContain('method') // reserved searchParams
expect(querys).not.toContain('nonce') // reserved searchParams
expect(querys).not.toContain('prefix') // reserved searchParams
expect(querys).toContain('signature')
expect(searchParams.get('foo')).toBe('bar')
expect(searchParams.get('expires')).not.toBe('anything')
expect(parseInt(searchParams.get('expires'))).toBeGreaterThan(Date.now())
expect(searchParams.get('signature')).not.toBe('anything')
expect(searchParams.get('signature')).toMatch(/[A-Za-z0-9-_.~]+/)
})
test('custom values', () => {
const ttl = 3600
const remoteAddress = '142.251.129.78'
const accessControlAllowMethods = 'GET,POST'
const nonce = crypto.randomUUID()
const pathname = '/JadsonLucena/'
let signedURL = signedAccess.signURL(url, {
ttl,
remoteAddress,
accessControlAllowMethods,
nonce,
pathname
})
signedURL = new URL(signedURL)
const searchParams = signedURL.searchParams
const querys = Array.from(searchParams.keys())
const URLOriginal = new URL(url)
expect(signedURL.origin).toBe(URLOriginal.origin)
expect(signedURL.pathname).toBe(URLOriginal.pathname)
expect(signedURL.hash).toBe(URLOriginal.hash)
expect(querys.length).toBe(8)
expect(querys).toContain('foo')
expect(querys).toContain('expires')
expect(querys).toContain('ip')
expect(querys).toContain('method')
expect(querys).toContain('nonce')
expect(querys).toContain('prefix')
expect(querys).toContain('signature')
expect(searchParams.get('foo')).toBe('bar')
expect(parseInt(searchParams.get('expires'))).toBeGreaterThan(Date.now())
expect(searchParams.get('ip')).toBe(remoteAddress)
expect(searchParams.getAll('method').sort()).toEqual(accessControlAllowMethods.split(',').sort())
expect(searchParams.get('nonce')).toBe(nonce)
expect(searchParams.get('prefix')).toMatch(/[A-Za-z0-9-_.~]+/)
expect(searchParams.get('signature')).toMatch(/[A-Za-z0-9-_.~]+/)
})
})
describe('verifyURL', () => {
const url = 'https://github.com/JadsonLucena/SignedAccess.js?foo=bar#id'
test('type guards', () => {
const signedURL = signedAccess.signURL(url);
[undefined, 0, false, null].forEach(input => expect(() => signedAccess.verifyURL(input)).toThrowError(new TypeError('Invalid URL')));
['xpto', 0, false, null].forEach(input => expect(() => signedAccess.verifyURL(signedURL, { algorithm: input })).toThrowError(new TypeError('Invalid algorithm')));
['127.000.000.001', '127.0.0.1/24', 'fhqwhgads', 127001, 0, false, null].forEach(input => expect(() => signedAccess.verifyURL(signedURL, { remoteAddress: input })).toThrow('Invalid remoteAddress'));
[0, false, null].forEach(input => expect(() => signedAccess.verifyURL(signedURL, { key: input })).toThrowError(new TypeError('Invalid key')));
['GETTER', 0, false, null].forEach(input => expect(() => signedAccess.verifyURL(signedURL, { method: input })).toThrowError(new TypeError('Invalid method')))
expect(() => signedAccess.verifyURL(undefined, {
algorithm: null,
key: null,
method: null,
remoteAddress: null
})).toThrowError(new AggregateError([
new TypeError('Invalid URL'),
new TypeError('Invalid method'),
new Error('Invalid remoteAddress')
], 'Invalid arguments'))
expect(() => signedAccess.verifyURL(signedAccess.signURL(url, {
accessControlAllowMethods: 'GET',
remoteAddress: '127.0.0.1'
}))).toThrowError(new AggregateError([
new Error('method required'),
new Error('remoteAddress required')
], 'Invalid URL'))
})
test('default values', () => {
const signedURL = signedAccess.signURL(url)
expect(signedAccess.verifyURL(signedURL)).toBeTruthy()
expect(signedAccess.verifyURL(signedURL, { algorithm: 'sha1' })).toBeFalsy()
expect(signedAccess.verifyURL(signedURL, { remoteAddress: '127.0.0.1' })).toBeTruthy() // should be ignored
expect(signedAccess.verifyURL(signedURL, { key: 'anything' })).toBeFalsy()
expect(signedAccess.verifyURL(signedURL, { method: 'POST' })).toBeTruthy() // should be ignored
})
test('custom values', () => {
let signedURL = signedAccess.signURL(url, { algorithm: 'sha1' })
expect(signedAccess.verifyURL(signedURL)).toBeFalsy()
expect(signedAccess.verifyURL(signedURL, { algorithm: 'sha1' })).toBeTruthy()
signedURL = signedAccess.signURL(url, { remoteAddress: '127.0.0.1' })
expect(() => signedAccess.verifyURL(signedURL)).toThrowError(new Error('remoteAddress required'))
expect(signedAccess.verifyURL(signedURL, { remoteAddress: '142.251.129.78' })).toBeFalsy()
expect(signedAccess.verifyURL(signedURL, { remoteAddress: '127.0.0.1' })).toBeTruthy()
signedURL = signedAccess.signURL(url, { key: 'xpto' })
expect(signedAccess.verifyURL(signedURL)).toBeFalsy()
expect(signedAccess.verifyURL(signedURL, { key: 'xpto' })).toBeTruthy()
signedURL = signedAccess.signURL(url, { accessControlAllowMethods: 'POST' })
expect(() => signedAccess.verifyURL(signedURL)).toThrowError(new Error('method required'))
expect(signedAccess.verifyURL(signedURL, { method: 'PATCH' })).toBeFalsy()
expect(signedAccess.verifyURL(signedURL, { method: 'POST' })).toBeTruthy()
signedURL = signedAccess.signURL('https://example.com/data/file1', {
pathname: '/data'
})
let mockSignedURL = `https://example.com/database?${new URL(signedURL).searchParams.toString()}`
expect(signedAccess.verifyURL(mockSignedURL)).toBeTruthy()
mockSignedURL = `https://example.com/data/file2?${new URL(signedURL).searchParams.toString()}`
expect(signedAccess.verifyURL(mockSignedURL)).toBeTruthy()
signedURL = signedAccess.signURL('https://example.com/data/file1', {
pathname: '/data/'
})
mockSignedURL = `https://example.com/database?${new URL(signedURL).searchParams.toString()}`
expect(signedAccess.verifyURL(mockSignedURL)).toBeFalsy()
mockSignedURL = `https://example.com/data/file2?${new URL(signedURL).searchParams.toString()}`
expect(signedAccess.verifyURL(mockSignedURL)).toBeTruthy()
signedURL = signedAccess.signURL(url, {
remoteAddress: '127.0.0.1',
accessControlAllowMethods: 'POST, PUT',
nonce: crypto.randomUUID(),
pathname: '/JadsonLucena/'
})
mockSignedURL = `https://github.com/JadsonLucena/WebSocket.js?${new URL(signedURL).searchParams.toString()}`
expect(() => signedAccess.verifyURL(mockSignedURL, { method: 'POST' })).toThrowError(new Error('remoteAddress required'))
expect(() => signedAccess.verifyURL(mockSignedURL, { remoteAddress: '142.251.129.78' })).toThrowError(new Error('method required'))
expect(signedAccess.verifyURL(mockSignedURL, {
remoteAddress: '142.251.129.78',
method: 'DELETE'
})).toBeFalsy()
expect(signedAccess.verifyURL(mockSignedURL, {
remoteAddress: '127.0.0.1',
method: 'GET'
})).toBeFalsy()
expect(signedAccess.verifyURL(mockSignedURL, {
remoteAddress: '142.251.129.78',
method: 'POST'
})).toBeFalsy()
expect(signedAccess.verifyURL(mockSignedURL, {
remoteAddress: '127.0.0.1',
method: 'PUT'
})).toBeTruthy()
})
})
describe('signCookie', () => {
const prefix = 'https://github.com/JadsonLucena/'
test('type guards', () => {
[undefined, 0, false, null].forEach(input => expect(() => signedAccess.signCookie(input)).toThrowError(new TypeError('Invalid prefix')));
['xpto', 0, false, null].forEach(input => expect(() => signedAccess.signCookie(prefix, { algorithm: input })).toThrowError(new TypeError('Invalid algorithm')));
['tomorrow', 0, false, null].forEach(input => expect(() => signedAccess.signCookie(prefix, { ttl: input })).toThrow('Invalid ttl'));
['127.000.000.001', '127.0.0.1/24', 'fhqwhgads', 127001, 0, false, null].forEach(input => expect(() => signedAccess.signCookie(prefix, { remoteAddress: input })).toThrow('Invalid remoteAddress'));
[0, false, null].forEach(input => expect(() => signedAccess.signCookie(prefix, { key: input })).toThrowError(new TypeError('Invalid key')));
['GETTER', 0, false, null].forEach(input => expect(() => signedAccess.signCookie(prefix, { accessControlAllowMethods: input })).toThrow('Invalid accessControlAllowMethods'));
[0, false, null].forEach(input => expect(() => signedAccess.signCookie(prefix, { nonce: input })).toThrowError(new TypeError('Invalid nonce')))
expect(() => signedAccess.signCookie(prefix, {
accessControlAllowMethods: null,
algorithm: null,
key: null,
nonce: null,
remoteAddress: null,
ttl: null
})).toThrowError(new AggregateError([
new TypeError('Invalid prefix'),
new Error('Invalid accessControlAllowMethods'),
new TypeError('Invalid algorithm'),
new TypeError('Invalid nonce'),
new Error('Invalid remoteAddress'),
new Error('Invalid ttl')
], 'Invalid arguments'))
})
test('default values', () => {
const signedCookie = signedAccess.signCookie(prefix)
const searchParams = new URLSearchParams(signedCookie)
const querys = Array.from(searchParams.keys())
expect(querys.length).toBe(3)
expect(querys).toContain('expires')
expect(querys).toContain('prefix')
expect(querys).toContain('signature')
expect(parseInt(searchParams.get('expires'))).toBeGreaterThan(Date.now())
expect(searchParams.get('prefix')).toMatch(/[A-Za-z0-9-_.~]+/)
expect(searchParams.get('signature')).toMatch(/[A-Za-z0-9-_.~]+/)
})
test('custom values', () => {
const ttl = 3600
const remoteAddress = '142.251.129.78'
const accessControlAllowMethods = 'GET,POST'
const nonce = crypto.randomUUID()
const signedCookie = signedAccess.signCookie(prefix, {
ttl,
remoteAddress,
accessControlAllowMethods,
nonce
})
const searchParams = new URLSearchParams(signedCookie)
const querys = Array.from(searchParams.keys())
expect(querys.length).toBe(7)
expect(querys).toContain('expires')
expect(querys).toContain('ip')
expect(querys).toContain('method')
expect(querys).toContain('nonce')
expect(querys).toContain('prefix')
expect(querys).toContain('signature')
expect(parseInt(searchParams.get('expires'))).toBeGreaterThan(Date.now())
expect(searchParams.get('ip')).toBe(remoteAddress)
expect(searchParams.getAll('method').sort()).toEqual(accessControlAllowMethods.split(',').sort())
expect(searchParams.get('nonce')).toBe(nonce)
expect(searchParams.get('prefix')).toMatch(/[A-Za-z0-9-_.~]+/)
expect(searchParams.get('signature')).toMatch(/[A-Za-z0-9-_.~]+/)
})
})
describe('verifyCookie', () => {
const prefix = 'https://github.com/JadsonLucena/'
const mockURL = 'https://github.com/JadsonLucena/SignedAccess.js?foo=bar#id'
test('type guards', () => {
const signedCookie = signedAccess.signCookie(prefix);
[undefined, 0, false, null].forEach(input => expect(() => signedAccess.verifyCookie(input, signedCookie)).toThrowError(new TypeError('Invalid URL')));
[undefined, 0, false, null].forEach(input => expect(() => signedAccess.verifyCookie(mockURL, input)).toThrowError(new TypeError('Invalid cookie')));
['xpto', 0, false, null].forEach(input => expect(() => signedAccess.verifyCookie(mockURL, signedCookie, { algorithm: input })).toThrowError(new TypeError('Invalid algorithm')));
['127.000.000.001', '127.0.0.1/24', 'fhqwhgads', 127001, 0, false, null].forEach(input => expect(() => signedAccess.verifyCookie(mockURL, signedCookie, { remoteAddress: input })).toThrow('Invalid remoteAddress'));
[0, false, null].forEach(input => expect(() => signedAccess.verifyCookie(mockURL, signedCookie, { key: input })).toThrowError(new TypeError('Invalid key')));
['GETTER', 0, false, null].forEach(input => expect(() => signedAccess.verifyCookie(mockURL, signedCookie, { method: input })).toThrowError(new TypeError('Invalid method')))
expect(() => signedAccess.verifyCookie(undefined, undefined, {
algorithm: null,
key: null,
method: null,
remoteAddress: null
})).toThrowError(new AggregateError([
new TypeError('Invalid URL'),
new TypeError('Invalid cookie'),
new TypeError('Invalid method'),
new Error('Invalid remoteAddress')
], 'Invalid arguments'))
expect(() => signedAccess.verifyCookie(mockURL, signedAccess.signCookie(prefix, {
accessControlAllowMethods: 'GET',
remoteAddress: '127.0.0.1'
}))).toThrowError(new AggregateError([
new Error('method required'),
new Error('remoteAddress required')
], 'Invalid cookie'))
})
test('default values', () => {
let signedCookie = signedAccess.signCookie(prefix)
expect(signedAccess.verifyCookie(mockURL, signedCookie)).toBeTruthy()
expect(signedAccess.verifyCookie(mockURL, signedCookie, { algorithm: 'sha1' })).toBeFalsy()
expect(signedAccess.verifyCookie(mockURL, signedCookie, { remoteAddress: '127.0.0.1' })).toBeTruthy() // should be ignored
expect(signedAccess.verifyCookie(mockURL, signedCookie, { key: 'anything' })).toBeFalsy()
expect(signedAccess.verifyCookie(mockURL, signedCookie, { method: 'POST' })).toBeTruthy() // should be ignored
signedCookie = signedAccess.signCookie(mockURL)
expect(signedAccess.verifyCookie(mockURL, signedCookie)).toBeTruthy()
})
test('custom values', () => {
let signedCookie = signedAccess.signCookie(prefix, { algorithm: 'sha256' })
expect(signedAccess.verifyCookie(mockURL, signedCookie)).toBeFalsy()
expect(signedAccess.verifyCookie(mockURL, signedCookie, { algorithm: 'sha256' })).toBeTruthy()
signedCookie = signedAccess.signCookie(prefix, { remoteAddress: '127.0.0.1' })
expect(() => signedAccess.verifyCookie(mockURL, signedCookie)).toThrowError(new Error('remoteAddress required'))
expect(signedAccess.verifyCookie(mockURL, signedCookie, { remoteAddress: '142.251.129.78' })).toBeFalsy()
expect(signedAccess.verifyCookie(mockURL, signedCookie, { remoteAddress: '127.0.0.1' })).toBeTruthy()
signedCookie = signedAccess.signCookie(prefix, { key: 'xpto' })
expect(signedAccess.verifyCookie(mockURL, signedCookie)).toBeFalsy()
expect(signedAccess.verifyCookie(mockURL, signedCookie, { key: 'xpto' })).toBeTruthy()
signedCookie = signedAccess.signCookie(prefix, { accessControlAllowMethods: 'POST' })
expect(() => signedAccess.verifyCookie(mockURL, signedCookie)).toThrowError(new Error('method required'))
expect(signedAccess.verifyCookie(mockURL, signedCookie, { method: 'PATCH' })).toBeFalsy()
expect(signedAccess.verifyCookie(mockURL, signedCookie, { method: 'POST' })).toBeTruthy()
signedCookie = signedAccess.signCookie('https://example.com/data')
expect(signedAccess.verifyCookie('https://example.com/database', signedCookie)).toBeTruthy()
expect(signedAccess.verifyCookie('https://example.com/data/file1', signedCookie)).toBeTruthy()
signedCookie = signedAccess.signCookie('https://example.com/data/')
expect(signedAccess.verifyCookie('https://example.com/database', signedCookie)).toBeFalsy()
expect(signedAccess.verifyCookie('https://example.com/data/file1', signedCookie)).toBeTruthy()
signedCookie = signedAccess.signCookie(prefix, {
remoteAddress: '127.0.0.1',
accessControlAllowMethods: 'POST, PUT',
nonce: crypto.randomUUID()
})
expect(() => signedAccess.verifyCookie(mockURL, signedCookie, { method: 'POST' })).toThrowError(new Error('remoteAddress required'))
expect(() => signedAccess.verifyCookie(mockURL, signedCookie, { remoteAddress: '142.251.129.78' })).toThrowError(new Error('method required'))
expect(signedAccess.verifyCookie(mockURL, signedCookie, {
remoteAddress: '142.251.129.78',
method: 'DELETE'
})).toBeFalsy()
expect(signedAccess.verifyCookie(mockURL, signedCookie, {
remoteAddress: '127.0.0.1',
method: 'GET'
})).toBeFalsy()
expect(signedAccess.verifyCookie(mockURL, signedCookie, {
remoteAddress: '142.251.129.78',
method: 'POST'
})).toBeFalsy()
expect(signedAccess.verifyCookie(mockURL, signedCookie, {
remoteAddress: '127.0.0.1',
method: 'PUT'
})).toBeTruthy()
})
})