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 potential vulnerability in use of the rand.Seed #2

Merged
merged 1 commit into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"net/url"
"path/filepath"
Expand Down Expand Up @@ -80,8 +79,7 @@ func main() {

loginError = WebAuthnError{Message: "Unable to login"}
registrationError = WebAuthnError{Message: "Error during registration"}

rand.Seed(time.Now().UnixNano())
util.RandInit()

users = make(map[string]u.User)
registrations = make(map[string]u.User)
Expand Down
16 changes: 14 additions & 2 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import (
"errors"
"fmt"
"log"
"math/rand"
"net/http"
"regexp"

"github.com/duo-labs/webauthn/webauthn"
"github.com/gorilla/sessions"

crypto_rand "crypto/rand"
"encoding/binary"
math_rand "math/rand"
)

const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
Expand Down Expand Up @@ -83,11 +86,20 @@ func SaveWebauthnSession(session *sessions.Session, key string, sessionData *web
return nil
}

func RandInit() {
var b [8]byte
_, err := crypto_rand.Read(b[:])
if err != nil {
panic("cannot seed math/rand package with cryptographically secure random number generator")
}
math_rand.Seed(int64(binary.LittleEndian.Uint64(b[:])))
}

// Generate a random string of alpha characters of length n
func RandStringBytesRmndr(n int) []byte {
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Int63()%int64(len(letterBytes))]
b[i] = letterBytes[math_rand.Int63()%int64(len(letterBytes))]
}
return b
}