Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: jwt key leakage risk (#1118)

h3n4lec20aea0b232
diff --git a/api/actuator.go b/api/actuator.go
index 4067f7ec36..041d4dac49 100644
--- a/api/actuator.go
+++ b/api/actuator.go
@@ -9,5 +9,6 @@ type ServerInfo struct {
Host string `json:"host"`
Port string `json:"port"`
NeedAdminSetup bool `json:"needAdminSetup"`
- StartedTs int64 `json:"startedTs"`
+ // Rand may be based on the server start time, thus exposing startedTs to the client may cause security issues (e.g. jwt key is based on Rand).
+ // StartedTs int64 `json:"startedTs"`
}
diff --git a/common/util.go b/common/util.go
index db496fca52..ec486f8f52 100644
--- a/common/util.go
+++ b/common/util.go
@@ -1,7 +1,8 @@
package common
import (
- "math/rand"
+ "crypto/rand"
+ "math/big"
"path"
"sort"
"strings"
@@ -25,7 +26,15 @@ func RandomString(n int) string {
var sb strings.Builder
sb.Grow(n)
for i := 0; i < n; i++ {
- sb.WriteRune(letters[rand.Intn(len(letters))])
+ // The reason for using crypto/rand instead of math/rand is that
+ // the former relies on hardware to generate random numbers and
+ // thus has a stronger source of random numbers.
+ randNum, err := rand.Int(rand.Reader, big.NewInt(int64(len(letters))))
+ if err != nil {
+ // Use 0 as default index when encounter error
+ sb.WriteRune(letters[0])
+ }
+ sb.WriteRune(letters[randNum.Uint64()])
}
return sb.String()
}
diff --git a/server/actuator.go b/server/actuator.go
index 7944a0cf86..03c80984fc 100644
--- a/server/actuator.go
+++ b/server/actuator.go
@@ -13,12 +13,11 @@ func (s *Server) registerActuatorRoutes(g *echo.Group) {
g.GET("/actuator/info", func(c echo.Context) error {
ctx := context.Background()
serverInfo := api.ServerInfo{
- Version: s.version,
- Readonly: s.readonly,
- Demo: s.demo,
- Host: s.host,
- Port: strconv.Itoa(s.port),
- StartedTs: s.startedTs,
+ Version: s.version,
+ Readonly: s.readonly,
+ Demo: s.demo,
+ Host: s.host,
+ Port: strconv.Itoa(s.port),
}
findRole := api.Owner