Skip to Content
fusion-orm
Guides

Security

How fusion-orm keeps attacker-controlled data out of your SQL — parameterized values, quoted identifiers, hardened literal appenders, guardrails, and logging hygiene.

fusion-orm's security posture is built into the SQL-rendering layer: every value binds through a placeholder, every identifier renders through the dialect's quoting, every literal appender fails closed on hostile input, and global writes are rejected unless you opt in. This page walks through each mechanism with its source of truth, then lists what the ORM deliberately does not do.

Values bind, they never concatenate

The Dialector contract forbids string-concatenating user data into executable SQL. Values travel through database/sql as bound parameters; the dialector only renders the placeholder:

// The value appenders (AppendString and friends) render literals; they must
// never string-concatenate user data — quoting and escaping only. AppendString
// rejects NUL bytes, and the numeric appenders guard against rendering a
// negative literal directly after '-' (which would form a "--" line comment,
// CVE-2024-44906 class).
// BindVarTo appends the placeholder for the 1-based parameter index
// ("?" or "$1").
SELECT * FROM "users" WHERE name = ?

Interpolation does happen — but only for display: ToSQL, Explain, and log traces render a human-readable string. The statement that actually executes always carries its values as Statement.Vars bound at call time (dialect/dialect.go:98-102).

Raw string fragments are developer SQL

Where("age > ?", minAge) parameterizes the value. But the SQL text you pass to Where, Order, Select, Exec, and Table is part of your program, not attacker input — the engine does not validate it. If a value can ever contain user data, it belongs in the argument list, not the fragment.

Identifiers go through QuoteTo

Identifiers (table names, column names, dotted paths like profile__avatar) are rendered through QuoteTo, which quotes each segment and doubles embedded quote characters. User data never reaches an identifier position unless you deliberately pass it there:

// AppendIdent appends a possibly dotted identifier with each segment quoted.
// "*" passes through unquoted (SELECT *), as does a bare trailing ".*";
// the empty identifier renders as the empty quoted name.

Column and table names come from your struct tags and the naming strategy, so the normal path is safe by construction. Map conditions quote their keys through the same path (chainable_api.go:197-199), and struct conditions quote the parsed column name (chainable_api.go:227).

Literal appenders are hardened

Literal rendering is where a naive ORM gets exploited. fusion's appenders in dialect/base.go and dialect/append.go handle each attack class:

NUL bytes are rejected, not stripped. Silently stripping a NUL would store a value different from the one the caller supplied — and PostgreSQL rejects NUL in text outright — so the appender fails closed:

// ErrStringNUL is reported when a string value contains a NUL byte (0x00).
// Silently stripping it would store a value different from the one the
// caller supplied; PostgreSQL rejects NUL in text outright.
var ErrStringNUL = errors.New("fusion: string contains a NUL byte (0x00)")

Negative numbers cannot form -- line comments. A negative literal appended directly after a trailing - would render as --, starting a line comment that swallows the rest of the query (the CVE-2024-44906 class fixed in bun). The guard inserts a space instead:

// guardLineComment inserts a space when a negative numeric literal would be
// appended immediately after a '-'. Without it, "col-?" formatted with -1
// becomes "col--1", where "--" starts a SQL line comment usable for
// injection. Mirrors bun's fix for CVE-2024-44906.

Both AppendInt64 (dialect/append.go:34) and AppendFloat64 (dialect/append.go:49) route through the guard.

Quotes are doubled, not stripped. AppendString renders each ' as '' (dialect/base.go:77-81), so a string value cannot break out of the literal.

JSON escaping keeps the quote path authoritative. The JSON appender emits a backslash without consuming the next byte, so a crafted \' sequence still gets escaped — the same CVE-2024-44906 class (dialect/base.go:124-127).

Bytes render as hex literals, per dialect. PostgreSQL uses the '\x...' form; SQLite overrides it with X'...':

SELECT * FROM "users" WHERE avatar = X'deadbeef'

