Skip to content

Commit c07b4a5

Browse files
authored
feat: add secure middleware (#832)
1 parent dca35bd commit c07b4a5

2 files changed

Lines changed: 5 additions & 6 deletions

File tree

server/resource.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"net/http"
88
"net/url"
99
"strconv"
10-
"strings"
1110
"time"
1211

1312
"github.com/usememos/memos/api"
@@ -263,11 +262,7 @@ func (s *Server) registerResourcePublicRoutes(g *echo.Group) {
263262
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch resource ID: %v", resourceID)).SetInternal(err)
264263
}
265264

266-
if strings.HasPrefix(resource.Type, echo.MIMETextHTML) {
267-
c.Response().Writer.Header().Set("Content-Type", echo.MIMETextPlain)
268-
} else {
269-
c.Response().Writer.Header().Set("Content-Type", resource.Type)
270-
}
265+
c.Response().Writer.Header().Set("Content-Type", resource.Type)
271266
c.Response().Writer.WriteHeader(http.StatusOK)
272267
c.Response().Writer.Header().Set(echo.HeaderCacheControl, "max-age=31536000, immutable")
273268
if _, err := c.Response().Writer.Write(resource.Blob); err != nil {

server/server.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ func NewServer(profile *profile.Profile) *Server {
4444
Timeout: 30 * time.Second,
4545
}))
4646

47+
e.Use(middleware.SecureWithConfig(middleware.SecureConfig{
48+
ContentSecurityPolicy: "default-src 'self'",
49+
}))
50+
4751
embedFrontend(e)
4852

4953
// In dev mode, set the const secret key to make signin session persistence.

0 commit comments

Comments
 (0)