# GHSA-3FX7-QVCV-CFFR: Minifying a JSX attribute string can turn an HTML entity into executable JSX/JavaScript syntax

- Database: GitHub
- Severity: CVSS 7.4 (High)
- Published: 2026-11-26
- Modified: 2026-11-26

## Affected versions

- @babel/generator (npm): fixed in 7.29.15
- @babel/generator (npm): fixed in 8.1.3

## Description

# Code injection in @babel/generator: minified output escapes JSX attribute strings with JavaScript backslash escapes, so a decoded `&quot;` closes the attribute

## Report metadata

| Field | Value |
| --- | --- |
| Package | `@babel/generator` (npm). PURL: **not verified**. The finding lists `pkg:npm/babel-generator`, but the affected code and the build we tested are `@babel/generator`. |
| Repository | https://github.com/babel/babel |
| Component | `StringLiteral` printer (`packages/babel-generator/src/generators/types.ts`), reached through the `JSXAttribute` printer (`packages/babel-generator/src/generators/jsx.ts`) |
| Vulnerability class | Code injection: a JSX attribute string breaks out of its context during code generation |
| CWE | [CWE-94](https://cwe.mitre.org/data/definitions/94.html) |
| Severity (reporter assessment) | High. Not CVSS-scored (see Impact). Needs the non-default `minified: true` option. |
| Verified against | Source read at commit `295a210350261c84eb0f523d441ffc3d75f3ec0b` (`@babel/generator` 8.1.2). PoC run against the published npm `@babel/generator@8.1.2`, whose `StringLiteral` generator matches that commit. `@babel/generator@7.29.14` showed the same injection. The exact introduced/fixed release range is **not established**. |
| API surface | Public documented API: `generate(ast, { minified: true })` |
| Affected in default configuration | No. `minified` defaults to `false`, and with `minified: false` the parsed raw value is reused and nothing is injected. (See the remediation notes about nodes with no raw value; not verified.) |

## Summary

When `minified` is enabled, the `StringLiteral` generator skips the original raw text and always prints `jsesc(node.value, this.format.jsescOption)`. It does this even when the literal is the value of a `JSXAttribute`. `jsesc` escapes a double quote as `\"`, but JSX attribute strings have no backslash escapes: the JSX tokenizer stops at the first matching quote. The backslash stays as data, and the quote ends the attribute early. Any text after that point is emitted as new JSX/JavaScript syntax.

An attacker who controls JSX attribute text can write `&quot;`. The JSX parser decodes it into a literal `"` in `node.value`, so the parsed AST shows one inert attribute string. After `generate(ast, { minified: true })`, the output holds an extra top-level statement. This matters to applications that parse or sanitize untrusted JSX as data, re-emit it minified, and then compile or evaluate the result.

- `StringLiteral` with `minified: false` and a raw value available -> raw source text reused, no injection (types.ts lines 184-188; confirmed dynamically)
- `StringLiteral` with `minified: true` as a `JSXAttribute` value -> **`jsesc` output emitted into JSX attribute context, types.ts lines 190-192**

## Impact

Take a consumer that treats parsed JSX attribute text as inert data, for example one that inspects the AST and allows only plain-text attribute values. If it then generates minified code and later compiles and runs it, attacker-chosen code runs with the privileges of whatever evaluates the output. During verification, the generated code was compiled with `@babel/preset-react` and run in `node:vm`, and it set `globalThis.pwned === 1`.

The attacker starts with bytes inside one JSX `StringLiteral` attribute value, which cannot introduce expressions or statements. After generation, they control a new top-level statement outside the JSX element. An AST check that allowed only text attributes never granted that. The documented purpose of `generate()` is to turn an AST into valid output, and this breaks AST round-trip semantics: re-parsing the output gives a different program.

Scoring: we did not assign a CVSS score. Exploitability depends on the consuming application's data flow: it must use `minified: true` on JSX that contains attacker-influenced attribute text, and it must later compile or evaluate the output. Based on verification, these consumers are **not** affected: those using the default `minified: false` on parsed JSX (raw value available), and those that never reparse, compile or execute the generated JSX. Consumers that attach no security meaning to attribute text staying inert are affected only as a correctness issue.

## Preconditions

1. The caller uses the public `generate` API with `minified: true`. This option is documented at `packages/babel-generator/src/index.ts` lines 172-175 and defaults to `false`.
2. The input AST has a `JSXAttribute` whose `StringLiteral` value contains `"`. For parsed input, an attacker can supply `&quot;`, which `jsxReadString()` decodes into a quote (`packages/babel-parser/src/plugins/jsx/index.ts` lines 180-203).
3. The attacker can control JSX attribute text, as source or as an AST, that the application treats as data.
4. The generated output is later reparsed, compiled or executed as JSX.

No non-standard usage, monkey-patching or unusual configuration is needed beyond turning on the documented `minified` option. The docs for `generate` (`index.ts` lines 231-236) say it turns an AST into code and keeps the output valid. The `minified` option (`index.ts` lines 172-175) carries no warning that JSX values may change syntax, and this is not documented as the caller's responsibility.

## Data flow

1. **Source**: the JSX tokenizer decodes entities in attribute strings. `jsxReadString()` converts `&...;` through `jsxReadEntity()`, has no backslash-escape branch, and stops only at the closing quote ([`packages/babel-parser/src/plugins/jsx/index.ts:180-203`](https://github.com/babel/babel/blob/295a210350261c84eb0f523d441ffc3d75f3ec0b/packages/babel-parser/src/plugins/jsx/index.ts#L180-L203)).
2. **Propagation**: `JSXAttribute` prints its `StringLiteral` value through the generic `this.print(node.value)`, with no JSX-specific handling ([`packages/babel-generator/src/generators/jsx.ts:4-9`](https://github.com/babel/babel/blob/295a210350261c84eb0f523d441ffc3d75f3ec0b/packages/babel-generator/src/generators/jsx.ts#L4-L9)).
3. **Sink**: in minified mode the raw-reuse branch is skipped (line 185), and the JavaScript-string encoding is emitted directly into JSX attribute context: `const val = jsesc(node.value, this.format.jsescOption); this.token(val);` ([`packages/babel-generator/src/generators/types.ts:190-192`](https://github.com/babel/babel/blob/295a210350261c84eb0f523d441ffc3d75f3ec0b/packages/babel-generator/src/generators/types.ts#L183-L192)).
4. **Reachability**: `generate` is the package's exported public entry point. Printer dispatch reaches `JSXAttribute` and then `StringLiteral` for parsed JSX string values ([`packages/babel-generator/src/index.ts:238-256`](https://github.com/babel/babel/blob/295a210350261c84eb0f523d441ffc3d75f3ec0b/packages/babel-generator/src/index.ts#L238-L256)).

## Vulnerable code

`packages/babel-generator/src/generators/types.ts`, lines 183-192 at `295a210350261c84eb0f523d441ffc3d75f3ec0b` (closing brace included):

```ts
export function StringLiteral(this: Printer, node: t.StringLiteral) {
  const raw = this.getPossibleRaw(node);
  if (!this.format.minified && raw !== undefined) { // raw reuse, disabled when minified
    this.token(raw); // safe path for parsed JSX attributes
    return;
  }

  const val = jsesc(node.value, this.format.jsescOption); // JavaScript-string encoder, no JSXAttribute check

  this.token(val); // SINK: emitted verbatim into JSX attribute context
}
```

## Reproduction

Confirmed dynamically against the published npm `@babel/generator@8.1.2`. Its `StringLiteral` generator matches commit `295a210350261c84eb0f523d441ffc3d75f3ec0b`. `@babel/generator@7.29.14` showed the same injection. The Node.js version used was not recorded.

What we observed:

1. Input: `const el = <div title="&quot;/>;globalThis.pwned=1;//" />;`. The parser decodes the entity into a quote in the attribute value. Parsing the original gives only a `VariableDeclaration`.
2. `generate(ast, { minified: true })` emits: `const el=<div title="\"/>;globalThis.pwned=1;//"/>;`
3. Re-parsing that output gives `VariableDeclaration` + `ExpressionStatement` (the injected assignment). The trailing `"/>;` is swallowed by the injected `//` line comment.
4. After compiling the output with `@babel/preset-react` (classic runtime) and running it in `node:vm`, `globalThis.pwned === 1`.
5. With `minified: false`, the raw value is reused and nothing is injected (`pwned` stays undefined).

The script below puts those steps in one file. The `React` stub is something this script adds so the compiled `React.createElement` call can run. We did not record how verification provided `React`.

```js
import { parse } from "@babel/parser";
import generate from "@babel/generator";
import { transformSync } from "@babel/core";
import vm from "node:vm";

const input = 'const el = <div title="&quot;/>;globalThis.pwned=1;//" />;';
const ast = parse(input, { plugins: ["jsx"] });

const out = generate(ast, { minified: true }).code;
console.log(out);
// const el=<div title="\"/>;globalThis.pwned=1;//"/>;

console.log(parse(out, { plugins: ["jsx"] }).program.body.map((n) => n.type));
// [ 'VariableDeclaration', 'ExpressionStatement' ]

const compiled = transformSync(out, {
  presets: [["@babel/preset-react", { runtime: "classic" }]],
}).code;
const ctx = { React: { createElement: () => null } };
vm.runInNewContext(compiled, ctx);
console.log(ctx.pwned); // 1
```

- **Expected:** the generated output keeps the single inert JSX attribute value, for example by keeping `&quot;` or using another JSX entity. Re-parsing adds no statement, and nothing executes.
- **Actual:** the output closes the attribute at the encoded quote. Re-parsing gives an extra `ExpressionStatement`, and compiling and running the output sets `globalThis.pwned` to `1`.

## Suggested remediation

When a `StringLiteral` is the value of a `JSXAttribute`, emit it with JSX attribute quoting instead of JavaScript string escaping. Reuse a valid raw JSX value when there is one. Otherwise, encode quote characters as JSX entities instead of backslash escapes, and encode ampersands that could start an entity.

The sketch below is untested. It is written against the role of the `JSXAttribute` printer at `jsx.ts` lines 4-9, not its exact current code, so please adapt it to the existing printer idioms (comment and source-location handling in particular):

```ts
// packages/babel-generator/src/generators/jsx.ts (sketch, untested)
function jsxAttributeStringToken(value: string): string {
  // JSX attribute strings have no backslash escapes; use entities instead.
  return '"' + value.replace(/&/g, "&amp;").replace(/"/g, "&quot;") + '"';
}

export function JSXAttribute(this: Printer, node: t.JSXAttribute) {
  this.print(node.name);
  if (node.value) {
    this.token("=");
    if (node.value.type === "StringLiteral") {
      const raw = this.getPossibleRaw(node.value);
      this.token(raw !== undefined ? raw : jsxAttributeStringToken(node.value.value));
    } else {
      this.print(node.value);
    }
  }
}
```

Another option is to keep the dispatch in `JSXAttribute` unchanged and have `StringLiteral` in `types.ts` branch on a `JSXAttribute` parent before it falls back to `jsesc`.

Notes:

- Reusing the raw value in minified mode also keeps the original entity spelling, so minified output matches non-minified output for parsed JSX.
- Depending on `jsescOption`, `jsesc` may emit other backslash escapes, such as `\n` or `\uXXXX`. JSX attribute strings do not process backslash escapes, so these would also change the attribute value in JSX context. This is **not verified**, but a JSX-specific encoder would cover it.
- From reading the snippet, the `jsesc` path is also taken when `raw` is `undefined`, for example `StringLiteral` nodes built or rewritten by transforms with no raw text. That would mean `minified: false` is affected for such nodes too. This is **not verified**. The fix above handles both cases.
- Other JSX positions that might print a `StringLiteral` through the same sink were not reviewed.
- Already safe, no change needed: non-minified generation of parsed JSX where the raw value is available. We confirmed this dynamically.

Suggested regression test alongside the generator's existing JSX tests (we did not identify the exact file): parse `<a b="&quot;/>;globalThis.pwned=1;//" />;` with the `jsx` plugin and generate with `{ minified: true }`. Then assert three things: the output contains no `\"` inside the attribute; re-parsing gives exactly one `ExpressionStatement`; and the re-parsed attribute value equals `"/>;globalThis.pwned=1;//`. A second case could do the same with a programmatically built `t.stringLiteral('"')` attribute value that has no raw text.

## References

- https://github.com/babel/babel/security/advisories/GHSA-3fx7-qvcv-cffr

Page: https://cve.now/GHSA-3FX7-QVCV-CFFR

Wrong or missing data? Send feedback: POST https://cve.now/api/v1/feedback (see https://cve.now/agents#feedback).
