|
| 1 | +/** |
| 2 | + * Tests based on https://github.com/hapijs/iron/blob/93fd15c76e656b1973ba134de64f3aeac66a0405/test/index.js |
| 3 | + * Copyright (c) 2012-2020, Sideway Inc, and project contributors |
| 4 | + * All rights reserved. |
| 5 | + * https://github.com/hapijs/iron/blob/93fd15c76e656b1973ba134de64f3aeac66a0405/LICENSE.md |
| 6 | + * |
| 7 | + * Rewritten and repurposed by Michał Miszczyszyn 2021 |
| 8 | + */ |
| 9 | +import * as EncryptCookies from '../src/utils/encryptCookies'; |
| 10 | + |
| 11 | +describe('encrypt', () => { |
| 12 | + const secret = 'some_not_random_password_that_is'; |
| 13 | + const value = 'test data'; |
| 14 | + |
| 15 | + it('turns object into a sealed then parses the sealed successfully', async () => { |
| 16 | + const sealed = await EncryptCookies.seal({ value, secret }); |
| 17 | + const unsealed = await EncryptCookies.unseal({ sealed, secret }); |
| 18 | + expect(unsealed).toEqual(value); |
| 19 | + }); |
| 20 | + |
| 21 | + it('unseal and sealed object with expiration', async () => { |
| 22 | + const sealed = await EncryptCookies.seal({ value, secret, ttl: 200 }); |
| 23 | + const unsealed = await EncryptCookies.unseal({ sealed, secret }); |
| 24 | + expect(unsealed).toEqual(value); |
| 25 | + }); |
| 26 | + |
| 27 | + it('fails for too short secret', async () => { |
| 28 | + await expect(EncryptCookies.seal({ value, secret: 'too short' })).rejects.toThrowErrorMatchingInlineSnapshot( |
| 29 | + `"Secret must be exactly 32 characters long!"`, |
| 30 | + ); |
| 31 | + }); |
| 32 | + |
| 33 | + it('unseals a sealed', async () => { |
| 34 | + const sealed = |
| 35 | + 'Fe26.2**SqhOkY8av81FPay7I60ktrpeOq7SgRNCcNN0rHWAMSg*3xsUfKKg2KiUWhsOmm1Nnw*_MeWO7OhJooR1Jc0cXQ5pp-wrtooQBeZsvNCSF9Yl5mm5xpCr8_SwxPJJkzwxN43**r3lxz-MMOws6YE-lDcXy6rmZc0mHHMVbXsndXmePgnA*JRDpLG7MxvgdoJqTeaTnUEQ-c0E6eyA66hVSr3f4BLmdfzZYU7fWIYGImEpEZgwzp_0jlF44R0Vr8BDQBlJiNw'; |
| 36 | + const unsealed = await EncryptCookies.unseal({ sealed, secret }); |
| 37 | + expect(JSON.parse(unsealed)).toEqual({ |
| 38 | + a: 1, |
| 39 | + array: [5, 6, {}], |
| 40 | + nested: { |
| 41 | + k: true, |
| 42 | + }, |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + it('returns an error when number of sealed components is wrong', async () => { |
| 47 | + const sealed = |
| 48 | + 'x*Fe26.2**SqhOkY8av81FPay7I60ktrpeOq7SgRNCcNN0rHWAMSg*3xsUfKKg2KiUWhsOmm1Nnw*_MeWO7OhJooR1Jc0cXQ5pp-wrtooQBeZsvNCSF9Yl5mm5xpCr8_SwxPJJkzwxN43**r3lxz-MMOws6YE-lDcXy6rmZc0mHHMVbXsndXmePgnA*JRDpLG7MxvgdoJqTeaTnUEQ-c0E6eyA66hVSr3f4BLmdfzZYU7fWIYGImEpEZgwzp_0jlF44R0Vr8BDQBlJiNw'; |
| 49 | + await expect(EncryptCookies.unseal({ sealed, secret })).rejects.toThrowErrorMatchingInlineSnapshot( |
| 50 | + `"Cannot unseal: Incorrect data format."`, |
| 51 | + ); |
| 52 | + }); |
| 53 | + |
| 54 | + it('returns an error when mac prefix is wrong', async () => { |
| 55 | + const sealed = |
| 56 | + 'Fe27.2**SqhOkY8av81FPay7I60ktrpeOq7SgRNCcNN0rHWAMSg*3xsUfKKg2KiUWhsOmm1Nnw*_MeWO7OhJooR1Jc0cXQ5pp-wrtooQBeZsvNCSF9Yl5mm5xpCr8_SwxPJJkzwxN43**r3lxz-MMOws6YE-lDcXy6rmZc0mHHMVbXsndXmePgnA*JRDpLG7MxvgdoJqTeaTnUEQ-c0E6eyA66hVSr3f4BLmdfzZYU7fWIYGImEpEZgwzp_0jlF44R0Vr8BDQBlJiNw'; |
| 57 | + await expect(EncryptCookies.unseal({ sealed, secret })).rejects.toThrowErrorMatchingInlineSnapshot( |
| 58 | + `"Cannot unseal: Unsupported version."`, |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + it('returns an error when integrity check fails', async () => { |
| 63 | + const sealed = |
| 64 | + 'Fe26.2**SqhOkY8av81FPay7I60ktrpeOq7SgRNCcNN0rHWAMSg*3xsUfKKg2KiUWhsOmm1Nnw*_MeWO7OhJooR1Jc0cXQ5pp-wrtooQBeZsvNCSF9Yl5mm5xpCr8_SwxPJJkzwxN43**r3lxz-MMOws6YE-lDcXy6rmZc0mHHMVbXsndXmePgnA*JRDpLG7MxvgdoJqTeaTnUEQ-c0E6eyA66hVSr3f4BLmdfzZYU7fWIYGImEpEZgwzp_0jlF44R0Vr8BDQBlJiNwLOL'; |
| 65 | + await expect(EncryptCookies.unseal({ sealed, secret })).rejects.toThrowErrorMatchingInlineSnapshot( |
| 66 | + `"Cannot unseal: Incorrect hmac seal value"`, |
| 67 | + ); |
| 68 | + }); |
| 69 | + |
| 70 | + it('returns an error when iv base64 decoding fails', async () => { |
| 71 | + const sealed = |
| 72 | + 'Fe26.2**0a27f421711152214f2cdd7fd8c515738204828f2d5c1ac50685231d38614de1*hUkUfX6sYUoKXh1QNx8oywLOL*AxjnFXiFUlQqdpNYK9lzAJzfm0S07vKo599fOi1Og7vuPaiQ6z8o487hDrs7xDu0**4eb9bef394dbaffa866f1e4246cf9d8c72a19d403da89760a3fc65c95d82301a*l65Cto8YluxfUbex2aD27hrA9Hccvhcryac0pkHfPvs'; |
| 73 | + await expect(EncryptCookies.unseal({ sealed, secret })).rejects.toThrowErrorMatchingInlineSnapshot( |
| 74 | + `"Cannot unseal: Incorrect hmac seal value"`, |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + it('returns an error when expired', async () => { |
| 79 | + const sealed = |
| 80 | + 'Fe26.2**552bc79cfa73de9855b539a624c6b404496995f443baf057b95c097f5503f330*sk9We2FqPEyHc5bSzfA1yA*tlyeEmz0jWnaRd4CDmrqeQ*1623946580929*807a2f0ac5aebd5e413e06c52ffbf52158566e73a551d805d3b68164c7869ed8*Y5XBmJC-4QZ4Q1iRUiN2f8SStLL23-57wXNayX-tiF0'; |
| 81 | + await expect(EncryptCookies.unseal({ sealed, secret })).rejects.toThrowErrorMatchingInlineSnapshot( |
| 82 | + `"Cannot unseal: Expired seal"`, |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + it('returns an error when expiration NaN', async () => { |
| 87 | + const sealed = |
| 88 | + 'Fe26.2**71ccf7404636c565d498200c002837f55ff5a0bf5e9ddecbd93953336709e9a4*JnIlC3F0_AhVSJQ2ALF3ow*A3s_DWrqGwWRjgC6mD5-SQ*1623946786465dupa*0e8513880d1c8410fb0e8a8e0c7ad43285ee67568b80ab2e76721e7381e14a14*iEz4o4dDQirX6Y1x2Om6Lpglg3XtDVjzkZvq3iRtFuM'; |
| 89 | + await expect(EncryptCookies.unseal({ sealed, secret })).rejects.toThrowErrorMatchingInlineSnapshot( |
| 90 | + `"Cannot unseal: Invalid expiration"`, |
| 91 | + ); |
| 92 | + }); |
| 93 | +}); |
0 commit comments