Open platform to collect and prioritize feedback
fix(attachments): validate edited comment images and detect content type
Matt Robertsaba074944d88
diff --git a/app/actions/post.go b/app/actions/post.goindex 787f259e4..3d36d39a4 100644--- a/app/actions/post.go+++ b/app/actions/post.go@@ -369,6 +369,20 @@ func (action *EditComment) Validate(ctx context.Context, user *entity.User) *valresult.AddFieldFailure("content", i18n.T(ctx, "validation.custom.maxattachments", i18n.Params{"number": 2}))}+ getAttachments := &query.GetAttachments{Post: action.Post, Comment: action.Comment}+ if err := bus.Dispatch(ctx, getAttachments); err != nil {+ return validate.Error(err)+ }++ messages, err := validate.MultiImageUpload(ctx, getAttachments.Result, action.Attachments, validate.MultiImageUploadOpts{+ MaxUploads: 2,+ MaxKilobytes: 5120,+ ExactRatio: false,+ })+ if err != nil {+ return validate.Error(err)+ }+ result.AddFieldFailure("attachments", messages...)}return resultdiff --git a/app/actions/post_test.go b/app/actions/post_test.goindex ff8bd9132..a131e8485 100644--- a/app/actions/post_test.go+++ b/app/actions/post_test.go@@ -7,6 +7,7 @@ import ("testing""github.com/getfider/fider/app"+ "github.com/getfider/fider/app/models/dto""github.com/getfider/fider/app/models/entity""github.com/getfider/fider/app/models/enum""github.com/getfider/fider/app/models/query"@@ -14,6 +15,8 @@ import ("github.com/getfider/fider/app/actions". "github.com/getfider/fider/app/pkg/assert""github.com/getfider/fider/app/pkg/bus"+ "github.com/getfider/fider/app/pkg/mock"+ "github.com/getfider/fider/app/pkg/validate")func TestCreateNewPost_InvalidPostTitles(t *testing.T) {@@ -199,6 +202,11 @@ func TestAddNewComment_NullAttachments(t *testing.T) {func TestEditComment_NullAttachments(t *testing.T) {RegisterT(t)+ bus.AddHandler(func(ctx context.Context, q *query.GetAttachments) error {+ q.Result = []string{"attachments/a.png"}+ return nil+ })+action := &actions.EditComment{}err := json.Unmarshal([]byte(`{"content": "Nice idea", "attachments": [null, {"bkey": "attachments/a.png", "remove": true}]}`), action)Expect(err).IsNil()@@ -208,3 +216,59 @@ func TestEditComment_NullAttachments(t *testing.T) {Expect(action.Attachments).HasLen(1)Expect(action.Attachments[0].BlobKey).Equals("attachments/a.png")}++func executeEditComment(existing []string, attachments ...*dto.ImageUpload) *validate.Result {+ bus.AddHandler(func(ctx context.Context, q *query.GetAttachments) error {+ q.Result = existing+ return nil+ })++ action := &actions.EditComment{+ Content: "Nice idea",+ Attachments: attachments,+ Post: &entity.Post{ID: 1},+ Comment: &entity.Comment{ID: 1},+ }+ return action.Validate(context.Background(), nil)+}++func newUpload(content []byte) *dto.ImageUpload {+ return &dto.ImageUpload{Upload: &dto.ImageUploadData{FileName: "image.png", ContentType: "image/png", Content: content}}+}++func TestEditComment_ValidAttachment(t *testing.T) {+ RegisterT(t)++ result := executeEditComment([]string{}, newUpload(mock.UniformPNG(100, 100)))+ ExpectSuccess(result)+}++func TestEditComment_UnsupportedAttachment(t *testing.T) {+ RegisterT(t)++ result := executeEditComment([]string{}, newUpload([]byte("<html><script>alert(1)</script></html>")))+ ExpectFailed(result, "attachments")+}++func TestEditComment_DecompressionBombAttachment(t *testing.T) {+ RegisterT(t)++ result := executeEditComment([]string{}, newUpload(mock.UniformPNG(12000, 12000)))+ ExpectFailed(result, "attachments")+}++func TestEditComment_TooManyAttachments(t *testing.T) {+ RegisterT(t)++ // The comment already has 2 attachments, so a new one exceeds the limit...+ existing := []string{"attachments/a.png", "attachments/b.png"}+ result := executeEditComment(existing, newUpload(mock.UniformPNG(100, 100)))+ ExpectFailed(result, "attachments")++ // ...unless one of the existing ones is removed+ result = executeEditComment(existing,+ &dto.ImageUpload{BlobKey: "attachments/a.png", Remove: true},+ newUpload(mock.UniformPNG(100, 100)),+ )+ ExpectSuccess(result)+}diff --git a/app/pkg/validate/image.go b/app/pkg/validate/image.goindex 14cfa697f..e93747b9a 100644--- a/app/pkg/validate/image.go+++ b/app/pkg/validate/image.go@@ -72,6 +72,11 @@ type ImageHeader struct {Progressive bool}+// ContentType returns the MIME type of the image format+func (h *ImageHeader) ContentType() string {+ return "image/" + h.Format+}+// ReadImageHeader reads the image header without decoding the pixel data.// Returns imagic.ErrNotSupported for anything imagic can't process.func ReadImageHeader(content []byte) (*ImageHeader, error) {diff --git a/app/services/sqlstore/postgres/attachment.go b/app/services/sqlstore/postgres/attachment.goindex 40adfad5a..99ff24d95 100644--- a/app/services/sqlstore/postgres/attachment.go+++ b/app/services/sqlstore/postgres/attachment.go@@ -13,6 +13,7 @@ import ("github.com/getfider/fider/app/pkg/dbx""github.com/getfider/fider/app/pkg/errors""github.com/getfider/fider/app/pkg/rand"+ "github.com/getfider/fider/app/pkg/validate""github.com/getfider/fider/app/services/blob")@@ -96,6 +97,13 @@ func getAttachments(ctx context.Context, q *query.GetAttachments) error {func uploadImage(ctx context.Context, c *cmd.UploadImage) error {if c.Image.Upload != nil && len(c.Image.Upload.Content) > 0 {+ // Never trust the client-supplied content type, it is served back as-is+ header, err := validate.ReadImageHeader(c.Image.Upload.Content)+ if err != nil {+ return errors.Wrap(err, "failed to read image header")+ }+ c.Image.Upload.ContentType = header.ContentType()+// Regenerate key if empty or wrong folder prefix (prevents cross-folder overwrites)if c.Image.BlobKey == "" || !strings.HasPrefix(c.Image.BlobKey, c.Folder+"/") {c.Image.BlobKey = fmt.Sprintf("%s/%s-%s", c.Folder, rand.String(64), blob.SanitizeFileName(c.Image.Upload.FileName))@@ -107,7 +115,7 @@ func uploadImage(ctx context.Context, c *cmd.UploadImage) error {c.Image.BlobKey = fmt.Sprintf("%s/%s-%s", c.Folder, rand.String(64), blob.SanitizeFileName(c.Image.Upload.FileName))}- err := bus.Dispatch(ctx, &cmd.StoreBlob{+ err = bus.Dispatch(ctx, &cmd.StoreBlob{Key: c.Image.BlobKey,Content: c.Image.Upload.Content,ContentType: c.Image.Upload.ContentType,diff --git a/app/services/sqlstore/postgres/attachment_test.go b/app/services/sqlstore/postgres/attachment_test.goindex 2ba8d435e..4a747b51d 100644--- a/app/services/sqlstore/postgres/attachment_test.go+++ b/app/services/sqlstore/postgres/attachment_test.go@@ -2,6 +2,7 @@ package postgres_testimport ("context"+ "os""testing""github.com/getfider/fider/app/models/cmd"@@ -10,9 +11,16 @@ import (. "github.com/getfider/fider/app/pkg/assert""github.com/getfider/fider/app/pkg/bus"+ "github.com/getfider/fider/app/pkg/env""github.com/getfider/fider/app/services/blob")+func readTestImage(name string) []byte {+ content, err := os.ReadFile(env.Path("/app/pkg/web/testdata/" + name))+ Expect(err).IsNil()+ return content+}+func TestUploadImage(t *testing.T) {ctx := SetupDatabaseTest(t)defer TeardownDatabaseTest()@@ -27,8 +35,8 @@ func TestUploadImage(t *testing.T) {uploadImage := &cmd.UploadImage{Image: &dto.ImageUpload{Upload: &dto.ImageUploadData{- Content: []byte("Hello World"),- ContentType: "text/plain",+ Content: readTestImage("logo1.png"),+ ContentType: "image/png",},},Folder: "avatars",@@ -39,6 +47,73 @@ func TestUploadImage(t *testing.T) {Expect(uploadImage.Image.BlobKey).HasLen(73)}+func TestUploadImage_ContentTypeFromImageBytes(t *testing.T) {+ ctx := SetupDatabaseTest(t)+ defer TeardownDatabaseTest()++ var storedContentType string+ bus.AddHandler(func(ctx context.Context, c *cmd.StoreBlob) error {+ storedContentType = c.ContentType+ return nil+ })+ bus.AddHandler(func(ctx context.Context, q *query.GetBlobByKey) error {+ return blob.ErrNotFound+ })++ testCases := []struct {+ file string+ expected string+ }{+ {"logo1.png", "image/png"},+ {"logo2.jpg", "image/jpeg"},+ {"logo3.gif", "image/gif"},+ }++ for _, testCase := range testCases {+ uploadImage := &cmd.UploadImage{+ Image: &dto.ImageUpload{+ Upload: &dto.ImageUploadData{+ FileName: "image.html",+ Content: readTestImage(testCase.file),+ ContentType: "text/html",+ },+ },+ Folder: "attachments",+ }+ err := bus.Dispatch(ctx, uploadImage)+ Expect(err).IsNil()+ Expect(storedContentType).Equals(testCase.expected)+ Expect(uploadImage.Image.Upload.ContentType).Equals(testCase.expected)+ }+}++func TestUploadImage_NotAnImage(t *testing.T) {+ ctx := SetupDatabaseTest(t)+ defer TeardownDatabaseTest()++ storeBlobCalled := false+ bus.AddHandler(func(ctx context.Context, c *cmd.StoreBlob) error {+ storeBlobCalled = true+ return nil+ })+ bus.AddHandler(func(ctx context.Context, q *query.GetBlobByKey) error {+ return blob.ErrNotFound+ })++ uploadImage := &cmd.UploadImage{+ Image: &dto.ImageUpload{+ Upload: &dto.ImageUploadData{+ Content: []byte("<script>alert(1)</script>"),+ ContentType: "image/png",+ },+ },+ Folder: "attachments",+ }+ err := bus.Dispatch(ctx, uploadImage)+ Expect(err).IsNotNil()+ Expect(storeBlobCalled).IsFalse()+}+func TestUploadImage_NoContent(t *testing.T) {ctx := SetupDatabaseTest(t)defer TeardownDatabaseTest()@@ -75,14 +150,14 @@ func TestUploadMultipleImages(t *testing.T) {Images: []*dto.ImageUpload{{Upload: &dto.ImageUploadData{- Content: []byte("Hello World 1"),- ContentType: "text/plain",+ Content: readTestImage("logo1.png"),+ ContentType: "image/png",},},{Upload: &dto.ImageUploadData{- Content: []byte("Hello World 2"),- ContentType: "text/plain",+ Content: readTestImage("logo2.jpg"),+ ContentType: "image/jpeg",},},},