Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix KeyError in Schematron When Handling Missing or Invalid Namespace Prefixes #423

Merged
merged 1 commit into from
May 26, 2024

Conversation

DaveLak
Copy link
Contributor

@DaveLak DaveLak commented May 15, 2024

Fixes: https://bugs.launchpad.net/lxml/+bug/2058177

Resolves an issue in the lxml.isoschematron.Schematron class where an unhandled KeyError was raised when Schematron is initialized with elements that have invalid or missing namespace prefixes. The KeyError occurred due to direct dictionary access without checking for the existence of the key.

The changes introduced here:

  • Replace direct dictionary access with dict.get() to safely retrieve values from namespace maps, preventing KeyError on missing/invalid prefixes.
  • Add unit tests to cover cases where schemas lack proper namespace prefixes or contain incorrect namespace prefixes.

Resolves an issue in the `lxml.isoschematron.Schematron` class where
an unhandled KeyError was raised when `Schematron` is initialized with
elements that have invalid or missing namespace prefixes. The KeyError
occurred due to direct dictionary access without checking for the
existence of the key.

The changes introduced here:

- Replace direct dictionary access with dict.get() to safely retrieve values
  from namespace maps, preventing KeyError on missing/invalid prefixes.
- Enhance unit tests to cover cases where schemas lack proper namespace prefixes
  or contain incorrect namespace prefixes, ensuring robust error handling.

Fixes: https://bugs.launchpad.net/lxml/+bug/2058177
@scoder
Copy link
Member

scoder commented May 25, 2024

Looks good to me. Thanks.

@scoder scoder merged commit 8ebcd2c into lxml:master May 26, 2024
44 of 46 checks passed
@DaveLak DaveLak deleted the fix-schematron-keyerror branch May 28, 2024 17:21
DaveLak added a commit to DaveLak/oss-fuzz that referenced this pull request May 29, 2024
The changes here update the `fuzz_sax` test harness to gracefully handle
the exception so the fuzzer will not exit when the exception is raised
and remove the special case handling in `fuzz_schematron.py` for a bug in
`lxml.isoschematron.Schematron that has been fixed.`

 ## `fuzz_sax.py`

The `IndexError` raised in `src/lxml/sax.py` is a known bug with an
issue marked as low priority in lxml's bug tracker since  2023-03-14:
https://bugs.launchpad.net/lxml/+bug/2011542

 ## `fuzz_schematron.py`

The unhandled `KeyError`  bug in the `lxml.isoschematron.Schematron`
class that was previously identified by fuzzing has been fixed upstream
via: lxml/lxml#423.
DavidKorczynski pushed a commit to google/oss-fuzz that referenced this pull request May 29, 2024
The changes here update the `fuzz_sax` test harness to gracefully handle
a documented exception so the fuzzer will not exit when the exception is
raised. This PR also removes the special case handling in
`fuzz_schematron.py` for a bug in `lxml.isoschematron.Schematron` that
has been fixed.

## Changes to `fuzz_sax.py`

The `IndexError` raised in `src/lxml/sax.py` is a known bug with an
issue marked as low priority in lxml's bug tracker since 2023-03-14:
https://bugs.launchpad.net/lxml/+bug/2011542

## Changes to `fuzz_schematron.py`

The unhandled `KeyError` bug in the `lxml.isoschematron.Schematron`
class that was previously identified by fuzzing has been fixed upstream
via: lxml/lxml#423.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants