Pii Leakage in Echo Go with Bearer Tokens
Pii Leakage in Echo Go with Bearer Tokens — how this specific combination creates or exposes the vulnerability
Pii Leakage in Echo Go with Bearer Tokens occurs when authentication credentials are handled in ways that expose personally identifiable information during request processing. In the Echo framework, Bearer Tokens are commonly passed via the Authorization header, but if responses or logs include these tokens or if token validation logic inadvertently exposes user data, Pii Leakage can occur. For example, when debugging or error handling paths include the full Authorization header, tokens and associated user identifiers may be written to logs or reflected in API responses, leading to Pii Leakage.
Consider an Echo route that validates a Bearer Token and returns user profile data. If the handler directly includes the token or user details from the token in verbose error messages, or if middleware logs headers without redaction, an attacker who can trigger an error response may receive Pii embedded in the payload. This risk is amplified when token introspection responses or user data structures include fields like email, name, or ID and are returned without proper filtering. Even when tokens themselves are not secret in the sense of encryption, the linkage between a token and Pii in responses can violate privacy expectations and compliance requirements such as GDPR and HIPAA.
Middleware that inspects Bearer Tokens for routing or context enrichment can also contribute to Pii Leakage if it propagates token-associated claims into response structures or logs. For instance, extracting user ID from a token and attaching it to a response body without necessity increases exposure. Similarly, misconfigured CORS or logging settings can cause token-related Pii to be exposed to unauthorized origins or through browser developer tools. The combination of Echo Go’s flexible routing and common patterns for Bearer Token usage creates multiple avenues where Pii can leak through error paths, logging, or improper data handling tied to token validation.
Bearer Tokens-Specific Remediation in Echo Go — concrete code fixes
To mitigate Pii Leakage in Echo Go when using Bearer Tokens, apply strict handling and filtering practices. Always validate tokens without exposing their contents in responses or logs, and ensure that user data returned by endpoints is limited to what is necessary. Below are concrete code examples demonstrating secure patterns.
Example 1: Secure Bearer Token validation without logging tokens
//go
package main
import (
"net/http"
"strings"
"github.com/labstack/echo/v4"
)
func main() {
e := echo.New()
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
auth := c.Request().Header.Get("Authorization")
if auth == "" {
return echo.NewHTTPError(http.StatusUnauthorized, "missing authorization header")
}
parts := strings.Split(auth, " ")
if len(parts) != 2 || parts[0] != "Bearer" {
return echo.NewHTTPError(http.StatusBadRequest, "invalid authorization format")
}
rawToken := parts[1]
// Validate token with your auth provider (e.g., verify signature, call introspection endpoint)
// Do NOT log rawToken or include it in any response
if !isValidToken(rawToken) {
return echo.NewHTTPError(http.StatusUnauthorized, "invalid token")
}
// Set minimal user context, avoid attaching Pii
c.Set("uid", extractUserID(rawToken))
return next(c)
}
})
e.GET("/profile", func(c echo.Context) error {
uid := c.Get("uid")
// Return only necessary, non-sensitive data
return c.JSON(http.StatusOK, map[string]interface{}{
"user_id": uid,
})
})
e.Logger.Fatal(e.Start(":8080"))
}
func isValidToken(token string) bool {
// Implement token validation logic (e.g., JWT verification or introspection)
return token == "valid_token_example"
}
func extractUserID(token string) string {
// Extract user ID without exposing claims
return "user-123"
}
Example 2: Response filtering to prevent Pii Leakage
//go
package main
import (
"net/http"
"github.com/labstack/echo/v4"
)
type UserProfile struct {
ID string `json:"user_id"`
Name string `json:"-"` // excluded from JSON output
Email string `json:"-"` // excluded to prevent Pii Leakage
}
func main() {
e := echo.New()
e.GET("/profile/:id", func(c echo.Context) error {
userID := c.Param("id")
// Fetch user data securely
profile := UserProfile{
ID: userID,
Name: "Jane Doe",
Email: "[email protected]",
}
// Return only safe fields
return c.JSON(http.StatusOK, map[string]string{
"user_id": profile.ID,
})
})
e.Logger.Fatal(e.Start(":8080"))
}
Example 3: Middleware to redact sensitive headers in logs
//go
package main
import (
"net/http"
"strings"
"github.com/labstack/echo/v4"
)
func secureLogger(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
req := c.Request()
// Redact Authorization header from logs
auth := req.Header.Get("Authorization")
if auth != "" && strings.HasPrefix(auth, "Bearer ") {
// Replace token with placeholder in request copy for logging
req.Header.Set("Authorization", "Bearer [REDACTED]")
}
// Proceed with request handling
return next(c)
}
}
func main() {
e := echo.New()
e.Use(secureLogger)
e.GET("/data", func(c echo.Context) error {
return c.String(http.StatusOK, "ok")
})
e.Logger.Fatal(e.Start(":8080"))
}
Key remediation practices
- Never log raw Bearer Tokens or include them in error responses.
- Limit returned user data to non-sensitive fields and use struct tags to exclude Pii fields.
- Use middleware to sanitize headers before logging or processing.
- Validate tokens through secure channels and avoid exposing introspection details to clients.
Related CWEs: dataExposure
| CWE ID | Name | Severity |
|---|---|---|
| CWE-200 | Exposure of Sensitive Information | HIGH |
| CWE-209 | Error Information Disclosure | MEDIUM |
| CWE-213 | Exposure of Sensitive Information Due to Incompatible Policies | HIGH |
| CWE-215 | Insertion of Sensitive Information Into Debugging Code | MEDIUM |
| CWE-312 | Cleartext Storage of Sensitive Information | HIGH |
| CWE-359 | Exposure of Private Personal Information (PII) | HIGH |
| CWE-522 | Insufficiently Protected Credentials | CRITICAL |
| CWE-532 | Insertion of Sensitive Information into Log File | MEDIUM |
| CWE-538 | Insertion of Sensitive Information into Externally-Accessible File | HIGH |
| CWE-540 | Inclusion of Sensitive Information in Source Code | HIGH |