Skip to content

fix(expand): #843 — E400-escape user PUA codepoints in the patsub replacement pipeline - #853

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix/843
Oct 19, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
fix/843

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Closes #843. Based on master 8e9c7c0f (merge of origin/master brought in mid-task per the #577 discipline; re-verified on the merged tree). Not merged from my side.


Root cause

The patsub replacement pipeline's own markers (PATSUB_QUOTED_VALUE_START/END/AMP/BACKSLASH, U+E310-E313) share the registry zone with user data, and nothing escaped user zone glyphs in flight: the scan pass copied literal replacement chars raw, and finish_patsub_replacement resolved any bare E310-E313 — including the user's own — as pipeline protocol:

  • ${v/b/$'\uE312x'} → a&xc (E312 read as PATSUB_QUOTED_AMP, rendered through the \\& path)
  • E310/E311 → axc (consumed as quoted-value region toggles and dropped)
  • E313 → a\\xc (read as PATSUB_QUOTED_BACKSLASH)

Reproduced on release builds; ticket tri-verified. GNU 5.3.0 treats the replacement as its own word (subst.c:4462 expand_string_for_rhs → parameter_brace_patsub:9424-9444) and strcreplace (stringlib.c:180, flags 2 via pat_subst:9252) only touches a literal &/\&/\\ — a U+E3xx glyph rides verbatim. The four #838 PATSUB leak asserts were never the leak: they consume in flight, and the entry side (this fix) is what let bare user codepoints reach them.

