Potential fix for code scanning alert no. 6: Incomplete multi-character sanitization #38
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.
Potential fix for https://github.com/DataScience-GT/query/security/code-scanning/6
In general, the problem is that the sanitizer attempts to remove whole multi‑character patterns (e.g., an entire
<script>...</script>block) in a single.replacepass. When nested or overlapping constructs exist, removing one occurrence can cause the remainder of the string to form a new, still‑dangerous pattern that is not subsequently removed. To fix this without changing broader behavior, we can either (a) repeatedly apply the existing regex replacements until the string stops changing, or (b) switch to a proven sanitization library. Staying within the existing file and without adding new dependencies, the least invasive and safest fix is to loop the current replacements until no more modifications occur.Concretely, inside
sanitizeInput, in thetypeof input === 'string'branch (around lines 39–49), we should:.replace(...).replace(...).trim().slice(...)with a small loop:sanitizedvariable toinput.do { ... } while (sanitized !== previous);loop, repeatedly apply the same series of.replacecalls tosanitized..trim().slice(0, 10000)once.This ensures that if removing one pattern exposes another instance of any of the targeted patterns (like
<script), subsequent iterations will remove those as well until the string is stable. No new imports or helpers are required, and behavior for well‑formed, non‑malicious inputs remains effectively the same except for potentially performing multiple passes.Suggested fixes powered by Copilot Autofix. Review carefully before merging.