guard

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package guard provides SQL injection prevention utilities for Quark ORM. It validates identifiers, operators, and raw queries against known-safe patterns.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidIdentifier = errors.New("invalid identifier")

ErrInvalidIdentifier is the sentinel for a rejected table/column identifier. It lives here (not in the root package) so ValidateIdentifier can wrap it with %w without an import cycle; the root package re-exports it as quark.ErrInvalidIdentifier, so callers can errors.Is() the rejection regardless of which call site (query, migrate, cte, events…) produced it — consistent with ErrInvalidJoin / ErrInvalidJSONPath.

Functions

func HasPlaceholders

func HasPlaceholders(query string) bool

HasPlaceholders checks if a query string contains parameter placeholders.

func ValidateJSONPath added in v0.3.0

func ValidateJSONPath(path string) error

ValidateJSONPath checks that a JSON path is shaped like a dotted identifier chain (e.g. "user.profile.email") and rejects anything that could carry SQL injection: empty paths, paths with quotes, semicolons, comment markers, whitespace, leading "$", or paths longer than maxJSONPathLen.

It is a package-level function (not a method) because the validation has no configurable state and the dialect layer needs to call it without holding an SQLGuard instance.

The error message starts with "ErrInvalidJSONPath:" so callers in package quark can wrap it with the public sentinel via errors.Join.

func ValidateJSONTablePath added in v0.3.0

func ValidateJSONTablePath(path string) error

ValidateJSONTablePath is a defensive validator for MariaDB's JSON_TABLE root path. Unlike ValidateJSONPath it accepts the JSONPath shapes that JSON_TABLE requires (`$`, `$[*]`, `$.items[0]`, `$.items[*].name`) but rejects anything that looks like injection.

The JSON_TABLE entry point is internal-only today; this validator exists so that any caller in package quark or trusted internal code paths still gets a fail-fast error if a bad value reaches the dialect.

func ValidateJoinOn added in v0.3.0

func ValidateJoinOn(expr string) error

ValidateJoinOn checks that an ON clause matches the minimal identifier- only grammar Quark accepts while a structured Join().On() builder is pending. It rejects anything containing literals, semicolons, comments, quotes, parentheses, or arbitrary whitespace surrounding non-token characters — so injection payloads ("...; DROP TABLE x") cannot pass.

Returns an error whose message starts with "ErrInvalidJoin:" so callers in package quark can wrap it with the public sentinel via errors.Join.

Types

type Quoter

type Quoter interface {
	Quote(identifier string) string
}

Quoter is a minimal interface for quoting SQL identifiers. It avoids a circular import with the dialect package.

type SQLGuard

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

SQLGuard provides security validations for SQL queries. It prevents SQL injection by validating identifiers and enforcing safe practices.

func New

func New() *SQLGuard

New creates a new SQLGuard with default settings.

func (*SQLGuard) QuoteIdentifier

func (g *SQLGuard) QuoteIdentifier(q Quoter, name string) (string, error)

QuoteIdentifier validates and quotes an identifier using the provided Quoter.

func (*SQLGuard) ValidateIdentifier

func (g *SQLGuard) ValidateIdentifier(name string) error

ValidateIdentifier checks if a table or column identifier is safe to use.

func (*SQLGuard) ValidateIdentifiers

func (g *SQLGuard) ValidateIdentifiers(names ...string) error

ValidateIdentifiers checks multiple identifiers at once.

func (*SQLGuard) ValidateJSONPath added in v0.3.0

func (g *SQLGuard) ValidateJSONPath(path string) error

ValidateJSONPath is the SQLGuard-bound counterpart to the package-level function; both share the same logic and accept the same shapes.

func (*SQLGuard) ValidateJoinOn added in v0.3.0

func (g *SQLGuard) ValidateJoinOn(expr string) error

ValidateJoinOn is the SQLGuard-bound counterpart to the package-level function; both share the same logic and accept the same shapes.

func (*SQLGuard) ValidateOperator

func (g *SQLGuard) ValidateOperator(op string) error

ValidateOperator checks if an operator is in the allowed whitelist.

func (*SQLGuard) ValidateRawQuery

func (g *SQLGuard) ValidateRawQuery(query string, requirePlaceholders bool) error

ValidateRawQuery performs basic validation on a raw SQL query. It is a best-effort heuristic backstop for the opt-in raw path (AllowRawQueries), NOT a complete anti-injection filter. It does not parse SQL, so it has known false positives — e.g. a `--` inside a string literal (`'range--max'`) is rejected even though it is not a comment; rephrase such a query. The real boundary for raw queries is AllowRawQueries (off by default) + placeholders for values.

Jump to

Keyboard shortcuts

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