Formatting failures fail closed. When a value cannot be rendered, the appender embeds the error into the SQL text as ?!(<message>) so the statement fails at execution with the original cause visible — never silently with a different value (dialect/append.go:8-11).

The global-write guard

Update and Delete without a WHERE clause — or without a model whose primary key is set — are rejected with fusion.ErrMissingWhereClause (errors.go:25-27). A bug that drops the conditions cannot silently turn "update one row" into "update every row":

global := db.Model(&crudUser{}).Update("name", "everyone")
if !errors.Is(global.Error, ErrMissingWhereClause) {
	t.Errorf("global Update = %v, want ErrMissingWhereClause", global.Error)
}
globalDelete := db.Model(&crudUser{}).Delete(nil)
if !errors.Is(globalDelete.Error, ErrMissingWhereClause) {
	t.Errorf("global Delete = %v", globalDelete.Error)
}

The escape hatch is explicit and per-session:

allowed := db.Session(&Session{AllowGlobalUpdate: true}).Model(&crudUser{}).Update("name", "x")

The guard runs before soft-delete rewriting, so a global soft delete also errors instead of rewriting every row (CHANGELOG.md:115-118).

Savepoint names are validated

SavePoint, RollbackTo, and the internal release path require a plain SQL identifier — [A-Za-z_][A-Za-z0-9_]* (transaction.go:165-168) — and reject anything else with ErrInvalidValue before the statement is built (transaction.go:154-157):

if err := tx.SavePoint("bad name; DROP TABLE users").Error; !errors.Is(err, ErrInvalidValue) {
	t.Errorf("SavePoint with hostile name = %v, want ErrInvalidValue", err)
}

The name is additionally quoted through the dialector (dialect/dialect.go:81-84), so both checks would have to fail together for a crafted name to matter.

Logging hygiene

Traces interpolate bound values through Explain — convenient for debugging, dangerous for production logs. Two mechanisms keep user data out:

ParameterizedQueries on the built-in logger drops the variables entirely, so the log shows placeholder text only:

// ParameterizedQueries strips bound variables from logged SQL so
// user data never reaches the log.
if l.config.ParameterizedQueries {
	return sql, nil
}

SlogQueryHook never logs parameters. The slog adapter records the SQL text, duration, row count, and error — and nothing else. The hook's own test asserts a secret bound value cannot appear in the output (query_hook_test.go:63-76):

// SlogQueryHook logs completed statements through log/slog without recording
// bound parameter values.

DSNs are credentials too

Connection strings carry passwords (postgres://user:secret@host/db). Pass them through environment variables or a secrets manager, and never route them through the logger — fusion does not log DSNs, but it also cannot stop your Open call from doing so.

Strict tags catch typos at parse time

A misspelled tag key would otherwise be ignored (with a warning) and the constraint silently missing. Config.StrictTags turns that warning into a parse failure with schema.ErrInvalidTag, naming the bad key (schema/tag.go:100-106):

// StrictTags rejects unknown fusion tag keys instead of warning.

Note the deliberate exception: a single valueless token still becomes the bare column name even in strict mode (schema/tag.go:96-98), because that is how column renaming is spelled.

What fusion-orm does NOT do

  • No RBAC or row-level security. Authorization lives in your application or in your database's policies; fusion neither enforces nor bypasses it.
  • No encryption. Data at rest and in transit is the database's and the driver's job (TLS on the DSN, disk encryption, column encryption).
  • No audit trail. CreatedAt/UpdatedAt are timestamps, not an audit log; use triggers or an application-level audit for that.
  • No input validation. The ORM guarantees SQL safety for the values it binds; whether an email looks like an email is your validator's job.
  • No SQL-policy rewrite. The guard rails (ErrMissingWhereClause, savepoint validation, StrictTags) protect against your mistakes — they do not sandbox arbitrary SQL you pass to Exec or raw fragments.

Defense in depth

The engine's guarantees cover the library's own rendering. Combine them with least-privilege database roles, connection TLS, and ParameterizedQueries in production for a full stack.

Where next

On this page