stylex icon indicating copy to clipboard operation
stylex copied to clipboard

Numeric normalization rewrites unquoted url() resource names and breaks image loading

Open poplolioi11 opened this issue 1 month ago • 3 comments

Describe the issue

The Babel plugin applies numeric CSS normalization inside unquoted url() resource names. Valid relative resource names are silently rewritten to different URLs:

Input Emitted by 0.19.1 Browser effect
url(100ms) url(.1s) requests .1s instead of 100ms
url(0rad) url(0deg) requests 0deg instead of 0rad
url(0.5.png) url(.5.png) requests .5.png instead of 0.5.png

In a real Chromium browser with matching local image fixtures, original CSS loads the images, the released compiler output requests nonexistent paths (404), and the attached patched compiler output loads the original images again. This affects uncommon but valid unquoted resource names; ordinary url(image.png) and quoted URL controls load correctly.

Expected behavior

Preserve the resource name within url(). Numeric-looking URL contents identify a resource and must not be treated as CSS time, angle, or number values.

Normal numeric optimization outside URLs should continue: 100ms → .1s, rotate(0rad) → rotate(0deg), and 0.5 → .5.

Quoting the URL, e.g. url("100ms"), is a workaround on the released version.

Steps to reproduce

Environment: macOS arm64, @stylexjs/[email protected], @babel/[email protected], [email protected]. The release reproduction runs on Node 22.17.0 and 24.19.0; source tests/build used Node 24.19.0 with Yarn 1.22.22. Native browser: Chromium 155.

  1. Install @stylexjs/[email protected] and @babel/[email protected] in a clean directory.
  2. Run:
const {transformSync} = require('@babel/core');
const imported = require('@stylexjs/babel-plugin');
const plugin = imported.default || imported;
for (const resource of ['100ms', '0rad', '0.5.png']) {
  const source = `import * as stylex from '@stylexjs/stylex';
    const styles = stylex.create({x: {backgroundImage: 'url(${resource})'}});
    stylex.props(styles.x);`;
  const result = transformSync(source, {
    filename: 'repro.js', configFile: false, babelrc: false,
    plugins: [[plugin, {runtimeInjection: true}]],
  });
  console.log(result.metadata.stylex.map(rule => rule[1].ltr));
}
  1. Observe the changed resource names shown above. Serve matching image resources and apply the generated CSS to reproduce broken image loading.

The same failures were reproduced with the unmodified normalizers from main commit 14b6600be1701e5926c5f4ea33b89468eda6d8c7 in new source-level and full-compiler regression tests.

Test case

The attached package contains a locked standalone reproduction, local browser page and image fixtures, exact compiler metadata, the proposed source patch including tests and its new helper, and validation logs. It contains no installed dependencies.

  • Compiler: npm ci --ignore-scripts --no-audit --no-fund, then node scripts/repro.cjs (intentionally exits 1 on 0.19.1).
  • Browser: python3 scripts/serve-browser.py, then open its printed loopback URL.
  • Browser results: all 15 original inputs and all 15 patched outputs decoded 80×80 images; 12 released outputs failed, while 3 released controls loaded.

Attachments:

stylex-url-reproduction-and-fix.zip Image

Additional comments

Proposed fix: skip URL-function descendants in normalizers/timings.js, zero-dimensions.js, and leading-zero.js. A shared predicate recognizes case variants and CSS-escaped names represented as a single function node. URL names and payloads are not rewritten.

Validation on the base commit:

  • Unmodified normalizers + new focused tests: 18 failed, 33 passed (19 existing snapshots passed).
  • Patched complete Babel plugin suite: 42 suites passed, 1 skipped; 1,031 tests passed, 64 skipped; 887 snapshots passed; existing coverage thresholds passed.
  • Official source dependency builds and Babel plugin build succeeded. The browser page uses CSS emitted by this built compiler.
  • Flow full-check --max-workers 2: 0 errors. ESLint and Prettier passed for all six changed files.
  • The complete patch applies cleanly to the base commit. The whole monorepo yarn test was not run.

Scope limitation: this is a focused normalization fix, not a replacement CSS parser. The existing parser splits hex-escaped function names with terminating whitespace, e.g. \75 rl(...), into separate nodes. This patch does not claim to repair or validate that grammar case.

Public duplicate searches (Issues and all-state PRs, checked 2026-10-24) covered unquoted URLs, URL normalization, 100ms, 0rad, and 0.5.png; no same report/fix was found. Related PRs #1435 (custom-property zero units) and #1828 (content quoting) address different cases. Private or differently worded reports cannot be excluded.

This report and patch were AI-assisted, followed by executable regression checks, an independent source review, and native browser verification. Submitted for maintainer review.

poplolioi11 avatar Oct 24 '26 01:10 poplolioi11

Thanks @poplolioi11. I had an agent put together a PR in https://github.com/facebook/stylex/pull/1983

I'm curious how you ran into this bug? The examples in your issue seem pretty unrealistic outside of maybe the 0.5.png one. Thanks again.

henryqdineen avatar Oct 24 '26 03:10 henryqdineen

I'd like to fix this.

Numeric normalization should not rewrite the contents of unquoted url() resource names — those identify files, not CSS time/angle values. I'll scope the normalizer to skip url() arguments, add regression cases (100ms, 0rad, 0.5.png), and open a PR for #1982.

AbbasFullstack avatar Oct 24 '26 03:10 AbbasFullstack

Thanks @henryqdineen for looking into this and putting together #1983.

To answer your question, this came from AI-assisted source inspection, followed by focused local regression cases and verification in a real Chromium browser.

I agree that 100ms and 0rad are unusual resource names. They were boundary cases to check whether URL contents were being treated as CSS numeric values. For the 0.5.png case, version 0.19.1 rewrites unquoted url(0.5.png) to url(.5.png), so the browser requests a different file. Quoted URLs worked in the controls.

With matching local image fixtures, the original CSS loaded the images, the 0.19.1 output requested missing paths, and output from our locally patched compiler (included in the issue attachments) loaded the original images again. The evidence is limited to these local reproductions; I have not observed this on a production site.

@AbbasFullstack, thank you for offering to help as well. Henry has already opened a proposed fix in #1983.

Thanks again for reviewing the report.

poplolioi11 avatar Oct 24 '26 03:10 poplolioi11