beads/internal/config/permissions_open_linux.go
Kevin Glynn f4d496fed2
fix(worktree): repair permissive .beads/ after worktree create (GH#3593) (#3640)
* fix(worktree): repair permissive .beads/ after worktree create (GH#3593)

Call config.FixBeadsDirPermissions on the new worktree's .beads/ after a
successful git worktree add so tracked directories checked out at umask
defaults match bd's 0700 expectation (same pattern as bd init / GH#3391).

Tests: TestRepairWorktreeBeadsPermissions (!windows).

* fix(worktree): reject unsafe beads permission targets

Validate .beads with Lstat before automatic permission repair so symlinks and non-directories cannot be chmod targets.

Agent-Signature: codex-gpt-5.6-sol-medium on behalf of matt wilkie

* fix(config): make permission repair race-safe

Open .beads without following symlinks, verify descriptor identity, and chmod the opened directory so path swaps cannot redirect permission changes.

Agent-Signature: codex-gpt-5.6-sol-medium on behalf of matt wilkie

* fix(config): preserve secure repair across platforms

Isolate Linux descriptor primitives, keep wasm targets buildable, and repair unreadable directories through an O_PATH handle with fchmodat2.

Agent-Signature: codex-gpt-5.6-sol-medium on behalf of matt wilkie

* test(config): tolerate legacy Linux chmod support

Skip optional unreadable-directory coverage when fchmodat2 is unavailable and verify unsupported descriptor chmod leaves permissions unchanged.

Agent-Signature: codex-gpt-5.6-sol-medium on behalf of matt wilkie

* fix(config): satisfy errcheck on deferred dir.Close

Co-authored-by: Cursor <cursoragent@cursor.com>

---------

Co-authored-by: matt wilkie <maphew@gmail.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-14 13:14:14 -07:00

40 lines
1 KiB
Go

//go:build linux
package config
import (
"errors"
"os"
"golang.org/x/sys/unix"
)
func openBeadsDirHandle(path string) (beadsDirHandle, error) {
fd, err := unix.Open(path, unix.O_RDONLY|unix.O_CLOEXEC|unix.O_NOFOLLOW|unix.O_DIRECTORY, 0)
if err == nil {
return os.NewFile(uintptr(fd), path), nil
}
if !errors.Is(err, unix.EACCES) && !errors.Is(err, unix.EPERM) {
return nil, err
}
// O_PATH needs no read permission. Chmod uses fchmodat2 with AT_EMPTY_PATH,
// so the operation remains anchored to this descriptor rather than the path.
return openBeadsDirPathHandle(path)
}
func openBeadsDirPathHandle(path string) (beadsDirHandle, error) {
fd, err := unix.Open(path, unix.O_PATH|unix.O_CLOEXEC|unix.O_NOFOLLOW|unix.O_DIRECTORY, 0)
if err != nil {
return nil, err
}
return &linuxPathDirHandle{File: os.NewFile(uintptr(fd), path)}, nil
}
type linuxPathDirHandle struct {
*os.File
}
func (h *linuxPathDirHandle) Chmod(mode os.FileMode) error {
return unix.Fchmodat(int(h.Fd()), "", uint32(mode.Perm()), unix.AT_EMPTY_PATH)
}