Permalink
Show file tree
Hide file tree
1 comment
on commit
sign in to comment.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Fix XXE vulnerability in XMP metadata parsing
For details: https://portswigger.net/web-security/xxe Reported by: Eric Therond eric.therond@sonarsource.com) of Sonarsource (https://www.sonarsource.com/)
- Loading branch information
Showing
3 changed files
with
59 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # This Source Code Form is subject to the terms of the Mozilla Public | ||
| # License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| # file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
| # | ||
| # Copyright (C) 2021, James R. Barlow (https://github.com/jbarlow83/) | ||
|
|
||
|
|
||
| from typing import IO, Any, AnyStr, Union | ||
|
|
||
| from lxml.etree import XMLParser as _UnsafeXMLParser | ||
| from lxml.etree import parse as _parse | ||
|
|
||
|
|
||
| class _XMLParser(_UnsafeXMLParser): | ||
| def __init__(self, *args, **kwargs): | ||
| # Prevent XXE attacks | ||
| # https://rules.sonarsource.com/python/type/Vulnerability/RSPEC-2755 | ||
| kwargs['resolve_entities'] = False | ||
| kwargs['no_network'] = True | ||
| super().__init__(*args, **kwargs) | ||
|
|
||
|
|
||
| def parse_xml(source: Union[AnyStr, IO[Any]], recover: bool = False): | ||
| """Wrapper around lxml's parse to provide protection against XXE attacks.""" | ||
|
|
||
| parser = _XMLParser(recover=recover, remove_pis=False) | ||
| return _parse(source, parser=parser) | ||
|
|
||
|
|
||
| __all__ = ['parse_xml'] |
This file contains 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
This file contains 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
3f38f73There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CVE-2021-29421 was assigned to this commit.