Files
pipelines/internal/server/chat_uploads.go
T

130 lines
3.8 KiB
Go

package server
import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
)
// maxChatUploadBytes bounds one file sent through POST /chat-uploads. Files
// travel over plain HTTP, outside the 2 MiB websocket frame, so a prompt can
// carry photos, logs and archives the inline base64 route cannot.
const maxChatUploadBytes = 100 << 20
// chatUploadHeader carries the file's name. Being a custom header, it also
// keeps a cross-site page from posting here without a CORS preflight, which
// neither the server nor its proxies answer.
const chatUploadHeader = "X-Pipelines-Upload-Name"
var chatUploadName = regexp.MustCompile(`^up-[0-9a-f]{32}-[^/\\]+$`)
type chatUploadReply struct {
Upload string `json:"upload"`
Name string `json:"name"`
Mime string `json:"mime"`
Size int64 `json:"size"`
}
// serveChatUpload stores the request body as one private chat attachment and
// returns the name a later chat prompt references in ChatAttachment.Upload.
func serveChatUpload(w http.ResponseWriter, r *http.Request) {
fail := func(status int, message string) {
http.Error(w, message, status)
}
rawName := r.Header.Get(chatUploadHeader)
if rawName == "" {
fail(http.StatusBadRequest, "missing "+chatUploadHeader)
return
}
if r.Header.Get("Sec-Fetch-Site") == "cross-site" {
fail(http.StatusForbidden, "cross-site upload refused")
return
}
decoded, err := url.PathUnescape(rawName)
if err != nil {
decoded = rawName
}
name := safeChatAttachmentName(decoded)
if r.ContentLength > maxChatUploadBytes {
fail(http.StatusRequestEntityTooLarge, "attachment exceeds the 100 MiB limit")
return
}
mime := strings.TrimSpace(r.Header.Get("Content-Type"))
if mime == "" {
mime = "application/octet-stream"
}
var token [16]byte
if _, err := rand.Read(token[:]); err != nil {
fail(http.StatusInternalServerError, "upload id unavailable")
return
}
stored := "up-" + hex.EncodeToString(token[:]) + "-" + name
chatAttachmentStore.Lock()
root, dir, used, usedFiles, err := openChatAttachmentStore()
if err != nil {
chatAttachmentStore.Unlock()
fail(http.StatusInternalServerError, err.Error())
return
}
defer root.Close()
if usedFiles+1 > chatAttachmentFileQuota || used+max(r.ContentLength, 0) > chatAttachmentQuota {
chatAttachmentStore.Unlock()
fail(http.StatusInsufficientStorage, "chat attachment storage quota exceeded; retry after retention cleanup")
return
}
file, err := root.OpenFile(stored, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600)
if err != nil {
chatAttachmentStore.Unlock()
fail(http.StatusInternalServerError, "write attachment: "+err.Error())
return
}
// Leased while it is written so retention cannot collect it mid-upload.
path := filepath.Join(dir, stored)
chatAttachmentStore.leased[path] = true
chatAttachmentStore.Unlock()
release := func(remove bool) {
chatAttachmentStore.Lock()
delete(chatAttachmentStore.leased, path)
if remove {
_ = root.Remove(stored)
}
chatAttachmentStore.Unlock()
}
size, err := io.Copy(file, io.LimitReader(r.Body, maxChatUploadBytes+1))
if err == nil && size > maxChatUploadBytes {
err = errors.New("attachment exceeds the 100 MiB limit")
}
if err == nil {
err = file.Sync()
}
if closeErr := file.Close(); err == nil {
err = closeErr
}
if err == nil && size == 0 {
err = errors.New("attachment is empty")
}
if err != nil {
release(true)
status := http.StatusBadRequest
if size > maxChatUploadBytes {
status = http.StatusRequestEntityTooLarge
}
fail(status, err.Error())
return
}
release(false)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "no-store")
_ = json.NewEncoder(w).Encode(chatUploadReply{Upload: stored, Name: name, Mime: mime, Size: size})
}