Authorization
jed has no users, roles, or GRANT statements in SQL — authorization lives above the engine, on
the session. A host serving untrusted queries configures a privilege envelope and the engine
enforces it mechanically: any operation the envelope withholds fails with 42501 at name
resolution, before it runs.
Pair authorization with the resource limits for metered work and covered scalar allocations. Review their memory coverage before exposing arbitrary SQL.
The model
Two object kinds, each with the PostgreSQL privileges jed has a feature for:
- Tables —
SELECT,INSERT,UPDATE,DELETE. - Functions —
EXECUTE.
Three layers compose into the effective privilege for an operation on an object:
default_privileges— the table privileges granted to every table (the “all tables” default). The default is all four; set it to{SELECT}for a read-only session.grant— extra privileges on one object, beyond the default.revoke— privileges withheld from one object. Revoke always wins over a grant and the default, so denying is order-independent.
A separate allow_ddl flag (default on) gates all CREATE / DROP / ALTER.
use jed::{CreateOptions, Database, Privilege, PrivilegeSet, SessionOptions};
fn main() -> jed::Result<()> {
let db = Database::create(CreateOptions { path: Some("app.jed".into()), ..Default::default() })?;
db.execute("CREATE TABLE report (id i32 PRIMARY KEY, body text)", &[])?;
db.execute("INSERT INTO report VALUES (1, 'hello')", &[])?;
// Serve untrusted queries through a SESSION granted ONLY read access: default_privileges =
// {SELECT} (a read-only envelope) with DDL disabled. A session is a handle minted from the
// database that shares its committed state. The engine enforces the envelope at name resolution —
// any write or schema change resolves to 42501, with no in-database role catalog.
let mut untrusted = db.session(SessionOptions {
default_privileges: PrivilegeSet::EMPTY.with(Privilege::Select),
allow_ddl: false,
..SessionOptions::default()
});
untrusted.execute("SELECT body FROM report", &[])?; // ok
let denied = untrusted.execute("DELETE FROM report", &[]);
assert_eq!(denied.unwrap_err().code(), "42501"); // permission denied for table report
untrusted.close(); // release the session (and its reader pin)
// grant/revoke adjust one object at a time on a session's envelope, and revoke always wins. Revoke
// EXECUTE on a volatile function to pin a session's determinism — calls to it then fail 42501.
let mut locked = db.session(SessionOptions::default());
locked.revoke(PrivilegeSet::EMPTY.with(Privilege::Execute), "uuidv4");
assert_eq!(locked.execute("SELECT uuidv4()", &[]).unwrap_err().code(), "42501");
locked.close();
Ok(())
}
package main
import (
"context"
"fmt"
"log"
jed "github.com/jackc/jed/impl/go"
)
func main() {
db, err := jed.CreateDatabase(jed.CreateOptions{Path: "app.jed"})
if err != nil {
log.Fatal(err)
}
defer db.Close()
ctx := context.Background()
mustExec(db, "CREATE TABLE report (id i32 PRIMARY KEY, body text)")
mustExec(db, "INSERT INTO report VALUES (1, 'hello')")
// Serve untrusted queries through a SESSION granted ONLY read access: DefaultPrivileges =
// {SELECT} (a read-only envelope) with DDL disabled. A session is a handle minted from the
// database that shares its committed state. The engine enforces the envelope at name resolution —
// any write or schema change resolves to 42501, with no in-database role catalog.
readOnly := jed.PrivSetEmpty.With(jed.PrivSelect)
noDDL := false
untrusted, err := db.Session(jed.SessionOptions{DefaultPrivileges: &readOnly, AllowDDL: &noDDL})
if err != nil {
log.Fatal(err)
}
defer untrusted.Close() // release the session (and its reader pin)
if _, err := untrusted.Exec(ctx, "SELECT body FROM report"); err != nil {
log.Fatal(err)
}
if _, err := untrusted.Exec(ctx, "DELETE FROM report"); err != nil {
fmt.Println("denied:", err) // 42501 permission denied for table report
}
// Grant/Revoke adjust one object at a time on a session's envelope, and revoke always wins. Revoke
// EXECUTE on a volatile function to pin a session's determinism — calls to it then fail 42501.
locked, err := db.Session(jed.SessionOptions{})
if err != nil {
log.Fatal(err)
}
defer locked.Close()
locked.Revoke(jed.PrivSetEmpty.With(jed.PrivExecute), "uuidv4")
if _, err := locked.Exec(ctx, "SELECT uuidv4()"); err != nil {
fmt.Println("denied:", err) // 42501
}
}
func mustExec(db *jed.Database, sql string) {
if _, err := db.Exec(context.Background(), sql); err != nil {
log.Fatal(err)
}
}
import { createDatabase, EngineError, PrivilegeSet } from 'jed-ts';
const db = await createDatabase({ path: 'app.jed' });
try {
await db.execute('CREATE TABLE report (id i32 PRIMARY KEY, body text)');
await db.execute("INSERT INTO report VALUES (1, 'hello')");
// Serve untrusted queries through a SESSION granted ONLY read access: defaultPrivileges = {select}
// (a read-only envelope) with DDL disabled. A session is a handle minted from the database that
// shares its committed state. The engine enforces the envelope at name resolution — any write or
// schema change resolves to 42501, with no in-database role catalog.
const untrusted = await db.session({
defaultPrivileges: PrivilegeSet.empty().with('select'),
allowDdl: false
});
try {
await untrusted.execute('SELECT body FROM report'); // ok
try {
await untrusted.execute('DELETE FROM report');
} catch (e) {
if (!(e instanceof EngineError) || e.code() !== '42501') throw e;
console.log('denied'); // 42501 permission denied for table report
}
} finally {
await untrusted.close(); // release the session (and its reader pin)
}
} finally {
await db.close();
}
What each statement needs
SELECTon every table a statement reads — itsFROM/JOINtables, subqueries anywhere in it, anINSERT … SELECTsource, and the columns anUPDATE/DELETEreads inWHERE/RETURNING/ an assignment. AnINSERTreads its target when it names anON CONFLICTtarget or has aRETURNINGthat references a column.INSERT/UPDATE/DELETEon the write target. A statement that both reads and writes needs both:UPDATE t … WHERE …needsUPDATEandSELECT; a bareINSERT INTO t VALUES …needs onlyINSERT;INSERT … ON CONFLICT (id) DO UPDATE …needsINSERT,UPDATE, andSELECT.- Views are granted like tables. Reading a view needs
SELECTon the view only by default; asecurity_invokerview also checks the relations its query reads.CREATE VIEWneedsallow_ddlplusSELECTon every table and view its query reads, so a session cannot create a view over data it could not read. EXECUTEon every named function it calls, wherever the call appears — including an aggregate’sFILTER, aWITHIN GROUPor windowOVER (…)clause, andORDER BY— and on the functions in the column defaults anINSERTfills orSET col = DEFAULTassigns and in the target’sCHECKconstraints. Built-in operators (+,=, …) are never gated — they are pure and unavoidable. RevokingEXECUTEonuuidv4()ornow()is the easy way to pin a session’s determinism.ANALYZE table [(columns)]needsSELECTon its target plusallow_ddl. Collection reads the table and transactionally replaces planner facts; this slice has no separateMAINTAINprivilege.
Existence is checked first
A privilege is required only once a name resolves to a real object. Selecting from a table that
does not exist is 42P01 (undefined table) even under an empty envelope — authorization gates what
exists, it never reveals what doesn’t by turning a different error code.