Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 36.4k
buffer: add Buffer.harden() method#28439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Uh oh!
There was an error while loading. Please reload this page.
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e5d71cd
buffer: add Buffer.harden() method
ChALkeR cebb1d5
[squash] buffer: undo freezing Buffer.prototype
ChALkeR 5a29ee6
[squash] buffer: add "strict" harden mode (default)
ChALkeR 028b6ed
[squash] buffer: add more options (all enabled by default)
ChALkeR 331a982
[squash] doc: add docs for Buffer.harden()
ChALkeR bd1b736
[squash] buffer: update a comment
ChALkeR 9bc2bce
[squash] allow Buffer.harden() only in sync mode
ChALkeR 97a956e
[squash] doc: label Buffer.harden as Experimental
ChALkeR 587c8d0
[squash] Update doc/api/buffer.md
ChALkeR File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -63,6 +63,7 @@ const { | ||
| const { | ||
| codes: { | ||
| ERR_ASSERTION, | ||
| ERR_BUFFER_OUT_OF_BOUNDS, | ||
| ERR_INVALID_ARG_TYPE, | ||
| ERR_INVALID_ARG_VALUE, | ||
| @@ -109,8 +110,18 @@ let poolSize, poolOffset, allocPool; | ||
| // do not own the ArrayBuffer allocator. Zero fill is always on in that case. | ||
| const zeroFill = bindingZeroFill || [0]; | ||
| // Hardening Buffer enables disables unsafe Buffer API, disables pooling, | ||
| // and enables mandratory zero-fill. This is more secure, but has potential | ||
| // performance and compatibility impact, depending on the usecase. | ||
| const hardened = { | ||
| applied: false, | ||
| zeroFill: false, | ||
| disablePool: false, | ||
| throwOnUnsafe: false, | ||
| }; | ||
| function createUnsafeBuffer(size) { | ||
| zeroFill[0] = 0; | ||
| zeroFill[0] = hardened.zeroFill ? 1 : 0; | ||
| try { | ||
| return new FastBuffer(size); | ||
| } finally { | ||
| @@ -140,6 +151,18 @@ const bufferWarning = 'Buffer() is deprecated due to security and usability ' + | ||
| 'Buffer.allocUnsafe(), or Buffer.from() methods instead.'; | ||
| function showFlaggedDeprecation() { | ||
| if (hardened.applied) { | ||
| if (hardened.throwOnUnsafe) { | ||
| throw new ERR_ASSERTION( | ||
| 'Unsafe Buffer() API is forbidden by Buffer strict hardening opt-in.' | ||
| ); | ||
| } | ||
| if (bufferWarningAlreadyEmitted) return; | ||
| process.emitWarning(bufferWarning, 'DeprecationWarning', 'DEP0005'); | ||
| bufferWarningAlreadyEmitted = true; | ||
| return; | ||
| } | ||
| if (bufferWarningAlreadyEmitted || | ||
| ++nodeModulesCheckCounter > 10000 || | ||
| (!require('internal/options').getOptionValue('--pending-deprecation') && | ||
| @@ -157,6 +180,50 @@ function showFlaggedDeprecation() { | ||
| bufferWarningAlreadyEmitted = true; | ||
| } | ||
| // Calling this method does not affect existing buffers, only new ones. | ||
| Buffer.harden = function({ | ||
| zeroFill = true, | ||
| disablePool = true, | ||
| throwOnUnsafe = true, | ||
| freeze = true, | ||
| } = {}) { | ||
| if (hardened.applied) { | ||
| // So that params are not changed afterwards | ||
| throw new ERR_ASSERTION( | ||
| 'Buffer.harden can be called only once' | ||
| ); | ||
| } | ||
| if (isInsideNodeModules()) { | ||
| throw new ERR_ASSERTION( | ||
| 'Buffer.harden() should be called only from the top-level module. ' + | ||
ChALkeR marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| 'Calling Buffer.harden() from dependencies is not supported.' | ||
| ); | ||
| } | ||
| const perf_hooks = require('perf_hooks'); | ||
| const stillSynchronous = perf_hooks.performance.nodeTiming.loopStart < 0; | ||
| if (!stillSynchronous) throw new ERR_ASSERTION( | ||
| 'Buffer.harden() should be called only in synchronous mode, during app ' + | ||
| 'startup. Calling Buffer.harden() asynchronously is not supported.' | ||
| ); | ||
| Object.assign(hardened, { | ||
| applied: true, | ||
| zeroFill: Boolean(zeroFill), | ||
| disablePool: Boolean(disablePool), | ||
| throwOnUnsafe: Boolean(throwOnUnsafe), | ||
| }); | ||
| if (disablePool) { | ||
| Object.defineProperty(Buffer, 'poolSize', { | ||
| enumerable: true, | ||
| get: () => 0, | ||
| set: () => {}, | ||
| }); | ||
| } | ||
| if (freeze) { | ||
| Object.freeze(Buffer); | ||
| Object.freeze(module.exports); | ||
| } | ||
| }; | ||
| function toInteger(n, defaultVal) { | ||
| n = +n; | ||
| if (!Number.isNaN(n) && | ||
| @@ -365,7 +432,7 @@ function allocate(size) { | ||
| if (size <= 0) { | ||
| return new FastBuffer(); | ||
| } | ||
| if (size < (Buffer.poolSize >>> 1)) { | ||
| if (size < (Buffer.poolSize >>> 1) && !hardened.disablePool) { | ||
| if (size > (poolSize - poolOffset)) | ||
| createPool(); | ||
| const b = new FastBuffer(allocPool, poolOffset, size); | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.