
Cloud Vulnerability DB
A community-led vulnerabilities database
@graphql-markdown/printer-legacy: GraphQL String argument defaults are inserted unescaped and can terminate the generated code fence| Field | Value |
|---|---|
| Package | @graphql-markdown/printer-legacy (npm, pkg:npm/%40graphql-markdown/printer-legacy) |
| Repository | https://github.com/graphql-markdown/graphql-markdown |
| Component | printCodeArguments (packages/printer-legacy/src/code.ts), reached from Printer.printCode / Printer.printType |
| Vulnerability class | Markdown/MDX code-fence breakout leading to stored cross-site scripting in generated documentation |
| CWE | CWE-79 |
| Severity (reporter assessment) | High |
| Verified against | Source at 5e2234e8f7c983f90b3b75a783c04187b11df7b7; published artifacts 1.17.0 (latest release) and 1.12.0. Reported affected range: <= 1.17.0 |
| API surface | Public documented API (Printer.printType(name, type) / Printer.printCode(type, options)); printCodeArguments is also exported by the package subpath |
| Affected in default configuration | Yes — default Printer code rendering is enough; the fence is the constant three-backtick MARKDOWN_SOC/MARKDOWN_EOC pair used by Printer.printCode |
| Notes on the claims above: | |
| - No CVE or GHSA identifier has been assigned, and no upstream advisory, fix commit or patched version is known to us. | |
- The <= 1.17.0 range is the reported affected range; the behaviour was observed on 1.17.0 and on 1.12.0. Intermediate releases were not individually checked. |
printCodeArguments builds each argument line of the generated SDL by concatenating the formatted default value directly into the line: printedDefault = \ = ${String(defaultValue)}`. The imported getFormattedDefaultValueonly wrapsGraphQLString/GraphQLIDvalues in quotes and does not escape newlines or backticks, andPrinter.printCodethen wraps the whole block in a fixed three-backtick fence. A GraphQL String default whose escape sequences decode to\n\n…` therefore emits a bare ` at column 0 inside the generated block, closing the fence early so that everything after it is rendered as page content rather than as code. The tainted input is ordinary schema data: field and directive argument default values. In a realistic consuming application the schema is not always first-party — remote/introspected schemas are a documented input source (docs/advanced/schema-loading.md), and the @graphql-markdown/coremain generator reachesPrinter.printType` on that schema. Anyone whose docs pipeline consumes a third-party or otherwise untrusted schema publishes attacker-chosen MDX/HTML/JSX on their documentation site.
Sibling paths in the same package handle similar schema-derived strings safely, which is the strongest evidence that this is a missing-escaping bug rather than intended output:
escapeMDX before emission (exact line not verified)escapeMDX before emission (exact line not verified)packages/printer-legacy/src/code.ts:57-65printSchema -> escaped to a single-line "\n```\n…" literalA malicious or compromised schema turns an argument default value into active content in the generated documentation: the injected markup lands outside every <pre><code> region of the rendered page, so a <img src="x" onerror="alert(document.domain)" /> payload executes script in the documentation site's origin for every visitor of that page. That gives the attacker script execution with the privileges of the documentation origin — session/cookie access for that origin, and the ability to rewrite what the page shows to readers.
The new capability is narrow but real: without this bug, an attacker who controls schema text can only cause inert text to be displayed inside a GraphQL code example. With it, that same text escapes the code example and becomes active page content. Printer.printCode's own documentation promises a GraphQL type definition code block; a fixed-length fence that schema data can terminate breaks that contract.
Scoring note: we did not compute a CVSS vector. Exploitability depends on the consuming application's data flow — specifically whether the schema is trusted and how the generated Markdown/MDX is rendered — and a single vector would misrepresent that. Consumers who only generate docs from first-party schemas they fully control are not exposed. Script execution was observed with renderers that accept raw HTML/JSX (CommonMark with allowDangerousHtml, and the MDX compiler used by the Docusaurus pipeline); whether a renderer configured to reject raw HTML fully mitigates the issue was not verified — the fence breakout itself still injects attacker-chosen Markdown (e.g. **bold** rendered as <strong>) outside the code block.
docs/advanced/schema-loading.md documents as a supported input source.GraphQLString/GraphQLID) whose value decodes to a newline followed by a run of three backticks. The GraphQL parser decodes \n escape sequences before getFormattedDefaultValue sees the value, and that formatter re-emits the runtime string without re-escaping (packages/graphql/src/formatter.ts:43-53, quoting only, at lines 48-50).Printer code rendering is affected, and the emitted fence is a constant three-backtick delimiter (packages/printer-legacy/src/printer.ts:372, packages/utils/src/markdown.ts:20). The documentation that describes remote-schema loading does not state that schema contents must be trusted, and the behaviour is not documented as a caller responsibility.const defaultValue = getFormattedDefaultValue(v as never); (packages/printer-legacy/src/code.ts:57-61). For String-like types the formatter adds surrounding quotes only, with no escaping of newlines or backticks (packages/graphql/src/formatter.ts:43-53).packages/printer-legacy/src/code.ts:61-65).Printer.printCode wraps the generated code with fixed three-backtick delimiters: MARKDOWN_SOC + code.trim() + MARKDOWN_EOC (packages/printer-legacy/src/printer.ts:337-372). Because the fence length is constant, a backtick run inside the code terminates it.printCodeArguments is called from printCodeField (packages/printer-legacy/src/code.ts:102), used by the object/operation/directive code printers dispatched from the public Printer.printCode and Printer.printType, which place the code section verbatim in the final MDX (packages/printer-legacy/src/printer.ts:602,620,650-658). The @graphql-markdown/core main generator reaches Printer.printType.packages/printer-legacy/src/code.ts, lines 41-68 at 5e2234e8f7c983f90b3b75a783c04187b11df7b7 (excerpt as reported, with the intervening lines elided):
const defaultValue = getFormattedDefaultValue(v as never);
let printedDefault = "";
if (defaultValue !== undefined && defaultValue !== null) {
printedDefault = ` = ${String(defaultValue)}`; // sink: schema data concatenated unescaped
}
...
return `${argIndentation}${propName}: ${propType}${printedDefault}`;The returned line is later placed between the constant MARKDOWN_SOC / MARKDOWN_EOC three-backtick delimiters in Printer.printCode (packages/printer-legacy/src/printer.ts:372, constants at packages/utils/src/markdown.ts:20). Descriptions and deprecation reasons printed by the same package do pass through escapeMDX and need no change.
Verified end-to-end against the published artifact @graphql-markdown/printer-legacy@1.17.0, installed from npm into a clean project with graphql@16. Four node:test assertions pass: the attack schema is valid (buildSchema + assertValidSchema), the injected markup appears at an offset outside every <pre><code> region, an MDX element is emitted rather than code text, and a control argument with a benign default stays inside <pre><code>. The same breakout reproduces via list ([String]) defaults and input-object field defaults, and on release 1.12.0.
import { buildSchema, assertValidSchema } from 'graphql';
import { Printer } from '@graphql-markdown/printer-legacy';
// The GraphQL parser decodes the \n escapes into real newlines before the
// printer's formatter sees the value.
const schema = buildSchema(
'type Query { attack(msg: String = "\\n```\\n\\n<img src=\\"x\\" onerror=\\"alert(document.domain)\\" />\\n\\nINJECTED **bold**\\n\\n```\\n") : String }'
);
assertValidSchema(schema);
await Printer.init(schema);
const mdx = await Printer.printType('Query', schema.getType('Query'));
console.log(mdx);
// Then render `mdx`:
// - CommonMark (micromark, allowDangerousHtml) -> live <img ... onerror=...> outside every <pre><code>
// - @mdx-js/mdx (the Docusaurus pipeline) -> _jsx("img", ...), i.e. a real element``` appears at column 0 in the printer's output and terminates the graphql fence early. The attacker's <img src="x" onerror="alert(document.domain)" /> renders as a live element outside every <pre><code> region, and INJECTED **bold** renders as <strong>bold</strong>. A control argument with a benign default remains inside <pre><code>.Escape schema-derived default values so they can never emit a raw newline or backtick run, and make the surrounding fence longer than any backtick run in the generated code.
// packages/graphql/src/formatter.ts — quote AND escape String-like defaults
const _formatDefaultValue = (type: unknown, defaultValue: unknown): string => {
if (isStringLikeType(type)) {
// reuse graphql-js string-literal printing, which escapes \n, " and control chars
return print(astFromValue(defaultValue, type as GraphQLInputType)!);
}
return String(defaultValue);
};A defence-in-depth variant, matching the printer's existing idiom of composing the fence from constants:
// packages/printer-legacy/src/printer.ts — pick a fence longer than any backtick run
const fence = "`".repeat(Math.max(3, longestBacktickRun(code) + 1));
return `${fence}graphql\n${code.trim()}\n${fence}\n`;The patches above are suggested, not tested by us. Notes:
printSchema already escapes the identical default to a single-line "\n```\n…" literal; aligning getFormattedDefaultValue with that behaviour is the smallest fix and keeps the printed SDL copy-pasteable.[String]) defaults and input-object field defaults; fixing the formatter covers all three, whereas only lengthening the fence leaves the unescaped value in the code block.escapeMDX and need no change; the non-String default paths (which are not wrapped in quotes) were not observed to break the fence.
Suggested regression test alongside the existing printer-legacy code-printer specs (exact spec path not verified): assert that printing a field whose String argument default decodes to \n```\n<img src="x" onerror="alert(1)" />\n```\n yields output in which no backtick run at column 0 appears inside the generated block, and that the <img> text remains inside the fenced code region — with a control case asserting a benign default is unchanged.Scope confirmed on main. printCodeArguments (@graphql-markdown/printer-legacy) is the only place argument default values are printed. They are formatted by getFormattedDefaultValue (@graphql-markdown/graphql), which wraps String/ID defaults in quotes without escaping them. Further entry points found during triage:
String!) skip the quoting branch, so their default is printed raw, with no quotes and no escaping;[String]!) skip the list formatter and are printed as a raw comma-joined string.
Any of these can emit a newline followed by a backtick run and close the graphql code fence.
The opt-in Example section has the same problem through @example operations. They are printed with graphql-js print, which keeps block strings on several lines, so a backtick line inside one can close the example fence.
graphql 17. With graphql 17, SDL-defined defaults are exposed as arg.default instead of defaultValue, so the printer currently prints no SDL default at all and this path is not reachable. A separate, public bug report tracks that. Its fix will go through the escaped formatter below.
Fix.getFormattedDefaultValue unwraps non-null types and prints every string default as an escaped, single-line GraphQL string literal (graphql-js print of a StringValue, the same output as printSchema).Printer.printCode makes the code fence longer than any backtick run inside the code.
Patched versions: @graphql-markdown/graphql 1.3.6, @graphql-markdown/printer-legacy 1.20.0, @graphql-markdown/docusaurus 1.39.3.
Workaround until you upgrade: only generate docs from schemas you trust, or use graphql 17.Source: NVD
Free Vulnerability Assessment
Evaluate your cloud security practices across 9 security domains to benchmark your risk level and identify gaps in your defenses.
Get a personalized demo
"Best User Experience I have ever seen, provides full visibility to cloud workloads."
"Wiz provides a single pane of glass to see what is going on in our cloud environments."
"We know that if Wiz identifies something as critical, it actually is."