-
Notifications
You must be signed in to change notification settings - Fork 0
feat(onboarding): opt-in "Allow insecure HTTP" for HTTP-only self-hosted instances (#16) #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
app/src/main/java/com/pinakes/app/data/network/CleartextGuard.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package com.pinakes.app.data.network | ||
|
|
||
| import com.pinakes.app.data.store.SessionStore | ||
| import dagger.hilt.EntryPoint | ||
| import dagger.hilt.InstallIn | ||
| import dagger.hilt.components.SingletonComponent | ||
| import okhttp3.Interceptor | ||
| import okhttp3.Response | ||
| import java.io.IOException | ||
|
|
||
| /** | ||
| * App-level cleartext gate. | ||
| * | ||
| * The Android network-security-config permits cleartext at the OS level (it can't whitelist a | ||
| * runtime-entered instance host), so the app is the sole gatekeeper. This interceptor enforces | ||
| * the same "HTTPS required, except loopback / opted-in" rule as [NetworkModule.isTransportAllowed] | ||
| * for EVERY OkHttp client — not just the API one — so image loading (Coil) and the ebook PDF | ||
| * download can't silently downgrade to plain HTTP on an otherwise-HTTPS instance. | ||
| * | ||
| * A plain-HTTP request to a non-loopback host is refused unless the user has explicitly enabled | ||
| * the "Allow insecure HTTP" opt-in ([SessionStore.allowInsecureHttp]). | ||
| */ | ||
| class CleartextGuardInterceptor(private val allowInsecureHttp: () -> Boolean) : Interceptor { | ||
|
|
||
| override fun intercept(chain: Interceptor.Chain): Response { | ||
| val url = chain.request().url | ||
| if (!url.isHttps && !isLoopback(url.host) && !allowInsecureHttp()) { | ||
| throw IOException( | ||
| "Cleartext HTTP blocked for ${url.host}. Enable \"Allow insecure HTTP\" to connect over plain HTTP.", | ||
| ) | ||
| } | ||
| return chain.proceed(chain.request()) | ||
| } | ||
|
|
||
| private fun isLoopback(host: String): Boolean = | ||
| host == "localhost" || host == "127.0.0.1" || host == "10.0.2.2" || host == "::1" || host == "[::1]" | ||
| } | ||
|
|
||
| /** Reaches the singleton [SessionStore] from code that can't be constructor-injected | ||
| * (the Coil [com.pinakes.app.PinakesApplication] image loader, the PDF downloader). */ | ||
| @EntryPoint | ||
| @InstallIn(SingletonComponent::class) | ||
| interface NetworkEntryPoint { | ||
| fun sessionStore(): SessionStore | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.