1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5
6[-1, 10].forEach((offset) => {
7  assert.throws(
8    () => Buffer.alloc(9).write('foo', offset),
9    {
10      code: 'ERR_OUT_OF_RANGE',
11      name: 'RangeError',
12      message: 'The value of "offset" is out of range. ' +
13               `It must be >= 0 && <= 9. Received ${offset}`
14    }
15  );
16});
17
18const resultMap = new Map([
19  ['utf8', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])],
20  ['ucs2', Buffer.from([102, 0, 111, 0, 111, 0, 0, 0, 0])],
21  ['ascii', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])],
22  ['latin1', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])],
23  ['binary', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])],
24  ['utf16le', Buffer.from([102, 0, 111, 0, 111, 0, 0, 0, 0])],
25  ['base64', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])],
26  ['base64url', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])],
27  ['hex', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])],
28]);
29
30// utf8, ucs2, ascii, latin1, utf16le
31const encodings = ['utf8', 'utf-8', 'ucs2', 'ucs-2', 'ascii', 'latin1',
32                   'binary', 'utf16le', 'utf-16le'];
33
34encodings
35  .reduce((es, e) => es.concat(e, e.toUpperCase()), [])
36  .forEach((encoding) => {
37    const buf = Buffer.alloc(9);
38    const len = Buffer.byteLength('foo', encoding);
39    assert.strictEqual(buf.write('foo', 0, len, encoding), len);
40
41    if (encoding.includes('-'))
42      encoding = encoding.replace('-', '');
43
44    assert.deepStrictEqual(buf, resultMap.get(encoding.toLowerCase()));
45  });
46
47// base64
48['base64', 'BASE64', 'base64url', 'BASE64URL'].forEach((encoding) => {
49  const buf = Buffer.alloc(9);
50  const len = Buffer.byteLength('Zm9v', encoding);
51
52  assert.strictEqual(buf.write('Zm9v', 0, len, encoding), len);
53  assert.deepStrictEqual(buf, resultMap.get(encoding.toLowerCase()));
54});
55
56// hex
57['hex', 'HEX'].forEach((encoding) => {
58  const buf = Buffer.alloc(9);
59  const len = Buffer.byteLength('666f6f', encoding);
60
61  assert.strictEqual(buf.write('666f6f', 0, len, encoding), len);
62  assert.deepStrictEqual(buf, resultMap.get(encoding.toLowerCase()));
63});
64
65// Invalid encodings
66for (let i = 1; i < 10; i++) {
67  const encoding = String(i).repeat(i);
68  const error = common.expectsError({
69    code: 'ERR_UNKNOWN_ENCODING',
70    name: 'TypeError',
71    message: `Unknown encoding: ${encoding}`
72  });
73
74  assert.ok(!Buffer.isEncoding(encoding));
75  assert.throws(() => Buffer.alloc(9).write('foo', encoding), error);
76}
77
78// UCS-2 overflow CVE-2018-12115
79for (let i = 1; i < 4; i++) {
80  // Allocate two Buffers sequentially off the pool. Run more than once in case
81  // we hit the end of the pool and don't get sequential allocations
82  const x = Buffer.allocUnsafe(4).fill(0);
83  const y = Buffer.allocUnsafe(4).fill(1);
84  // Should not write anything, pos 3 doesn't have enough room for a 16-bit char
85  assert.strictEqual(x.write('ыыыыыы', 3, 'ucs2'), 0);
86  // CVE-2018-12115 experienced via buffer overrun to next block in the pool
87  assert.strictEqual(Buffer.compare(y, Buffer.alloc(4, 1)), 0);
88}
89
90// Should not write any data when there is no space for 16-bit chars
91const z = Buffer.alloc(4, 0);
92assert.strictEqual(z.write('\u0001', 3, 'ucs2'), 0);
93assert.strictEqual(Buffer.compare(z, Buffer.alloc(4, 0)), 0);
94// Make sure longer strings are written up to the buffer end.
95assert.strictEqual(z.write('abcd', 2), 2);
96assert.deepStrictEqual([...z], [0, 0, 0x61, 0x62]);
97
98// Large overrun could corrupt the process
99assert.strictEqual(Buffer.alloc(4)
100  .write('ыыыыыы'.repeat(100), 3, 'utf16le'), 0);
101
102{
103  // .write() does not affect the byte after the written-to slice of the Buffer.
104  // Refs: https://github.com/nodejs/node/issues/26422
105  const buf = Buffer.alloc(8);
106  assert.strictEqual(buf.write('ыы', 1, 'utf16le'), 4);
107  assert.deepStrictEqual([...buf], [0, 0x4b, 0x04, 0x4b, 0x04, 0, 0, 0]);
108}
109