Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(session): scope API runtime admin-console bypass to owned sessions

wizardchendb9bdc27cdff
diff --git a/internal/application/service/session.go b/internal/application/service/session.go
index 935c465e7..f6663e961 100644
--- a/internal/application/service/session.go
+++ b/internal/application/service/session.go
@@ -22,6 +22,30 @@ func sessionUserIDFromContext(ctx context.Context) string {
return types.SessionOwnerIDFromContext(ctx)
}
+// runtimeMayBypassAdminConsoleRead reports whether a non-admin caller on the
+// owner-scoped read path may open a channel-managed session. Admin console reads
+// use the GetByID fallback in loadSessionForRead and never call this helper.
+func runtimeMayBypassAdminConsoleRead(
+ ctx context.Context,
+ session *types.Session,
+ imPlatform string,
+) bool {
+ principal, ok := types.PrincipalFromContext(ctx)
+ if !ok || session == nil {
+ return false
+ }
+
+ switch principal.Type {
+ case types.PrincipalIMUser:
+ return strings.TrimSpace(imPlatform) != ""
+ case types.PrincipalAPITenant, types.PrincipalAPIExternalUser:
+ ownerID := types.SessionOwnerIDFromContext(ctx)
+ return types.IsAPISessionOwnerID(session.UserID) && session.UserID == ownerID
+ default:
+ return false
+ }
+}
+
// loadSessionForRead loads a session honoring the caller's per-user scope, with
// an Admin+ fallback that additionally permits reading tenant channel sessions
// (API-key, IM, and embed) from the Web console. Non-admin callers must not
@@ -34,22 +58,13 @@ func loadSessionForRead(
ownerID, sessionID string,
) (*types.Session, error) {
isAdmin := types.TenantRoleFromContext(ctx).HasPermission(types.TenantRoleAdmin)
- principal, hasPrincipal := types.PrincipalFromContext(ctx)
- isChannelRuntime := false
- if hasPrincipal {
- switch principal.Type {
- case types.PrincipalAPITenant,
- types.PrincipalAPIPlatform,
- types.PrincipalAPIExternalUser,
- types.PrincipalIMUser:
- isChannelRuntime = true
- }
- }
session, err := repo.Get(ctx, tenantID, ownerID, sessionID)
if err == nil {
imPlatform, _ := repo.GetIMPlatform(ctx, tenantID, sessionID)
- if types.SessionRequiresAdminConsoleRead(session, imPlatform) && !isAdmin && !isChannelRuntime {
+ if types.SessionRequiresAdminConsoleRead(session, imPlatform) &&
+ !isAdmin &&
+ !runtimeMayBypassAdminConsoleRead(ctx, session, imPlatform) {
return nil, apperrors.ErrSessionNotFound
}
if imPlatform != "" {
diff --git a/internal/application/service/session_user_scope_test.go b/internal/application/service/session_user_scope_test.go
index 7045f6076..ceeb336c3 100644
--- a/internal/application/service/session_user_scope_test.go
+++ b/internal/application/service/session_user_scope_test.go
@@ -29,6 +29,17 @@ func testAPISessionScopeContext(tenantID uint64, externalUserID string) context.
})
}
+func testAPITenantKeyScopeContext(tenantID uint64, keyID uint64) context.Context {
+ ctx := context.WithValue(context.Background(), types.TenantIDContextKey, tenantID)
+ ctx = context.WithValue(ctx, types.UserIDContextKey, "system-1")
+ ctx = context.WithValue(ctx, types.TenantRoleContextKey, types.TenantRoleViewer)
+ ctx = types.WithPrincipal(ctx, types.Principal{
+ Type: types.PrincipalAPITenant,
+ ID: "1",
+ })
+ return types.WithTenantAPIKeyScope(ctx, types.TenantAPIKeyScope{KeyID: keyID})
+}
+
func newTestSessionService(t *testing.T) (*sessionService, *gorm.DB) {
t.Helper()
@@ -319,6 +330,49 @@ func TestGetSessionDeniesViewerOnIMSession(t *testing.T) {
require.Equal(t, "feishu", got.IMPlatform)
}
+func TestGetSessionAllowsAPITenantRuntimeToReadOwnAPIKeySession(t *testing.T) {
+ svc, db := newTestSessionService(t)
+
+ apiSession := &types.Session{
+ TenantID: 1,
+ UserID: types.SessionOwnerAPITenantKeyPrefix + "1:10",
+ Title: "api key session",
+ }
+ require.NoError(t, db.Create(apiSession).Error)
+
+ got, err := svc.GetSession(testAPITenantKeyScopeContext(1, 10), apiSession.ID)
+ require.NoError(t, err)
+ require.Equal(t, apiSession.ID, got.ID)
+}
+
+func TestGetSessionDeniesAPITenantRuntimeFromReadingIMSession(t *testing.T) {
+ svc, db := newTestSessionService(t)
+ require.NoError(t, db.AutoMigrate(&testListSessionsIMChannelSession{}))
+
+ imSession := &types.Session{TenantID: 1, Title: "feishu chat"}
+ require.NoError(t, db.Create(imSession).Error)
+ require.NoError(t, db.Create(&testListSessionsIMChannelSession{
+ SessionID: imSession.ID, Platform: "feishu",
+ }).Error)
+
+ _, err := svc.GetSession(testAPITenantKeyScopeContext(1, 10), imSession.ID)
+ require.ErrorIs(t, err, apperrors.ErrSessionNotFound)
+}
+
+func TestGetSessionDeniesAPIExternalUserRuntimeFromReadingIMSession(t *testing.T) {
+ svc, db := newTestSessionService(t)
+ require.NoError(t, db.AutoMigrate(&testListSessionsIMChannelSession{}))
+
+ imSession := &types.Session{TenantID: 1, Title: "feishu chat"}
+ require.NoError(t, db.Create(imSession).Error)
+ require.NoError(t, db.Create(&testListSessionsIMChannelSession{
+ SessionID: imSession.ID, Platform: "feishu",
+ }).Error)
+
+ _, err := svc.GetSession(testAPISessionScopeContext(1, "1:alice"), imSession.ID)
+ require.ErrorIs(t, err, apperrors.ErrSessionNotFound)
+}
+
func TestGetSessionAllowsIMRuntimeToReadIMSession(t *testing.T) {
svc, db := newTestSessionService(t)
require.NoError(t, db.AutoMigrate(&testListSessionsIMChannelSession{}))