Files
AFFiNE/packages/frontend/apps/android
CommaBotandDarkSky c91b08fbcc fix(android): allow plain http only to local network servers (#15925)
Release builds of the Android app set `usesCleartextTraffic=false` and
`MIXED_CONTENT_NEVER_ALLOW` (#15118), so the app could not reach a
self-hosted server over http at all, including one on the local network.

This allows plain-text http/ws only to local network hosts, matching iOS
`NSAllowsLocalNetworking`:

- unqualified single-label names (`nas`, `localhost`) and `.local` names
- loopback, private and link-local IP literals: 127/8, 10/8, 172.16/12,
192.168/16, 169.254/16, `::1`, fc00::/7, fe80::/10

Anything else is refused, including public-looking names that resolve to
a private address (names are never resolved).

Android WebView cannot allow mixed content per host: the app runs at
`https://localhost`, so every http/ws request is mixed content,
`mixedContentMode` is WebView-wide, and WebView only honours the
app-wide cleartext flag. So cleartext and `MIXED_CONTENT_ALWAYS_ALLOW`
are enabled globally, and the rule is enforced in three places with the
same host check:

- `AffineWebViewClient.shouldInterceptRequest` answers plain http
requests to other hosts with a 403.
- The web app wraps `WebSocket` and `fetch` (main thread and the nbstore
worker), because WebView request interception never sees WebSocket
handshakes. This is where the sync `ws://` connection is refused.
- An OkHttp interceptor rejects plain http to other hosts for the native
clients (GraphQL, SSE, auth).

On Android and iOS, the add-server step rejects public `http://`
addresses with a message asking for an https address, a local IP or a
`.local` name.

The host check exists in TS (`@affine/core/utils/local-network`) and
Kotlin (`LocalNetwork.kt`); both are tested against the same table,
`core/src/utils/local-network/fixtures/hosts.json` (the Android unit
tests read it as a test resource). Other tests show public `http://` and
`ws://` are blocked in the web guard and in an OkHttp client, while LAN,
`.local` and https pass.

#15509 (server on a LAN IP) is fixed by this. The setup in #15438
(`http://affine.home.com`) stays blocked, as it is on iOS: that server
needs https, a local IP address or a `.local` name.

Tested: the TS tests with vitest, the Kotlin tests with JUnit outside
Gradle (Kotlin 2.2.20, JDK 21). Not run on a device or emulator; the
WebView and WebSocket behaviour is covered by unit tests only.

fix #15438
fix #15509


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **New Features**
* Android now allows HTTP connections to servers on the local network,
while blocking insecure connections to public servers. HTTPS connections
remain available.
* When adding a self-hosted server on Android or iOS, you’ll see
guidance if its public address uses HTTP. Use an HTTPS address or a
local network address instead.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

Co-authored-by: DarkSky <darksky2048@gmail.com>
2026-11-24 12:01:26 +08:00
..
2026-09-20 13:01:49 +08:00

Android

AFFiNE Android app.

Setup

  • set CARGO_HOME to your system environment

  • add

    rust.cargoCommand={replace_with_your_own_cargo_home_absolute_path}/bin/cargo

    rust.rustcCommand={replace_with_your_own_cargo_home_absolute_path}/bin/rustc

    to App/local.properties

Build

  • yarn install
  • BUILD_TYPE=canary PUBLIC_PATH="/" yarn affine @affine/android build
  • yarn affine @affine/android cap sync
  • yarn affine @affine/android cap open android