1 # disallow use of the Buffer() constructor (no-buffer-constructor)
3 This rule was **deprecated** in ESLint v7.0.0. Please use the corresponding rule in [`eslint-plugin-node`](https://github.com/mysticatea/eslint-plugin-node).
5 In Node.js, the behavior of the `Buffer` constructor is different depending on the type of its argument. Passing an argument from user input to `Buffer()` without validating its type can lead to security vulnerabilities such as remote memory disclosure and denial of service. As a result, the `Buffer` constructor has been deprecated and should not be used. Use the producer methods `Buffer.from`, `Buffer.alloc`, and `Buffer.allocUnsafe` instead.
9 This rule disallows calling and constructing the `Buffer()` constructor.
11 Examples of **incorrect** code for this rule:
15 new Buffer([1, 2, 3]);
20 new Buffer(res.body.amount);
21 new Buffer(res.body.values);
24 Examples of **correct** code for this rule:
28 Buffer.allocUnsafe(5);
29 Buffer.from([1, 2, 3]);
31 Buffer.alloc(res.body.amount);
32 Buffer.from(res.body.values);
37 If you don't use Node.js, or you still need to support versions of Node.js that lack methods like `Buffer.from`, then you should not enable this rule.
41 * [Buffer API documentation](https://nodejs.org/api/buffer.html)
42 * [Let's fix Node.js Buffer API](https://github.com/ChALkeR/notes/blob/master/Lets-fix-Buffer-API.md)
43 * [Buffer(number) is unsafe](https://github.com/nodejs/node/issues/4660)