Skip to content

Commit 6373b65

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 225c489 commit 6373b65

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

modules/context/api.go

Lines changed: 13 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2595,6 +2595,10 @@ function updateDeadline(deadlineString) {
25952595
data: JSON.stringify({
25962596
'due_date': realDeadline,
25972597
}),
2598+
headers: {
2599+
'X-Csrf-Token': csrf,
2600+
'X-Remote': true,
2601+
},
25982602
contentType: 'application/json',
25992603
type: 'POST',
26002604
success: function () {

routers/api/v1/api.go

Lines changed: 7 additions & 3 deletions
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)