Fix — the #735 E400 mechanism applied in flight (src/executor/expand_braced_replacement.rs)

  1. Scan side: every literal registry-zone char the marking pass copies now goes through push_replacement_literal → markers::push_literal_char (E400 prefix): the main other arm, '...' spans, double-quoted spans, backslash escapes, and the literal next-char after an unexpandable $. The raw-byte introducer E000 stays verbatim (it owns the pair protocol; an E400 prefix would split a pair — the [bug] Assignment RHS: control-byte var adjacent to live $() leaks U+E000+U+E0xx carrier pair into stored value (argument position clean) — corrupts OMB binaryanomaly/rjorgenson PS1 #296 frozen-carrier class).
  2. ANSI-C spans: $'...' bodies escape typed zone glyphs at body collection; \\u-decoded codepoints are already pair-escaped by the canonical decoder, so decoded content copies verbatim (re-escaping would double-encode the pair — one decode at the output boundary would then restore the wrong text).
  3. Finish side: finish_patsub_replacement copies E400+c and E000+payload pairs verbatim before resolving markers, so escaped user data can never be read as a live marker regardless of which entry arm produced it (inline $'...', stored-variable expansion, raw typed glyphs).
  4. fix(#824): decode-boundary leak asserts fired on user-literal marker text (HD1 in declare -p, guard in xtrace) #838 contract preserved: the golden leak asserts now probe strip_escape_pairs(output) — only LIVE markers assert; an escaped codepoint in the output is restored user data (${v/b/$'\uE312x'} emits U+E312). markers.rs: the four PATSUB markers register user_reachable: true with the in-flight escape contract documented; governance 3.2 table gains the patsub row.

GNU differential (WSL GNU bash 5.3.0, script-file oracle, byte-exact)

All forms byte-identical rubash-vs-GNU after the fix (master red on the PUA shapes):

form GNU 5.3.0 master this PR
${v/b/$'\\uE312x'} (and E310/E311/E313) a + glyph + xc a&xc / axc / a\\xc identical to GNU
${v/b/&$'\\uE312'} (match ref) ab + glyph + c ab&c identical to GNU
${v/b/\\&$'\\uE312'} a& + glyph + c a&&c identical to GNU
${w//b/$'\\uE312x'} (global) per-match glyph & corruption identical to GNU
rep=$$'\uE312x'; ${v/b/$rep} (storage pair path) glyph survives a&xc identical to GNU
raw bytes $'\\x10'..$'\\x13' literal byte ok / partial identical to GNU
literal typed glyphs (raw UTF-8 in body / in '...') glyph & identical to GNU
declare -p / printf %q / echo double-echo glyph once stray E400 identical to GNU
shopt patsub_replacement off glyph + literal & & identical to GNU
E400/E401 zone edges, E400+\&, \q+glyph adjacency GNU shapes diverged identical to GNU
\\1 reference forms a1c etc. ok identical to GNU (stays green)

Oracles: oracle843.sh (15 forms), oracle843b.sh (11), oracle843c.sh (3) — run as cat script | wsl.exe bash -s, output od-compared byte-for-byte.

Tests

Full dual-direction suite (--no-fail-fast --test-threads=1)

tree passed failed failure set vs base
base b79e678 5551 36 — (34 unique, environment baseline)
fix on b79e678 5562 37 identical + 1 concurrency flake (windows_listing_cache…, green serially on both trees)
fix on merged 8e9c7c0 5568 37 identical + profiler_regressions fixture_text_tree — reproduces on pure 8e9c7c0 (5/6 runs red there, 4/5 on the branch): pre-existing master-side #815 report-ordering flake, unrelated to this change (no src delta between the trees)

Zero new failures, zero lost fixes; local environment failures within the known ~37-44 baseline band.

fix-agent and others added 3 commits October 19, 2026 13:31
…t pipeline (rubash#843)

The replacement side of ${var/pat/rep} carries user data that may legally
be a registry-zone glyph: GNU 5.3.0 prints ${v/b/$'\uE312x'} as
a<E312>xc (subst.c:4462 expand_string_for_rhs feeds the replacement through
expand_word_internal as its own word; strcreplace stringlib.c:180 flags-2
only touches a literal &/\&/\\). rubash's pipeline consumed bare
E310-E313 as its own PATSUB markers — E312 rendered as '&' through the
\\& path, E310/E311 dropped as quoted-value region toggles, E313 as '\\'.

Fix (the #735 mechanism applied in flight): the scan pass E400-escapes
every literal registry-zone char it copies (push_replacement_literal —
replacement body, '...' spans, dquote spans, backslash escapes, literal
next-char after $); the ANSI-C $'...' span escapes typed glyphs at body
collection; finish_patsub_replacement copies E400/E000 pairs verbatim
before resolving markers, and its #838 leak probe (strip_escape_pairs)
asserts only LIVE markers — an escaped codepoint in the output is restored
user data. The raw-byte introducer E000 stays verbatim (owning the pair
protocol; an E400 prefix would split a pair, the #296 frozen-carrier
class). markers.rs: PATSUB family documented user_reachable with the
in-flight escape contract; governance 3.2 table gains the patsub row.

Verification: WSL GNU 5.3.0 script-file oracles oracle843.sh/oracle843b.sh
(15+11 forms: E310/E311/E312/E313 + & and \\& refs + // global + rep-var
+ raw-byte payloads + typed literals + declare -p + %q + shopt-off +
E400/E401 edges) byte-identical rubash-vs-GNU; new
tests/issue843_patsub_pua_escape_regressions.rs (12 cases, all failing on
master); marker_leak_golden 8/8; #838 issue824_marker_decode_boundary_
regressions 8/8; #323 patsub ANSI-C 11/11; lib 689/689; fmt clean;
RUSTFLAGS=-D warnings cargo check clean.
@caomengxuan666

Copy link
Copy Markdown
Member Author

独立代码评审:APPROVE-WITH-NITS

独立复核轮(自建 worktree @ dab4ad7,基线 worktree @ 8e9c7c0,全部验证实做)。无 BLOCKER。

核心审计(转义完备性)

  • 五类字面拷贝点穷尽性:逐行读 expand_braced_replacement.rs diff——主扫描 other 臂、'...' 跨度、双引号跨度(含 \X 臂内层)、非引用反斜杠 Some(_) 臂、$ 后字面字符臂,全部改走 push_replacement_literal;另有 $'...' 体收集期转义。未找到第六类漏网:$(...)/反引号/$var/~ 展开结果不经字面拷贝点,走 bytes_to_shell_text(comsub 捕获边界 874/949/1739 行)配对编码——我实跑了反引号与 $(printf '\xee\x8c\x92') 输出 PUA 进替换的形态,双侧字节一致。decode_ansi_c_quoted 仅在 \u 解码发射点 push_literal_char(ansi.rs:380 唯一调用点),透传字符不重复转义——无双重转义路径。
  • E000/E400/raw-byte 所有权边界:raw-byte 载荷 = E001+byte ≤ E100,结构上撞不到 E310..E313,finish 的 E000 臂是防御性正确;E000 原样拷贝是 [bug] Assignment RHS: control-byte var adjacent to live $() leaks U+E000+U+E0xx carrier pair into stored value (argument position clean) — corrupts OMB binaryanomaly/rjorgenson PS1 #296 冻结配对协议强制(E400 前缀会拆对)。与 markers.rs 3.2 表新行自洽;user_reachable 为文档/审计字段(无代码消费方),置 true 零行为风险。
  • 反向风险(过度转义/双重还原)实测:用户字面 E400、E400+E312 双字符序列、尾随 E400、E401、孤立 E000 全部探针——逐字符转义 + 输出边界单次解码在任意序列上无歧义,与 GNU 字节一致(除下述预存项)。存储值(已配对编码)经 $rep 进替换只解一次,符合语义。

GNU 语义与坐标

  • vendored bash 源码核实:strcreplace 确在 stringlib.c:180,flags-2(escape_backslash)经 pat_subst subst.c:9252 strcreplace(rep,'&',mstr,2);&/\&/\\ 之外字符透传,PUA 为数据。parameter_brace_patsub 9424-9444、quote_string_for_repl 3891 均核实。
  • oracle 双侧对照:我另拟 54 形态(超出代理所列:E310-E313、&/\&/\1、// 全局、rep 变量、raw 0x10-0x13、declare -p/%q、shopt 关、nocasematch、@/* 位置参数与数组、嵌套 patsub、反斜杠+裸字形、未识别 \q 邻接、E400/E401 区缘、直打字形、双引号内 $'...' 不解码、comsub/反引号 PUA)——WSL GNU bash 5.3.0 vs rubash 逐字节一致 53/54。
  • 核心形红/绿:master 二进制实测 a&xc/axc/axc/a\xc,PR 侧四个全为正确 UTF-8 字形。

回归

  • 新 12 例全绿;marker_leak_golden 8/8、issue824 8/8、issue323 11/11、issue735 5/5、lib 689/689——与主张一致。
  • 全量 --test-threads=1 双侧(base=8e9c7c0f vs head=dab4ad71):失败集逐名恒等(23 例,全为预存 Windows 环境依赖失败,非本 PR 引入)。
  • cargo fmt --check 干净;RUSTFLAGS=-D warnings lib/bins 两侧均 0。
  • 夹带扫描:3 提交文件集与信息一致;merge 4348cd0 三文件 blob 与 master 侧完全一致、merge-tree 双侧零改动,无 evil-merge;GPL 仅坐标引用,无代码拷贝。

NITS(非阻塞)

  1. 坐标引偏:CHANGELOG 与测试头写 "subst.c:4462 expand_string_for_rhs"——patsub 替换实际走 expand_string_for_patsub(subst.c:3950,parameter_brace_patsub:9433 调用);4462 是 =/:= 赋值 RHS 展开器。语义论证不受影响(同一 expand_word_internal 机制),建议后续顺手改引。
  2. -D warnings 主张口径:lib/bins 干净属实,但 --all-targets 在 master 上本就有 issue528/issue282/issue651 等预存 dead-code 报错(本 PR 未触碰,双侧恒等)。主张宜限定作用域。
  3. 预存分歧(顺带记录,非本 PR 引入,基线二进制实测前后逐字节一致):替换文本中孤立的字面 U+E000(非成对)会按畸形 raw-byte 对解码(rubash $'a?c' vs GNU a<E000>xc)——E000 所有权协议([bug] Assignment RHS: control-byte var adjacent to live $() leaks U+E000+U+E0xx carrier pair into stored value (argument position clean) — corrupts OMB binaryanomaly/rjorgenson PS1 #296)下用户字面 E000 不可表示的已知类,若尚无 tracking 值得单开 issue。

结论:APPROVE-WITH-NITS。机制正确、边界自洽、验证扎实,三条 nit 均为文档/记录层面,不阻塞合并。

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.

patsub 替换文本的用户 PUA 码点未 E400 转义,与 PATSUB 标记碰撞(release 可复现)——#838 CR NIT-3

1 participant