Integer Overflow in Fiber with Hmac Signatures
Integer Overflow in Fiber with Hmac Signatures
An integer overflow in a Fiber-based service using Hmac Signatures can occur when a numeric value used in length or size calculations wraps around, producing an unexpectedly small or invalid buffer size. This can lead to memory corruption or allow an attacker to control more data than intended. In a signed request flow, an attacker may supply a crafted length field that overflows when combined with other values, bypassing expected size checks and enabling signature forgery or out-of-bounds reads during verification.
Consider a request that includes a body length and a timestamp combined into a prehash value for an Hmac Signature. If the length is stored as an unsigned 16-bit integer and an attacker sends a length near the maximum value (e.g., 65535), adding even a small constant can overflow, resulting in a tiny computed length. The server may then read far fewer bytes than expected, interpreting the remaining bytes as part of the signature or another field. Because Hmac Signatures bind the payload to the key, subtle changes in how the payload length is interpreted can change the prehash input, potentially allowing an attacker to align forged data with a valid-looking signature if the overflow leads to truncation or misalignment.
In practice, this vulnerability is not about breaking the cryptographic primitive itself but about incorrect arithmetic upstream of the Hmac verification. For example, a Fiber route might compute a prehash as prehash = length + timestamp, then sign prehash + body. If length overflows, the prehash becomes smaller than intended, and the server may verify a signature against a truncated body. An attacker can exploit this by sending a length that wraps to zero, causing the server to treat a short body as valid when it actually contains extra injected data appended after the expected end. This can expose authentication bypass or enable injection if the signature validation does not strictly enforce exact payload boundaries.
Because Fiber does not inherently protect against integer overflow in user-controlled numeric fields, developers must validate and sanitize all size-related inputs before using them in Hmac calculations. This includes checking that additions used to build the prehash cannot wrap, using 64-bit integers where appropriate, and ensuring that the buffer passed to the Hmac function matches the declared length exactly. Without these safeguards, an API that relies on Hmac Signatures in Fiber remains vulnerable to logic flaws that compromise integrity even when the crypto is sound.
Hmac Signatures-Specific Remediation in Fiber
Remediation focuses on safe arithmetic, strict length validation, and canonical handling of the payload before signing or verification. Always use integer types large enough to hold worst-case sums, and check for overflow before performing additions used in prehash construction. Ensure the body length used in Hmac matches the actual byte slice passed to the verification function, and reject requests where declared length deviates from the provided payload.
Below are concrete, working examples for a Fiber route that signs requests with Hmac and verifies them safely.
- Server-side signing and verification in Go using
crypto/hmac:
// Sign request on the client
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"fmt"
"net/http"
"strconv"
"time"
)
func signRequest(body string, secret string) (string, string, error) {
timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
length := int64(len(body)) // use int64 to avoid overflow
prehash := fmt.Sprintf("%d%d", length, timestamp) // build prehash safely
mac := hmac.New(sha256.New, []byte(secret))
mac.Write([]byte(prehash + body))
signature := hex.EncodeToString(mac.Sum(nil))
return signature, timestamp, nil
}
// Verify request on the Fiber server
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"fmt"
"net/http"
"strconv"
)
func verifyHmac(next fiber.Handler) fiber.Handler {
return func(c *fiber.Ctx) error {
receivedSig := c.Get("X-Signature")
receivedTimestamp := c.Get("X-Timestamp")
body := c.Request().Body()
length := int64(len(body)) // actual length from the body
// Recompute prehash using the same canonical format
prehash := fmt.Sprintf("%d%d", length, receivedTimestamp)
mac := hmac.New(sha256.New, []byte(secretKey))
mac.Write([]byte(prehash + string(body)))
expectedSig := hex.EncodeToString(mac.Sum(nil))
if !hmac.Equal([]byte(expectedSig), []byte(receivedSig)) {
return c.Status(fiber.StatusUnauthorized).SendString("invalid signature")
}
return next(c)
}
}
- Key remediation steps embedded in the code:
- Use
int64for length and timestamp sums to avoid 16-bit or 32-bit overflow. - Validate that the declared length equals
len(body)before building the prehash; reject mismatches. - Use
hmac.Equalfor constant-time comparison to prevent timing attacks. - Include both length and timestamp in the prehash to bind size and freshness, reducing replay and overflow opportunities.
In addition to code fixes, apply schema-level constraints (e.g., JSON schema numeric ranges) and runtime checks to ensure no user-supplied integer can exceed safe bounds. This approach keeps Hmac Signatures reliable and prevents integer overflow from undermining integrity in Fiber services.