This commit is contained in:
parent
14079f4608
commit
e78f382ee4
@ -118,7 +118,7 @@ func contextIterceptor(h controllers.HandlerFunc) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if stateChangingMethod(r.Method) && hasSessionCookie(r) && (!sameOriginRequest(r) || !csrf.Valid(r)) {
|
if stateChangingMethod(r.Method) && !csrfExemptPath(r.URL.Path) && hasSessionCookie(r) && (!sameOriginRequest(r) || !csrf.Valid(r)) {
|
||||||
response.NewErrorResponse(response.ParamsError, "请求来源校验失败", "").FPrint(w)
|
response.NewErrorResponse(response.ParamsError, "请求来源校验失败", "").FPrint(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -262,6 +262,15 @@ func stateChangingMethod(method string) bool {
|
|||||||
return method == http.MethodPost || method == http.MethodPut || method == http.MethodPatch || method == http.MethodDelete
|
return method == http.MethodPost || method == http.MethodPut || method == http.MethodPatch || method == http.MethodDelete
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func csrfExemptPath(path string) bool {
|
||||||
|
switch normalizedAPIPath(path) {
|
||||||
|
case "/api/user/info":
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func hasSessionCookie(r *http.Request) bool {
|
func hasSessionCookie(r *http.Request) bool {
|
||||||
if session.Instance == nil {
|
if session.Instance == nil {
|
||||||
return false
|
return false
|
||||||
|
|||||||
@ -30,6 +30,15 @@ func TestRouterRejectsGetForPostOnlyReadStateEndpoints(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCSRFExemptsReadOnlyUserInfoEndpoint(t *testing.T) {
|
||||||
|
if !csrfExemptPath("/api/v1/user/info") {
|
||||||
|
t.Fatal("user info endpoint must be exempt from CSRF token validation")
|
||||||
|
}
|
||||||
|
if csrfExemptPath("/api/v1/user/edit") {
|
||||||
|
t.Fatal("user edit endpoint must keep CSRF token validation")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func configureProductionOriginTest(t *testing.T) {
|
func configureProductionOriginTest(t *testing.T) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
oldConfig := config.Instance
|
oldConfig := config.Instance
|
||||||
|
|||||||
@ -12,6 +12,19 @@ export default defineConfig(({ mode }) => {
|
|||||||
const backendUrl = (env.VITE_DEV_BACKEND_URL || 'https://127.0.0.1:443').replace(/\/$/, '')
|
const backendUrl = (env.VITE_DEV_BACKEND_URL || 'https://127.0.0.1:443').replace(/\/$/, '')
|
||||||
const backendOrigin = (env.VITE_DEV_BACKEND_ORIGIN || 'https://xuans.com').replace(/\/$/, '')
|
const backendOrigin = (env.VITE_DEV_BACKEND_ORIGIN || 'https://xuans.com').replace(/\/$/, '')
|
||||||
|
|
||||||
|
const rewriteDevelopmentCookies = (proxyResponse) => {
|
||||||
|
const setCookies = proxyResponse.headers['set-cookie']
|
||||||
|
if (!Array.isArray(setCookies)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
proxyResponse.headers['set-cookie'] = setCookies.map((cookie) => (
|
||||||
|
cookie
|
||||||
|
.replace(/;\s*Secure/gi, '')
|
||||||
|
.replace(/;\s*Domain=[^;]*/gi, '')
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
plugins: [
|
plugins: [
|
||||||
vue(),
|
vue(),
|
||||||
@ -42,6 +55,7 @@ export default defineConfig(({ mode }) => {
|
|||||||
proxyRequest.setHeader('x-forwarded-proto', originURL.protocol.replace(':', ''))
|
proxyRequest.setHeader('x-forwarded-proto', originURL.protocol.replace(':', ''))
|
||||||
proxyRequest.removeHeader('referer')
|
proxyRequest.removeHeader('referer')
|
||||||
})
|
})
|
||||||
|
proxy.on('proxyRes', rewriteDevelopmentCookies)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"/attachments": {
|
"/attachments": {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user