sqlident

package
v1.0.0-rc.2 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package sqlident validates and quotes SQL identifiers.

Values in Hermod's generated SQL are parameterized, but *identifiers* — table and schema names — cannot be: no driver supports a placeholder in `INSERT INTO ?`. They are interpolated into the statement, so they have to be proven safe before they get there.

That mattered more than it looks. A SQL sink without an explicit table name falls back to the table and schema carried on the message itself, and a message's table can come from the wire: the WebSocket source sets it straight from the inbound envelope (`m.SetTable(env.Table)`). A peer could therefore choose the identifier that gets pasted into a MERGE or INSERT against the destination database — arbitrary SQL execution on a system that is usually far more sensitive than Hermod itself. The same held, with an authenticated Editor rather than a remote peer, for identifiers taken from sink config.

Validation is deliberately strict rather than clever. It allows what a table name legitimately is and rejects everything else, instead of trying to escape hostile input — an allowlist cannot be outwitted by a quoting trick.

Index

Constants

View Source
const MaxLength = 128

MaxLength bounds an identifier. The shortest limit among the databases Hermod targets is 63 bytes (PostgreSQL); 128 covers the rest with room to spare, and any name longer than that is not a real table.

Variables

This section is empty.

Functions

func MustBeSafe

func MustBeSafe(name string) (string, error)

MustBeSafe returns name unchanged when it validates, and an error otherwise. It is the minimal call site for code that already builds its own SQL and just needs the identifier checked before interpolation.

func Quote

func Quote(d Dialect, name string) (string, error)

Quote validates name and returns it quoted for the dialect.

Quoting alone is not the defence — validation is. Quoting on top means a legitimate name that collides with a reserved word still works.

func Validate

func Validate(name string) error

Validate reports whether name is a safe SQL identifier, optionally schema-qualified as `schema.table`.

Accepted: an optional schema part and a name part, each starting with a letter or underscore and continuing with letters, digits or underscores. Everything else — quotes, semicolons, whitespace, comment markers, unicode look-alikes, empty parts — is rejected.

Types

type Dialect

type Dialect string

Dialect selects the quoting style.

const (
	Postgres   Dialect = "postgres"
	MySQL      Dialect = "mysql"
	MSSQL      Dialect = "mssql"
	SQLite     Dialect = "sqlite"
	ClickHouse Dialect = "clickhouse"
)

Jump to

Keyboard shortcuts

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