mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2026-09-20 15:12:00 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
817b781c88 | ||
|
|
87eaa9e18c |
@@ -25,12 +25,18 @@ func RegisterSecurityHooks(handler *Handler, securityList *security.SecurityList
|
|||||||
// Hook 1: BeforeRead - Load security rules
|
// Hook 1: BeforeRead - Load security rules
|
||||||
handler.Hooks().Register(BeforeRead, func(hookCtx *HookContext) error {
|
handler.Hooks().Register(BeforeRead, func(hookCtx *HookContext) error {
|
||||||
secCtx := newSecurityContext(hookCtx)
|
secCtx := newSecurityContext(hookCtx)
|
||||||
|
if security.IsModelSecurityDisabled(secCtx) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
return security.LoadSecurityRules(secCtx, securityList)
|
return security.LoadSecurityRules(secCtx, securityList)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Hook 2: BeforeScan - Apply row-level security filters
|
// Hook 2: BeforeScan - Apply row-level security filters
|
||||||
handler.Hooks().Register(BeforeScan, func(hookCtx *HookContext) error {
|
handler.Hooks().Register(BeforeScan, func(hookCtx *HookContext) error {
|
||||||
secCtx := newSecurityContext(hookCtx)
|
secCtx := newSecurityContext(hookCtx)
|
||||||
|
if security.ShouldSkipRowSecurity(secCtx, hookCtx.Operation) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
return security.ApplyRowSecurity(secCtx, securityList)
|
return security.ApplyRowSecurity(secCtx, securityList)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -97,6 +103,10 @@ func (s *securityContext) GetEntity() string {
|
|||||||
return s.ctx.Entity
|
return s.ctx.Entity
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *securityContext) GetOperation() string {
|
||||||
|
return s.ctx.Operation
|
||||||
|
}
|
||||||
|
|
||||||
func (s *securityContext) GetModel() interface{} {
|
func (s *securityContext) GetModel() interface{} {
|
||||||
return s.ctx.Model
|
return s.ctx.Model
|
||||||
}
|
}
|
||||||
|
|||||||
+59
-17
@@ -232,9 +232,37 @@ func LoadSecurityRules(secCtx SecurityContext, securityList *SecurityList) error
|
|||||||
// ApplyRowSecurity is a public wrapper for applyRowSecurity that accepts a SecurityContext
|
// ApplyRowSecurity is a public wrapper for applyRowSecurity that accepts a SecurityContext
|
||||||
// This allows other packages to apply row-level security using the generic interface
|
// This allows other packages to apply row-level security using the generic interface
|
||||||
func ApplyRowSecurity(secCtx SecurityContext, securityList *SecurityList) error {
|
func ApplyRowSecurity(secCtx SecurityContext, securityList *SecurityList) error {
|
||||||
|
// Spec adapters that expose the dispatched operation can enforce the same
|
||||||
|
// model-rule bypass even when ApplyRowSecurity is called directly.
|
||||||
|
if operationCtx, ok := secCtx.(interface{ GetOperation() string }); ok &&
|
||||||
|
ShouldSkipRowSecurity(secCtx, operationCtx.GetOperation()) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
return applyRowSecurity(secCtx, securityList)
|
return applyRowSecurity(secCtx, securityList)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ShouldSkipRowSecurity reports whether row-security enforcement should be
|
||||||
|
// skipped for the operation. It uses the same model-rule resolution as
|
||||||
|
// CheckModelAuthAllowed so the model registry remains the single source of
|
||||||
|
// truth for security behavior.
|
||||||
|
func ShouldSkipRowSecurity(secCtx SecurityContext, operation string) bool {
|
||||||
|
rules, ok := resolveModelRules(secCtx)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return rules.SecurityDisabled || (operation == "read" && rules.CanPublicRead)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsModelSecurityDisabled reports whether all model-level security processing
|
||||||
|
// is disabled for the model. This is distinct from ShouldSkipRowSecurity:
|
||||||
|
// CanPublicRead skips row filtering for reads but must still allow other read
|
||||||
|
// security, such as column masking, to be loaded.
|
||||||
|
func IsModelSecurityDisabled(secCtx SecurityContext) bool {
|
||||||
|
rules, ok := resolveModelRules(secCtx)
|
||||||
|
return ok && rules.SecurityDisabled
|
||||||
|
}
|
||||||
|
|
||||||
// ApplyColumnSecurity is a public wrapper for applyColumnSecurity that accepts a SecurityContext
|
// ApplyColumnSecurity is a public wrapper for applyColumnSecurity that accepts a SecurityContext
|
||||||
// This allows other packages to apply column-level security using the generic interface
|
// This allows other packages to apply column-level security using the generic interface
|
||||||
func ApplyColumnSecurity(secCtx SecurityContext, securityList *SecurityList) error {
|
func ApplyColumnSecurity(secCtx SecurityContext, securityList *SecurityList) error {
|
||||||
@@ -303,25 +331,14 @@ func checkModelDeleteAllowed(secCtx SecurityContext) error {
|
|||||||
// 7. Guest (UserID == 0) → return "authentication required".
|
// 7. Guest (UserID == 0) → return "authentication required".
|
||||||
// 8. Authenticated user → allow (operation-specific checks remain in BeforeUpdate/BeforeDelete).
|
// 8. Authenticated user → allow (operation-specific checks remain in BeforeUpdate/BeforeDelete).
|
||||||
func CheckModelAuthAllowed(secCtx SecurityContext, operation string) error {
|
func CheckModelAuthAllowed(secCtx SecurityContext, operation string) error {
|
||||||
rules, ok := GetModelRulesFromContext(secCtx.GetContext())
|
rules, ok := resolveModelRules(secCtx)
|
||||||
if !ok {
|
if !ok {
|
||||||
schema := secCtx.GetSchema()
|
// Model not registered - fall through to auth check
|
||||||
entity := secCtx.GetEntity()
|
userID, _ := secCtx.GetUserID()
|
||||||
var err error
|
if userID == 0 {
|
||||||
if schema != "" {
|
return fmt.Errorf("authentication required")
|
||||||
rules, err = modelregistry.GetModelRulesByName(fmt.Sprintf("%s.%s", schema, entity))
|
|
||||||
}
|
|
||||||
if err != nil || schema == "" {
|
|
||||||
rules, err = modelregistry.GetModelRulesByName(entity)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
// Model not registered - fall through to auth check
|
|
||||||
userID, _ := secCtx.GetUserID()
|
|
||||||
if userID == 0 {
|
|
||||||
return fmt.Errorf("authentication required")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if rules.SecurityDisabled {
|
if rules.SecurityDisabled {
|
||||||
@@ -347,6 +364,31 @@ func CheckModelAuthAllowed(secCtx SecurityContext, operation string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// resolveModelRules returns model rules from the request context first, then
|
||||||
|
// falls back to the schema-qualified and unqualified registry names.
|
||||||
|
func resolveModelRules(secCtx SecurityContext) (modelregistry.ModelRules, bool) {
|
||||||
|
if rules, ok := GetModelRulesFromContext(secCtx.GetContext()); ok {
|
||||||
|
return rules, true
|
||||||
|
}
|
||||||
|
|
||||||
|
schema := secCtx.GetSchema()
|
||||||
|
entity := secCtx.GetEntity()
|
||||||
|
var err error
|
||||||
|
if schema != "" {
|
||||||
|
var rules modelregistry.ModelRules
|
||||||
|
rules, err = modelregistry.GetModelRulesByName(fmt.Sprintf("%s.%s", schema, entity))
|
||||||
|
if err == nil {
|
||||||
|
return rules, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rules, err := modelregistry.GetModelRulesByName(entity)
|
||||||
|
if err != nil {
|
||||||
|
return modelregistry.ModelRules{}, false
|
||||||
|
}
|
||||||
|
return rules, true
|
||||||
|
}
|
||||||
|
|
||||||
// CheckModelUpdateAllowed is the public wrapper for checkModelUpdateAllowed.
|
// CheckModelUpdateAllowed is the public wrapper for checkModelUpdateAllowed.
|
||||||
func CheckModelUpdateAllowed(secCtx SecurityContext) error {
|
func CheckModelUpdateAllowed(secCtx SecurityContext) error {
|
||||||
return checkModelUpdateAllowed(secCtx)
|
return checkModelUpdateAllowed(secCtx)
|
||||||
|
|||||||
Reference in New Issue
Block a user