Skip to content

Fix uri type on bytes that are not ASCII - #243

Open
Che4ter wants to merge 1 commit into
fox-it:mainfrom
Che4ter:fix/uri-bytes
Open

Che4ter wants to merge 1 commit into
fox-it:mainfrom
Che4ter:fix/uri-bytes

Conversation

@Che4ter

@Che4ter Che4ter commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

string.__new__ decodes bytes (with errors="surrogateescape"), but uri.__init__ passed the original argument to urlparse. urlparse decodes bytes as ASCII, so a uri created from bytes had two problems:

from flow.record.fieldtypes import uri

uri(b"http://example.com/\xc3\x8c")
# UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 19

uri(b"http://example.com/a").hostname
# b'example.com'

Change

uri.__init__ parses str(self), the string that string.__new__ already built, instead of the argument. Input that is a str is unchanged. Input that is bytes now gives str parts, and bytes that are not valid UTF-8 are kept the way the string type keeps them.

Behaviour change: uri(<bytes>).hostname, .path and the other parts are str and no longer bytes. I found nothing in flow.record or dissect.target that reads them as bytes.

Tests

test_uri_type_bytes in tests/fieldtypes/test_fieldtypes.py: ASCII bytes, UTF-8 bytes with a non-ASCII character, and bytes that are not valid UTF-8. Without the change it fails (hostname is b'www.google.com'); with it the uri tests pass. The rest of the suite has the same failures with and without the change in my environment (17 in tests/tools/test_rdump.py and 1 in tests/test_regressions.py, and three adapter test files whose optional packages are not installed). ruff check and ruff format --check are clean.

Related to fox-it/dissect.target#1947

string.__new__ decodes bytes, but uri.__init__ passed the original
argument to urlparse, which decodes bytes as ASCII. A uri created from
bytes with a non-ASCII character raised a UnicodeDecodeError, for
example for URLs of the browser history plugins of dissect.target. For
ASCII bytes the parts (hostname, path and so on) were bytes, not str.

Parse the decoded string, as the string type does.

Related to fox-it/dissect.target#1947

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant