Regex Dos in Fiber with Hmac Signatures
Regex Dos in Fiber with Hmac Signatures — how this specific combination creates or exposes the vulnerability
A Regex Denial-of-Service (ReDoS) occurs when a regular expression has overlapping or nested quantifiers on unbounded input, causing catastrophic backtracking. In Fiber, a popular fast HTTP router for Go, this risk is amplified when HMAC signatures are used to validate request authenticity and are processed with vulnerable patterns.
Consider an endpoint that expects an HMAC signature in a header, e.g., X-API-Signature. To verify integrity, the server might parse query parameters or body fields and apply a regex to extract or validate parts of the input before computing the HMAC. If the regex is poorly constructed, an attacker can send crafted data that causes the regex engine to consume excessive CPU time. Because HMAC verification typically occurs before business logic, this creates a denial-of-service vector on the authentication boundary itself.
For example, a common mistake is using a pattern like (a+)+ to validate repeated segments in a token or identifier. When combined with Hmac Signatures in Fiber, an attacker can send a long string of as, causing exponential backtracking during signature validation. The scan checks such patterns across 12 security checks, including Input Validation and Authentication, to flag unsafe regex usage that can degrade performance or block legitimate requests.
In the context of API security scanning, middleBrick detects these patterns by correlating static analysis of route handlers and middleware with runtime behavior. If your OpenAPI spec describes signature validation logic and the implementation uses vulnerable regex, the tool reports this under Authentication and Input Validation findings. This is critical because ReDoS does not require authentication; it is an unauthenticated attack surface that can disrupt availability without credentials.
Real-world CVEs such as CVE-2022-31129 illustrate how regex vulnerabilities in parsing logic can lead to denial-of-service. When Hmac Signatures in Fiber rely on complex string extraction, similar risks emerge if the regex is not linear. The scanner tests for these by analyzing regex constructs and flagging patterns known to cause excessive backtracking, helping teams identify weak points before an attacker exploits them.
Hmac Signatures-Specific Remediation in Fiber — concrete code fixes
To mitigate Regex Dos in Fiber when using Hmac Signatures, focus on simplifying regex patterns, avoiding nested quantifiers, and moving validation logic outside hot paths. Below are concrete, secure code examples for Fiber in Go.
Vulnerable Pattern Example
The following demonstrates a risky approach that can lead to ReDoS:
// DO NOT USE: vulnerable regex with nested quantifiers
re := regexp.MustCompile(`^([a-zA-Z0-9]+)+$`)
func validateInput(input string) bool {
return re.MatchString(input)
}
Secure Remediation 1: Use Linear Regex
Replace nested quantifiers with a single-level pattern. For Hmac Signatures, which are typically hex or base64, keep validation simple and deterministic:
// Secure: linear regex without backtracking risk
var hexRegex = regexp.MustCompile(`^[a-fA-F0-9]+$`)
var base64Regex = regexp.MustCompile(`^[A-Za-z0-9+/]+=*$`)
func isValidHex(s string) bool {
return hexRegex.MatchString(s)
}
func isValidBase64(s string) bool {
return base64Regex.MatchString(s)
}
Secure Remediation 2: Avoid Regex Altogether for Fixed Formats
For Hmac Signatures, prefer length checks and character set validation without regex:
// Secure: no regex, constant-time checks for hex HMAC
func isValidHexNoRegex(s string) bool {
if len(s) != 64 { // SHA256 hex length
return false
}
for _, c := range s {
if !(c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F') {
return false
}
}
return true
}
Middleware Integration in Fiber
Apply validation in middleware to protect all routes consistently:
func SignatureValidationMiddleware() fiber.Handler {
return func(c *fiber.Ctx) error {
sig := c.Get("X-API-Signature")
if !isValidHexNoRegex(sig) {
return c.Status(fiber.StatusBadRequest).SendString("invalid signature format")
}
// Proceed to HMAC verification using crypto/hmac
return c.Next()
}
}
// Attach to routes
app.Post("/webhook", SignatureValidationMiddleware(), func(c *fiber.Ctx) error {
// Handle request after safe validation
return c.SendString("ok")
})
Additional Guidance
- Keep regex patterns flat: avoid
(...)+or(a|aa)*. - Use standard library tools like
regexpwith caution; consider linear alternatives. - In CI/CD, integrate middleBrick to flag unsafe regex patterns in your OpenAPI specs and source code, especially around authentication and Hmac Signatures in Fiber implementations.
Related CWEs: inputValidation
| CWE ID | Name | Severity |
|---|---|---|
| CWE-20 | Improper Input Validation | HIGH |
| CWE-22 | Path Traversal | HIGH |
| CWE-74 | Injection | CRITICAL |
| CWE-77 | Command Injection | CRITICAL |
| CWE-78 | OS Command Injection | CRITICAL |
| CWE-79 | Cross-site Scripting (XSS) | HIGH |
| CWE-89 | SQL Injection | CRITICAL |
| CWE-90 | LDAP Injection | HIGH |
| CWE-91 | XML Injection | HIGH |
| CWE-94 | Code Injection | CRITICAL |