Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: limit received image proportions #59

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions backend/routes/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package routes
import (
"encoding/json"
"fmt"
"image"
_ "image/png"
"io"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -42,8 +44,6 @@ func imageToPixelData(imageData []byte) []byte {
}

func addTemplateImg(w http.ResponseWriter, r *http.Request) {
// TODO: Limit file size / proportions between 5x5 and 64x64
// Passed like this curl -F "[email protected]" http://localhost:8080/addTemplateImg
file, _, err := r.FormFile("image")
if err != nil {
panic(err)
Expand All @@ -58,6 +58,19 @@ func addTemplateImg(w http.ResponseWriter, r *http.Request) {
}
defer tempFile.Close()

// Decode the image to check dimensions
img, format, err := image.Decode(file)
if err != nil {
http.Error(w, "Failed to decode the image: "+err.Error()+" - format: "+format, http.StatusBadRequest)
return
}
bounds := img.Bounds()
width, height := bounds.Max.X-bounds.Min.X, bounds.Max.Y-bounds.Min.Y
if width < 5 || width > 50 || height < 5 || height > 50 {
http.Error(w, fmt.Sprintf("Image dimensions out of allowed range (5x5 to 50x50). Uploaded image size: %dx%d", width, height), http.StatusBadRequest)
return
}

// Read all data from the uploaded file and write it to the temporary file
fileBytes, err := ioutil.ReadAll(file)
if err != nil {
Expand Down