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: Catch UnicodeDecodeError on a malformed problem report #52

Merged
merged 1 commit into from
Jan 6, 2023

Conversation

bdrung
Copy link
Collaborator

@bdrung bdrung commented Jan 5, 2023

apport-unpack will crash when passing a binary file that is not valid UTF-8:

$ apport-unpack /bin/ls unpack
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/problem_report.py", line 175, in load
    (key, value) = line.split(b":", 1)
    ^^^^^^^^^^^^
ValueError: not enough values to unpack (expected 2, got 1)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/bin/apport-unpack", line 91, in <module>
    main()
  File "/usr/bin/apport-unpack", line 67, in main
    pr = load_report(args.report)
         ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/bin/apport-unpack", line 47, in load_report
    pr.load(f, binary=False)
  File "/usr/lib/python3/dist-packages/problem_report.py", line 178, in load
    f"Malformed problem report: Line {line.decode()!r}"
                                      ^^^^^^^^^^^^^
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc0 in position 24: invalid start byte

When wrapping the ValueError into an MalformedProblemReport, do not expect the causing line to be valid UTF-8. Replace the invalid characters with backslashed escape sequences.

Bug: https://launchpad.net/bugs/1996040

apport-unpack will crash when passing a binary file that is not valid
UTF-8:

```
$ apport-unpack /bin/ls unpack
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/problem_report.py", line 175, in load
    (key, value) = line.split(b":", 1)
    ^^^^^^^^^^^^
ValueError: not enough values to unpack (expected 2, got 1)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/bin/apport-unpack", line 91, in <module>
    main()
  File "/usr/bin/apport-unpack", line 67, in main
    pr = load_report(args.report)
         ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/bin/apport-unpack", line 47, in load_report
    pr.load(f, binary=False)
  File "/usr/lib/python3/dist-packages/problem_report.py", line 178, in load
    f"Malformed problem report: Line {line.decode()!r}"
                                      ^^^^^^^^^^^^^
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc0 in position 24: invalid start byte
```

When wrapping the `ValueError` into an `MalformedProblemReport`, do not
expect the causing line to be valid UTF-8. Replace the invalid
characters with backslashed escape sequences.

Bug: https://launchpad.net/bugs/1996040
Signed-off-by: Benjamin Drung <benjamin.drung@canonical.com>
@codecov
Copy link

codecov bot commented Jan 5, 2023

Codecov Report

Merging #52 (1349d94) into main (f5a6b8c) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             main      #52   +/-   ##
=======================================
  Coverage   80.13%   80.14%           
=======================================
  Files          81       81           
  Lines       17583    17588    +5     
=======================================
+ Hits        14091    14096    +5     
  Misses       3492     3492           
Impacted Files Coverage Δ
problem_report.py 97.14% <ø> (ø)
tests/unit/test_problem_report.py 99.36% <100.00%> (+0.01%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@schopin-pro
Copy link
Contributor

LGTM.

@schopin-pro schopin-pro merged commit aed1f92 into canonical:main Jan 6, 2023
@bdrung bdrung deleted the malformed-utf8 branch January 6, 2023 17:08
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