Phase 13, part 2: each team's own database role
A team's connections take a Postgres role that owns its schema and may use the shared tables, nothing else: even SQL naming another team's schema is refused. The second guard behind the search_path. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,6 +2,7 @@ package db
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -73,6 +74,18 @@ func TestTeamsSchemas(t *testing.T) {
|
||||
if err := a.QueryRow(`SELECT count(*) FROM teams`).Scan(&n); err != nil || n != 1 {
|
||||
t.Errorf("the shared teams table from a team: %d %v", n, err)
|
||||
}
|
||||
// The second guard: a team's connections can't reach another team's
|
||||
// schema, nor the main schema's own copies of the team tables, even
|
||||
// by name.
|
||||
for _, q := range []string{
|
||||
`SELECT count(*) FROM ` + teams.Schema(2) + `.settings`,
|
||||
`SELECT count(*) FROM ` + d.mainSchema(t) + `.settings`,
|
||||
`INSERT INTO ` + teams.Schema(2) + `.settings (key, value) VALUES ('x', '"y"')`,
|
||||
} {
|
||||
if _, err := a.Exec(q); err == nil || !strings.Contains(err.Error(), "permission denied") {
|
||||
t.Errorf("%s: %v", q, err)
|
||||
}
|
||||
}
|
||||
// Creating again only migrates; dropping removes it all.
|
||||
if err := teams.Create(1); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -84,3 +97,12 @@ func TestTeamsSchemas(t *testing.T) {
|
||||
t.Error("a dropped team opens")
|
||||
}
|
||||
}
|
||||
|
||||
// mainSchema is the database's main schema (a test's own).
|
||||
func (d *DB) mainSchema(t *testing.T) string {
|
||||
var s string
|
||||
if err := d.SQL.QueryRow(`SELECT current_schema()`).Scan(&s); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user