GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — buffer: introduce Buffer.encode() method by JacksonTian · Pull Request #4565 · nodejs/node

Conversation

@JacksonTian

@JacksonTian JacksonTian commented Jan 7, 2016

Copy link
Copy Markdown
Contributor

Buffer constructor has too much behaviour.

it is usual to convert string to Buffer in the form of new
Buffer(str).

If don't check type of first argument, new Buffer(number) can lead
Buffer vulnerability. For example:

  • https://github.com/websockets/ws/releases/tag/1.0.1
  • RangeError: Attempt to allocate Buffer larger than maximum size: 0x3fffffff bytes qiniu/nodejs-sdk#123

Introduce Buffer.encode() method to be explicit about encoding.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@JacksonTian
buffer: introduce Buffer.encode() method
1827b5d
Buffer constructor has too much behaviour.

it is usual to convert string to Buffer in the form of new
Buffer(str).

If don't check type of first argument, new Buffer(number) can lead
Buffer vulnerability. For example:

- https://github.com/websockets/ws/releases/tag/1.0.1
- qiniu/nodejs-sdk#123

Introduce Buffer.encode() method to be explicit abort encoding.
@mscdex mscdex added the buffer Issues and PRs related to the buffer subsystem. label Jan 7, 2016
@mscdex

mscdex commented Jan 7, 2016

Copy link
Copy Markdown
Contributor

Why not simply do new Buffer(val.toString(), encoding) or if val can be undefined/null, new Buffer((val || '').toString(), encoding)? I don't see why an extra method like this needs to be added to core.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@JacksonTian

JacksonTian commented Jan 7, 2016

Copy link
Copy Markdown
Contributor Author

In fact, too much guys use new Buffer() but forget to validate/convert input.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mscdex

mscdex commented Jan 7, 2016

Copy link
Copy Markdown
Contributor

IMHO that seems more like a userland problem. Either way, any such userland code would need to be changed, so you might as well just do new Buffer((val || '').toString(), encoding).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ronkorving

ronkorving commented Jan 7, 2016

Copy link
Copy Markdown
Contributor

A userland problem that is promoted by the design of Buffer. I think this is well illustrated by the fact that your solution of val || '' would turn the number 0 into empty string, not the string '0'. That's not converting the number to a string. IMHO, the proper userland solution would be new Buffer(String(val));.

But that aside, doesn't this illustrate that there may indeed be a bit of an issue here?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@cjihrig

cjihrig commented Jan 7, 2016

Copy link
Copy Markdown
Contributor

IMO, a function like this falls into the same discussion as a parameter to the Buffer constructor to zero fill the allocated memory. Both of these have trivial existing solutions (Buffer(val + '') and fill(0)). If people aren't using the existing solutions, I'm not really convinced that they would use these new approaches either. I think we just need to increase and improve messaging about this.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mscdex

mscdex commented Jan 7, 2016

Copy link
Copy Markdown
Contributor

@ronkorving That was merely an example. My point was this can be easily done in userland and IMHO it does not require the addition of a new Buffer method.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@trevnorris

trevnorris commented Jan 7, 2016

Copy link
Copy Markdown
Contributor

It needs to be accepted that this is not a vulnerability of the Buffer API. How it will operate is well documented, and there aren't any surprises when using Buffer as documented (if there are then we do have a bug, or documentation fail).

Specifically in the ws case it's even stated in the release notes that they "didn't do any checks for the type of data [they] were sending."[1] This is akin to not sanitizing an incoming query and allowing a SQL injection attack, except in the SQL case it's written off as poor programming practice from the implementer.

So in any case the most we'd be doing is making the API more "developer-friendly". As for this PR, allowing the argument to be coerced doesn't follow its own documentation. If anything will be accepted then it should state {Value} and that any non string argument will be coerced. Or it should throw if the argument is not a string.

Anyway, I'm open to discussion on the topic but want to make sure we really understand why this or something similar would be added.

[1] https://github.com/websockets/ws/releases/tag/1.0.1

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@JacksonTian

JacksonTian commented Jan 8, 2016

Copy link
Copy Markdown
Contributor Author

Yes, Make the API more "developer-friendly" is my point.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jasnell jasnell added the semver-minor PRs that contain new features and should be released in the next minor version. label Jan 8, 2016
@jasnell

jasnell commented Mar 22, 2016

Copy link
Copy Markdown
Member

This can be closed now that the new buffer api's have landed.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jasnell jasnell closed this Mar 22, 2016
@JacksonTian

JacksonTian commented Mar 22, 2016

Copy link
Copy Markdown
Contributor Author

