-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
Expand file tree
/
Copy pathtest-crypto-encoding-validation-error.js
More file actions
101 lines (82 loc) Β· 2.55 KB
/
test-crypto-encoding-validation-error.js
File metadata and controls
101 lines (82 loc) Β· 2.55 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
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
// This test checks if error is thrown in case of wrong encoding provided into cipher.
const assert = require('assert');
const { createCipheriv, randomBytes } = require('crypto');
const createCipher = () => {
return createCipheriv('aes-256-cbc', randomBytes(32), randomBytes(16));
};
{
const cipher = createCipher();
cipher.update('test', 'utf-8', 'utf-8');
assert.throws(
() => cipher.update('666f6f', 'hex', 'hex'),
{ message: /Cannot change encoding/ }
);
}
{
const cipher = createCipher();
cipher.update('test', 'utf-8', 'utf-8');
assert.throws(
() => cipher.final('hex'),
{ message: /Cannot change encoding/ }
);
}
{
const cipher = createCipher();
cipher.update('test', 'utf-8', 'utf-8');
assert.throws(
() => cipher.final('bad2'),
{ message: /^Unknown encoding: bad2$/, code: 'ERR_UNKNOWN_ENCODING' }
);
}
{
const cipher = createCipher();
assert.throws(
() => cipher.update('test', 'utf-8', 'bad3'),
{ message: /^Unknown encoding: bad3$/, code: 'ERR_UNKNOWN_ENCODING' }
);
}
// Regression tests for https://github.com/nodejs/node/issues/45189:
// Unknown input encodings used to be silently accepted by Cipher/Decipher
// `update`, producing incorrect (and silently non-deterministic) output.
// They must now reject with ERR_UNKNOWN_ENCODING.
{
const cipher = createCipher();
assert.throws(
() => cipher.update('test', 'bad', 'hex'),
{ message: /^Unknown encoding: bad$/, code: 'ERR_UNKNOWN_ENCODING' }
);
}
{
const { createDecipheriv } = require('crypto');
const decipher = createDecipheriv(
'aes-256-cbc', randomBytes(32), randomBytes(16));
assert.throws(
() => decipher.update('test', 'bad', 'hex'),
{ message: /^Unknown encoding: bad$/, code: 'ERR_UNKNOWN_ENCODING' }
);
}
// A buffer-like data argument should not trigger encoding validation,
// because the input encoding is ignored when data is not a string.
{
const cipher = createCipher();
// Should not throw.
cipher.update(Buffer.from('test'), 'bad-but-ignored', 'hex');
}
// Valid input encodings must continue to work.
{
const cipher = createCipher();
let result = cipher.update('test', 'utf-8', 'hex');
result += cipher.final('hex');
assert.strictEqual(typeof result, 'string');
}
// Omitting the input encoding (undefined / null) is allowed; the
// underlying binding falls back to its default behavior.
{
const cipher = createCipher();
// Should not throw.
cipher.update(Buffer.from('test'));
}