Potential fix for code scanning alert no. 40: Incomplete string escaping or encoding#240
Potential fix for code scanning alert no. 40: Incomplete string escaping or encoding#240justlevine wants to merge 1 commit intodevelopfrom
Conversation
…ing or encoding Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
|
There was a problem hiding this comment.
Pull request overview
This pull request addresses a security code scanning alert (CodeQL alert #40) about incomplete string escaping in the getClassNamesFromString utility function. The issue arises from using String.prototype.replace() with a plain string argument, which only replaces the first occurrence rather than all occurrences, potentially leaving special characters unescaped.
Changes:
- Replaced
.replace( '"', '' )with.slice( 0, -1 )for removing the trailing quote from class attribute values - This change provides structurally precise string manipulation since the regex guarantees the string ends with a quote character
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Potential fix for https://github.com/rtCamp/snapwp/security/code-scanning/40
Generally, to fix this type of issue you should avoid using
String.prototype.replacewith a plain string when you intend to remove or escape all or specific occurrences of a character. Instead, either use a global regular expression (/.../g) to operate on all occurrences, or use a structurally precise manipulation such as slicing off a known prefix/suffix or using a capturing regex that extracts just what you need.For this specific function, we know
classAttributeis a string likeclass="foo bar baz"due to the preceding regex. We can robustly extract the class names by either:class="and then removing only the final trailing"viaslicerather than a genericreplace.The minimal change, preserving all existing behavior, is:
classAttributematch as-is..replace( 'class="', '' ).replace( '"', '' )with:replace('class="', '')(which is safe because it should appear only at the start); and"), via.slice(0, -1).This avoids incomplete string replacement, does not change any public API behavior, and does not require any imports or new helpers. All changes are within
packages/core/src/utils/styles/get-class-names-from-string.ts.Suggested fixes powered by Copilot Autofix. Review carefully before merging.