Skip to content

feat(websockets): ssl pinning, custom CAs and configurable validation - #21

Merged
edusperoni merged 2 commits into
masterfrom
feat/ssl-pinning
Nov 22, 2026
Merged

edusperoni merged 2 commits into
masterfrom
feat/ssl-pinning

Conversation

@edusperoni

@edusperoni edusperoni commented Apr 29, 2025 •

Copy link
Copy Markdown
Contributor

Adds TLS configuration to @valor/nativescript-websockets on iOS and Android, and builds the iOS xcframework reproducibly.

ssl option

import { WebSocket } from '@valor/nativescript-websockets/websocket';

new WebSocket(url, protocols, {
  ssl: {
    validation?: 'system' | 'pins-only' | 'none', // default 'system'
    pins?: Array<{ type: 'pem'; cert: string } | { type: 'sha256'; hash: string }>,
    ca?: string | string[], // PEM roots that replace the system trust store
  },
});
  • 'system': normal chain, hostname and expiry validation (against ca when given). Pins, if any, must match a certificate of the validated chain.
  • 'pins-only': no chain, hostname or expiry checks; the leaf certificate's public key must match a pin. For self-signed certificates and servers reached by IP.
  • 'none': no validation.

Pins are SHA-256 hashes of the SubjectPublicKeyInfo (OkHttp/HPKP format). Invalid or contradictory options throw from the constructor.

  • iOS: server trust is evaluated with SecTrust before anything is written to the socket. This replaces the old RCTSR_SSLPinnedCertificates check, which disabled chain validation and accepted any presented certificate equal to a pinned one, and removes a validation bypass in DEBUG builds of the framework.
  • Android: 'system' uses the platform trust manager (or one built from ca) and OkHttp's CertificatePinner. 'pins-only' and 'none' decide in the hostname verifier, because an exception thrown from a JS trust-manager callback crashes the app instead of failing the handshake.

Polyfill

import '@valor/nativescript-websockets' now installs the polyfill only when no global WebSocket exists. installWebsocketPolyfill() and the WebSocket class can be imported from @valor/nativescript-websockets/websocket without side effects.

Reproducible xcframework

native-src/ios/build.sh is replaced by xcrepro (xcrepro.config.json). xcrepro verify builds from two different paths to the same hash, and the output contains no machine-specific paths. The Mac Catalyst and visionOS simulator slices now also include x86_64.

Testing

On an iOS 26.3 simulator and an Android 16 emulator, against local wss servers and wss://echo.websocket.org, 23 cases behaved as expected on both platforms, including:

  • a self-signed certificate without subjectAltName on an IP;
  • an attacker certificate with the pinned certificate appended (rejected);
  • a private CA, a wrong hostname, and right/wrong intermediate pins on a public host;
  • all invalid-option errors.

The server received no upgrade request from any rejected connection. Not tested: physical devices, iOS < 15 fallback paths, Mac Catalyst and visionOS at runtime.

@nx-cloud

nx-cloud Bot commented Apr 29, 2025 •

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit 986e2ee

Command Status Duration Result
nx affected --target=build.all ✅ Succeeded 1m 54s View ↗
nx affected:lint ✅ Succeeded 3s View ↗

💡 With Ultracache enabled, Nx Cloud learns each task's file inputs in CI and uses them on later runs. Turn on Ultracache in workspace settings ↗ · Read docs ↗


☁️ Nx Cloud last updated this comment at 2026-11-22 22:05:37 UTC

@edusperoni
edusperoni force-pushed the feat/ssl-pinning branch 2 times, most recently from cbde523 to 2e45384 Compare November 21, 2026 01:33
Adds an `ssl` option to the WebSocket constructor on iOS and Android:

- validation: 'system' (default) | 'pins-only' | 'none'
- pins: public-key (SPKI SHA-256) pins, given as a PEM certificate or a
  base64 hash. With 'system' they must match a certificate of the
  validated chain; with 'pins-only' they must match the leaf, and the
  chain, hostname and expiry are not checked (self-signed / IP servers).
- ca: PEM roots that replace the system trust store.

Invalid or contradictory options throw from the constructor.

iOS evaluates server trust with SecTrust before anything is written to
the socket, replacing the RCTSR_SSLPinnedCertificates check (which
disabled chain validation and accepted any presented certificate equal
to a pinned one) and the DEBUG-only validation bypass.

index.ts now only installs the polyfill when no global WebSocket exists;
installWebsocketPolyfill() and the WebSocket class can be imported from
'@valor/nativescript-websockets/websocket' without side effects.
Replaces native-src/ios/build.sh with `xcrepro build` (configured in
xcrepro.config.json), so the committed xcframework is byte-identical
regardless of who builds it or where, and carries no local paths.

The Mac Catalyst and visionOS simulator slices now also include x86_64.
@edusperoni edusperoni changed the title feat(websockets): add ssl pinning feat(websockets): ssl pinning, custom CAs and configurable validation Nov 22, 2026
@edusperoni
edusperoni marked this pull request as ready for review November 22, 2026 22:07
@edusperoni
edusperoni merged commit 595c524 into master Nov 22, 2026
2 checks passed
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.

1 participant