Reviewer · Security
Validate untrusted input
Any user-supplied input accepted by HTTP handlers (query parameters, headers, path segments, etc.) must be bounded and strictly validated before it is used—especially before it is echoed back into response headers or logs, where it could enable injection (e.g. CRLF/header injection) or resource-exhaustion attacks.
Any user-supplied input accepted by HTTP handlers (query parameters, headers, path segments, etc.) must be bounded and strictly validated before it is used—especially before it is echoed back into response headers or logs, where it could enable injection (e.g. CRLF/header injection) or resource-exhaustion attacks.
Concretely:
- Enforce hard limits on the number and size of user-supplied values (e.g. reject requests with too many query parameters or overly long parameter values) before doing any work or writing any response data.
- Use an explicit allowlist pattern/regex for structured input rather than a denylist. For example, when accepting dotted config paths, require each segment to match a known-safe character class:
var validConfigQueryPath = regexp.MustCompile(`^[A-Za-z0-9_]+(\.[A-Za-z0-9_]+)*$`)
func isValidConfigQueryPath(path string) bool {
return validConfigQueryPath.MatchString(path)
}
This rejects empty segments, leading/trailing/double dots, control characters, and CRLF sequences before any header is written or lookup is attempted.
- Perform all validation checks (size/count limits, format checks) up front and return a clear 4xx error (e.g.
400 Bad Requestwith an explanatory message) as soon as a violation is detected—never after partial processing or after writing response headers. - Add test cases covering each rejected shape (empty, oversized, malformed, control characters) to lock in the validation behavior.</description>
</invoke>