Mitigation Strategy: Enforce Strict Certificate and Hostname Verification
-
Mitigation Strategy: Enforce Strict Certificate and Hostname Verification
-
Description:
- No Custom
HostnameVerifier
: Ensure that no customHostnameVerifier
is implemented that overrides the default behavior and always returnstrue
. The defaultHostnameVerifier
in OkHttp correctly validates the hostname against the certificate. - No
TrustAllCerts
: Absolutely ensure that no customTrustManager
is used that trusts all certificates (e.g., aTrustManager
with an emptycheckServerTrusted
method). This is a critical security flaw. - Certificate Pinning (Implementation):
- Identify the target server's certificate chain.
- Extract the public key hashes (SPKI – Subject Public Key Information) from the server's certificate, or preferably, from an intermediate CA certificate in the chain.
- Use OkHttp's
CertificatePinner.Builder
to create aCertificatePinner
instance. - Add the extracted public key hashes to the
CertificatePinner
using theadd()
method, specifying the hostname and the hash in the formatpinSha256/<base64-encoded-hash>
. - Build the
CertificatePinner
and set it on theOkHttpClient.Builder
using thecertificatePinner()
method. - Implement a robust pin rotation strategy, including backup pins and monitoring of expiration dates.
- No Custom
-
Threats Mitigated:
- Man-in-the-Middle (MitM) Attacks: (Severity: Critical)
- Certificate Authority (CA) Compromise: (Severity: High)
-
Impact:
- MitM Attacks: Risk reduced from Critical to Low (with pinning) or Moderate (without pinning, but with proper hostname and certificate validation).
- CA Compromise: Risk reduced from High to Low (with pinning).
-
Currently Implemented:
- Basic hostname and certificate validation are implemented by default in
NetworkModule.kt
. - Certificate pinning is partially implemented in
SecurityConfig.kt
, but only for the primary API endpoint (api.example.com
).
- Basic hostname and certificate validation are implemented by default in
-
Missing Implementation:
- Certificate pinning is missing for other API endpoints.
- Backup pins and automated pin rotation are not implemented.
-
Mitigation Strategy: Enforce HTTP/2 and Monitor for Downgrades (OkHttp Configuration)
-
Mitigation Strategy: Enforce HTTP/2 and Monitor for Downgrades (OkHttp Configuration)
-
Description:
- Prefer HTTP/2: Explicitly configure OkHttp to prefer HTTP/2 using
OkHttpClient.Builder.protocols(listOf(Protocol.HTTP_2, Protocol.HTTP_1_1))
. This prioritizes HTTP/2 over HTTP/1.1. - Protocol Logging Interceptor: Implement an
Interceptor
to log the protocol used for each connection:Add this interceptor to yourclass ProtocolLoggingInterceptor : Interceptor { override fun intercept(chain: Interceptor.Chain): Response { val request = chain.request() val response = chain.proceed(request) Log.d("OkHttp", "Protocol: ${response.protocol()}") return response } }
OkHttpClient
.
- Prefer HTTP/2: Explicitly configure OkHttp to prefer HTTP/2 using
-
Threats Mitigated:
- HTTP/2 Downgrade Attacks: (Severity: Moderate)
-
Impact:
- HTTP/2 Downgrade Attacks: Risk reduced from Moderate to Low (with monitoring).
-
Currently Implemented:
- OkHttp is configured to use both HTTP/2 and HTTP/1.1.
-
Missing Implementation:
- Explicit prioritization of HTTP/2 is not set.
- Protocol-specific logging is not implemented.
-
Mitigation Strategy: Manage Connection Pooling and Response Handling
-
Mitigation Strategy: Manage Connection Pooling and Response Handling
-
Description:
- Rely on OkHttp's Pool: Do not manually manage connections. Use OkHttp's built-in connection pool.
- Close Responses: Always close the
Response
body after processing the response, usingresponse.body()?.close()
or atry-with-resources
block. - Configure Timeouts: Set
connectTimeout
,readTimeout
, andwriteTimeout
on theOkHttpClient
usingOkHttpClient.Builder
. - Connection Pool Settings (Optional): Adjust
ConnectionPool
settings (maxIdleConnections
,keepAliveDuration
) if necessary, usingOkHttpClient.Builder.connectionPool()
.
-
Threats Mitigated:
- Connection Reuse Issues: (Severity: Low to Moderate)
- Resource Exhaustion (DoS): (Severity: Moderate)
-
Impact:
- Connection Reuse Issues: Risk reduced to Very Low.
- Resource Exhaustion (DoS): Risk reduced to Low.
-
Currently Implemented:
- OkHttp's default connection pool is used.
readTimeout
andconnectTimeout
are set.- Some response handling uses
try-with-resources
.
-
Missing Implementation:
writeTimeout
is not set.- Consistent use of
try-with-resources
(or explicitclose()
) is missing. - Connection pool settings are not explicitly configured.
-
Mitigation Strategy: Secure Header Management with Interceptors
-
Mitigation Strategy: Secure Header Management with Interceptors
-
Description:
- Interceptors for Headers: Use OkHttp
Interceptor
s to add, remove, or modify all sensitive headers (e.g., API keys, authorization tokens). Centralize header management.class AuthInterceptor(private val apiKey: String) : Interceptor { override fun intercept(chain: Interceptor.Chain): Response { val originalRequest = chain.request() val newRequest = originalRequest.newBuilder() .header("Authorization", "Bearer $apiKey") .build() return chain.proceed(newRequest) } }
- CookieJar: Use OkHttp's
CookieJar
to manage cookies. Implement a customCookieJar
if you need fine-grained control.
- Interceptors for Headers: Use OkHttp
-
Threats Mitigated:
- Unintentional Data Leaks (Headers/Cookies): (Severity: Moderate to High)
-
Impact:
- Unintentional Data Leaks: Risk reduced to Low.
-
Currently Implemented:
- An
AuthInterceptor
is used for an API key. - A default
CookieJar
is used.
- An
-
Missing Implementation:
- No custom
CookieJar
for specific cookie policies.
- No custom
-
Mitigation Strategy: Prevent Denial of Service (DoS) via OkHttp Configuration
-
Mitigation Strategy: Prevent Denial of Service (DoS) via OkHttp Configuration
-
Description:
- Timeouts: Set
connectTimeout
,readTimeout
, andwriteTimeout
on theOkHttpClient
. - Dispatcher Configuration: Use
OkHttpClient.Builder.dispatcher()
to configure theDispatcher
:maxRequests
: Limit total concurrent requests.maxRequestsPerHost
: Limit concurrent requests per host.
- Timeouts: Set
-
Threats Mitigated:
- Denial of Service (DoS) via Resource Exhaustion: (Severity: Moderate to High)
-
Impact:
- DoS: Risk reduced (combined with server-side mitigations).
-
Currently Implemented:
connectTimeout
andreadTimeout
are set.
-
Missing Implementation:
writeTimeout
is not set.Dispatcher
configuration is not explicitly set.
-
Mitigation Strategy: Keep OkHttp Updated
-
Mitigation Strategy: Keep OkHttp Updated
-
Description:
- Dependency Management: Use a dependency management tool (Gradle, Maven).
- Regular Updates: Regularly update OkHttp to the latest stable version.
-
Threats Mitigated:
- Known Vulnerabilities in OkHttp: (Severity: Varies)
-
Impact:
- Known Vulnerabilities: Risk reduced.
-
Currently Implemented:
- OkHttp dependency is managed.
-
Missing Implementation:
- No automated update process.
-