fix(shell): zsh history makes --json output invalid UTF-8

未关闭
#9,137 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 3 天内回复

@rudraa2005 已经在做这个了。

开始于 2026年10月23日。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
25/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
cpp, sql, zsh
领域
cli, data

调研方向

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 内容生成。

描述

bug triage

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 --json as 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

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

osquery/osquery 的其他 Issue

查看 osquery/osquery 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。