]> source.dussan.org Git - gitea.git/commitdiff
Fix #5226 by adding CSRF checking to api reqToken and add CSRF to the POST header...
authorzeripath <art27@cantab.net>
Sun, 4 Nov 2018 01:15:55 +0000 (01:15 +0000)
committertechknowlogick <hello@techknowlogick.com>
Sun, 4 Nov 2018 01:15:55 +0000 (21:15 -0400)
* Add CSRF checking to reqToken and place CSRF in the post for deadline creation

Fixes #5226, #5249

* /api/v1/admin/users routes should have reqToken middleware

integrations/api_admin_test.go
integrations/git_test.go
modules/context/api.go
public/js/index.js
routers/api/v1/api.go

index 690edad757a640a878ff56eb739071bfbea58ccf..b8dded9c116a4bd80153d6295a7ca254144e4d7d 100644 (file)
@@ -39,8 +39,8 @@ func TestAPIAdminCreateAndDeleteSSHKey(t *testing.T) {
                OwnerID:     keyOwner.ID,
        })
 
-       req = NewRequestf(t, "DELETE", "/api/v1/admin/users/%s/keys/%d?token="+token,
-               keyOwner.Name, newPublicKey.ID)
+       req = NewRequestf(t, "DELETE", "/api/v1/admin/users/%s/keys/%d?token=%s",
+               keyOwner.Name, newPublicKey.ID, token)
        session.MakeRequest(t, req, http.StatusNoContent)
        models.AssertNotExistsBean(t, &models.PublicKey{ID: newPublicKey.ID})
 }
@@ -51,7 +51,7 @@ func TestAPIAdminDeleteMissingSSHKey(t *testing.T) {
        session := loginUser(t, "user1")
 
        token := getTokenForLoggedInUser(t, session)
-       req := NewRequestf(t, "DELETE", "/api/v1/admin/users/user1/keys/%d?token="+token, models.NonexistentID)
+       req := NewRequestf(t, "DELETE", "/api/v1/admin/users/user1/keys/%d?token=%s", models.NonexistentID, token)
        session.MakeRequest(t, req, http.StatusNotFound)
 }
 
@@ -73,8 +73,8 @@ func TestAPIAdminDeleteUnauthorizedKey(t *testing.T) {
 
        session = loginUser(t, normalUsername)
        token = getTokenForLoggedInUser(t, session)
-       req = NewRequestf(t, "DELETE", "/api/v1/admin/users/%s/keys/%d?token="+token,
-               adminUsername, newPublicKey.ID)
+       req = NewRequestf(t, "DELETE", "/api/v1/admin/users/%s/keys/%d?token=%s",
+               adminUsername, newPublicKey.ID, token)
        session.MakeRequest(t, req, http.StatusForbidden)
 }
 
index 7ac375dd029d673cb5a43cfe00301c9fb8f258a9..96d39e0519e7dfeadc66413854d7d3c7a4f0c2c3 100644 (file)
@@ -143,7 +143,8 @@ func TestGit(t *testing.T) {
 
                        session := loginUser(t, "user1")
                        keyOwner := models.AssertExistsAndLoadBean(t, &models.User{Name: "user2"}).(*models.User)
-                       urlStr := fmt.Sprintf("/api/v1/admin/users/%s/keys", keyOwner.Name)
+                       token := getTokenForLoggedInUser(t, session)
+                       urlStr := fmt.Sprintf("/api/v1/admin/users/%s/keys?token=%s", keyOwner.Name, token)
 
                        dataPubKey, err := ioutil.ReadFile(keyFile + ".pub")
                        assert.NoError(t, err)
index 0bf4307726e8e3b75ea0cadfe03fe75664744580..6a9c792370f4fd23c08c84bbcd0f7d72c573e355 100644 (file)
@@ -8,6 +8,8 @@ import (
        "fmt"
        "strings"
 
+       "github.com/go-macaron/csrf"
+
        "code.gitea.io/git"
        "code.gitea.io/gitea/models"
        "code.gitea.io/gitea/modules/base"
@@ -97,6 +99,17 @@ func (ctx *APIContext) SetLinkHeader(total, pageSize int) {
        }
 }
 
+// RequireCSRF requires a validated a CSRF token
+func (ctx *APIContext) RequireCSRF() {
+       headerToken := ctx.Req.Header.Get(ctx.csrf.GetHeaderName())
+       formValueToken := ctx.Req.FormValue(ctx.csrf.GetFormName())
+       if len(headerToken) > 0 || len(formValueToken) > 0 {
+               csrf.Validate(ctx.Context.Context, ctx.csrf)
+       } else {
+               ctx.Context.Error(401)
+       }
+}
+
 // APIContexter returns apicontext as macaron middleware
 func APIContexter() macaron.Handler {
        return func(c *Context) {
index 9aafa7d6464b5cbb4916360182b7993765af9eb4..f5d3ef2d93d71b1546a9063a177e2a3b76bcaafa 100644 (file)
@@ -2595,6 +2595,10 @@ function updateDeadline(deadlineString) {
         data: JSON.stringify({
             'due_date': realDeadline,
         }),
+        headers: {
+            'X-Csrf-Token': csrf,
+            'X-Remote': true,
+        },
         contentType: 'application/json',
         type: 'POST',
         success: function () {
index b12cb1374a61bf40511a1cb117cb533c65225297..a839ce8dc1d5fc5ff872166344a5b0d7dbb9f6fa 100644 (file)
@@ -174,11 +174,15 @@ func repoAssignment() macaron.Handler {
 
 // Contexter middleware already checks token for user sign in process.
 func reqToken() macaron.Handler {
-       return func(ctx *context.Context) {
-               if true != ctx.Data["IsApiToken"] {
-                       ctx.Error(401)
+       return func(ctx *context.APIContext) {
+               if true == ctx.Data["IsApiToken"] {
+                       return
+               }
+               if ctx.IsSigned {
+                       ctx.RequireCSRF()
                        return
                }
+               ctx.Context.Error(401)
        }
 }
 
@@ -635,7 +639,7 @@ func RegisterRoutes(m *macaron.Macaron) {
                                        m.Post("/repos", bind(api.CreateRepoOption{}), admin.CreateRepo)
                                })
                        })
-               }, reqAdmin())
+               }, reqToken(), reqAdmin())
 
                m.Group("/topics", func() {
                        m.Get("/search", repo.TopicSearch)