Skip to content

Support HTTP Custom AI endpoints for local and LAN servers - #30

Open
marufmoinuddin wants to merge 7 commits into
smnexstudio:mainfrom
marufmoinuddin:pr/custom-local-endpoints-v060
Open

marufmoinuddin wants to merge 7 commits into
smnexstudio:mainfrom
marufmoinuddin:pr/custom-local-endpoints-v060

Conversation

@marufmoinuddin

@marufmoinuddin marufmoinuddin commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Accept Custom AI HTTP endpoints for local/private addresses and local hostnames while keeping public endpoints HTTPS-only.
  • Preserve base-path and pasted /chat/completions URL normalization.
  • Use a separate guarded client for Custom AI local HTTP POSTs; preserve normal provider/update HTTPS guards, disabled redirects and response caps.
  • Explain unencrypted HTTP text/key transmission in the English endpoint hint.

Android network policy

The network-security base config permits cleartext at platform level; local-host restriction is enforced in endpoint validation and the dedicated HTTP client, not by the XML policy. System certificate trust is retained. Local hostname classification is syntactic, not a DNS/private-address verification.

Scope

Independent of the whole-page scan; based on upstream main 48ab2e4 (0.6.0).

Validation

In the combined integration worktree on the VPS:

  • Debug APK built successfully.
  • core:model tests passed.
  • EngineHttpGuardTest and ManifestPolicyTest passed, including production-guard local HTTP acceptance and HTTP rejection for another engine.
  • Phone/real LAN-server testing and full-suite execution have been performed and working successfully in my device Xiaomi 15.

@smnexstudio

Copy link
Copy Markdown
Owner

Follow-up after reviewing the PR more closely to clarify what is already covered and what still needs attention.

Already implemented (thanks for addressing these):

  • Separate localHttpClient, while the shared provider and update clients continue to enforce HttpsOnly.
  • Routing that allows HTTP only for CUSTOM providers with an http:// URL, along with a test confirming that Gemini still rejects HTTP.
  • Broad and readable isLocalHost validation.
  • Existing /chat/completions URL normalization remains intact.

Remaining concerns

The main concern is that network_security_config.xml now permits cleartext traffic across the entire app. While this is necessary (Android XML cannot express a wildcard for all 192.168.x.x addresses, and OkHttp checks the platform policy first), it also removes the platform-level backstop for the app-wide HTTPS-only policy.

1. Explicitly block non-HTTPS WebView requests (blocking)

Previously, the reader relied on Android's platform policy to reject redirects from HTTPS pages to HTTP. With the cleartext policy now relaxed, that protection is no longer available.

Please explicitly enforce HTTPS-only navigation and requests in the WebView, covering:

  • Reader sites
  • Gemini
  • Custom AI

This should also account for redirects from HTTPS to HTTP, rather than checking only the initial URL.

2. Expand test coverage

Please add or update tests to cover the remaining scenarios, particularly:

  • HTTP requests being allowed only for valid local CUSTOM endpoints.
  • HTTP requests being rejected for non-local custom endpoints.
  • Gemini and other providers continuing to reject HTTP.
  • WebView navigation and redirects from HTTPS to HTTP being blocked.
  • Existing HTTPS behavior and URL normalization remaining unaffected.

3. Smaller follow-up items

  • Translations: Update the remaining 15 translations of key_base_url_note, which still describe the endpoint as HTTPS-only. Also, display a visible warning in the configuration sheet whenever the entered address uses http://.
  • Documentation: PRIVACY.md, docs/ARCHITECTURE.md (NFR-2), and the repository's HTTPS-only policy need to reflect the new local HTTP exception. I'm happy to take care of these once the implementation design is finalized.
  • URL validation: Prefer HttpUrl.isHttps over url.startsWith("http://") for the routing check, so the decision is based on the parsed URL rather than string matching.
image

Why separate Custom AI and LAN / Private Custom AI?

They represent two different trust boundaries and should have separate security rules.

  • Custom AI: For cloud providers (OpenAI, DeepSeek, Groq, etc.). HTTPS only, API key required.
  • LAN / Private Custom AI: For self-hosted servers (Ollama, LM Studio, llama.cpp). HTTP allowed for private addresses only, API key optional.

