Title: Buffer(number) is unsafe · Issue #4660 · nodejs/node · GitHub
Open Graph Title: Buffer(number) is unsafe · Issue #4660 · nodejs/node
X Title: Buffer(number) is unsafe · Issue #4660 · nodejs/node
Description: tl;dr This issue proposes: Change new Buffer(number) to return safe, zeroed-out memory Create a new API for creating uninitialized Buffers, Buffer.alloc(number) Update: Jan 15, 2016 Upon further consideration, I think that returning zero...
Open Graph Description: tl;dr This issue proposes: Change new Buffer(number) to return safe, zeroed-out memory Create a new API for creating uninitialized Buffers, Buffer.alloc(number) Update: Jan 15, 2016 Upon further co...
X Description: tl;dr This issue proposes: Change new Buffer(number) to return safe, zeroed-out memory Create a new API for creating uninitialized Buffers, Buffer.alloc(number) Update: Jan 15, 2016 Upon further co...
Opengraph URL: https://github.com/nodejs/node/issues/4660
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Buffer(number) is unsafe","articleBody":"### tl;dr\n\nThis issue proposes:\n1. Change `new Buffer(number)` to return safe, zeroed-out memory\n2. Create a new API for creating uninitialized Buffers, `Buffer.alloc(number)`\n### Update: Jan 15, 2016\n\nUpon further consideration, I think that returning zeroed out memory is a separate issue. The core issue is: unsafe buffer allocation should be in a different API.\n\nI now support adding two APIs:\n- `Buffer.from(value)` - convert from any type to a buffer\n- `Buffer.alloc(size)` - create an uninitialized buffer with given size\n\nThis solves the core problem that affected `ws` and `bittorrent-dht` which is `Buffer(variable)` getting tricked into taking a number argument.\n### Why is `Buffer` unsafe?\n\nToday, the node.js `Buffer` constructor is overloaded to handle many different argument types like `String`, `Array`, `Object`, `TypedArrayView` (`Uint8Array`, etc.), `ArrayBuffer`, and also `Number`.\n\nThe API is optimized for convenience: you can throw any type at it, and it will try to do what you want.\n\nBecause the Buffer constructor is so powerful, you often see code like this:\n\n``` js\n// Convert UTF-8 strings to hex\nfunction toHex (str) {\n return new Buffer(str).toString('hex')\n}\n```\n\n**_But what happens if `toHex` is called with a `Number` argument?**_\n### Remote Memory Disclosure\n\nIf an attacker can make your program call the `Buffer` constructor with a `Number` argument, then they can make it allocate uninitialized memory from the node.js process. This could potentially disclose TLS private keys, user data, or database passwords.\n\nWhen the `Buffer` constructor is passed a `Number` argument, it returns an **UNINITIALIZED** block of memory of the specified `size`. When you create a `Buffer` like this, you **MUST** overwrite the contents before returning it to the user.\n### Would this ever be a problem in real code?\n\nYes. It's surprisingly common to forget to check the type of your variables in a dynamically-typed language like JavaScript.\n\nUsually the consequences of assuming the wrong type is that your program crashes with an uncaught exception. But the failure mode for forgetting to check the type of arguments to the `Buffer` constructor is more catastrophic.\n\nHere's an example of a vulnerable service that takes a JSON payload and converts it to hex:\n\n``` js\n// Take a JSON payload {str: \"some string\"} and convert it to hex\nvar server = http.createServer(function (req, res) {\n var data = ''\n req.setEncoding('utf8')\n req.on('data', function (chunk) {\n data += chunk\n })\n req.on('end', function () {\n var body = JSON.parse(data)\n res.end(new Buffer(body.str).toString('hex'))\n })\n})\n\nserver.listen(8080)\n```\n\nIn this example, an http client just has to send:\n\n``` json\n{\n \"str\": 1000\n}\n```\n\nand it will get back 1,000 bytes of uninitialized memory from the server.\n\nThis is a very serious bug. It's similar in severity to the [the Heartbleed bug](http://heartbleed.com/) that allowed disclosure of OpenSSL process memory by remote attackers.\n### Which real-world packages were vulnerable?\n#### [`bittorrent-dht`](https://www.npmjs.com/package/bittorrent-dht)\n\n@mafintosh and I found this issue in one of our own packages, [`bittorrent-dht`](https://www.npmjs.com/package/bittorrent-dht). The bug would allow anyone on the internet to send a series of messages to a user of `bittorrent-dht` and get them to reveal 20 bytes at a time of uninitialized memory from the node.js process.\n\nHere's [the commit](https://github.com/feross/bittorrent-dht/commit/6c7da04025d5633699800a99ec3fbadf70ad35b8) that fixed it. We released a new fixed version, created a [Node Security Project disclosure](https://nodesecurity.io/advisories/68), and deprecated all vulnerable versions on npm so users will get a warning to upgrade to a newer version.\n#### [`ws`](https://www.npmjs.com/package/ws)\n\nThat got us wondering if there were other vulnerable packages. Sure enough, within a short period of time, we found the same issue in [`ws`](https://www.npmjs.com/package/ws), the most popular WebSocket implementation in node.js.\n\nIf certain APIs were called with `Number` parameters instead of `String` or `Buffer` as expected, then uninitialized server memory would be disclosed to the remote peer.\n\nThese were the vulnerable methods:\n\n``` js\nsocket.send(number)\nsocket.ping(number)\nsocket.pong(number)\n```\n\nHere's a vulnerable socket server with some echo functionality:\n\n``` js\nserver.on('connection', function (socket) {\n socket.on('message', function (message) {\n message = JSON.parse(message)\n if (message.type === 'echo') {\n socket.send(message.data) // send back the user's message\n }\n })\n})\n```\n\n`socket.send(number)` called on the server, will disclose server memory.\n\nHere's [the release](https://github.com/websockets/ws/releases/tag/1.0.1) where the issue was fixed, with a more detailed explanation. Props to @3rd-Eden for the quick fix. Here's the [Node Security Project disclosure](https://nodesecurity.io/advisories/67).\n### What's the solution?\n\nIt's important that node.js offers a fast way to get memory otherwise performance-critical applications would needlessly get a lot slower.\n\nBut we need a better way to _signal our intent_ as programmers. **When we want uninitialized memory, we should request it explicitly.**\n\nSensitive functionality should not be packed into a developer-friendly API that loosely accepts many different types. This type of API encourages the lazy practice of passing variables in without checking the type very carefully.\n#### `Buffer.alloc(number)`\n\nThe functionality of creating buffers with uninitialized memory should be part of another API. We propose `Buffer.alloc(number)`. This way, it's not part of an API that frequently gets user input of all sorts of different types passed into it.\n\n``` js\nvar buf = Buffer.alloc(16) // careful, uninitialized memory!\n\n// Immediately overwrite the uninitialized buffer with data from another buffer\nfor (var i = 0; i \u003c buf.length; i++) {\n buf[i] = otherBuf[i]\n}\n```\n### How do we fix node.js core?\n\nWe sent [a PR](https://github.com/nodejs/node/pull/4514) (merged as `semver-major`) which defends against one case:\n\n``` js\nvar str = 16\nnew Buffer(str, 'utf8')\n```\n\nIn this situation, it's implied that the programmer intended the first argument to be a string, since they passed an encoding as a second argument. Today, node.js will allocate uninitialized memory in the case of `new Buffer(number, encoding)`, which is probably not what the programmer intended.\n\nBut this is only a partial solution, since if the programmer does `new Buffer(variable)` (without an `encoding` parameter) there's no way to know what they intended. If `variable` is sometimes a number, then uninitialized memory will sometimes be returned.\n### What's the real long-term fix?\n\nWe could deprecate and remove `new Buffer(number)` and use `Buffer.alloc(number)` when we need uninitialized memory. But that would break 1000s of packages. So that's a no-go.\n\nInstead, we believe the best solution is to:\n1. Change `new Buffer(number)` to return safe, zeroed-out memory\n2. Create a new API for creating uninitialized Buffers. We propose: `Buffer.alloc(number)`\n\nThis way, existing code continues working and the impact on the npm ecosystem will be minimal. Over time, npm maintainers can migrate performance-critical code to use `Buffer.alloc(number)` instead of `new Buffer(number)`.\n### Conclusion\n\nWe think there's a serious design issue with the `Buffer` API as it exists today. It promotes insecure software by putting high-risk functionality into a convenient API with friendly \"developer ergonomics\".\n\nThis wasn't merely a theoretical exercise because we found the issue in some of the most popular npm packages.\n\nEventually, we hope that node.js core can switch to this new, safer behavior. We believe the impact on the ecosystem would be minimal since it's not a breaking change. Well-maintained, popular packages would be updated to use `Buffer.alloc` quickly, while older, insecure packages would magically become safe from this attack vector.\n","author":{"url":"https://github.com/feross","@type":"Person","name":"feross"},"datePublished":"2016-01-13T05:19:14Z","url":"https://github.com/nodejs/node/issues/4660"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:bd07d279-6da7-9b0a-2617-5881c8cc45f5 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CB62:2C47BC:179EB40:1F2D0E6:6AED8A02 |
| html-safe-nonce | f89a257911c80def9c5d860666a0d4561d2c8bc7b03b5ea643cff10cbeffa9c0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQjYyOjJDNDdCQzoxNzlFQjQwOjFGMkQwRTY6NkFFRDhBMDIiLCJ2aXNpdG9yX2lkIjoiMzE1NzMxODQzNDg5ODY3NjIyNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 8dda22516d3b32cdf66a097a5f3bb2ea9da7cbea7a449fb6844a0d7436c25d7e |
| hovercard-subject-tag | issue:126343377 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/nodejs/node/4660/issue_layout |
| twitter:image | https://opengraph.githubassets.com/bbf8031a7930ed5dde84f03c2433df0bf301c7150213555bd3c48ac4182c380a/nodejs/node/issues/4660 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/bbf8031a7930ed5dde84f03c2433df0bf301c7150213555bd3c48ac4182c380a/nodejs/node/issues/4660 |
| og:image:alt | tl;dr This issue proposes: Change new Buffer(number) to return safe, zeroed-out memory Create a new API for creating uninitialized Buffers, Buffer.alloc(number) Update: Jan 15, 2016 Upon further co... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | feross |
| hostname | github.com |
| expected-hostname | github.com |
| None | 56fb42677ae87ed3e64410323d67980aa65d0485568a7858f874e7edfc6d1c11 |
| turbo-cache-control | no-preview |
| go-import | github.com/nodejs/node git https://github.com/nodejs/node.git |
| octolytics-dimension-user_id | 9950313 |
| octolytics-dimension-user_login | nodejs |
| octolytics-dimension-repository_id | 27193779 |
| octolytics-dimension-repository_nwo | nodejs/node |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 27193779 |
| octolytics-dimension-repository_network_root_nwo | nodejs/node |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 9c9a37e2423ed382cf39f70b5aa4b4858b40a3b0 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width