Skip to content

Commit 9bc2bce

Browse files
committed
[squash] allow Buffer.harden() only in sync mode
1 parent bd1b736 commit 9bc2bce

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

doc/api/buffer.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -954,8 +954,9 @@ This method has effect only of subsequent Buffer API usage, Buffer instances
954954
created before `Buffer.harden()` is called are not affected.
955955

956956
Warning: for ecosystem compatibility and security reasons `Buffer.harden()` can
957-
be called only once and only from the top-level application code. Attempting to
958-
call it from a library in `node_modules` will throw.
957+
be called only once and only from the top-level application code and only before
958+
the first turn of the event loop. Attempting to call it asynchronously in
959+
runtime or from a library in `node_modules` will throw.
959960

960961
By default, it enables mandratory zero fill, disables Buffer pooling, disables
961962
deprecated unsafe Buffer API, freezes `Buffer` and `require('buffer')` objects.

lib/buffer.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@ Buffer.harden = function({
199199
'Calling Buffer.harden() from dependencies is not supported.'
200200
);
201201
}
202+
const perf_hooks = require('perf_hooks');
203+
const stillSynchronous = perf_hooks.performance.nodeTiming.loopStart < 0;
204+
if (!stillSynchronous) throw new ERR_ASSERTION(
205+
'Buffer.harden() should be called only in synchronous mode, during app ' +
206+
'startup. Calling Buffer.harden() asynchronously is not supported.'
207+
);
202208
Object.assign(hardened, {
203209
applied: true,
204210
zeroFill: Boolean(zeroFill),

0 commit comments

Comments
 (0)