Pii Leakage in Fiber
Fiber-Specific Remediation
Remediating PII leakage in Fiber applications requires a multi-layered approach combining input validation, output filtering, and secure error handling. The first layer is implementing proper data sanitization before database queries:
app.Get("/api/users/:id", func(c *fiber.Ctx) error {
id := c.Params("id")
// Validate ID format before query
if !isValidUUID(id) {
return c.Status(400).JSON(fiber.Map{
"error": "Invalid user ID format",
})
}
var user User
result := db.First(&user, id)
// Generic error message without revealing existence
if result.RowsAffected == 0 {
return c.Status(404).JSON(fiber.Map{
"error": "Resource not found",
})
}
// Filter sensitive fields before response
response := map[string]interface{}{
"id": user.ID,
"name": user.Name,
"created_at": user.CreatedAt,
}
return c.JSON(response)
})This approach validates input format, uses generic error messages, and explicitly filters the response to exclude sensitive fields like email, phone, and address.
For logging middleware, implement proper redaction:
app.Use(func(c *fiber.Ctx) error {
// Create redacted copy of headers
redactedHeaders := c.Fasthttp().Request.Header.Clone()
// Remove sensitive headers
redactedHeaders.Del("Authorization")
redactedHeaders.Del("Cookie")
redactedHeaders.Del("Set-Cookie")
log.Printf("Request: %s %s from %s",
c.Method(), c.Path(), c.IP())
return c.Next()
})Implement secure error handling middleware to prevent stack trace exposure:
app.Use(func(c *fiber.Ctx) error {
return c.Next()
}, func(err error, c *fiber.Ctx) error {
// Log the actual error internally
log.Printf("Error: %v", err)
// Return generic error to client
return c.Status(500).JSON(fiber.Map{
"error": "Internal server error",
})
})For query parameter handling, implement input validation and sanitization:
app.Get("/search", func(c *fiber.Ctx) error {
query := c.Query("q")
// Validate and sanitize input
if len(query) > 255 {
return c.Status(400).JSON(fiber.Map{
"error": "Query too long",
})
}
// Sanitize to prevent injection
sanitizedQuery := sanitizeInput(query)
results := searchDatabase(sanitizedQuery)
return c.JSON(fiber.Map{
"results": results,
// Never echo back raw query
})
})Additionally, implement response filtering for all API endpoints using middleware:
func filterSensitiveData(next fiber.Handler) fiber.Handler {
return func(c *fiber.Ctx) error {
// Execute the handler
err := next(c)
// If response is JSON, filter sensitive fields
if c.Response().Header.ContentType() == "application/json" {
var data map[string]interface{}
if err := json.Unmarshal(c.Response().Body(), &data); err == nil {
// Remove sensitive keys
for _, key := range []string{"email", "phone", "ssn", "password"} {
delete(data, key)
}
// Re-serialize filtered response
filtered, _ := json.Marshal(data)
c.Response().SetBody(filtered)
}
}
return err
}
}
app.Use(filterSensitiveData)This comprehensive approach ensures PII is protected at every layer of your Fiber application, from input validation through response filtering and secure error handling.
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 |