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.
Package grants also owns the remembered network answers. They live beside the path table rather than inside it: a path row is keyed by a location and matched by walking its parents, while a network row is keyed by a host or a command and matched exactly. Folding the two into one file would make an old golder that rewrites grants.json silently drop the network rules, and would make every /grants listing mix two units that answer different questions.
The file is deliberately separate for the same reason: a downgrade must not turn a remembered "yes, fetch github.com" into a parse error for the path table, and it must not delete a rule the user believes is still there.
Index ¶
- Constants
- func AddReadRoot(raw string, life Lifetime, source string) (string, error)
- func AddWritableRoot(raw string, life Lifetime, source string) (string, error)
- func Allows(path string, cap Capability) bool
- func DefaultNetworkPath(getenv func(string) string) string
- func DefaultPath(getenv func(string) string) string
- func NormalizeNetworkCommand(raw string) (string, error)
- func NormalizeNetworkHost(raw string) (string, error)
- func Open(getenv func(string) string) (warnings []string, err error)
- func OpenNetwork(getenv func(string) string) (warnings []string, err error)
- func ReadRoots() []string
- func RemoveNetwork(kind NetworkKind, key string) bool
- func SetProcess(s *Store)
- func SetProcessNetwork(s *NetworkStore)
- func WritableRoots() []string
- type Capability
- type Decision
- type Entries
- type Grant
- type LegacyPaths
- type Lifetime
- type MigrateResult
- type NetworkDecision
- type NetworkEntries
- type NetworkGrant
- type NetworkKind
- type NetworkStore
- func (s *NetworkStore) Add(g NetworkGrant) (NetworkGrant, error)
- func (s *NetworkStore) Check(kind NetworkKind, key, tool string) NetworkVerdict
- func (s *NetworkStore) Entries() NetworkEntries
- func (s *NetworkStore) Loaded() bool
- func (s *NetworkStore) Remove(kind NetworkKind, key string) bool
- func (s *NetworkStore) Warnings() []string
- type NetworkVerdict
- type Options
- type Removal
- type Scope
- type Store
- func (s *Store) Add(g Grant) (Grant, error)
- func (s *Store) Allows(path string, cap Capability) bool
- func (s *Store) Check(path string, cap Capability) Verdict
- func (s *Store) Entries() Entries
- func (s *Store) Loaded() bool
- func (s *Store) Migrate(paths LegacyPaths) (MigrateResult, error)
- func (s *Store) Path() string
- func (s *Store) Refuse(path string, caps Capability, life Lifetime) (Grant, error)
- func (s *Store) Remove(path string) (Removal, error)
- func (s *Store) RemoveCapability(path string, caps Capability) (Removal, error)
- func (s *Store) Warnings() []string
- type Verdict
Constants ¶
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 )
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 ¶
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 ¶
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 DefaultNetworkPath ¶ added in v1.3.3
DefaultNetworkPath returns network_grants.json in the state root.
func DefaultPath ¶
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 NormalizeNetworkCommand ¶ added in v1.3.3
NormalizeNetworkCommand reduces a simple command to "program subcommand". It refuses anything that is not one literal simple command: pipelines, separators, redirects, substitutions and dynamic words all make the key a promise about a shape the command does not actually have.
func NormalizeNetworkHost ¶ added in v1.3.3
NormalizeNetworkHost accepts a bare DNS host, lowercases it, strips a trailing dot, and refuses the shapes that must never become a standing permission: wildcards, IP literals (including metadata), single-label names, loopback, private ranges and the special-use suffixes used by local networks. The caller then offers only a per-call answer for those.
func Open ¶
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 OpenNetwork ¶ added in v1.3.3
OpenNetwork initializes the process network table. A failure is returned to the caller, which treats it like the path table's open failure: the feature is unavailable, not a reason to guess.
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 RemoveNetwork ¶ added in v1.3.3
func RemoveNetwork(kind NetworkKind, key string) bool
RemoveNetwork removes a rule from the process table.
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 SetProcessNetwork ¶ added in v1.3.3
func SetProcessNetwork(s *NetworkStore)
SetProcessNetwork installs a table directly; tests use it.
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 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.
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 ¶
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.
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 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 NetworkDecision ¶ added in v1.3.3
type NetworkDecision uint8
NetworkDecision is the answer for one rule lookup.
const ( NetworkUndecided NetworkDecision = iota NetworkAllowed NetworkDenied )
func (NetworkDecision) String ¶ added in v1.3.3
func (d NetworkDecision) String() string
type NetworkEntries ¶ added in v1.3.3
type NetworkEntries struct {
Remembered []NetworkGrant
Session []NetworkGrant
}
NetworkEntries is the whole network table in its two layers.
func (NetworkEntries) All ¶ added in v1.3.3
func (e NetworkEntries) All() []NetworkGrant
All returns every row, remembered first.
type NetworkGrant ¶ added in v1.3.3
type NetworkGrant struct {
Kind NetworkKind `json:"kind"`
Key string `json:"key"`
Tool string `json:"tool,omitempty"`
Allow bool `json:"allow"`
Lifetime Lifetime `json:"lifetime"`
Source string `json:"source,omitempty"`
}
NetworkGrant is one remembered answer. Tool empty means it applies to every tool that can produce this key; a host rule is normally tool-scoped because "fetch github.com" is not "curl github.com".
func AddNetwork ¶ added in v1.3.3
func AddNetwork(g NetworkGrant) (NetworkGrant, error)
AddNetwork records a rule in the process table.
func AddNetworkCommand ¶ added in v1.3.3
AddNetworkCommand validates and records a command rule.
func AddNetworkHost ¶ added in v1.3.3
func AddNetworkHost(raw, tool string, allow bool, life Lifetime, source string) (NetworkGrant, error)
AddNetworkHost validates and records a host rule. Hosts are exact: a grant for api.github.com does not cover github.com, and wildcards are not accepted.
type NetworkKind ¶ added in v1.3.3
type NetworkKind string
NetworkKind is what a network rule names. A host rule is exact and applies to the fetch tools; a command rule is an exact program/subcommand shape for bash; a tool rule is the whole tool (the only honest key for websearch, whose request host depends on which backend credential is present).
const ( NetworkHost NetworkKind = "host" NetworkCommand NetworkKind = "command" NetworkTool NetworkKind = "tool" )
type NetworkStore ¶ added in v1.3.3
type NetworkStore struct {
// contains filtered or unexported fields
}
NetworkStore is the process table for network rules. It mirrors Store's layering and locking, with a different key and no parent walk.
func NewNetwork ¶ added in v1.3.3
func NewNetwork(opts Options) (*NetworkStore, error)
NewNetwork opens the managed network file. A missing file starts empty.
func ProcessNetwork ¶ added in v1.3.3
func ProcessNetwork() *NetworkStore
ProcessNetwork returns the process table, or nil when it was never opened.
func (*NetworkStore) Add ¶ added in v1.3.3
func (s *NetworkStore) Add(g NetworkGrant) (NetworkGrant, error)
Add records one answer. A remembered write that fails falls back to a session rule, exactly as the path table does, so a dialog cannot claim a rule was remembered when the file was not written.
func (*NetworkStore) Check ¶ added in v1.3.3
func (s *NetworkStore) Check(kind NetworkKind, key, tool string) NetworkVerdict
Check answers one exact rule lookup. Session answers rank before remembered ones, as they do in the path table.
func (*NetworkStore) Entries ¶ added in v1.3.3
func (s *NetworkStore) Entries() NetworkEntries
Entries returns both layers, sorted by kind and key.
func (*NetworkStore) Loaded ¶ added in v1.3.3
func (s *NetworkStore) Loaded() bool
Loaded reports whether the managed file existed when opened.
func (*NetworkStore) Remove ¶ added in v1.3.3
func (s *NetworkStore) Remove(kind NetworkKind, key string) bool
Remove deletes a rule from both layers.
func (*NetworkStore) Warnings ¶ added in v1.3.3
func (s *NetworkStore) Warnings() []string
Warnings returns non-fatal problems collected during load/save.
type NetworkVerdict ¶ added in v1.3.3
type NetworkVerdict struct {
Decision NetworkDecision
By NetworkGrant
}
NetworkVerdict is the outcome of one lookup.
func NetworkCheck ¶ added in v1.3.3
func NetworkCheck(kind NetworkKind, key, tool string) NetworkVerdict
NetworkCheck consults the process table.
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).
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store is the unified table.
func FromSnapshot ¶ added in v1.3.1
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 ¶
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 ¶
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 ¶
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 ¶
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) Refuse ¶
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 ¶
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.
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. |