Skip to content
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

Exec stdin might lost data if pass in Readable.from(string) #1772

Open
hax opened this issue Jul 3, 2024 · 3 comments
Open

Exec stdin might lost data if pass in Readable.from(string) #1772

hax opened this issue Jul 3, 2024 · 3 comments

Comments

@hax
Copy link

hax commented Jul 3, 2024

const buff = Buffer.alloc(data.length + 1);

Current implementation assume stdin is always a binary stream, but it does not really check the type. So if pass Readable.from(string) as stdin, most time it works, unless the string contains non-ASCII characters, in such case, data is string and data.length is number of UTF16 code units, which will smaller than the number of UTF8 code units or bytes.

I suggest the implementation should either deal with the string case, or just throw type error if not buffer.

@brendandburns
Copy link
Contributor

Happy to take a PR with a unit test to repro this and either fix that you prefer.

@cjihrig
Copy link
Contributor

cjihrig commented Sep 27, 2024

Here is a small reproduction:

'use strict';
const { deepStrictEqual } = require('node:assert');
const { Readable } = require('node:stream');
const { test } = require('node:test');
const { WebSocketHandler } = require('./dist/web-socket-handler');

test('repro', () => {
  const { promise, resolve } = Promise.withResolvers();
  const stream = new Readable({ read() {} });
  const ws = {
    close() {},
    send(data) {
      deepStrictEqual(data, Buffer.from([0x0f, 0xe2, 0x98, 0x83]));
      resolve();
    }
  };

  stream.setEncoding('utf8');
  stream.push('☃');
  WebSocketHandler.handleStandardInput(ws, stream, 0x0f);
  return promise;
});

I'll work on a fix.

cjihrig added a commit to cjihrig/javascript that referenced this issue Sep 28, 2024
This commit updates the way the WebSocketHandler processes
string data. Prior to this commit, it relied on string lengths,
which does not properly account for multi-byte characters. This
commit changes the logic to account for string encodings that
contain multi-byte characters.

Fixes: kubernetes-client#1772
@cjihrig
Copy link
Contributor

cjihrig commented Oct 1, 2024

I think this can be closed since #1900 was merged. I'm a bit surprised that it didn't auto-close.

EDIT: Maybe it didn't auto-close since the PR targeted the release-1.x branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants