Skip to content

latin-1 User-Agent is breaking requests #146

@azertyfun

Description

@azertyfun

We have a user with a latin-1 encoded user agent:

>>> b"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) 3V\xE4xel/1.5.0-239 Chrome/108.0.5359.215 Electron/22.3.8 Safari/537.36".decode('utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe4 in position 83: invalid continuation byte
>>> b"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) 3V\xE4xel/1.5.0-239 Chrome/108.0.5359.215 Electron/22.3.8 Safari/537.36".decode('latin-1')
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) 3Växel/1.5.0-239 Chrome/108.0.5359.215 Electron/22.3.8 Safari/537.36'

With kong 3.8 this is breaking as it seems to make the assumptions that headers are utf-8 encoded:

----------------------------------------, context: ngx.timer
Exception occurred during processing of request from , context: ngx.timer
Traceback (most recent call last):, context: ngx.timer
  File "/usr/local/lib/python3.10/dist-packages/kong_pdk/listener.py", line 59, in handle, context: ngx.timer
  File "msgpack/_unpacker.pyx", line 540, in msgpack._cmsgpack.Unpacker.__next__, context: ngx.timer
  File "msgpack/_unpacker.pyx", line 463, in msgpack._cmsgpack.Unpacker._unpack, context: ngx.timer
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe4 in position 83: invalid continuation byte, context: ngx.timer
----------------------------------------, context: ngx.timer

However this assumption is incorrect as the encoding of HTTP headers seems to be ill-defined. I don't think that HTTP client is necessarily making an "incorrect" assumption by using latin-1. The PDK probably should just treat headers as bytes, or at the very least have a fallback mechanism to ascii on the str.decode.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions