Files
larksuite__cli/shortcuts/sheets/csv_put_guard_test.go
sang-neo03 d12b39cf46 feat(vfs): allow absolute paths under a built-in path policy (#2580)
* feat(vfs): allow absolute paths under a built-in path allowlist

Path flags only accepted paths relative to the working directory, so an
agent passing a full path (typically under /tmp) failed on its first call
and had to retry with a relative one.

Absolute paths are now accepted when they resolve inside a built-in
allowlist: the working directory, /tmp, and ~/files. A built-in denylist
covers system and credential locations and wins over the allowlist,
including over the working directory. Both lists are compiled in and read
no environment variable, flag, or config file, so the effective policy is
fixed by the binary; upgrading is all it takes for the new behavior to
apply.

Containment is decided by file identity (device and inode) alongside the
resolved name, because a single directory has many spellings: APFS folds
U+017F onto "s", so ".sshh" spelled with it opens ~/.ssh, and NTFS and
APFS both compare case-insensitively.

Reads are hardened where the policy applies: O_NOFOLLOW pins the final
component, O_NONBLOCK keeps a FIFO from blocking before it can be
refused, and the opened descriptor is matched against the inspected
object, rejected when it is not a regular file, and rejected when it
carries extra hard links. The relaxed local-input tier used by apps
upload keeps its own contract (symlinks are legitimate arguments there)
and gains the denylist check instead.

Two behaviors are deliberate rather than incidental. Working inside a
denylisted directory now refuses even relative paths, since the denylist
is unconditional. Running as root leaves only the working directory and
/tmp, because the home directory is then /root, itself a deny root.

Existing tests asserted the old "every absolute path is refused"
baseline; they now assert the allowlist. Traversal fixtures escape to the
filesystem root, which stays outside every allowed root on Linux, where
the temp directory that hosts t.TempDir() is /tmp itself.

* fix(vfs): close two paths around the built-in denylist

A "~/..." argument had two readings: validation expanded it to the home
directory, while a caller that keeps the original string — SafeLocalFlagPath
returns it verbatim — opens whatever "~" names in the working directory. A
symlink there carried reads past the denylist, confirmed by reading
/etc/passwd through it. Every interpretation of an argument is now checked,
so the shorthand still reaches ~/files while the literal entry cannot
escape.

With no LARKSUITE_CLI_CONFIG_DIR and no reachable home directory,
core.GetBaseConfigDir keeps credentials in a bare ".lark-cli" resolved
against the working directory, which is an allow root. That fallback is now
mirrored as a deny root, so containers whose home lookup fails do not expose
their stored tokens.

* fix(vfs): enforce hard-link checks across readers

* fix(vfs): stop an output hard link from rewriting a file outside the allowlist

A hard link has no target for name resolution to follow, so a link inside an
allowed root looked like an allowed destination while sharing its inode with a
file outside every root. A caller that truncated the approved name in place
rewrote that outside file: `auth qrcode --output <link>` reported success and
replaced a 43-byte JSON file outside the allowlist with its PNG.

Output validation now refuses an existing target that carries more than one
name, which covers callers that write directly, and auth qrcode commits
through a temp file and a rename, which replaces the directory entry and
leaves the other names alone. Writers already going through FileIO.Save were
never affected, since that path has always committed by rename.

* fix(vfs): give the hard-link refusal a workable recovery hint

The message told the caller to copy the file into an allowed directory, which
answers a question they did not ask: the file that triggers this is normally
already inside one, with every one of its names there too. It now states what
the check actually cannot do — enumerate the other names a file is reachable
by — and offers the step that works, which is to copy the file and use the
copy.

* test(vfs): pick the denylist fixture for the platform under test

Two tests reached for "/etc/passwd" as a denylisted absolute path. That path
is not absolute on Windows, so one test met the foreign-path rejection instead
of the denylist it was asserting, and the other saw the path joined to the
working directory and no rejection at all. Both now ask for a deny root that
exists on the platform running them — the credential directories under the
account home qualify everywhere — which keeps the denylist covered on Windows
rather than skipping it there.

Verified on Windows 10.0.19045 by running the package's test binary from this
branch and from main: main passed, this branch failed these two, and both pass
after the change. The other packages this branch touches were compared the
same way and their Windows results are identical on both sides.

* fix(vfs): state the hard-link check as the condition it tests

The check read as "bail out unless the target can be inspected", which
nilerr reads as an error swallowed on the way out. It now names the case it
acts on — an existing regular file with more than one name — and the comment
carries what the early return used to imply: a target that cannot be
inspected has no link count to judge, and the write layer reports the real
failure with proper typing.

* docs(vfs): scope the policy's environment claim to what holds

The header promised that neither list accepts runtime input and that no
caller controlling the environment can widen them. Two inputs contradict
that: LARKSUITE_CLI_CONFIG_DIR contributes a deny root, and where the account
database cannot name the running uid, $HOME decides where ~/files points —
reproduced in a container running as an unregistered uid, which wrote into a
directory the environment chose.

The comments now state the preference and its boundary rather than a
guarantee, and record what the boundary costs: a directory named "files"
under the named path, with the home directory itself still outside the
allowlist and every candidate home still carrying the credential deny roots.
The trustedHome note also said the pure-Go lookup falls back to $HOME
silently; it does so only when $USER is set as well, and returns an error
otherwise, which drops the ~/files root instead of moving it.

No behavior change.

* fix(auth): keep the mode of a QR output file that already exists

Committing the QR write by rename fixed a hard link from rewriting a file
outside the allowlist, but it also changed what happens to the target's mode.
A rename installs the temp file's inode, mode included, where the previous
in-place write left the existing file's mode untouched. Overwriting a target
the caller had restricted to 0600 therefore published it as 0644.

The mode now comes from the file already at the path; only a path with
nothing at it takes the default. Verified against main, which preserved 0600
here, and covered by a test that fails when the fixed mode is restored.

* test(sheets): move the csv file-alias tests onto the new path baseline

Merging main brought #2559's tests for the --file → --csv alias, written
against the policy this branch replaces. Two of them fail on it, both because
the verdict they describe moved rather than disappeared.

The out-of-tree case used /tmp, which the allowlist now accepts, so the value
came back as a missing file instead of an out-of-tree one; it now names a path
no allow root can contain. The directory case is refused when the descriptor
is inspected, before a read is attempted, so the message reads "not a regular
file". What the caller sees of both — the flag named, the cause kept, stdin
offered — is unchanged.

That message listed the kinds it refuses and omitted directories, which is how
it reached a directory test reading as a mismatch. It now names them.

* fix(im): let the path policy judge a download target

`+messages-resources-download` refused an absolute --output before the shared
policy saw it, so the flag stayed relative-only after the policy learned to
accept full paths. It is the command behind 99% of a reported 1,189 download
path errors in one week, where 97.2% of first calls passed an absolute path
and every later success had switched to a relative one.

The shape checks are gone. Both call sites already hand the result to
ResolveSavePath, which applies the allowlist, the denylist and symlink
resolution, so refusing a shape here decided nothing the policy would not
decide better — an absolute path is now answered by where it points rather
than by how it is written.

The file-key checks stay, and they are what the batch caller relies on: it
embeds the key in the path, and a key carrying a separator is refused as a
malformed key, so a traversal cannot be built from one. Verified against a
real tenant: /tmp and ~/files now save, while ~/.ssh, /etc and a path outside
every root are still refused.

* test(im): pin the download output contract the policy now decides

The dry-run suite listed an absolute path among the values --output must
refuse. That held while the command rejected the shape itself; now that the
built-in policy decides, /tmp is an allowed root and the path is accepted, so
the case asserted a rule that no longer exists.

It is replaced by the two halves of the real contract: an absolute path
inside an allowed root reaches the request, and a path that resolves outside
every root — a parent escape from this working directory, or a denylisted
directory — is still turned down as a validation error naming --output.

* fix(vfs): hold a relative path to the working directory

Accepting /tmp as an allow root gave a relative path somewhere new to go.
A process whose working directory sits under /tmp — CI runners, containers
and agent sandboxes commonly arrange that — could climb out with "../" and
still satisfy the allowlist, because the sibling it landed in was also under
/tmp. /tmp is world-writable, so that sibling can belong to another user or
another session, and the write side commits by rename, which replaces an
existing target unconditionally. The previous policy refused this: it
required every resolved path to stay under the working directory.

Naming a full path and climbing out of the working directory are different
acts and no longer share one verdict. An absolute path is judged by the
allowlist, which is what this branch set out to allow; a relative one has to
resolve inside the working directory, whatever wider root contains it.

The home denylist grows at the same time and for the same reason: the working
directory is an allow root and running from the home directory is ordinary,
so a credential store there is reachable by a relative name unless the list
covers it. It now names the common ones — netrc, git and shell credentials,
kube, docker, azure, gh, gcloud, the language package registries — and the
shell histories, which carry pasted keys as reliably as a credential file.

---------
2026-09-01 22:18:29 +08:00

298 lines
12 KiB
Go

// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package sheets
import (
"os"
"strings"
"testing"
"github.com/larksuite/cli/internal/cmdutil"
_ "github.com/larksuite/cli/internal/vfs/localfileio"
"github.com/larksuite/cli/shortcuts/common"
"github.com/spf13/cobra"
)
func newCSVGuardRuntime(csvVal string) *common.RuntimeContext {
cmd := &cobra.Command{Use: "test"}
cmd.Flags().String("csv", "", "")
cmd.ParseFlags(nil)
cmd.Flags().Set("csv", csvVal)
return &common.RuntimeContext{Cmd: cmd}
}
// TestGuardCSVValueIsNotFilePath covers the existing-file tier: a bare --csv
// value naming a real file is a forgotten "@". The prescription names the fix
// with a <path> placeholder — the untrusted value must not be spliced into
// command-shaped text an agent would copy verbatim.
func TestGuardCSVValueIsNotFilePath(t *testing.T) {
dir := t.TempDir()
cmdutil.TestChdir(t, dir)
if err := os.WriteFile("data.csv", []byte("a,b\n1,2\n"), 0644); err != nil {
t.Fatal(err)
}
err := guardCSVValueIsNotFilePath(newCSVGuardRuntime("data.csv"))
ve := requireValidation(t, err, "existing file")
if !strings.Contains(ve.Message, `"data.csv"`) {
t.Errorf("message should name the offending value as data, got: %q", ve.Message)
}
if !strings.Contains(ve.Message, "--csv @<path>") {
t.Errorf("message should prescribe the @ form via placeholder, got: %q", ve.Message)
}
if strings.Contains(ve.Message, "@data.csv") {
t.Errorf("message must not splice the value into a command fragment, got: %q", ve.Message)
}
if ve.Param != "--csv" {
t.Errorf("param = %q, want --csv", ve.Param)
}
}
// TestGuardCSVValueIsNotFilePath_MissingButPathShaped covers the second tier.
// A path that doesn't resolve used to pass through and be written into the
// cell verbatim — a wrong value with a success exit code. The common source is
// an absolute path: `@` rejects those, so the caller drops the `@` and retries.
// Since the file can't be read from cwd, the prescription is stdin.
func TestGuardCSVValueIsNotFilePath_MissingButPathShaped(t *testing.T) {
dir := t.TempDir()
cmdutil.TestChdir(t, dir)
for _, v := range []string{
"nope.csv", // relative path from another working directory
"./missing.csv", // explicit relative prefix
"../sibling/x.tsv", // parent-relative
"/tmp/nope.csv", // absolute — the `@`-rejected case
"~/data.tsv", // home-relative
"/var/tmp/export", // no extension, but an unmistakable path prefix
"C:/Users/me/a.csv", // windows-style, still ASCII path shape
} {
err := guardCSVValueIsNotFilePath(newCSVGuardRuntime(v))
ve := requireValidation(t, err, "looks like a file path")
if !strings.Contains(ve.Hint, "--csv @") || !strings.Contains(ve.Hint, "--csv - <") {
t.Errorf("value %q: hint should offer both @file and stdin, got: %q", v, ve.Hint)
}
// The untrusted value must never appear inside the command-shaped
// hint: "--csv - < $(id).csv" copied by an agent would expand in a
// POSIX shell. The value is only named as quoted data in the message.
if strings.Contains(ve.Hint, v) {
t.Errorf("value %q: hint must not splice the raw value into a command fragment, got: %q", v, ve.Hint)
}
if !strings.Contains(ve.Message, v) {
t.Errorf("value %q: message should still name the offending value, got: %q", v, ve.Message)
}
}
}
// TestGuardCSVValueIsNotFilePath_SkipsResolvedInput pins the origin rule that
// makes the shape heuristic safe: a value that arrived via @file / stdin is
// never inspected, however path-shaped its content — so the hint's promise
// that stdin writes such text verbatim actually holds, and a correct
// `--csv @file` invocation can't be re-rejected for its content.
func TestGuardCSVValueIsNotFilePath_SkipsResolvedInput(t *testing.T) {
dir := t.TempDir()
cmdutil.TestChdir(t, dir)
if err := os.WriteFile("data.csv", []byte("a,b\n1,2\n"), 0644); err != nil {
t.Fatal(err)
}
for _, v := range []string{
"nope.csv", // path-shaped, missing — rejected when inline
"data.csv", // names an existing file — rejected when inline
} {
rctx := newCSVGuardRuntime(v)
common.TestMarkInputResolved(rctx, "csv")
if err := guardCSVValueIsNotFilePath(rctx); err != nil {
t.Errorf("resolved value %q must skip the guard, got: %v", v, err)
}
}
}
// TestGuardCSVValueIsNotFilePath_PassesThrough pins what must still reach the
// sheet untouched. The prose cases are why the guard checks a narrow shape
// instead of "contains a filename": an earlier name-shape heuristic rejected
// them. "N/A" and "README.md" pin the two narrowing rules — a slash alone is
// not a path, and a filename alone is not a CSV path.
func TestGuardCSVValueIsNotFilePath_PassesThrough(t *testing.T) {
dir := t.TempDir()
cmdutil.TestChdir(t, dir)
for _, v := range []string{
"改完记得更新config.json", // CJK prose ending in a filename
"remember to update data.csv", // prose mentioning a file
"a,b\n1,2", // multi-cell CSV
"hello world",
"N/A", // slash, but no CSV extension and no path prefix
"README.md", // filename shape, not a CSV one
"report 2026.csv", // has a space: content, not a path
"",
} {
if err := guardCSVValueIsNotFilePath(newCSVGuardRuntime(v)); err != nil {
t.Errorf("content %q must pass through, got: %v", v, err)
}
}
}
// newCSVFileAliasRuntime is newCSVGuardRuntime plus the record chainFlagAliases
// leaves when the value was typed as --file.
func newCSVFileAliasRuntime(csvVal string) *common.RuntimeContext {
rctx := newCSVGuardRuntime(csvVal)
rctx.Cmd.Annotations = map[string]string{aliasSourceAnnotation("csv"): "file"}
return rctx
}
// TestResolveCSVPathFromFileAlias covers the value-side half of the file → csv
// alias: --file names a path, so a value naming a readable file is read like
// `--csv @<path>` instead of being written into the sheet as literal text.
func TestResolveCSVPathFromFileAlias(t *testing.T) {
dir := t.TempDir()
cmdutil.TestChdir(t, dir)
if err := os.WriteFile("data.csv", []byte("a,b\n1,2\n"), 0644); err != nil {
t.Fatal(err)
}
t.Run("reads the named file", func(t *testing.T) {
rctx := newCSVFileAliasRuntime("./data.csv")
if err := resolveCSVPathFromFileAlias(rctx); err != nil {
t.Fatalf("resolve: %v", err)
}
if got, _ := rctx.Cmd.Flags().GetString("csv"); got != "a,b\n1,2\n" {
t.Errorf("--csv = %q, want the file contents", got)
}
})
t.Run("the contents are marked source-resolved", func(t *testing.T) {
// Contents read from a file may legitimately look like anything,
// including a path — the shape guards downstream must see the same bit
// they would for --csv @<path>, or a one-cell CSV holding "report.csv"
// is rejected as a caller who forgot the @.
if err := os.WriteFile("pathshaped.csv", []byte("report.csv\n"), 0644); err != nil {
t.Fatal(err)
}
rctx := newCSVFileAliasRuntime("./pathshaped.csv")
if err := resolveCSVPathFromFileAlias(rctx); err != nil {
t.Fatalf("resolve: %v", err)
}
if !rctx.InputResolvedFromSource("csv") {
t.Error("the rewritten value must be marked as read from a source")
}
if err := guardCSVValueIsNotFilePath(rctx); err != nil {
t.Errorf("path-shaped file contents must survive the guard, got: %v", err)
}
})
// The fixture is a path no allow root can contain. /tmp used to serve here
// and no longer does: the built-in path policy accepts it, so a value under
// it is answered as a missing file rather than an out-of-tree one.
t.Run("an out-of-tree path is rejected toward stdin", func(t *testing.T) {
err := resolveCSVPathFromFileAlias(newCSVFileAliasRuntime("/outside-every-allow-root/data.csv"))
ve := requireValidation(t, err, "outside the built-in allowlist")
if ve.Param != "--file" {
t.Errorf("param = %q, want the flag the caller actually typed", ve.Param)
}
if ve.Cause == nil {
t.Error("the underlying path error should be preserved as Cause")
}
if !strings.Contains(ve.Hint, "--csv - <") {
t.Errorf("hint should offer stdin for a file outside the tree, got: %q", ve.Hint)
}
})
t.Run("inline CSV text still passes through", func(t *testing.T) {
// --file holding literal CSV was accepted before this rule existed;
// naming nothing readable, it stays the --csv guard's business.
rctx := newCSVFileAliasRuntime("a,b\n1,2")
if err := resolveCSVPathFromFileAlias(rctx); err != nil {
t.Fatalf("resolve: %v", err)
}
if got, _ := rctx.Cmd.Flags().GetString("csv"); got != "a,b\n1,2" {
t.Errorf("--csv = %q, want the value untouched", got)
}
if rctx.InputResolvedFromSource("csv") {
t.Error("a value that was not read from a file must not be marked resolved")
}
})
t.Run("a value typed as --csv is not re-read as a path", func(t *testing.T) {
// Without the alias record the rule must not fire: --csv promises text,
// and its own guard (not this one) answers a path passed to it.
rctx := newCSVGuardRuntime("./data.csv")
if err := resolveCSVPathFromFileAlias(rctx); err != nil {
t.Fatalf("resolve: %v", err)
}
if got, _ := rctx.Cmd.Flags().GetString("csv"); got != "./data.csv" {
t.Errorf("--csv = %q, want --csv left to its guard", got)
}
})
t.Run("@file and stdin values are already contents", func(t *testing.T) {
rctx := newCSVFileAliasRuntime("./data.csv")
common.TestMarkInputResolved(rctx, "csv")
if err := resolveCSVPathFromFileAlias(rctx); err != nil {
t.Fatalf("resolve: %v", err)
}
if got, _ := rctx.Cmd.Flags().GetString("csv"); got != "./data.csv" {
t.Errorf("--csv = %q, want a resolved value left alone", got)
}
})
}
// TestResolveCSVPathFromFileAlias_UnreadablePaths pins that every unreadable
// path is answered under --file, the flag the caller typed. Handing these to
// the --csv guard instead named the wrong flag and, for a file that exists but
// cannot be opened, prescribed "pass it with @" — advice that routes through
// this very reader and fails identically.
func TestResolveCSVPathFromFileAlias_UnreadablePaths(t *testing.T) {
dir := t.TempDir()
cmdutil.TestChdir(t, dir)
t.Run("a path-shaped value that names nothing", func(t *testing.T) {
err := resolveCSVPathFromFileAlias(newCSVFileAliasRuntime("./typo.csv"))
ve := requireValidation(t, err, "names no file under the current directory")
if ve.Param != "--file" {
t.Errorf("param = %q, want --file", ve.Param)
}
if ve.Cause == nil {
t.Error("the underlying read error should be preserved as Cause")
}
})
t.Run("a file that exists but cannot be read", func(t *testing.T) {
if err := os.WriteFile("noread.csv", []byte("a,b\n"), 0o000); err != nil {
t.Fatal(err)
}
if _, err := os.ReadFile("noread.csv"); err == nil {
t.Skip("running with rights that ignore file modes")
}
err := resolveCSVPathFromFileAlias(newCSVFileAliasRuntime("./noread.csv"))
ve := requireValidation(t, err, "cannot read file")
if ve.Param != "--file" {
t.Errorf("param = %q, want --file", ve.Param)
}
if ve.Cause == nil {
t.Error("the underlying read error should be preserved as Cause")
}
if strings.Contains(ve.Hint, "@") {
t.Errorf("@file shares this reader, so it cannot be the fix; hint was %q", ve.Hint)
}
})
// A directory is now refused when the descriptor is inspected, before any
// read is attempted, so the verdict reads "not a regular file" rather than
// the read failure it used to surface. What the caller sees of it — the
// flag named and the cause kept — is unchanged.
t.Run("a directory", func(t *testing.T) {
if err := os.Mkdir("adir", 0o755); err != nil {
t.Fatal(err)
}
err := resolveCSVPathFromFileAlias(newCSVFileAliasRuntime("./adir"))
ve := requireValidation(t, err, "not a regular file")
if ve.Param != "--file" {
t.Errorf("param = %q, want --file", ve.Param)
}
if ve.Cause == nil {
t.Error("the underlying read error should be preserved as Cause")
}
})
}