grants

package
v1.5.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package grants is the single authorization table: which paths golder may read, write, and run side-effect tools in — and for how long.

Why one table. The same question used to be answered by three stores with three schemas: per-directory trust in $GOLDER_HOME/trust.json (a map of directory to a nullable bool), the sandbox roots in ~/.config/golder/permissions.toml (two string arrays), and two package-level registries the gates filled as they granted things. None could answer the whole question, which is why /grants list and /boundary list each had to point at the other. The model here is one row per path:

path      — cleaned absolute, covers the subtree unless Scope says otherwise
allow     — the capabilities this row grants
deny      — the capabilities this row refuses
lifetime  — remembered (on disk) or session (this process only)

Capabilities are not one axis. Execute is the odd one and the reason the model needs three bits rather than a read/write pair:

Read     — open or list the path
Write    — create, change or delete under it (implies Read)
Execute  — run side-effect tools with the path as their working directory

Execute is what trust.json always meant. Keeping it separate is what lets a migrated trust decision stay exactly as wide as it was: a directory trusted for running commands is not thereby a directory whose files may be rewritten from anywhere else, and conflating the two is how a permission store silently widens on upgrade.

A row may both grant and refuse. That is not a contradiction: writing roots and a trust refusal can name the same directory, and the honest reading is "may write here, may not execute here". Decisions are per capability and most-specific-wins, so a refusal on a child still beats a grant on its parent.

The store is safe for concurrent use: grants arrive from a driver goroutine (a dialog answer) while run goroutines resolve paths against the table.

Index

Constants

View Source
const (
	// ReadWrite is the pair a path grant carries.
	ReadWrite = Read | Write
	// Full is every capability, for callers that mean "no restriction here".
	Full = Read | Write | Execute
)
View Source
const CurrentVersion = 1

CurrentVersion is the on-disk schema version. A file written by a newer golder is refused outright rather than partially understood: reading half a permission store is worse than refusing to start with it.

Variables

This section is empty.

Functions

func AddReadRoot

func AddReadRoot(raw string, life Lifetime, source string) (string, error)

