Skip to content

Commit 84adf0b

Browse files
committed
fix: upgrade generateBoundary from MD5 to SHA-256
1 parent 8b4c91a commit 84adf0b

2 files changed

Lines changed: 25 additions & 15 deletions

File tree

internal/hooks/hook_executor_v2.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ package hooks
1717
import (
1818
"bytes"
1919
"context"
20-
"crypto/md5"
2120
"crypto/rand"
21+
"crypto/sha256"
2222
"encoding/hex"
2323
"math/big"
2424
"strings"
@@ -37,7 +37,7 @@ type HookExecutorMessageBoundaryProtocol struct {
3737
}
3838

3939
// generateBoundary is a function for creating boundaries that can be mocked
40-
var generateBoundary = generateMD5FromRandomString
40+
var generateBoundary = generateRandomBoundary
4141

4242
// Execute processes the data received by the SDK.
4343
func (e *HookExecutorMessageBoundaryProtocol) Execute(ctx context.Context, opts HookExecOpts) (string, error) {
@@ -100,24 +100,25 @@ func (e *HookExecutorMessageBoundaryProtocol) Execute(ctx context.Context, opts
100100
return buffout.String(), nil
101101
}
102102

103-
// generateMD5FromRandomString returns the MD5 hash of a randomized string.
103+
// generateRandomBoundary returns the SHA-256 hash of a randomized string for use
104+
// as a message boundary between the CLI and SDK.
104105
//
105106
// Reference: https://gist.github.com/dopey/c69559607800d2f2f90b1b1ed4e550fb
106-
func generateMD5FromRandomString() string {
107+
func generateRandomBoundary() string {
107108
const alphanumericCharacters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
108109
const length = 10
109110

110-
randomBytes := make([]byte, 0)
111+
randomBytes := make([]byte, 0, length)
111112
for range length {
112113
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(alphanumericCharacters))))
113114
if err != nil {
114-
return "3561f3a3c5576e2ce0dc0d1e268bb9b2" // Return default value to continue execution
115+
// Return default value to continue execution
116+
return "a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1b2"
115117
}
116118
randomBytes = append(randomBytes, alphanumericCharacters[num.Int64()])
117119
}
118120

119-
MD5Hash := md5.New()
120-
s := MD5Hash.Sum(randomBytes)
121-
122-
return hex.EncodeToString(s)
121+
hash := sha256.New()
122+
hash.Write(randomBytes)
123+
return hex.EncodeToString(hash.Sum(nil))
123124
}

internal/hooks/hook_executor_v2_test.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
package hooks
1616

1717
import (
18+
"crypto/sha256"
19+
"encoding/hex"
1820
"errors"
1921
"io"
2022
"strings"
@@ -215,11 +217,18 @@ func Test_Hook_Execute_V2_Protocol(t *testing.T) {
215217
}
216218
}
217219

218-
func Test_Hook_Execute_V2_GenerateMD5FromRandomString(t *testing.T) {
219-
randomString1 := generateMD5FromRandomString()
220-
randomString2 := generateMD5FromRandomString()
220+
func Test_Hook_Execute_V2_GenerateRandomBoundary(t *testing.T) {
221+
randomString1 := generateRandomBoundary()
222+
randomString2 := generateRandomBoundary()
221223

222224
assert.NotEqual(t, randomString1, randomString2)
223-
assert.GreaterOrEqual(t, len(randomString1), 10)
224-
assert.GreaterOrEqual(t, len(randomString2), 10)
225+
assert.Equal(t, 64, len(randomString1))
226+
assert.Equal(t, 64, len(randomString2))
227+
}
228+
229+
func Test_Hook_Execute_V2_GenerateRandomBoundary_UsesSHA256(t *testing.T) {
230+
boundary := generateRandomBoundary()
231+
_, err := hex.DecodeString(boundary)
232+
assert.NoError(t, err)
233+
assert.Equal(t, sha256.Size*2, len(boundary))
225234
}

0 commit comments

Comments
 (0)