Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(server): validate workspace ownership for attachment uploads and queries (#683)
Jiayuan Zhangf4016fc72109
diff --git a/server/internal/handler/file.go b/server/internal/handler/file.goindex a8becbd3fe..0681ede28c 100644--- a/server/internal/handler/file.go+++ b/server/internal/handler/file.go@@ -80,7 +80,11 @@ func (h *Handler) groupAttachments(r *http.Request, commentIDs []pgtype.UUID) maif len(commentIDs) == 0 {return nil}- attachments, err := h.Queries.ListAttachmentsByCommentIDs(r.Context(), commentIDs)+ workspaceID := resolveWorkspaceID(r)+ attachments, err := h.Queries.ListAttachmentsByCommentIDs(r.Context(), db.ListAttachmentsByCommentIDsParams{+ Column1: commentIDs,+ WorkspaceID: parseUUID(workspaceID),+ })if err != nil {slog.Error("failed to load attachments for comments", "error", err)return nil@@ -180,12 +184,25 @@ func (h *Handler) UploadFile(w http.ResponseWriter, r *http.Request) {SizeBytes: int64(len(data)),}- // Optional issue_id / comment_id from form fields+ // Optional issue_id / comment_id from form fields — validate ownership.if issueID := r.FormValue("issue_id"); issueID != "" {- params.IssueID = parseUUID(issueID)+ issue, err := h.Queries.GetIssueInWorkspace(r.Context(), db.GetIssueInWorkspaceParams{+ ID: parseUUID(issueID),+ WorkspaceID: parseUUID(workspaceID),+ })+ if err != nil {+ writeError(w, http.StatusForbidden, "invalid issue_id")+ return+ }+ params.IssueID = issue.ID}if commentID := r.FormValue("comment_id"); commentID != "" {- params.CommentID = parseUUID(commentID)+ comment, err := h.Queries.GetComment(r.Context(), parseUUID(commentID))+ if err != nil || uuidToString(comment.WorkspaceID) != workspaceID {+ writeError(w, http.StatusForbidden, "invalid comment_id")+ return+ }+ params.CommentID = comment.ID}att, err := h.Queries.CreateAttachment(r.Context(), params)diff --git a/server/pkg/db/generated/attachment.sql.go b/server/pkg/db/generated/attachment.sql.goindex 966e3daec5..75b522231f 100644--- a/server/pkg/db/generated/attachment.sql.go+++ b/server/pkg/db/generated/attachment.sql.go@@ -237,12 +237,17 @@ func (q *Queries) ListAttachmentsByComment(ctx context.Context, arg ListAttachmeconst listAttachmentsByCommentIDs = `-- name: ListAttachmentsByCommentIDs :manySELECT id, workspace_id, issue_id, comment_id, uploader_type, uploader_id, filename, url, content_type, size_bytes, created_at FROM attachment-WHERE comment_id = ANY($1::uuid[])+WHERE comment_id = ANY($1::uuid[]) AND workspace_id = $2ORDER BY created_at ASC`-func (q *Queries) ListAttachmentsByCommentIDs(ctx context.Context, dollar_1 []pgtype.UUID) ([]Attachment, error) {- rows, err := q.db.Query(ctx, listAttachmentsByCommentIDs, dollar_1)+type ListAttachmentsByCommentIDsParams struct {+ Column1 []pgtype.UUID `json:"column_1"`+ WorkspaceID pgtype.UUID `json:"workspace_id"`+}++func (q *Queries) ListAttachmentsByCommentIDs(ctx context.Context, arg ListAttachmentsByCommentIDsParams) ([]Attachment, error) {+ rows, err := q.db.Query(ctx, listAttachmentsByCommentIDs, arg.Column1, arg.WorkspaceID)if err != nil {return nil, err}diff --git a/server/pkg/db/queries/attachment.sql b/server/pkg/db/queries/attachment.sqlindex 1ba0a3e009..ac9b18eaa7 100644--- a/server/pkg/db/queries/attachment.sql+++ b/server/pkg/db/queries/attachment.sql@@ -19,7 +19,7 @@ WHERE id = $1 AND workspace_id = $2;-- name: ListAttachmentsByCommentIDs :manySELECT * FROM attachment-WHERE comment_id = ANY($1::uuid[])+WHERE comment_id = ANY($1::uuid[]) AND workspace_id = $2ORDER BY created_at ASC;-- name: ListAttachmentURLsByIssueOrComments :many