feat(session): implement strict owner scope for API-key sessions

Add GetOwnedSession method to enforce strict ownership checks for session mutations, ensuring tenant admins can read but not modify API-key sessions. Update relevant handlers to utilize this method for operations that alter session data, maintaining security and integrity of API-key sessions.
This commit is contained in:
wizardchen
2026-07-20 19:27:25 +08:00
committed by lyingbug
parent 76a4ffe525
commit 29bec6c613
9 changed files with 95 additions and 12 deletions
+7 -3
View File
@@ -195,10 +195,14 @@ func (r *sessionRepository) QueryPaged(
// and cleared the per-user scope for this source.
return db.Where("s.user_id LIKE ?", types.SessionOwnerAPITenantKeyPrefix+"%")
case "web":
// User web chats only — exclude embed-widget sessions (same IM-null row).
// User web chats only — exclude embed-widget sessions (same IM-null
// row) and tenant API-key sessions (surfaced only in the admin-only
// "api" bucket). The user_id NULL check keeps legacy tenant-level web
// rows visible, since "col NOT LIKE ?" is unknown (not true) for NULL.
return db.Where(
"ics.id IS NULL AND (s.description = '' OR s.description NOT LIKE ?)",
embedPrefix+"%",
"ics.id IS NULL AND (s.description = '' OR s.description NOT LIKE ?) "+
"AND (s.user_id IS NULL OR s.user_id NOT LIKE ?)",
embedPrefix+"%", types.SessionOwnerAPITenantKeyPrefix+"%",
)
case "embed":
return db.Where("ics.id IS NULL AND s.description LIKE ?", embedPrefix+"%")