fix: Improve HTML entity sanitization for invalid XML characters (#348)#1683
Open
Rithboss wants to merge 2 commits intofreelawproject:mainfrom
Open
fix: Improve HTML entity sanitization for invalid XML characters (#348)#1683Rithboss wants to merge 2 commits intofreelawproject:mainfrom
Rithboss wants to merge 2 commits intofreelawproject:mainfrom
Conversation
Fixes freelawproject#348 - Enhanced clean_html() to remove HTML entities for all invalid XML characters - Added comprehensive regex to handle decimal entities (� through &freelawproject#31;) - Added support for hexadecimal entities (� through ) - Excludes valid XML characters: tab (0x09), LF (0x0A), CR (0x0D) - Added comprehensive test suite with 6 test cases covering: - ESC character (\x1b) removal - Various invalid XML characters (NULL, SOH, STX, BS, VT, FF, SO, ESC, US) - Valid XML character preservation (tab, newline, carriage return) - HTML entity handling (&freelawproject#27;, etc.) - Integration with strip_bad_html_tags_insecure() - Real-world docket text scenarios This prevents XMLSyntaxError when parsing PACER dockets that contain invalid XML characters like escape sequences.
|
|
for more information, see https://pre-commit.ci
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.
Fixes #348
Summary
This PR enhances the HTML sanitization logic to properly handle invalid XML character entities, preventing when parsing PACER dockets that contain escape sequences or other invalid XML characters.
Problem
PACER dockets sometimes contain invalid XML characters (like - ESC character) that cause parsing failures with error:
The existing
clean_html()function had limited support for removing invalid character entities, only handling � through �.Solution
clean_html()to remove ALL invalid XML character entitiesTesting
Added comprehensive test suite (
tests/local/test_xml_character_sanitization.py) with 6 test cases:All tests pass (6/6).
Changes
juriscraper/lib/html_utils.pyCHANGES.mdtests/local/test_xml_character_sanitization.pyBackward Compatibility
This change is fully backward compatible. It only removes invalid characters that would have caused parsing errors anyway.