feat(session): gate IM/embed/API channel sessions behind admin
Restrict channel-managed sessions (API keys, IM, embed) to tenant admin in the Web console for list/read paths, hide channel sidebar buckets from non-admins, and add CountSessionsBySource for aggregate stats without exposing session rows to viewers.
This commit is contained in:
@@ -12,7 +12,8 @@ type SessionService interface {
|
||||
// CreateSession creates a session
|
||||
CreateSession(ctx context.Context, session *types.Session) (*types.Session, error)
|
||||
// GetSession gets a session, honoring the caller's per-user scope with an
|
||||
// Admin+ read fallback for tenant API-key sessions. Use only for read paths.
|
||||
// Admin+ read fallback for tenant channel sessions (API / IM / embed).
|
||||
// Use only for read paths.
|
||||
GetSession(ctx context.Context, id string) (*types.Session, error)
|
||||
// GetOwnedSession gets a session strictly within the caller's owner scope
|
||||
// (no Admin+ API-key fallback). Write/mutation endpoints must use this so a
|
||||
@@ -41,6 +42,9 @@ type SessionService interface {
|
||||
// ListSessions returns a page of sessions for the current tenant/user with
|
||||
// search/source filters and pin-aware ordering. User scope is pulled from ctx.
|
||||
ListSessions(ctx context.Context, query *types.SessionListQuery) (*types.PageResult, error)
|
||||
// CountSessionsBySource returns the total for a source filter without the
|
||||
// Admin+ gate applied by ListSessions (for aggregate stats endpoints).
|
||||
CountSessionsBySource(ctx context.Context, query *types.SessionListQuery) (int64, error)
|
||||
// SetSessionPinned pins or unpins the session for the current user scope.
|
||||
// Returns the number of rows affected; 0 signals "not found" to the handler.
|
||||
SetSessionPinned(ctx context.Context, sessionID string, pinned bool) (int64, error)
|
||||
|
||||
@@ -3,6 +3,7 @@ package types
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -139,12 +140,41 @@ func (s *Session) BeforeCreate(tx *gorm.DB) (err error) {
|
||||
// Owner/admin can observe API-key traffic that is otherwise isolated per key.
|
||||
const SessionSourceAPI = "api"
|
||||
|
||||
// SessionSourceWeb is the source filter for a user's own Web-console chats.
|
||||
const SessionSourceWeb = "web"
|
||||
|
||||
// SessionListSourceRequiresAdmin reports whether a session-list source filter
|
||||
// exposes tenant-wide channel traffic (API / IM / embed) in the Web console.
|
||||
func SessionListSourceRequiresAdmin(source string) bool {
|
||||
src := strings.TrimSpace(source)
|
||||
if src == "" || strings.EqualFold(src, SessionSourceWeb) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// SessionRequiresAdminConsoleRead reports whether a session row is channel-
|
||||
// managed traffic that non-admin web users must not open from the console.
|
||||
func SessionRequiresAdminConsoleRead(s *Session, imPlatform string) bool {
|
||||
if s == nil {
|
||||
return false
|
||||
}
|
||||
if strings.HasPrefix(s.UserID, SessionOwnerAPITenantKeyPrefix) {
|
||||
return true
|
||||
}
|
||||
if strings.HasPrefix(s.Description, EmbedSessionMarkerPrefix) ||
|
||||
strings.HasPrefix(s.UserID, PrincipalEmbedSession+":") {
|
||||
return true
|
||||
}
|
||||
return strings.TrimSpace(imPlatform) != ""
|
||||
}
|
||||
|
||||
// SessionListQuery bundles the parameters for listing sessions.
|
||||
// UserID empty means "tenant-wide" (used by API-key callers / legacy rows).
|
||||
// Keyword matches title ILIKE '%keyword%'.
|
||||
// Source values: "web" (user chats, no IM/embed), "embed" / "embed:{channelID}",
|
||||
// "api" (all API-key sessions, Admin+ only), or an IM platform name
|
||||
// (e.g. "feishu", "wechat").
|
||||
// (e.g. "feishu", "wechat"). IM and embed sources are also Admin+ only.
|
||||
// AgentID currently only filters sessions that have an IM channel mapping.
|
||||
type SessionListQuery struct {
|
||||
TenantID uint64
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package types
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestSessionListSourceRequiresAdmin(t *testing.T) {
|
||||
tests := []struct {
|
||||
source string
|
||||
want bool
|
||||
}{
|
||||
{"", false},
|
||||
{"web", false},
|
||||
{"WEB", false},
|
||||
{"api", true},
|
||||
{"embed", true},
|
||||
{"embed:ch-1", true},
|
||||
{"feishu", true},
|
||||
{"qqbot", true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
if got := SessionListSourceRequiresAdmin(tt.source); got != tt.want {
|
||||
t.Fatalf("SessionListSourceRequiresAdmin(%q) = %v, want %v", tt.source, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionRequiresAdminConsoleRead(t *testing.T) {
|
||||
api := &Session{UserID: SessionOwnerAPITenantKeyPrefix + "1:10"}
|
||||
if !SessionRequiresAdminConsoleRead(api, "") {
|
||||
t.Fatal("API-key session should require admin")
|
||||
}
|
||||
|
||||
embed := &Session{Description: EmbedSessionMarkerPrefix + "ch-1"}
|
||||
if !SessionRequiresAdminConsoleRead(embed, "") {
|
||||
t.Fatal("embed description should require admin")
|
||||
}
|
||||
|
||||
embedOwner := &Session{UserID: PrincipalEmbedSession + ":1:ch-1:sess-1"}
|
||||
if !SessionRequiresAdminConsoleRead(embedOwner, "") {
|
||||
t.Fatal("embed owner id should require admin")
|
||||
}
|
||||
|
||||
im := &Session{Title: "hello"}
|
||||
if !SessionRequiresAdminConsoleRead(im, "feishu") {
|
||||
t.Fatal("IM-mapped session should require admin")
|
||||
}
|
||||
|
||||
web := &Session{UserID: "alice", Title: "my chat"}
|
||||
if SessionRequiresAdminConsoleRead(web, "") {
|
||||
t.Fatal("personal web session should not require admin")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user