Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: CSRF vulnerability #44

Merged
merged 8 commits into from
Jul 18, 2023
31 changes: 18 additions & 13 deletions backend/WebUI/api_webui.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package WebUI

import (
"context"
"crypto/rand"
"crypto/tls"
"encoding/json"
"fmt"
"math/rand"
"net/http"
"reflect"
"strconv"
Expand Down Expand Up @@ -411,9 +411,14 @@ func JWT(email, userId, tenantId string) string {
mu.Lock()

if jwtKey == "" {
rand.Seed(time.Now().UnixNano())
for i := 0; i < 256; i++ {
jwtKey += string(rune(rand.Intn(128)))
randomBytes := make([]byte, 128)
_, err := rand.Read(randomBytes)
if err != nil {
logger.ProcLog.Warnln("Generate JWT Private Key error.")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean that if creating a random key fails, the signature is done with an empty string as key?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but I think that it would not happened. This err checking scope is added to pass the golangci-lint.

} else {
jwtKey = string(randomBytes)
}
}
}
mu.Unlock()
Expand Down Expand Up @@ -551,7 +556,7 @@ func GetTenants(c *gin.Context) {
setCorsHeader(c)

if !CheckAuth(c) {
c.JSON(http.StatusNotFound, gin.H{"cause": "Illegal Token"})
c.JSON(http.StatusUnauthorized, gin.H{"cause": "Illegal Token"})
return
}

Expand All @@ -575,7 +580,7 @@ func GetTenantByID(c *gin.Context) {
setCorsHeader(c)

if !CheckAuth(c) {
c.JSON(http.StatusNotFound, bson.M{})
c.JSON(http.StatusUnauthorized, gin.H{"cause": "Illegal Token"})
return
}

Expand Down Expand Up @@ -607,7 +612,7 @@ func PostTenant(c *gin.Context) {
setCorsHeader(c)

if !CheckAuth(c) {
c.JSON(http.StatusNotFound, bson.M{})
c.JSON(http.StatusUnauthorized, gin.H{"cause": "Illegal Token"})
return
}

Expand Down Expand Up @@ -637,7 +642,7 @@ func PutTenantByID(c *gin.Context) {
setCorsHeader(c)

if !CheckAuth(c) {
c.JSON(http.StatusNotFound, bson.M{})
c.JSON(http.StatusUnauthorized, gin.H{"cause": "Illegal Token"})
return
}

Expand Down Expand Up @@ -677,7 +682,7 @@ func DeleteTenantByID(c *gin.Context) {
setCorsHeader(c)

if !CheckAuth(c) {
c.JSON(http.StatusNotFound, bson.M{})
c.JSON(http.StatusUnauthorized, gin.H{"cause": "Illegal Token"})
return
}

Expand Down Expand Up @@ -719,7 +724,7 @@ func GetUsers(c *gin.Context) {
setCorsHeader(c)

if !CheckAuth(c) {
c.JSON(http.StatusNotFound, bson.M{})
c.JSON(http.StatusUnauthorized, gin.H{"cause": "Illegal Token"})
return
}

Expand Down Expand Up @@ -756,7 +761,7 @@ func GetUserByID(c *gin.Context) {
setCorsHeader(c)

if !CheckAuth(c) {
c.JSON(http.StatusNotFound, bson.M{})
c.JSON(http.StatusUnauthorized, gin.H{"cause": "Illegal Token"})
return
}

Expand Down Expand Up @@ -796,7 +801,7 @@ func PostUserByID(c *gin.Context) {
setCorsHeader(c)

if !CheckAuth(c) {
c.JSON(http.StatusNotFound, bson.M{})
c.JSON(http.StatusUnauthorized, gin.H{"cause": "Illegal Token"})
return
}

Expand Down Expand Up @@ -850,7 +855,7 @@ func PutUserByID(c *gin.Context) {
setCorsHeader(c)

if !CheckAuth(c) {
c.JSON(http.StatusNotFound, bson.M{})
c.JSON(http.StatusUnauthorized, gin.H{"cause": "Illegal Token"})
return
}

Expand Down Expand Up @@ -926,7 +931,7 @@ func DeleteUserByID(c *gin.Context) {
setCorsHeader(c)

if !CheckAuth(c) {
c.JSON(http.StatusNotFound, bson.M{})
c.JSON(http.StatusUnauthorized, gin.H{"cause": "Illegal Token"})
return
}

Expand Down