forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth_origin_test.go
More file actions
135 lines (121 loc) · 4.71 KB
/
Copy pathauth_origin_test.go
File metadata and controls
135 lines (121 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package middleware
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/QuantumNous/new-api/common"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func runOriginGuardRequest(t *testing.T, origin, referer string) *httptest.ResponseRecorder {
t.Helper()
gin.SetMode(gin.TestMode)
router := gin.New()
router.POST("/api/user/auth/refresh", SessionCookieOriginGuard(), func(c *gin.Context) {
c.Status(http.StatusNoContent)
})
request := httptest.NewRequest(http.MethodPost, "https://panel.example.com/api/user/auth/refresh", nil)
request.Host = "panel.example.com"
request.Header.Set("Origin", origin)
if origin == "" {
request.Header.Del("Origin")
}
if referer != "" {
request.Header.Set("Referer", referer)
}
response := httptest.NewRecorder()
router.ServeHTTP(response, request)
return response
}
func TestSessionCookieOriginGuard(t *testing.T) {
previousSecure := common.SessionCookieSecure
previousTrustedURLs := common.SessionCookieTrustedURLs
common.SessionCookieSecure = true
common.SessionCookieTrustedURLs = []string{"https://trusted.example.com"}
t.Cleanup(func() {
common.SessionCookieSecure = previousSecure
common.SessionCookieTrustedURLs = previousTrustedURLs
})
tests := []struct {
name string
origin string
referer string
expected int
}{
{name: "same origin", origin: "https://panel.example.com", expected: http.StatusNoContent},
{name: "trusted exact origin", origin: "https://trusted.example.com", expected: http.StatusNoContent},
{name: "referer fallback", referer: "https://panel.example.com/profile", expected: http.StatusNoContent},
{name: "missing both", expected: http.StatusForbidden},
{name: "null origin", origin: "null", expected: http.StatusForbidden},
{name: "suffix attack", origin: "https://trusted.example.com.evil.test", expected: http.StatusForbidden},
{name: "scheme mismatch", origin: "http://panel.example.com", expected: http.StatusForbidden},
{name: "path in origin", origin: "https://panel.example.com/profile", expected: http.StatusForbidden},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
response := runOriginGuardRequest(t, test.origin, test.referer)
assert.Equal(t, test.expected, response.Code)
assert.Empty(t, response.Header().Get("Access-Control-Allow-Origin"))
})
}
}
func TestSessionCookieOriginGuardDevelopmentCompatibility(t *testing.T) {
previousSecure := common.SessionCookieSecure
previousTrustedURLs := common.SessionCookieTrustedURLs
t.Cleanup(func() {
common.SessionCookieSecure = previousSecure
common.SessionCookieTrustedURLs = previousTrustedURLs
})
common.SessionCookieTrustedURLs = nil
tests := []struct {
name string
secure bool
origin string
expected int
}{
{name: "insecure mode allows mismatched development origins", origin: "http://localhost:3001", expected: http.StatusNoContent},
{name: "insecure mode allows missing origin", expected: http.StatusNoContent},
{name: "secure mode rejects mismatched development origins", secure: true, origin: "http://localhost:3001", expected: http.StatusForbidden},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
common.SessionCookieSecure = test.secure
gin.SetMode(gin.TestMode)
router := gin.New()
router.POST("/api/user/auth/refresh", SessionCookieOriginGuard(), func(c *gin.Context) {
c.Status(http.StatusNoContent)
})
request := httptest.NewRequest(http.MethodPost, "http://localhost:3000/api/user/auth/refresh", nil)
request.Host = "localhost:3000"
if test.origin != "" {
request.Header.Set("Origin", test.origin)
}
response := httptest.NewRecorder()
router.ServeHTTP(response, request)
assert.Equal(t, test.expected, response.Code)
assert.Empty(t, response.Header().Get("Access-Control-Allow-Origin"))
})
}
}
func TestSessionCookieOriginGuardDoesNotTrustForwardedProtoFromClient(t *testing.T) {
previousSecure := common.SessionCookieSecure
previousTrustedURLs := common.SessionCookieTrustedURLs
common.SessionCookieSecure = true
common.SessionCookieTrustedURLs = nil
t.Cleanup(func() {
common.SessionCookieSecure = previousSecure
common.SessionCookieTrustedURLs = previousTrustedURLs
})
gin.SetMode(gin.TestMode)
router := gin.New()
router.POST("/api/user/auth/refresh", SessionCookieOriginGuard(), func(c *gin.Context) {
c.Status(http.StatusNoContent)
})
request := httptest.NewRequest(http.MethodPost, "http://panel.example.com/api/user/auth/refresh", nil)
request.Host = "panel.example.com"
request.Header.Set("Origin", "https://panel.example.com")
request.Header.Set("X-Forwarded-Proto", "https")
response := httptest.NewRecorder()
router.ServeHTTP(response, request)
assert.Equal(t, http.StatusForbidden, response.Code)
}