Skip to content

Potential fix for code scanning alert no. 14: Incomplete multi-character sanitization - #12

Closed
KiidxAtlas wants to merge 1 commit into
mainfrom
alert-autofix-14
Closed

KiidxAtlas wants to merge 1 commit into
mainfrom
alert-autofix-14

Conversation

@KiidxAtlas

Copy link
Copy Markdown
Owner

Potential fix for https://github.com/KiidxAtlas/python-hover/security/code-scanning/14

In general, the problem is that the sanitization first removes tags with a regex, then decodes HTML entities, which can recreate < and > characters and thus resurrect tags such as <script>. To fix this without changing existing functionality too much, we should ensure that after all entity decoding and whitespace normalization, the resulting string no longer contains dangerous HTML constructs, especially script blocks. Two straightforward strategies, both compatible with the existing pipeline, are: (1) escape any < characters that could start an HTML tag, or (2) remove or neutralize <script> tags specifically after decoding. Since this output is intended as Markdown, preserving < and > as plain text is often desirable (for code samples etc.), so the best minimal fix is to strip any <script ...>...</script> blocks that might have been reintroduced by entity decoding.

Concretely, we should add a final sanitization step after the existing entity decoding and whitespace normalization, just before return md.trim();. This step will repeatedly remove any <script> blocks using a robust regex that handles multiline content and nested </> inside the body. To avoid the incomplete multi-character sanitization problem, we will apply the replacement in a loop until no changes occur. All changes will be restricted to docs-engine/src/scraping/sphinxScraper.ts inside the shown snippet, and no new imports are needed.

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

…ter sanitization

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@github-actions

github-actions Bot commented Apr 3, 2026

Copy link
Copy Markdown

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Snapshot Warnings

⚠️: No snapshots were found for the head SHA 9c37518.
Ensure that dependencies are being submitted on PR branches and consider enabling retry-on-snapshot-warnings. See the documentation for more information and troubleshooting advice.

Scanned Files

None

// As a final safety step, strip any script tags that might have been
// reintroduced by HTML entity decoding above.
let prev: string;
const scriptTagPattern = /<script\b[^>]*>[\s\S]*?<\/script>/gi;

Check failure

Code scanning / CodeQL

Bad HTML filtering regexp High

This regular expression does not match script end tags like </script >.

Copilot Autofix

AI 6 months ago

In general, the robust way to fix this is to avoid hand-written HTML-stripping regexes entirely and instead rely on a well-tested HTML parser or sanitization library that understands browser quirks (for example, ignoring attributes on closing tags, handling malformed tags, etc.). For this specific case, we want to keep existing behavior and minimize changes, while making the "final safety step" actually remove any remaining script content that a browser might interpret as a script, even if the closing tag is malformed.

The single best minimal change here is to stop trying to match well-formed <script>...</script> blocks with a regex that insists on a precise </script> end tag. Instead, we can: (1) remove any remaining <script start tags in a loop, regardless of how they are closed, and (2) optionally also remove standalone </script-like closing tags. Since this function already strips all other tags, losing only the script tag text (or even the script contents) is acceptable and aligns with the comment that this is a "final safety step". Concretely, within htmlToMarkdown near lines 1011–1018 in docs-engine/src/scraping/sphinxScraper.ts, replace the current scriptTagPattern and do/while loop with a simpler pattern that matches any <script tag (opening or malformed), such as /<\/?script\b[^>]*>/gi, and run it until no more matches remain. That avoids reliance on a specific </script> shape, and covers odd cases like </script foo="bar">. No additional imports or helper functions are required.

Suggested changeset 1
docs-engine/src/scraping/sphinxScraper.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/docs-engine/src/scraping/sphinxScraper.ts b/docs-engine/src/scraping/sphinxScraper.ts
--- a/docs-engine/src/scraping/sphinxScraper.ts
+++ b/docs-engine/src/scraping/sphinxScraper.ts
@@ -1009,9 +1009,11 @@
         md = md.replace(/\n{3,}/g, '\n\n');
 
         // As a final safety step, strip any script tags that might have been
-        // reintroduced by HTML entity decoding above.
+        // reintroduced by HTML entity decoding above. We deliberately match
+        // both opening and closing tags with optional attributes to handle
+        // malformed but browser-accepted variants like "</script foo="bar">".
         let prev: string;
-        const scriptTagPattern = /<script\b[^>]*>[\s\S]*?<\/script>/gi;
+        const scriptTagPattern = /<\/?script\b[^>]*>/gi;
         do {
             prev = md;
             md = md.replace(scriptTagPattern, '');
EOF
@@ -1009,9 +1009,11 @@
md = md.replace(/\n{3,}/g, '\n\n');

// As a final safety step, strip any script tags that might have been
// reintroduced by HTML entity decoding above.
// reintroduced by HTML entity decoding above. We deliberately match
// both opening and closing tags with optional attributes to handle
// malformed but browser-accepted variants like "</script foo="bar">".
let prev: string;
const scriptTagPattern = /<script\b[^>]*>[\s\S]*?<\/script>/gi;
const scriptTagPattern = /<\/?script\b[^>]*>/gi;
do {
prev = md;
md = md.replace(scriptTagPattern, '');
Copilot is powered by AI and may make mistakes. Always verify output.
@KiidxAtlas KiidxAtlas closed this Jun 12, 2026
@KiidxAtlas
KiidxAtlas deleted the alert-autofix-14 branch June 12, 2026 01:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants