Mitigation Strategy: Request/Response Middleware for Sanitization
-
Description:
- Create a Custom Faraday Middleware Class: Define a new Ruby class that inherits from
Faraday::Middleware
. - Implement the
call
Method: This method receives the request environment (env
). - Inspect Request Headers: Access
env.request_headers
. Redact or remove sensitive headers (e.g.,Authorization
,Cookie
). Use a whitelist. - Inspect Request Body (Carefully): If
env.request_body
exists andContent-Type
is structured (e.g.,application/json
), parse it safely. Redact sensitive fields. Handle parsing errors. - Call the Next Middleware: Use
@app.call(env)
. - Inspect Response (in
on_complete
): Use.on_complete
on@app.call(env)
's result. Repeat steps 3 and 4 for the response. - Register the Middleware: Use
conn.use YourMiddlewareClass
when creating your Faraday connection. Position it strategically (before logging).
- Create a Custom Faraday Middleware Class: Define a new Ruby class that inherits from
-
Threats Mitigated:
- Unintentional Exposure of Sensitive Data in Requests: (Severity: High)
- Data Leakage via Logging: (Severity: High)
-
Impact:
- Unintentional Exposure of Sensitive Data: Significantly reduces risk.
- Data Leakage via Logging: Significantly reduces risk.
-
Currently Implemented:
- Partially implemented in
app/middleware/sensitive_data_redactor.rb
. RedactsAuthorization
header.
- Partially implemented in
-
Missing Implementation:
- Response header redaction.
- Request/response body redaction.
- Not used by all Faraday connections.
Mitigation Strategy: Limit Redirects (follow_redirects
)
-
Description:
- Identify Faraday Connections: Locate all Faraday connection configurations.
- Configure
follow_redirects
: If enabled, setlimit
to a reasonable value (e.g., 3 or 5). - Disable if Unnecessary: If following redirects isn't needed, disable it.
-
Threats Mitigated:
- Open Redirects (via
follow_redirects
): (Severity: Medium) - Denial of Service (DoS) via Redirect Loops: (Severity: Low)
- Open Redirects (via
-
Impact:
- Open Redirects: Reduces risk.
- DoS via Redirect Loops: Eliminates risk of infinite loops.
-
Currently Implemented:
- Partially implemented. Some connections have limits, but not all.
-
Missing Implementation:
- Consistent application to all connections.
- Some connections might use default (unlimited) redirects.
Mitigation Strategy: Choose and Configure Secure Adapters
-
Description:
- Review Adapter Options: Examine Faraday adapters (Net::HTTP, Typhoeus, Excon) and documentation.
- Prefer Secure Defaults: Choose adapters known for security. Net::HTTP (with TLS) is a good default.
- Configure TLS Properly: Use TLS 1.2+ with strong cipher suites. Avoid insecure versions/ciphers. This is done within the Faraday adapter configuration.
- Avoid Custom Adapters (Unless Necessary): If creating a custom adapter, follow security best practices (TLS, input validation).
-
Threats Mitigated:
- Insecure Adapter Configuration: (Severity: Medium to High)
- Man-in-the-Middle (MITM) Attacks (if TLS is misconfigured): (Severity: High)
-
Impact:
- Insecure Adapter Configuration: Reduces risk.
- MITM Attacks: Significantly reduces risk with correct TLS.
-
Currently Implemented:
- Primarily uses Net::HTTP.
-
Missing Implementation:
- Explicit TLS configuration (ciphers, minimum version) is inconsistent.
Mitigation Strategy: Enforce SSL/TLS Certificate Verification
-
Description:
- Locate Faraday Connections: Find all configurations.
- Explicitly Enable Verification: Set
ssl: { verify: true }
for all connections, especially in production. Make it explicit. - Use Test Certificates: In development/testing, use self-signed certs or a local CA, not disabling verification. Configure Faraday to trust them.
- Consider Certificate Pinning (Advanced): Explore pinning (may require custom middleware/adapter features). This is configured through Faraday (or its adapter).
-
Threats Mitigated:
- Man-in-the-Middle (MITM) Attacks: (Severity: High)
-
Impact:
- MITM Attacks: Eliminates risk with correct implementation.
-
Currently Implemented:
- Verification is enabled by default in most connections.
-
Missing Implementation:
- Not all connections explicitly set
ssl: { verify: true }
. - No use of test certificates; verification is sometimes disabled.
- No certificate pinning.
- Not all connections explicitly set
Mitigation Strategy: Set Reasonable Timeouts
-
Description:
- Locate Faraday Connections: Find all configurations.
- Configure Timeouts: Set
conn.options.timeout
(overall) andconn.options.open_timeout
(connection open) to reasonable values (seconds). - Consider Per-Endpoint Timeouts: For different services, set timeouts per-request:
request.options.timeout
andrequest.options.open_timeout
.
-
Threats Mitigated:
- Denial of Service (DoS) via Slow Requests: (Severity: Medium)
-
Impact:
- DoS via Slow Requests: Significantly reduces risk.
-
Currently Implemented:
- Some connections have timeouts, but not all.
-
Missing Implementation:
- Consistent application to all connections.
- Some might use default (long) timeouts.
- No per-endpoint timeouts.