Skip to content

Commit f5b97f5

Browse files
committed
Add CSRF checking to reqToken and place CSRF in the post for deadline creation
Fixes go-gitea#5226, go-gitea#5249
1 parent f95c966 commit f5b97f5

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

modules/context/api.go

+13
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"fmt"
99
"strings"
1010

11+
"github.com/go-macaron/csrf"
12+
1113
"code.gitea.io/git"
1214
"code.gitea.io/gitea/models"
1315
"code.gitea.io/gitea/modules/base"
@@ -97,6 +99,17 @@ func (ctx *APIContext) SetLinkHeader(total, pageSize int) {
9799
}
98100
}
99101

102+
// RequireCSRF requires a validated a CSRF token
103+
func (ctx *APIContext) RequireCSRF() {
104+
headerToken := ctx.Req.Header.Get(ctx.csrf.GetHeaderName())
105+
formValueToken := ctx.Req.FormValue(ctx.csrf.GetFormName())
106+
if len(headerToken) > 0 || len(formValueToken) > 0 {
107+
csrf.Validate(ctx.Context.Context, ctx.csrf)
108+
} else {
109+
ctx.Context.Error(401)
110+
}
111+
}
112+
100113
// APIContexter returns apicontext as macaron middleware
101114
func APIContexter() macaron.Handler {
102115
return func(c *Context) {

public/js/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -2590,6 +2590,10 @@ function updateDeadline(deadlineString) {
25902590
data: JSON.stringify({
25912591
'due_date': realDeadline,
25922592
}),
2593+
headers: {
2594+
'X-Csrf-Token': csrf,
2595+
'X-Remote': true,
2596+
},
25932597
contentType: 'application/json',
25942598
type: 'POST',
25952599
success: function () {

routers/api/v1/api.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,15 @@ func repoAssignment() macaron.Handler {
174174

175175
// Contexter middleware already checks token for user sign in process.
176176
func reqToken() macaron.Handler {
177-
return func(ctx *context.Context) {
178-
if true != ctx.Data["IsApiToken"] {
179-
ctx.Error(401)
177+
return func(ctx *context.APIContext) {
178+
if true == ctx.Data["IsApiToken"] {
179+
return
180+
}
181+
if ctx.IsSigned {
182+
ctx.RequireCSRF()
180183
return
181184
}
185+
ctx.Context.Error(401)
182186
}
183187
}
184188

0 commit comments

Comments
 (0)