Skip to content

[restricted] 库入口 ShellInvocation 仍硬拒 -r/--restricted(rc2 "invalid shell option name";#527/#560 未覆盖嵌入路径)(P2) #626

Closed

Description

@caomengxuan666

环境

  • 宿主:Niubash(嵌入 rubash,经 rubash::invocation::ShellInvocation::parse(..) + apply_to_executor(..),见 niubash src/main.rs:478,510)
  • rubash:origin/master 20268da4;另在已发布 Niubash 1.4.0(rubash e785c3c8f610)上同样复现 —— 两份一致,排除本地脏改动因素
  • 平台:Windows 复现;该路径与平台无关,Unix 宿主同理

最小复现

宿主 niu 走库入口(ShellInvocation):

$ niu -r -c 'echo ok'
bash: line 0: restricted: invalid shell option name
rc=2

$ niu --restricted -c 'echo ok'
bash: line 0: restricted: invalid shell option name
rc=2

对照(同一个执行器,只是入口不同):

$ niu -c 'set -r; cd C:/; echo after-cd'    # set -r 走 builtin 路径
niu: line 1: cd: restricted
after-cd
rc=0

$ rubash.exe -r -c 'echo ok'                 # 原生 bin 路径(#560 修的是这条)
ok
$ rubash.exe -r -c 'cd C:/; echo after-cd'
bash: line 1: cd: restricted
after-cd

结论:执行器本身支持 restricted(set -r 与原生 rubash.exe -r 都生效),只有库入口在 -c 之前把它硬拒成 rc2。

根因

  • src/invocation.rs:424-446:cli_flag_name('r') => Some("restricted");
    同文件 164-166 行 "--restricted" 也 push ("restricted", true) 进 shell_flags。
  • src/invocation.rs:238-245:apply_to_executor 对每个 shell_flags 先做
    if !executor.is_shell_option(name) { return Err(format!("{name}: invalid shell option name")); }
  • src/builtins/set/options.rs:12-130 的 SHELL_OPTIONS 按设计不含 restricted
    (该文件 114-117 行注释:GNU 中 restriction 是 flags.c change_flag 的独立 shell flag,
    不出现在 set -o 枚举与 SHELLOPTS 里)。
  • 于是 is_shell_option("restricted") == false → 硬拒;错误在 -c 之前冒泡,整条命令不执行。

#527 / PR #560 修的是原生 src/main.rs 的 argv 解析(-s 之后 -r 被静默吞掉那条)。
库入口 ShellInvocation 未被覆盖:tests/issue527_restricted_shell_entry.rs 只跑
CARGO_BIN_EXE_rubash(原生 bin),因此这条回归至今未暴露。

期望行为(GNU bash 5.3.0,WSL oracle,参见 #527)

bash -r -c 'echo ok'             # ok, rc 0,$- 含 r
bash --restricted -c 'echo ok'   # ok, rc 0
bash -r -c 'cd /'                # bash: line 1: cd: restricted

建议修复

在 apply_to_executor 里把 restricted 当独立 invocation flag 处理(像 login/posix 那样
绕开 set -o 表),或在校验分支对 restricted 直接
executor.set_shell_option("restricted", enabled)。set_shell_option 自身不做校验
(src/executor/public_accessors.rs:827),执行器侧已按 __RUBASH_SETOPT_restricted 生效
(src/executor/pipeline_exec.rs:3189 restricted_command_error、
src/executor/alias_set_builtins.rs:118、src/executor/shell_options.rs:750)。

回归测试建议

补一条走库入口的用例(ShellInvocation::parse(&["-r","-c", ...]) + apply_to_executor),
断言 is_shell_option 不再拦截 restricted、且受限模式下 cd 被拒;现有 issue527 只覆盖原生 bin。

Activity

  1. added
    bug
    gnu-baseline
    P2-medium
    on Oct 15, 2026
  2. caomengxuan666 commented on Oct 15, 2026

    @caomengxuan666
    MemberAuthor

    Fixed end-to-end by rubash PR #628 (7107279) and Niubash PR #363 (ffee8fb6), both merged after green CI.

    Library direct invocation tests 8/8, invocation units 9/9, existing native restricted entry tests 5/5. Niubash latest upstream without override: startup matrix 26/26, actual ConPTY tests 2/2, greeting/precmd boundary unit 1/1. Independent smoke checks for both -r and --restricted with -c echo ok now return rc=0 with stdout ok and empty stderr.

    The implementation preserves the GNU startup boundary: startup can cd and assign PATH, restrictions activate before hooks/user body, startup set -r is not cleared, and named -o restricted remains invalid. No host-side semantic workaround. The unrelated known background-job Ctrl-D timeout remains documented, not claimed fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

P2-mediumbuggnu-baseline

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions