
PEACH
Un cadre d’isolation des locataires
escapeMDX() for every preceding special character in a schema description| Field | Value |
|---|---|
| Package | @graphql-markdown/utils |
| Repository | https://github.com/graphql-markdown/graphql-markdown |
| Component | escapeMDX in packages/utils/src/string.ts, reached through generateDocFromSchema |
| Vulnerability class | Injection: stored XSS and MDX/JSX expression injection through incomplete output escaping |
| CWE | CWE-79 |
| Severity (reporter assessment) | High |
| Verified against | Commit f4da8ad30fce5c9e001d082f0580a15968b6fd70 (1.42.0-era main). No other commits or released versions were checked, so the affected version range is not verified. |
| API surface | Public documented API: buildConfig(...) + generateDocFromSchema(config), and the gqlmd CLI (graphql-to-doc) |
| Affected in default configuration | Yes. No non-standard option is needed: the stock Markdown output already contains the raw HTML. Choosing the documented MDX output additionally exposes MDX expression and JSX interpretation. Whether HTML executes depends on the downstream renderer allowing raw HTML or compiling the page as MDX. |
| No CVE or GHSA has been assigned for this issue as far as we know. |
escapeMDX() decides whether a special character ({ < _ * > }) sits inside inline code by checking that the entire remaining string contains an even number of backticks. If a schema description puts raw HTML or MDX before a single unmatched backtick, the lookahead fails for every special character before it. That content then goes into the generated Markdown/MDX verbatim instead of being encoded as &#x....; entities. A lone backtick does not open a code span, so the content it "protects" is not in code at all.
In a consuming app, the tainted input is any GraphQL type description in the schema being documented. The project documents remote introspection/SDL, Git and GitHub schema sources (docs/get-started.md:90-103), so the schema author is often not the docs site operator.
The same function handles nearly identical inputs safely, which shows the unsafe case is a bug and not intended behaviour (all three cases go through string.ts:178-182):
<b>)string.ts:155-175.If an attacker controls a schema description, they can inject raw HTML into the generated documentation pages. That is stored XSS in readers' browsers whenever the renderer allows HTML. When the page is compiled as MDX (as Docusaurus does), the attacker can also inject JSX and arbitrary JavaScript expressions. During verification, such an expression ran when the page was rendered server-side, so the injected code runs in the docs site's MDX build/SSR environment as well as in readers' browsers.
Here is what the bug changes. Normally <, >, { and } in descriptions become HTML entities and show up as inert text. With the bypass, the attacker moves from schema text into markup and expressions that the documentation renderer executes.
Scoring note: we did not compute a CVSS vector. Exploitability depends on the consumer's data flow (where the schema comes from) and on how the generated pages are rendered. Not affected: consumers that only generate docs from fully trusted schemas. The XSS part also does not affect consumers whose renderer neither allows raw HTML nor compiles MDX.
generateDocFromSchema (normally after buildConfig) or the gqlmd CLI, from a schema whose description text is not fully trusted. The project documents remote URL/introspection, Git and GitHub schema sources (docs/get-started.md:90-103).<img src=x onerror=alert(1)> `` ``.packages/core/README.md:20-29 documents direct generation without any warning that schema descriptions must be trusted. The behaviour is not documented as the caller's responsibility.schemaLocation, which may be a remote, Git or GitHub source (packages/core/src/generator.ts:247-262).Printer.printType (packages/printer-legacy/src/printer.ts:597-632, call at line 604). type.description is then passed to escapeMDX via formatDescription(type) (packages/printer-legacy/src/common.ts:34-45).`${String(str)}`.replaceAll(/(?<!`)([{<_*>}])(?=(?:[^`]*`[^`]*`)*[^`]*$)/g, toHTMLUnicode) (packages/utils/src/string.ts:178-182). No later stage escapes it: Printer.printType stores the formatted description section and the renderer writes it out unchanged.generateDocFromSchema entry point (packages/core/src/generator.ts:537-541).packages/utils/src/string.ts, lines 178-182 at f4da8ad30fce5c9e001d082f0580a15968b6fd70:
export const escapeMDX = (str: unknown): string => {
return `${String(str)}`.replaceAll(
/(?<!`)([{<_*>}])(?=(?:[^`]*`[^`]*`)*[^`]*$)/g, // SINK: lookahead counts backtick parity over the whole remaining string
toHTMLUnicode,
);
};Verified dynamically at commit f4da8ad30fce5c9e001d082f0580a15968b6fd70. The Node.js version and exact CLI flags were not recorded.
Unit check from source. The input below comes back from escapeMDX unchanged. The same text without the trailing backtick is fully encoded.
escapeMDX('<img src=x onError={alert(1)} /> {globalThis.pwned = 1} trailing `');
// returned unchanged: no characters encodedEnd to end. We ran the built gqlmd CLI (graphql-to-doc) on a schema.graphql containing an object type with the description below, plus a control type with the same kind of content but no trailing backtick. The type name and other schema contents shown here are illustrative.
"""
Benign text <img src="x" onError={() => 0} /> {globalThis.__PWNED__ = "expr-executed-at-render"} trailing `
"""
type Evil {
ping: String
}Observed:
docs/api/types/objects/evil.md. The control type was emitted as entities (<b>...).@mdx-js/mdx v3 and react-dom/server (Docusaurus' MDX format). The output contained a real <img> element, and the JavaScript expression ran: globalThis.__PWNED__ was set. The control page left it undefined.
The equivalent programmatic entry point is below. It reaches the same sink, but this exact snippet was not run separately:import { writeFileSync } from "node:fs";
import { buildConfig, generateDocFromSchema } from "@graphql-markdown/core";
writeFileSync("/tmp/schema.graphql", '"""<img src=x onerror=alert(document.domain)> `"""' + String.fromCharCode(10) + "type Query { ping: String }" + String.fromCharCode(10));
const config = await buildConfig({ schema: "/tmp/schema.graphql", rootPath: "/tmp/docs", baseURL: "api" });
await generateDocFromSchema(config);<img ...> (inert text), and MDX braces are encoded.<img ...> and {...} expressions. Rendered as MDX, the image element is created and the expression runs.Swap the parity-based lookahead for a Markdown-aware routine that only preserves special characters inside a confirmed, balanced code span. An unmatched backtick must never turn escaping off. Below is an untested sketch in the project's existing style. It follows the CommonMark rule that a code span closes only on a backtick run of the same length:
const MDX_SPECIAL_CHARS = /[{<_*>}]/g;
const escapeOutsideCode = (text: string): string =>
text.replaceAll(MDX_SPECIAL_CHARS, toHTMLUnicode);
export const escapeMDX = (str: unknown): string => {
const input = String(str);
let result = "";
let index = 0;
while (index < input.length) {
const opener = /`+/g;
opener.lastIndex = index;
const open = opener.exec(input);
if (!open) break;
const fence = open[0];
const closer = new RegExp("(?<!`)" + fence + "(?!`)", "g");
closer.lastIndex = open.index + fence.length;
const close = closer.exec(input);
if (!close) {
// unmatched backtick run: literal text, keep escaping what precedes and follows it
result += escapeOutsideCode(input.slice(index, open.index + fence.length));
index = open.index + fence.length;
continue;
}
result += escapeOutsideCode(input.slice(index, open.index));
result += input.slice(open.index, close.index + fence.length); // balanced code span: preserved
index = close.index + fence.length;
}
return result + escapeOutsideCode(input.slice(index));
};Notes:
(?<!`` ``)` lookbehind also skips any special character that directly follows a backtick, even outside a code span. The sketch drops that behaviour. Check whether any existing test depends on it.escapeMDX go through the same sink and benefit from the same fix: printer-legacy/src/common.ts:39,45,87, printer-legacy/src/badge.ts:102 and printer-legacy/src/decorator.ts:273. We traced only the description path end to end, so whether the others can be exploited is not verified.packages/utils/src/string.ts (test file name not verified):escapeMDX('<img src=x onerror=alert(1)> `` ``')encodes<and>as</>`.escapeMDX('{globalThis.pwned = 1} trailing `` ``')encodes{and}`.`<b>` is still preserved unescaped.Fixed in @graphql-markdown/utils 1.14.3, which @graphql-markdown/core 1.27.0 and @graphql-markdown/docusaurus 1.39.3 pull in. Upgrade, or refresh your lockfile so that @graphql-markdown/utils resolves to 1.14.3 or later.
escapeMDX() no longer decides by counting backticks. It now scans the text and leaves MDX special characters unescaped only inside code that Markdown will definitely render as code:
The new check is deliberately strict. Some legitimate code is now escaped too:
|
In those cases, {, <, >, _ and * inside the code show up as HTML entities (for example {). This only affects how the page looks, not its security. To avoid it, start fenced code blocks at column 0 after a line of text, and keep inline code on one line.If you cannot upgrade yet:
Source: NVD
Évaluation gratuite des vulnérabilités
Évaluez vos pratiques de sécurité cloud dans 9 domaines de sécurité afin d’évaluer votre niveau de risque et d’identifier les lacunes dans vos défenses.
Obtenez une démo personnalisée
"La meilleure expérience utilisateur que j’ai jamais vue, offre une visibilité totale sur les workloads cloud."
"Wiz fournit une interface unique pour voir ce qui se passe dans nos environnements cloud."
"Nous savons que si Wiz identifie quelque chose comme critique, c’est qu’il l’est réellement."