fix(shell): zsh history makes --json output invalid UTF-8
维护者通常 3 天内回复
@rudraa2005 已经在做这个了。
开始于 2026年10月23日。
评估
调研方向
Start by reproducing the zsh history case from the issue, then read genShellHistoryFromFile in osquery/tables/system/posix/shell_history.cpp and the JSON path through osquery/devtools/printer.cpp, osquery/core/sql/row.cpp and osquery/utils/json/json.cpp. Done means the osqueryi --json output for the single emoji entry parses as UTF-8 JSON, and the ASCII control still returns echo ASCII. The issue leaves the byte representation to maintainers, so agree on that approach first.
由索引模型根据 Issue 内容生成。
描述
Summary
Querying shell_history.command with osqueryi --json can produce bytes that are not valid UTF-8, so a JSON consumer rejects the entire result. A synthetic history entry written and successfully read back by zsh reproduces the failure. The history contains only echo 😀; no real shell history is needed.
Steps to reproduce
What steps did you take to reproduce the issue?
Use the release baseline below in a disposable Linux environment with zsh and Python installed. Run these commands as a fresh, non-root test user whose home has no existing history. The first command creates that test user's history file.
LC_ALL=C.UTF-8 zsh -fic 'HISTSIZE=10; SAVEHIST=10; HISTFILE=$HOME/.zsh_history; print -s -- "echo 😀"; fc -W'
# zsh reads the same entry back correctly:
LC_ALL=C.UTF-8 zsh -fic 'HISTSIZE=10; fc -R $HOME/.zsh_history; fc -ln 1'
# Inspect the bytes that zsh wrote:
python3 -c 'from pathlib import Path; print((Path.home()/".zsh_history").read_bytes().hex(" "))'
osqueryi --json 'select command from shell_history' > result.json
python3 -c 'import json; print(json.load(open("result.json", encoding="utf-8")))'
For a passing control, replace the synthetic history with an ASCII entry using the same first command with echo ASCII, then repeat the query and JSON load. The failure also reproduced with both entries present before reduction to the single emoji entry.
Expected behavior
What did you expect to see?
The documented --json output mode should emit valid JSON that a UTF-8 JSON consumer can read. Querying a valid zsh history file should not make the whole result undecodable. Table columns may contain raw bytes, so this report leaves the representation of those bytes and the appropriate implementation layer to the maintainers.
Actual behavior
What did you see instead?
zsh reads the history back as echo 😀. Its on-disk command bytes are metafied, and osquery emits those same bytes inside the JSON string. The query exits successfully, but Python rejects the output.
History file hex:
65 63 68 6f 20 f0 83 bf 83 b8 80 0a
osqueryi stdout shown as a Python bytes literal:
b'[\n {"command":"echo \xf0\x83\xbf\x83\xb8\x80"}\n]\n'
Python JSON load:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf0 in position 21: invalid continuation byte
ASCII-only control:
[{'command': 'echo ASCII'}]
Selecting hex(command) as command_hex instead produced valid JSON with 6563686F20F083BF83B880. This preserves the raw command bytes for a consumer that explicitly decodes them, but requires changing the query and consumer.
Evidence
- Expected source: The shell documentation describes
--jsonas JSON output in docs/wiki/introduction/using-osqueryi.md. RFC 8259 section 8.1 specifies UTF-8 for JSON exchanged between systems. - Failure source: The executed synthetic-history reproduction and byte dump above show successful query execution followed by a UTF-8 decoding failure; the ASCII control loads successfully.
- Evidence provenance: observed
- Local verification: reproduced
- Reproduction completeness: complete
Current master was inspected in a fresh clone. The entire osquery/tables/system/posix/shell_history.cpp file is identical to the release tag: genShellHistoryFromFile copies either the full line or the timestamp-stripped command into a string column. The source path continues through jsonPrint in osquery/devtools/printer.cpp, serializeRowJSON in osquery/core/sql/row.cpp, and JSON::toString in osquery/utils/json/json.cpp, which uses a default RapidJSON writer and reports success. This is a source-level check of the current branch, not an executed master build.
Merged PR #4617 explains that table columns may contain bytes as-is and fixed Python Thrift decoding for such data. It did not change the shell JSON output path. That distinction is why the failed contract here is JSON output validity, rather than a prescribed requirement to decode zsh's storage format inside the table.
Restoration check
Repeat the single-entry reproduction and parse the whole result as UTF-8 JSON. The output should contain a usable representation of the row rather than fail decoding or silently discard it. Repeat the ASCII control and verify that its command remains echo ASCII. Keep the input as history actually written by zsh; replacing it with hand-written UTF-8 would bypass the trigger.
Runtime or environment
What operating system and version are you using?
version = 24.04.5 LTS (Noble Numbat)
build =
platform = ubuntu
Linux arm64, in an isolated Docker container without privileged mode. Both zsh and osquery ran as a fresh non-root test user. zsh reported 5.9 (aarch64-unknown-linux-gnu); the locale was C.UTF-8.
What version of osquery are you using?
version = 5.23.1
This executed release came from the official Linux arm64 .deb GitHub release asset. It was the latest published release when checked before reproduction.
The current default branch was inspected at master commit 9a7a144c9183634dd50af32e7b080c37613e6708. No current-branch binary was built or run.
Triage assessment
- Impact level: P2
- Assessment status: supported
- Impact basis: One valid zsh history entry makes the whole JSON result unreadable by a strict UTF-8 JSON consumer. The failure is bounded to data with affected byte sequences; the ASCII control succeeds.
- Workaround status: available
- Workaround basis: Selecting
hex(command)was executed and produced valid JSON while preserving the raw bytes. Consumers must change the query and explicitly decode the hex and zsh representation to recover command text.
- 主要语言
- C++
- 星标
- 23.8k
- 派生
- 2.7k
- 平均合并
- 6 天 11 小时
- 30 天内合并 PR
- 69
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
osquery/osquery 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 74/100
维护者通常 3 天内回复
-
usb_devices: usb_address and usb_port are wrong for bus/device numbers 8–99 on Linux (zero-padded udev values parsed as octal)可能已有人在做 @mjatin2007 于 6 天前认领。 未关闭Linux virtual tables
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 3 天内回复
-
Update the expired Slack invite link in README可能重新可做 @ayushkrsingh946 于 40 天前认领,目前没有进行中的 PR。 未关闭maintainer task
难度 1/5 1 小时以内 新手友好度 68/100
osquery/osquery#9215 · 4 条评论 ·
维护者通常 3 天内回复
-
fix(docs): debugging guide incorrectly says invalid configuration blocks daemon startup可能已有人在做 @nightcityblade 于 54 天前认领。 未关闭configuration documentation
难度 2/5 1-3 小时 新手友好度 72/100
osquery/osquery#9143 · 1 条评论 ·
维护者通常 3 天内回复
-
scheduled_tasks.hidden returns the task's Enabled state instead of Hidden (Windows)可能已有人在做 @a-bonfim-tech 于 67 天前认领。 未关闭bug triage virtual tables Windows
难度 2/5 1-3 小时 新手友好度 86/100
维护者通常 3 天内回复
相似的 Issue
-
Make the HTTP server's keep-alive and headers timeouts configurable可能已有人在做 @mattgodbolt 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 76/100
compiler-explorer/compiler-explorer#9902 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
Nixtla/statsforecast#1371 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
[Bug]: pip install -U cudaq-qec fails with a conflict that names cudaq-qec itself可能已有人在做 @fedonman 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
task
难度 2/5 1-3 小时 新手友好度 76/100
SparseLinearAlgebra/spla#316 · 1 条评论 ·
维护者通常 1 天内回复