fix(ext/node): node:buffer validates INSPECT_MAX_BYTES (#29469)
Some checks are pending
ci / pre-build (push) Waiting to run
ci / test debug linux-aarch64 (push) Blocked by required conditions
ci / test release linux-aarch64 (push) Blocked by required conditions
ci / test debug macos-aarch64 (push) Blocked by required conditions
ci / test release macos-aarch64 (push) Blocked by required conditions
ci / bench release linux-x86_64 (push) Blocked by required conditions
ci / lint debug linux-x86_64 (push) Blocked by required conditions
ci / lint debug macos-x86_64 (push) Blocked by required conditions
ci / lint debug windows-x86_64 (push) Blocked by required conditions
ci / test debug linux-x86_64 (push) Blocked by required conditions
ci / test release linux-x86_64 (push) Blocked by required conditions
ci / test debug macos-x86_64 (push) Blocked by required conditions
ci / test release macos-x86_64 (push) Blocked by required conditions
ci / test debug windows-x86_64 (push) Blocked by required conditions
ci / test release windows-x86_64 (push) Blocked by required conditions
ci / build wasm32 (push) Blocked by required conditions
ci / publish canary (push) Blocked by required conditions

Co-authored-by: David Sherret <dsherret@users.noreply.github.com>
This commit is contained in:
Bartek Iwańczuk 2025-05-28 01:31:35 +02:00 committed by GitHub
parent e3bf5ee5db
commit 1b6736d3f9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 64 additions and 9 deletions

View file

@ -119,7 +119,9 @@ const MAX_UINT32 = 2 ** 32;
const customInspectSymbol = SymbolFor("nodejs.util.inspect.custom");
export const INSPECT_MAX_BYTES = 50;
let INSPECT_MAX_BYTES_ = 50;
export const INSPECT_MAX_BYTES = INSPECT_MAX_BYTES_;
export const constants = {
MAX_LENGTH: kMaxLength,
@ -611,17 +613,17 @@ Buffer.prototype[customInspectSymbol] =
Buffer.prototype.inspect =
function inspect() {
let str = "";
const max = INSPECT_MAX_BYTES;
str = StringPrototypeTrim(
StringPrototypeReplace(
// deno-lint-ignore prefer-primordials
this.toString("hex", 0, max),
this.toString("hex", 0, INSPECT_MAX_BYTES_),
SPACER_PATTERN,
"$1 ",
),
);
if (this.length > max) {
str += " ... ";
if (this.length > INSPECT_MAX_BYTES_) {
const remaining = this.length - INSPECT_MAX_BYTES_;
str += ` ... ${remaining} more byte${remaining > 1 ? "s" : ""}`;
}
return "<Buffer " + str + ">";
};
@ -2766,7 +2768,7 @@ export function transcode(source, fromEnco, toEnco) {
}
}
export default {
const mod = {
atob,
btoa,
Blob,
@ -2774,9 +2776,21 @@ export default {
constants,
isAscii,
isUtf8,
INSPECT_MAX_BYTES,
get INSPECT_MAX_BYTES() {
return INSPECT_MAX_BYTES_;
},
set INSPECT_MAX_BYTES(val) {
validateNumber(val, "INSPECT_MAX_BYTES", 0);
INSPECT_MAX_BYTES_ = val;
},
kMaxLength,
kStringMaxLength,
SlowBuffer,
transcode,
};
// NB(bartlomieju): we want to have a default exports from this module for ES imports,
// as well as make it work with `require` in such a way that getters/setters
// for `INSPECT_MAX_BYTES` work correctly - using `as "module.exports"` ensures
// that `require`ing this module does that.
export { mod as "module.exports", mod as default };

View file

@ -298,6 +298,7 @@
"test-buffer-readint.js",
"test-buffer-readuint.js",
"test-buffer-safe-unsafe.js",
"test-buffer-set-inspect-max-bytes.js",
"test-buffer-sharedarraybuffer.js",
"test-buffer-slice.js",
"test-buffer-slow.js",

View file

@ -1,7 +1,7 @@
<!-- deno-fmt-ignore-file -->
# Remaining Node Tests
1176 tests out of 3993 have been ported from Node 23.9.0 (29.45% ported, 71.07% remaining).
1177 tests out of 3993 have been ported from Node 23.9.0 (29.48% ported, 71.05% remaining).
NOTE: This file should not be manually edited. Please edit `tests/node_compat/config.json` and run `deno task setup` in `tests/node_compat/runner` dir instead.
@ -292,7 +292,6 @@ NOTE: This file should not be manually edited. Please edit `tests/node_compat/co
- [parallel/test-buffer-pool-untransferable.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-buffer-pool-untransferable.js)
- [parallel/test-buffer-prototype-inspect.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-buffer-prototype-inspect.js)
- [parallel/test-buffer-resizable.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-buffer-resizable.js)
- [parallel/test-buffer-set-inspect-max-bytes.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-buffer-set-inspect-max-bytes.js)
- [parallel/test-buffer-write-fast.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-buffer-write-fast.js)
- [parallel/test-c-ares.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-c-ares.js)
- [parallel/test-child-process-advanced-serialization-largebuffer.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-child-process-advanced-serialization-largebuffer.js)

View file

@ -0,0 +1,41 @@
// deno-fmt-ignore-file
// deno-lint-ignore-file
// Copyright Joyent and Node contributors. All rights reserved. MIT license.
// Taken from Node 23.9.0
// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
'use strict';
require('../common');
const assert = require('assert');
const buffer = require('buffer');
const rangeErrorObjs = [NaN, -1];
const typeErrorObj = 'and even this';
for (const obj of rangeErrorObjs) {
assert.throws(
() => buffer.INSPECT_MAX_BYTES = obj,
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
}
);
assert.throws(
() => buffer.INSPECT_MAX_BYTES = obj,
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
}
);
}
assert.throws(
() => buffer.INSPECT_MAX_BYTES = typeErrorObj,
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
}
);