Skip to content

fix(api): OptionsView recognizes flag-only shell options (restricted) — #2040 - #2148

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/2040-optionsview-flagonly
Oct 29, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/2040-optionsview-flagonly

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Fixes #2040 (found by the #668 watch face; niu's interim bypass was executor-direct writes, PR #668 310d5e2 — this restores the view as the sanctioned path).

Root cause

OptionsView::set (src/api/views.rs:378 @ d5c172a) dispatches on is_shell_option — membership in the set -o table (SHELL_OPTIONS, src/builtins/set/options.rs:12) — and falls through to the shopt table otherwise. GNU restricted lives in neither:

  • it is owned by the single-letter flag table: flags.c:182 { 'r', &restricted } in shell_flags[];
  • it is absent from the set -o enumeration: set.def:194-237 o_options[] (vendored tree @9c465866);
  • hence it never appears in set -o listings or SHELLOPTS.

The set builtin arms it directly through the letter branch (set -r → set_shell_option(env_vars, "restricted", ...), src/builtins/set.rs:327, no family gate), so the set machine was wider than the view's dispatch predicate: a legal flag-only name fell through both families, returned false, and changed nothing — silently.

Fix (issue disposition 1: let the view recognize flag options)

  • src/builtins/set/options.rs: new is_flag_only_shell_option() whitelist beside is_shell_option, naming the GNU class (shell_flags[] vs o_options[]); sole instance restricted.
  • src/api/views.rs OptionsView::set: flag-only names route through the same set_shell_option machinery the set -r builtin branch uses — no second write path — with GNU's one-way gate intact: change_flag refuses FLAG_OFF while the option is on (flags.c:231-233), so set("restricted", false) on a live restriction returns false with no state change; while unarmed it is a successful no-op (set +r writes 0 over 0).
  • src/executor/state_views.rs view_option_enabled: flag-only names read through the same key the executor's restricted-shell consumers consult, keeping get/set symmetric over the flag surface.
  • src/api/session.rs: Session::options doc mentions the flag surface.

Regression test — tests/issue2040_optionsview_flagonly_regressions.rs (7/7)

External-consumer style (rubash::api only):

Pin Result
set("restricted", true) arms, returns true; get → Some(true) green (pre-fix: false/None)
Arming is the live persona: cd refused (cd: restricted), PATH read-only (PATH: readonly variable) — maybe_make_restricted, shell.c:1278-1299, via the shared machinery green
set("restricted", false) while armed → refused, false, state intact (flags.c:231-233) green
Disarm while unarmed → successful no-op green
restricted stays out of list() and SHELLOPTS (that is what flag-only means) green
Both known families dispatch unchanged; unknown names still refuse green

A/B vs pristine (origin/master d5c172a)

  • Pristine run of the new suite: 1 passed / 6 failed (the single pass is the unchanged-families pin). With the fix: 7/7.
  • Environment baseline: CARGO_TARGET_DIR=D:/repo/nbbuild/pkg11/H3/target cargo test --release:
    • --lib: 1063 passed, 0 failed.
    • Adjacent integration targets green: shopt/set (issue1487, issue346_348, issue705, issue1941_1948), restricted (issue527, issue626, issue804), session threading/interrupt, posix roundtrip.
    • Full cargo test --release is blocked at pristine master by tests/typed_cell_coherence_fuzz.rs calling the #[cfg(debug_assertions)] Executor::verify_typed_cells_forced (variable_state.rs:962, fecc9f9) — pre-existing, unrelated to this branch.
    • Remaining failures are A/B-identical on pristine (none caused here): issue1306 (BASH_SOURCE in -c), session_api::build_run_and_state_views_round_trip (temp-cwd canonicalize), multi_session_isolation matrix, issue0171 family (std::os::unix — non-Windows compile).

Fmt disclosure

cargo fmt aborts with os error 206 (command line too long) in this environment (known hook precedent); the six touched files were formatted with direct rustfmt (rc 0), so --no-verify is used for the commit.

