Repository navigation
fix: report non-UTF-8 stdin --event data as an error, not a traceback - #237
teeps-heisenberg wants to merge 6 commits into
Conversation
send_event() decodes event data piped via `--event -` with sys.stdin.buffer.read().decode() outside any exception handling. If the piped bytes are not valid UTF-8, the UnicodeDecodeError escaped and the program died with a traceback. Catch UnicodeDecodeError on the stdin path, log it as the new error E259, increment the error counter, log the traceback at debug level and drop the event, the same way invalid JSON (E143) is handled right below. Bump the "last unique Exxx error number" comment. Fixes 8go#236
|
Does not the test at Benjamin_Loison/Improve_websites_thanks_to_open_source/issues/4388 look satisfying to you? |
Add a pytest test that pipes a UTF-16 encoded event (as written by Windows PowerShell 5.1 ">" redirection) to send_event() via stdin and checks that it is dropped with error E259 and counted, instead of raising UnicodeDecodeError. Adapted from the test in the earlier, deleted PR for this issue. Refs 8go#236
|
Thanks. A realistic case is an event JSON file saved as UTF-16, for example with Windows PowerShell 5.1's I added the test from the earlier PR, adapted to use a UTF-16 payload and to also check that E259 is logged. It fails on master with |
|
While I agree that reporting as an error is cleaner than a traceback, is not it possible to instead accept other encodings than UTF-8? |
Event JSON piped in with --event - was always decoded as UTF-8, so a file saved as UTF-16 (e.g. by Windows PowerShell 5.1 ">") or with a UTF-8 BOM was rejected. Detect the encoding the same way json.loads does for bytes (json.detect_encoding: UTF-8/16/32, with or without a BOM) and decode with it. Data that is still undecodable, such as Latin-1, is reported with E259 as before. Refs 8go#236
|
Yes. I changed the stdin path to detect the encoding with
|
Please apply the same detection there in a follow-up commit in this pull request. |
Event files given with --event FILE were read in text mode with the locale encoding, so a UTF-16 file (e.g. written by Windows PowerShell 5.1 ">") was not decoded correctly (an uncaught UnicodeDecodeError with a UTF-8 locale, a misleading E143 with cp1252) and a UTF-8 BOM broke JSON parsing. Read the file as bytes and decode it with the same json.detect_encoding step as the stdin path; undecodable data is reported with E259 instead of a traceback. Refs 8go#236
|
Done in the latest commit: One behaviour change to be aware of: with a non-UTF-8 locale encoding (typically Windows before Python 3.15), a cp1252/Latin-1 event file used to be read with the locale codepage; it is now reported as E259, consistent with stdin. On the same systems a UTF-8 event file used to be silently sent as mojibake and is now decoded correctly. |
|
Thanks for the edits, in theory I am confident this pull request is good, I am going to check the actual code changes in a few days: https://framagenda.org/apps/calendar/p/Dg8W8tqd5Dp9KTd3/dayGridMonth/now/view/full/L3JlbW90ZS5waHAvZGF2L3B1YmxpYy1jYWxlbmRhcnMvRGc4Vzh0cWQ1RHA5S1RkMy85MzUyMDAxMS1lMzc2LTQwNjQtODQ5Mi01ZTMxMWM2MDVlNDguaWNz/1796468400 |
|
https://chat.mistral.ai/work/e5eacde5-6f50-490f-8ce3-e1c77f64b4f3
I agree this comment, can you refactor accordingly your pull request? |
Pass the raw bytes to json.loads instead of calling json.detect_encoding directly, as requested in review.
json.loads sees a BOM or UTF-16/32 whitespace as non-empty input; check the decoded text it reports instead and drop the event quietly.
|
Done: the raw bytes now go straight to |
What was wrong
When an event is piped in with
--event -,send_event()decoded it withsys.stdin.buffer.read().decode(), i.e. always as UTF-8, without handling decoding errors. Bytes that are not valid UTF-8 (e.g. UTF-16 with a BOM, or Latin-1) raise an uncaughtUnicodeDecodeErrorand the program exits with a traceback. A UTF-8 byte-order mark is kept in the decoded text, so such input then fails JSON parsing (E143).A realistic trigger: an event JSON file saved as UTF-16, for example with Windows PowerShell 5.1's
>redirection (which writes theFF FEbyte-order mark), then fed to matrix-commander from a shell that passes bytes through unchanged, e.g.cat event.json | matrix-commander --event -in Git Bash or WSL, ormatrix-commander --event - < event.jsonin cmd.exe.What changed
json.detect_encoding(), the same detectionjson.loads()uses for bytes input: UTF-8, UTF-16 and UTF-32, with or without a byte-order mark. Such events are now sent.\xe9) are caught: a new errorE259is logged,gs.err_countis incremented, the traceback is logged at debug level, and the event is dropped. This mirrors the existing E143 handling (invalid JSON) a few lines below. Legacy 8-bit encodings are not guessed, since a wrong guess would silently send mangled text.--event FILEpreviously read the file in text mode with the locale encoding (a UTF-16 file failed with an uncaughtUnicodeDecodeErrorunder a UTF-8 locale, or with a misleading E143 under cp1252, and a UTF-8 BOM was kept). It now reads bytes and uses the same detection and E259 handling. Note: with a non-UTF-8 locale encoding (typically Windows before Python 3.15), a Latin-1/cp1252 file used to be read with the locale codepage; it is now reported as E259 instead. In the other direction, on those systems a UTF-8 event file was decoded as cp1252 and sent as mojibake without an error; it is now decoded correctly.tests/test_event_stdin.py, adapted from the test in the earlier, deleted PR for this issue. It pipes the same event via stdin as UTF-8, UTF-8 with BOM, UTF-16LE with BOM, UTF-16BE and UTF-32 and checks that it reachesroom_sendwith the right content; and it pipes Latin-1 bytes and checks that the event is dropped, counted and reported as E259. The same cases are repeated with--event FILE. CI does not run pytest today, so the test documents the behaviour rather than gating it.How tested
On Windows, Python 3.15, with
pip install -e ..python-magichangs on import on this machine (libmagic loading), so it was replaced by a stub module onPYTHONPATHfor the test run; everything else, includingmatrix-nio, is real.pytest tests/test_event_stdin.py:12 passedwith this change. Against the earlier commits of this PR, 4 of the 6 stdin cases fail with the UTF-8-only commit, and 5 of the 6 file cases fail without the file change (all but plain UTF-8).matrix_commander.py(the file has pre-existing warnings elsewhere). flake8 andblack --check -l 79are clean on the test file.Not tested: a live run against a Matrix server.
Fixes #236