Skip to content

fix: report non-UTF-8 stdin --event data as an error, not a traceback - #237

Open
teeps-heisenberg wants to merge 6 commits into
8go:masterfrom
teeps-heisenberg:fix/issue-236-event-stdin-utf8
Open

teeps-heisenberg wants to merge 6 commits into
8go:masterfrom
teeps-heisenberg:fix/issue-236-event-stdin-utf8

Conversation

@teeps-heisenberg

@teeps-heisenberg teeps-heisenberg commented Dec 1, 2026 •

Copy link
Copy Markdown

What was wrong

When an event is piped in with --event -, send_event() decoded it with sys.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 uncaught UnicodeDecodeError and 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 the FF FE byte-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, or matrix-commander --event - < event.json in cmd.exe.

What changed

  • The event bytes, from stdin or from a file, are decoded with the encoding reported by json.detect_encoding(), the same detection json.loads() uses for bytes input: UTF-8, UTF-16 and UTF-32, with or without a byte-order mark. Such events are now sent.
  • Bytes that still cannot be decoded (e.g. Latin-1 text with a raw \xe9) are caught: a new error E259 is logged, gs.err_count is 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 FILE previously read the file in text mode with the locale encoding (a UTF-16 file failed with an uncaught UnicodeDecodeError under 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.
  • Updated the "last unique Exxx error number used" comment to E259.
  • Added 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 reaches room_send with 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-magic hangs on import on this machine (libmagic loading), so it was replaced by a stub module on PYTHONPATH for the test run; everything else, including matrix-nio, is real.

  • pytest tests/test_event_stdin.py: 12 passed with 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).
  • flake8 reports no warnings on the changed lines of matrix_commander.py (the file has pre-existing warnings elsewhere). flake8 and black --check -l 79 are clean on the test file.

Not tested: a live run against a Matrix server.

Fixes #236

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
@Benjamin-Loison

Copy link
Copy Markdown
Collaborator

printf '\xff\xfe{}' does not look like a real usage, can you provide one? That is a not empty event.

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
@teeps-heisenberg

Copy link
Copy Markdown
Author

Thanks. A realistic case is an event JSON file saved as UTF-16, for example with Windows PowerShell 5.1's > redirection, which writes the FF FE byte-order mark, and 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, or matrix-commander --event - < event.json in cmd.exe. An event whose body was saved in Latin-1 (café with a raw \xe9 byte) fails the same way. I updated the PR description accordingly.

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 UnicodeDecodeError and passes with the fix. CI doesn't run pytest, so it isn't gating.

@Benjamin-Loison

Copy link
Copy Markdown
Collaborator

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
@teeps-heisenberg

Copy link
Copy Markdown
Author

Yes. I changed the stdin path to detect the encoding with json.detect_encoding, which is what json.loads itself uses for bytes input. UTF-8 (with or without BOM), UTF-16 and UTF-32 are now decoded and sent, so the PowerShell UTF-16 file works. Bytes that are none of those (e.g. Latin-1) still get the E259 error instead of a traceback, since guessing a legacy 8-bit encoding could silently send mangled text. Tests cover each encoding plus the Latin-1 case.

--event FILE is unchanged in this PR and still reads with the locale encoding; I can apply the same detection there in a follow-up if that is wanted.

@Benjamin-Loison

Copy link
Copy Markdown
Collaborator

--event FILE is unchanged in this PR and still reads with the locale encoding

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
@teeps-heisenberg

Copy link
Copy Markdown
Author

Done in the latest commit: --event FILE now reads the file as bytes and goes through the same json.detect_encoding decoding and E259 handling as stdin. The test file covers both paths (12 cases).

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.

@Benjamin-Loison

Benjamin-Loison commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator

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

@Benjamin-Loison

Copy link
Copy Markdown
Collaborator

https://chat.mistral.ai/work/e5eacde5-6f50-490f-8ce3-e1c77f64b4f3

json.detect_encoding

Since it's private-ish, prefer json.loads(raw_bytes) (which uses it for you) over calling detect_encoding directly in production code, as its behavior isn't guaranteed stable across Python versions.

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.
@teeps-heisenberg

Copy link
Copy Markdown
Author

Done: the raw bytes now go straight to json.loads, which detects UTF-8/16/32 (with or without a BOM) itself, so json.detect_encoding is no longer called. A UnicodeDecodeError from it is still reported as E259, and other parse errors stay E143. Input that decodes to nothing or only whitespace (for example a BOM-only file) is still dropped quietly; I added tests for that. All 18 tests in tests/test_event_stdin.py pass.

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.

*event: handle non-UTF-8 stdin pipe without traceback*

2 participants