Fix image formats and size, add feedback
This commit is contained in:
1 parent
feaab7e4e2
commit
fef1abade8
11 files changed
+192
-12
No files matched your search
@@ -22,7 +22,8 @@ func statusFor(err error) int {
|
||||
return http.StatusUnauthorized
|
||||
case errors.Is(err, invite.ErrInvalid),
|
||||
errors.Is(err, user.ErrUnsupportedFormat),
|
||||
errors.Is(err, user.ErrExceedDimensions):
|
||||
errors.Is(err, user.ErrExceedDimensions),
|
||||
errors.Is(err, user.ErrImageTooLarge):
|
||||
return http.StatusBadRequest
|
||||
case errors.Is(err, auth.ErrCanceled):
|
||||
return http.StatusRequestTimeout
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"goAuth/internal/user"
|
||||
)
|
||||
|
||||
// avatarLimitsResponse describes the constraints applied to avatar uploads so
|
||||
// the frontend can mirror them (file picker filter, size hint and client-side
|
||||
// pre-validation) without hardcoding values.
|
||||
type avatarLimitsResponse struct {
|
||||
MaxSizeBytes int `json:"maxSizeBytes"`
|
||||
MaxWidth int `json:"maxWidth"`
|
||||
MaxHeight int `json:"maxHeight"`
|
||||
AllowedMimeTypes []string `json:"allowedMimeTypes"`
|
||||
}
|
||||
|
||||
type configResponse struct {
|
||||
Avatar avatarLimitsResponse `json:"avatar"`
|
||||
}
|
||||
|
||||
// getConfig exposes public, non-secret limits used by the frontend. It is
|
||||
// served on the public chain so the CSRF cookie is seeded on this GET request
|
||||
// as well.
|
||||
func (h *Handler) getConfig(w http.ResponseWriter, r *http.Request) {
|
||||
writeJson(w, http.StatusOK, configResponse{
|
||||
Avatar: avatarLimitsResponse{
|
||||
MaxSizeBytes: user.MaxImageSize,
|
||||
MaxWidth: user.MaxImageWidth,
|
||||
MaxHeight: user.MaxImageHeight,
|
||||
AllowedMimeTypes: user.AllowedAvatarMIMETypes(),
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -141,14 +141,27 @@ func readAvatarUpload(r *http.Request) ([]byte, error) {
|
||||
contentType := r.Header.Get("Content-Type")
|
||||
const multipart = "multipart/form-data"
|
||||
|
||||
// Read at most one byte more than allowed so an oversized upload is
|
||||
// detected and rejected instead of silently truncated.
|
||||
const limit = user.MaxImageSize + 1
|
||||
|
||||
var reader io.Reader = r.Body
|
||||
if len(contentType) >= len(multipart) && contentType[:len(multipart)] == multipart {
|
||||
file, _, err := r.FormFile("avatar")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
return io.ReadAll(io.LimitReader(file, maxRequestBody))
|
||||
reader = file
|
||||
}
|
||||
|
||||
return io.ReadAll(io.LimitReader(r.Body, maxRequestBody))
|
||||
data, err := io.ReadAll(io.LimitReader(reader, limit))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(data) > user.MaxImageSize {
|
||||
return nil, user.ErrImageTooLarge
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
@@ -17,6 +17,8 @@ func (h *Handler) registerRoutes(mux *http.ServeMux) {
|
||||
userChain := middleware.MiddlewareChain{corsMw, csrfMw, sessionMw}
|
||||
superuserChain := middleware.MiddlewareChain{corsMw, csrfMw, sessionMw, h.superuser}
|
||||
|
||||
mux.Handle("GET /api/config", publicChain.ThenFunc(h.getConfig))
|
||||
|
||||
mux.Handle("POST /api/auth/signup", publicChain.ThenFunc(h.signup))
|
||||
mux.Handle("POST /api/auth/signin", publicChain.ThenFunc(h.signin))
|
||||
mux.Handle("POST /api/auth/signout", userChain.ThenFunc(h.signout))
|
||||
|
||||
@@ -7,5 +7,6 @@ var (
|
||||
ErrNotFound = errors.New("user not found")
|
||||
ErrUnsupportedFormat = errors.New("not supported format")
|
||||
ErrExceedDimensions = errors.New("image exceeds max dimenstions")
|
||||
ErrImageTooLarge = errors.New("image exceeds max size")
|
||||
ErrInvalidPassword = errors.New("invalid password")
|
||||
)
|
||||
@@ -9,9 +9,14 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
MaxImageWidth = 1024
|
||||
MaxImageHeight = 1024
|
||||
PasswordCost = 14
|
||||
// MaxImageWidth and MaxImageHeight cap the dimensions of an uploaded
|
||||
// avatar.
|
||||
MaxImageWidth = 2048
|
||||
MaxImageHeight = 2048
|
||||
// MaxImageSize is the maximum accepted avatar upload size in bytes
|
||||
// (4 MiB).
|
||||
MaxImageSize = 4 << 20
|
||||
PasswordCost = 14
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
|
||||
@@ -10,6 +10,22 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// allowedImageFormats maps the image formats returned by image.DecodeConfig
|
||||
// (derived from the file content, not the extension) to the MIME types the
|
||||
// frontend advertises in the file picker. JPEG files may carry either a .jpg
|
||||
// or a .jpeg extension; both decode to the "jpeg" format and are accepted.
|
||||
var allowedImageFormats = map[string]string{
|
||||
"jpeg": "image/jpeg",
|
||||
"png": "image/png",
|
||||
}
|
||||
|
||||
// AllowedAvatarMIMETypes returns the MIME types accepted for avatars. The
|
||||
// "image/jpg" alias is included so file pickers that treat ".jpg" as a
|
||||
// distinct type still allow the upload.
|
||||
func AllowedAvatarMIMETypes() []string {
|
||||
return []string{"image/jpeg", "image/jpg", "image/png"}
|
||||
}
|
||||
|
||||
type Validator struct {
|
||||
emailRegex *regexp.Regexp
|
||||
loginRegex *regexp.Regexp
|
||||
@@ -50,6 +66,10 @@ func (v *Validator) VerifyPassword(password string) bool {
|
||||
}
|
||||
|
||||
func (v *Validator) VerifyImage(data []byte) error {
|
||||
if len(data) > MaxImageSize {
|
||||
return ErrImageTooLarge
|
||||
}
|
||||
|
||||
r := bytes.NewReader(data)
|
||||
cfg, format, err := image.DecodeConfig(r)
|
||||
if err != nil {
|
||||
@@ -57,9 +77,7 @@ func (v *Validator) VerifyImage(data []byte) error {
|
||||
return err
|
||||
}
|
||||
|
||||
switch format {
|
||||
case "jpeg", "png":
|
||||
default:
|
||||
if _, ok := allowedImageFormats[format]; !ok {
|
||||
return ErrUnsupportedFormat
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user