Skip to content

css: keep required grouping parens in @container conditions when minifying - #31330

Merged
Jarred-Sumner merged 1 commit into
mainfrom
farm/19cafd66/css-container-needs-parens
May 24, 2026
Merged

Jarred-Sumner merged 1 commit into
mainfrom
farm/19cafd66/css-container-needs-parens

Conversation

@robobun

@robobun robobun commented May 24, 2026

Copy link
Copy Markdown
Collaborator

What does this PR do?

Fixes a CSS fuzzer invariant violation (invariant:css:minified CSS does not reparse): @container conditions with a nested and/or group lose the grouping parentheses the grammar requires, so Bun's own CSS parser can't read its minified output back.

Repro:

BUN_FEATURE_FLAG_INTERNAL_FOR_TESTING=1 bun -e 'const c = require("bun:internal-for-testing").cssInternals;
const min = c.minifyTest("@container style(not ((w:)and (r:))){{c:", "");
console.log(min); // @container style(not (w:) and (r:)){{c:}}
c.minifyTest(min, "");'
# error: parsing failed: Unexpected token: and

not only takes a single parenthesized query, so not ((a) and (b)) must keep the group — the re-printed not (a) and (b) is a parse error. The same happens for realistic inputs, in both size and style container queries, and also when mixing operators:

