Skip to content

Fix UnsafeOkHttpClient Conditional Logic - #118

Open
KAbaidoo wants to merge 7 commits into
re-kast:developfrom
KAbaidoo:fix/unsafe-okhttp-client-condition
Open

KAbaidoo wants to merge 7 commits into
re-kast:developfrom
KAbaidoo:fix/unsafe-okhttp-client-condition

Conversation

@KAbaidoo

@KAbaidoo KAbaidoo commented Aug 8, 2026

Copy link
Copy Markdown

Description

This PR adds an option to allow insecure TLS in ApiConfig

In NetworkModule.kt, there is a flawed condition for when to use the UnsafeOkHttpClient. This logic assumes any URL starting with https has a valid production certificate, and any URL that doesn't (i.e., http://) needs TLS disabled. But http:// traffic doesn't use TLS anyway! The actual use case for UnsafeOkHttpClient is for local testing with self-signed HTTPS certificates. If a developer uses a self-signed https://10.0.2.2/, this code will give them a regular OkHttpClient which will immediately crash on a certificate error.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Refactor / maintenance

Checklist

  • I have read the Contributing guidelines.
  • The code builds and existing tests pass (./gradlew build).
  • I have added or updated tests where relevant.
  • I have updated the documentation where relevant.
  • No secrets, tokens, or subscription keys are included in this change.

* Update `ApiConfig` to include `allowInsecureTls` property for local testing
* Refactor `NetworkModule` to use the `allowInsecureTls` flag when configuring `OkHttpClient` instead of checking the URL protocol prefix
* Update `ApiConfig` to include `allowInsecureTls` property for local testing
* Refactor `NetworkModule` to use the `allowInsecureTls` flag when configuring `OkHttpClient` instead of checking the URL protocol prefix
* Update `ApiConfig` test helper to include the `allowInsecureTls` parameter
* Refactor `OkHttpClient` provider tests to explicitly verify standard vs. unsafe clients based on the `allowInsecureTls` flag
* Add assertions to validate hostname verification behavior for secure and insecure configurations

@dubdabasoduba dubdabasoduba left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean up the linting issues then we can merge it

if (config.baseUrl.startsWith("https")) {
OkHttpClient.Builder()
} else {
if (config.allowInsecureTls) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Smart way to do it thanks

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces an explicit ApiConfig.allowInsecureTls switch and updates the app DI NetworkModule to use the unsafe OkHttp builder only when that flag is enabled, fixing the prior scheme-based logic that failed for self-signed https:// endpoints during local testing.

Changes:

  • Add allowInsecureTls (default false) to ApiConfig to explicitly opt into bypassing TLS verification.
  • Update NetworkModule.provideOkHttpClient to choose UnsafeOkHttpClient based on allowInsecureTls rather than URL scheme.
  • Update NetworkModuleTest to validate the new behavior and expectations.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
android/momo-api-sdk/src/commonMain/kotlin/io/rekast/sdk/utils/ApiConfig.kt Adds allowInsecureTls flag to runtime SDK configuration.
android/app/src/main/kotlin/io/rekast/sdk/app/di/NetworkModule.kt Switches unsafe-client selection logic to be driven by the new config flag.
android/app/src/test/kotlin/io/rekast/sdk/app/di/NetworkModuleTest.kt Updates tests and assertions to reflect allowInsecureTls behavior.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines 60 to +64
ApiConfig(
baseUrl = "http://10.0.2.2:8080/",
apiUserId = "user-id",
environment = "sandbox"
environment = "sandbox",
allowInsecureTls = true
Comment on lines +122 to +127
val isUnsafe = try {
client.hostnameVerifier.verify("any-hostname.example.com", null)
} catch (e: NullPointerException) {
false
}
org.junit.Assert.assertFalse(isUnsafe)
Comment on lines +159 to 163
if (config.allowInsecureTls) {
UnsafeOkHttpClient().unsafeOkHttpClient
} else {
OkHttpClient.Builder()
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KAbaidoo check this

@KAbaidoo KAbaidoo Sep 20, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@codecov

codecov Bot commented Aug 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

* Update indentation and line wrapping for the `try-catch` block in `NetworkModuleTest` to comply with formatting standards.
auto-merge was automatically disabled August 25, 2026 08:54

Head branch was pushed to by a user without write access

@KAbaidoo

Copy link
Copy Markdown
Author

Clean up the linting issues then we can merge it

Linting issues resolved!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants