Skip to content

Commit

Permalink
fix: (gosec) G115: integer overflow conversion uint -> int64
Browse files Browse the repository at this point in the history
Not ideal though
  • Loading branch information
KEINOS committed Aug 21, 2024
1 parent 6849248 commit 4605b04
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
6 changes: 6 additions & 0 deletions totp/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/ecdh"
"crypto/rand"
"encoding/pem"
"math"
"testing"
"time"

Expand Down Expand Up @@ -342,12 +343,17 @@ func TestKey_skew_as_one(t *testing.T) {
numValid := 0
numIterations := 10

if timeSleep > uint(math.MaxInt) {
t.Fatalf("output length too large: %d", timeSleep)
}

// If skew is set to 0, the validation fails 60-70% of the time.
for range numIterations {
passCode, err := key.PassCode()
require.NoError(t, err, "failed to generate passcode")

// Sleep to validate passcode with an almost last-minute deadline.
//nolint:gosec // timeSleep is checked above
time.Sleep(time.Second * time.Duration(timeSleep))

if ok := key.Validate(passCode); ok {
Expand Down
7 changes: 7 additions & 0 deletions totp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package totp

import (
"crypto/ecdh"
"math"

"github.com/pkg/errors"
"github.com/zeebo/blake3"
Expand All @@ -19,6 +20,12 @@ const (
// OptionKDFDefault is the default key derivation function (KDF) for TOTP secret
// key derivation from ECDH shared secret. The underlying KDF is BLAKE3.
func OptionKDFDefault(secret, ctx []byte, outLen uint) ([]byte, error) {
// Check if outLen is within the int range
if outLen > uint(math.MaxInt) {
return nil, errors.Errorf("output length too large: %d", outLen)
}

//nolint:gosec // outLen is checked above
out := int(outLen)
if out <= 0 {
return nil, errors.Errorf("invalid output length: %d", out)
Expand Down
4 changes: 2 additions & 2 deletions totp/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ func TestOptionKDFDefault_invalid_length(t *testing.T) {

require.Error(t, err,
"requesting key with length 0 should return an error")
require.Contains(t, err.Error(), "output length too large",
"error message must contain the error reason")
require.Nil(t, newSecret,
"returned key must be nil on error")
require.Contains(t, err.Error(), "invalid output length",
"error message must contain the error reason")
})
}

0 comments on commit 4605b04

Please sign in to comment.