Escape */ sequences in generated JSDoc to prevent comment injection#1611
Merged
Escape */ sequences in generated JSDoc to prevent comment injection#1611
Conversation
🦋 Changeset detectedLatest commit: 7f25d28 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
OpenAPI spec fields (descriptions, titles, examples, patterns, etc.) are untrusted input that may contain `*/` sequences. When embedded in JSDoc block comments, these sequences prematurely close the comment, producing invalid TypeScript output. Add `escapeJSDocContent()` to SchemaFormatters that replaces `*/` with `*\/` and expose it as a template utility. Apply escaping consistently across all JSDoc-emitting templates (data-contract-jsdoc, object-field-jsdoc, route-docs, api) and enum field descriptions. Closes #1321
a1c92bd to
7f25d28
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
OpenAPI specification fields (descriptions, titles, examples, patterns, etc.) are untrusted input that may contain
*/sequences. When these values are embedded into JSDoc block comments in generated TypeScript output, a*/sequence prematurely closes the comment block, producing syntactically invalid TypeScript. This affects any spec with response types or schema annotations containing*/.Solution
Add a centralized
escapeJSDocContent()method toSchemaFormattersthat replaces*/with*\/in any value destined for JSDoc output. The method handlesundefined(returns"") and coerces non-string values viaString(), since OpenAPI fields likeminimum,maximum, anddefaultcan be numbers, booleans, or objects.The escape is applied consistently across all JSDoc-emitting code paths:
schema-formatters.ts—escapeJSDocContent()is called withinformatDescription()and on enum field descriptions during formattingcode-gen-process.ts—escapeJSDocContentis exposed as a template utility alongsideformatDescriptiondata-contract-jsdoc.ejs— All@format,@min,@max,@pattern,@example,@default, and other annotation values are escaped;titleis routed throughformatDescriptionfor consistencyobject-field-jsdoc.ejs— Same treatment for object field annotationsroute-docs.ejs— Route documentation tags, response status codes, type signatures, and descriptions are escapedapi.ejs— Top-level API metadata (@title,@version,@license,@baseUrl,@contact, etc.) is escapedA dedicated test suite (
tests/spec/issue-1321/) validates that*/in descriptions, enum values, examples, and patterns produces valid output.Verification
bun run testpasses, including snapshot updates fortests/spec/responses/(the existing inline response type comments now use*\/instead of the previous ad-hoc\*/*\escaping)tests/spec/issue-1321/covers a schema with*/in descriptions, enum descriptions, examples, and patternsCloses #1321
Closes #672
Closes #704
Supersedes #1368
Note
Medium Risk
Touches core code generation formatting/templates, which can subtly change emitted TypeScript comments and snapshot outputs across many users, though the change is narrowly scoped to JSDoc escaping.
Overview
Prevents JSDoc comment injection/broken generated TypeScript by escaping
*/sequences coming from untrusted OpenAPI fields.Adds
SchemaFormatters.escapeJSDocContent()and wires it through the codegen/template utilities, then updates all JSDoc-emitting templates (schema/field docs, route docs, and top-level API metadata, plus enum value descriptions) to consistently escape annotation values (e.g.,@example,@pattern, response types/status, tags, routes).Adds a new regression spec (
tests/spec/issue-1321) with snapshots and updates existing response snapshots to reflect the new escaping behavior.Written by Cursor Bugbot for commit 7f25d28. This will update automatically on new commits. Configure here.