Skip to content
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
7 changes: 1 addition & 6 deletions internal/middleware/jqschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,7 @@ func init() {

// generateRandomID generates a random ID for payload storage
func generateRandomID() string {
id, err := strutil.RandomHex(16)
if err != nil {
// Fallback to a process- and time-based ID if random generation fails.
return fmt.Sprintf("fallback-%d-%d", os.Getpid(), time.Now().UnixNano())
}
return id
return strutil.RandomHexWithFallback(16)
}

// applyJqSchema applies the jq schema transformation to JSON data
Expand Down
22 changes: 22 additions & 0 deletions internal/strutil/random_hex.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package strutil

import (
"crypto/rand"
"encoding/binary"
"encoding/hex"
"fmt"
"os"
"time"
)

// RandomHex returns a hex-encoded string of n cryptographically random bytes.
Expand All @@ -18,3 +21,22 @@ func RandomHex(n int) (string, error) {
}
return hex.EncodeToString(b), nil
}

// RandomHexWithFallback returns a hex-encoded string of n random bytes.
// On the normal path it returns the same output as RandomHex(n) — a string of
// length 2*n containing cryptographically random hex characters.
// If crypto/rand is unavailable, it falls back to a hex-encoded pid+nanosecond
// value that is unique within a single process run. The fallback is non-cryptographic
// and should only arise in unusual runtime environments; it always produces a
// 32-character hex string (16 bytes), regardless of n. For the typical call site
// (n == 16) the fallback output length matches the normal output length.
func RandomHexWithFallback(n int) string {
s, err := RandomHex(n)
if err != nil {
b := make([]byte, 16)
binary.LittleEndian.PutUint64(b[:8], uint64(os.Getpid()))
binary.LittleEndian.PutUint64(b[8:], uint64(time.Now().UnixNano()))
return hex.EncodeToString(b)
}
return s
Comment on lines +25 to +41

Copilot AI Apr 28, 2026

Copy link

Choose a reason for hiding this comment

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

Despite the name/comment, this function does not always return hex: on fallback it returns a fallback-... string which will fail hex.DecodeString and break callers that assume hex semantics (length 2*n, lowercase hex, etc.). Consider either making the fallback also hex-encoded (e.g., hash/encode the fallback bytes), or changing the API to surface whether fallback occurred (or return an error) so callers can decide how to handle non-hex output.

This issue also appears in the following locations of the same file:

  • line 24
  • line 25

Copilot uses AI. Check for mistakes.
}
40 changes: 40 additions & 0 deletions internal/strutil/random_hex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,43 @@ func TestRandomHex_Uniqueness(t *testing.T) {
seen[id] = true
}
}

// TestRandomHexWithFallback_ReturnsValidHex verifies the normal (non-fallback) path returns
// a valid hex-encoded string of the correct length.
func TestRandomHexWithFallback_ReturnsValidHex(t *testing.T) {
result := RandomHexWithFallback(16)
require.NotEmpty(t, result, "result should not be empty")
// On a healthy system crypto/rand always succeeds, so we expect a 32-char hex string.
assert.Len(t, result, 32, "16 bytes should produce 32 hex chars")
_, err := hex.DecodeString(result)
assert.NoError(t, err, "result should be valid hex")
}
Comment on lines +93 to +102

Copilot AI Apr 28, 2026

Copy link

Choose a reason for hiding this comment

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

TestRandomHexWithFallback_ReturnsValidHex assumes crypto/rand always succeeds and fails the test if the fallback path is taken (which is exactly the scenario this helper is meant to support in unusual environments). Consider making the test deterministic by temporarily overriding crypto/rand.Reader with a known-good reader so this test always exercises the non-fallback path, and add a separate test that forces rand.Read to fail to cover the fallback path explicitly.

Copilot uses AI. Check for mistakes.

// TestRandomHexWithFallback_ZeroBytes verifies zero bytes produces an empty string.
func TestRandomHexWithFallback_ZeroBytes(t *testing.T) {
result := RandomHexWithFallback(0)
assert.Empty(t, result, "zero bytes should produce empty string")
}

// TestRandomHexWithFallback_Uniqueness verifies the function produces unique values.
func TestRandomHexWithFallback_Uniqueness(t *testing.T) {
seen := make(map[string]bool)
for i := 0; i < 100; i++ {
id := RandomHexWithFallback(16)
assert.NotEmpty(t, id)
assert.False(t, seen[id], "RandomHexWithFallback should produce unique values")
seen[id] = true
}
}

// TestRandomHexWithFallback_NegativeFallsBack verifies that a negative size triggers the
// fallback path (since RandomHex returns an error for negative n) and that the fallback
// still produces a valid hex-encoded string.
func TestRandomHexWithFallback_NegativeFallsBack(t *testing.T) {
result := RandomHexWithFallback(-1)
// The fallback produces a valid 32-character hex string (pid + nanoseconds encoded).
assert.NotEmpty(t, result, "fallback should produce a non-empty string")
_, err := hex.DecodeString(result)
assert.NoError(t, err, "fallback should produce valid hex")
assert.Len(t, result, 32, "fallback should produce 32 hex chars (16 bytes)")
}
Loading