Skip to content

Fix "Cannot read properties of undefined" crash on malformed arbitrary value #18133

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

Merged
merged 4 commits into from
May 23, 2025

Conversation

RobinMalfait
Copy link
Member

@RobinMalfait RobinMalfait commented May 23, 2025

This PR fixes a crash when an arbitrary value was malformed and crashed the build.

If you have a utility like [--btn-border:var(--color-maroon)/90)] which is malformed, it will crash the build. It might not be easy to spot but the easy is the additional ) after the 90.

The reason this crashes is because we parse the value var(--color-maroon)/90) and when we see ) we assume it's the end of a "function" which also assumes it was preceded by a (. This is not the case and we crash.

This PR fixes that by not assuming the parsed object is available and uses ? to be safe and only access nodes if it's available.

I'm actually not 100% sure what the best solution is in this scenario because these candidates could (and will) be returned from Oxide so even if we throw a more descriptive error, it will still crash the build and you might not even have control over the candidate.

This candidate will now eventually generate the following CSS:

.\[--btn-border\:var\(--color-maroon\)\/90\)\] {
  --btn-border: var(--color-maroon) / ;
}

Which still looks odd, but even Lightning CSS doesn't throw an error in this case (because it's a CSS variable definition), so I think it's the best we can do. If you open your devtools you will see the weird values, so it's still debug-able.

image

Fixes: #17064

Test plan

Manually tested the candidate that crashed it, and after the change generated the above CSS. Then used it in JSFiddle to proof it's fixed now. https://jsfiddle.net/z850ykew/

Couldn't use Tailwind Play because the candidate will cause a crash there as well 😅

- Add missing `path`, which is required according to the types
- Remove unused variables warnings
Right now if a candidate looks like `[--btn-border:var(--color-maroon)/90)]` then there is a bad closing `)`.

In this case the value parser is parsing `var(--color-maroon)/90)`, and
because of the `)` we will hit the "closing function" branch. This
branch currently assumes that an opening `(` was used.

I am actually not 100% sure what we should do here because I don't think
there is a good solution. We could throw an error with more info, but
since this kind of class could be returned from Oxide you may not have
control over this at all.
@RobinMalfait RobinMalfait requested a review from a team as a code owner May 23, 2025 19:44
@RobinMalfait RobinMalfait enabled auto-merge (squash) May 23, 2025 19:56
@RobinMalfait RobinMalfait merged commit 884f02c into main May 23, 2025
7 checks passed
@RobinMalfait RobinMalfait deleted the fix/issue-17064 branch May 23, 2025 19:59
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.

TypeError: Cannot read properties of undefined (reading 'nodes')
2 participants