Skip to content

Commit 028b6ed

Browse files
committed
[squash] buffer: add more options (all enabled by default)
1 parent 5a29ee6 commit 028b6ed

1 file changed

Lines changed: 32 additions & 18 deletions

File tree

lib/buffer.js

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,15 @@ const zeroFill = bindingZeroFill || [0];
113113
// Hardening Buffer enables Buffer constructor runtime deprecation,
114114
// disables pooling, and enables mandratory zero-fill. This is more secure, but
115115
// has potential performance impact, depending on the usecase.
116-
const harden = {
117-
NONE: 0, // falsy
118-
STRICT: 1,
119-
LAX: 2,
116+
const hardened = {
117+
applied: false,
118+
zeroFill: false,
119+
disablePool: false,
120+
throwOnUnsafe: false,
120121
};
121-
let hardened = harden.NONE;
122122

123123
function createUnsafeBuffer(size) {
124-
zeroFill[0] = hardened ? 1 : 0;
124+
zeroFill[0] = hardened.zeroFill ? 1 : 0;
125125
try {
126126
return new FastBuffer(size);
127127
} finally {
@@ -151,8 +151,8 @@ const bufferWarning = 'Buffer() is deprecated due to security and usability ' +
151151
'Buffer.allocUnsafe(), or Buffer.from() methods instead.';
152152

153153
function showFlaggedDeprecation() {
154-
if (hardened) {
155-
if (hardened !== harden.LAX) {
154+
if (hardened.applied) {
155+
if (hardened.throwOnUnsafe) {
156156
throw new ERR_ASSERTION(
157157
'Unsafe Buffer() API is forbidden by Buffer strict hardening opt-in.'
158158
);
@@ -181,8 +181,13 @@ function showFlaggedDeprecation() {
181181
}
182182

183183
// Calling this method does not affect existing buffers, only new ones.
184-
Buffer.harden = function({ strict = true } = {}) {
185-
if (hardened) {
184+
Buffer.harden = function({
185+
zeroFill = true,
186+
disablePool = true,
187+
throwOnUnsafe = true,
188+
freeze = true,
189+
} = {}) {
190+
if (hardened.applied) {
186191
// So that params are not changed afterwards
187192
throw new ERR_ASSERTION(
188193
'Buffer.harden can be called only once'
@@ -194,14 +199,23 @@ Buffer.harden = function({ strict = true } = {}) {
194199
'Calling Buffer.harden() from dependencies is not supported.'
195200
);
196201
}
197-
hardened = strict ? harden.STRICT : harden.LAX;
198-
Object.defineProperty(Buffer, 'poolSize', {
199-
enumerable: true,
200-
get: () => 0,
201-
set: () => {},
202+
Object.assign(hardened, {
203+
applied: true,
204+
zeroFill: Boolean(zeroFill),
205+
disablePool: Boolean(disablePool),
206+
throwOnUnsafe: Boolean(throwOnUnsafe),
202207
});
203-
Object.freeze(Buffer);
204-
Object.freeze(module.exports);
208+
if (disablePool) {
209+
Object.defineProperty(Buffer, 'poolSize', {
210+
enumerable: true,
211+
get: () => 0,
212+
set: () => {},
213+
});
214+
}
215+
if (freeze) {
216+
Object.freeze(Buffer);
217+
Object.freeze(module.exports);
218+
}
205219
};
206220

207221
function toInteger(n, defaultVal) {
@@ -412,7 +426,7 @@ function allocate(size) {
412426
if (size <= 0) {
413427
return new FastBuffer();
414428
}
415-
if (size < (Buffer.poolSize >>> 1) && !hardened) {
429+
if (size < (Buffer.poolSize >>> 1) && !hardened.disablePool) {
416430
if (size > (poolSize - poolOffset))
417431
createPool();
418432
const b = new FastBuffer(allocPool, poolOffset, size);

0 commit comments

Comments
 (0)