swagger-typescript-api icon indicating copy to clipboard operation
swagger-typescript-api copied to clipboard

Fix JSDoc comment security vulnerability: escape only necessary */ sequences

Open FDiskas opened this issue 4 months ago • 4 comments

vibe coded PR

closes #1321

This PR fixes a security vulnerability where unescaped characters in OpenAPI/Swagger descriptions could break generated TypeScript files or enable code injection attacks. The fix uses minimal escaping - only escaping the dangerous */ sequences while leaving harmless /* sequences unescaped to reduce noise in generated documentation.

Problem

Vulnerable input example:

description: "This endpoint is equivalent to **/information** endpoint"
summary: "Get data with **/ alert('XSS') /** injection attempt"

Generated vulnerable output:

/**
 * Description with */ alert('XSS') /* injection attempt
 */

The */ sequence breaks out of the JSDoc comment, allowing arbitrary JavaScript code execution.

Solution

The fix applies surgical escaping that only targets the dangerous pattern:

  • */ sequences are escaped to *\/ (prevents comment breakout)
  • /* sequences remain unescaped (harmless inside comments)

Safe output after fix:

/**
 * Description with *\/ alert('XSS') /* injection attempt
 */

Technical Details

  • Core changes: Updated escapeJSDocContent() in src/schema-parser/schema-formatters.ts to only escape */
  • Template consistency: Modified route-docs.ejs to use consistent escaping approach
  • Smart detection: formatDescription() detects already-escaped content to prevent double-escaping
  • Fixed snapshots: Corrected test snapshots to show proper single backslash escaping

Impact

  • Security: Prevents code injection through OpenAPI documentation fields
  • Clean output: Reduces unnecessary escaping noise (no more \/* in docs)
  • Compatibility: No breaking changes to existing functionality

FDiskas avatar Aug 20 '25 20:08 FDiskas

🦋 Changeset detected

Latest commit: 37bf80a8c9421ed50a2ebf053c8a635d7bb16b21

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
swagger-typescript-api Patch

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

changeset-bot[bot] avatar Aug 20 '25 20:08 changeset-bot[bot]

bugbot run

smorimoto avatar Aug 21 '25 02:08 smorimoto

Updated PR

FDiskas avatar Aug 25 '25 21:08 FDiskas

bugbot run

smorimoto avatar Aug 28 '25 07:08 smorimoto