feat(web): Go W-G5 session, auth, password, and RBAC gates
Wire password hash, session CRUD, auth login/check/logout, and app.auth/gate ABIs so security and C0/C1 offline smokes pass on libweb.so. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
4357045904
commit
6e94d0a179
14 files changed
+1592
-10
No files matched your search
@@ -0,0 +1,437 @@
|
||||
// Package session ports in-memory / SQLite session store from the Rust web plugin.
|
||||
package session
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/db"
|
||||
)
|
||||
|
||||
const (
|
||||
sessionTable = "_marqdo_sessions"
|
||||
csrfKey = "_csrf"
|
||||
defaultTTL = 3600
|
||||
)
|
||||
|
||||
// Config controls session backend.
|
||||
type Config struct {
|
||||
DBURL string
|
||||
TTLSec uint64
|
||||
CookieSecure bool
|
||||
}
|
||||
|
||||
var (
|
||||
cfgMu sync.Mutex
|
||||
cfg = Config{TTLSec: defaultTTL}
|
||||
|
||||
memMu sync.Mutex
|
||||
mem = map[string]memRec{}
|
||||
memTTL uint64 = defaultTTL
|
||||
)
|
||||
|
||||
type memRec struct {
|
||||
exp uint64
|
||||
data map[string]any
|
||||
}
|
||||
|
||||
func nowSecs() uint64 {
|
||||
return uint64(time.Now().Unix())
|
||||
}
|
||||
|
||||
func cloneData(m map[string]any) map[string]any {
|
||||
out := make(map[string]any, len(m))
|
||||
for k, v := range m {
|
||||
out[k] = v
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Configure sets session backend before listen or offline ABI calls.
|
||||
func Configure(c Config) {
|
||||
ttl := c.TTLSec
|
||||
if ttl == 0 {
|
||||
ttl = defaultTTL
|
||||
}
|
||||
c.TTLSec = ttl
|
||||
if c.DBURL != "" {
|
||||
_ = ensureSessionTable(c.DBURL)
|
||||
}
|
||||
cfgMu.Lock()
|
||||
cfg = c
|
||||
cfgMu.Unlock()
|
||||
}
|
||||
|
||||
func getCfg() Config {
|
||||
cfgMu.Lock()
|
||||
defer cfgMu.Unlock()
|
||||
return cfg
|
||||
}
|
||||
|
||||
// Reset clears in-memory sessions (tests / each listen).
|
||||
func Reset(ttlSec uint64) {
|
||||
ttl := ttlSec
|
||||
if ttl == 0 {
|
||||
ttl = defaultTTL
|
||||
}
|
||||
cfgMu.Lock()
|
||||
cfg.TTLSec = ttl
|
||||
if cfg.DBURL == "" {
|
||||
// keep cookie_secure / db as-is when already configured
|
||||
}
|
||||
cfgMu.Unlock()
|
||||
memMu.Lock()
|
||||
mem = map[string]memRec{}
|
||||
memTTL = ttl
|
||||
memMu.Unlock()
|
||||
}
|
||||
|
||||
func ensureSessionTable(url string) error {
|
||||
_, err := db.Exec(url, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS "%s" (
|
||||
id TEXT PRIMARY KEY,
|
||||
expires_at INTEGER NOT NULL,
|
||||
data TEXT NOT NULL
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_marqdo_sessions_exp ON "%s"(expires_at);`, sessionTable, sessionTable), nil)
|
||||
return err
|
||||
}
|
||||
|
||||
func pruneMem() {
|
||||
now := nowSecs()
|
||||
for id, rec := range mem {
|
||||
if rec.exp <= now {
|
||||
delete(mem, id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func pruneSQL(url string) {
|
||||
now := int64(nowSecs())
|
||||
_, _ = db.Exec(url, fmt.Sprintf(`DELETE FROM "%s" WHERE expires_at <= ?`, sessionTable), []any{now})
|
||||
}
|
||||
|
||||
func secureRandomID() (string, error) {
|
||||
b := make([]byte, 32)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return hex.EncodeToString(b), nil
|
||||
}
|
||||
|
||||
func randomCSRF() (string, error) {
|
||||
b := make([]byte, 24)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return hex.EncodeToString(b), nil
|
||||
}
|
||||
|
||||
func ensureCSRF(data map[string]any) (string, error) {
|
||||
if v, ok := data[csrfKey].(string); ok && v != "" {
|
||||
return v, nil
|
||||
}
|
||||
t, err := randomCSRF()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
data[csrfKey] = t
|
||||
return t, nil
|
||||
}
|
||||
|
||||
func loadSQL(url, id string) (uint64, map[string]any, bool) {
|
||||
q, err := db.Query(url, fmt.Sprintf(`SELECT expires_at, data FROM "%s" WHERE id = ?`, sessionTable), []any{id})
|
||||
if err != nil {
|
||||
return 0, nil, false
|
||||
}
|
||||
rows, _ := q["rows"].([]any)
|
||||
if len(rows) == 0 {
|
||||
return 0, nil, false
|
||||
}
|
||||
row, ok := rows[0].(map[string]any)
|
||||
if !ok {
|
||||
return 0, nil, false
|
||||
}
|
||||
exp := uint64(asInt64(row["expires_at"]))
|
||||
dataS, _ := row["data"].(string)
|
||||
data := map[string]any{}
|
||||
_ = json.Unmarshal([]byte(dataS), &data)
|
||||
if data == nil {
|
||||
data = map[string]any{}
|
||||
}
|
||||
return exp, data, true
|
||||
}
|
||||
|
||||
func asInt64(v any) int64 {
|
||||
switch t := v.(type) {
|
||||
case int64:
|
||||
return t
|
||||
case float64:
|
||||
return int64(t)
|
||||
case int:
|
||||
return int64(t)
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func saveSQL(url, id string, exp uint64, data map[string]any) error {
|
||||
raw, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = db.Exec(url, fmt.Sprintf(`
|
||||
INSERT INTO "%s" (id, expires_at, data) VALUES (?, ?, ?)
|
||||
ON CONFLICT(id) DO UPDATE SET expires_at = excluded.expires_at, data = excluded.data`, sessionTable),
|
||||
[]any{id, int64(exp), string(raw)})
|
||||
return err
|
||||
}
|
||||
|
||||
func deleteSQL(url, id string) {
|
||||
_, _ = db.Exec(url, fmt.Sprintf(`DELETE FROM "%s" WHERE id = ?`, sessionTable), []any{id})
|
||||
}
|
||||
|
||||
func touchExpiry(ttl uint64) uint64 {
|
||||
return nowSecs() + ttl
|
||||
}
|
||||
|
||||
func getRecord(id string) (map[string]any, bool) {
|
||||
c := getCfg()
|
||||
now := nowSecs()
|
||||
if c.DBURL != "" {
|
||||
pruneSQL(c.DBURL)
|
||||
exp, data, ok := loadSQL(c.DBURL, id)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
if exp <= now {
|
||||
deleteSQL(c.DBURL, id)
|
||||
return nil, false
|
||||
}
|
||||
return data, true
|
||||
}
|
||||
memMu.Lock()
|
||||
defer memMu.Unlock()
|
||||
pruneMem()
|
||||
rec, ok := mem[id]
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
if rec.exp <= now {
|
||||
delete(mem, id)
|
||||
return nil, false
|
||||
}
|
||||
return cloneData(rec.data), true
|
||||
}
|
||||
|
||||
func putRecord(id string, exp uint64, data map[string]any) bool {
|
||||
c := getCfg()
|
||||
if c.DBURL != "" {
|
||||
return saveSQL(c.DBURL, id, exp, data) == nil
|
||||
}
|
||||
memMu.Lock()
|
||||
defer memMu.Unlock()
|
||||
mem[id] = memRec{exp: exp, data: cloneData(data)}
|
||||
return true
|
||||
}
|
||||
|
||||
// NewID creates a session and returns its id (optional ttl; 0 = configured default).
|
||||
func NewID(ttlSec uint64) string {
|
||||
c := getCfg()
|
||||
ttl := ttlSec
|
||||
if ttl == 0 {
|
||||
ttl = c.TTLSec
|
||||
}
|
||||
if ttl == 0 {
|
||||
ttl = defaultTTL
|
||||
}
|
||||
if ttlSec > 0 {
|
||||
cfgMu.Lock()
|
||||
cfg.TTLSec = ttl
|
||||
cfgMu.Unlock()
|
||||
}
|
||||
id, err := secureRandomID()
|
||||
if err != nil {
|
||||
id = fmt.Sprintf("fallback-%d", nowSecs())
|
||||
}
|
||||
data := map[string]any{}
|
||||
_, _ = ensureCSRF(data)
|
||||
exp := touchExpiry(ttl)
|
||||
if c.DBURL != "" {
|
||||
pruneSQL(c.DBURL)
|
||||
_ = saveSQL(c.DBURL, id, exp, data)
|
||||
} else {
|
||||
memMu.Lock()
|
||||
memTTL = ttl
|
||||
pruneMem()
|
||||
mem[id] = memRec{exp: exp, data: data}
|
||||
memMu.Unlock()
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
// New is ABI shape web_session_new → {"id":"…"}.
|
||||
func New(ttlSec uint64) map[string]any {
|
||||
return map[string]any{"id": NewID(ttlSec)}
|
||||
}
|
||||
|
||||
// Set stores a key (sliding expiry). ABI: {"ok":bool}.
|
||||
func Set(id, key string, value any) map[string]any {
|
||||
return map[string]any{"ok": SetValue(id, key, value)}
|
||||
}
|
||||
|
||||
// SetValue is the low-level setter.
|
||||
func SetValue(id, key string, value any) bool {
|
||||
data, ok := getRecord(id)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
data[key] = value
|
||||
return putRecord(id, touchExpiry(getCfg().TTLSec), data)
|
||||
}
|
||||
|
||||
// Get is ABI shape: {"ok":true,"value":…} or {"ok":false}.
|
||||
func Get(id, key string) map[string]any {
|
||||
v, ok := GetValue(id, key)
|
||||
if !ok {
|
||||
return map[string]any{"ok": false}
|
||||
}
|
||||
return map[string]any{"ok": true, "value": v}
|
||||
}
|
||||
|
||||
// GetValue returns the value and whether it existed (sliding expiry on hit).
|
||||
func GetValue(id, key string) (any, bool) {
|
||||
data, ok := getRecord(id)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
v, exists := data[key]
|
||||
if !exists {
|
||||
return nil, false
|
||||
}
|
||||
putRecord(id, touchExpiry(getCfg().TTLSec), data)
|
||||
return v, true
|
||||
}
|
||||
|
||||
// Del removes a key. ABI: {"ok":bool}.
|
||||
func Del(id, key string) map[string]any {
|
||||
return map[string]any{"ok": DelKey(id, key)}
|
||||
}
|
||||
|
||||
// DelKey is the low-level deleter.
|
||||
func DelKey(id, key string) bool {
|
||||
data, ok := getRecord(id)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
if _, exists := data[key]; !exists {
|
||||
return false
|
||||
}
|
||||
delete(data, key)
|
||||
return putRecord(id, touchExpiry(getCfg().TTLSec), data)
|
||||
}
|
||||
|
||||
// Destroy removes a session. ABI: {"ok":bool}.
|
||||
func Destroy(id string) map[string]any {
|
||||
return map[string]any{"ok": DestroyID(id)}
|
||||
}
|
||||
|
||||
// DestroyID is the low-level destroyer.
|
||||
func DestroyID(id string) bool {
|
||||
c := getCfg()
|
||||
if c.DBURL != "" {
|
||||
deleteSQL(c.DBURL, id)
|
||||
return true
|
||||
}
|
||||
memMu.Lock()
|
||||
defer memMu.Unlock()
|
||||
_, ok := mem[id]
|
||||
if ok {
|
||||
delete(mem, id)
|
||||
}
|
||||
return ok
|
||||
}
|
||||
|
||||
// CSRFFor returns (creating if needed) the CSRF token for a session.
|
||||
func CSRFFor(id string) (string, bool) {
|
||||
data, ok := getRecord(id)
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
tok, err := ensureCSRF(data)
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
putRecord(id, touchExpiry(getCfg().TTLSec), data)
|
||||
return tok, true
|
||||
}
|
||||
|
||||
// ValidateCSRF checks token against session.
|
||||
func ValidateCSRF(id, token string) bool {
|
||||
if token == "" {
|
||||
return false
|
||||
}
|
||||
v, ok := GetValue(id, csrfKey)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
s, _ := v.(string)
|
||||
return s == token
|
||||
}
|
||||
|
||||
// SessionCookie builds Set-Cookie for marqdo_sid.
|
||||
func SessionCookie(id string, ttlSec uint64, secure bool) string {
|
||||
s := fmt.Sprintf("marqdo_sid=%s; HttpOnly; Path=/; SameSite=Lax; Max-Age=%d", id, ttlSec)
|
||||
if secure {
|
||||
s += "; Secure"
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// IDFromCookie parses marqdo_sid from a Cookie header.
|
||||
func IDFromCookie(cookieHeader string) (string, bool) {
|
||||
if cookieHeader == "" {
|
||||
return "", false
|
||||
}
|
||||
for _, part := range strings.Split(cookieHeader, ";") {
|
||||
p := strings.TrimSpace(part)
|
||||
if v, ok := strings.CutPrefix(p, "marqdo_sid="); ok {
|
||||
v = strings.Trim(v, `"`)
|
||||
if v != "" {
|
||||
return v, true
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// ParseRolesCSV splits "admin,author" into lowercased roles.
|
||||
func ParseRolesCSV(raw string) []string {
|
||||
var out []string
|
||||
for _, part := range strings.Split(raw, ",") {
|
||||
part = strings.TrimSpace(part)
|
||||
if part != "" {
|
||||
out = append(out, strings.ToLower(part))
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// RoleAllowed reports whether role is in allowed (empty allowed = all).
|
||||
func RoleAllowed(role string, allowed []string) bool {
|
||||
if len(allowed) == 0 {
|
||||
return true
|
||||
}
|
||||
role = strings.ToLower(role)
|
||||
for _, a := range allowed {
|
||||
if strings.EqualFold(a, role) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package session_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/db"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/session"
|
||||
)
|
||||
|
||||
func TestSessionSetGetDel(t *testing.T) {
|
||||
session.Configure(session.Config{TTLSec: 3600})
|
||||
session.Reset(3600)
|
||||
id := session.NewID(0)
|
||||
if id == "" {
|
||||
t.Fatal("empty id")
|
||||
}
|
||||
if !session.SetValue(id, "username", "admin") {
|
||||
t.Fatal("set")
|
||||
}
|
||||
v, ok := session.GetValue(id, "username")
|
||||
if !ok || v != "admin" {
|
||||
t.Fatalf("get=%v ok=%v", v, ok)
|
||||
}
|
||||
got := session.Get(id, "username")
|
||||
if got["ok"] != true || got["value"] != "admin" {
|
||||
t.Fatalf("Get ABI: %v", got)
|
||||
}
|
||||
if !session.DelKey(id, "username") {
|
||||
t.Fatal("del")
|
||||
}
|
||||
if _, ok := session.GetValue(id, "username"); ok {
|
||||
t.Fatal("del leftover")
|
||||
}
|
||||
if !session.DestroyID(id) {
|
||||
t.Fatal("destroy")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCSRFAndCookie(t *testing.T) {
|
||||
session.Reset(3600)
|
||||
id := session.NewID(0)
|
||||
tok, ok := session.CSRFFor(id)
|
||||
if !ok || tok == "" {
|
||||
t.Fatal("csrf")
|
||||
}
|
||||
if !session.ValidateCSRF(id, tok) {
|
||||
t.Fatal("validate")
|
||||
}
|
||||
if session.ValidateCSRF(id, "bad") {
|
||||
t.Fatal("bad csrf")
|
||||
}
|
||||
cookie := session.SessionCookie(id, 3600, false)
|
||||
got, ok := session.IDFromCookie("theme=light; " + cookie)
|
||||
if !ok || got != id {
|
||||
t.Fatalf("cookie parse %q != %q", got, id)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionPersistsInSQLite(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "persist.db")
|
||||
url := "sqlite:" + path
|
||||
db.ResetPool()
|
||||
session.Configure(session.Config{DBURL: url, TTLSec: 3600})
|
||||
id := session.NewID(0)
|
||||
if !session.SetValue(id, "theme", "dark") {
|
||||
t.Fatal("set")
|
||||
}
|
||||
session.Configure(session.Config{DBURL: url, TTLSec: 3600})
|
||||
v, ok := session.GetValue(id, "theme")
|
||||
if !ok || v != "dark" {
|
||||
t.Fatalf("persist get=%v ok=%v", v, ok)
|
||||
}
|
||||
_ = os.RemoveAll(dir)
|
||||
}
|
||||
|
||||
func TestNewABI(t *testing.T) {
|
||||
session.Reset(120)
|
||||
out := session.New(120)
|
||||
id, _ := out["id"].(string)
|
||||
if id == "" {
|
||||
t.Fatal(out)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user