Stop publishing repository-wide prompt ledgers and enforce per-user prompt isolation through authenticated Gitea IDs.
Hop-State: A_06FN8NFDRFAFN5VCJTHJAAG Hop-Proposal: R_06FN8NEGJ054CPMJF9ZAN70 Hop-Task: T_06FN8HT6XG332C3XY6PYCCG Hop-Attempt: AT_06FN8HT6XGRXW0B4WPFFBJG
This commit is contained in:
@@ -21,6 +21,8 @@ const maxWebhookBody = 2 << 20
|
||||
|
||||
type giteaClient interface {
|
||||
Repository(context.Context, string, string) (gitea.Repository, error)
|
||||
User(context.Context, string) (gitea.User, error)
|
||||
AuthenticatedUser(context.Context, string) (gitea.User, bool, error)
|
||||
ViewerCanReadRepository(context.Context, string, string, string) (bool, error)
|
||||
}
|
||||
|
||||
@@ -51,7 +53,18 @@ func (s *server) listPrompts(w http.ResponseWriter, r *http.Request) {
|
||||
writeError(w, http.StatusBadRequest, "owner and repo are required")
|
||||
return
|
||||
}
|
||||
allowed, err := s.gitea.ViewerCanReadRepository(r.Context(), r.Header.Get("Cookie"), owner, name)
|
||||
cookie := r.Header.Get("Cookie")
|
||||
viewer, authenticated, err := s.gitea.AuthenticatedUser(r.Context(), cookie)
|
||||
if err != nil {
|
||||
s.logger.Error("load prompt viewer", "owner", owner, "name", name, "error", err)
|
||||
writeError(w, http.StatusBadGateway, "could not verify signed-in user")
|
||||
return
|
||||
}
|
||||
if !authenticated {
|
||||
writeError(w, http.StatusUnauthorized, "sign in to view your prompts")
|
||||
return
|
||||
}
|
||||
allowed, err := s.gitea.ViewerCanReadRepository(r.Context(), cookie, owner, name)
|
||||
if err != nil {
|
||||
s.logger.Error("check prompt viewer access", "owner", owner, "name", name, "error", err)
|
||||
writeError(w, http.StatusBadGateway, "could not verify repository access")
|
||||
@@ -61,7 +74,7 @@ func (s *server) listPrompts(w http.ResponseWriter, r *http.Request) {
|
||||
writeError(w, http.StatusUnauthorized, "sign in to view this repository's prompts")
|
||||
return
|
||||
}
|
||||
prompts, err := s.store.ListPrompts(r.Context(), owner, name, 100)
|
||||
prompts, err := s.store.ListPrompts(r.Context(), owner, name, viewer.ID, 100)
|
||||
if err != nil {
|
||||
s.logger.Error("list prompts", "owner", owner, "name", name, "error", err)
|
||||
writeError(w, http.StatusInternalServerError, "could not list prompts")
|
||||
@@ -77,6 +90,7 @@ func (s *server) createPrompt(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
var input struct {
|
||||
UserLogin string `json:"user_login"`
|
||||
TaskID string `json:"task_id"`
|
||||
AttemptID string `json:"attempt_id"`
|
||||
StateID string `json:"state_id"`
|
||||
@@ -92,6 +106,11 @@ func (s *server) createPrompt(w http.ResponseWriter, r *http.Request) {
|
||||
writeError(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
input.UserLogin = strings.TrimSpace(input.UserLogin)
|
||||
if !validSlug(input.UserLogin) {
|
||||
writeError(w, http.StatusBadRequest, "user_login is required")
|
||||
return
|
||||
}
|
||||
input.Prompt = strings.TrimSpace(input.Prompt)
|
||||
if input.Prompt == "" || len(input.Prompt) > 32<<10 {
|
||||
writeError(w, http.StatusBadRequest, "prompt is required and must be at most 32 KiB")
|
||||
@@ -110,9 +129,20 @@ func (s *server) createPrompt(w http.ResponseWriter, r *http.Request) {
|
||||
writeError(w, http.StatusBadRequest, "metadata must be valid JSON and at most 16 KiB")
|
||||
return
|
||||
}
|
||||
promptOwner, err := s.gitea.User(r.Context(), input.UserLogin)
|
||||
if err != nil {
|
||||
if errors.Is(err, gitea.ErrTokenNotConfigured) {
|
||||
writeError(w, http.StatusServiceUnavailable, err.Error())
|
||||
return
|
||||
}
|
||||
s.logger.Error("resolve prompt owner", "user_login", input.UserLogin, "error", err)
|
||||
writeError(w, http.StatusBadGateway, "could not resolve prompt owner")
|
||||
return
|
||||
}
|
||||
saved, err := s.store.CreatePrompt(r.Context(), store.CreatePromptInput{
|
||||
Owner: owner, Repository: name, TaskID: input.TaskID, AttemptID: input.AttemptID, StateID: input.StateID,
|
||||
Prompt: input.Prompt, AgentName: input.AgentName, AgentModel: input.AgentModel, Status: input.Status,
|
||||
GiteaUserID: promptOwner.ID,
|
||||
Prompt: input.Prompt, AgentName: input.AgentName, AgentModel: input.AgentModel, Status: input.Status,
|
||||
ResponseSummary: input.ResponseSummary, Metadata: input.Metadata, CompletedAt: input.CompletedAt,
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user