Documentation ¶
Overview ¶
Package safesql implements a safe version of database/sql to prevent accidental SQL injections.
Usage should ideally be identical to the standard sql package with the exception that strings should be String instead.
The simplest way to transition to this package is to turn statements like
db.Query("SELECT ...", args...)
into
db.Query(safesql.New("SELECT ..."), args...)
Once safesql is adopted, importing database/sql should be banned with the sole exception of this package. Types from database/sql that are inherently safe have aliases in this package to allow for an easier transition and smaller allowlists.
For leftover exceptions and for transitions the legacyconversions and uncheckedconversions packages can be used. Similarly, the testconversions should only be used during tests.
Index ¶
- Variables
- func Drivers() []string
- func Register(name string, driver driver.Driver)
- type ColumnType
- type Conn
- func (c *Conn) BeginTx(ctx context.Context, opts *TxOptions) (*Tx, error)
- func (c *Conn) Close() error
- func (c *Conn) ExecContext(ctx context.Context, query String, args ...any) (Result, error)
- func (c *Conn) PingContext(ctx context.Context) error
- func (c *Conn) PrepareContext(ctx context.Context, query String) (*Stmt, error)
- func (c *Conn) QueryContext(ctx context.Context, query String, args ...any) (*Rows, error)
- func (c *Conn) QueryRowContext(ctx context.Context, query String, args ...any) *Row
- type DB
- func (db *DB) Begin() (*Tx, error)
- func (db *DB) BeginTx(ctx context.Context, opts *TxOptions) (*Tx, error)
- func (db *DB) Close() error
- func (db *DB) Conn(ctx context.Context) (*Conn, error)
- func (db *DB) Exec(query String, args ...any) (Result, error)
- func (db *DB) ExecContext(ctx context.Context, query String, args ...any) (Result, error)
- func (db *DB) Ping() error
- func (db *DB) PingContext(ctx context.Context) error
- func (db *DB) Prepare(query String) (*Stmt, error)
- func (db *DB) PrepareContext(ctx context.Context, query String) (*Stmt, error)
- func (db *DB) Query(query String, args ...any) (*Rows, error)
- func (db *DB) QueryContext(ctx context.Context, query String, args ...any) (*Rows, error)
- func (db *DB) QueryRow(query String, args ...any) *Row
- func (db *DB) QueryRowContext(ctx context.Context, query String, args ...any) *Row
- func (db *DB) SetConnMaxIdleTime(d time.Duration)
- func (db *DB) SetConnMaxLifetime(d time.Duration)
- func (db *DB) SetMaxIdleConns(n int)
- func (db *DB) SetMaxOpenConns(n int)
- func (db *DB) Stats() DBStats
- type DBStats
- type IsolationLevel
- type NamedArg
- type NullBool
- type NullFloat64
- type NullInt32
- type NullInt64
- type NullString
- type NullTime
- type Out
- type RawBytes
- type RealNumber
- type Result
- type Row
- type Rows
- type Scanner
- type Stmt
- type String
- type Tx
- func (tx *Tx) Commit() error
- func (tx *Tx) Exec(query String, args ...any) (Result, error)
- func (tx *Tx) ExecContext(ctx context.Context, query String, args ...any) (Result, error)
- func (tx *Tx) Prepare(query String) (*Stmt, error)
- func (tx *Tx) PrepareContext(ctx context.Context, query String) (*Stmt, error)
- func (tx *Tx) Query(query String, args ...any) (*Rows, error)
- func (tx *Tx) QueryContext(ctx context.Context, query String, args ...any) (*Rows, error)
- func (tx *Tx) QueryRow(query String, args ...any) *Row
- func (tx *Tx) QueryRowContext(ctx context.Context, query String, args ...any) *Row
- func (tx *Tx) Rollback() error
- func (tx *Tx) Stmt(stmt *Stmt) *Stmt
- func (tx *Tx) StmtContext(ctx context.Context, stmt *Stmt) *Stmt
- type TxOptions
Constants ¶
This section is empty.
Variables ¶
var ( // ErrConnDone is https://pkg.go.dev/database/sql#ErrConnDone ErrConnDone = sql.ErrConnDone // ErrNoRows is https://pkg.go.dev/database/sql#ErrNoRows ErrNoRows = sql.ErrNoRows // ErrTxDone is https://pkg.go.dev/database/sql#ErrTxDone ErrTxDone = sql.ErrTxDone )
Functions ¶
Types ¶
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn is a tiny wrapper for https://pkg.go.dev/database/sql#Conn The Raw method has been removed for security reasons.
func (*Conn) BeginTx ¶
BeginTx is a tiny wrapper for https://pkg.go.dev/database/sql#Conn.BeginTx
func (*Conn) Close ¶
Close is a tiny wrapper for https://pkg.go.dev/database/sql#Conn.Close
func (*Conn) ExecContext ¶
ExecContext is a tiny wrapper for https://pkg.go.dev/database/sql#Conn.ExecContext
func (*Conn) PingContext ¶
PingContext is a tiny wrapper for https://pkg.go.dev/database/sql#Conn.PingContext
func (*Conn) PrepareContext ¶
PrepareContext is a tiny wrapper for https://pkg.go.dev/database/sql#Conn.PrepareContext
func (*Conn) QueryContext ¶
QueryContext is a tiny wrapper for https://pkg.go.dev/database/sql#Conn.QueryContext
func (*Conn) QueryRowContext ¶
QueryRowContext is a tiny wrapper for https://pkg.go.dev/database/sql#Conn.QueryRowContext
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB is a tiny wrapper for https://pkg.go.dev/database/sql#DB The Driver method has been removed for security reasons.
func Open ¶
Open is a tiny wrapper for https://pkg.go.dev/database/sql#Open
func OpenDB ¶
OpenDB is a tiny wrapper for https://pkg.go.dev/database/sql#OpenDB
func (*DB) Begin ¶
Begin is a tiny wrapper for https://pkg.go.dev/database/sql#DB.Begin
func (*DB) BeginTx ¶
BeginTx is a tiny wrapper for https://pkg.go.dev/database/sql#DB.BeginTx
func (*DB) Close ¶
Close is a tiny wrapper for https://pkg.go.dev/database/sql#DB.Close
func (*DB) Conn ¶
Conn is a tiny wrapper for https://pkg.go.dev/database/sql#DB.Conn
func (*DB) Exec ¶
Exec is a tiny wrapper for https://pkg.go.dev/database/sql#DB.Exec
func (*DB) ExecContext ¶
ExecContext is a tiny wrapper for https://pkg.go.dev/database/sql#DB.ExecContext
func (*DB) Ping ¶
Ping is a tiny wrapper for https://pkg.go.dev/database/sql#DB.Ping
func (*DB) PingContext ¶
PingContext is a tiny wrapper for https://pkg.go.dev/database/sql#DB.PingContext
func (*DB) Prepare ¶
Prepare is a tiny wrapper for https://pkg.go.dev/database/sql#DB.Prepare
func (*DB) PrepareContext ¶
PrepareContext is a tiny wrapper for https://pkg.go.dev/database/sql#DB.PrepareContext
func (*DB) Query ¶
Query is a tiny wrapper for https://pkg.go.dev/database/sql#DB.Query
func (*DB) QueryContext ¶
QueryContext is a tiny wrapper for https://pkg.go.dev/database/sql#DB.QueryContext
func (*DB) QueryRow ¶
QueryRow is a tiny wrapper for https://pkg.go.dev/database/sql#DB.QueryRow
func (*DB) QueryRowContext ¶
QueryRowContext is a tiny wrapper for https://pkg.go.dev/database/sql#DB.QueryRowContext
func (*DB) SetConnMaxIdleTime ¶
SetConnMaxIdleTime is a tiny wrapper for https://pkg.go.dev/database/sql#DB.SetConnMaxIdleTime
func (*DB) SetConnMaxLifetime ¶
SetConnMaxLifetime is a tiny wrapper for https://pkg.go.dev/database/sql#DB.SetConnMaxLifetime
func (*DB) SetMaxIdleConns ¶
SetMaxIdleConns is a tiny wrapper for https://pkg.go.dev/database/sql#DB.SetMaxIdleConns
func (*DB) SetMaxOpenConns ¶
SetMaxOpenConns is a tiny wrapper for https://pkg.go.dev/database/sql#DB.SetMaxOpenConns
func (*DB) Stats ¶
Stats is a tiny wrapper for https://pkg.go.dev/database/sql#DB.Stats
type IsolationLevel ¶
type IsolationLevel = sql.IsolationLevel
IsolationLevel is https://pkg.go.dev/sql#IsolationLevel
type NullFloat64 ¶
type NullFloat64 = sql.NullFloat64
NullFloat64 is https://pkg.go.dev/sql#NullFloat64
type RealNumber ¶
type RealNumber interface { constraints.Integer | constraints.Float }
type String ¶
type String struct {
// contains filtered or unexported fields
}
String wraps a string that is safe and does not contain user-controlled input.
func New ¶
func New(text stringConstant) String
New constructs a String from a compile-time constant string. Since the stringConstant type is unexported the only way to call this function outside of this package is to pass a string literal or an untyped string const.
Note(empijei): this can be bypassed by using generics with ~string, but that feels very unlikely to happen by accident and malicious programmers are not part of the threat model of this package.
func NewFromNumber ¶
func NewFromNumber[N RealNumber](i N) String
NewFromNumber constructs a String from a number.
func StringConcat ¶
StringConcat concatenates the given [String]s into a trusted string.
Note(empijei): this function may be abused to create arbitrary queries from user inputs, but malicious programmers are not part of the threat model of this package.
func StringJoin ¶
StringJoin joins the given [String]s with the given separator the same way strings.Join would.
Note(empijei): this function may be abused to create arbitrary queries from user inputs, but malicious programmers are not part of the threat model of this package.
func StringSplit ¶
StringSplit functions as strings.Split but for [String]s.
type Tx ¶
type Tx struct {
// contains filtered or unexported fields
}
Tx is a tiny wrapper for https://pkg.go.dev/database/sql#Tx
func (*Tx) Commit ¶
Commit is a tiny wrapper for https://pkg.go.dev/database/sql#Tx.Commit
func (*Tx) Exec ¶
Exec is a tiny wrapper for https://pkg.go.dev/database/sql#Tx.Exec
func (*Tx) ExecContext ¶
ExecContext is a tiny wrapper for https://pkg.go.dev/database/sql#Tx.ExecContext
func (*Tx) Prepare ¶
Prepare is a tiny wrapper for https://pkg.go.dev/database/sql#Tx.Prepare
func (*Tx) PrepareContext ¶
PrepareContext is a tiny wrapper for https://pkg.go.dev/database/sql#Tx.PrepareContext
func (*Tx) Query ¶
Query is a tiny wrapper for https://pkg.go.dev/database/sql#Tx.Query
func (*Tx) QueryContext ¶
QueryContext is a tiny wrapper for https://pkg.go.dev/database/sql#Tx.QueryContext
func (*Tx) QueryRow ¶
QueryRow is a tiny wrapper for https://pkg.go.dev/database/sql#Tx.QueryRow
func (*Tx) QueryRowContext ¶
QueryRowContext is a tiny wrapper for https://pkg.go.dev/database/sql#Tx.QueryRowContext
func (*Tx) Rollback ¶
Rollback is a tiny wrapper for https://pkg.go.dev/database/sql#Tx.Rollback
func (*Tx) Stmt ¶
Stmt is a tiny wrapper for https://pkg.go.dev/database/sql#Tx.Stmt
func (*Tx) StmtContext ¶
StmtContext is a tiny wrapper for https://pkg.go.dev/database/sql#Tx.StmtContext
Directories ¶
Path | Synopsis |
---|---|
internal
|
|
Package legacyconversions can be used to atomically switch to safesql.
|
Package legacyconversions can be used to atomically switch to safesql. |
Package testconversions can be used to easily create unsafe SQL strings during tests.
|
Package testconversions can be used to easily create unsafe SQL strings during tests. |
Package uncheckedconversions can be used to manually promote unsafe strings to safesql.String.
|
Package uncheckedconversions can be used to manually promote unsafe strings to safesql.String. |