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:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user