Thanks @jasnell

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@JacksonTian
JacksonTian deleted the safe_encoding branch March 22, 2016 02:13
This was referenced Sep 15, 2021
[Snyk] Fix for 1 vulnerabilities Zintexiss/node-22#60
Open
[Snyk] Fix for 1 vulnerabilities baby636/node#23
Open
[Snyk] Fix for 1 vulnerabilities ryan-ally/node#41
Open
This was referenced Dec 6, 2023
[Snyk] Security upgrade mocha from 7.2.0 to 8.3.0 aliscco/alisco-node#1129
Open
[Snyk] Security upgrade mocha from 7.2.0 to 8.3.0 aliscco/alisco-node#1130
Open
@leonardoadame leonardoadame mentioned this pull request Dec 6, 2023
[Snyk] Security upgrade mocha from 7.2.0 to 8.3.0 leonardoadame/node#557
Open
This was referenced Dec 6, 2023
[Snyk] Security upgrade mocha from 6.2.3 to 8.3.0 aliscco/alisco-node#1131
Open
[Snyk] Security upgrade mocha from 6.2.3 to 8.3.0 aliscco/alisco-node#1132
Open
[Snyk] Security upgrade mocha from 7.2.0 to 8.3.0 aliscco/alisco-node#1133
Open
[Snyk] Security upgrade mocha from 6.2.3 to 8.3.0 aliscco/alisco-node#1134
Open
@ryan-ally ryan-ally mentioned this pull request Dec 7, 2023
[Snyk] Security upgrade mocha from 7.2.0 to 8.3.0 ryan-ally/node#97
Open
This was referenced Dec 7, 2023
[Snyk] Security upgrade mocha from 6.2.3 to 8.3.0 aliscco/alisco-node#1135
Open
[Snyk] Security upgrade mocha from 6.2.3 to 8.3.0 aliscco/alisco-node#1136
Open
[Snyk] Security upgrade mocha from 6.2.3 to 8.3.0 aliscco/alisco-node#1137
Open
[Snyk] Security upgrade mocha from 7.2.0 to 8.3.0 aliscco/alisco-node#1139
Open
This was referenced Dec 7, 2023
[Snyk] Fix for 1 vulnerabilities aliscco/alisco-node#1140
Open
[Snyk] Security upgrade mocha from 6.1.4 to 8.3.0 aliscco/alisco-node#1142
Open
[Snyk] Security upgrade mocha from 6.2.3 to 8.3.0 aliscco/alisco-node#1143
Open
@leonardoadame leonardoadame mentioned this pull request Dec 7, 2023
[Snyk] Security upgrade mocha from 6.2.3 to 8.3.0 leonardoadame/node#562
Open
This was referenced Dec 7, 2023
[Snyk] Security upgrade mocha from 6.2.3 to 8.3.0 aliscco/alisco-node#1144
Open
[Snyk] Security upgrade mocha from 6.2.0 to 8.3.0 aliscco/alisco-node#1145
Open
This was referenced Dec 7, 2023
[Snyk] Security upgrade mocha from 7.1.1 to 8.3.0 leonardoadame/node#563
Open
[Snyk] Security upgrade mocha from 6.2.3 to 8.3.0 leonardoadame/node#566
Open
@baby636 baby636 mentioned this pull request Dec 7, 2023
[Snyk] Security upgrade mocha from 7.2.0 to 8.3.0 baby636/node#120
Open
This was referenced Dec 7, 2023
[Snyk] Security upgrade mocha from 6.2.3 to 8.3.0 aliscco/alisco-node#1147
Open
[Snyk] Security upgrade mocha from 7.2.0 to 8.3.0 aliscco/alisco-node#1148
Open
[Snyk] Security upgrade mocha from 6.2.3 to 8.3.0 aliscco/alisco-node#1149
Open
[Snyk] Security upgrade mocha from 6.2.3 to 8.3.0 aliscco/alisco-node#1150
Open
[Snyk] Security upgrade mocha from 7.2.0 to 8.3.0 aliscco/alisco-node#1151
Open
[Snyk] Security upgrade mocha from 6.2.0 to 8.3.0 aliscco/alisco-node#1152
Open
[Snyk] Security upgrade mocha from 7.2.0 to 8.3.0 aliscco/alisco-node#1153
Open
[Snyk] Security upgrade mocha from 6.2.3 to 8.3.0 aliscco/alisco-node#1154
Open
[Snyk] Security upgrade mocha from 6.2.3 to 8.3.0 aliscco/alisco-node#1155
Open
This was referenced Dec 8, 2023
[Snyk] Fix for 1 vulnerabilities leonardoadame/node#567
Open
[Snyk] Security upgrade mocha from 7.2.0 to 8.3.0 leonardoadame/node#569
Open
@aliscco aliscco mentioned this pull request Dec 8, 2023
[Snyk] Security upgrade mocha from 7.2.0 to 8.3.0 aliscco/alisco-node#1157
Open
This was referenced Dec 8, 2023
[Snyk] Security upgrade mocha from 7.2.0 to 8.3.0 leonardoadame/node#570
Open
[Snyk] Security upgrade mocha from 7.2.0 to 8.3.0 leonardoadame/node#571
Open
@aliscco aliscco mentioned this pull request Dec 8, 2023
[Snyk] Security upgrade mocha from 7.2.0 to 8.3.0 aliscco/alisco-node#1160
Open
@leonardoadame leonardoadame mentioned this pull request Dec 8, 2023
[Snyk] Security upgrade mocha from 6.2.3 to 8.3.0 leonardoadame/node#572
Open
This was referenced Dec 8, 2023
[Snyk] Security upgrade mocha from 7.2.0 to 8.3.0 aliscco/alisco-node#1162
Open
[Snyk] Security upgrade mocha from 6.2.3 to 8.3.0 aliscco/alisco-node#1163
Open
[Snyk] Security upgrade mocha from 6.2.3 to 8.3.0 aliscco/alisco-node#1164
Open
@adamlaska adamlaska mentioned this pull request Dec 8, 2023
[Snyk] Security upgrade mocha from 6.2.3 to 8.3.0 adamlaska/node#134
Open
@leonardoadame leonardoadame mentioned this pull request Dec 8, 2023
[Snyk] Security upgrade mocha from 6.2.3 to 8.3.0 leonardoadame/node#574
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

buffer Issues and PRs related to the buffer subsystem. semver-minor PRs that contain new features and should be released in the next minor version.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@JacksonTian @mscdex @ronkorving @cjihrig @trevnorris @jasnell
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.