Migrate Java Random to SecureRandom #1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Migrate OkHttp from Random to SecureRandom for Enhanced Cryptographic Security
Description
This PR comprehensively migrates OkHttp from standard
Random
implementations to cryptographically secureSecureRandom
implementations across all security-sensitive operations. The migration includes extensive performance monitoring tools and documentation to ensure security improvements don't compromise performance.Motivation
Random
implementations are not cryptographically secure and can be predictable, creating vulnerabilities in security-sensitive operations like WebSocket frame masking, OAuth token generation, and SSL/TLS certificate handlingSecureRandom
aligns with security best practices for cryptographic operations and helps meet security compliance requirementsKey Changes
Random
usage toSecureRandom
across OkHttp components including WebSocket handling, connection pooling, and OAuth session managementRandomBenchmark
,RandomMigrationMonitor
,RandomMigrationUtility
) to measure and optimize the security vs performance trade-offsImpact
Files Changed (23 files, +2,101/-23 lines)
Core Changes
okhttp3/OkHttpClient.kt
- Updated client-level random generationokhttp3/internal/connection/RealConnectionPool.kt
- Secured connection pool randomizationokhttp3/internal/ws/RealWebSocket.kt
&WebSocketWriter.kt
- Secured WebSocket frame maskingokhttp3/slack/OAuthSessionFactory.java
- Secured OAuth session generationokhttp3/sample/SampleServer.java
- Updated sample implementationsNew Performance & Testing Infrastructure
docs/random_migration_guide.md
- Comprehensive migration documentationinternal/performance/RandomBenchmark.kt
- Performance comparison toolsinternal/performance/RandomMigrationMonitor.kt
- Real-time monitoringinternal/performance/RandomMigrationUtility.kt
- Migration utilitiesTest Plan
The migration maintains full backward compatibility while significantly improving the security posture of random number generation throughout OkHttp.
Link to run