larry-zy commented on code in PR #1592: URL: https://github.com/apache/dubbo-admin/pull/1592#discussion_r4302652710
########## ai/security/security.go: ########## @@ -0,0 +1,183 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package security + +import ( + "context" + "errors" + "net/http" + "strings" +) + +const ( + AuthMethodStatic = "static" + AuthMethodSystem = "system" + + ScopeSessionAdmin = "ai:sessions:admin" +) + +var ( + ErrAuthenticationRequired = errors.New("authentication required") + ErrResourceNotAccessible = errors.New("resource not accessible") +) + +// Subject is the authenticated identity supplied by a trusted server-side +// authenticator. Request payloads and ordinary client headers are not identity +// sources. +type Subject struct { + TenantID string + UserID string + Roles []string + Scopes []string + AuthMethod string +} + +func (s Subject) Validate() error { + if strings.TrimSpace(s.TenantID) == "" || strings.TrimSpace(s.UserID) == "" || strings.TrimSpace(s.AuthMethod) == "" { + return ErrAuthenticationRequired + } + return nil +} + +func (s Subject) HasScope(scope string) bool { + for _, candidate := range s.Scopes { + if candidate == scope { + return true + } + } + return false +} + +// ResourceReference is an untrusted resource requested by a caller. Its +// presence in AccessContext is not proof that access has been authorized. +type ResourceReference struct { + Kind string + ID string +} + +// AccessContext is passed explicitly to every Store operation that can read or +// mutate user-owned data. +type AccessContext struct { + Subject Subject + ResourceReferences []ResourceReference + systemCapability bool +} + +func NewAccessContext(subject Subject, references ...ResourceReference) (AccessContext, error) { + if err := subject.Validate(); err != nil { + return AccessContext{}, err + } + subject.Roles = append([]string(nil), subject.Roles...) + subject.Scopes = append([]string(nil), subject.Scopes...) + return AccessContext{Subject: subject, ResourceReferences: append([]ResourceReference(nil), references...)}, nil +} + +func MustAccessContext(subject Subject, references ...ResourceReference) AccessContext { + access, err := NewAccessContext(subject, references...) + if err != nil { + panic(err) + } + return access +} + +func SystemAccessContext() AccessContext { + return AccessContext{ + Subject: Subject{TenantID: "system", UserID: "system", AuthMethod: AuthMethodSystem}, + systemCapability: true, + } +} + +func (a AccessContext) Validate() error { return a.Subject.Validate() } + +func (a AccessContext) IsSystem() bool { return a.systemCapability } + +// CanAccessOwner intentionally grants no cross-tenant access. The reserved +// admin scope permits managing other owners only within the same tenant. +func (a AccessContext) CanAccessOwner(tenantID, ownerID string) bool { + if a.Validate() != nil { + return false + } + if a.IsSystem() { + return true + } + if a.Subject.TenantID != tenantID { + return false + } + return a.Subject.UserID == ownerID || a.Subject.HasScope(ScopeSessionAdmin) Review Comment: [P2] Keep partially owned sessions isolated from business access This admin branch grants access even when `ownerID` is empty, and GORM's admin query also omits the owner predicate. Without running legacy adoption, I reproduced HTTP GET, LIST, and DELETE returning 200 for same-tenant legacy sessions with `owner_id = ''` or `NULL`, while an ordinary user's GET returned 404. This contradicts the ADR's stated policy that partially owned rows remain isolated for explicit repair. Please reject incomplete ownership in both the policy and GORM queries before applying the same-tenant admin override. ########## ai/store/gorm/store.go: ########## @@ -626,6 +685,28 @@ func (s *GormStore) findSession(tx *gorm.DB, sessionID string, lock bool) (*Sess return &model, nil } +func (s *GormStore) findActiveSession(tx *gorm.DB, access security.AccessContext, sessionID string, lock bool) (*SessionModel, error) { + model, err := s.findSession(tx, access, sessionID, lock) + if err != nil { + return nil, err + } + if err := conversationstore.ValidateSessionActive(sessionID, model.Status, isExpired(model, time.Now())); err != nil { + return nil, err + } + return model, nil +} + +func scopeSessions(query *gorm.DB, access security.AccessContext) *gorm.DB { + if access.IsSystem() { + return query + } + query = query.Where("tenant_id = ?", access.Subject.TenantID) + if !access.Subject.HasScope(security.ScopeSessionAdmin) { + query = query.Where("owner_id = ?", access.Subject.UserID) Review Comment: [P1] Use exact identity comparison for Store ownership checks These predicates inherit the column collation, while `AccessContext.CanAccessOwner` uses exact Go string equality. With case-insensitive identity columns, the GORM-backed memory tool can therefore read another tenant's or user's history when the identity differs only in case: `memory_all_by_session_id -> AllMemory -> findActiveSession -> scopeSessions` has no policy recheck. I reproduced both tenant-only and owner-only variants through the real memory tool using SQLite `NOCASE`; the case-sensitive controls were denied. Please align Store ownership comparisons with `CanAccessOwner`. Direct HTTP reads and deletes remain blocked by Manager's secondary authorization; no MySQL instance was tested. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
