Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
canifa_note
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vũ Hoàng Anh
canifa_note
Commits
e1941e78
Commit
e1941e78
authored
Jan 06, 2026
by
Steven
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: attachment type checks
parent
874a4a71
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
73 additions
and
1 deletion
+73
-1
attachment_service.go
server/router/api/v1/attachment_service.go
+15
-1
attachment_service_test.go
server/router/api/v1/test/attachment_service_test.go
+58
-0
No files found.
server/router/api/v1/attachment_service.go
View file @
e1941e78
...
@@ -6,6 +6,8 @@ import (
...
@@ -6,6 +6,8 @@ import (
"encoding/binary"
"encoding/binary"
"fmt"
"fmt"
"io"
"io"
"mime"
"net/http"
"os"
"os"
"path/filepath"
"path/filepath"
"regexp"
"regexp"
...
@@ -63,7 +65,19 @@ func (s *APIV1Service) CreateAttachment(ctx context.Context, request *v1pb.Creat
...
@@ -63,7 +65,19 @@ func (s *APIV1Service) CreateAttachment(ctx context.Context, request *v1pb.Creat
return
nil
,
status
.
Errorf
(
codes
.
InvalidArgument
,
"filename contains invalid characters or format"
)
return
nil
,
status
.
Errorf
(
codes
.
InvalidArgument
,
"filename contains invalid characters or format"
)
}
}
if
request
.
Attachment
.
Type
==
""
{
if
request
.
Attachment
.
Type
==
""
{
return
nil
,
status
.
Errorf
(
codes
.
InvalidArgument
,
"type is required"
)
ext
:=
filepath
.
Ext
(
request
.
Attachment
.
Filename
)
mimeType
:=
mime
.
TypeByExtension
(
ext
)
if
mimeType
==
""
{
mimeType
=
http
.
DetectContentType
(
request
.
Attachment
.
Content
)
}
// ParseMediaType to strip parameters
mediaType
,
_
,
err
:=
mime
.
ParseMediaType
(
mimeType
)
if
err
==
nil
{
request
.
Attachment
.
Type
=
mediaType
}
}
if
request
.
Attachment
.
Type
==
""
{
request
.
Attachment
.
Type
=
"application/octet-stream"
}
}
if
!
isValidMimeType
(
request
.
Attachment
.
Type
)
{
if
!
isValidMimeType
(
request
.
Attachment
.
Type
)
{
return
nil
,
status
.
Errorf
(
codes
.
InvalidArgument
,
"invalid MIME type format"
)
return
nil
,
status
.
Errorf
(
codes
.
InvalidArgument
,
"invalid MIME type format"
)
...
...
server/router/api/v1/test/attachment_service_test.go
0 → 100644
View file @
e1941e78
package
test
import
(
"context"
"testing"
"github.com/stretchr/testify/require"
v1pb
"github.com/usememos/memos/proto/gen/api/v1"
)
func
TestCreateAttachment
(
t
*
testing
.
T
)
{
ts
:=
NewTestService
(
t
)
defer
ts
.
Cleanup
()
ctx
:=
context
.
Background
()
user
,
err
:=
ts
.
CreateRegularUser
(
ctx
,
"test_user"
)
require
.
NoError
(
t
,
err
)
userCtx
:=
ts
.
CreateUserContext
(
ctx
,
user
.
ID
)
// Test case 1: Create attachment with empty type but known extension
t
.
Run
(
"EmptyType_KnownExtension"
,
func
(
t
*
testing
.
T
)
{
attachment
,
err
:=
ts
.
Service
.
CreateAttachment
(
userCtx
,
&
v1pb
.
CreateAttachmentRequest
{
Attachment
:
&
v1pb
.
Attachment
{
Filename
:
"test.png"
,
Content
:
[]
byte
(
"fake png content"
),
},
})
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
"image/png"
,
attachment
.
Type
)
})
// Test case 2: Create attachment with empty type and unknown extension, but detectable content
t
.
Run
(
"EmptyType_UnknownExtension_ContentSniffing"
,
func
(
t
*
testing
.
T
)
{
// PNG magic header: 89 50 4E 47 0D 0A 1A 0A
pngContent
:=
[]
byte
{
0x89
,
0x50
,
0x4E
,
0x47
,
0x0D
,
0x0A
,
0x1A
,
0x0A
}
attachment
,
err
:=
ts
.
Service
.
CreateAttachment
(
userCtx
,
&
v1pb
.
CreateAttachmentRequest
{
Attachment
:
&
v1pb
.
Attachment
{
Filename
:
"test.unknown"
,
Content
:
pngContent
,
},
})
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
"image/png"
,
attachment
.
Type
)
})
// Test case 3: Empty type, unknown extension, random content -> fallback to application/octet-stream
t
.
Run
(
"EmptyType_Fallback"
,
func
(
t
*
testing
.
T
)
{
randomContent
:=
[]
byte
{
0x00
,
0x01
,
0x02
,
0x03
}
attachment
,
err
:=
ts
.
Service
.
CreateAttachment
(
userCtx
,
&
v1pb
.
CreateAttachmentRequest
{
Attachment
:
&
v1pb
.
Attachment
{
Filename
:
"test.data"
,
Content
:
randomContent
,
},
})
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
"application/octet-stream"
,
attachment
.
Type
)
})
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment