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

Bug: is_identifier_string is broken for some characters under typical environments (e.g. "Terser on Node.js and run on browser") #1512

Merged

Conversation

naruaway
Copy link
Contributor

@naruaway naruaway commented Mar 28, 2024

TL;DR; When running on Node.js (e.g. v18), is_identifier_string incorrectly decide that "a・b is a valid identifier" (the string includes "U+30FB KATAKANA MIDDLE DOT") and Terser applies optimization based on this and it can generate broken JavaScript for typical environments.

For example, console.log({"a・b":123}) will be console.log({a・b:123}), which will throw SyntaxError: Invalid or unexpected token when parsed on recent Google Chrome (tested v123). Note that we can check this "syntax error" also in this TypeScript Playground.

How does this happen?

is_identifier_string internally uses ID_Continue regex, which uses \p{ID_Continue} when available (source). This means that depending on the runtime environment (e.g. Node.js / Deno / Bun) and the version, the actual character set will be different since ID_Continue is being updated by Unicode spec.

Unicode says:

The Other_ID_Start and Other_ID_Continue properties are thus designed to ensure that the Unicode identifier specification is backward compatible. Any sequence of characters that qualified as an identifier in some version of Unicode will continue to qualify as an identifier in future versions

So if we use newer logic for deciding "is_identifier" for a minifier, it can generate JavaScript file which is not compatibile with older environment.
I think it's better to hard-code the whole logic of "is_identifier". Otherwise any runtime updates such as Node.js update can start generating yet another broken JavaScript from Terser.

The solution in this PR

I just removed the variant to use unicode properties in regex. Notice that this bug is not reproducible when running Terser in web browser since \p{ID_Continue} is executed by browser, which has older set of characters than Node.js.
It would be nice if we can write test case to avoid reintroducing Unicode properties regex but so far I have no idea. I just added a test case for the currently known problematic case.

Concerns for this PR

There might be performance degradation. But I think it's more important not to generate broken script for typical use cases.
Also it's better to make sure the behavior is consistent across environments for this kind of library IMHO

Misc

console.log(/\p{ID_Continue}/u.test('\u30FB')) can be used for a quick test case, which can be true or false depending on supported unicode version

@fabiosantoscode
Copy link
Collaborator

Thanks for catching this!

@fabiosantoscode
Copy link
Collaborator

This looks good, thanks!

@fabiosantoscode fabiosantoscode merged commit a05702d into terser:master Mar 30, 2024
10 checks passed
@naruaway
Copy link
Contributor Author

Thank you so much for quick review & merge @fabiosantoscode!
I might write more details on this issue later on this MR or somewhere but we are mostly using Node.js v18 and v18.20.0 got released recently, which includes (for the official pre built binary):

ICU updated to 74.2. Includes CLDR 44.1 and Unicode 15.1.

Here the ICU version update (74.2) was the trigger of the issue for "U+30FB KATAKANA MIDDLE DOT" character (other affected characters to be researched)

So after the Node.js update of v18.20.0, we observed some of the final JavaScript bundle from webpack got "broken" (I mean, it causes parse error even with recent Google Chrome)
Do you think you can publish the new version of terser soonish?

@fabiosantoscode
Copy link
Collaborator

As a mitigation for your issue, you can use the ascii_only format option (I think).

I release Terser on weekdays, around 1-3PM UTC, to avoid causing breakage at bad times.

@naruaway
Copy link
Contributor Author

Ok, that's reasonable, thanks for clarification and your effort to maintain Terser 🙏

@naruaway
Copy link
Contributor Author

Note: U+30FB and U+0FF6 seem to have special historical context, which is specially treated and explained well in esbuild: https://github.com/evanw/esbuild/blob/44e746965d783646f97daf3d0617ff816727e7fb/scripts/gen-unicode-table.js#L51

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

Successfully merging this pull request may close these issues.

None yet

2 participants