禁区 check: src/lexer/continuation.rs untouched; no carrier-byte changes; LF throughout; no elevation.

#2040

Root cause: OptionsView::set (src/api/views.rs:378) dispatches on
is_shell_option — membership in the `set -o` table (SHELL_OPTIONS,
src/builtins/set/options.rs:12) — and falls through to the shopt table
otherwise. GNU `restricted` lives in neither: it is a flag-only option,
owned by the single-letter shell_flags[] table (flags.c:182
{ 'r', &restricted }), absent from the `set -o` enumeration
(set.def:194-237 o_options[]) and from SHELLOPTS. The set builtin arms
it directly (`set -r` -> set_shell_option(env_vars, "restricted", ...),
src/builtins/set.rs:327, no family gate), so the set machine was wider
than the view's dispatch predicate: a legal flag-only name returned
false with no state change (rubash#2040, found by the #668 watch face;
niu worked around it by bypassing the view — PR #668 310d5e2).

Fix (the issue's disposition 1: let the view recognize flag options):
- src/builtins/set/options.rs: new is_flag_only_shell_option()
  whitelist beside is_shell_option, naming the GNU class
  (shell_flags[] vs o_options[]); sole instance `restricted`.
- src/api/views.rs OptionsView::set: flag-only names route through the
  same set_shell_option machinery the `set -r` builtin branch uses,
  with GNU's one-way gate intact (change_flag refuses FLAG_OFF while
  the option is on, flags.c:231-233): armed && !enabled returns false,
  no state change. No second write path.
- src/executor/state_views.rs view_option_enabled: flag-only names
  read through the same key the executor's restricted-shell consumers
  consult (shell_option_enabled), keeping get/set symmetric.
- src/api/session.rs: Session::options doc mentions the flag surface.

Invariants pinned by tests/issue2040_optionsview_flagonly_regressions.rs
(external-consumer style, rubash::api only; 7/7 green):
- set("restricted", true) arms and returns true; get reads Some(true)
  (pre-fix: false/None — A/B on pristine d5c172a fails 6/7).
- Arming is the live persona: `cd` refused ("cd: restricted") and PATH
  read-only ("PATH: readonly variable") immediately after arming
  (maybe_make_restricted, shell.c:1278-1299, via the shared machinery).
- set("restricted", false) while armed: refused, false, state intact
  (flags.c:231-233); while unarmed: successful no-op.
- `restricted` stays out of list() and SHELLOPTS (that is what
  flag-only means); both known families dispatch unchanged; unknown
  names still refuse.

Verification: CARGO_TARGET_DIR build, cargo test --release --lib
1063 passed; adjacent integration targets green (shopt/set/restricted/
session_threading/interrupt/posix-roundtrip etc.). Full
`cargo test --release` is blocked at pristine master by
tests/typed_cell_coherence_fuzz.rs calling the #[cfg(debug_assertions)]
Executor::verify_typed_cells_forced (variable_state.rs:962, fecc9f9) —
pre-existing, unrelated. Remaining failures A/B-identical on pristine:
issue1306 (BASH_SOURCE in -c), session_api::build_run_and_state_views_
round_trip (temp-cwd canonicalize), multi_session_isolation matrix,
issue0171 family (std::os::unix, non-Windows compile).

Fmt disclosure: `cargo fmt` aborts with os error 206 (command line too
long) in this environment (known hook precedent); the six touched
files were formatted with direct rustfmt (rc 0), so --no-verify is
used for this commit.

Fixes: #2040
@caomengxuan666
caomengxuan666 merged commit 8755dd7 into master Oct 29, 2026
11 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/2040-optionsview-flagonly branch October 30, 2026 01:41
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.

OptionsView::set 不能武装 flag-only 选项(restricted):名字合法却静默返回 false(#668 盯守中发现)

1 participant