Skip to content

Commit aa3a41f

Browse files
amcaplanclaude
andcommitted
Fix XSS vulnerability in GraphiQL config injection
Security scan flagged an XSS vector where unsanitized query parameters could break out of the <script> context when embedded in the HTML page. Solution: - Escape <, >, and & characters when embedding JSON in the HTML script tag - Use Unicode escapes (\u003c, \u003e, \u0026) instead of HTML entities - Unicode escapes are decoded by JavaScript's JSON parser, preserving the original query text for GraphiQL - HTML entities (like &gt;) would NOT be decoded inside <script> tags, breaking GraphQL query syntax This prevents XSS while maintaining correct functionality. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent c509e57 commit aa3a41f

File tree

1 file changed

+7
-1
lines changed
  • packages/app/src/cli/services/dev/graphiql

1 file changed

+7
-1
lines changed

packages/app/src/cli/services/dev/graphiql/server.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,13 @@ export function setupGraphiQLServer({
187187
}
188188

189189
// Inject config script before </head>
190-
const configScript = `<script>window.__GRAPHIQL_CONFIG__ = ${JSON.stringify(config)};</script>`
190+
// Escape < > & in JSON to prevent XSS when embedding in HTML script tags
191+
// Use Unicode escapes so JavaScript correctly decodes them (HTML entities would break the query)
192+
const safeJson = JSON.stringify(config)
193+
.replace(/</g, '\\u003c')
194+
.replace(/>/g, '\\u003e')
195+
.replace(/&/g, '\\u0026')
196+
const configScript = `<script>window.__GRAPHIQL_CONFIG__ = ${safeJson};</script>`
191197
indexHtml = indexHtml.replace('</head>', `${configScript}\n </head>`)
192198

193199
res.setHeader('Content-Type', 'text/html')

0 commit comments

Comments
 (0)