fix(security): verify passwords, bind row-security args, fail closed on panic

Verify bcrypt passwords in Direct mode and the shipped procedures, hash on
register/reset, ignore client-supplied roles and level at registration and
drop the password from the jwt_login payload. Legacy cleartext upgrade is
opt-in. Row security templates now bind the user as a parameter, validate
identifiers, attach via common.SelectQuery and fail the request if the
filter cannot be attached. ApplyColumnSecurity and GetRowSecurityTemplate
convert panics to errors and the hooks fail closed. Update audit status.
This commit is contained in:
Hein
2026-09-30 13:44:59 +02:00
parent f9c948ca4e
commit 8a94d884e7
12 changed files with 648 additions and 158 deletions
+49
View File
@@ -2,8 +2,10 @@ package security
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"reflect"
"sync"
"sync/atomic"
"testing"
@@ -145,3 +147,50 @@ func TestSplitTagDropsEmpty(t *testing.T) {
t.Fatalf("got %v", got)
}
}
func TestColumnSecurityPanicFailsClosed(t *testing.T) {
type Rec struct {
JSONCol string `json:"json_col" bun:"json_col"`
}
sl, _ := NewSecurityList(&slowProvider{})
sl.ColumnSecurity["public.t@1"] = []ColumnSecurity{{
Schema: "public", Tablename: "t", Path: []string{"JSONCol"}, Accesstype: "mask", UserID: 1,
}}
// A struct boxed in an interface is not addressable, so SetString panics.
recs := []any{Rec{JSONCol: "secret"}}
out, err := sl.ApplyColumnSecurity(reflect.ValueOf(recs), reflect.TypeOf(Rec{}), 1, "public", "t")
if err == nil {
t.Fatalf("panic must be returned as an error, got out=%v", out)
}
if errors.Is(err, ErrNoColumnSecurity) {
t.Fatal("a panic must not look like 'no rules'")
}
}
func TestNoRulesIsNotAnError(t *testing.T) {
sl, _ := NewSecurityList(&slowProvider{})
if _, err := sl.GetRowSecurityTemplate(1, "s", "t"); !errors.Is(err, ErrNoRowSecurity) {
t.Fatalf("got %v", err)
}
if _, err := sl.ApplyColumnSecurity(reflect.ValueOf([]int{}), reflect.TypeOf(0), 1, "s", "t"); !errors.Is(err, ErrNoColumnSecurity) {
t.Fatalf("got %v", err)
}
}
func TestApplyColumnSecurityHookFailsClosedOnPanic(t *testing.T) {
type Rec struct {
JSONCol string `bun:"json_col"`
}
sl, _ := NewSecurityList(&slowProvider{})
sl.ColumnSecurity["public.t@1"] = []ColumnSecurity{{
Schema: "public", Tablename: "t", Path: []string{"JSONCol"}, Accesstype: "mask", UserID: 1,
}}
secCtx := &mockSecurityContext{
ctx: context.Background(), userID: 1, hasUser: true, schema: "public", entity: "t",
model: &Rec{}, result: []any{Rec{JSONCol: "secret"}},
}
if err := ApplyColumnSecurity(secCtx, sl); err == nil {
t.Fatal("a panic during masking must fail the request, not return unmasked data")
}
}