Skip to content

Commit cd0bedd

Browse files
committed
Use filetype package to check file content for application/octet-stream content type header
1 parent 5de742b commit cd0bedd

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

Diff for: attachments.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func FetchAndStoreAttachment(ctx context.Context, b Backend, channel Channel, at
115115

116116
// prioritize to use the response content type header if provided
117117
contentTypeHeader := trace.Response.Header.Get("Content-Type")
118-
if contentTypeHeader != "" {
118+
if contentTypeHeader != "" && contentTypeHeader != "application/octet-stream" {
119119
mimeType, _, _ = mime.ParseMediaType(contentTypeHeader)
120120
if extension == "" {
121121
extensions, err := mime.ExtensionsByType(mimeType)

Diff for: attachments_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ func TestFetchAndStoreAttachment(t *testing.T) {
2323
"http://mock.com/media/hello2": {
2424
httpx.NewMockResponse(200, map[string]string{"Content-Type": "image/jpeg"}, testJPG),
2525
},
26+
"http://mock.com/media/hello3": {
27+
httpx.NewMockResponse(200, map[string]string{"Content-Type": "application/octet-stream"}, testJPG),
28+
},
2629
"http://mock.com/media/hello.mp3": {
2730
httpx.NewMockResponse(502, nil, []byte(`My gateways!`)),
2831
},
@@ -82,6 +85,12 @@ func TestFetchAndStoreAttachment(t *testing.T) {
8285
assert.NoError(t, err)
8386
assert.Equal(t, &courier.Attachment{ContentType: "unavailable", URL: "http://mock.com/media/hello.pdf"}, att)
8487

88+
att, err = courier.FetchAndStoreAttachment(ctx, mb, mockChannel, "http://mock.com/media/hello3", clog)
89+
assert.NoError(t, err)
90+
assert.Equal(t, "image/jpeg", att.ContentType)
91+
assert.Equal(t, "https://backend.com/attachments/338ff339-5663-49ed-8ef6-384876655d1b.jpg", att.URL)
92+
assert.Equal(t, 17301, att.Size)
93+
8594
// an actual error on our part should be returned as an error
8695
mb.SetStorageError(errors.New("boom"))
8796

0 commit comments

Comments
 (0)