Skip to content

util: reject immutable buffers in encodeInto - #67017

Open
Davien21 wants to merge 1 commit into
nodejs:mainfrom
Davien21:encodeinto-immutable
Open

Davien21 wants to merge 1 commit into
nodejs:mainfrom
Davien21:encodeinto-immutable

Conversation

@Davien21

Copy link
Copy Markdown

Context

encodeInto() was writing into a Uint8Array even when its buffer came from transferToImmutable(), so it changed memory that's supposed to be read-only.

Now it throws ERR_INVALID_ARG_VALUE and the bytes are left alone.

Fixes: #66346 This is part of the immutable ArrayBuffer audit.

The check is in the C++ binding, before either write path, so the short-string path and the fast path for long strings are both covered. When the buffer is immutable the binding returns false and the JS side throws, with the same error code and message as websocketMask().

One thing I wasn't sure about: an empty string into a zero-length immutable view also throws, rather than quietly doing nothing. I went with consistency, but I'm happy to change it if you'd rather it be a no-op?

I added test/parallel/test-whatwg-encoding-custom-textencoder-immutable.js, which tries empty, short, accented, long, multi-byte and lone-surrogate strings against whole, offset and zero-length views. I built Node locally on macOS (arm64), and tools/test.py test/parallel/test-whatwg-encoding-* passed, as did make lint-js lint-cpp.

encodeInto() wrote into a Uint8Array backed by an immutable
ArrayBuffer created with transferToImmutable(). Check the backing
store before either write path and throw ERR_INVALID_ARG_VALUE,
matching websocketMask().

Fixes: nodejs#66346
Signed-off-by: Chidiebere Ekennia <54110155+Davien21@users.noreply.github.com>
@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. encoding Issues and PRs related to the TextEncoder and TextDecoder APIs. needs-ci PRs that need a full CI run. labels Oct 26, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Welcome to Node.js, and thank you for your first contribution!

Before review, please take a moment to read:

Please make sure every commit is signed off. For a first pull request, GitHub Actions require collaborator approval and Jenkins CI must be started by a collaborator or triager, so an initial wait is normal.

Caution

AgentScan found account activity patterns that may be consistent with automation. This is a heuristic, not proof that this pull request was opened by an agent or violates policy. AI-assisted contributions are permitted, but automated tooling must not open pull requests without advance approval, and contributors must personally understand, test, verify, and take responsibility for every submitted change. See the AgentScan analysis, AI use policy, and automation policy for additional context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agentscan:mixed-signals c++ Issues and PRs that require attention from people who are familiar with C++. encoding Issues and PRs related to the TextEncoder and TextDecoder APIs. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Immutable ArrayBuffer's are almost here. Are we ready?

2 participants