Why split them?

  1. Prevent accidental downgrade: A mistyped cloud URL must never result in API keys and translation data being sent over unencrypted HTTP.
  2. Isolate the exception: Private-address validation and cleartext allowances remain limited to the LAN engine, preserving HTTPS-only behavior everywhere else.
  3. Different authentication requirements: Cloud providers require an API key, while local servers often don't.
  4. Clearer UX: A separate engine and setup sheet can explicitly warn users when they connect over an unencrypted connection.

Still required: Explicit WebView non-HTTPS blocking and resolved-address (DNS) validation, since the app-wide network security configuration now permits cleartext traffic.

CI is red

Please fix the build .

Thanks again for the work so far. The core implementation is in place; the main remaining priority is ensuring that relaxing Android's cleartext policy for local endpoints doesn't unintentionally weaken HTTPS enforcement elsewhere in the app.

@smnexstudio smnexstudio left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Please implement the suggested changes, address the remaining security requirements, and resolve any build failures. Ensure the existing functionality and tests remain intact, and verify that the project builds successfully before submitting the updated PR.

@marufmoinuddin

Copy link
Copy Markdown
Contributor Author

Security redesign

The implementation now restores Android’s platform cleartext block while isolating the LAN HTTP exception:

  • Restored cleartextTrafficPermitted=false in the network-security base configuration, with no domain exceptions or debug trust overrides. LAN HTTP support no longer requires relaxing WebView’s platform policy. Reader HTTPS checks, HTTP upgrades, and mixed-content blocking remain additional protections.
  • Restricted the dedicated HTTP socket transport to CUSTOM_LAN JSON POST requests. Cloud requests and LAN HTTPS continue using Retrofit/OkHttp with standard TLS validation.
  • Added validation of all resolved DNS addresses, direct connection to a validated private/loopback address, and connected-peer validation before sending request data. The transport does not follow redirects or use proxies.
  • Added request, response, and metadata bounds; strict HTTP framing checks; configured timeouts; and interruptible cancellation.

The branch also separates cloud and LAN endpoint/model/credential settings, supports optional LAN authentication without cloud-key fallback, preserves base-path and pasted /chat/completions normalization, and includes LAN-specific hints and visible HTTP warnings in English and all 15 translated resource sets.

PRIVACY.md, architecture NFR-2, and docs/LAN_ENDPOINT_SECURITY.md describe the revised design and its limitations.

Validation and scope

Fresh local checks reported for the integrated redesign:

  • Engine JVM suite: 170 tests, zero failures, errors, or skips.
  • ManifestPolicyTest: seven tests, zero failures, errors, or skips.

The eight new socket-transport tests cover POST/path handling and optional authorization; isolation of 301/302/303/307/308 redirects; fixed-length and chunked response limits; chunk decoding; public-address rejection; ambiguous/duplicate framing rejection; and cancellation. The initial cancellation failure and subsequent correction are documented.

Earlier commits also received a fresh six-module JVM run totaling 493 passing tests, followed by a separate settings-module run of 61 passing tests after additional settings coverage. These are historical, commit-specific results—not a fresh full-project run for 7f0ffd5.

A separately inspected existing debug APK passed ZIP-integrity and APK Signature Scheme v2 checks and contained a network-security configuration with platform cleartext blocking and system certificate trust only. Its exact source commit was not established, so that inspection does not verify that 7f0ffd5 successfully packages or runs. Local assembly attempts encountered execution/connector deadlines.

No Android/WebView runtime result is claimed for this redesign. Earlier environment preparation did not produce an emulator test run, and emulator work has stopped. The earlier Xiaomi 15 statement should not be treated as verification of the current commit.

Review focus and remaining verification

Please review whether the restored platform cleartext policy and isolated LAN transport address the WebView downgrade concern without weakening cloud HTTPS enforcement.

The new HTTP/1.1 transport also needs security and compatibility review. It intentionally rejects unsupported compression and duplicate headers, connects to one validated DNS answer, and does not pool connections or automatically replay requests. DNS resolution relies on the system resolver.

Full-project CI for 7f0ffd5 was still running at the last check; I am not claiming a green result or a verified full-project build for this commit. Android runtime compatibility also remains unverified.

The redesign is ready for review, but this update does not claim merge readiness.

Thank you for the detailed feedback you provided earlier. It helped strengthen the separation between cloud and LAN endpoints.

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.

2 participants