fix(attachments): enforce upload body read deadline
This commit is contained in:
@@ -3,6 +3,7 @@ package handler
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
@@ -43,11 +44,25 @@ func (h *AttachmentHandler) Upload(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if deadline, ok := r.Context().Deadline(); ok {
|
||||||
|
controller := http.NewResponseController(w)
|
||||||
|
if err := controller.SetReadDeadline(deadline); err != nil {
|
||||||
|
httpx.WriteInternalError(w, requestID, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer func() { _ = controller.SetReadDeadline(time.Time{}) }()
|
||||||
|
}
|
||||||
|
|
||||||
contentType := r.Header.Get("Content-Type")
|
contentType := r.Header.Get("Content-Type")
|
||||||
r.Body = http.MaxBytesReader(w, r.Body, int64(h.maxBytes))
|
r.Body = http.MaxBytesReader(w, r.Body, int64(h.maxBytes))
|
||||||
|
|
||||||
att, err := h.service.Upload(r.Context(), userID, chatID, req.Filename, r.Body, contentType)
|
att, err := h.service.Upload(r.Context(), userID, chatID, req.Filename, r.Body, contentType)
|
||||||
switch {
|
switch {
|
||||||
|
case errors.Is(err, service.ErrUploadReadTimeout):
|
||||||
|
r.Close = true
|
||||||
|
w.Header().Set("Connection", "close")
|
||||||
|
httpx.WriteError(w, http.StatusRequestTimeout, dto.ErrorCodeInvalidRequest, "upload body read timed out", requestID)
|
||||||
|
return
|
||||||
case errors.Is(err, service.ErrInvalidSize):
|
case errors.Is(err, service.ErrInvalidSize):
|
||||||
httpx.WriteError(w, http.StatusRequestEntityTooLarge, dto.ErrorCodePayloadTooLarge, "file too large", requestID)
|
httpx.WriteError(w, http.StatusRequestEntityTooLarge, dto.ErrorCodePayloadTooLarge, "file too large", requestID)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -11,6 +11,10 @@ type responseWriter struct {
|
|||||||
status int
|
status int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *responseWriter) Unwrap() http.ResponseWriter {
|
||||||
|
return w.ResponseWriter
|
||||||
|
}
|
||||||
|
|
||||||
func (w *responseWriter) WriteHeader(status int) {
|
func (w *responseWriter) WriteHeader(status int) {
|
||||||
w.status = status
|
w.status = status
|
||||||
w.ResponseWriter.WriteHeader(status)
|
w.ResponseWriter.WriteHeader(status)
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
_ "image/jpeg"
|
_ "image/jpeg"
|
||||||
_ "image/png"
|
_ "image/png"
|
||||||
"io"
|
"io"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
@@ -24,6 +25,7 @@ var (
|
|||||||
ErrAttachmentNotFound = errors.New("attachment not found")
|
ErrAttachmentNotFound = errors.New("attachment not found")
|
||||||
ErrInvalidImage = errors.New("invalid image")
|
ErrInvalidImage = errors.New("invalid image")
|
||||||
ErrInvalidSize = errors.New("invalid size")
|
ErrInvalidSize = errors.New("invalid size")
|
||||||
|
ErrUploadReadTimeout = errors.New("upload body read timed out")
|
||||||
)
|
)
|
||||||
|
|
||||||
type attachmentQueries interface {
|
type attachmentQueries interface {
|
||||||
@@ -80,6 +82,10 @@ func (s *AttachmentService) Upload(
|
|||||||
if errors.As(err, &maxBytesErr) {
|
if errors.As(err, &maxBytesErr) {
|
||||||
return model.Attachment{}, ErrInvalidSize
|
return model.Attachment{}, ErrInvalidSize
|
||||||
}
|
}
|
||||||
|
var networkErr net.Error
|
||||||
|
if errors.As(err, &networkErr) && networkErr.Timeout() {
|
||||||
|
return model.Attachment{}, ErrUploadReadTimeout
|
||||||
|
}
|
||||||
return model.Attachment{}, err
|
return model.Attachment{}, err
|
||||||
}
|
}
|
||||||
if size > int64(s.maxBytes) {
|
if size > int64(s.maxBytes) {
|
||||||
|
|||||||
Reference in New Issue
Block a user