Mitigation Strategy: Middleware Configuration and Ordering
Description:
- Planning: Create a document outlining the purpose of each Fiber middleware, its dependencies, and security implications. Define a clear execution order, prioritizing security-critical Fiber middleware.
- Implementation: In your Fiber application's setup (e.g.,
main.go
), add Fiber middleware in the planned order. Use Fiber's built-in middleware functions (e.g.,app.Use(fiber.Compress())
,app.Use(fiber.Cors(config))
,app.Use(csrf.New(config))
). For custom Fiber middleware, ensure correct placement. - Configuration: Configure each Fiber middleware instance with appropriate settings. For
fiber.Cors
, specify allowed origins, methods, and headers explicitly. Forfiber.CSRF
, integrate with your templating/frontend. Forfiber.Limiter
, set appropriate rate limits. - Testing: Write integration tests verifying the correct behavior of Fiber middleware. Test scenarios where middleware should block/allow requests (e.g., invalid CSRF token with
fiber.CSRF
, exceeding rate limit withfiber.Limiter
, unauthorized origin withfiber.Cors
). - Review: Regularly review the Fiber middleware configuration and order, especially after adding new features or updating Fiber or its dependencies.
Threats Mitigated:
- Cross-Site Request Forgery (CSRF) (High Severity): Incorrect or missing
fiber.CSRF
middleware allows attackers to trick users. - Cross-Origin Resource Sharing (CORS) Misconfiguration (Medium to High Severity): Overly permissive
fiber.Cors
settings allow unauthorized access. - Denial of Service (DoS) (Medium to High Severity): Missing or misconfigured
fiber.Compress
orfiber.Limiter
can lead to DoS. - Authentication Bypass (Critical Severity): Incorrect Fiber middleware order can bypass authentication.
- Authorization Bypass (Critical Severity): Incorrect Fiber middleware order can bypass authorization.
- Data Leakage (Medium to High Severity): Misconfigured
fiber.Recover
or custom error handling can expose information.
Impact:
- CSRF: Risk reduced from High to Low (with proper
fiber.CSRF
implementation). - CORS Misconfiguration: Risk reduced from Medium/High to Low (with correct
fiber.Cors
configuration). - DoS: Risk reduced from Medium/High to Low/Medium (depending on attack and
fiber.Compress
/fiber.Limiter
configuration). - Authentication/Authorization Bypass: Risk reduced from Critical to Low (if auth middleware is correctly placed).
- Data Leakage: Risk reduced from Medium/High to Low/Medium (depending on data and
fiber.Recover
configuration).
Currently Implemented: (Example: fiber.Cors
and fiber.CSRF
are used, but fiber.Limiter
is not. Middleware order is documented in middleware.md
.)
Missing Implementation: (Example: fiber.Limiter
is missing for all API endpoints. Integration tests for Fiber middleware interactions are incomplete.)
Mitigation Strategy: Error Handling and Information Leakage (Fiber-Specific)
Description:
- Custom Error Handler: Create a custom error handler function:
func myCustomErrorHandler(c *fiber.Ctx, err error) error
. - Global Configuration: Use
app.Config.ErrorHandler = myCustomErrorHandler
to set the global Fiber error handler. - Error Handling Logic: Inside
myCustomErrorHandler
:- Log the error details (including stack traces, if appropriate) internally. Do not send to the client.
- Determine an appropriate HTTP status code based on the error type.
- Return a generic error message to the client via
c.Status(...).JSON(...)
. Never returnerr.Error()
directly. Example:return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "An unexpected error occurred."})
- Route-Specific (Optional): Handle errors within route handlers if needed, but never expose internal details.
- Testing: Write tests that trigger errors and verify responses do not contain sensitive information from Fiber or underlying libraries.
Threats Mitigated:
- Information Disclosure (Medium to High Severity): Exposing internal Fiber error details (stack traces, internal paths) helps attackers.
Impact:
- Information Disclosure: Risk reduced from Medium/High to Low.
Currently Implemented: (Example: A custom error handler is in handlers/errors.go
, but doesn't consistently log all details.)
Missing Implementation: (Example: Some routes return raw Fiber error messages. Testing for information leakage is incomplete.)
Mitigation Strategy: Routing and Parameter Handling (Fiber-Specific)
Description:
- Precise Routes: Define Fiber routes with specific paths and methods. Avoid broad wildcards or regex unless necessary.
- Parameter Validation: Use Fiber's parsing/validation:
app.Get("/users/:id", func(c *fiber.Ctx) error { id, err := c.ParamsInt("id") // Use Fiber's methods if err != nil { return c.Status(fiber.StatusBadRequest).SendString("Invalid ID") // Use Fiber's status codes } // ... use the validated 'id' ... return nil })
- Type Conversion: Use Fiber's
c.ParamsInt
,c.ParamsBool
,c.Query
, etc., for type conversion. - Whitelist (if applicable): If a Fiber parameter has limited valid values, use a whitelist.
- Sanitization: If using user input in file paths/commands, sanitize thoroughly. Whitelist when possible. This is crucial even with Fiber.
- Testing: Test with valid and invalid Fiber parameter values to ensure validation works.
Threats Mitigated:
- Parameter Tampering (Medium to High Severity): Attackers modify Fiber route parameters.
- Path Traversal (High Severity): If user input constructs file paths (even with Fiber routing), attackers can access unintended files.
- Command Injection (Critical Severity): If user input is in commands (even indirectly via Fiber), attackers can execute arbitrary commands.
Impact:
- Parameter Tampering: Risk reduced from Medium/High to Low (with Fiber's validation).
- Path Traversal: Risk reduced from High to Low (with sanitization/whitelisting – Fiber alone doesn't prevent this).
- Command Injection: Risk reduced from Critical to Low (with sanitization/whitelisting – Fiber alone doesn't prevent this).
Currently Implemented: (Example: Basic Fiber parameter validation is used, but sanitization for file paths is missing in one handler.)
Missing Implementation: (Example: /files/:filename
doesn't sanitize filename
, risking path traversal – even with Fiber's routing.)
Mitigation Strategy: Session Management (Fiber-Specific)
Description:
- Secure Store: Use Fiber's session middleware with a secure backend (Redis, database). Do not use the default in-memory store in production.
- Configuration: Configure Fiber's session middleware:
Cookie.Secure = true
(HTTPS only)Cookie.HttpOnly = true
(prevent JS access)Cookie.SameSite = fiber.SameSiteStrictMode
(restrict cross-origin)Expiration
: Set a reasonable expiration.
- Session ID: Fiber's default session ID generation should be cryptographically strong; verify.
- Invalidation: On logout, explicitly invalidate the session:
session.Destroy()
. - Testing: Test Fiber session creation, expiration, invalidation, concurrent sessions, and access after logout.
Threats Mitigated:
- Session Hijacking (High Severity): Attackers steal Fiber session cookies.
- Session Fixation (High Severity): Attackers set a known Fiber session ID.
- Cross-Site Scripting (XSS) (High Severity): If Fiber session cookies aren't
HttpOnly
, they can be stolen via XSS.
Impact:
- Session Hijacking/Fixation: Risk reduced from High to Low (with proper Fiber session configuration).
- XSS (session cookies): Risk reduced from High to Low (with
HttpOnly
).
Currently Implemented: (Example: Fiber's session middleware uses Redis. Secure
, HttpOnly
, SameSite
are set. Sessions expire after 30 minutes.)
Missing Implementation: (Example: No automated tests verify Fiber session security attributes.)