Skip to content

Commit be55902

Browse files
authored
fix: added a fingers-crossed handling for failed input data decoding (#612)
Signed-off-by: Jan Kowalleck <[email protected]>
1 parent 46cd517 commit be55902

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

cyclonedx_py/client.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,14 @@ def _get_input_parser(self) -> BaseParser:
290290
with input_data_fh:
291291
input_data = input_data_fh.read()
292292
if isinstance(input_data, bytes):
293-
input_encoding = (chardetect(input_data)['encoding'] or '').replace(
294-
# replace Windows-encoding with code-page
295-
'Windows-', 'cp')
296-
input_data = input_data.decode(input_encoding)
293+
try:
294+
input_encoding = (chardetect(input_data)['encoding'] or sys.getdefaultencoding()).replace(
295+
# replace Windows-encoding with code-page
296+
'Windows-', 'cp')
297+
input_data = input_data.decode(input_encoding)
298+
except ValueError:
299+
# last resort: try utf8 and hope for the best
300+
input_data = input_data.decode('utf-8', 'backslashreplace')
297301
input_data_fh.close()
298302

299303
if self._arguments.input_from_conda_explicit:

cyclonedx_py/parser/poetry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def __init__(
8787
debug_message: DebugMessageCallback = quiet
8888
) -> None:
8989
debug_message('open file: {}', poetry_lock_filename)
90-
with open(poetry_lock_filename) as plf:
90+
with open(poetry_lock_filename, errors='backslashreplace') as plf:
9191
super(PoetryFileParser, self).__init__(
9292
poetry_lock_contents=plf.read(), use_purl_bom_ref=use_purl_bom_ref,
9393
debug_message=debug_message
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# This file is automatically @generated by Poetry 1.6.1 and should not be changed by hand.
2+
3+
[[package]]
4+
name = "pyhumps"
5+
version = "3.7.1"
6+
description = "🐫 Convert strings (and dictionary keys) between snake case, camel case and pascal case in Python. Inspired by Humps for Node"
7+
optional = false
8+
python-versions = "*"
9+
files = [
10+
{file = "pyhumps-3.7.1-py3-none-any.whl", hash = "sha256:c6f2d833f2c7afae039d71b7dc0aba5412ae5b8c8c33d4a208c1d412de17229e"},
11+
{file = "pyhumps-3.7.1.tar.gz", hash = "sha256:5616f0afdbc73ef479fa9999f4abdcb336a0232707ff1a0b86e29fc9339e18da"},
12+
]
13+
14+
[metadata]
15+
lock-version = "2.0"
16+
python-versions = "^3.11"
17+
content-hash = "e9676d347231afe6a46e027d88442e90348436b55346267e68a37e340c5f8f6f"

tests/test_parser_poetry.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,12 @@ def test_simple_purl_bom_ref(self, lock_file_name: str) -> None:
5353
self.assertEqual(component.purl.to_string(), component.bom_ref.value)
5454
self.assertEqual('0.10.2', component.version)
5555
self.assertEqual(2, len(component.external_references), f'{component.external_references}')
56+
57+
def test_regression_issue611(self) -> None:
58+
# see https://github.com/CycloneDX/cyclonedx-python/issues/611
59+
lock_file_name = 'poetry-lock-regression-issue611.txt.bin'
60+
poetry_lock_filename = os.path.join(os.path.dirname(__file__), 'fixtures', lock_file_name)
61+
parser = PoetryFileParser(poetry_lock_filename=poetry_lock_filename, use_purl_bom_ref=True)
62+
self.assertEqual(1, parser.component_count())
63+
component = next(filter(lambda c: c.name == 'pyhumps', parser.get_components()), None)
64+
self.assertEqual('pyhumps', component.name)

0 commit comments

Comments
 (0)