Add escapeHtml function for HTML context escaping - #1263
Open
pavankumar-vh wants to merge 1 commit into
Open
Conversation
The escapeString function is used for generic string escaping (likely for embedding JS/JSON string literals in generated code), not for HTML output. Overloading it with HTML escaping would break existing callers that depend on the un-escaped characters passing through. Added a separate escapeHtml function that explicitly escapes HTML-unsafe characters (<, >, &, ') to prevent XSS attacks and HTML injection when embedding user-controlled strings in HTML contexts. Also added comprehensive test coverage for both functions to document the distinction and prevent regressions.
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.
Overview
Add a dedicated
escapeHtmlfunction for escaping strings in HTML contexts, separate from the existingescapeStringfunction.Problem
The previous PR (#1231) modified
escapeStringto escape HTML-unsafe characters, but this could break existing callers that depend on the un-escaped characters passing through (e.g., building strings for JSON parsing or non-HTML contexts).Fix
Added a separate
escapeHtmlfunction that explicitly escapes HTML-unsafe characters (<,>,&,') to prevent XSS attacks and HTML injection. The existingescapeStringfunction remains unchanged for backward compatibility.Testing
Added comprehensive test coverage for both functions:
escapeStringtests verify it still escapes JSON special characters but NOT HTML-unsafe onesescapeHtmltests verify it properly escapes<script>,&,',>to prevent XSSAll 24 tests pass (19 existing + 5 new).
Files Changed
common/src/util/string.ts- Added escapeHtml functioncommon/src/util/__tests__/string.test.ts- Added test coverage for both functionsScope
This change only touches
common/which is an approved contribution area per the Contributing Guide.