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

Don't throw exception when encoding text as UTF-8 bytes fails #40

Closed
phoerious opened this issue Jul 9, 2021 · 2 comments · Fixed by #41
Closed

Don't throw exception when encoding text as UTF-8 bytes fails #40

phoerious opened this issue Jul 9, 2021 · 2 comments · Fixed by #41

Comments

@phoerious
Copy link
Contributor

The following line throws an exception if the input HTML string cannot be encoded as UTF-8:

bytes_html = html.encode('UTF-8')

When using Selectolax for parsing arbitrary web data, this condition can be triggered easily when the server reports the wrong encoding or multiple encodings are mixed, particularly in combination with UTF-7.

Take the byte string b'Roboto+Condensed', which (on its own) should have correctly been decoded as either ASCII or UTF-8. However, interpreted as UTF-7, the string contains an invalid shift sequence:

>>> b'Roboto+Condensed'.decode('utf-7')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "../encodings/utf_7.py", line 12, in decode
    return codecs.utf_7_decode(input, errors, True)
UnicodeDecodeError: 'utf7' codec can't decode bytes in position 6-15: unterminated shift sequence

Ignoring this error will produce a Unicode string which cannot be re-encoded as UTF-8 bytes without either replacing or ignoring invalid characters:

>>> b'Roboto+Condensed'.decode('utf-7', errors='ignore')
'Robotoઉ\udd7a笞'
>>> b'Roboto+Condensed'.decode('utf-7', errors='ignore').encode('utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'utf-8' codec can't encode character '\udd7a' in position 7: surrogates not allowed

Obviously, the easy fix is to do the encoding step beforehand, pass the raw bytes to Selectolax and force it to use UTF-8 by setting detect_encoding=False, but that's a lot of stuff you have to figure out in order to get it right, because the error is not really intuitive and can occur without any apparent reason. Since Selectolax already has a decode_errors parameter that defaults to "ignore", I would suggest to apply it also to this encoding step.

def __init__(self, html, detect_encoding=True, use_meta_tags=True, decode_errors = 'ignore'):

@rushter
Copy link
Owner

rushter commented Jul 9, 2021

That's a good suggestion. Would you like to make a PR?

@phoerious
Copy link
Contributor Author

Sure, can do.

phoerious added a commit to phoerious/selectolax that referenced this issue Jul 9, 2021
rushter pushed a commit that referenced this issue Jul 10, 2021
* Apply `decode_errors` to encoding as well, fixes #40

* Fix __repr__() segfault and buggy items() on _Attributes

When calling attrs on a temporary instance, the tree may already be
deallocated when the tag name is being retrieved, which causes a null pointer
dereference. This fix doesn't really solve the problem, but at least it
prevents the crash.

Also fixes a bug in items() that made the method useless.

Fixes #39

* Add test for unencodable strings
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 a pull request may close this issue.

2 participants