AddReadRoot records a directory grant for reading and returns its normalized form. It is the one way a read grant enters the table, so the validation cannot be skipped by a caller: a root too broad to be a boundary (the filesystem root, $HOME, $HOME's parent) is refused, which is what keeps a typo from turning into "read everything".

func AddWritableRoot

func AddWritableRoot(raw string, life Lifetime, source string) (string, error)

AddWritableRoot records a directory grant for reading and writing inside the sandbox, and returns its normalized form. Write carries Read (a file you may write but not read back is a half-failure), so one row answers both sides.

func Allows

func Allows(path string, cap Capability) bool

Allows consults the process table, returning false when there is none.

func DefaultPath

func DefaultPath(getenv func(string) string) string

DefaultPath returns the managed file: grants.json in the golder state root ($GOLDER_HOME, else ~/.golder) — beside config.toml, proxy.toml and trust.json, the same kind of thing: state golder writes on the user's behalf, in a file the user may still edit.

It sits in the state root rather than under ~/.config because the root is also what the sandbox denies (seatbelt.ProtectedGolderPaths): a permission store outside that set is a permission store the agent may be able to rewrite.

func Open

func Open(getenv func(string) string) (warnings []string, err error)

Open initializes the process table: it loads the managed file and, on a first run, folds the legacy stores into it. It reports the warnings a caller should print once (an unreadable legacy file, a failed migration write); they never abort a run, because losing an old grant is an inconvenience while refusing to start is not.

Calling it twice replaces the table, which is what a test wants and what a second call in production would be a bug about — hence the single documented call site in main.

func ReadRoots

func ReadRoots() []string

ReadRoots returns the subtree paths the table grants read access to, which is the form the sandbox and the read-root registry want.

It exists so the table can be the single source of truth without the legacy registries reading it directly: a caller that used to take permissions.ReadRoots() takes this instead, and the conversion (subtree-only, read-capable) happens in one place rather than at each call site.

func SetProcess

func SetProcess(s *Store)

SetProcess installs a table directly. It exists for tests and for a caller that built a table it wants to be the process one.

func WritableRoots

func WritableRoots() []string

WritableRoots returns the subtree paths the table grants write access to: the form the sandbox's writable registry wants.

Types

type Capability

type Capability uint8

Capability is one thing a grant may permit or refuse. The zero value permits nothing, so a capability set that was never filled in is inert rather than silently powerful.

const (
	// Read permits opening or listing the path.
	Read Capability = 1 << iota
	// Write permits creating, changing and deleting under the path. It implies
	// Read: writing a file that cannot be read back only produces confusing
	// half-failures, and every store this replaces agreed.
	Write
	// Execute permits running side-effect tools (bash, apply_patch) with this
	// path as their working directory. It is what a directory-level trust
	// decision has always meant, and it is deliberately not implied by Write:
	// being allowed to edit files somewhere is not the same as being allowed to
	// run arbitrary programs there.
	Execute
)

func ParseCapability

func ParseCapability(s string) (Capability, bool)

ParseCapability reads the capability spellings the command line accepts. It accepts combined forms in any order ("rw", "wr", "rwe") plus the long names of each bit, so both a person typing `/grants add /x rw` and a config file spelling it out are understood.

func (Capability) Has

func (c Capability) Has(want Capability) bool

Has reports whether c carries every capability in want.

func (Capability) String

func (c Capability) String() string

String renders the capability set in the spelling the dialogs and /trust print: the letters a person types, in a stable order, e.g. "rw" or "rwe".

type Decision

type Decision uint8

Decision is the answer for one (path, capability) question.

const (
	// Undecided means nothing in the table speaks to this path and capability.
	Undecided Decision = iota
	// Allowed means the most specific row grants it.
	Allowed
	// Denied means the most specific row refuses it.
	Denied
)

func (Decision) String

func (d Decision) String() string

type Entries

type Entries struct {
	// Remembered is the managed store.
	Remembered []Grant
	// Session is this process only.
	Session []Grant
}

Entries is the whole table, in the two layers /trust reports.

func (Entries) All

func (e Entries) All() []Grant

All returns every row from every layer, for a caller that wants one list.

type Grant

type Grant struct {
	// Path is a cleaned absolute path.
	Path string `json:"path"`
	// Allow is what this row grants.
	Allow Capability `json:"-"`
	// Deny is what this row refuses. A row may carry both; see the package
	// comment for why that is not a contradiction.
	Deny Capability `json:"-"`
	// Scope is Subtree or Exact.
	Scope Scope `json:"scope,omitempty"`
	// Lifetime is Session or Remembered.
	Lifetime Lifetime `json:"lifetime"`
	// Source records where a remembered row came from, for /trust's provenance
	// column: a legacy file, a dialog, or a command. Empty for session rows.
	Source string `json:"source,omitempty"`
}

Grant is one row of the table.

func Add

func Add(g Grant) (Grant, error)

Add records a grant in the process table and reports whether it could be persisted (Remembered) or only held for this session (Session). A grant with no table to record it in is dropped, and the caller is told.

func Refuse

func Refuse(path string, caps Capability, life Lifetime) (Grant, error)

Refuse records a refusal in the process table.

func (Grant) String

func (g Grant) String() string

String renders one row the way /trust prints it.

type LegacyPaths

type LegacyPaths struct {
	// Trust is $GOLDER_HOME/trust.json: directory → nullable bool.
	Trust string
	// Permissions is the retired ~/.config/golder/permissions.toml: the managed
	// writable and readable roots it used to hold.
	Permissions string
}

LegacyPaths are the stores to read when the unified table appears for the first time. Every field may be empty, which skips that source.

func LegacyPathsFor

func LegacyPathsFor(getenv func(string) string) LegacyPaths

LegacyPathsFor resolves the legacy locations from the environment, matching the paths the old stores themselves used.

type Lifetime

type Lifetime string

Lifetime is how long a grant lasts.

const (
	// Session grants die with the process and are never written to disk.
	Session Lifetime = "session"
	// Remembered grants are persisted and apply to every later run.
	Remembered Lifetime = "remembered"
)

type MigrateResult

type MigrateResult struct {
	// Imported are the rows the legacy stores produced.
	Imported []Grant
	// Warnings are non-fatal problems: an unreadable or malformed legacy file.
	// Losing an old grant is an inconvenience; refusing to start is not.
	Warnings []string
	// Wrote reports whether the managed file was written.
	Wrote bool
}

MigrateResult reports what a migration found and did, so the caller can tell the user what changed instead of silently rewriting their permissions.

type Options

type Options struct {
	// Path overrides the managed file location. Empty uses DefaultPath.
	Path string
	// Getenv overrides the environment lookup used by DefaultPath.
	Getenv func(string) string
	// AllowUnpersisted keeps a store usable when DefaultPath resolves to "".
	// Every remembered request then yields a session grant, and Add reports the
	// degradation through the returned Grant rather than an error.
	AllowUnpersisted bool
}

Options configure New.

type Removal

type Removal struct {
	// Session and Remembered each report whether that layer held the row.
	Session    bool
	Remembered bool
	// Persisted reports that the managed file was rewritten.
	Persisted bool
}

Removal reports which layers a Remove touched.

func RemoveCapability

func RemoveCapability(path string, caps Capability) (Removal, bool, error)

RemoveCapability revokes capabilities from a path in the process table, keeping whatever else the row grants. It reports which layers held it, and false when there is no table (the caller has nothing to revoke).

func (Removal) Touched

func (r Removal) Touched() bool

Touched reports whether any layer held the row.

type Scope

type Scope uint8

Scope is how wide a grant reaches.

const (
	// Subtree covers the path itself and everything under it. It is the default
	// and what a directory grant means.
	Subtree Scope = iota
	// Exact covers only the path itself. It is what an answer about one file
	// produces, so approving a read of /etc/hosts does not hand over /etc.
	Exact
)

func (Scope) String

func (s Scope) String() string

String renders the scope for /trust's listing.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store is the unified table.

func FromSnapshot added in v1.3.1

func FromSnapshot(entries Entries) (*Store, error)

FromSnapshot builds an in-memory authorization table for a one-call tool worker. It never opens or writes the managed store: the parent has already read it and sent the decisions this call was approved under. Keeping the two layers separate preserves their precedence (session over remembered).

func New

func New(opts Options) (*Store, error)

New opens the table at the managed location. A missing file is not an error: the store starts empty and reports Loaded() == false so the caller can decide whether to run a migration. A present-but-unreadable or newer-versioned file IS an error, because guessing around a broken permission store is how a revoked grant comes back.

func Process

func Process() *Store

Process returns the process table, or nil when it was never initialized (a caller that forgot Open, or a test that did not need one). A nil return is not an error: callers treat it as "no grants recorded", which is exactly what an empty table means.

func (*Store) Add

func (s *Store) Add(g Grant) (Grant, error)

Add records a grant. A remembered grant is written to disk before it is reported as remembered: an answer the user gave in order to stop being asked must survive a restart, so when the write fails the grant is kept for this session and the returned value says Session, letting the caller tell the truth instead of promising persistence it did not achieve.

rows are merged into the existing row for the path: a later grant narrows or widens the same path rather than shadowing it, so `/grants add /x r` after `/grants add /x w` leaves /x as "rw" instead of whichever row won by ordering.

func (*Store) Allows

func (s *Store) Allows(path string, cap Capability) bool

Allows is the boolean form of Check, for callers that only gate.

func (*Store) Check

func (s *Store) Check(path string, cap Capability) Verdict

Check answers one (path, capability) question by most-specific-wins: the walk starts at the path itself and moves toward the root, and the first layer at the first level that speaks to this capability decides it. A refusal recorded for a child therefore still beats a grant inherited from its parent.

Layers rank session, then remembered. Session ranks first so an answer given in this run takes effect immediately rather than being masked by an older saved row for the same directory.

func (*Store) Entries

func (s *Store) Entries() Entries

Entries returns every row: remembered, then session, each sorted by path. /trust prints this; the layers are separate because removing a row means different things in each.

func (*Store) Loaded

func (s *Store) Loaded() bool

Loaded reports whether the managed file existed when the store was opened. False means this is a first run for the unified table, which is the moment to migrate the legacy stores.

func (*Store) Migrate

func (s *Store) Migrate(paths LegacyPaths) (MigrateResult, error)

Migrate reads the legacy stores, adds their rows to the table, and persists once. It is idempotent and safe to call on every start: it does nothing when the managed file already existed or nothing was found, so a user who has moved on is not re-migrated from a stale file.

func (*Store) Path

func (s *Store) Path() string

Path returns the managed file location, for /trust to print.

func (*Store) Refuse

func (s *Store) Refuse(path string, caps Capability, life Lifetime) (Grant, error)

Refuse records an explicit refusal. It exists as its own entry point because "no" is a decision worth persisting: without it, answering no to a directory would be forgotten and the next call would ask again.

func (*Store) Remove

func (s *Store) Remove(path string) (Removal, error)

Remove revokes a path from the layers the caller can edit: the session layer always, and the managed file when it holds a row.

func (*Store) RemoveCapability

func (s *Store) RemoveCapability(path string, caps Capability) (Removal, error)

RemoveCapability revokes specific capabilities from a path, keeping the rest of its row, and reports which layers held it. The row is deleted outright once it grants and refuses nothing, so the file never accumulates empty rows.

It exists because a row carries a SET, and the commands that manage it are separate: `/trust` speaks for Execute, `/boundary` for Read and Write, and a directory the user reads and runs commands in is one row with both (`re`). Deleting the row for either command would silently revoke the other's grant, which is the kind of cross-talk that makes people stop trusting a permission system.

It clears exactly the capabilities named, never more: removing Write leaves a Read the row carried, because a caller that wanted to revoke reading would have said so. Grant is the opposite operation and the same rule applies — see mergeGrant, which drops only the bits a new answer speaks to.

func (*Store) Warnings

func (s *Store) Warnings() []string

Warnings returns the non-fatal problems seen while opening or saving, so the caller can surface them once. They never block a run.

type Verdict

type Verdict struct {
	// Decision is Allowed, Denied or Undecided.
	Decision Decision
	// By is the path of the row that decided it, "" when undecided. It is the
	// path the user would have to revoke to change the answer.
	By string
	// Grant is the deciding row.
	Grant Grant
}

Verdict is the outcome of Check.

func Check

func Check(path string, cap Capability) Verdict

Check consults the process table. With no table every path is Undecided, which is the pre-table behavior: the caller asks.

Directories

Path Synopsis
Package grantstest installs an empty authorization table for a test.
Package grantstest installs an empty authorization table for a test.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL