feat(waiter): encrypted, key-pinned connection to the venue server (native app)
- Android: MainActivity installs a WebViewClient whose onReceivedSslError proceeds only when the server's SPKI SHA-256 is in TrustStore (pins of all paired venues, SharedPreferences); XeniaTlsPlugin lets JS set that list. Spike showed this one callback covers fetch/XHR, images AND WebSockets. Pins are per key, not per address: IP changes, renewals and expiry never need action; a different key is always refused. - Pairing: QR key (k=) must equal the server's advertised pin, else refused; typed addresses trust the advertised key on first use. Then the venue moves to https://<ip>:<tls.port> (stays on HTTP if that port isn't reachable yet). - upgradeToTls(): on every start, a plain-HTTP venue moves onto TLS once the server offers it (never accepting a key different from the stored one). - main.jsx pushes the venues' pins to native before the first request. - Rediscovery made proactive: checks the saved address at start and every minute while the live connection is down, instead of waiting for requests to an unanswered IP to time out (minutes). HTTPS probes get 5s: the first TLS connection in a fresh process takes ~2s (measured). E2E on the emulator vs an isolated stack: wrong-key QR refused; pairing on TLS; tables + wss live; impostor server with another key refused natively; HTTP venue upgraded on start; server cert renewed (same key) - app keeps working without re-pairing; rediscovery over TLS (11s). Step 5 HTTP rediscovery (now 2.7s/4.8s) and cold-start login tests, and web modes, pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
*/
|
||||
import { deliveryMode, getActiveVenue, saveVenue } from '../config/server'
|
||||
import { probeIdentity, scanForVenue } from './rediscovery'
|
||||
import { syncTrustedKeys, tlsBaseUrl } from './tls'
|
||||
|
||||
const MIN_INTERVAL_MS = 60_000
|
||||
|
||||
@@ -46,3 +47,29 @@ export function rediscover({ force = false, onProgress } = {}) {
|
||||
})
|
||||
return running
|
||||
}
|
||||
|
||||
/**
|
||||
* Move a venue that is still on plain HTTP (paired before TLS existed, or while
|
||||
* :8443 wasn't reachable) onto the encrypted endpoint. Runs on every app start
|
||||
* until it succeeds; afterwards it's a no-op. A key that differs from the one
|
||||
* stored at pairing is never accepted silently — that venue stays as it is.
|
||||
* Resolves to the new base URL, or null.
|
||||
*/
|
||||
export async function upgradeToTls() {
|
||||
if (!canRediscover()) return null
|
||||
const venue = getActiveVenue()
|
||||
if (!venue.baseUrl.startsWith('http://')) return null
|
||||
|
||||
const identity = await probeIdentity(venue.baseUrl)
|
||||
const spki = identity?.tls?.spki_sha256
|
||||
if (!spki || identity.site_id !== venue.siteId) return null
|
||||
if (venue.spki && venue.spki !== spki) return null
|
||||
|
||||
await syncTrustedKeys([spki])
|
||||
const secureUrl = tlsBaseUrl(venue.baseUrl, identity.tls)
|
||||
const secure = await probeIdentity(secureUrl)
|
||||
if (secure?.site_id !== venue.siteId) return null
|
||||
|
||||
saveVenue({ ...venue, baseUrl: secureUrl, spki })
|
||||
return secureUrl
|
||||
}
|
||||
|
||||
@@ -10,7 +10,8 @@
|
||||
*
|
||||
* Contract: docs/reference/lan-access.md
|
||||
*/
|
||||
import { saveVenue } from '../config/server'
|
||||
import { deliveryMode, saveVenue } from '../config/server'
|
||||
import { syncTrustedKeys, tlsBaseUrl } from './tls'
|
||||
|
||||
// api_version values (from /api/system/identity) this build can talk to.
|
||||
export const SUPPORTED_API_VERSIONS = [1]
|
||||
@@ -39,6 +40,7 @@ export function parseServerInput(raw) {
|
||||
return {
|
||||
baseUrl: url.origin,
|
||||
expectedSiteId: url.searchParams.get('pair') || null,
|
||||
expectedKey: url.searchParams.get('k') || null, // TLS public-key pin from the manager's QR
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,7 +89,7 @@ function fallbackSiteId(baseUrl) {
|
||||
* Does NOT switch to it — the caller decides (switchVenue reloads the app).
|
||||
*/
|
||||
export async function pairWith(raw) {
|
||||
const { baseUrl, expectedSiteId } = parseServerInput(raw)
|
||||
const { baseUrl, expectedSiteId, expectedKey } = parseServerInput(raw)
|
||||
const identity = await probeServer(baseUrl)
|
||||
|
||||
if (expectedSiteId && identity.site_id && identity.site_id !== expectedSiteId) {
|
||||
@@ -95,10 +97,33 @@ export async function pairWith(raw) {
|
||||
'Σε αυτή τη διεύθυνση απαντά διαφορετικό κατάστημα από αυτό του QR. Ζητήστε νέο QR από τον διαχειριστή.'
|
||||
)
|
||||
}
|
||||
const spki = identity.tls?.spki_sha256 || null
|
||||
if (expectedKey && spki !== expectedKey) {
|
||||
// The QR carries the real server's key: anything else is a different machine
|
||||
throw new PairingError(
|
||||
'Το κλειδί ασφαλείας του server δεν ταιριάζει με το QR. Ζητήστε νέο QR από τον διαχειριστή.'
|
||||
)
|
||||
}
|
||||
|
||||
return saveVenue({
|
||||
const venue = {
|
||||
siteId: identity.site_id || fallbackSiteId(baseUrl),
|
||||
name: identity.venue_name || new URL(baseUrl).host,
|
||||
baseUrl,
|
||||
})
|
||||
spki,
|
||||
}
|
||||
// Native app + server offers TLS: talk to it encrypted from now on. The pin
|
||||
// comes from the QR (verified above) or, for a typed address, from the server
|
||||
// itself (trust on first use). If :tls.port isn't reachable yet (proxy not
|
||||
// updated), stay on plain HTTP — upgradeToTls() retries on every start.
|
||||
if (spki && identity.site_id && deliveryMode() === 'native') {
|
||||
const secureUrl = tlsBaseUrl(baseUrl, identity.tls)
|
||||
await syncTrustedKeys([spki])
|
||||
try {
|
||||
const secure = await probeServer(secureUrl)
|
||||
if (secure.site_id === identity.site_id) venue.baseUrl = secureUrl
|
||||
} catch {
|
||||
// keep plain HTTP for now
|
||||
}
|
||||
}
|
||||
return saveVenue(venue)
|
||||
}
|
||||
|
||||
@@ -14,6 +14,10 @@
|
||||
import { SUPPORTED_API_VERSIONS } from './pairing'
|
||||
|
||||
const PROBE_TIMEOUT_MS = 1500
|
||||
// The first TLS connection to a venue server in a fresh app process takes ~2s
|
||||
// (WebView certificate check + our pin callback); after that it's milliseconds.
|
||||
// A shorter timeout would abort a perfectly healthy server.
|
||||
const TLS_PROBE_TIMEOUT_MS = 5000
|
||||
const CONCURRENCY = 48
|
||||
|
||||
/** Same-/24 candidate origins for a base URL, nearest addresses first. [] if not an IPv4 URL. */
|
||||
@@ -36,7 +40,8 @@ export function subnetCandidates(baseUrl) {
|
||||
}
|
||||
|
||||
/** Identity of a server, or null (unreachable / not Xenia / unsupported API). */
|
||||
export async function probeIdentity(origin, { timeoutMs = PROBE_TIMEOUT_MS, signal } = {}) {
|
||||
export async function probeIdentity(origin, { timeoutMs, signal } = {}) {
|
||||
timeoutMs ??= origin.startsWith('https:') ? TLS_PROBE_TIMEOUT_MS : PROBE_TIMEOUT_MS
|
||||
const controller = new AbortController()
|
||||
const timer = setTimeout(() => controller.abort(), timeoutMs)
|
||||
const onAbort = () => controller.abort()
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* Encrypted LAN connection for the native app (plan step 7).
|
||||
*
|
||||
* Venue servers serve https://<ip>:<tls.port> with a self-signed certificate.
|
||||
* The native WebView client (MainActivity) accepts it only when the server's
|
||||
* public-key pin is in the trusted set — which this module keeps equal to the
|
||||
* pins of all paired venues. Pins are per key, not per address, so IP changes
|
||||
* and certificate renewals never need anything from the waiter.
|
||||
*/
|
||||
import { registerPlugin } from '@capacitor/core'
|
||||
import { listVenues } from '../config/server'
|
||||
|
||||
const XeniaTls = registerPlugin('XeniaTls')
|
||||
|
||||
/** Push the pins of all paired venues (+ `extra`, e.g. a venue being paired) to native. */
|
||||
export async function syncTrustedKeys(extra = []) {
|
||||
const keys = [...new Set([...listVenues().map(v => v.spki), ...extra].filter(Boolean))]
|
||||
try {
|
||||
await XeniaTls.setTrustedKeys({ keys })
|
||||
} catch (e) {
|
||||
console.warn('[tls] could not update trusted keys', e)
|
||||
}
|
||||
}
|
||||
|
||||
/** https://<same host>:<tls port> for a venue's plain-HTTP base URL. */
|
||||
export function tlsBaseUrl(baseUrl, tls) {
|
||||
return `https://${new URL(baseUrl).hostname}:${tls.port}`
|
||||
}
|
||||
Reference in New Issue
Block a user