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:
@@ -127,8 +127,11 @@ func (h *Handler) parseQARequest(c *gin.Context, logPrefix string) (*qaRequestCo
|
||||
logPrefix, sessionID, secutils.SanitizeForLog(secutils.CompactImageDataURLForLog(string(requestJSON))))
|
||||
}
|
||||
|
||||
// Get session
|
||||
session, err := h.sessionService.GetSession(ctx, sessionID)
|
||||
// Get session. QA writes new messages into the session, so use the strict
|
||||
// owner scope: a tenant admin may read an API-key session but must not be
|
||||
// able to post messages to it (which would otherwise fail later at message
|
||||
// creation with a 500 instead of a clean not-found).
|
||||
session, err := h.sessionService.GetOwnedSession(ctx, sessionID)
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "Failed to get session, session ID: %s, error: %v", sessionID, err)
|
||||
return nil, nil, errors.NewNotFoundError("Session not found")
|
||||
|
||||
@@ -258,8 +258,10 @@ func (h *Handler) StopSession(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Verify message belongs to the current tenant
|
||||
session, err := h.sessionService.GetSession(ctx, sessionID)
|
||||
// Verify message belongs to the current tenant. Stopping generation mutates
|
||||
// an in-flight run, so use the strict owner scope: a tenant admin may read an
|
||||
// API-key session but must not be able to interrupt its (external) API calls.
|
||||
session, err := h.sessionService.GetOwnedSession(ctx, sessionID)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, map[string]interface{}{
|
||||
"session_id": sessionID,
|
||||
|
||||
@@ -20,7 +20,9 @@ import (
|
||||
func (h *Handler) UploadTemporaryDocument(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
sessionID := c.Param("session_id")
|
||||
if _, err := h.sessionService.GetSession(ctx, sessionID); err != nil {
|
||||
// Uploading attaches content to the session, so use the strict owner scope:
|
||||
// a tenant admin may read an API-key session but must not add attachments.
|
||||
if _, err := h.sessionService.GetOwnedSession(ctx, sessionID); err != nil {
|
||||
c.Error(apperrors.NewNotFoundError("Session not found"))
|
||||
return
|
||||
}
|
||||
@@ -163,7 +165,9 @@ func (h *Handler) PreviewTemporaryDocument(c *gin.Context) {
|
||||
func (h *Handler) DeleteTemporaryDocument(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
sessionID := sessionIDParam(c)
|
||||
if _, err := h.sessionService.GetSession(ctx, sessionID); err != nil {
|
||||
// Deleting mutates the session's attachments, so use the strict owner scope:
|
||||
// a tenant admin may read an API-key session but must not remove attachments.
|
||||
if _, err := h.sessionService.GetOwnedSession(ctx, sessionID); err != nil {
|
||||
c.Error(apperrors.NewNotFoundError("Session not found"))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
stderrors "errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/errors"
|
||||
@@ -42,9 +43,16 @@ func (h *Handler) GenerateTitle(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Get session from database
|
||||
session, err := h.sessionService.GetSession(ctx, sessionID)
|
||||
// Get session from database. Title generation writes the session row, so use
|
||||
// the strict owner scope: a tenant admin may read an API-key session but must
|
||||
// not be able to (re)generate its title.
|
||||
session, err := h.sessionService.GetOwnedSession(ctx, sessionID)
|
||||
if err != nil {
|
||||
if stderrors.Is(err, errors.ErrSessionNotFound) {
|
||||
logger.Warnf(ctx, "Session not found, ID: %s", sessionID)
|
||||
c.Error(errors.NewNotFoundError(err.Error()))
|
||||
return
|
||||
}
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(errors.NewInternalServerError(err.Error()))
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user