From 537d96d5a87c736ddcfee5a1bbe4660e0664871d Mon Sep 17 00:00:00 2001 From: Anthony Ettinger Date: Mon, 30 Nov 2026 01:18:02 -0800 Subject: [PATCH] security: keep the SQLite database owner-only (0600) (#180) SQLite creates agentbbs.db and its -wal/-shm files 0644, so any account on the host could read members' emails, DMs and login IPs; Caddy did, through a planted homepage symlink. Open now chmods all three to 0600. Co-authored-by: Claude Opus 5.5 --- internal/store/store.go | 18 +++++++++++++++ internal/store/store_filemode_test.go | 33 +++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 internal/store/store_filemode_test.go diff --git a/internal/store/store.go b/internal/store/store.go index 919661e..5a2f66f 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -7,6 +7,7 @@ import ( "database/sql" "errors" "fmt" + "os" "strings" "time" @@ -403,9 +404,26 @@ func Open(path string) (Store, error) { db.Close() return nil, err } + if err := restrictFileMode(path); err != nil { + db.Close() + return nil, err + } return &sqliteStore{db: db}, nil } +// restrictFileMode makes the database and its WAL/SHM files owner-only. +// SQLite creates them 0644, which let any account on the host (Caddy +// included) read every member's email, DMs and login IPs (2026-11 incident). +// SQLite gives later -wal/-shm files the main file's mode, so this sticks. +func restrictFileMode(path string) error { + for _, p := range []string{path, path + "-wal", path + "-shm"} { + if err := os.Chmod(p, 0o600); err != nil && !errors.Is(err, os.ErrNotExist) { + return fmt.Errorf("chmod %s: %w", p, err) + } + } + return nil +} + // migrate applies additive schema changes that must not fail on existing // databases. New columns live here (not in schema) so there is one code path. func migrate(db *sql.DB) error { diff --git a/internal/store/store_filemode_test.go b/internal/store/store_filemode_test.go new file mode 100644 index 0000000..6668b74 --- /dev/null +++ b/internal/store/store_filemode_test.go @@ -0,0 +1,33 @@ +package store + +import ( + "os" + "path/filepath" + "testing" +) + +// The database holds members' emails, DMs and login IPs, so no other account +// on the host may read it, including after a write creates the WAL file. +func TestOpenMakesDatabaseOwnerOnly(t *testing.T) { + path := filepath.Join(t.TempDir(), "t.db") + st, err := Open(path) + if err != nil { + t.Fatalf("open: %v", err) + } + defer st.Close() + if _, err := st.EnsureUser("alice", "member", "SHA256:a"); err != nil { + t.Fatalf("write: %v", err) + } + for _, p := range []string{path, path + "-wal", path + "-shm"} { + fi, err := os.Stat(p) + if os.IsNotExist(err) { + continue + } + if err != nil { + t.Fatalf("stat %s: %v", p, err) + } + if mode := fi.Mode().Perm(); mode&0o077 != 0 { + t.Errorf("%s is %o, want no group/other access", filepath.Base(p), mode) + } + } +}