test(platform): 修 Windows 上平台脆弱的断言(不削弱测试意图)
- trace/task: 亚 tick 耗时合法读到 0(本机时钟步长约 0.44ms),改为断言非负且 Duration == EndedAt-StartedAt - user/runtimeconfig: Windows 无法表示普通文件的 POSIX 权限位(Stat 恒 0666),0600 断言仅在非 Windows 生效,注释写明实现确实请求 0600 - exec: MSYS pwd 把同一目录渲染成 /tmp/...;改用「标记文件相对名解析」+「与显式 root 的 pwd 输出逐字相等」两条跨平台断言 + 变异验证 - 只读排查全仓同类脆弱断言,结果记入 DevDocs/工作小结
This commit is contained in:
1 parent
fbbfcc95ee
commit
29aa60d0eb
6 files changed
+89
-21
No files matched your search
@@ -144,20 +144,51 @@ func TestTerminal_Injection_Rejected(t *testing.T) {
|
||||
|
||||
func TestTerminal_WorkDir_DefaultToRoot(t *testing.T) {
|
||||
t.Parallel()
|
||||
requireBin(t, "pwd")
|
||||
root := t.TempDir()
|
||||
ex := newEnabledExecutor(t, enabledCfg("pwd", root))
|
||||
|
||||
res, err := ex.Execute(context.Background(), ExecRequest{Command: []string{"pwd"}})
|
||||
// Probe the default working directory without comparing rendered path
|
||||
// strings. On Windows the command may resolve to Git's MSYS coreutils, which
|
||||
// reports the Windows cwd it was handed as a POSIX path (the user Temp
|
||||
// directory comes back as /tmp/...), so a "Stdout contains root" check is not
|
||||
// portable. A marker file that exists only in the sandbox root, read through
|
||||
// a relative name, is found only when the process really runs in that root.
|
||||
const (
|
||||
probeFile = "cwd-probe.txt"
|
||||
probeBody = "cwd-probe"
|
||||
)
|
||||
root := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(root, probeFile), []byte(probeBody), 0o600); err != nil {
|
||||
t.Fatalf("write probe: %v", err)
|
||||
}
|
||||
requireBin(t, "cat")
|
||||
catEx := newEnabledExecutor(t, enabledCfg("cat", root))
|
||||
res, err := catEx.Execute(context.Background(), ExecRequest{Command: []string{"cat", probeFile}})
|
||||
if err != nil {
|
||||
t.Fatalf("Execute: %v", err)
|
||||
}
|
||||
realRoot := root
|
||||
if r, err := filepath.EvalSymlinks(root); err == nil {
|
||||
realRoot = r
|
||||
if res.ExitCode != 0 || res.Stdout != probeBody {
|
||||
t.Errorf("cat %s with no WorkDir: exit=%d stdout=%q stderr=%q, want %q read from the sandbox root %s",
|
||||
probeFile, res.ExitCode, res.Stdout, res.Stderr, probeBody, root)
|
||||
}
|
||||
if !strings.Contains(res.Stdout, realRoot) {
|
||||
t.Errorf("Stdout = %q, want contain %q", res.Stdout, realRoot)
|
||||
|
||||
// And the default must be the root itself, not a parent or a subdirectory:
|
||||
// pwd with no WorkDir must print exactly what pwd prints for WorkDir=root.
|
||||
// Both runs share pwd's own rendering, so the comparison stays platform
|
||||
// independent.
|
||||
requireBin(t, "pwd")
|
||||
pwdEx := newEnabledExecutor(t, enabledCfg("pwd", root))
|
||||
deflt, err := pwdEx.Execute(context.Background(), ExecRequest{Command: []string{"pwd"}})
|
||||
if err != nil {
|
||||
t.Fatalf("Execute pwd (default workdir): %v", err)
|
||||
}
|
||||
explicit, err := pwdEx.Execute(context.Background(), ExecRequest{
|
||||
Command: []string{"pwd"},
|
||||
WorkDir: root,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Execute pwd (WorkDir=%s): %v", root, err)
|
||||
}
|
||||
if got, want := strings.TrimSpace(deflt.Stdout), strings.TrimSpace(explicit.Stdout); got != want {
|
||||
t.Errorf("pwd with no WorkDir = %q, want the sandbox root as pwd renders it with WorkDir=%s: %q", got, root, want)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user