Skip to content

Commit 46c13a4

Browse files
authored
chore: add skipper for secure (#913)
1 parent 96798e1 commit 46c13a4

File tree

8 files changed

+74
-6
lines changed

8 files changed

+74
-6
lines changed

server/common.go

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package server
22

33
import (
4+
"net/http"
5+
46
"github.com/labstack/echo/v4"
57
"github.com/usememos/memos/api"
68
"github.com/usememos/memos/common"
@@ -16,6 +18,10 @@ func composeResponse(data interface{}) response {
1618
}
1719
}
1820

21+
func DefaultGetRequestSkipper(c echo.Context) bool {
22+
return c.Request().Method == http.MethodGet
23+
}
24+
1925
func (server *Server) DefaultAuthSkipper(c echo.Context) bool {
2026
ctx := c.Request().Context()
2127
path := c.Path()

server/resource.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http"
88
"net/url"
99
"strconv"
10+
"strings"
1011
"time"
1112

1213
"github.com/pkg/errors"
@@ -266,7 +267,11 @@ func (s *Server) registerResourcePublicRoutes(g *echo.Group) {
266267
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch resource ID: %v", resourceID)).SetInternal(err)
267268
}
268269

269-
c.Response().Writer.Header().Set("Content-Type", resource.Type)
270+
if strings.HasPrefix(resource.Type, "text") || strings.HasPrefix(resource.Type, "application") {
271+
c.Response().Writer.Header().Set("Content-Type", echo.MIMETextPlain)
272+
} else {
273+
c.Response().Writer.Header().Set("Content-Type", resource.Type)
274+
}
270275
c.Response().Writer.WriteHeader(http.StatusOK)
271276
c.Response().Writer.Header().Set(echo.HeaderCacheControl, "max-age=31536000, immutable")
272277
c.Response().Writer.Header().Set(echo.HeaderContentSecurityPolicy, "default-src 'self'")

server/server.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,13 @@ func NewServer(ctx context.Context, profile *profile.Profile) (*Server, error) {
6464

6565
e.Use(middleware.CORS())
6666

67-
e.Use(middleware.Secure())
67+
e.Use(middleware.SecureWithConfig(middleware.SecureConfig{
68+
Skipper: DefaultGetRequestSkipper,
69+
XSSProtection: "1; mode=block",
70+
ContentTypeNosniff: "nosniff",
71+
XFrameOptions: "SAMEORIGIN",
72+
HSTSPreloadEnabled: false,
73+
}))
6874

6975
e.Use(middleware.TimeoutWithConfig(middleware.TimeoutConfig{
7076
Skipper: middleware.DefaultSkipper,

server/version/version.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import (
77

88
// Version is the service current released version.
99
// Semantic versioning: https://semver.org/
10-
var Version = "0.9.1"
10+
var Version = "0.10.0"
1111

1212
// DevVersion is the service current development version.
13-
var DevVersion = "0.9.1"
13+
var DevVersion = "0.10.0"
1414

1515
func GetCurrentVersion(mode string) string {
1616
if mode == "dev" {
@@ -29,7 +29,6 @@ func GetMinorVersion(version string) string {
2929

3030
func GetSchemaVersion(version string) string {
3131
minorVersion := GetMinorVersion(version)
32-
3332
return minorVersion + ".0"
3433
}
3534

server/version/version_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package version
2+
3+
import "testing"
4+
5+
func TestIsVersionGreaterOrEqualThan(t *testing.T) {
6+
tests := []struct {
7+
version string
8+
target string
9+
want bool
10+
}{
11+
{
12+
version: "0.9.1",
13+
target: "0.9.1",
14+
want: true,
15+
},
16+
{
17+
version: "0.10.0",
18+
target: "0.9.1",
19+
want: true,
20+
},
21+
{
22+
version: "0.9.0",
23+
target: "0.9.1",
24+
want: false,
25+
},
26+
}
27+
for _, test := range tests {
28+
result := IsVersionGreaterOrEqualThan(test.version, test.target)
29+
if result != test.want {
30+
t.Errorf("got result %v, want %v.", result, test.want)
31+
}
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- activity
2+
CREATE TABLE activity (
3+
id INTEGER PRIMARY KEY AUTOINCREMENT,
4+
creator_id INTEGER NOT NULL,
5+
created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
6+
type TEXT NOT NULL DEFAULT '',
7+
level TEXT NOT NULL CHECK (level IN ('INFO', 'WARN', 'ERROR')) DEFAULT 'INFO',
8+
payload TEXT NOT NULL DEFAULT '{}'
9+
);

store/db/migration/prod/LATEST__SCHEMA.sql

+10
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,13 @@ CREATE TABLE tag (
9393
creator_id INTEGER NOT NULL,
9494
UNIQUE(name, creator_id)
9595
);
96+
97+
-- activity
98+
CREATE TABLE activity (
99+
id INTEGER PRIMARY KEY AUTOINCREMENT,
100+
creator_id INTEGER NOT NULL,
101+
created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
102+
type TEXT NOT NULL DEFAULT '',
103+
level TEXT NOT NULL CHECK (level IN ('INFO', 'WARN', 'ERROR')) DEFAULT 'INFO',
104+
payload TEXT NOT NULL DEFAULT '{}'
105+
);

web/src/components/EmbedMemoDialog.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const EmbedMemoDialog: React.FC<Props> = (props: Props) => {
3434
<code className="w-full break-all whitespace-pre-wrap">{memoEmbeddedCode()}</code>
3535
</pre>
3636
<p className="w-full text-sm leading-6 flex flex-row justify-between items-center mt-2">
37-
* Only the public memo supports.
37+
<span className="italic opacity-80">* Only the public memo supports.</span>
3838
<span className="btn-primary" onClick={handleCopyCode}>
3939
Copy
4040
</span>

0 commit comments

Comments
 (0)