-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Fix deprecated Buffer constructor usage and add safeguards #2947
Conversation
This PR is out of scope for uglify. |
@kzc Is |
As long as the old Buffer constructor works there's no need to change it. |
@kzc If
|
@kzc Alternatively, if this looks too cumbersome, I would recommend to just use It would imply dropping support for Node.js ≤ 4.5.0 then, but those shouldn't be used nowdays. Even the whole 4.x branch (v4.8.7 is the latest patch release there atm) would be out of maintenance LTS in about two months from now. |
@ChALkeR thanks for the contribution and the information about Node.js deprecation plans. I think the best course of action would be to leave the PR open for now and wait for the deprecation to happen, since it sounds to me nothing would actually break. FWIW, those two functions are only used for source-map related functionalities, so the majority use case of mangle & compress would not trigger any future warning messages. |
Tracking: nodejs/node#19079 |
Node 10 is out now. This PR should be applied. |
Alternative approach would be to drop support for Node.js < 4.5.0, then this patch would become a trivial two-liner change with no line additions. Note that the whole Node.js 4.x branch (including 4.9.x) would become unsupported iby upstream in several days. Upd: ah, I already mentioned that. |
Accidental calls by a future uglify maintainer? Does |
Accidential calls to
Testcase: var UglifyJS = require(".");
var result = UglifyJS.minify({
"file1.js": "var a = function() {};"
}, {
sourceMap: { url: "inline" }
});
console.log(result); This is what happens:
It's not an error, but it's a warning. That warning is currently not emitted when UglifyJS2 is placed inside a Running |
lib/minify.js
Outdated
return new Buffer(b64, "base64").toString(); | ||
if (Buffer.from && Buffer.from !== Uint8Array.from) { | ||
// Node >= 4.5.0 | ||
return Buffer.from(b64, "base64").toString("ascii"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
although the function is called to_ascii
, converting the buffer to ascii instead of utf8 breaks parsing of source maps with valid unicode characters in them. i think this branch should just do .toString()
just like the one below.
e; only relevant if maintainers want to take this at all, of course :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Browser-side atob()
and btoa()
methods (used above) do not support non-ascii.
Refs:
- https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/atob
- https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/btoa
So parsing of source maps with valid unicode characters is already broken when this method is executed in a browser environment, and changing this single .toString()
won't help much. I will update it, though, to keep the behavior closer to the original one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
ae6f08b added the `'ascii'` parameter to Buffer conversion functions `to_ascii` and `to_base64`. However `to_ascii` didn't actually convert to ascii before, but to a utf8 string (the default Buffer.toString behaviour). Passing in 'ascii' breaks reading and writing valid source map with unicode characters. This patch removes the 'ascii' parameter so that the conversion uses utf8 again. I would've filed this upstream but it didn't get merged into uglify-js yet (only uglify-es, and thus terser). Ref mishoo/UglifyJS#2947
ae6f08b added the `'ascii'` parameter to Buffer conversion functions `to_ascii` and `to_base64`. However `to_ascii` didn't actually convert to ascii before, but to a utf8 string (the default Buffer.toString behaviour). Passing in 'ascii' breaks reading and writing valid source map with unicode characters. This patch removes the 'ascii' parameter so that the conversion uses utf8 again. I would've filed this upstream but it didn't get merged into uglify-js yet (only uglify-es, and thus terser). Ref mishoo/UglifyJS#2947
This avoids using deprecated Buffer constructor API on newer Node.js versions. To achieve that, Buffer.from presence is checked, with validating that it's not the same method as Uint8Array.from. Also, additional checks were added for older Node.js versions to ensure that a number is never accidentally passed to the Buffer constructor. Throwing is in line with browser atob/btoa behavior, and in line with what Buffer.from does on numbers in newer Node.js versions. No actual security issues present in that code, the safeguard has been added preemptively to avoid accidential calls to atob/btoa(number) in the future. Refs: https://nodejs.org/api/deprecations.html#deprecations_dep0005_buffer_constructor
7d1c0a7
to
b468103
Compare
fixes mishoo#2947 fixes mishoo#3277 fixes mishoo#3411
This avoids using deprecated Buffer constructor API on newer Node.js versions.
To achieve that, Buffer.from presence is checked, with validating that it's not the same method as Uint8Array.from.
Also, additional checks were added for older Node.js versions to ensure that a number is never accidentally passed to the Buffer constructor. Throwing is in line with browser atob/btoa behavior, and in line with what Buffer.from does on numbers in newer Node.js versions.
No actual security issues present in that code, the safeguard has been added preemptively to avoid accidential calls to atob/btoa(number) in the future.
Refs:
https://nodejs.org/api/deprecations.html#deprecations_dep0005_buffer_constructor