input minified (before this PR) reparse
@container not ((width > 100px) and (height > 100px)) @container not (width>100px) and (height>100px) ❌ Unexpected token: and
@container style(not ((width: 1px) and (height: 2px))) @container style(not (width:1px) and (height:2px)) ❌
@container ((width > 100px) or (height > 100px)) and (orientation: landscape) @container (width>100px) or (height>100px) and (orientation:landscape) ❌ (can't mix and/or without parens)

Any such stylesheet breaks when Bun-minified CSS is fed back through bun build / CSS imports, and @media handles all of these correctly — only @container is affected.

Root cause

needs_parens for ContainerCondition and StyleQuery in src/css/rules/container.rs has the operator comparison inverted:

Operation { operator, .. } => Some(*operator) == parent_operator,

It adds parentheses when a nested group has the same operator as its parent (where they're redundant) and drops them when the operator differs or the parent is not (parent_operator == None) — exactly the positions where the grammar requires <query-in-parens> / <style-in-parens>. MediaCondition::needs_parens in media_query.rs already uses !=, which is why the equivalent @media queries roundtrip fine.

The fix

Flip the comparison to != in both StyleQuery::needs_parens and ContainerCondition::needs_parens, matching MediaCondition and current lightningcss. Nested groups now keep their parens under not and under a parent with a different operator, and same-operator nesting flattens (((a) and (b)) and (c) → (a) and (b) and (c)), which is smaller and still equivalent.

All the outputs above now match lightningcss 1.32.0 byte-for-byte, and the fuzzer input roundtrips: @container style(not ((w:)and (r:))){{c: → @container style(not ((w:) and (r:))){{c:}} → reparses to itself.

Verification

  • New describe("container") block in test/js/bun/css/css.test.ts: minification of not/mixed-operator/style() groups plus reparsing of the minified forms. Without the fix 12 of the 16 tests fail; with it all pass.
  • test/js/bun/css/css.test.ts + doesnt_crash.test.ts + color.test.ts on the debug build: no new failures (the only failures are pre-existing timeouts of iteration-heavy fuzz/ANSI-color tests that also time out on unmodified main in the same environment and don't involve @container).

@robobun

robobun commented May 24, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 4:29 AM PT - May 24th, 2026

✅ @robobun, your commit 1f051aaf12f61fa3b1fb3b1c792612e86e90efea passed in Build #57526! 🎉


🧪   To try this PR locally:

bunx bun-pr 31330

That installs a local version of the PR into your bun-31330 executable, so you can run:

bun-31330 --bun

@coderabbitai

coderabbitai Bot commented May 24, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

@robobun, we couldn't start this review because you've used your available PR reviews for now.

Your plan currently allows 1 review/hour. Refill in 35 minutes and 19 seconds.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more review capacity refills, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than trial, open-source, and free plans. In all cases, review capacity refills continuously over time.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: d8486601-0c66-451c-9b80-030c0a42f431

📥 Commits

Reviewing files that changed from the base of the PR and between 49c97de and 1f051aa.

📒 Files selected for processing (2)
  • src/css/rules/container.rs
  • test/js/bun/css/css.test.ts

Comment @coderabbitai help to get the list of available commands and usage tips.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — two-character fix flipping an inverted operator comparison to match MediaCondition::needs_parens, with thorough test coverage.

Extended reasoning...

Overview

This PR flips == to != in two needs_parens implementations (StyleQuery and ContainerCondition) in src/css/rules/container.rs. The previous comparison was inverted: it added grouping parentheses when a nested @container operation had the same operator as its parent (redundant) and dropped them when the operator differed or the parent was not (required by the CSS grammar). The result was minified output that Bun's own parser rejected. A new describe("container") block in test/js/bun/css/css.test.ts adds 16 minification/roundtrip cases covering not, mixed-operator, and style() queries.

Security risks

None. This is pure CSS serialization logic — no I/O, auth, or untrusted-input handling beyond what the CSS parser already does.

Level of scrutiny

Low. The fix is a minimal, mechanical correction that brings these two impls into exact alignment with MediaCondition::needs_parens (media_query.rs:881, which already uses !=) and with SupportsCondition::needs_parens (which follows the same "parens when parent differs" pattern). I verified the reference implementation in the codebase matches the PR description's claim. The grammar reasoning is correct: not takes a single <query-in-parens>, and and/or cannot be mixed at the same level without grouping, so Some(op) != parent_operator (which is true both under not where parent_operator == None and under a different operator) is the right predicate.

Other factors

The bug-hunting system found no issues. The added tests are comprehensive — they cover both the negative cases (parens preserved under not and mixed operators) and the positive flattening case (same-operator nesting drops redundant parens), plus idempotence checks that the minified forms reparse to themselves. The PR description notes outputs now match lightningcss 1.32.0 byte-for-byte.

@Jarred-Sumner
Jarred-Sumner merged commit dd0883d into main May 24, 2026
79 of 80 checks passed
@Jarred-Sumner
Jarred-Sumner deleted the farm/19cafd66/css-container-needs-parens branch May 24, 2026 20:47
springmin pushed a commit to springmin/bun that referenced this pull request May 25, 2026
* oven/main (20 new commits):
  webcore: free Blob's owned content type on drop (oven-sh#31358)
  Support cross-compiling macOS binaries from Linux (oven-sh#31303)
  test: forward keep-alive requests in proxy.test.ts's mock proxy (oven-sh#31352)
  Port Bun.stringWidth to C++ with explicit SIMD (oven-sh#31351)
  Fix quadratic hang reporting duplicate-binding parse errors in the transpiler (oven-sh#31341)
  shell: don't abort when a glob's directory prefix doesn't exist (oven-sh#31367)
  Error instead of crashing on deeply nested statements in the transpiler (oven-sh#31333)
  Fix JSX transform panic when a bare `key` prop precedes `key` with a value (oven-sh#31350)
  Cap ANSI markdown indentation so deeply nested lists render in linear time (oven-sh#31366)
  css: bound selector-list expansion when compiling nesting for older targets (oven-sh#31277)
  node:http2: reassemble HEADERS+CONTINUATION before HPACK decoding (oven-sh#31323)
  Fix `await using` expression printing `using` as `await` (oven-sh#31324)
  Parenthesize `async` when it starts a for-of loop initializer (oven-sh#31326)
  Print Infinity and negative numeric property keys as computed properties (oven-sh#31328)
  css: keep required grouping parens in @container conditions when minifying (oven-sh#31330)
  Fix panic on anonymous export default class with an auto-accessor field (oven-sh#31331)
  node:http2: send GOAWAY frames on stream 0 (oven-sh#31353)
  parser: fix Scope mismatch while visiting panic from decorators on dropped class members (oven-sh#31340)
  webcrypto: reject oversized BufferSource inputs instead of aborting (oven-sh#31356)
  Error instead of crashing on deeply nested TypeScript types in the transpiler (oven-sh#31361)

Resolved conflicts:
  - scripts/build.ts: kept both OHOS and macOS-cross argv entries
  - scripts/build/config.ts: kept both OHOS and macOS-cross config fields
  - scripts/build/deps/webkit.ts: kept OHOS fno-pic exclusion, adopted upstream -flto=thin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants