Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Readme.md 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # safer-buffer [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![javascript style guide][standard-image]][standard-url] [![Security Responsible Disclosure][secuirty-image]][secuirty-url]
  2. [travis-image]: https://travis-ci.org/ChALkeR/safer-buffer.svg?branch=master
  3. [travis-url]: https://travis-ci.org/ChALkeR/safer-buffer
  4. [npm-image]: https://img.shields.io/npm/v/safer-buffer.svg
  5. [npm-url]: https://npmjs.org/package/safer-buffer
  6. [standard-image]: https://img.shields.io/badge/code_style-standard-brightgreen.svg
  7. [standard-url]: https://standardjs.com
  8. [secuirty-image]: https://img.shields.io/badge/Security-Responsible%20Disclosure-green.svg
  9. [secuirty-url]: https://github.com/nodejs/security-wg/blob/master/processes/responsible_disclosure_template.md
  10. Modern Buffer API polyfill without footguns, working on Node.js from 0.8 to current.
  11. ## How to use?
  12. First, port all `Buffer()` and `new Buffer()` calls to `Buffer.alloc()` and `Buffer.from()` API.
  13. Then, to achieve compatibility with outdated Node.js versions (`<4.5.0` and 5.x `<5.9.0`), use
  14. `const Buffer = require('safer-buffer').Buffer` in all files where you make calls to the new
  15. Buffer API. _Use `var` instead of `const` if you need that for your Node.js version range support._
  16. Also, see the
  17. [porting Buffer](https://github.com/ChALkeR/safer-buffer/blob/master/Porting-Buffer.md) guide.
  18. ## Do I need it?
  19. Hopefully, not — dropping support for outdated Node.js versions should be fine nowdays, and that
  20. is the recommended path forward. You _do_ need to port to the `Buffer.alloc()` and `Buffer.from()`
  21. though.
  22. See the [porting guide](https://github.com/ChALkeR/safer-buffer/blob/master/Porting-Buffer.md)
  23. for a better description.
  24. ## Why not [safe-buffer](https://npmjs.com/safe-buffer)?
  25. _In short: while `safe-buffer` serves as a polyfill for the new API, it allows old API usage and
  26. itself contains footguns._
  27. `safe-buffer` could be used safely to get the new API while still keeping support for older
  28. Node.js versions (like this module), but while analyzing ecosystem usage of the old Buffer API
  29. I found out that `safe-buffer` is itself causing problems in some cases.
  30. For example, consider the following snippet:
  31. ```console
  32. $ cat example.unsafe.js
  33. console.log(Buffer(20))
  34. $ ./node-v6.13.0-linux-x64/bin/node example.unsafe.js
  35. <Buffer 0a 00 00 00 00 00 00 00 28 13 de 02 00 00 00 00 05 00 00 00>
  36. $ standard example.unsafe.js
  37. standard: Use JavaScript Standard Style (https://standardjs.com)
  38. /home/chalker/repo/safer-buffer/example.unsafe.js:2:13: 'Buffer()' was deprecated since v6. Use 'Buffer.alloc()' or 'Buffer.from()' (use 'https://www.npmjs.com/package/safe-buffer' for '<4.5.0') instead.
  39. ```
  40. This is allocates and writes to console an uninitialized chunk of memory.
  41. [standard](https://www.npmjs.com/package/standard) linter (among others) catch that and warn people
  42. to avoid using unsafe API.
  43. Let's now throw in `safe-buffer`!
  44. ```console
  45. $ cat example.safe-buffer.js
  46. const Buffer = require('safe-buffer').Buffer
  47. console.log(Buffer(20))
  48. $ standard example.safe-buffer.js
  49. $ ./node-v6.13.0-linux-x64/bin/node example.safe-buffer.js
  50. <Buffer 08 00 00 00 00 00 00 00 28 58 01 82 fe 7f 00 00 00 00 00 00>
  51. ```
  52. See the problem? Adding in `safe-buffer` _magically removes the lint warning_, but the behavior
  53. remains identiсal to what we had before, and when launched on Node.js 6.x LTS — this dumps out
  54. chunks of uninitialized memory.
  55. _And this code will still emit runtime warnings on Node.js 10.x and above._
  56. That was done by design. I first considered changing `safe-buffer`, prohibiting old API usage or
  57. emitting warnings on it, but that significantly diverges from `safe-buffer` design. After some
  58. discussion, it was decided to move my approach into a separate package, and _this is that separate
  59. package_.
  60. This footgun is not imaginary — I observed top-downloaded packages doing that kind of thing,
  61. «fixing» the lint warning by blindly including `safe-buffer` without any actual changes.
  62. Also in some cases, even if the API _was_ migrated to use of safe Buffer API — a random pull request
  63. can bring unsafe Buffer API usage back to the codebase by adding new calls — and that could go
  64. unnoticed even if you have a linter prohibiting that (becase of the reason stated above), and even
  65. pass CI. _I also observed that being done in popular packages._
  66. Some examples:
  67. * [webdriverio](https://github.com/webdriverio/webdriverio/commit/05cbd3167c12e4930f09ef7cf93b127ba4effae4#diff-124380949022817b90b622871837d56cR31)
  68. (a module with 548 759 downloads/month),
  69. * [websocket-stream](https://github.com/maxogden/websocket-stream/commit/c9312bd24d08271687d76da0fe3c83493871cf61)
  70. (218 288 d/m, fix in [maxogden/websocket-stream#142](https://github.com/maxogden/websocket-stream/pull/142)),
  71. * [node-serialport](https://github.com/node-serialport/node-serialport/commit/e8d9d2b16c664224920ce1c895199b1ce2def48c)
  72. (113 138 d/m, fix in [node-serialport/node-serialport#1510](https://github.com/node-serialport/node-serialport/pull/1510)),
  73. * [karma](https://github.com/karma-runner/karma/commit/3d94b8cf18c695104ca195334dc75ff054c74eec)
  74. (3 973 193 d/m, fix in [karma-runner/karma#2947](https://github.com/karma-runner/karma/pull/2947)),
  75. * [spdy-transport](https://github.com/spdy-http2/spdy-transport/commit/5375ac33f4a62a4f65bcfc2827447d42a5dbe8b1)
  76. (5 970 727 d/m, fix in [spdy-http2/spdy-transport#53](https://github.com/spdy-http2/spdy-transport/pull/53)).
  77. * And there are a lot more over the ecosystem.
  78. I filed a PR at
  79. [mysticatea/eslint-plugin-node#110](https://github.com/mysticatea/eslint-plugin-node/pull/110) to
  80. partially fix that (for cases when that lint rule is used), but it is a semver-major change for
  81. linter rules and presets, so it would take significant time for that to reach actual setups.
  82. _It also hasn't been released yet (2018-03-20)._
  83. Also, `safer-buffer` discourages the usage of `.allocUnsafe()`, which is often done by a mistake.
  84. It still supports it with an explicit concern barier, by placing it under
  85. `require('safer-buffer/dangereous')`.
  86. ## But isn't throwing bad?
  87. Not really. It's an error that could be noticed and fixed early, instead of causing havoc later like
  88. unguarded `new Buffer()` calls that end up receiving user input can do.
  89. This package affects only the files where `var Buffer = require('safer-buffer').Buffer` was done, so
  90. it is really simple to keep track of things and make sure that you don't mix old API usage with that.
  91. Also, CI should hint anything that you might have missed.
  92. New commits, if tested, won't land new usage of unsafe Buffer API this way.
  93. _Node.js 10.x also deals with that by printing a runtime depecation warning._
  94. ### Would it affect third-party modules?
  95. No, unless you explicitly do an awful thing like monkey-patching or overriding the built-in `Buffer`.
  96. Don't do that.
  97. ### But I don't want throwing…
  98. That is also fine!
  99. Also, it could be better in some cases when you don't comprehensive enough test coverage.
  100. In that case — just don't override `Buffer` and use
  101. `var SaferBuffer = require('safer-buffer').Buffer` instead.
  102. That way, everything using `Buffer` natively would still work, but there would be two drawbacks:
  103. * `Buffer.from`/`Buffer.alloc` won't be polyfilled — use `SaferBuffer.from` and
  104. `SaferBuffer.alloc` instead.
  105. * You are still open to accidentally using the insecure deprecated API — use a linter to catch that.
  106. Note that using a linter to catch accidential `Buffer` constructor usage in this case is strongly
  107. recommended. `Buffer` is not overriden in this usecase, so linters won't get confused.
  108. ## «Without footguns»?
  109. Well, it is still possible to do _some_ things with `Buffer` API, e.g. accessing `.buffer` property
  110. on older versions and duping things from there. You shouldn't do that in your code, probabably.
  111. The intention is to remove the most significant footguns that affect lots of packages in the
  112. ecosystem, and to do it in the proper way.
  113. Also, this package doesn't protect against security issues affecting some Node.js versions, so for
  114. usage in your own production code, it is still recommended to update to a Node.js version
  115. [supported by upstream](https://github.com/nodejs/release#release-schedule).