guard

package
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package guard decides whether a statement may run against a datasource.

Evaluate is a pure function of the statement and the policy. It never touches a database, which is what allows the whole policy surface to be covered by fast table-driven tests — and what makes it trustworthy.

The governing rule is fail-closed: sqlparse is a tokenizer, not a full MySQL parser, so anything it cannot classify is refused in production rather than assumed harmless.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decision

type Decision struct {
	Verdict Verdict
	// Reason explains the verdict; the status bar and dialog show it.
	Reason string
	// TypeToConfirm, when set, is the phrase the user must type out before
	// the statement runs. Reserved for irreversible operations, where a
	// reflexive "yes" is the failure mode being defended against.
	TypeToConfirm string
	// InjectLimit is the LIMIT to append, or zero to leave the SQL alone.
	InjectLimit int
	// Unlockable reports that the production write lock is the only thing in
	// the way, so the caller may offer the way past it.
	//
	// It is a flag rather than a sentence because guard must not name a route
	// through an interface it cannot see: the reason once read "unlock with
	// :write", which was a command no preset had. Whoever draws the dialog
	// knows what the keys are; this package does not.
	Unlockable bool
}

Decision is the result of evaluating one statement.

func Evaluate

func Evaluate(stmt sqlparse.Statement, p Policy) Decision

Evaluate applies p to stmt.

type Policy

type Policy struct {
	// Env decides how strict the rules are.
	Env config.Env
	// AutoLimit is appended to unbounded SELECTs. Zero disables it.
	AutoLimit int
	// WritesEnabled records the session-level ":write" opt-in. It only ever
	// relaxes a Deny into a Confirm, and never for unbounded or destructive
	// statements.
	WritesEnabled bool
	// InTransaction says a transaction is open, which changes what is true
	// rather than what is permitted: the connection is held for its whole
	// life, so session state set on it does reach the next statement.
	InTransaction bool
}

Policy is the rule set in force for one datasource.

type Verdict

type Verdict int

Verdict is what the runner should do with a statement.

const (
	// Allow runs the statement immediately.
	Allow Verdict = iota
	// Confirm runs it only after the user agrees.
	Confirm
	// Deny refuses to run it at all.
	Deny
)

func (Verdict) String

func (v Verdict) String() string

Jump to

Keyboard shortcuts

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