From 0b7fc9ec7d3986ec8a129e984b6136a9ae10f773 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Wed, 29 Jul 2026 10:13:42 +0300 Subject: [PATCH 01/96] Add App Shield attestation API, and fix the broken iOS App Attest linkage Introduces com.codename1.security.shield, the client half of an enterprise attestation and API-shielding service, and fixes several pre-existing defects found while building it. Fixes that stand on their own: - iOS App Attest never linked. IOSNative.java declares `native boolean isAppAttestSupported()` but IOSNative.m defined it without ParparVM's `_R_boolean` mangling in both #ifdef arms, so `ios.appAttest=true` failed at link time. Renamed; the App Attest path now builds and links. - App Attest also re-generated a hardware key on every request and never asserted. Apple's model is attest once, then assert many times against the recorded key; the old flow burned Apple's per-device attestation budget and made device continuity impossible. Rewritten as a keychain-backed state machine with DCError-driven recovery and throttle backoff. - The non-prompting SecureStorage tier was missing on Android and JavaSE, so Secrets.get() re-hit the network on every call on Android. Implemented with a dedicated AndroidKeyStore key (separate from the biometric key, which is invalidated on re-enrolment) and PBKDF2/AES-GCM in the simulator. - CN1JailbreakDetector's fork() probe exited the child rather than the app, so it never did what its comment claimed. Removed, and the detector split into a signal source plus a thin exit wrapper -- iOS getCompromiseReasons() was previously only a canExecute("cydia://") probe that returns false on modern iOS unless the app also declares the scheme. New API: - AppShield plus an SPI seam (ShieldEngine/EngineContext/ShieldEngineRegistry). Discovery is a registered singleton rather than Class.forName, matching the convention documented in SystemBrowser: class names are obfuscated, so name-based lookup is unreliable by construction. The in-tree UnprotectedEngine is the open-source default and fails open in every direction, so an app written against this API compiles and runs unchanged without an engine. - NetworkGuard, a narrow interception seam in com.codename1.io, wired into ConnectionRequest for header attachment and certificate vetting. The per-request checkSSLCertificates hook still runs first and unchanged. - SSLCertificate gains public-key digests. The existing fingerprints are whole-certificate digests, which change on every renewal even on the same key -- pinning those is how pinning earns its reputation for causing outages. The richer form is opt-in, so existing callers see byte-identical data. - WebSocket.header(), emitted by the ports that build the handshake themselves and documented as unavailable on iOS and in the browser rather than silently dropped. Simulator: JavaSEPort had no DeviceIntegrity overrides at all, so the branches an app takes on a compromised device were unreachable off real hardware. Adds Simulate > App Shield covering attestation outcomes, device signals, token expiry and forced pin mismatch. Verified: core suite (4309 tests) green; Android, JavaSE, iOS, Windows and Linux ports compile; the generated iOS project builds and links both with and without ios.appAttest. The iOS DER walk for public-key digests was checked against openssl output for EC and RSA chains before being written in Objective-C. Co-Authored-By: Claude Opus 5 (1M context) --- .../impl/CodenameOneImplementation.java | 45 ++ .../src/com/codename1/impl/WebSocketImpl.java | 58 +++ .../com/codename1/io/ConnectionRequest.java | 182 +++++++- .../src/com/codename1/io/NetworkGuard.java | 78 ++++ .../src/com/codename1/io/NetworkManager.java | 46 ++ .../src/com/codename1/io/WebSocket.java | 34 ++ .../codename1/security/DeviceIntegrity.java | 17 + .../com/codename1/security/SecureStorage.java | 7 +- .../codename1/security/shield/AppShield.java | 434 ++++++++++++++++++ .../security/shield/FailureMode.java | 44 ++ .../codename1/security/shield/HostPolicy.java | 81 ++++ .../com/codename1/security/shield/PinSet.java | 147 ++++++ .../security/shield/ShieldConfig.java | 165 +++++++ .../security/shield/ShieldException.java | 47 ++ .../security/shield/ShieldListener.java | 43 ++ .../security/shield/ShieldSignal.java | 85 ++++ .../security/shield/ShieldSignals.java | 148 ++++++ .../security/shield/ShieldStatus.java | 133 ++++++ .../security/shield/ShieldToken.java | 109 +++++ .../security/shield/package-info.java | 50 ++ .../shield/spi/DefaultEngineContext.java | 111 +++++ .../security/shield/spi/EngineContext.java | 76 +++ .../security/shield/spi/ShieldEngine.java | 112 +++++ .../shield/spi/ShieldEngineRegistry.java | 100 ++++ .../shield/spi/UnprotectedEngine.java | 139 ++++++ .../security/shield/spi/package-info.java | 33 ++ CodenameOne/src/com/codename1/ui/Display.java | 13 + .../impl/android/AndroidImplementation.java | 381 +++++++++------ .../impl/android/AndroidSecureStorage.java | 190 ++++++++ .../impl/android/AndroidWebSocketImpl.java | 1 + .../com/codename1/impl/javase/JavaSEPort.java | 279 ++++++++++- .../impl/javase/JavaSESecureStorage.java | Bin 4001 -> 8027 bytes .../codename1/impl/javase/JavaSEShield.java | 167 +++++++ .../impl/javase/JavaSEWebSocketImpl.java | 1 + .../impl/linux/LinuxWebSocketImpl.java | 1 + .../impl/windows/WindowsWebSocketImpl.java | 1 + .../nativeSources/CN1JailbreakDetector.h | 20 +- .../nativeSources/CN1JailbreakDetector.m | 99 ++-- Ports/iOSPort/nativeSources/IOSNative.m | 147 ++++-- .../nativeSources/NetworkConnectionImpl.m | 114 ++++- .../impl/ios/IOSDeviceIntegrity.java | 354 ++++++++++++-- .../codename1/impl/ios/IOSImplementation.java | 90 +++- .../src/com/codename1/impl/ios/IOSNative.java | 39 +- .../io/SSLCertificateChainParsingTest.java | 145 ++++++ .../security/shield/ShieldApiTest.java | 282 ++++++++++++ 45 files changed, 4585 insertions(+), 263 deletions(-) create mode 100644 CodenameOne/src/com/codename1/io/NetworkGuard.java create mode 100644 CodenameOne/src/com/codename1/security/shield/AppShield.java create mode 100644 CodenameOne/src/com/codename1/security/shield/FailureMode.java create mode 100644 CodenameOne/src/com/codename1/security/shield/HostPolicy.java create mode 100644 CodenameOne/src/com/codename1/security/shield/PinSet.java create mode 100644 CodenameOne/src/com/codename1/security/shield/ShieldConfig.java create mode 100644 CodenameOne/src/com/codename1/security/shield/ShieldException.java create mode 100644 CodenameOne/src/com/codename1/security/shield/ShieldListener.java create mode 100644 CodenameOne/src/com/codename1/security/shield/ShieldSignal.java create mode 100644 CodenameOne/src/com/codename1/security/shield/ShieldSignals.java create mode 100644 CodenameOne/src/com/codename1/security/shield/ShieldStatus.java create mode 100644 CodenameOne/src/com/codename1/security/shield/ShieldToken.java create mode 100644 CodenameOne/src/com/codename1/security/shield/package-info.java create mode 100644 CodenameOne/src/com/codename1/security/shield/spi/DefaultEngineContext.java create mode 100644 CodenameOne/src/com/codename1/security/shield/spi/EngineContext.java create mode 100644 CodenameOne/src/com/codename1/security/shield/spi/ShieldEngine.java create mode 100644 CodenameOne/src/com/codename1/security/shield/spi/ShieldEngineRegistry.java create mode 100644 CodenameOne/src/com/codename1/security/shield/spi/UnprotectedEngine.java create mode 100644 CodenameOne/src/com/codename1/security/shield/spi/package-info.java create mode 100644 Ports/JavaSE/src/com/codename1/impl/javase/JavaSEShield.java create mode 100644 maven/core-unittests/src/test/java/com/codename1/io/SSLCertificateChainParsingTest.java create mode 100644 maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java diff --git a/CodenameOne/src/com/codename1/impl/CodenameOneImplementation.java b/CodenameOne/src/com/codename1/impl/CodenameOneImplementation.java index 362b8b74b4e..d2cb55765fb 100644 --- a/CodenameOne/src/com/codename1/impl/CodenameOneImplementation.java +++ b/CodenameOne/src/com/codename1/impl/CodenameOneImplementation.java @@ -6363,6 +6363,28 @@ public boolean canGetSSLCertificates() { return false; } + /// True when this port can report a digest of each certificate's subject public key info, + /// enabling public-key pinning through [#getSSLCertificatesEx(Object, String)]. + /// + /// Public-key pins survive certificate renewal on the same key pair; whole-certificate + /// fingerprints do not, which is why a renewal can otherwise take a pinning app offline. + public boolean canGetPublicKeyDigests() { + return false; + } + + /// The richer certificate list, grouped per certificate. + /// + /// Same `algorithm:value` encoding as [#getSSLCertificates(Object, String)], with two + /// additions: a `CHAIN:` entry starts each certificate's group (0 is the leaf), and a + /// `SPKI-SHA-256:` entry carries the public-key digest. + /// + /// Called only when something asked for public-key digests, so a port that does not override + /// this simply never sees it. The default delegates to the flat form, which parses correctly + /// and just yields no digests. + public String[] getSSLCertificatesEx(Object connection, String url) throws IOException { + return getSSLCertificates(connection, url); + } + /// SSL certificate checks must be performed via a callback from the native side, /// rather than explicitly checking as part of NetworkManager's connection /// flow. This is mainly for iOS POST requests. If we try to get the SSL certs @@ -11239,6 +11261,29 @@ public String[] getEnabledAccessibilityServices() { return new String[0]; } + /// Discards cached platform attestation state so the next [#requestIntegrityToken(String)] starts + /// from a fresh hardware key. + /// + /// Apple's App Attest model is attest once, then assert many times against the key the server + /// recorded. When the server no longer recognises that key -- the app was reinstalled, the device + /// was restored from a backup, or the key was invalidated by the OS -- the client has to throw the + /// key away and attest again. This is how the attestation layer is told to do that. No-op where + /// attestation is unsupported or stateless (Play Integrity holds no client-side key). + public void resetAttestation() { + } + + /// Returns digests of the certificates the running application is actually signed with, so a build + /// can be compared against the identity it was built under and repackaging can be reported. + /// + /// Deliberately not surfaced on [com.codename1.security.DeviceIntegrity]: an app has no use for its + /// own signature, and a comparison performed on the device is defeated by the same patch that did + /// the repackaging. The value of this is that it is reported to a verifying service, which checks it + /// against what the build server recorded. Returns an empty array where the platform has no such + /// concept. + public String[] getAppSignerDigests() { + return new String[0]; + } + /// Marks the current screen as secure, blocking OS screenshots, screen recording and accessibility /// screen scraping while it is displayed (Android `FLAG_SECURE`). No-op where unsupported. /// diff --git a/CodenameOne/src/com/codename1/impl/WebSocketImpl.java b/CodenameOne/src/com/codename1/impl/WebSocketImpl.java index c4c99892583..2c2267b2d7f 100644 --- a/CodenameOne/src/com/codename1/impl/WebSocketImpl.java +++ b/CodenameOne/src/com/codename1/impl/WebSocketImpl.java @@ -36,6 +36,7 @@ public abstract class WebSocketImpl { private final String url; private WebSocketEventSink sink; private String[] requestedSubprotocols; + private final java.util.Hashtable requestHeaders = new java.util.Hashtable(); // Written by the port's connect/handshake thread, read by the user's // connect handler -- volatile to publish the value across threads. @SuppressWarnings("PMD.AvoidUsingVolatile") @@ -71,6 +72,63 @@ protected final String[] requestedSubprotocols() { return requestedSubprotocols; } + /// Adds a header to the opening handshake. Called by the public facade + /// before `connect(int)`. A null or empty name is ignored; a null value + /// removes a previously set header. + /// + /// Ports that build the handshake themselves emit these; ports built on a + /// platform WebSocket that does not expose the handshake ignore them. See + /// the facade's `header` method for which those are. + public final void setRequestHeader(String name, String value) { + if (name == null || name.length() == 0) { + return; + } + if (value == null) { + requestHeaders.remove(name); + } else { + requestHeaders.put(name, value); + } + } + + /// The extra handshake headers, keyed by name. Never null; ports read this + /// while building the handshake. Ports must not emit an entry whose name + /// collides with a header the handshake sets itself. + protected final java.util.Hashtable requestHeaders() { + return requestHeaders; + } + + /// Appends the extra handshake headers in `name: value` CRLF form, skipping + /// any that would collide with a header the handshake already wrote. + /// + /// Shared here rather than copied per port so the collision list and the + /// header-injection guard stay in one place: a header value carrying CR or + /// LF would otherwise let a caller inject arbitrary handshake headers. + protected final void appendRequestHeaders(StringBuilder req) { + java.util.Enumeration keys = requestHeaders.keys(); + while (keys.hasMoreElements()) { + String name = (String) keys.nextElement(); + String value = (String) requestHeaders.get(name); + if (isReservedHandshakeHeader(name)) { + continue; + } + if (containsCrLf(name) || containsCrLf(value)) { + continue; + } + req.append(name).append(": ").append(value).append("\r\n"); + } + } + + private static boolean isReservedHandshakeHeader(String name) { + String n = name.toLowerCase(); + return n.equals("host") || n.equals("upgrade") || n.equals("connection") + || n.equals("sec-websocket-key") || n.equals("sec-websocket-version") + || n.equals("sec-websocket-protocol") || n.equals("content-length"); + } + + private static boolean containsCrLf(String s) { + return s.indexOf('\r') >= 0 || s.indexOf('\n') >= 0; + } + /// Records the subprotocol the server selected. Ports call this once /// the handshake completes, before firing `sink().onConnect()`, so the /// value is visible to the user's connect handler. diff --git a/CodenameOne/src/com/codename1/io/ConnectionRequest.java b/CodenameOne/src/com/codename1/io/ConnectionRequest.java index f310829981b..5559f46c1bd 100644 --- a/CodenameOne/src/com/codename1/io/ConnectionRequest.java +++ b/CodenameOne/src/com/codename1/io/ConnectionRequest.java @@ -191,6 +191,16 @@ public class ConnectionRequest implements IOProgressListener { private String destinationStorage; private SSLCertificate[] sslCertificates; private boolean checkSSLCertificates; + + /// Set when a [NetworkGuard] rejected this request's certificate chain, so the failure can be + /// rethrown as itself instead of surfacing as a generic connection error (or, worse, as a + /// successful empty response). + private IOException pinFailure; + + /// Whether to ask the platform for the richer certificate details that include public-key + /// digests. Off unless a guard says this host is pinned, so every existing caller keeps + /// receiving byte-identical data from [#getSSLCertificates()]. + private boolean collectPublicKeyDigests; /// A flag that turns off checking for invalid certificates. private boolean insecure; /// The request body can be used instead of arguments to pass JSON data to a restful request, @@ -881,21 +891,54 @@ boolean checkCertificatesNativeCallback() { //throw new RuntimeException("checkCertificates() can only be explicitly called on platforms that require native callbacks for checking certificates."); return true; } - if (!checkSSLCertificates) { + if (!shouldInspectCertificates()) { // If the request doesn't require checking SSL certificates, then this returns true. // meaning that it checks out OK. return true; } try { - checkSSLCertificates(getSSLCertificates()); + SSLCertificate[] certs = getSSLCertificates(); + checkSSLCertificates(certs); + NetworkGuard guard = NetworkManager.getNetworkGuard(); + if (guard != null) { + guard.checkCertificates(this, certs); + } return !shouldStop(); } catch (IOException ex) { + // Retained so the failure surfaces as a real error rather than an + // empty successful response. This callback can only answer with a + // boolean; performOperationComplete rethrows it. + pinFailure = ex; Log.e(ex); return false; } } + /// True when the certificate chain for this request should be fetched and vetted, either + /// because the request opted in itself or because the installed [NetworkGuard] pins this host. + private boolean shouldInspectCertificates() { + if (checkSSLCertificates) { + return true; + } + NetworkGuard guard = NetworkManager.getNetworkGuard(); + if (guard == null) { + return false; + } + try { + if (guard.isCertificateCheckRequired(url)) { + // Ask the platform for the richer chain details (public key + // digests, per-certificate grouping). Off by default so every + // existing caller keeps seeing byte-identical data. + collectPublicKeyDigests = true; + return true; + } + } catch (Throwable t) { + Log.e(t); + } + return false; + } + /// Performs the actual network request on behalf of the network manager void performOperation() throws IOException { performOperationComplete(); @@ -910,6 +953,16 @@ boolean performOperationComplete() throws IOException { if (shouldStop()) { return true; } + pinFailure = null; + NetworkGuard requestGuard = NetworkManager.getNetworkGuard(); + if (requestGuard != null) { + // Deliberately here rather than in initConnection(): that method is + // protected, and a subclass that overrides it without calling super + // would silently lose the decoration. Also deliberately not in + // NetworkThread.prepare(), which runs inside the queue lock where a + // blocking token fetch would stall every other request. + requestGuard.beforeRequest(this); + } if (cacheMode == CachingMode.OFFLINE || cacheMode == CachingMode.OFFLINE_FIRST) { InputStream is = null; //NOPMD CloseResource try { @@ -980,17 +1033,31 @@ boolean performOperationComplete() throws IOException { } } } - if (checkSSLCertificates && canGetSSLCertificates() && + if (shouldInspectCertificates() && canGetSSLCertificates() && // For iOS only... it needs to use a callback from native code // for checking the SSL certificates - otherwise it will send // empty POST bodies. !Util.getImplementation().checkSSLCertificatesRequiresCallbackFromNative()) { sslCertificates = getSSLCertificatesImpl(connection, url); + // The request's own hook runs first and unchanged, so an app that + // already pins by overriding it keeps working; the guard layers on. checkSSLCertificates(sslCertificates); + NetworkGuard certGuard = NetworkManager.getNetworkGuard(); + if (certGuard != null) { + certGuard.checkCertificates(this, sslCertificates); + } if (shouldStop()) { return true; } } + if (pinFailure != null) { + // Raised by the iOS native callback, which can only answer with a + // boolean. Surfacing it here is what stops a rejected chain from + // looking like a successful zero-byte response to the caller. + IOException toThrow = pinFailure; + pinFailure = null; + throw toThrow; + } if (isWriteRequest()) { progress = NetworkEvent.PROGRESS_TYPE_OUTPUT; output = impl.openOutputStream(connection); @@ -1139,6 +1206,17 @@ boolean performOperationComplete() throws IOException { } } } + } catch (IOException ioe) { + // On iOS the certificate check runs in a native callback that can only + // answer with a boolean, so the connection fails with a generic error + // some way after the real cause. Substitute the recorded cause, or the + // caller sees "connection reset" for what was actually a pin mismatch. + if (pinFailure != null) { + IOException cause = pinFailure; + pinFailure = null; + throw cause; + } + throw ioe; } finally { // always cleanup connections/streams even in case of an exception impl.cleanup(output); @@ -1550,8 +1628,61 @@ public SSLCertificate[] getSSLCertificates() throws IOException { return sslCertificates; } + /// Parses the richer per-certificate form of the platform's certificate list. + /// + /// Entries arrive as `algorithm:value`, exactly as in the flat form, with a `CHAIN:` + /// delimiter starting each certificate's group. Anything before the first delimiter is treated + /// as the leaf, so a port that reports digests without grouping still yields usable data. + static SSLCertificate[] parseGroupedCertificates(String[] entries) { + java.util.Vector out = new java.util.Vector(); + SSLCertificate current = null; + int index = 0; + for (int i = 0; i < entries.length; i++) { + String entry = entries[i]; + if (entry == null) { + continue; + } + int splitPos = entry.indexOf(':'); + if (splitPos == -1) { + continue; + } + String algorithm = entry.substring(0, splitPos); + String value = entry.substring(splitPos + 1); + if ("CHAIN".equals(algorithm)) { + current = new SSLCertificate(); + try { + current.chainIndex = Integer.parseInt(value); + } catch (NumberFormatException nfe) { + current.chainIndex = index; + } + index++; + out.addElement(current); + continue; + } + if (current == null) { + current = new SSLCertificate(); + current.chainIndex = index++; + out.addElement(current); + } + if ("SPKI-SHA-256".equals(algorithm)) { + current.publicKeyDigest = value; + current.publicKeyDigestAlgorithm = "SHA-256"; + } else if (current.certificateUniqueKey == null) { + current.certificateAlgorithm = algorithm; + current.certificateUniqueKey = value; + } + } + SSLCertificate[] arr = new SSLCertificate[out.size()]; + out.copyInto(arr); + return arr; + } + private SSLCertificate[] getSSLCertificatesImpl(Object connection, String url) throws IOException { - String[] sslCerts = Util.getImplementation().getSSLCertificates(connection, url); + CodenameOneImplementation impl = Util.getImplementation(); + if (collectPublicKeyDigests && impl.canGetPublicKeyDigests()) { + return parseGroupedCertificates(impl.getSSLCertificatesEx(connection, url)); + } + String[] sslCerts = impl.getSSLCertificates(connection, url); SSLCertificate[] out = new SSLCertificate[sslCerts.length]; int i = 0; for (String sslCertStr : sslCerts) { @@ -3032,6 +3163,9 @@ public static final class SSLCertificate { private String certificateUniqueKey; private String certificateAlgorithm; + private String publicKeyDigest; + private String publicKeyDigestAlgorithm; + private int chainIndex; /// Gets a fingerprint for the SSL certificate encoded using the algorithm /// specified by `#getCertificteAlgorithm()` @@ -3047,6 +3181,46 @@ public String getCertificteUniqueKey() { public String getCertificteAlgorithm() { return certificateAlgorithm; } + + /// Same value as [#getCertificteUniqueKey()], under a spelling that is not a typo. + /// The original name is kept because existing code calls it. + public String getFingerprint() { + return certificateUniqueKey; + } + + /// Same value as [#getCertificteAlgorithm()], under a spelling that is not a typo. + public String getFingerprintAlgorithm() { + return certificateAlgorithm; + } + + /// A base64 digest of this certificate's subject public key info, or null when the + /// platform did not supply one. + /// + /// Prefer this over [#getFingerprint()] when pinning. A whole-certificate fingerprint + /// changes every time the certificate is renewed, even on the same key pair, so pinning it + /// means an expiry can take the app offline. The public key survives renewal. + /// + /// Populated only when something asked for it -- an installed [NetworkGuard] that pins + /// this host. Otherwise it stays null so existing certificate handling is unaffected. + public String getPublicKeyDigest() { + return publicKeyDigest; + } + + /// The digest algorithm behind [#getPublicKeyDigest()], normally `SHA-256`. + public String getPublicKeyDigestAlgorithm() { + return publicKeyDigestAlgorithm; + } + + /// Position in the chain the server presented; 0 is the leaf. Meaningful only when the + /// platform reported per-certificate grouping, otherwise 0. + public int getChainIndex() { + return chainIndex; + } + + /// True for the server's own certificate as opposed to an issuer in the chain. + public boolean isLeaf() { + return chainIndex == 0; + } } private static class ImageStorageSuccessCallback implements SuccessCallback { diff --git a/CodenameOne/src/com/codename1/io/NetworkGuard.java b/CodenameOne/src/com/codename1/io/NetworkGuard.java new file mode 100644 index 00000000000..6150c006875 --- /dev/null +++ b/CodenameOne/src/com/codename1/io/NetworkGuard.java @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ +package com.codename1.io; + +import java.io.IOException; + +/// Interception point for cross-cutting network policy: decorating outgoing requests, and vetting +/// the TLS certificate chain before a request body is written. +/// +/// At most one guard is installed per app, via +/// [NetworkManager#setNetworkGuard(NetworkGuard)], and the slot seals after the first call. +/// [com.codename1.security.shield.AppShield] is the intended consumer, but the interface is +/// deliberately generic and carries no dependency on it. +/// +/// #### This does not replace the per-request certificate hook +/// +/// [ConnectionRequest#checkSSLCertificates(ConnectionRequest.SSLCertificate[])] remains the way an +/// individual request pins for itself, and it still runs first and unchanged. The guard runs +/// afterwards, so an app that already pins manually keeps working and simply gains a second, +/// app-wide layer. +public interface NetworkGuard { + + /// Called on the network thread immediately before the connection is opened, and again on each + /// retry or redirect so a stale token is never reused. + /// + /// May block -- it is off the EDT and outside the network queue's lock. Add headers here. + /// Throwing fails the request through the normal error path. + void beforeRequest(ConnectionRequest request) throws IOException; + + /// Whether this URL's certificate chain needs to be inspected, i.e. whether the host has pins. + /// + /// Must be fast and purely local. On iOS this is consulted from the TLS delegate thread while + /// the handshake is held open. + /// + /// Returning true has a cost beyond the check itself: the framework asks the platform for the + /// richer certificate details, including public-key digests, which it does not otherwise + /// collect. Return false for hosts with no pins. + boolean isCertificateCheckRequired(String url); + + /// Vets the certificate chain, after the handshake and before any request body is written. + /// + /// Throwing an `IOException` aborts the request and surfaces through the request's normal + /// error handling, rather than completing it with an empty response. + /// + /// Must be local and non-blocking, for the same delegate-thread reason as + /// [#isCertificateCheckRequired(String)]. In particular it must not try to fetch a fresh pin + /// set: use the last known one and let a mismatch stand. + void checkCertificates(ConnectionRequest request, + ConnectionRequest.SSLCertificate[] certificates) throws IOException; + + /// Called once the response code is known, including for failed requests. + /// + /// This is how a token layer learns its token was refused -- a 401 or 403 from a protected host + /// usually means the cached token should be discarded before the next attempt. Must not throw; + /// exceptions here are logged and swallowed so a telemetry problem cannot fail a request that + /// otherwise succeeded. + void afterResponse(ConnectionRequest request, int responseCode); +} diff --git a/CodenameOne/src/com/codename1/io/NetworkManager.java b/CodenameOne/src/com/codename1/io/NetworkManager.java index 92e750854b4..5e400072bd5 100644 --- a/CodenameOne/src/com/codename1/io/NetworkManager.java +++ b/CodenameOne/src/com/codename1/io/NetworkManager.java @@ -207,6 +207,42 @@ public static NetworkManager getInstance() { return INSTANCE; } + private static volatile NetworkGuard networkGuard; + private static boolean networkGuardSealed; + + /// Installs the app-wide [NetworkGuard]. + /// + /// The first call wins and the slot then seals. A guard can veto requests and enforce + /// certificate pins, so allowing it to be replaced at runtime would let any code that runs + /// later -- including code an attacker injected -- swap in a permissive one. + /// + /// @throws IllegalStateException if a guard is already installed + public static void setNetworkGuard(NetworkGuard guard) { + if (guard == null) { + throw new IllegalArgumentException("guard is null"); + } + synchronized (NetworkManager.class) { + if (networkGuardSealed) { + throw new IllegalStateException("A network guard is already installed"); + } + networkGuard = guard; + networkGuardSealed = true; + } + } + + /// The installed guard, or null when none was installed. + public static NetworkGuard getNetworkGuard() { + return networkGuard; + } + + /// Test hook: drops the installed guard and unseals the slot. + static void resetNetworkGuardForTesting() { + synchronized (NetworkManager.class) { + networkGuard = null; + networkGuardSealed = false; + } + } + void resetAPN() { autoDetected = false; } @@ -1132,6 +1168,16 @@ private boolean runCurrentRequest(@Async.Execute ConnectionRequest req) { if (requestWasCompleted) { req.complete = true; } + NetworkGuard guard = networkGuard; + if (guard != null) { + try { + guard.afterResponse(req, req.getResponseCode()); + } catch (Throwable t) { + // A guard's bookkeeping must never turn a completed + // request into a failed one. + Log.e(t); + } + } // Read once into a local. A listener removed from the EDT -- // which is where postResponse() runs, queued while this thread // was still finishing the request -- can null the field between diff --git a/CodenameOne/src/com/codename1/io/WebSocket.java b/CodenameOne/src/com/codename1/io/WebSocket.java index 9ba59014f65..e01a5d5e69b 100644 --- a/CodenameOne/src/com/codename1/io/WebSocket.java +++ b/CodenameOne/src/com/codename1/io/WebSocket.java @@ -237,6 +237,40 @@ public WebSocket subprotocols(String... protocols) { return this; } + /// Add a header to the opening handshake. Must be called before [connect]. + /// Passing a null value removes a previously set header. Returns `this` for + /// chaining. + /// + /// Typically used to carry an authorization or attestation token, since a + /// WebSocket has no other place to put one. + /// + /// ``` + /// WebSocket.build("wss://api.example.com/stream") + /// .header("X-CN1-Attest", token) + /// .connect(); + /// ``` + /// + /// #### Not supported everywhere + /// + /// Emitted on Android, desktop, Windows and Linux, which build the opening + /// handshake themselves. **Silently dropped on iOS and in the browser**, + /// which hand the handshake to a platform WebSocket that exposes no way to + /// add headers to it. + /// + /// Where headers are unavailable, obtain a short-lived ticket over an + /// ordinary HTTPS request -- which can be attested and pinned normally -- + /// and pass it in the URL query instead. That also avoids leaking a + /// long-lived credential into a URL. + /// + /// Headers the handshake sets itself -- `Host`, `Upgrade`, `Connection`, + /// `Sec-WebSocket-Key`, `Sec-WebSocket-Version`, `Sec-WebSocket-Protocol` + /// -- are reserved and are ignored if passed here. Use [subprotocols] for + /// the last of those. + public WebSocket header(String name, String value) { + impl.setRequestHeader(name, value); + return this; + } + /// The subprotocol the server selected during the handshake, or null /// when none was negotiated. Valid once the [ConnectHandler] has fired. public String getSelectedSubprotocol() { diff --git a/CodenameOne/src/com/codename1/security/DeviceIntegrity.java b/CodenameOne/src/com/codename1/security/DeviceIntegrity.java index 801c70155a2..371372902d5 100644 --- a/CodenameOne/src/com/codename1/security/DeviceIntegrity.java +++ b/CodenameOne/src/com/codename1/security/DeviceIntegrity.java @@ -110,6 +110,23 @@ public static boolean isAttestationSupported() { return Display.getInstance().isAttestationSupported(); } + /// Discards the cached platform attestation state, so the next [#requestIntegrityToken(String)] + /// attests from a fresh hardware key. + /// + /// Only iOS holds client-side attestation state. Apple's model is: generate a hardware key once, + /// attest it once, then produce cheap assertions against it for every subsequent request. Your + /// backend records the key when it accepts the attestation. If the backend later rejects a request + /// because it does not recognise the key -- the app was reinstalled, the device was restored from a + /// backup, or the OS invalidated the key -- call this, then request a token again. That is the only + /// correct recovery; retrying with the same key will keep failing. + /// + /// Do not call this on every failure. Attestation is rate limited by Apple, and re-attesting in a + /// loop will get the app throttled. No-op on Android, where Play Integrity keeps no client key, and + /// where attestation is unsupported. + public static void resetAttestation() { + Display.getInstance().resetAttestation(); + } + /// Non-exiting RASP check. Returns true when the device shows signs of being rooted, jailbroken, /// running under dynamic instrumentation (e.g. Frida) or otherwise tampered. Unlike the /// `android.rootCheck` / `ios.detectJailbreak` launch gates this never terminates the app, so it is diff --git a/CodenameOne/src/com/codename1/security/SecureStorage.java b/CodenameOne/src/com/codename1/security/SecureStorage.java index 91cc9416d86..b3eca7d6024 100644 --- a/CodenameOne/src/com/codename1/security/SecureStorage.java +++ b/CodenameOne/src/com/codename1/security/SecureStorage.java @@ -144,9 +144,10 @@ public void setKeychainAccessGroup(String group) { // `SecAccessControl`. Entries survive app updates and OS // reboots; they are extracted only after the user unlocks the // device at least once after each reboot. - // - Android: `EncryptedSharedPreferences` (Tink-backed AES-GCM) - // without `setUserAuthenticationRequired(true)`. No biometric - // prompt. + // - Android: AES-GCM under a dedicated AndroidKeyStore key created + // without `setUserAuthenticationRequired(true)`, persisted to a + // private preferences file. No biometric prompt. Devices below + // API 23 fall back to obfuscated (not encrypted) storage. // - JavaSE simulator: `java.util.prefs.Preferences` encrypted // with an AES key derived from the OS user account. Useful // for round-tripping `LlmClient.openai(SecureStorage.getInstance().get("openai_key"))` diff --git a/CodenameOne/src/com/codename1/security/shield/AppShield.java b/CodenameOne/src/com/codename1/security/shield/AppShield.java new file mode 100644 index 00000000000..9bde5485ff9 --- /dev/null +++ b/CodenameOne/src/com/codename1/security/shield/AppShield.java @@ -0,0 +1,434 @@ +/* + * Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ +package com.codename1.security.shield; + +import com.codename1.io.ConnectionRequest; +import com.codename1.io.Log; +import com.codename1.security.shield.spi.ShieldEngine; +import com.codename1.security.shield.spi.ShieldEngineRegistry; +import com.codename1.ui.Display; +import com.codename1.util.AsyncResource; +import java.util.Hashtable; +import java.util.Vector; + +/// API shielding: proves to your own backend that a request came from a genuine, unmodified build +/// of your app running on a device that has not been tampered with. +/// +/// #### How it differs from [com.codename1.security.DeviceIntegrity] +/// +/// `DeviceIntegrity` is the raw platform primitive -- it hands you a Play Integrity or App Attest +/// blob and leaves verification, policy and enforcement to you. `AppShield` is the managed layer +/// on top: the attestation is verified server side against Apple and Google, evaluated against a +/// policy you control, and turned into a short-lived signed token your backend can check with a +/// few lines of middleware. Both remain available and `DeviceIntegrity` keeps working unchanged; +/// an app that only needs the raw blob should keep using it. +/// +/// #### The shape of the thing +/// +/// ```java +/// AppShield.init(new ShieldConfig() +/// .protect("api.mybank.example", HostPolicy.PROTECTED)); +/// +/// // ...then just make requests. Protected hosts get the header and the pin check. +/// ConnectionRequest r = new ConnectionRequest("https://api.mybank.example/transfer", true); +/// NetworkManager.getInstance().addToQueueAndWait(r); +/// ``` +/// +/// Your backend rejects any request whose token is missing, expired or unsigned. That check is +/// where the security actually lives -- not in this class. A device the attacker fully controls +/// can always strip a header; what it cannot do is mint a token, because the token is signed by a +/// service the attacker does not control, on the strength of a statement from Apple or Google. +/// +/// #### When the engine is absent +/// +/// Builds without the enterprise attestation engine -- open-source builds, and any project not +/// entitled to it -- get a working, inert implementation. [#isProtected()] returns false, +/// [#fetchToken()] completes with [ShieldStatus#UNPROTECTED] rather than hanging, [#attach] does +/// nothing, and no request is ever blocked. The API is safe to call unconditionally; there is no +/// need to guard call sites. +/// +/// #### Threading +/// +/// [#fetchToken()] is asynchronous. [#attach(ConnectionRequest)] blocks and must not be called on +/// the EDT -- in normal use you never call it yourself, because a protected host is handled +/// automatically on the network thread. +public final class AppShield { + + private static ShieldConfig config; + private static boolean initialized; + private static ShieldStatus lastStatus = ShieldStatus.NOT_INITIALIZED; + private static final Vector listeners = new Vector(); + private static final Hashtable runtimeHosts = new Hashtable(); + + private AppShield() { + } + + // ----------------------------------------------------------------- + // Lifecycle + // ----------------------------------------------------------------- + + /// Initializes the shield. Call once during app startup, after `Display.init`. + /// + /// Safe to call in a build with no attestation engine: it logs one line and leaves the shield + /// inert. Calling it twice is a no-op. + public static void init(ShieldConfig cfg) { + synchronized (AppShield.class) { + if (initialized) { + return; + } + config = cfg == null ? new ShieldConfig() : cfg; + initialized = true; + } + ShieldEngine engine = ShieldEngineRegistry.getEngine(); + try { + engine.initialize(contextForEngine(), config); + setStatus(engine.isAvailable() ? ShieldStatus.OK : ShieldStatus.UNPROTECTED); + } catch (Throwable t) { + // A failure inside the engine must not stop the app from starting. + Log.e(t); + setStatus(ShieldStatus.UNPROTECTED); + } + } + + /// True when a real attestation engine is present and available. False in an open-source or + /// unentitled build, and in the simulator unless simulation is switched on. + public static boolean isProtected() { + return ShieldEngineRegistry.getEngine().isAvailable(); + } + + /// The active engine's name, for diagnostics and support logs. + public static String getEngineName() { + return ShieldEngineRegistry.getEngine().getName(); + } + + /// The configuration passed to [#init(ShieldConfig)], or defaults if it has not been called. + public static ShieldConfig getConfig() { + synchronized (AppShield.class) { + if (config == null) { + config = new ShieldConfig(); + } + return config; + } + } + + // ----------------------------------------------------------------- + // Tokens + // ----------------------------------------------------------------- + + /// Fetches a time-limited token, reusing the cached one when it is still good. + public static AsyncResource fetchToken() { + return fetchToken(null); + } + + /// Fetches a token bound to specific request data. + /// + /// Binding ties the token to one request, so a token lifted off a captured request cannot be + /// replayed against a different one. Worth the extra round trip on the calls that matter -- + /// a transfer, a password change -- and not worth it on the rest, which should use the plain + /// [#fetchToken()]. + /// + /// @param bindingData the data to bind to, typically a digest of the request body + public static AsyncResource fetchToken(final String bindingData) { + final AsyncResource result = new AsyncResource(); + if (!initialized) { + result.error(new ShieldException(ShieldStatus.NOT_INITIALIZED, + "AppShield.init(...) has not been called")); + return result; + } + Display.getInstance().scheduleBackgroundTask(new Runnable() { + public void run() { + try { + ShieldToken token = ShieldEngineRegistry.getEngine().fetchToken(bindingData); + setStatus(token.getStatus()); + result.complete(token); + } catch (ShieldException e) { + setStatus(e.getStatus()); + result.error(e); + } catch (Throwable t) { + setStatus(ShieldStatus.SERVICE_DOWN); + result.error(t); + } + } + }); + return result; + } + + /// Discards any cached token. Call this when your backend rejects a token, so the next request + /// re-attests rather than replaying the token that was just refused. + public static void invalidateToken() { + try { + ShieldEngineRegistry.getEngine().invalidate(); + } catch (Throwable t) { + Log.e(t); + } + } + + // ----------------------------------------------------------------- + // Request binding + // ----------------------------------------------------------------- + + /// Attaches the attestation header to a request. + /// + /// **Blocks** while a token is fetched, so it must be called on a network thread. Requests to + /// hosts registered via [ShieldConfig#protect(String, HostPolicy)] are handled automatically + /// and do not need this; use it for a request built outside the normal path. + /// + /// Honours the host's [FailureMode]: under [FailureMode#OPEN] a token failure leaves the + /// request untouched, under [FailureMode#CLOSED] it propagates. + public static void attach(ConnectionRequest request) throws ShieldException { + if (request == null || !initialized) { + return; + } + String host = hostOf(request.getUrl()); + HostPolicy policy = policyFor(host); + if (!policy.isAttachToken()) { + return; + } + try { + ShieldToken token = ShieldEngineRegistry.getEngine().fetchToken(null); + setStatus(token.getStatus()); + if (token.isValid()) { + request.addRequestHeader(getConfig().getTokenHeader(), token.getValue()); + return; + } + failOrContinue(policy, new ShieldException(token.getStatus(), + "No valid attestation token for " + host)); + } catch (ShieldException e) { + setStatus(e.getStatus()); + failOrContinue(policy, e); + } + } + + private static void failOrContinue(HostPolicy policy, ShieldException e) throws ShieldException { + if (policy.getFailureMode() == FailureMode.CLOSED) { + throw e; + } + Log.p("AppShield: continuing without a token (" + e.getStatus().getId() + + "); host policy is fail-open."); + } + + /// The headers a protected URL should carry, for network paths that do not go through + /// `ConnectionRequest` -- notably `BrowserComponent.setURL(url, headers)`. + /// + /// Returns an empty table when the host is unprotected or no token is available. Never blocks: + /// it uses the cached token only, because the callers are typically on the EDT. + /// + /// Note this covers only the initial navigation. Requests the loaded page makes itself are not + /// visible to the framework and cannot be given a token or pinned. + public static Hashtable headersFor(String url) { + Hashtable out = new Hashtable(); + if (!initialized || url == null) { + return out; + } + if (!policyFor(hostOf(url)).isAttachToken()) { + return out; + } + ShieldToken token = getCachedToken(); + if (token != null && token.isValid()) { + out.put(getConfig().getTokenHeader(), token.getValue()); + } + return out; + } + + /// The cached token without triggering a fetch. May be null or lapsed. Never blocks. + public static ShieldToken getCachedToken() { + try { + return ShieldEngineRegistry.getEngine().getCachedToken(); + } catch (Throwable t) { + return null; + } + } + + // ----------------------------------------------------------------- + // Host policy + // ----------------------------------------------------------------- + + /// Registers a protected host after [#init(ShieldConfig)], for a backend discovered at + /// runtime. + public static void addProtectedHost(String host, HostPolicy policy) { + if (host != null && host.length() > 0) { + runtimeHosts.put(host.toLowerCase(), + policy == null ? HostPolicy.PROTECTED : policy); + } + } + + /// The policy in force for a host. Returns [HostPolicy#UNPROTECTED] for anything not + /// registered, which is the great majority of hosts an app talks to. + public static HostPolicy policyFor(String host) { + if (host == null) { + return HostPolicy.UNPROTECTED; + } + Object runtime = runtimeHosts.get(host.toLowerCase()); + if (runtime != null) { + return (HostPolicy) runtime; + } + return getConfig().policyFor(host); + } + + // ----------------------------------------------------------------- + // Pinning + // ----------------------------------------------------------------- + + /// The pin set currently in force. Never null; may be [PinSet#EMPTY]. + public static PinSet getPinSet() { + try { + PinSet set = ShieldEngineRegistry.getEngine().getPinSet(); + return set == null ? PinSet.EMPTY : set; + } catch (Throwable t) { + return PinSet.EMPTY; + } + } + + // ----------------------------------------------------------------- + // RASP + // ----------------------------------------------------------------- + + /// The runtime self-protection observations recorded so far, combining what the engine + /// detected with anything the app or a library reported to [ShieldSignals]. + /// + /// Informational. The attestation service applies the policy, and it may reach a different + /// conclusion than a naive reading of this array -- an emulator signal, for instance, is + /// normal on a developer's machine. + public static ShieldSignal[] getSignals() { + ShieldSignal[] fromEngine; + try { + fromEngine = ShieldEngineRegistry.getEngine().collectSignals(); + } catch (Throwable t) { + fromEngine = new ShieldSignal[0]; + } + if (fromEngine != null) { + for (int i = 0; i < fromEngine.length; i++) { + ShieldSignals.add(fromEngine[i]); + } + } + return ShieldSignals.snapshot(); + } + + /// The most recent token status. [ShieldStatus#NOT_INITIALIZED] before [#init(ShieldConfig)]. + public static ShieldStatus getStatus() { + synchronized (AppShield.class) { + return lastStatus; + } + } + + // ----------------------------------------------------------------- + // Observation + // ----------------------------------------------------------------- + + /// Registers a listener for status and signal changes. Callbacks arrive on the EDT. + public static void addListener(ShieldListener l) { + if (l == null) { + return; + } + synchronized (listeners) { + if (!listeners.contains(l)) { + listeners.addElement(l); + } + } + ShieldSignals.addListener(l); + } + + public static void removeListener(ShieldListener l) { + synchronized (listeners) { + listeners.removeElement(l); + } + ShieldSignals.removeListener(l); + } + + // ----------------------------------------------------------------- + // Internals + // ----------------------------------------------------------------- + + private static void setStatus(ShieldStatus status) { + if (status == null) { + return; + } + ShieldListener[] copy; + synchronized (AppShield.class) { + if (status.equals(lastStatus)) { + return; + } + lastStatus = status; + } + synchronized (listeners) { + if (listeners.isEmpty()) { + return; + } + copy = new ShieldListener[listeners.size()]; + listeners.copyInto(copy); + } + Display.getInstance().callSerially(new StatusDispatch(copy, status)); + } + + private static final class StatusDispatch implements Runnable { + private final ShieldListener[] targets; + private final ShieldStatus status; + + StatusDispatch(ShieldListener[] targets, ShieldStatus status) { + this.targets = targets; + this.status = status; + } + + public void run() { + for (int i = 0; i < targets.length; i++) { + targets[i].statusChanged(status); + } + } + } + + /// Extracts the host from a URL without pulling in a URL parser. Returns null when the URL is + /// not absolute, in which case the host is treated as unprotected. + static String hostOf(String url) { + if (url == null) { + return null; + } + int scheme = url.indexOf("://"); + if (scheme < 0) { + return null; + } + int start = scheme + 3; + int end = url.length(); + for (int i = start; i < url.length(); i++) { + char c = url.charAt(i); + if (c == '/' || c == '?' || c == '#') { + end = i; + break; + } + } + String authority = url.substring(start, end); + // Strip userinfo and port. + int at = authority.lastIndexOf('@'); + if (at >= 0) { + authority = authority.substring(at + 1); + } + int colon = authority.lastIndexOf(':'); + if (colon >= 0 && authority.indexOf(']') < colon) { + authority = authority.substring(0, colon); + } + return authority.length() == 0 ? null : authority.toLowerCase(); + } + + private static com.codename1.security.shield.spi.EngineContext contextForEngine() { + return ShieldEngineRegistry.getDefaultContext(); + } +} diff --git a/CodenameOne/src/com/codename1/security/shield/FailureMode.java b/CodenameOne/src/com/codename1/security/shield/FailureMode.java new file mode 100644 index 00000000000..cf688260f26 --- /dev/null +++ b/CodenameOne/src/com/codename1/security/shield/FailureMode.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ +package com.codename1.security.shield; + +/// What a protected host should do when a token cannot be obtained. +/// +/// This is deliberately a per-host decision. An app typically wants [#CLOSED] on the handful of +/// endpoints that move money or read personal data, and [#OPEN] everywhere else, so that a shield +/// service outage degrades one feature rather than bricking the app. +public enum FailureMode { + + /// Send the request without a token. The customer's backend still decides what to do with an + /// unattested request; this only means the client does not block it locally. + /// + /// This is the default, and it is the only behaviour available when the app was built without + /// the enterprise engine. + OPEN, + + /// Refuse to send the request, failing it with a [ShieldException] carrying the reason. Use + /// this only where a false negative is more acceptable than an unattested call, and only after + /// reading [ShieldStatus#isTransient()] -- most token failures are network problems, not + /// compromised devices. + CLOSED +} diff --git a/CodenameOne/src/com/codename1/security/shield/HostPolicy.java b/CodenameOne/src/com/codename1/security/shield/HostPolicy.java new file mode 100644 index 00000000000..f76ddbf5db4 --- /dev/null +++ b/CodenameOne/src/com/codename1/security/shield/HostPolicy.java @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ +package com.codename1.security.shield; + +/// What the shield does for a given host: whether to attach an attestation token, whether to +/// enforce certificate pins, and what to do when a token cannot be obtained. +/// +/// Hosts are opt-in. A host with no policy registered gets [#UNPROTECTED] and is left completely +/// alone -- no header, no pin check, no possibility of a blocked request. That default is what +/// lets an existing app adopt the shield on its own API without disturbing its analytics, CDN, +/// map-tile or ad traffic. +public final class HostPolicy { + + /// The policy for any host the app did not explicitly register. Does nothing at all. + public static final HostPolicy UNPROTECTED = new HostPolicy(false, false, FailureMode.OPEN); + + /// Attach a token, enforce pins if the service has published any, and let the request through + /// when no token is available. The sensible starting point for a protected host. + public static final HostPolicy PROTECTED = new HostPolicy(true, true, FailureMode.OPEN); + + /// As [#PROTECTED] but refuses to send the request without a valid token. Adopt only after + /// running with [#PROTECTED] long enough to know the real token-failure rate for your users. + public static final HostPolicy ENFORCED = new HostPolicy(true, true, FailureMode.CLOSED); + + private final boolean attachToken; + private final boolean enforcePins; + private final FailureMode failureMode; + + public HostPolicy(boolean attachToken, boolean enforcePins, FailureMode failureMode) { + this.attachToken = attachToken; + this.enforcePins = enforcePins; + this.failureMode = failureMode == null ? FailureMode.OPEN : failureMode; + } + + /// True when requests to this host carry the attestation header. + public boolean isAttachToken() { + return attachToken; + } + + /// True when the certificate chain for this host is checked against the published pin set. + /// Note that enforcement still only happens if a pin set for the host actually exists; see + /// [PinSet] for the never-brick rules. + public boolean isEnforcePins() { + return enforcePins; + } + + /// What to do when no token could be obtained. + public FailureMode getFailureMode() { + return failureMode; + } + + /// True when this policy does nothing, so callers can skip work entirely. + public boolean isNoOp() { + return !attachToken && !enforcePins; + } + + public String toString() { + return "HostPolicy[token=" + attachToken + ", pins=" + enforcePins + + ", onFailure=" + failureMode + "]"; + } +} diff --git a/CodenameOne/src/com/codename1/security/shield/PinSet.java b/CodenameOne/src/com/codename1/security/shield/PinSet.java new file mode 100644 index 00000000000..7b417799158 --- /dev/null +++ b/CodenameOne/src/com/codename1/security/shield/PinSet.java @@ -0,0 +1,147 @@ +/* + * Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ +package com.codename1.security.shield; + +import java.util.Hashtable; +import java.util.Vector; + +/// An immutable set of certificate pins, keyed by host, as published by the attestation service. +/// +/// Pins are over the **subject public key info**, not the whole certificate, so a host can renew +/// its certificate on the same key pair without invalidating the pin. A chain matches if *any* +/// certificate in it matches *any* pin for the host, which is what makes it safe to pin an issuing +/// CA as the backup. +/// +/// #### The never-brick rules +/// +/// Client-side pinning is the one part of the shield that can take an app offline for reasons the +/// developer cannot fix without an app store release, so the failure behaviour is deliberately +/// asymmetric: +/// +/// - A host with **no pins** is never enforced. That covers first run, a cold start with no +/// network, and any host the service has not published pins for. +/// - A **failed pin fetch** never fails a request. The last known set is kept. +/// - Pins carry a soft expiry, after which a refresh is attempted, and a much later hard expiry. +/// Past the hard expiry the set is dropped and enforcement stops. A device that cannot reach the +/// service for weeks loses pinning; it does not lose the app. +/// - Only an actual mismatch -- a host that *has* pins presenting a chain that matches none of +/// them -- fails a request, and it fails before any request body is written. +public final class PinSet { + + /// A pin set with no hosts. Enforces nothing. + public static final PinSet EMPTY = new PinSet(new Hashtable(), 0, 0, 0); + + private final Hashtable hostToPins; + private final int version; + private final long softExpiry; + private final long hardExpiry; + + /// @param hostToPins host (lowercase) to a `Vector` of base64 SHA-256 SPKI digests + /// @param version monotonic version from the service, used to detect a newer published set + /// @param softExpiry local millis after which a refresh should be attempted, 0 for never + /// @param hardExpiry local millis after which the set is discarded entirely, 0 for never + public PinSet(Hashtable hostToPins, int version, long softExpiry, long hardExpiry) { + this.hostToPins = hostToPins == null ? new Hashtable() : hostToPins; + this.version = version; + this.softExpiry = softExpiry; + this.hardExpiry = hardExpiry; + } + + public int getVersion() { + return version; + } + + /// True once the set should be refreshed. Does not mean it has stopped being enforced. + public boolean isStale() { + return softExpiry > 0 && System.currentTimeMillis() > softExpiry; + } + + /// True once the set is too old to keep enforcing. At this point pinning disables itself + /// rather than risk locking a long-offline device out of its own app. + public boolean isExpired() { + return hardExpiry > 0 && System.currentTimeMillis() > hardExpiry; + } + + /// True when this set has at least one pin for the host and has not hard-expired, i.e. when a + /// chain for this host is actually going to be checked. + public boolean isEnforcedFor(String host) { + if (host == null || isExpired()) { + return false; + } + Vector pins = pinsFor(host); + return pins != null && !pins.isEmpty(); + } + + /// The pins registered for a host, honouring a leading `*.` wildcard, or null when the host is + /// not pinned. + public Vector pinsFor(String host) { + if (host == null) { + return null; + } + String h = host.toLowerCase(); + Object exact = hostToPins.get(h); + if (exact != null) { + return (Vector) exact; + } + // Walk up the labels so a "*.example.com" entry covers "api.example.com". + int dot = h.indexOf('.'); + while (dot >= 0 && dot < h.length() - 1) { + Object wild = hostToPins.get("*." + h.substring(dot + 1)); + if (wild != null) { + return (Vector) wild; + } + dot = h.indexOf('.', dot + 1); + } + return null; + } + + /// True when at least one of the supplied chain digests matches a pin for the host. + /// + /// Returns true when the host is not pinned at all -- "no opinion" must never be reported as a + /// mismatch, or an unpinned host would start failing. + public boolean matches(String host, String[] chainSpkiDigests) { + if (!isEnforcedFor(host)) { + return true; + } + if (chainSpkiDigests == null || chainSpkiDigests.length == 0) { + return false; + } + Vector pins = pinsFor(host); + for (int i = 0; i < chainSpkiDigests.length; i++) { + if (chainSpkiDigests[i] != null && pins.contains(chainSpkiDigests[i])) { + return true; + } + } + return false; + } + + /// True when no host is pinned. + public boolean isEmpty() { + return hostToPins.isEmpty(); + } + + public String toString() { + return "PinSet[version=" + version + ", hosts=" + hostToPins.size() + + ", stale=" + isStale() + ", expired=" + isExpired() + "]"; + } +} diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java b/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java new file mode 100644 index 00000000000..3060313e787 --- /dev/null +++ b/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java @@ -0,0 +1,165 @@ +/* + * Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ +package com.codename1.security.shield; + +import java.util.Enumeration; +import java.util.Hashtable; + +/// Configuration for [AppShield#init(ShieldConfig)]. Chainable. +/// +/// ```java +/// AppShield.init(new ShieldConfig() +/// .protect("api.mybank.example", HostPolicy.PROTECTED) +/// .protect("*.mybank.example", HostPolicy.PROTECTED)); +/// ``` +/// +/// The defaults are chosen so that adding the shield to an existing app changes nothing until +/// hosts are explicitly protected: no host is touched, failures are open, and signal collection is +/// on because reporting costs nothing and is what makes the service useful. +public final class ShieldConfig { + + /// Header that carries the attestation token. + /// + /// Deliberately not `Authorization`: that slot belongs to the app's own user authentication, + /// and the two answer different questions -- who the user is, versus whether this is a genuine + /// unmodified app on an uncompromised device. A backend needs both, so they must compose. + public static final String DEFAULT_TOKEN_HEADER = "X-CN1-Attest"; + + private static final String DEFAULT_ENDPOINT = "https://cloud.codenameone.com/api/v2/attest"; + + private String endpoint = DEFAULT_ENDPOINT; + private String tokenHeader = DEFAULT_TOKEN_HEADER; + private FailureMode defaultFailureMode = FailureMode.OPEN; + private int refreshThresholdPercent = 50; + private boolean collectSignals = true; + private final Hashtable hostPolicies = new Hashtable(); + + /// Overrides the attestation service endpoint. Only needed for a private deployment or a test + /// double. + public ShieldConfig endpoint(String url) { + if (url != null) { + this.endpoint = url; + } + return this; + } + + /// Overrides the header used to carry the token. Change this only if it collides with + /// something already in use on your backend. + public ShieldConfig tokenHeader(String name) { + if (name != null && name.length() > 0) { + this.tokenHeader = name; + } + return this; + } + + /// The failure mode applied to hosts registered without an explicit one. + public ShieldConfig defaultFailureMode(FailureMode mode) { + if (mode != null) { + this.defaultFailureMode = mode; + } + return this; + } + + /// How far through a token's lifetime to trigger a background refresh, as a percentage. + /// Refreshing early is what stops a request ever having to wait on the network. + public ShieldConfig refreshThresholdPercent(int percent) { + if (percent > 0 && percent < 100) { + this.refreshThresholdPercent = percent; + } + return this; + } + + /// Whether to gather runtime self-protection observations. On by default; they ride along with + /// the token fetch, so there is no extra request and no extra battery cost. + public ShieldConfig collectSignals(boolean collect) { + this.collectSignals = collect; + return this; + } + + /// Registers a host to protect. Accepts an exact host or a leading `*.` wildcard covering its + /// subdomains. Hosts not registered here are never touched. + public ShieldConfig protect(String hostPattern, HostPolicy policy) { + if (hostPattern != null && hostPattern.length() > 0) { + hostPolicies.put(hostPattern.toLowerCase(), + policy == null ? HostPolicy.PROTECTED : policy); + } + return this; + } + + /// Registers a host with [HostPolicy#PROTECTED]. + public ShieldConfig protect(String hostPattern) { + return protect(hostPattern, HostPolicy.PROTECTED); + } + + public String getEndpoint() { + return endpoint; + } + + public String getTokenHeader() { + return tokenHeader; + } + + public FailureMode getDefaultFailureMode() { + return defaultFailureMode; + } + + public int getRefreshThresholdPercent() { + return refreshThresholdPercent; + } + + public boolean isCollectSignals() { + return collectSignals; + } + + /// Resolves a host to its policy: exact match first, then the nearest `*.` wildcard, then + /// [HostPolicy#UNPROTECTED]. Never returns null. + public HostPolicy policyFor(String host) { + if (host == null) { + return HostPolicy.UNPROTECTED; + } + String h = host.toLowerCase(); + Object exact = hostPolicies.get(h); + if (exact != null) { + return (HostPolicy) exact; + } + int dot = h.indexOf('.'); + while (dot >= 0 && dot < h.length() - 1) { + Object wild = hostPolicies.get("*." + h.substring(dot + 1)); + if (wild != null) { + return (HostPolicy) wild; + } + dot = h.indexOf('.', dot + 1); + } + return HostPolicy.UNPROTECTED; + } + + /// True when at least one host is registered, so callers can skip work entirely. + public boolean hasProtectedHosts() { + return !hostPolicies.isEmpty(); + } + + /// The registered host patterns. + public Enumeration protectedHosts() { + return hostPolicies.keys(); + } +} diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldException.java b/CodenameOne/src/com/codename1/security/shield/ShieldException.java new file mode 100644 index 00000000000..a35b7dd3f2a --- /dev/null +++ b/CodenameOne/src/com/codename1/security/shield/ShieldException.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ +package com.codename1.security.shield; + +import java.io.IOException; + +/// Raised when a shield operation cannot produce a usable token, or when a request to a protected +/// host is refused because its certificate chain matched no configured pin. +/// +/// Extends `IOException` so it flows through the normal `ConnectionRequest` error path rather than +/// needing its own handling. Always check [#getStatus()] before deciding what to show the user: +/// [ShieldStatus#isTransient()] distinguishes "could not reach the service" from "this device was +/// rejected", and those deserve very different UX. +public class ShieldException extends IOException { + + private final ShieldStatus status; + + public ShieldException(ShieldStatus status, String message) { + super(message); + this.status = status == null ? ShieldStatus.NOT_INITIALIZED : status; + } + + /// Why the operation failed. Never null. + public ShieldStatus getStatus() { + return status; + } +} diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldListener.java b/CodenameOne/src/com/codename1/security/shield/ShieldListener.java new file mode 100644 index 00000000000..5872b55350a --- /dev/null +++ b/CodenameOne/src/com/codename1/security/shield/ShieldListener.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ +package com.codename1.security.shield; + +/// Callback for shield state changes. Register with [AppShield#addListener(ShieldListener)]. +/// +/// All callbacks are delivered on the EDT, so they may touch the UI directly. The framework itself +/// never shows a dialog or terminates the app over a shield event -- what the user sees is entirely +/// the app's decision, made here. +public interface ShieldListener { + + /// The token status changed, for example from [ShieldStatus#OK] to [ShieldStatus#REJECTED]. + /// + /// Branch on [ShieldStatus#isTransient()] before reacting. A transient status means the + /// service was unreachable and will likely be reachable again shortly; reacting to it the same + /// way as [ShieldStatus#REJECTED] is how an app ends up locking out users on a bad connection. + void statusChanged(ShieldStatus status); + + /// A new runtime self-protection observation was recorded. Informational: the attestation + /// service decides what a signal means for token issuance, and it may reach a different + /// conclusion than the device would. + void signalRaised(ShieldSignal signal); +} diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldSignal.java b/CodenameOne/src/com/codename1/security/shield/ShieldSignal.java new file mode 100644 index 00000000000..13c6c6f4a15 --- /dev/null +++ b/CodenameOne/src/com/codename1/security/shield/ShieldSignal.java @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ +package com.codename1.security.shield; + +/// One runtime self-protection observation, such as "a hooking framework is loaded". +/// +/// Signals are **reports, not verdicts**. The device never decides it is compromised and never +/// terminates itself over one of these; it reports what it saw and the attestation service decides +/// whether to keep issuing tokens. That ordering matters for two reasons: a hard local exit is +/// trivially patched out of the binary, and it destroys the telemetry that would have told the +/// developer an attack was happening at all. +public final class ShieldSignal { + + /// A rooted Android device. + public static final String ROOT = "root"; + /// A jailbroken iOS device. + public static final String JAILBREAK = "jailbreak"; + /// A dynamic instrumentation or hooking framework is present. + public static final String HOOK = "hook"; + /// The app is running on an emulator or simulator. + public static final String EMULATOR = "emulator"; + /// A debugger is attached to the process. + public static final String DEBUGGER = "debugger"; + /// The app's signing certificate does not match the one it was built with. + public static final String REPACKAGED = "repackaged"; + /// An accessibility service that is not on the allow list is enabled. + public static final String ACCESSIBILITY = "accessibility"; + + private final String id; + private final int severity; + private final String detail; + private final long timestamp; + + public ShieldSignal(String id, int severity, String detail) { + this.id = id; + this.severity = severity < 0 ? 0 : (severity > 100 ? 100 : severity); + this.detail = detail; + this.timestamp = System.currentTimeMillis(); + } + + /// A stable identifier such as [#HOOK]. Engines may report ids this build predates. + public String getId() { + return id; + } + + /// How strongly this points at an attack, 0 to 100. Advisory only -- the service applies the + /// policy, so a low severity here does not mean the service will ignore it. + public int getSeverity() { + return severity; + } + + /// What was actually observed, for example the offending package or library name. May be null. + public String getDetail() { + return detail; + } + + /// When the observation was made. + public long getTimestamp() { + return timestamp; + } + + public String toString() { + return id + "(" + severity + (detail == null ? "" : ", " + detail) + ")"; + } +} diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldSignals.java b/CodenameOne/src/com/codename1/security/shield/ShieldSignals.java new file mode 100644 index 00000000000..6a08e395890 --- /dev/null +++ b/CodenameOne/src/com/codename1/security/shield/ShieldSignals.java @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ +package com.codename1.security.shield; + +import com.codename1.ui.Display; +import java.util.Vector; + +/// Collection point for runtime self-protection observations. +/// +/// The framework's own detections report here, and so can a cn1lib or the app itself when it +/// notices something the platform checks cannot see -- a failed server-side consistency check, a +/// suspicious sequence of user actions. Everything recorded here is offered to the attestation +/// service on the next token fetch, where the policy engine decides what it means. +/// +/// The bus is bounded: it keeps only the most recent observations, and repeat reports of the same +/// id collapse onto the existing entry rather than accumulating. A hooking framework that trips a +/// detector on every frame must not be able to exhaust memory. +public final class ShieldSignals { + + private static final int MAX_SIGNALS = 32; + + private static final Vector signals = new Vector(); + private static final Vector listeners = new Vector(); + + private ShieldSignals() { + } + + /// Records an observation. Repeat reports of an id already present update that entry in place. + /// Safe to call from any thread; listeners are notified on the EDT. + public static void add(ShieldSignal signal) { + if (signal == null || signal.getId() == null) { + return; + } + synchronized (signals) { + for (int i = 0; i < signals.size(); i++) { + if (((ShieldSignal) signals.elementAt(i)).getId().equals(signal.getId())) { + signals.setElementAt(signal, i); + notifyListeners(signal); + return; + } + } + if (signals.size() >= MAX_SIGNALS) { + signals.removeElementAt(0); + } + signals.addElement(signal); + } + notifyListeners(signal); + } + + /// Convenience overload for the common case. + public static void add(String id, int severity, String detail) { + add(new ShieldSignal(id, severity, detail)); + } + + /// The observations recorded so far. Never null. + public static ShieldSignal[] snapshot() { + synchronized (signals) { + ShieldSignal[] out = new ShieldSignal[signals.size()]; + signals.copyInto(out); + return out; + } + } + + /// True when any recorded observation is at or above the given severity. + public static boolean hasSignalAtLeast(int severity) { + synchronized (signals) { + for (int i = 0; i < signals.size(); i++) { + if (((ShieldSignal) signals.elementAt(i)).getSeverity() >= severity) { + return true; + } + } + } + return false; + } + + /// Discards every recorded observation. Intended for tests and for the simulator's + /// signal-faking menu. + public static void clear() { + synchronized (signals) { + signals.removeAllElements(); + } + } + + static void addListener(ShieldListener l) { + if (l == null) { + return; + } + synchronized (listeners) { + if (!listeners.contains(l)) { + listeners.addElement(l); + } + } + } + + static void removeListener(ShieldListener l) { + synchronized (listeners) { + listeners.removeElement(l); + } + } + + private static void notifyListeners(ShieldSignal signal) { + ShieldListener[] copy; + synchronized (listeners) { + if (listeners.isEmpty()) { + return; + } + copy = new ShieldListener[listeners.size()]; + listeners.copyInto(copy); + } + Display.getInstance().callSerially(new SignalDispatch(copy, signal)); + } + + private static final class SignalDispatch implements Runnable { + private final ShieldListener[] targets; + private final ShieldSignal signal; + + SignalDispatch(ShieldListener[] targets, ShieldSignal signal) { + this.targets = targets; + this.signal = signal; + } + + public void run() { + for (int i = 0; i < targets.length; i++) { + targets[i].signalRaised(signal); + } + } + } +} diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldStatus.java b/CodenameOne/src/com/codename1/security/shield/ShieldStatus.java new file mode 100644 index 00000000000..b2e2c891d90 --- /dev/null +++ b/CodenameOne/src/com/codename1/security/shield/ShieldStatus.java @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ +package com.codename1.security.shield; + +/// Outcome of a shield operation. +/// +/// The single most important distinction in this class is between *"I could not reach the +/// attestation service"* ([#NO_NETWORK], [#POOR_NETWORK], [#SERVICE_DOWN], [#RATE_LIMITED]) and +/// *"the attestation service looked at this device and said no"* ([#REJECTED]). An app should +/// almost always treat the first group as a transient condition to retry through, and only the +/// second as evidence that something is actually wrong with the device it is running on. +/// Collapsing the two into a single "attestation failed" boolean is the most common way to build +/// an app that either locks out users on a train or trusts a rooted phone. +/// +/// This is a class of constants rather than an enum because the vocabulary is wire-visible: the +/// attestation engine may report a status that this build of the framework predates, and +/// [#getId()] round-trips it rather than failing to resolve. +public final class ShieldStatus { + + /// The operation succeeded and any token returned is usable. + public static final ShieldStatus OK = new ShieldStatus("ok", true); + + /// The app was built without the enterprise attestation engine. Everything degrades to a + /// no-op: no token is issued, no pin is enforced, and no request is blocked. + public static final ShieldStatus UNPROTECTED = new ShieldStatus("unprotected", false); + + /// [AppShield#init(ShieldConfig)] has not been called yet. + public static final ShieldStatus NOT_INITIALIZED = new ShieldStatus("notInitialized", false); + + /// The device has no connectivity. Transient. + public static final ShieldStatus NO_NETWORK = new ShieldStatus("noNetwork", false); + + /// The request timed out or DNS failed. Transient. + public static final ShieldStatus POOR_NETWORK = new ShieldStatus("poorNetwork", false); + + /// The attestation service answered with a server error. Transient. + public static final ShieldStatus SERVICE_DOWN = new ShieldStatus("serviceUnavailable", false); + + /// This device is asking too often and is being throttled. Transient, but back off before + /// retrying rather than looping. + public static final ShieldStatus RATE_LIMITED = new ShieldStatus("rateLimited", false); + + /// The service evaluated this device and declined to issue a token. **Not** transient: the + /// device itself is what failed the policy. Retrying will not help. + public static final ShieldStatus REJECTED = new ShieldStatus("rejected", false); + + /// The certificate chain presented by a protected host matched no configured pin. The request + /// was refused before any request body was sent. + public static final ShieldStatus PIN_MISMATCH = new ShieldStatus("pinMismatch", false); + + private static final ShieldStatus[] KNOWN = { + OK, UNPROTECTED, NOT_INITIALIZED, NO_NETWORK, POOR_NETWORK, + SERVICE_DOWN, RATE_LIMITED, REJECTED, PIN_MISMATCH + }; + + private final String id; + private final boolean success; + + private ShieldStatus(String id, boolean success) { + this.id = id; + this.success = success; + } + + /// The stable wire identifier, e.g. `rateLimited`. + public String getId() { + return id; + } + + /// True only for [#OK]. Every other status means no usable token was produced. + public boolean isSuccess() { + return success; + } + + /// True when the failure is about reaching the service rather than about this device. Retrying + /// later may succeed. False for [#REJECTED] and [#PIN_MISMATCH], which describe the device and + /// the connection respectively. + public boolean isTransient() { + return this == NO_NETWORK || this == POOR_NETWORK + || this == SERVICE_DOWN || this == RATE_LIMITED; + } + + /// Resolves a wire identifier to a constant, or synthesises a non-success status for an + /// identifier this build does not know about. Never returns null. + public static ShieldStatus forId(String id) { + if (id == null) { + return NOT_INITIALIZED; + } + for (int i = 0; i < KNOWN.length; i++) { + if (KNOWN[i].id.equals(id)) { + return KNOWN[i]; + } + } + return new ShieldStatus(id, false); + } + + public String toString() { + return id; + } + + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof ShieldStatus)) { + return false; + } + return id.equals(((ShieldStatus) o).id); + } + + public int hashCode() { + return id.hashCode(); + } +} diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldToken.java b/CodenameOne/src/com/codename1/security/shield/ShieldToken.java new file mode 100644 index 00000000000..43c9ee6d5f8 --- /dev/null +++ b/CodenameOne/src/com/codename1/security/shield/ShieldToken.java @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ +package com.codename1.security.shield; + +/// A short-lived attestation token, ready to be attached to a request. +/// +/// The value is opaque to the app. It is meaningful only to the backend that verifies it against +/// the published Codename One signing keys -- do not parse it, and do not make a security decision +/// on the device based on its contents, because a device the attacker controls can be made to say +/// anything. +/// +/// #### Expiry is measured locally, on purpose +/// +/// Validity is tracked as "fetched at + time to live" using local elapsed time, never by reading an +/// expiry field out of the token and comparing it against the device clock. On a rooted device the +/// clock is attacker-controlled, so a token-embedded expiry can be made to look valid forever. The +/// verifying backend does its own absolute-time check regardless; [#isValid()] exists so the client +/// knows when to refresh, not to enforce anything. +public final class ShieldToken { + + private final String value; + private final ShieldStatus status; + private final long fetchedAt; + private final long ttlMillis; + private final String binding; + + public ShieldToken(String value, ShieldStatus status, long fetchedAt, + long ttlMillis, String binding) { + this.value = value; + this.status = status == null ? ShieldStatus.OK : status; + this.fetchedAt = fetchedAt; + this.ttlMillis = ttlMillis; + this.binding = binding; + } + + /// The opaque token to place in the request header. May be null when [#getStatus()] is not + /// [ShieldStatus#OK]. + public String getValue() { + return value; + } + + /// Outcome of the fetch that produced this token. + public ShieldStatus getStatus() { + return status; + } + + /// Milliseconds until this token stops being worth sending, or 0 once it has lapsed. + public long getMillisUntilExpiry() { + long remaining = (fetchedAt + ttlMillis) - System.currentTimeMillis(); + return remaining > 0 ? remaining : 0; + } + + /// True when the token has a value, was fetched successfully, and has not lapsed. + public boolean isValid() { + return value != null && status.isSuccess() && getMillisUntilExpiry() > 0; + } + + /// True once the token is far enough through its lifetime to be worth refreshing in the + /// background. Refreshing before expiry is what keeps a request from ever having to block. + public boolean shouldRefresh(int thresholdPercent) { + if (ttlMillis <= 0) { + return true; + } + long used = System.currentTimeMillis() - fetchedAt; + return used * 100 >= ttlMillis * thresholdPercent; + } + + /// The request-binding data this token was minted for, or null when it is a plain + /// time-limited token not tied to a specific request. + public String getBinding() { + return binding; + } + + /// True when this token was minted for exactly the supplied binding data. A token bound to one + /// request must not be reused for another; that is the whole point of binding. + public boolean isBoundTo(String data) { + if (binding == null) { + return data == null; + } + return binding.equals(data); + } + + /// Never renders the token value -- these strings end up in logs. + public String toString() { + return "ShieldToken[status=" + status.getId() + + ", validMs=" + getMillisUntilExpiry() + + ", bound=" + (binding != null) + "]"; + } +} diff --git a/CodenameOne/src/com/codename1/security/shield/package-info.java b/CodenameOne/src/com/codename1/security/shield/package-info.java new file mode 100644 index 00000000000..01dff0e4a22 --- /dev/null +++ b/CodenameOne/src/com/codename1/security/shield/package-info.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ + +/// API shielding: attestation tokens, over-the-air certificate pinning, and runtime +/// self-protection reporting. +/// +/// Start at [com.codename1.security.shield.AppShield]. +/// +/// #### What this can and cannot do +/// +/// Worth being precise about, because the category attracts overclaiming. On a device the attacker +/// fully controls, no client-side check is unbypassable -- detection code can be patched out and +/// headers can be stripped. What this buys you is threefold: +/// +/// 1. Your backend gets a **cryptographically verifiable statement from Apple or Google** about the +/// app and device, evaluated by a service the attacker does not control. That is a categorically +/// different thing from a boolean your own app computed about itself. +/// 2. The cost of scripted abuse rises from "reproduce the API calls with a shell script" to +/// "reverse-engineer and re-sign a native binary, per release". +/// 3. Certificate pins rotate over the air, so a pin change no longer needs an app store release -- +/// which is what makes pinning practical to run at all. +/// +/// It does not make an app unhackable, and any product in this space that says otherwise is selling +/// something. +/// +/// #### The load-bearing part is on your server +/// +/// The token means nothing until your backend refuses to serve requests without a valid one. Until +/// that check exists, adding the shield changes nothing about your security. +package com.codename1.security.shield; diff --git a/CodenameOne/src/com/codename1/security/shield/spi/DefaultEngineContext.java b/CodenameOne/src/com/codename1/security/shield/spi/DefaultEngineContext.java new file mode 100644 index 00000000000..45c8067e222 --- /dev/null +++ b/CodenameOne/src/com/codename1/security/shield/spi/DefaultEngineContext.java @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ +package com.codename1.security.shield.spi; + +import com.codename1.io.Log; +import com.codename1.security.DeviceIntegrity; +import com.codename1.security.SecureStorage; +import com.codename1.security.shield.ShieldSignal; +import com.codename1.security.shield.ShieldSignals; +import com.codename1.ui.Display; +import com.codename1.util.AsyncResource; + +/// The framework-backed [EngineContext] handed to an engine at initialization. +/// +/// Every method is defensive: an engine runs early in startup, often on a device in an unusual +/// state, and a platform probe that throws must degrade to an empty answer rather than take the +/// app down before it has drawn a frame. +final class DefaultEngineContext implements EngineContext { + + static final DefaultEngineContext INSTANCE = new DefaultEngineContext(); + + private DefaultEngineContext() { + } + + public SecureStorage getSecureStorage() { + return SecureStorage.getInstance(); + } + + public AsyncResource requestPlatformAttestation(String nonce) { + return DeviceIntegrity.requestIntegrityToken(nonce); + } + + public boolean isPlatformAttestationSupported() { + try { + return DeviceIntegrity.isAttestationSupported(); + } catch (Throwable t) { + return false; + } + } + + public void resetPlatformAttestation() { + try { + DeviceIntegrity.resetAttestation(); + } catch (Throwable t) { + Log.e(t); + } + } + + public String[] getPlatformCompromiseReasons() { + try { + String[] r = DeviceIntegrity.getCompromiseReasons(); + return r == null ? new String[0] : r; + } catch (Throwable t) { + return new String[0]; + } + } + + public String[] getEnabledAccessibilityServices() { + try { + String[] r = DeviceIntegrity.getEnabledAccessibilityServices(); + return r == null ? new String[0] : r; + } catch (Throwable t) { + return new String[0]; + } + } + + public String[] getAppSignerDigests() { + try { + String[] r = Display.getInstance().getAppSignerDigests(); + return r == null ? new String[0] : r; + } catch (Throwable t) { + return new String[0]; + } + } + + public String getProperty(String key, String defaultValue) { + try { + return Display.getInstance().getProperty(key, defaultValue); + } catch (Throwable t) { + return defaultValue; + } + } + + public void log(String message) { + Log.p(message); + } + + public void publishSignal(ShieldSignal signal) { + ShieldSignals.add(signal); + } +} diff --git a/CodenameOne/src/com/codename1/security/shield/spi/EngineContext.java b/CodenameOne/src/com/codename1/security/shield/spi/EngineContext.java new file mode 100644 index 00000000000..824217b8c2a --- /dev/null +++ b/CodenameOne/src/com/codename1/security/shield/spi/EngineContext.java @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ +package com.codename1.security.shield.spi; + +import com.codename1.security.SecureStorage; +import com.codename1.security.shield.ShieldSignal; +import com.codename1.util.AsyncResource; + +/// The narrow set of framework services lent to a [ShieldEngine]. +/// +/// Handed to the engine rather than reached for, so the engine never needs access to the platform +/// implementation object. That keeps the framework's implementation accessor package-private, and +/// it keeps the list of things an engine can do small enough to review. +public interface EngineContext { + + /// Non-prompting secure storage, for the attestation key identifier and cached tokens. On + /// device this is the platform keychain or keystore. + SecureStorage getSecureStorage(); + + /// Requests a raw platform attestation (Play Integrity or App Attest) bound to the nonce. + /// + /// The result is opaque and must be forwarded to the verifying service; the engine must not + /// try to interpret it on the device, because a device the attacker controls can be made to + /// produce any interpretation. + AsyncResource requestPlatformAttestation(String nonce); + + /// True when the platform provides attestation and this build bundled it. + boolean isPlatformAttestationSupported(); + + /// Clears cached platform attestation state, forcing a fresh hardware key on the next request. + /// Used when the service reports that the device's attestation key is unknown to it. + void resetPlatformAttestation(); + + /// Platform-detected compromise reasons, such as `root` or `frida`. + String[] getPlatformCompromiseReasons(); + + /// Component identifiers of the accessibility services currently enabled. + String[] getEnabledAccessibilityServices(); + + /// Digests of the certificates the running app is actually signed with, for comparison against + /// what it was built with. Empty where the platform cannot report it. + /// + /// Not exposed as public framework API: nothing in an app needs this, and publishing it would + /// only tell an attacker exactly which value to fake. + String[] getAppSignerDigests(); + + /// A build-stamped property, such as the build key or the per-build hardening manifest. + String getProperty(String key, String defaultValue); + + /// Writes to the framework log. + void log(String message); + + /// Publishes an observation to [com.codename1.security.shield.ShieldSignals], where the app + /// can see it and from where it is offered to the service on the next token fetch. + void publishSignal(ShieldSignal signal); +} diff --git a/CodenameOne/src/com/codename1/security/shield/spi/ShieldEngine.java b/CodenameOne/src/com/codename1/security/shield/spi/ShieldEngine.java new file mode 100644 index 00000000000..06d83e186b7 --- /dev/null +++ b/CodenameOne/src/com/codename1/security/shield/spi/ShieldEngine.java @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ +package com.codename1.security.shield.spi; + +import com.codename1.security.shield.PinSet; +import com.codename1.security.shield.ShieldConfig; +import com.codename1.security.shield.ShieldException; +import com.codename1.security.shield.ShieldSignal; +import com.codename1.security.shield.ShieldToken; + +/// The service-provider seam between the public shield API and the attestation engine that +/// implements it. +/// +/// Codename One ships an inert default that reports itself unavailable, so an app written against +/// [com.codename1.security.shield.AppShield] compiles and runs everywhere. A build entitled to the +/// enterprise engine has a real implementation registered by the build server before +/// `Display.init`, via [ShieldEngineRegistry#setEngine(ShieldEngine)]. +/// +/// #### What must never move into the framework +/// +/// The split is only worth anything if the engine keeps the parts an attacker would want to reach. +/// An implementation must own, and must never delegate to open framework code: +/// +/// - challenge and nonce generation -- predictable nonces make replay possible; +/// - any key material, and any code that touches it; +/// - the pin **comparison** and the decision to fail a request. The framework may hold a +/// [PinSet]; patching the framework's copy must not be enough to disable pinning; +/// - the detection heuristics themselves. Published heuristics are bypassed heuristics, so the +/// framework only ever sees finished [ShieldSignal] results -- and only the ones the engine +/// chooses to publish; +/// - interpretation of the raw platform attestation. Those responses go to the verifying service, +/// which the attacker does not control, rather than being judged on the device. +/// +/// The security property this preserves is not "the app refuses to make the call" -- an attacker +/// who controls the device can always strip a header. It is that the customer's backend refuses to +/// *serve* a request without a valid, unexpired, service-signed token, which a substituted engine +/// cannot mint. +public interface ShieldEngine { + + /// A stable name for diagnostics, for example `unprotected`, `simulator` or the enterprise + /// engine's own identifier. + String getName(); + + /// True when this engine can actually attest. False for the inert default, which is how + /// [com.codename1.security.shield.AppShield#isProtected()] is answered. + boolean isAvailable(); + + /// Called once from [com.codename1.security.shield.AppShield#init(ShieldConfig)]. Must not + /// block on the network; do warm-up work on a background thread. + void initialize(EngineContext ctx, ShieldConfig config); + + /// Obtains a token, blocking until it has one or fails. Called on a network thread, never the + /// EDT. + /// + /// @param bindingData request data to bind the token to, or null for a plain time-limited + /// token. A bound token is only valid for the request whose data was supplied. + /// @throws ShieldException carrying the [com.codename1.security.shield.ShieldStatus] that + /// explains whether the failure was about reaching the service or about this device + ShieldToken fetchToken(String bindingData) throws ShieldException; + + /// The cached token, without contacting the service. Returns null when nothing is cached. + /// + /// Must never block: callers are typically on the EDT, deciding whether they can decorate a + /// request right now. + ShieldToken getCachedToken(); + + /// Decides whether a certificate chain is acceptable for a host. + /// + /// Must be purely local and non-blocking: on iOS this is invoked synchronously from the TLS + /// delegate thread while the handshake is held open, so any network call or blocking wait here + /// deadlocks the connection. + /// + /// Returns true when the host is not pinned -- "no opinion" must never read as a mismatch. + /// + /// @param spkiDigests base64 SHA-256 digests of each chain certificate's public key info + /// @param certDigests whole-certificate digests, for engines that pin those instead + boolean verifyPins(String host, String[] spkiDigests, String[] certDigests); + + /// The pin set currently in force, never null. May be [PinSet#EMPTY]. + PinSet getPinSet(); + + /// The runtime self-protection observations this engine wants reported. May legitimately be a + /// subset of what it detected. + ShieldSignal[] collectSignals(); + + /// Discards any cached token, forcing the next fetch to go to the service. Called when a + /// backend rejects a token, which usually means the device's attestation state is stale. + void invalidate(); + + /// Releases resources. Called when the app is shutting down. + void shutdown(); +} diff --git a/CodenameOne/src/com/codename1/security/shield/spi/ShieldEngineRegistry.java b/CodenameOne/src/com/codename1/security/shield/spi/ShieldEngineRegistry.java new file mode 100644 index 00000000000..cb4c8419a10 --- /dev/null +++ b/CodenameOne/src/com/codename1/security/shield/spi/ShieldEngineRegistry.java @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ +package com.codename1.security.shield.spi; + +/// Where the attestation engine registers itself. +/// +/// Registration is by direct instance, not by class name. Codename One obfuscates and renames +/// classes -- ProGuard/R8 on Android, the bytecode-to-C translation on iOS -- so a +/// `Class.forName` lookup is unreliable by construction. The port or the build-server-generated +/// bootstrap instantiates the engine itself and passes the instance here, which survives renaming +/// because it is an ordinary symbol reference. The same convention is used elsewhere in the +/// framework for port-supplied implementations. +/// +/// Registration happens in one of three places: +/// +/// - **Device builds**: the build server splices a bootstrap into the generated application stub, +/// ahead of `Display.init`, when the project is entitled to the enterprise engine. +/// - **Simulator**: the desktop port's post-init bootstrap scan picks it up, which is the one +/// place a name-based lookup is safe because the desktop port is not obfuscated. +/// - **Tests**: call [#setEngine(ShieldEngine)] directly. +/// +/// The first registration wins and the registry then seals. Without that, any code running later +/// in the process -- including code an attacker injected -- could swap in an engine that returns +/// whatever it likes. Sealing does not make the app tamper-proof (an attacker who can patch the +/// binary can patch this too); it removes the version of the attack that needs no patching at all. +public final class ShieldEngineRegistry { + + private static ShieldEngine engine; + private static boolean sealed; + + private ShieldEngineRegistry() { + } + + /// Registers the engine. The first call wins. + /// + /// @throws IllegalStateException if an engine is already registered + public static void setEngine(ShieldEngine e) { + if (e == null) { + throw new IllegalArgumentException("engine is null"); + } + synchronized (ShieldEngineRegistry.class) { + if (sealed) { + throw new IllegalStateException( + "A shield engine is already registered: " + engine.getName()); + } + engine = e; + sealed = true; + } + } + + /// The registered engine, or the inert default when none was registered. Never null, so no + /// caller needs a null check and no code path can silently skip a check that should have run. + public static ShieldEngine getEngine() { + synchronized (ShieldEngineRegistry.class) { + return engine != null ? engine : UnprotectedEngine.INSTANCE; + } + } + + /// The framework-backed [EngineContext] an engine is initialized with. Exposed so a port or a + /// test can construct an engine against the real services without reimplementing them. + public static EngineContext getDefaultContext() { + return DefaultEngineContext.INSTANCE; + } + + /// True when a real engine was registered. + public static boolean isEngineRegistered() { + synchronized (ShieldEngineRegistry.class) { + return engine != null; + } + } + + /// Test hook: drops the registration and unseals. Not for application use -- there is no + /// legitimate reason for a shipping app to replace its engine at runtime. + static void resetForTesting() { + synchronized (ShieldEngineRegistry.class) { + engine = null; + sealed = false; + } + } +} diff --git a/CodenameOne/src/com/codename1/security/shield/spi/UnprotectedEngine.java b/CodenameOne/src/com/codename1/security/shield/spi/UnprotectedEngine.java new file mode 100644 index 00000000000..d29b2b8aed7 --- /dev/null +++ b/CodenameOne/src/com/codename1/security/shield/spi/UnprotectedEngine.java @@ -0,0 +1,139 @@ +/* + * Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ +package com.codename1.security.shield.spi; + +import com.codename1.security.DeviceIntegrity; +import com.codename1.security.shield.PinSet; +import com.codename1.security.shield.ShieldConfig; +import com.codename1.security.shield.ShieldException; +import com.codename1.security.shield.ShieldSignal; +import com.codename1.security.shield.ShieldStatus; +import com.codename1.security.shield.ShieldToken; +import java.util.Vector; + +/// The engine used when no attestation engine was registered -- an open-source build, a build not +/// entitled to the enterprise engine, or a unit test. +/// +/// The contract it implements is the degradation promise the public API makes: an app written +/// against the shield must run everywhere, and must never fail closed just because attestation is +/// unavailable. +/// +/// - `fetchToken` **completes**, with a [ShieldStatus#UNPROTECTED] failure. It never hangs and +/// never throws synchronously, so callers written for the real engine follow their normal error +/// path instead of deadlocking. +/// - `verifyPins` returns true. There is no pin set to enforce, and reporting "no opinion" as a +/// mismatch would break every request. +/// - Nothing here can block a request. +/// +/// It is not entirely inert: [#collectSignals()] still reports what the free platform checks +/// found, so an app can react to a rooted device without an enterprise entitlement. +final class UnprotectedEngine implements ShieldEngine { + + static final UnprotectedEngine INSTANCE = new UnprotectedEngine(); + + private UnprotectedEngine() { + } + + public String getName() { + return "unprotected"; + } + + public boolean isAvailable() { + return false; + } + + public void initialize(EngineContext ctx, ShieldConfig config) { + if (ctx != null) { + ctx.log("AppShield: no attestation engine registered; running unprotected. " + + "Tokens are not issued and certificate pins are not enforced."); + } + } + + public ShieldToken fetchToken(String bindingData) throws ShieldException { + throw new ShieldException(ShieldStatus.UNPROTECTED, + "This build has no attestation engine, so no token can be issued."); + } + + public ShieldToken getCachedToken() { + return null; + } + + public boolean verifyPins(String host, String[] spkiDigests, String[] certDigests) { + return true; + } + + public PinSet getPinSet() { + return PinSet.EMPTY; + } + + public ShieldSignal[] collectSignals() { + String[] reasons; + try { + reasons = DeviceIntegrity.getCompromiseReasons(); + } catch (Throwable t) { + // Never let a platform probe break the caller; an absent signal is + // strictly better than a crashed app. + return new ShieldSignal[0]; + } + if (reasons == null || reasons.length == 0) { + return new ShieldSignal[0]; + } + Vector out = new Vector(); + for (int i = 0; i < reasons.length; i++) { + ShieldSignal s = toSignal(reasons[i]); + if (s != null) { + out.addElement(s); + } + } + ShieldSignal[] arr = new ShieldSignal[out.size()]; + out.copyInto(arr); + return arr; + } + + private static ShieldSignal toSignal(String reason) { + if (reason == null) { + return null; + } + if ("root".equals(reason)) { + return new ShieldSignal(ShieldSignal.ROOT, 70, null); + } + if ("jailbreak".equals(reason)) { + return new ShieldSignal(ShieldSignal.JAILBREAK, 70, null); + } + if ("frida".equals(reason)) { + return new ShieldSignal(ShieldSignal.HOOK, 90, "frida"); + } + if ("emulator".equals(reason)) { + // Low severity on purpose: every developer's device is an emulator. + // The service weighs it against the other signals. + return new ShieldSignal(ShieldSignal.EMULATOR, 30, null); + } + return new ShieldSignal(reason, 50, null); + } + + public void invalidate() { + } + + public void shutdown() { + } +} diff --git a/CodenameOne/src/com/codename1/security/shield/spi/package-info.java b/CodenameOne/src/com/codename1/security/shield/spi/package-info.java new file mode 100644 index 00000000000..062b0127f6b --- /dev/null +++ b/CodenameOne/src/com/codename1/security/shield/spi/package-info.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ + +/// Service-provider interface between the public shield API and the attestation engine. +/// +/// Application code does not use this package. It exists so the engine that performs attestation, +/// pin enforcement and tamper detection can be supplied separately from the framework, while +/// [com.codename1.security.shield.AppShield] keeps a single stable surface that compiles and runs +/// whether or not an engine is present. +/// +/// See [com.codename1.security.shield.spi.ShieldEngine] for the contract, and in particular for +/// the list of responsibilities an engine must not delegate back into open framework code. +package com.codename1.security.shield.spi; diff --git a/CodenameOne/src/com/codename1/ui/Display.java b/CodenameOne/src/com/codename1/ui/Display.java index e92ec26b328..26c6232b16f 100644 --- a/CodenameOne/src/com/codename1/ui/Display.java +++ b/CodenameOne/src/com/codename1/ui/Display.java @@ -6859,6 +6859,19 @@ public String[] getEnabledAccessibilityServices() { return impl.getEnabledAccessibilityServices(); } + /// Discards cached platform attestation state, forcing the next attestation to start from a fresh + /// hardware key. See `com.codename1.security.DeviceIntegrity#resetAttestation()`. + public void resetAttestation() { + impl.resetAttestation(); + } + + /// Returns digests of the certificates the running app is signed with. Low level hook for the + /// attestation layer, which reports them to a verifying service; an on-device comparison proves + /// nothing on its own. Empty where the platform has no such concept. + public String[] getAppSignerDigests() { + return impl.getAppSignerDigests(); + } + /// Marks the current screen secure (Android `FLAG_SECURE`), blocking screenshots/recording/scraping. public void setSecureScreen(boolean secure) { impl.setSecureScreen(secure); diff --git a/Ports/Android/src/com/codename1/impl/android/AndroidImplementation.java b/Ports/Android/src/com/codename1/impl/android/AndroidImplementation.java index cf0e724c03b..469b8a115fb 100644 --- a/Ports/Android/src/com/codename1/impl/android/AndroidImplementation.java +++ b/Ports/Android/src/com/codename1/impl/android/AndroidImplementation.java @@ -221,7 +221,7 @@ import java.security.MessageDigest; import java.text.ParseException; import java.util.*; -import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.AtomicLong; import javax.net.ssl.HttpsURLConnection; import javax.xml.parsers.ParserConfigurationException; @@ -231,10 +231,10 @@ import org.xml.sax.SAXException; //import android.webkit.JavascriptInterface; -public class AndroidImplementation extends CodenameOneImplementation implements IntentResultListener { - private AndroidCalendarSource calendarSource; - private static final AtomicLong V3_NOTIFICATION_SEQUENCE = new AtomicLong(); - +public class AndroidImplementation extends CodenameOneImplementation implements IntentResultListener { + private AndroidCalendarSource calendarSource; + private static final AtomicLong V3_NOTIFICATION_SEQUENCE = new AtomicLong(); + public static final Thread.UncaughtExceptionHandler exceptionHandler = new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable e) { @@ -821,94 +821,94 @@ private static byte[] readInputStream(InputStream i) throws IOException { } - public static void appendNotification(String type, String body, Context a) { + public static void appendNotification(String type, String body, Context a) { appendNotification(type, body, null, null, a); - } - - /** Receives the managed typed envelope from FCM without applying legacy push decoding. */ - public static void handleV3Push(final String envelope, Context context, - boolean appRunning, Class appStubClass) { - if (appRunning && Display.isInitialized() - && com.codename1.push.PushClient.hasActiveClient()) { - Display.getInstance().callSerially(new Runnable() { - public void run() { - com.codename1.push.PushClient.dispatch(envelope); - } - }); - return; - } - try { - org.json.JSONObject message = new org.json.JSONObject(envelope); - // The pending-push file explicitly encodes whether a legacy type is present. - // A missing type is the sentinel for a typed V3 envelope and is replayed intact. - appendNotification(null, envelope, context); - if (message.optBoolean("silent", false)) { - return; - } - String title = message.optString("title", ""); - String body = message.optString("body", ""); - String image = message.optString("image", ""); - if (title.length() == 0 && body.length() == 0 && image.length() == 0) { - return; - } - if (title.length() == 0) { - title = context.getApplicationInfo().loadLabel(context.getPackageManager()).toString(); - } - Intent intent = new Intent(context, appStubClass); - PendingIntent contentIntent = createPendingIntent(context, 0, intent); - int smallIcon = context.getResources().getIdentifier("ic_stat_notify", "drawable", - context.getPackageName()); - if (smallIcon == 0) { - smallIcon = context.getApplicationInfo().icon; - } - NotificationCompat.Builder builder = new NotificationCompat.Builder(context) - .setContentTitle(title) - .setContentText(body) - .setSmallIcon(smallIcon) - .setContentIntent(contentIntent) - .setAutoCancel(true) - .setWhen(System.currentTimeMillis()); - NotificationManager manager = (NotificationManager) - context.getSystemService(Context.NOTIFICATION_SERVICE); - setNotificationChannel(manager, builder, context); - String collapseKey = message.optString("collapseKey", null); - String messageId = message.optString("id", null); - String notificationTag; - if (collapseKey != null && collapseKey.length() > 0) { - notificationTag = v3NotificationTag("CN1_PUSH_V3_COLLAPSE:", collapseKey); - } else if (messageId != null && messageId.length() > 0) { - notificationTag = v3NotificationTag("CN1_PUSH_V3_MESSAGE:", messageId); - } else { - notificationTag = "CN1_PUSH_V3_EPHEMERAL:" + System.currentTimeMillis() - + ":" + V3_NOTIFICATION_SEQUENCE.incrementAndGet(); - } - manager.notify(notificationTag, 0, builder.build()); - } catch (Exception error) { - Log.e("Codename One", "Failed to handle a Push V3 envelope", error); - } - } - - private static String v3NotificationTag(String prefix, String value) { - if (prefix.length() + value.length() <= 128) { - return prefix + value; - } - try { - byte[] digest = MessageDigest.getInstance("SHA-256") - .digest(value.getBytes(StandardCharsets.UTF_8)); - StringBuilder out = new StringBuilder(prefix.length() + digest.length * 2); - out.append(prefix); - for (byte item : digest) { - int unsigned = item & 0xff; - if (unsigned < 0x10) { - out.append('0'); - } - out.append(Integer.toHexString(unsigned)); - } - return out.toString(); - } catch (Exception error) { - return prefix + Integer.toHexString(value.hashCode()); - } - } + } + + /** Receives the managed typed envelope from FCM without applying legacy push decoding. */ + public static void handleV3Push(final String envelope, Context context, + boolean appRunning, Class appStubClass) { + if (appRunning && Display.isInitialized() + && com.codename1.push.PushClient.hasActiveClient()) { + Display.getInstance().callSerially(new Runnable() { + public void run() { + com.codename1.push.PushClient.dispatch(envelope); + } + }); + return; + } + try { + org.json.JSONObject message = new org.json.JSONObject(envelope); + // The pending-push file explicitly encodes whether a legacy type is present. + // A missing type is the sentinel for a typed V3 envelope and is replayed intact. + appendNotification(null, envelope, context); + if (message.optBoolean("silent", false)) { + return; + } + String title = message.optString("title", ""); + String body = message.optString("body", ""); + String image = message.optString("image", ""); + if (title.length() == 0 && body.length() == 0 && image.length() == 0) { + return; + } + if (title.length() == 0) { + title = context.getApplicationInfo().loadLabel(context.getPackageManager()).toString(); + } + Intent intent = new Intent(context, appStubClass); + PendingIntent contentIntent = createPendingIntent(context, 0, intent); + int smallIcon = context.getResources().getIdentifier("ic_stat_notify", "drawable", + context.getPackageName()); + if (smallIcon == 0) { + smallIcon = context.getApplicationInfo().icon; + } + NotificationCompat.Builder builder = new NotificationCompat.Builder(context) + .setContentTitle(title) + .setContentText(body) + .setSmallIcon(smallIcon) + .setContentIntent(contentIntent) + .setAutoCancel(true) + .setWhen(System.currentTimeMillis()); + NotificationManager manager = (NotificationManager) + context.getSystemService(Context.NOTIFICATION_SERVICE); + setNotificationChannel(manager, builder, context); + String collapseKey = message.optString("collapseKey", null); + String messageId = message.optString("id", null); + String notificationTag; + if (collapseKey != null && collapseKey.length() > 0) { + notificationTag = v3NotificationTag("CN1_PUSH_V3_COLLAPSE:", collapseKey); + } else if (messageId != null && messageId.length() > 0) { + notificationTag = v3NotificationTag("CN1_PUSH_V3_MESSAGE:", messageId); + } else { + notificationTag = "CN1_PUSH_V3_EPHEMERAL:" + System.currentTimeMillis() + + ":" + V3_NOTIFICATION_SEQUENCE.incrementAndGet(); + } + manager.notify(notificationTag, 0, builder.build()); + } catch (Exception error) { + Log.e("Codename One", "Failed to handle a Push V3 envelope", error); + } + } + + private static String v3NotificationTag(String prefix, String value) { + if (prefix.length() + value.length() <= 128) { + return prefix + value; + } + try { + byte[] digest = MessageDigest.getInstance("SHA-256") + .digest(value.getBytes(StandardCharsets.UTF_8)); + StringBuilder out = new StringBuilder(prefix.length() + digest.length * 2); + out.append(prefix); + for (byte item : digest) { + int unsigned = item & 0xff; + if (unsigned < 0x10) { + out.append('0'); + } + out.append(Integer.toHexString(unsigned)); + } + return out.toString(); + } catch (Exception error) { + return prefix + Integer.toHexString(value.hashCode()); + } + } public static void appendNotification(String type, String body, String image, String category, Context a) { try { @@ -7175,6 +7175,46 @@ public boolean canGetSSLCertificates() { return true; } + @Override + public boolean canGetPublicKeyDigests() { + return true; + } + + @Override + public String[] getSSLCertificatesEx(Object connection, String url) throws IOException { + if (connection instanceof HttpsURLConnection) { + HttpsURLConnection conn = (HttpsURLConnection) connection; + try { + conn.connect(); + java.security.cert.Certificate[] certs = conn.getServerCertificates(); + java.util.List out = new java.util.ArrayList(); + for (int i = 0; i < certs.length; i++) { + java.security.cert.Certificate cert = certs[i]; + out.add("CHAIN:" + i); + MessageDigest sha256 = MessageDigest.getInstance("SHA-256"); + sha256.update(cert.getEncoded()); + out.add("SHA-256:" + dumpHex(sha256.digest())); + MessageDigest sha1 = MessageDigest.getInstance("SHA1"); + sha1.update(cert.getEncoded()); + out.add("SHA1:" + dumpHex(sha1.digest())); + // getPublicKey().getEncoded() is already the DER SubjectPublicKeyInfo, + // which is exactly what a public-key pin is computed over. + java.security.PublicKey pk = cert.getPublicKey(); + if (pk != null && pk.getEncoded() != null) { + MessageDigest spki = MessageDigest.getInstance("SHA-256"); + spki.update(pk.getEncoded()); + out.add("SPKI-SHA-256:" + + com.codename1.util.Base64.encodeNoNewline(spki.digest())); + } + } + return out.toArray(new String[out.size()]); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + return new String[0]; + } + /** * @inheritDoc */ @@ -8411,20 +8451,20 @@ public boolean isContactsPermissionGranted() { @Override - public String[] getAllContacts(boolean withNumbers) { + public String[] getAllContacts(boolean withNumbers) { if(!checkForPermission(Manifest.permission.READ_CONTACTS, "This is required to get the contacts")){ return new String[]{}; } return AndroidContactsManager.getInstance().getContacts(getContext(), withNumbers); - } - - @Override - public com.codename1.calendar.LocalCalendarSource getLocalCalendarSource() { - if (calendarSource == null) { - calendarSource = new AndroidCalendarSource(getContext()); - } - return calendarSource; - } + } + + @Override + public com.codename1.calendar.LocalCalendarSource getLocalCalendarSource() { + if (calendarSource == null) { + calendarSource = new AndroidCalendarSource(getContext()); + } + return calendarSource; + } @Override public Contact getContactById(String id) { @@ -9026,10 +9066,10 @@ private ClipData enrichClipWithBinaryContent(ClipboardContent content, ClipData imageExt = "gif"; } if (imageBytes != null) { - // AndroidGradleBuilder exposes cache/intent_files through the app's - // FileProvider. Keep generated clipboard payloads inside that root so - // FileProvider can safely create a content:// URI for paste targets. - File imageFile = new File(new File(getContext().getCacheDir(), "intent_files"), + // AndroidGradleBuilder exposes cache/intent_files through the app's + // FileProvider. Keep generated clipboard payloads inside that root so + // FileProvider can safely create a content:// URI for paste targets. + File imageFile = new File(new File(getContext().getCacheDir(), "intent_files"), "cn1-clip-image-" + System.currentTimeMillis() + "." + imageExt); imageFile.getParentFile().mkdirs(); OutputStream os = new FileOutputStream(imageFile); @@ -9063,14 +9103,14 @@ private ClipData enrichClipWithBinaryContent(ClipboardContent content, ClipData continue; } Uri u; - if (pathOrUri.startsWith("content:")) { - u = Uri.parse(pathOrUri); - } else { - File file = pathOrUri.startsWith("file:") - ? new File(Uri.parse(pathOrUri).getPath()) - : new File(pathOrUri); - u = FileProvider.getUriForFile(getContext(), authority, file); - getContext().grantUriPermission("android", u, Intent.FLAG_GRANT_READ_URI_PERMISSION); + if (pathOrUri.startsWith("content:")) { + u = Uri.parse(pathOrUri); + } else { + File file = pathOrUri.startsWith("file:") + ? new File(Uri.parse(pathOrUri).getPath()) + : new File(pathOrUri); + u = FileProvider.getUriForFile(getContext(), authority, file); + getContext().grantUriPermission("android", u, Intent.FLAG_GRANT_READ_URI_PERMISSION); } if (clip == null) { clip = new ClipData("Codename One", new String[]{ "text/uri-list" }, new ClipData.Item(u)); @@ -10593,7 +10633,7 @@ public static boolean hasAndroidMarket(Context activity) { } @Override - public void registerPush(Hashtable metaData, boolean noFallback) { + public void registerPush(Hashtable metaData, boolean noFallback) { if (getActivity() == null) { return; } @@ -10604,18 +10644,18 @@ public void registerPush(Hashtable metaData, boolean noFallback) { } } - boolean huawei = "huawei".equals(Display.getInstance().getProperty("cn1.push.transport", "")); - if (!hasAndroidMarket() && !huawei) { - Log.d("Codename One", "Device doesn't have Android market/google play can't register for push!"); - return; - } - String id = ""; - if (!huawei) { - id = (String)metaData.get(com.codename1.push.Push.GOOGLE_PUSH_KEY); - if (id == null) { - id = Display.getInstance().getProperty("gcm.sender_id", null); - } - } + boolean huawei = "huawei".equals(Display.getInstance().getProperty("cn1.push.transport", "")); + if (!hasAndroidMarket() && !huawei) { + Log.d("Codename One", "Device doesn't have Android market/google play can't register for push!"); + return; + } + String id = ""; + if (!huawei) { + id = (String)metaData.get(com.codename1.push.Push.GOOGLE_PUSH_KEY); + if (id == null) { + id = Display.getInstance().getProperty("gcm.sender_id", null); + } + } Log.d("Codename One", "Sending async push request for id: " + id); ((CodenameOneActivity) getActivity()).registerForPush(id); } @@ -10629,9 +10669,9 @@ public static void registerPolling() { } @Override - public void deregisterPush() { - boolean has = hasAndroidMarket() - || "huawei".equals(Display.getInstance().getProperty("cn1.push.transport", "")); + public void deregisterPush() { + boolean has = hasAndroidMarket() + || "huawei".equals(Display.getInstance().getProperty("cn1.push.transport", "")); if (has) { ((CodenameOneActivity) getActivity()).stopReceivingPush(); deregisterPushFromServer(); @@ -13563,6 +13603,87 @@ public boolean isDeviceCompromised() { return getCompromiseReasons().length > 0; } + /** + * Base64 SHA-256 digests of the certificates this APK is actually signed with. + * + *

Uses the v2/v3 signing-block API on API 28 and up, which reports the full + * signing lineage after a key rotation; below that only the legacy v1 signature + * is available. Note that under Play App Signing the digest seen here is + * Google's app signing key, not the developer's upload key -- comparing + * against the upload key is the classic way to make every production install + * report itself as repackaged.

+ */ + @Override + public String[] getAppSignerDigests() { + try { + Context ctx = getContext(); + if (ctx == null) { + return new String[0]; + } + PackageManager pm = ctx.getPackageManager(); + String pkg = ctx.getPackageName(); + Signature[] signatures = null; + if (android.os.Build.VERSION.SDK_INT >= 28) { + // Reflection because the port compiles against an older android.jar + // than the devices it runs on, the same reason the Play Integrity + // call in this file is reflective. + signatures = signingCertificatesViaReflection(pm, pkg); + } + if (signatures == null) { + PackageInfo info = pm.getPackageInfo(pkg, PackageManager.GET_SIGNATURES); + signatures = info.signatures; + } + if (signatures == null) { + return new String[0]; + } + java.util.ArrayList out = new java.util.ArrayList(); + for (int i = 0; i < signatures.length; i++) { + MessageDigest md = MessageDigest.getInstance("SHA-256"); + md.update(signatures[i].toByteArray()); + out.add(com.codename1.util.Base64.encodeNoNewline(md.digest())); + } + return out.toArray(new String[out.size()]); + } catch (Throwable t) { + // Reporting nothing is better than failing a request over a + // package-manager quirk on some OEM build. + com.codename1.io.Log.e(t); + return new String[0]; + } + } + + /** + * PackageManager.GET_SIGNING_CERTIFICATES. Inlined because the port compiles + * against an android.jar that predates it. + */ + private static final int FLAG_GET_SIGNING_CERTIFICATES = 0x08000000; + + /** + * Reads the v2/v3 signing certificates on API 28+, or null when unavailable so + * the caller falls back to the legacy v1 signatures. + */ + private static Signature[] signingCertificatesViaReflection(PackageManager pm, String pkg) { + try { + PackageInfo info = pm.getPackageInfo(pkg, FLAG_GET_SIGNING_CERTIFICATES); + java.lang.reflect.Field signingInfoField = + PackageInfo.class.getField("signingInfo"); + Object signingInfo = signingInfoField.get(info); + if (signingInfo == null) { + return null; + } + Class signingInfoClass = signingInfo.getClass(); + boolean multipleSigners = ((Boolean) signingInfoClass + .getMethod("hasMultipleSigners").invoke(signingInfo)).booleanValue(); + // With one signer the history includes the pre-rotation certificates, + // which a server comparing against an older build still needs to accept. + String method = multipleSigners + ? "getApkContentsSigners" + : "getSigningCertificateHistory"; + return (Signature[]) signingInfoClass.getMethod(method).invoke(signingInfo); + } catch (Throwable t) { + return null; + } + } + @Override public String[] getCompromiseReasons() { java.util.ArrayList reasons = new java.util.ArrayList(); diff --git a/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java b/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java index c1329f53e74..556273f1664 100644 --- a/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java +++ b/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java @@ -54,6 +54,7 @@ import javax.crypto.KeyGenerator; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; +import javax.crypto.spec.GCMParameterSpec; import javax.crypto.spec.IvParameterSpec; /** @@ -76,6 +77,13 @@ * key and recreate it on first failure to recover. * See Google issue 65578763. * + * + *

The non-prompting tier ({@code set(account, value)} and friends) uses a + * separate keystore key ({@code CN1PlainKey}) and preferences file, + * created without {@code setUserAuthenticationRequired}, with AES/GCM. Keeping + * it separate matters: the biometric key is invalidated whenever the user + * re-enrols biometrics, and secrets read on every network call must survive + * that.

*/ public final class AndroidSecureStorage extends SecureStorage { @@ -83,6 +91,16 @@ public final class AndroidSecureStorage extends SecureStorage { private static final String PREFS = "CN1BiometricSecureStorage"; private static final String ANDROID_KEY_STORE = "AndroidKeyStore"; + /** + * Deliberately distinct from {@link #KEY_ID}: the biometric key is created + * with {@code setUserAuthenticationRequired(true)} and is invalidated when + * the user re-enrols biometrics. The non-prompting tier must survive that, + * so it gets its own key and its own preferences file. + */ + private static final String PLAIN_KEY_ID = "CN1PlainKey"; + private static final String PLAIN_PREFS = "CN1PlainSecureStorage"; + private static final int GCM_TAG_BITS = 128; + private KeyStore keyStore; private KeyGenerator keyGenerator; private Cipher cipher; @@ -177,6 +195,178 @@ public AsyncResource remove(String reason, String account) { return result; } + // --- Non-prompting tier ------------------------------------------------ + // + // AES/GCM under a dedicated AndroidKeyStore key created *without* + // setUserAuthenticationRequired, so reads never raise a biometric prompt. + // Deliberately not androidx.security EncryptedSharedPreferences: that + // would force a transitive dependency on every Android build and it is + // itself deprecated. The value is stored as + // base64(iv) + ":" + base64(ciphertext) in a private preferences file. + + @Override + public boolean set(String account, String value) { + if (account == null || value == null) { + return false; + } + if (Build.VERSION.SDK_INT < 23) { + return legacyPlainSet(account, value); + } + try { + SecretKey key = plainKey(true); + if (key == null) { + return false; + } + Cipher c = Cipher.getInstance("AES/GCM/NoPadding"); + c.init(Cipher.ENCRYPT_MODE, key); + byte[] enc = c.doFinal(value.getBytes("UTF-8")); + plainPrefs().edit() + .putString(account, Base64.encodeToString(c.getIV(), Base64.NO_WRAP) + + ":" + Base64.encodeToString(enc, Base64.NO_WRAP)) + .apply(); + return true; + } catch (InvalidKeyException e) { + // Includes KeyPermanentlyInvalidatedException. + resetPlainKey(); + return false; + } catch (Throwable t) { + Log.e(t); + return false; + } + } + + @Override + public String get(String account) { + if (account == null) { + return null; + } + if (Build.VERSION.SDK_INT < 23) { + return legacyPlainGet(account); + } + String stored = plainPrefs().getString(account, null); + if (stored == null) { + return null; + } + int sep = stored.indexOf(':'); + if (sep < 0) { + return null; + } + try { + SecretKey key = plainKey(false); + if (key == null) { + return null; + } + byte[] iv = Base64.decode(stored.substring(0, sep), Base64.NO_WRAP); + byte[] enc = Base64.decode(stored.substring(sep + 1), Base64.NO_WRAP); + Cipher c = Cipher.getInstance("AES/GCM/NoPadding"); + c.init(Cipher.DECRYPT_MODE, key, new GCMParameterSpec(GCM_TAG_BITS, iv)); + return new String(c.doFinal(enc), "UTF-8"); + } catch (InvalidKeyException e) { + // The key was invalidated out from under us (device-wide credential + // change, or the Samsung 8.0.0 quirk documented on the biometric + // tier). Everything encrypted under it is unrecoverable, so drop + // the key and the ciphertexts rather than failing forever. + resetPlainKey(); + return null; + } catch (UnrecoverableKeyException e) { + resetPlainKey(); + return null; + } catch (Throwable t) { + Log.e(t); + return null; + } + } + + @Override + public boolean remove(String account) { + if (account == null) { + return false; + } + plainPrefs().edit().remove(account).apply(); + return true; + } + + private SharedPreferences plainPrefs() { + return AndroidNativeUtil.getActivity() + .getApplicationContext() + .getSharedPreferences(PLAIN_PREFS, Context.MODE_PRIVATE); + } + + /** + * Loads the non-prompting keystore key, optionally creating it. Returns + * null when the key is absent and {@code create} is false, or when + * generation fails. + */ + private SecretKey plainKey(boolean create) throws Exception { + keyStore().load(null); + SecretKey existing = (SecretKey) keyStore.getKey(PLAIN_KEY_ID, null); + if (existing != null || !create) { + return existing; + } + KeyGenerator gen = KeyGenerator.getInstance( + KeyProperties.KEY_ALGORITHM_AES, ANDROID_KEY_STORE); + gen.init(new KeyGenParameterSpec.Builder(PLAIN_KEY_ID, + KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT) + .setBlockModes(KeyProperties.BLOCK_MODE_GCM) + .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE) + .setKeySize(256) + .setRandomizedEncryptionRequired(true) + .build()); + gen.generateKey(); + return (SecretKey) keyStore.getKey(PLAIN_KEY_ID, null); + } + + private void resetPlainKey() { + try { + keyStore().deleteEntry(PLAIN_KEY_ID); + } catch (KeyStoreException e) { + Log.e(e); + } + plainPrefs().edit().clear().apply(); + } + + // API 22 and below have no KeyGenParameterSpec. The preferences file is + // still app-private, but the value is only obfuscated, not encrypted -- + // it is extractable from a rooted device or a backup. + private boolean legacyPlainSet(String account, String value) { + warnLegacyPlainStorage(); + try { + plainPrefs().edit() + .putString(account, Base64.encodeToString( + value.getBytes("UTF-8"), Base64.NO_WRAP)) + .apply(); + return true; + } catch (IOException e) { + Log.e(e); + return false; + } + } + + private String legacyPlainGet(String account) { + warnLegacyPlainStorage(); + String stored = plainPrefs().getString(account, null); + if (stored == null) { + return null; + } + try { + return new String(Base64.decode(stored, Base64.NO_WRAP), "UTF-8"); + } catch (IOException e) { + Log.e(e); + return null; + } + } + + private boolean legacyPlainWarned; + + private void warnLegacyPlainStorage() { + if (!legacyPlainWarned) { + legacyPlainWarned = true; + Log.p("SecureStorage: this device predates Android API 23, so the " + + "non-prompting tier stores values obfuscated rather than " + + "encrypted. Do not use it for high-value secrets here."); + } + } + /** * Generic helper that initialises the cipher under the keystore key, * prompts the user via {@code BiometricPrompt} (or legacy diff --git a/Ports/Android/src/com/codename1/impl/android/AndroidWebSocketImpl.java b/Ports/Android/src/com/codename1/impl/android/AndroidWebSocketImpl.java index b844a6d7715..55ff0b54b60 100644 --- a/Ports/Android/src/com/codename1/impl/android/AndroidWebSocketImpl.java +++ b/Ports/Android/src/com/codename1/impl/android/AndroidWebSocketImpl.java @@ -162,6 +162,7 @@ private void doHandshake(int connectTimeoutMs) throws IOException { } req.append("\r\n"); } + appendRequestHeaders(req); req.append("\r\n"); out.write(req.toString().getBytes(ASCII)); out.flush(); diff --git a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEPort.java b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEPort.java index 357cf751e1d..33fd9ac3787 100644 --- a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEPort.java +++ b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEPort.java @@ -6539,6 +6539,8 @@ public void actionPerformed(ActionEvent ae) { simulateMenu.add(biometricMenu); + final JMenu shieldMenu = installShieldSimulationMenu(simulateMenu, pref); + final JMenu nfcMenu = installNfcSimulationMenu(simulateMenu, pref); final JMenu foldableMenu = installFoldableSimulationMenu(simulateMenu, pref); @@ -7056,6 +7058,7 @@ public void actionPerformed(ActionEvent e) { simulateMenu.add(motionSim); simulateMenu.add(pushSim); simulateMenu.add(biometricMenu); + simulateMenu.add(shieldMenu); simulateMenu.add(nfcMenu); simulateMenu.add(foldableMenu); simulateMenu.add(statusBarTapDiag); @@ -8316,6 +8319,163 @@ public void run() { }); } + /** + * Builds the {@code Simulate > App Shield} menu. + * + *

Every toggle here exists to make an otherwise untestable branch + * reachable on a developer's desktop. "Force Pin Mismatch On Next Request" + * is the most valuable of them: a fail-closed pinning branch is otherwise + * only exercisable by deliberately mis-pinning a live host.

+ */ + private JMenu installShieldSimulationMenu(JMenu simulateMenu, final Preferences pref) { + JMenu shieldMenu = new JMenu("App Shield"); + shieldMenu.setToolTipText("Simulate attestation outcomes, compromised-device signals " + + "and certificate pin failures."); + + final JCheckBoxMenuItem supported = new JCheckBoxMenuItem("Attestation Supported", + pref.getBoolean("ShieldSim.supported", true)); + JavaSEShield.attestationSupported = supported.isSelected(); + supported.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent ae) { + JavaSEShield.attestationSupported = supported.isSelected(); + pref.putBoolean("ShieldSim.supported", supported.isSelected()); + } + }); + shieldMenu.add(supported); + + JMenu outcomeMenu = new JMenu("Attestation Result"); + ButtonGroup outcomeGroup = new ButtonGroup(); + String storedOutcome = pref.get("ShieldSim.outcome", + JavaSEShield.AttestOutcome.PASS.name()); + for (final JavaSEShield.AttestOutcome outcome : JavaSEShield.AttestOutcome.values()) { + JRadioButtonMenuItem item = new JRadioButtonMenuItem(outcome.name()); + if (outcome.name().equals(storedOutcome)) { + item.setSelected(true); + JavaSEShield.attestOutcome = outcome; + } + outcomeGroup.add(item); + item.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent ae) { + JavaSEShield.attestOutcome = outcome; + pref.put("ShieldSim.outcome", outcome.name()); + } + }); + outcomeMenu.add(item); + } + shieldMenu.add(outcomeMenu); + + shieldMenu.addSeparator(); + + // The signals a compromised device would report. Independent toggles + // because an app's response to a rooted device and to a hooking + // framework are usually different decisions. + shieldMenu.add(shieldToggle(pref, "Rooted / Jailbroken", "ShieldSim.rooted", + new ShieldToggleSink() { + @Override + public void set(boolean v) { + JavaSEShield.simRooted = v; + } + })); + shieldMenu.add(shieldToggle(pref, "Hooking Framework (Frida)", "ShieldSim.hooked", + new ShieldToggleSink() { + @Override + public void set(boolean v) { + JavaSEShield.simHooked = v; + } + })); + shieldMenu.add(shieldToggle(pref, "Emulator", "ShieldSim.emulator", + new ShieldToggleSink() { + @Override + public void set(boolean v) { + JavaSEShield.simEmulator = v; + } + })); + shieldMenu.add(shieldToggle(pref, "Debugger Attached", "ShieldSim.debugger", + new ShieldToggleSink() { + @Override + public void set(boolean v) { + JavaSEShield.simDebugger = v; + } + })); + shieldMenu.add(shieldToggle(pref, "Repackaged", "ShieldSim.repackaged", + new ShieldToggleSink() { + @Override + public void set(boolean v) { + JavaSEShield.simRepackaged = v; + } + })); + shieldMenu.add(shieldToggle(pref, "Untrusted Accessibility Service", + "ShieldSim.accessibility", new ShieldToggleSink() { + @Override + public void set(boolean v) { + JavaSEShield.simUntrustedAccessibility = v; + } + })); + + shieldMenu.addSeparator(); + + shieldMenu.add(shieldToggle(pref, "Serve Expired Token", "ShieldSim.expiredToken", + new ShieldToggleSink() { + @Override + public void set(boolean v) { + JavaSEShield.serveExpiredToken = v; + } + })); + + // The one branch that is otherwise effectively untestable. + shieldMenu.add(shieldToggle(pref, "Force Pin Mismatch On Next Request", + "ShieldSim.pinMismatch", new ShieldToggleSink() { + @Override + public void set(boolean v) { + JavaSEShield.forcePinMismatch = v; + } + })); + shieldMenu.add(shieldToggle(pref, "Fail Pin Fetch", "ShieldSim.pinFetchFail", + new ShieldToggleSink() { + @Override + public void set(boolean v) { + JavaSEShield.failPinFetch = v; + } + })); + + shieldMenu.addSeparator(); + + JMenuItem status = new JMenuItem("Show Shield Status..."); + status.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent ae) { + JOptionPane.showMessageDialog(canvas, JavaSEShield.describe(), + "App Shield Simulation", JOptionPane.INFORMATION_MESSAGE); + } + }); + shieldMenu.add(status); + + simulateMenu.add(shieldMenu); + return shieldMenu; + } + + /** Lambda stand-in so the toggle wiring is written once rather than nine times. */ + private interface ShieldToggleSink { + void set(boolean value); + } + + private JCheckBoxMenuItem shieldToggle(final Preferences pref, String label, + final String prefKey, final ShieldToggleSink sink) { + final JCheckBoxMenuItem item = new JCheckBoxMenuItem(label, + pref.getBoolean(prefKey, false)); + sink.set(item.isSelected()); + item.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent ae) { + sink.set(item.isSelected()); + pref.putBoolean(prefKey, item.isSelected()); + } + }); + return item; + } + private JMenu installNfcSimulationMenu(JMenu simulateMenu, final Preferences pref) { JMenu nfcMenu = new JMenu("NFC"); @@ -14254,9 +14414,47 @@ public String[] getSSLCertificates(Object connection, String url) throws IOExcep public boolean canGetSSLCertificates() { return true; } - - - + + @Override + public boolean canGetPublicKeyDigests() { + return true; + } + + @Override + public String[] getSSLCertificatesEx(Object connection, String url) throws IOException { + if (connection instanceof HttpsURLConnection) { + HttpsURLConnection conn = (HttpsURLConnection) connection; + try { + conn.connect(); + java.security.cert.Certificate[] certs = conn.getServerCertificates(); + java.util.List out = new java.util.ArrayList(); + for (int i = 0; i < certs.length; i++) { + java.security.cert.Certificate cert = certs[i]; + out.add("CHAIN:" + i); + MessageDigest sha256 = MessageDigest.getInstance("SHA-256"); + sha256.update(cert.getEncoded()); + out.add("SHA-256:" + dumpHex(sha256.digest())); + MessageDigest sha1 = MessageDigest.getInstance("SHA1"); + sha1.update(cert.getEncoded()); + out.add("SHA1:" + dumpHex(sha1.digest())); + // getPublicKey().getEncoded() is already the DER SubjectPublicKeyInfo, + // which is exactly what a public-key pin is computed over. + java.security.PublicKey pk = cert.getPublicKey(); + if (pk != null && pk.getEncoded() != null) { + MessageDigest spki = MessageDigest.getInstance("SHA-256"); + spki.update(pk.getEncoded()); + out.add("SPKI-SHA-256:" + + com.codename1.util.Base64.encodeNoNewline(spki.digest())); + } + } + return out.toArray(new String[out.size()]); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + return new String[0]; + } + /** * @inheritDoc */ @@ -18797,6 +18995,81 @@ public boolean isJailbrokenDevice() { return super.isJailbrokenDevice(); } + // --- DeviceIntegrity / App Shield simulation -------------------------- + // + // Without these the simulator reports "no attestation, clean device" for + // everything, so the branches an app takes when a device looks compromised + // are unreachable until it is on real hardware. Driven by the + // Simulate > App Shield menu; see JavaSEShield. + + @Override + public boolean isAttestationSupported() { + return JavaSEShield.attestationSupported; + } + + @Override + public com.codename1.util.AsyncResource requestIntegrityToken(String nonce) { + final com.codename1.util.AsyncResource result = + new com.codename1.util.AsyncResource(); + if (!JavaSEShield.attestationSupported + || JavaSEShield.attestOutcome == JavaSEShield.AttestOutcome.UNSUPPORTED) { + result.error(new UnsupportedOperationException( + "Simulated: attestation is not supported on this device")); + return result; + } + switch (JavaSEShield.attestOutcome) { + case PASS: + // Stamped as simulated so it cannot be mistaken for, or accepted + // as, a real attestation by any backend. + result.complete("cn1sim:attest:" + (nonce == null ? "" : nonce)); + break; + case FAIL_REJECTED: + result.error(new RuntimeException( + "Simulated: the attestation service rejected this device")); + break; + case FAIL_NO_NETWORK: + result.error(new java.io.IOException("Simulated: no network")); + break; + case FAIL_SERVICE_DOWN: + result.error(new java.io.IOException( + "Simulated: the attestation service is unavailable")); + break; + case FAIL_RATE_LIMITED: + result.error(new RuntimeException("Simulated: rate limited, back off")); + break; + default: + result.error(new RuntimeException("Simulated attestation failure")); + } + return result; + } + + @Override + public void resetAttestation() { + // Nothing is cached in the simulator; the menu is the state. + } + + @Override + public boolean isDeviceCompromised() { + return JavaSEShield.simReasons().length > 0; + } + + @Override + public String[] getCompromiseReasons() { + return JavaSEShield.simReasons(); + } + + @Override + public String[] getEnabledAccessibilityServices() { + return JavaSEShield.simAccessibility(); + } + + @Override + public void setSecureScreen(boolean secure) { + // No OS-level equivalent on the desktop. Recorded so the menu can show + // whether the app asked for it, which is what a developer is checking. + JavaSEShield.secureScreen = secure; + } + @Override public Boolean canExecute(String url) { // If this is a registered simulator hook URL, report it as diff --git a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSESecureStorage.java b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSESecureStorage.java index 4c175eca4a6c42be7ad3ba926bb52277ff307dd1..608602e72d7751c6581b80cb57cfebd4a4c34eea 100644 GIT binary patch literal 8027 zcmcIp?^7E&61~s-6}|ql!^Vt(+f8jHBozZDctdQLZIYv|wy5z~#SaCo)~D=w@!n3wP;o|KPBRXH$^`3qk!vP>@Vp zQc+|y1+!TcM}ZOAr*<5Zou^3&E!0X(eYSVnr(SI&{(>Tn)SL z&&T|>d(s*4yYYEfx($TFD!k@IwYCI>m~)7?W8cCBfj&2f03)3QJD^Mm1 zmvNv7NCI0;dO7(+gocc?#v3PbpmiFUd4t54P^4^`$E8ZHqN$j&OK_yGf)ECx#Ll_^ zqPPtnhFy{ zu_iA74k;L-vDGkM0?&NU4XDoS)f=#y6O+vp(zZ}pR5WkHu*dtj^si>Si+7{ zQayiko~h5C@l8)gybm!lQUQiX%tq3`kT;LKT$(8M+j^aZL!sqTg^yXO4P3^iM3#~E z3qYMmL(~JgDAu$?tc7C)<>k-y$zS|Xty3fYlPH~w5>K90yn|gO%m=Zq8GZQr3`~^j zkx@Doq5uBm;~-GrAryav2ipzWp@ZWNR{X}G!ZSB+r}dMS&r!61gu@q_fTa4GAOAMGZ@yARzZvmL%C5aXcQ#sw@)9ravPMSJnN6r zw`0g_B9lGm3h&KCD5DU}Be8fZC$pt?3Dsm-BAsR1Q44Q<>O%Pw8*=LzR|BkcE7nk8 zWu zoysBz0j{m6{4BJ5GvbS(y*;!CYt%sOMJ!NkS^%S8*>98i#IEWIS&0Es+ zhnz415Z_xNQfd#nyeG62sC071j-eIP$aBc1`~d0NV*L(42v(I zcD%-ez_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(sKi;jYs#mZqyOgIH9NK(ni*3=8xa+FVxDr0cW$oy^efZ1EYFCmGA-^bo zLo>|h-)W6-hPR)R%Zq$3#|A13Ae;3IvL<0YY^Vt1w7Q?1+8LvYo$$rKm$?K)&{kDl M-kKNpJNG;P0eC;WFaQ7m delta 47 zcmV+~0MP&2KA|75!UdDx1w4~72i%hu3Z9c13?q{W3>cGI3>}k~462hx6DqU54f_TH FeG0825Uc App Shield} menu. + * + *

Exists so a developer can exercise the branches an app takes when a device + * looks compromised or attestation fails. Those are exactly the paths that are + * otherwise impossible to test: rooting a phone, attaching a hooking framework + * or deliberately mis-pinning a real host are all things nobody does casually, + * so without simulation the error handling ships untested and is discovered by + * users.

+ * + *

Mirrors {@link JavaSEBiometrics}: plain static fields driven by menu + * checkboxes and persisted to preferences, deliberately with no logic of its + * own.

+ */ +public final class JavaSEShield { + + private JavaSEShield() { + } + + // --- attestation ------------------------------------------------------ + + /** What the next attestation request should do. */ + public enum AttestOutcome { + /** Return a simulated token. */ + PASS, + /** The service evaluated the device and said no. Not retryable. */ + FAIL_REJECTED, + /** No connectivity. Retryable. */ + FAIL_NO_NETWORK, + /** The service returned an error. Retryable. */ + FAIL_SERVICE_DOWN, + /** The device is being throttled. Retryable after backoff. */ + FAIL_RATE_LIMITED, + /** The platform has no attestation at all. */ + UNSUPPORTED + } + + public static AttestOutcome attestOutcome = AttestOutcome.PASS; + + /** Whether the simulated platform reports attestation support. */ + public static boolean attestationSupported = true; + + // --- device signals --------------------------------------------------- + + public static boolean simRooted; + public static boolean simHooked; + public static boolean simEmulator; + public static boolean simDebugger; + public static boolean simRepackaged; + public static boolean simUntrustedAccessibility; + + // --- token ------------------------------------------------------------ + + /** Simulated token lifetime. */ + public static int tokenTtlSeconds = 300; + + /** Hand out a token that has already lapsed, to exercise refresh handling. */ + public static boolean serveExpiredToken; + + // --- pinning ---------------------------------------------------------- + + /** + * Fail the next certificate check. + * + *

The most useful switch here by a distance: it is the only practical way + * to test a fail-closed branch, short of deliberately mis-pinning a live + * host and waiting for the request to break.

+ */ + public static boolean forcePinMismatch; + + /** Simulate being unable to fetch a pin set. Must never fail a request. */ + public static boolean failPinFetch; + + /** True when the window is displaying a screen marked secure. */ + public static boolean secureScreen; + + /** The compromise reasons the simulated device reports. */ + public static String[] simReasons() { + List out = new ArrayList(); + if (simRooted) { + out.add("root"); + } + if (simHooked) { + out.add("frida"); + } + if (simEmulator) { + out.add("emulator"); + } + if (simDebugger) { + out.add("debugger"); + } + if (simRepackaged) { + out.add("repackaged"); + } + return out.toArray(new String[out.size()]); + } + + /** The accessibility services the simulated device reports as enabled. */ + public static String[] simAccessibility() { + if (!simUntrustedAccessibility) { + return new String[0]; + } + return new String[] {"com.example.malware/.OverlayService"}; + } + + /** Resets every toggle. Used by the menu's reset item and by tests. */ + public static void reset() { + attestOutcome = AttestOutcome.PASS; + attestationSupported = true; + simRooted = false; + simHooked = false; + simEmulator = false; + simDebugger = false; + simRepackaged = false; + simUntrustedAccessibility = false; + tokenTtlSeconds = 300; + serveExpiredToken = false; + forcePinMismatch = false; + failPinFetch = false; + } + + /** A human-readable dump for the menu's status dialog. */ + public static String describe() { + StringBuilder sb = new StringBuilder(); + sb.append("Attestation outcome: ").append(attestOutcome).append('\n'); + sb.append("Attestation supported: ").append(attestationSupported).append('\n'); + String[] reasons = simReasons(); + sb.append("Device signals: ") + .append(reasons.length == 0 ? "(none)" : String.join(", ", reasons)).append('\n'); + sb.append("Accessibility: ") + .append(simUntrustedAccessibility ? "untrusted service enabled" : "clean").append('\n'); + sb.append("Token TTL: ").append(tokenTtlSeconds).append("s") + .append(serveExpiredToken ? " (serving expired)" : "").append('\n'); + sb.append("Pinning: ") + .append(forcePinMismatch ? "forcing mismatch" : "normal") + .append(failPinFetch ? ", pin fetch failing" : "").append('\n'); + sb.append("Secure screen: ").append(secureScreen).append('\n'); + return sb.toString(); + } +} diff --git a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEWebSocketImpl.java b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEWebSocketImpl.java index ed50c455a46..4c21f646eba 100644 --- a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEWebSocketImpl.java +++ b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEWebSocketImpl.java @@ -163,6 +163,7 @@ private void doHandshake(int connectTimeoutMs) throws IOException { } req.append("\r\n"); } + appendRequestHeaders(req); req.append("\r\n"); out.write(req.toString().getBytes(StandardCharsets.ISO_8859_1)); out.flush(); diff --git a/Ports/LinuxPort/src/com/codename1/impl/linux/LinuxWebSocketImpl.java b/Ports/LinuxPort/src/com/codename1/impl/linux/LinuxWebSocketImpl.java index a2fe4e80f36..f1d0e8a412e 100644 --- a/Ports/LinuxPort/src/com/codename1/impl/linux/LinuxWebSocketImpl.java +++ b/Ports/LinuxPort/src/com/codename1/impl/linux/LinuxWebSocketImpl.java @@ -137,6 +137,7 @@ private void doHandshake(int connectTimeoutMs) throws IOException { req.append("Connection: Upgrade\r\n"); req.append("Sec-WebSocket-Key: ").append(key).append("\r\n"); req.append("Sec-WebSocket-Version: 13\r\n"); + appendRequestHeaders(req); req.append("\r\n"); out.write(bytes(req.toString())); out.flush(); diff --git a/Ports/WindowsPort/src/com/codename1/impl/windows/WindowsWebSocketImpl.java b/Ports/WindowsPort/src/com/codename1/impl/windows/WindowsWebSocketImpl.java index 921caa72a6a..3aacd07582f 100644 --- a/Ports/WindowsPort/src/com/codename1/impl/windows/WindowsWebSocketImpl.java +++ b/Ports/WindowsPort/src/com/codename1/impl/windows/WindowsWebSocketImpl.java @@ -152,6 +152,7 @@ private void doHandshake(int connectTimeoutMs) throws IOException { req.append("Connection: Upgrade\r\n"); req.append("Sec-WebSocket-Key: ").append(key).append("\r\n"); req.append("Sec-WebSocket-Version: 13\r\n"); + appendRequestHeaders(req); req.append("\r\n"); out.write(bytes(req.toString())); out.flush(); diff --git a/Ports/iOSPort/nativeSources/CN1JailbreakDetector.h b/Ports/iOSPort/nativeSources/CN1JailbreakDetector.h index 96fa9e7ba05..5468066b864 100644 --- a/Ports/iOSPort/nativeSources/CN1JailbreakDetector.h +++ b/Ports/iOSPort/nativeSources/CN1JailbreakDetector.h @@ -20,7 +20,25 @@ * Please contact Codename One through http://www.codenameone.com/ if you * need additional information or have any questions. */ +#import + //#define CN1_DETECT_JAILBREAK 1 + +/** + * Runs every jailbreak / hooking probe and returns the ones that fired as a + * comma separated list of stable codes, or an empty string on a clean device. + * Codes: dyldInsert, hookLib, jailbreakFile, restrictedWrite, traced. + * + * Always compiled, independent of CN1_DETECT_JAILBREAK, because + * DeviceIntegrity.getCompromiseReasons() surfaces these at runtime without + * terminating the app. Returns an empty string on the simulator. + */ +NSString *cn1JailbreakSignals(void); + #ifdef CN1_DETECT_JAILBREAK -void cn1DetectJailbreakBypassesAndExit(); +/** + * Legacy hard gate kept for the ios.detectJailbreak build hint: runs + * cn1JailbreakSignals() and terminates the process if anything fired. + */ +void cn1DetectJailbreakBypassesAndExit(void); #endif diff --git a/Ports/iOSPort/nativeSources/CN1JailbreakDetector.m b/Ports/iOSPort/nativeSources/CN1JailbreakDetector.m index 1dfee47b79f..d748ab6fa51 100644 --- a/Ports/iOSPort/nativeSources/CN1JailbreakDetector.m +++ b/Ports/iOSPort/nativeSources/CN1JailbreakDetector.m @@ -22,25 +22,31 @@ */ #import "CN1JailbreakDetector.h" -#ifdef CN1_DETECT_JAILBREAK -#import +#import #import #import #import #import #import -void cn1DetectJailbreakBypassesAndExit() { +// Note: there is deliberately no fork() probe here. The classic form, +// "if (fork() == 0) { exit(0); }", terminates the *child* and lets the parent +// sail on, so it never did what it claimed. Reinstating it correctly would +// mean calling a restricted syscall that trips App Review static analysis, for +// a signal the dyld-image and restricted-path probes already carry. + +NSString *cn1JailbreakSignals(void) { #if (TARGET_IPHONE_SIMULATOR) - return; -#endif - // Detect common dynamic library injection used by Frida/Objection and similar tools + return @""; +#else + NSMutableArray *signals = [NSMutableArray array]; + + // Dynamic library injection, as used by Frida/Objection and friends. if (getenv("DYLD_INSERT_LIBRARIES") != NULL) { - NSLog(@"DYLD_INSERT_LIBRARIES detected."); - exit(0); + [signals addObject:@"dyldInsert"]; } - // List of known libraries used by bypass tools like Liberty Lite and Substrate + // Known hooking / jailbreak-bypass libraries loaded into the process. NSArray *bypassLibraries = @[ @"LibertyLite.dylib", @"Substrate.dylib", @@ -49,23 +55,22 @@ void cn1DetectJailbreakBypassesAndExit() { @"tsProtector.dylib", @"FridaGadget" ]; - - // Check all loaded dynamic libraries - for (int i = 0; i < _dyld_image_count(); i++) { + for (uint32_t i = 0; i < _dyld_image_count(); i++) { const char *imageName = _dyld_get_image_name(i); + if (imageName == NULL) { + continue; + } NSString *libraryName = [NSString stringWithUTF8String:imageName]; - - // Check if the library name matches any known bypass tool libraries for (NSString *bypassLibrary in bypassLibraries) { if ([libraryName containsString:bypassLibrary]) { - // Jailbreak bypass detected, exit the app - NSLog(@"Bypass library detected: %@", bypassLibrary); - exit(0); // Exit the app if a bypass tool is detected + [signals addObject:@"hookLib"]; + i = _dyld_image_count(); + break; } } } - - // Additional check for file access to system areas (indicates potential bypass) + + // Files that only exist once the sandbox has been broken out of. NSArray *restrictedPaths = @[ @"/Applications/Cydia.app", @"/Library/MobileSubstrate/MobileSubstrate.dylib", @@ -74,45 +79,43 @@ void cn1DetectJailbreakBypassesAndExit() { @"/etc/apt", @"/private/var/lib/apt/" ]; - NSFileManager *fileManager = [NSFileManager defaultManager]; for (NSString *path in restrictedPaths) { if ([fileManager fileExistsAtPath:path]) { - // Jailbreak files detected, exit the app - NSLog(@"Jailbreak-related file detected: %@", path); - exit(0); // Exit the app if a jailbreak-related file is found + [signals addObject:@"jailbreakFile"]; + break; } } - - // Check if we can write to a restricted area (bypasses may allow this) - NSString *testPath = @"/private/jailbreakTest.txt"; - NSError *error; - BOOL wroteFile = [@"Test" writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:&error]; - if (wroteFile && !error) { + + // Writing outside the sandbox should be impossible. + NSString *testPath = @"/private/cn1JailbreakTest.txt"; + NSError *error = nil; + BOOL wroteFile = [@"Test" writeToFile:testPath atomically:YES + encoding:NSUTF8StringEncoding error:&error]; + if (wroteFile && error == nil) { [fileManager removeItemAtPath:testPath error:nil]; - // Able to write to restricted area, exit the app - NSLog(@"Write access to restricted area detected."); - exit(0); // Exit the app if write access to restricted areas is detected - } - - // Check for abnormal system behavior like successful fork() - if (fork() == 0) { - // fork() should not succeed on non-jailbroken devices, exit if it does - NSLog(@"Fork succeeded, indicating jailbreak bypass."); - exit(0); // Exit the app if fork() succeeds + [signals addObject:@"restrictedWrite"]; } - - // Check for process tracing (which could indicate Liberty Lite tampering) + + // A debugger or instrumentation tool attached to the process. struct kinfo_proc info; size_t size = sizeof(info); int name[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()}; - if (sysctl(name, 4, &info, &size, NULL, 0) == 0 && (info.kp_proc.p_flag & P_TRACED) != 0) { - // Process is being traced, likely due to a jailbreak bypass - NSLog(@"Process tracing detected, indicating jailbreak bypass."); - exit(0); // Exit the app if process tracing is detected + if (sysctl(name, 4, &info, &size, NULL, 0) == 0 + && (info.kp_proc.p_flag & P_TRACED) != 0) { + [signals addObject:@"traced"]; + } + + return [signals componentsJoinedByString:@","]; +#endif +} + +#ifdef CN1_DETECT_JAILBREAK +void cn1DetectJailbreakBypassesAndExit(void) { + NSString *signals = cn1JailbreakSignals(); + if (signals.length > 0) { + NSLog(@"Jailbreak bypass detected: %@", signals); + exit(0); } - - // If no jailbreak bypass was detected, the app continues as normal - NSLog(@"No jailbreak bypass detected."); } #endif diff --git a/Ports/iOSPort/nativeSources/IOSNative.m b/Ports/iOSPort/nativeSources/IOSNative.m index ef60c5babc3..5593a06c931 100644 --- a/Ports/iOSPort/nativeSources/IOSNative.m +++ b/Ports/iOSPort/nativeSources/IOSNative.m @@ -28,6 +28,7 @@ #include "xmlvm.h" #include "java_lang_String.h" #import "CN1ES2compat.h" +#import "CN1JailbreakDetector.h" #if TARGET_OS_WATCH #import "CN1CGGraphics.h" #import "CN1WatchHost.h" @@ -14572,7 +14573,7 @@ void com_codename1_impl_ios_IOSNative_stopBiometricAuthentication__(CN1_THREAD_S #import #import -JAVA_BOOLEAN com_codename1_impl_ios_IOSNative_isAppAttestSupported__(CN1_THREAD_STATE_MULTI_ARG JAVA_OBJECT me) { +JAVA_BOOLEAN com_codename1_impl_ios_IOSNative_isAppAttestSupported___R_boolean(CN1_THREAD_STATE_MULTI_ARG JAVA_OBJECT me) { #if !TARGET_OS_TV && !TARGET_OS_WATCH if (@available(iOS 14.0, *)) { if (NSClassFromString(@"DCAppAttestService") == NULL) { @@ -14586,66 +14587,150 @@ JAVA_BOOLEAN com_codename1_impl_ios_IOSNative_isAppAttestSupported__(CN1_THREAD_ #endif // !TARGET_OS_TV && !TARGET_OS_WATCH } -void com_codename1_impl_ios_IOSNative_requestAppAttestToken___int_java_lang_String(CN1_THREAD_STATE_MULTI_ARG JAVA_OBJECT me, JAVA_INT requestId, JAVA_OBJECT nonce) { +// Reports a failure back to Java. errorCode carries the raw DCError value so the +// Java side can tell "the key is invalid, throw it away and re-attest" (2) from +// "Apple is throttling us, back off" (4) -- treating those the same is how an +// app burns its attestation budget in a retry loop. +static void cn1AppAttestFail(JAVA_INT requestId, NSError *err, NSString *fallback) { + NSString *m = err != nil ? err.localizedDescription : fallback; + JAVA_INT code = err != nil ? (JAVA_INT)err.code : -1; + JAVA_OBJECT jmsg = fromNSString(getThreadLocalData(), m); + com_codename1_impl_ios_IOSDeviceIntegrity_nativeAttestError___int_int_java_lang_String(getThreadLocalData(), requestId, code, jmsg); +} + +// DCAppAttestService completion handlers run on an arbitrary dispatch queue. +// Hopping to the main queue means every re-entry into the VM comes from a known +// thread, matching what the biometrics block above does. +#define CN1_APP_ATTEST_ON_MAIN(block) dispatch_async(dispatch_get_main_queue(), block) + +void com_codename1_impl_ios_IOSNative_appAttestGenerateKey___int(CN1_THREAD_STATE_MULTI_ARG JAVA_OBJECT me, JAVA_INT requestId) { #if !TARGET_OS_TV && !TARGET_OS_WATCH POOL_BEGIN(); if (@available(iOS 14.0, *)) { DCAppAttestService *service = [DCAppAttestService sharedService]; if (!service.isSupported) { - JAVA_OBJECT jmsg = fromNSString(getThreadLocalData(), @"App Attest not supported"); - com_codename1_impl_ios_IOSDeviceIntegrity_nativeAttestError___int_java_lang_String(getThreadLocalData(), requestId, jmsg); + cn1AppAttestFail(requestId, nil, @"App Attest not supported"); POOL_END(); return; } - NSString *nsNonce = (nonce == JAVA_NULL) ? @"" : toNSString(CN1_THREAD_STATE_PASS_ARG nonce); - NSData *nonceData = [nsNonce dataUsingEncoding:NSUTF8StringEncoding]; - unsigned char hashBytes[CC_SHA256_DIGEST_LENGTH]; - CC_SHA256(nonceData.bytes, (CC_LONG)nonceData.length, hashBytes); - NSData *clientDataHash = [NSData dataWithBytes:hashBytes length:CC_SHA256_DIGEST_LENGTH]; [service generateKeyWithCompletionHandler:^(NSString *keyId, NSError *genErr) { - if (genErr != nil || keyId == nil) { - NSString *m = genErr ? genErr.localizedDescription : @"App Attest key generation failed"; - JAVA_OBJECT jmsg = fromNSString(getThreadLocalData(), m); - com_codename1_impl_ios_IOSDeviceIntegrity_nativeAttestError___int_java_lang_String(getThreadLocalData(), requestId, jmsg); - return; - } - [service attestKey:keyId clientDataHash:clientDataHash completionHandler:^(NSData *attestationObject, NSError *attErr) { + CN1_APP_ATTEST_ON_MAIN(^{ + if (genErr != nil || keyId == nil) { + cn1AppAttestFail(requestId, genErr, @"App Attest key generation failed"); + return; + } + JAVA_OBJECT jkey = fromNSString(getThreadLocalData(), keyId); + com_codename1_impl_ios_IOSDeviceIntegrity_nativeKeyGenerated___int_java_lang_String(getThreadLocalData(), requestId, jkey); + }); + }]; + } else { + cn1AppAttestFail(requestId, nil, @"App Attest requires iOS 14+"); + } + POOL_END(); +#else + cn1AppAttestFail(requestId, nil, @"App Attest not available on this platform"); +#endif // !TARGET_OS_TV && !TARGET_OS_WATCH +} + +void com_codename1_impl_ios_IOSNative_appAttestAttestKey___int_java_lang_String_java_lang_String(CN1_THREAD_STATE_MULTI_ARG JAVA_OBJECT me, JAVA_INT requestId, JAVA_OBJECT keyId, JAVA_OBJECT clientDataHashB64) { +#if !TARGET_OS_TV && !TARGET_OS_WATCH + POOL_BEGIN(); + if (@available(iOS 14.0, *)) { + DCAppAttestService *service = [DCAppAttestService sharedService]; + NSString *nsKeyId = (keyId == JAVA_NULL) ? nil : toNSString(CN1_THREAD_STATE_PASS_ARG keyId); + NSString *nsHash = (clientDataHashB64 == JAVA_NULL) ? nil : toNSString(CN1_THREAD_STATE_PASS_ARG clientDataHashB64); + NSData *clientDataHash = nsHash == nil ? nil + : [[NSData alloc] initWithBase64EncodedString:nsHash options:0]; + if (nsKeyId == nil || clientDataHash == nil) { + cn1AppAttestFail(requestId, nil, @"App Attest attestation missing key or hash"); + POOL_END(); + return; + } + [service attestKey:nsKeyId clientDataHash:clientDataHash completionHandler:^(NSData *attestationObject, NSError *attErr) { + CN1_APP_ATTEST_ON_MAIN(^{ if (attErr != nil || attestationObject == nil) { - NSString *m = attErr ? attErr.localizedDescription : @"App Attest attestation failed"; - JAVA_OBJECT jmsg = fromNSString(getThreadLocalData(), m); - com_codename1_impl_ios_IOSDeviceIntegrity_nativeAttestError___int_java_lang_String(getThreadLocalData(), requestId, jmsg); + cn1AppAttestFail(requestId, attErr, @"App Attest attestation failed"); return; } - NSData *keyIdData = [keyId dataUsingEncoding:NSUTF8StringEncoding]; - NSString *b64Key = [keyIdData base64EncodedStringWithOptions:0]; NSString *b64Att = [attestationObject base64EncodedStringWithOptions:0]; - NSString *token = [NSString stringWithFormat:@"%@:%@", b64Key, b64Att]; - JAVA_OBJECT jtoken = fromNSString(getThreadLocalData(), token); - com_codename1_impl_ios_IOSDeviceIntegrity_nativeAttestSuccess___int_java_lang_String(getThreadLocalData(), requestId, jtoken); - }]; + JAVA_OBJECT jatt = fromNSString(getThreadLocalData(), b64Att); + com_codename1_impl_ios_IOSDeviceIntegrity_nativeAttestationReady___int_java_lang_String(getThreadLocalData(), requestId, jatt); + }); + }]; + } else { + cn1AppAttestFail(requestId, nil, @"App Attest requires iOS 14+"); + } + POOL_END(); +#else + cn1AppAttestFail(requestId, nil, @"App Attest not available on this platform"); +#endif // !TARGET_OS_TV && !TARGET_OS_WATCH +} + +void com_codename1_impl_ios_IOSNative_appAttestGenerateAssertion___int_java_lang_String_java_lang_String(CN1_THREAD_STATE_MULTI_ARG JAVA_OBJECT me, JAVA_INT requestId, JAVA_OBJECT keyId, JAVA_OBJECT clientDataHashB64) { +#if !TARGET_OS_TV && !TARGET_OS_WATCH + POOL_BEGIN(); + if (@available(iOS 14.0, *)) { + DCAppAttestService *service = [DCAppAttestService sharedService]; + NSString *nsKeyId = (keyId == JAVA_NULL) ? nil : toNSString(CN1_THREAD_STATE_PASS_ARG keyId); + NSString *nsHash = (clientDataHashB64 == JAVA_NULL) ? nil : toNSString(CN1_THREAD_STATE_PASS_ARG clientDataHashB64); + NSData *clientDataHash = nsHash == nil ? nil + : [[NSData alloc] initWithBase64EncodedString:nsHash options:0]; + if (nsKeyId == nil || clientDataHash == nil) { + cn1AppAttestFail(requestId, nil, @"App Attest assertion missing key or hash"); + POOL_END(); + return; + } + [service generateAssertion:nsKeyId clientDataHash:clientDataHash completionHandler:^(NSData *assertion, NSError *assertErr) { + CN1_APP_ATTEST_ON_MAIN(^{ + if (assertErr != nil || assertion == nil) { + cn1AppAttestFail(requestId, assertErr, @"App Attest assertion failed"); + return; + } + NSString *b64 = [assertion base64EncodedStringWithOptions:0]; + JAVA_OBJECT jassert = fromNSString(getThreadLocalData(), b64); + com_codename1_impl_ios_IOSDeviceIntegrity_nativeAssertionReady___int_java_lang_String(getThreadLocalData(), requestId, jassert); + }); }]; } else { - JAVA_OBJECT jmsg = fromNSString(getThreadLocalData(), @"App Attest requires iOS 14+"); - com_codename1_impl_ios_IOSDeviceIntegrity_nativeAttestError___int_java_lang_String(getThreadLocalData(), requestId, jmsg); + cn1AppAttestFail(requestId, nil, @"App Attest requires iOS 14+"); } POOL_END(); #else - com_codename1_impl_ios_IOSDeviceIntegrity_nativeAttestError___int_java_lang_String(getThreadLocalData(), requestId, JAVA_NULL); + cn1AppAttestFail(requestId, nil, @"App Attest not available on this platform"); #endif // !TARGET_OS_TV && !TARGET_OS_WATCH } #else // CN1_USE_APP_ATTEST // App Attest not enabled (ios.appAttest build hint off): DeviceCheck.framework // is neither imported nor linked. Report unsupported / fail the request. -JAVA_BOOLEAN com_codename1_impl_ios_IOSNative_isAppAttestSupported__(CN1_THREAD_STATE_MULTI_ARG JAVA_OBJECT me) { +JAVA_BOOLEAN com_codename1_impl_ios_IOSNative_isAppAttestSupported___R_boolean(CN1_THREAD_STATE_MULTI_ARG JAVA_OBJECT me) { return JAVA_FALSE; } -void com_codename1_impl_ios_IOSNative_requestAppAttestToken___int_java_lang_String(CN1_THREAD_STATE_MULTI_ARG JAVA_OBJECT me, JAVA_INT requestId, JAVA_OBJECT nonce) { - com_codename1_impl_ios_IOSDeviceIntegrity_nativeAttestError___int_java_lang_String(getThreadLocalData(), requestId, JAVA_NULL); +void com_codename1_impl_ios_IOSNative_appAttestGenerateKey___int(CN1_THREAD_STATE_MULTI_ARG JAVA_OBJECT me, JAVA_INT requestId) { + com_codename1_impl_ios_IOSDeviceIntegrity_nativeAttestError___int_int_java_lang_String(getThreadLocalData(), requestId, -1, JAVA_NULL); +} + +void com_codename1_impl_ios_IOSNative_appAttestAttestKey___int_java_lang_String_java_lang_String(CN1_THREAD_STATE_MULTI_ARG JAVA_OBJECT me, JAVA_INT requestId, JAVA_OBJECT keyId, JAVA_OBJECT clientDataHashB64) { + com_codename1_impl_ios_IOSDeviceIntegrity_nativeAttestError___int_int_java_lang_String(getThreadLocalData(), requestId, -1, JAVA_NULL); +} + +void com_codename1_impl_ios_IOSNative_appAttestGenerateAssertion___int_java_lang_String_java_lang_String(CN1_THREAD_STATE_MULTI_ARG JAVA_OBJECT me, JAVA_INT requestId, JAVA_OBJECT keyId, JAVA_OBJECT clientDataHashB64) { + com_codename1_impl_ios_IOSDeviceIntegrity_nativeAttestError___int_int_java_lang_String(getThreadLocalData(), requestId, -1, JAVA_NULL); } #endif // CN1_USE_APP_ATTEST +// Jailbreak/instrumentation signals. Always compiled, independent of both +// CN1_USE_APP_ATTEST and CN1_DETECT_JAILBREAK, because DeviceIntegrity reports +// these at runtime without terminating the app. +JAVA_OBJECT com_codename1_impl_ios_IOSNative_iosJailbreakSignals___R_java_lang_String(CN1_THREAD_STATE_MULTI_ARG JAVA_OBJECT me) { + POOL_BEGIN(); + NSString *signals = cn1JailbreakSignals(); + JAVA_OBJECT result = fromNSString(CN1_THREAD_STATE_PASS_ARG (signals == nil ? @"" : signals)); + POOL_END(); + return result; +} + // --- CarPlay (CarPlay.framework) ------------------------------------------ // Gated by CN1_USE_CARPLAY: the builder uncomments the define, links // CarPlay.framework, injects the CarPlay scene into the Info.plist scene manifest diff --git a/Ports/iOSPort/nativeSources/NetworkConnectionImpl.m b/Ports/iOSPort/nativeSources/NetworkConnectionImpl.m index 192987c3314..d2bbba98741 100644 --- a/Ports/iOSPort/nativeSources/NetworkConnectionImpl.m +++ b/Ports/iOSPort/nativeSources/NetworkConnectionImpl.m @@ -178,11 +178,19 @@ -(void) connection: (NSURLConnection*)connection willSendRequestForAuthenticatio if (i>0) { [certs appendString:@","]; } + // CHAIN: opens each certificate's group; index 0 is the leaf. The + // Java side keeps this and the SPKI entry out of the legacy flat list + // so existing checkSSLCertificates overrides see unchanged data. + [certs appendFormat:@"CHAIN:%d,", i]; [certs appendString:@"SHA-256:"]; [certs appendString:[self getFingerprint256:certRef]]; [certs appendString:@",SHA1:"]; [certs appendString:[self getFingerprint:certRef]]; - + NSString* spki = [self getPublicKeyDigest:certRef]; + if (spki != nil) { + [certs appendString:@",SPKI-SHA-256:"]; + [certs appendString:spki]; + } } sslCertificates = [[NSString stringWithString:certs] retain]; if (com_codename1_io_NetworkManager_checkCertificatesNativeCallback___int_R_boolean(CN1_THREAD_GET_STATE_PASS_ARG connectionId)) { @@ -207,6 +215,110 @@ - (NSString*) getFingerprint: (SecCertificateRef) cert { return [fingerprint stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; } +/** + * Reads one DER TLV at `off`. Returns NO when the buffer is too short or the + * length encoding is one we do not handle (indefinite length, or a length that + * would not fit). `headerLen` is the tag plus length bytes, `totalLen` covers + * the whole TLV. + */ +static BOOL cn1ReadDerTlv(const uint8_t* buf, NSUInteger len, NSUInteger off, + uint8_t* tag, NSUInteger* headerLen, NSUInteger* totalLen) { + if (off + 2 > len) { + return NO; + } + *tag = buf[off]; + NSUInteger n = buf[off + 1]; + if (n < 0x80) { + *headerLen = 2; + *totalLen = 2 + n; + } else { + NSUInteger countBytes = n & 0x7f; + // 0x80 is indefinite length, which DER forbids; more than 4 length bytes + // would mean a certificate larger than anything we will ever be handed. + if (countBytes == 0 || countBytes > 4 || off + 2 + countBytes > len) { + return NO; + } + NSUInteger contentLen = 0; + for (NSUInteger i = 0; i < countBytes; i++) { + contentLen = (contentLen << 8) | buf[off + 2 + i]; + } + *headerLen = 2 + countBytes; + *totalLen = *headerLen + contentLen; + } + return off + *totalLen <= len; +} + +/** + * Base64 SHA-256 over the certificate's SubjectPublicKeyInfo, which is what a + * public-key pin is computed over. + * + * Walks the certificate DER rather than going through SecCertificateCopyKey + + * SecKeyCopyExternalRepresentation. That pair hands back the *raw* key, so + * reconstructing the SPKI means prepending a hand-maintained ASN.1 header chosen + * per key type and size -- a table that silently produces wrong digests for any + * key type it does not know about. The DER walk is algorithm-agnostic and + * matches `openssl x509 -pubkey | openssl pkey -pubin -outform der` exactly. + * + * Returns nil if the structure is not what we expect, in which case the caller + * simply omits the entry and pinning falls back to whole-certificate digests. + */ +- (NSString*) getPublicKeyDigest: (SecCertificateRef) cert { + // Plain CoreFoundation rather than a toll-free bridge cast: this file builds + // both with and without ARC, and the correct bridging annotation differs + // between the two. An explicit CFRelease is unambiguous in either mode. + CFDataRef certData = SecCertificateCopyData(cert); + if (certData == NULL) { + return nil; + } + const uint8_t* buf = CFDataGetBytePtr(certData); + NSUInteger len = (NSUInteger) CFDataGetLength(certData); + NSString* result = nil; + uint8_t tag; + NSUInteger headerLen, totalLen; + NSUInteger off; + int i; + + // Certificate ::= SEQUENCE { tbsCertificate, signatureAlgorithm, signature } + if (!cn1ReadDerTlv(buf, len, 0, &tag, &headerLen, &totalLen) || tag != 0x30) { + goto cleanup; + } + off = headerLen; + + // tbsCertificate ::= SEQUENCE { ... } + if (!cn1ReadDerTlv(buf, len, off, &tag, &headerLen, &totalLen) || tag != 0x30) { + goto cleanup; + } + off += headerLen; + + // [0] EXPLICIT Version is optional and absent in a v1 certificate. + if (!cn1ReadDerTlv(buf, len, off, &tag, &headerLen, &totalLen)) { + goto cleanup; + } + if (tag == 0xA0) { + off += totalLen; + } + + // Skip serialNumber, signature, issuer, validity, subject. The next element + // is subjectPublicKeyInfo. + for (i = 0; i < 5; i++) { + if (!cn1ReadDerTlv(buf, len, off, &tag, &headerLen, &totalLen)) { + goto cleanup; + } + off += totalLen; + } + + if (cn1ReadDerTlv(buf, len, off, &tag, &headerLen, &totalLen) && tag == 0x30) { + uint8_t digest[CC_SHA256_DIGEST_LENGTH]; + CC_SHA256(buf + off, (CC_LONG) totalLen, digest); + NSData* digestData = [NSData dataWithBytes:digest length:CC_SHA256_DIGEST_LENGTH]; + result = [digestData base64EncodedStringWithOptions:0]; + } + +cleanup: + CFRelease(certData); + return result; +} + - (NSString*) getFingerprint256: (SecCertificateRef) cert { NSData* keyData = (__bridge NSData*) SecCertificateCopyData(cert); diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index 541137161d2..89b836c70b2 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -22,8 +22,12 @@ */ package com.codename1.impl.ios; +import com.codename1.io.Log; +import com.codename1.security.Hash; +import com.codename1.security.SecureStorage; import com.codename1.ui.Display; import com.codename1.util.AsyncResource; +import com.codename1.util.Base64; import java.util.HashMap; import java.util.Map; @@ -32,34 +36,118 @@ * iOS backing for App Attest (DeviceCheck.framework), surfaced through * {@link com.codename1.security.DeviceIntegrity#requestIntegrityToken(String)}. * - *

The native side dispatches results back via the static - * {@link #nativeAttestSuccess(int, String)} / {@link #nativeAttestError(int, String)} - * methods. As with {@link IOSBiometrics}, the static initializer invokes each - * with no-op values so the ParparVM dead-code eliminator does not strip the - * native callback targets (no Java caller exists).

+ *

Attest once, assert many

+ * + *

Apple's model is: generate a Secure Enclave key once, attest it + * once so the server can record its public key, then produce cheap + * assertions against that key for every subsequent request. Key generation and + * attestation are both rate limited; assertions are not.

+ * + *

This class implements that state machine over three keychain entries in + * {@link IOSSecureStorage}'s non-prompting tier, so the identity survives app + * restarts and updates:

+ * + *
    + *
  • {@code cn1.appattest.keyId} -- the key identifier + *
  • {@code cn1.appattest.state} -- {@code new} or {@code attested} + *
  • {@code cn1.appattest.retryAfter} -- backoff deadline after a throttle + *
+ * + *

Token format

+ * + *

Tokens are prefixed so a backend can tell the two forms apart, which the + * bare {@code base64:base64} form could not:

+ * + *
+ * cn1aa1:attest:<b64 keyId>:<b64 attestationObject>
+ * cn1aa1:assert:<b64 keyId>:<b64 assertion>:<b64 clientData>
+ * 
+ * + *

The client data for an assertion is a small JSON object carrying the + * server nonce, so the server can recompute the hash the assertion signed.

+ * + *

ParparVM note

+ * + *

The native side dispatches results back through the static callbacks + * below. As with {@link IOSBiometrics}, the static initializer invokes each with + * no-op values so the dead-code eliminator does not strip them -- no Java caller + * exists, and without a reachable reference they become empty stubs and the + * native call silently does nothing.

*/ final class IOSDeviceIntegrity { static { // Prevents the iOS VM optimizer from eliding these native callbacks. - nativeAttestSuccess(-1, null); - nativeAttestError(-1, null); + nativeKeyGenerated(-1, null); + nativeAttestationReady(-1, null); + nativeAssertionReady(-1, null); + nativeAttestError(-1, -1, null); } - private static final Map> REQUESTS = - new HashMap>(); + static final String TOKEN_PREFIX = "cn1aa1"; + + private static final String KEY_ID = "cn1.appattest.keyId"; + private static final String KEY_STATE = "cn1.appattest.state"; + private static final String KEY_RETRY_AFTER = "cn1.appattest.retryAfter"; + + private static final String STATE_NEW = "new"; + private static final String STATE_ATTESTED = "attested"; + + /** DCError.invalidKey -- the server or the OS no longer knows this key. */ + private static final int DC_ERROR_INVALID_KEY = 2; + /** DCError.serverUnavailable -- Apple is throttling or unreachable. */ + private static final int DC_ERROR_SERVER_UNAVAILABLE = 4; + + private static final long MIN_BACKOFF_MILLIS = 30L * 1000L; + private static final long MAX_BACKOFF_MILLIS = 60L * 60L * 1000L; + + private static final Map REQUESTS = + new HashMap(); private static int nextRequestId = 1; + private static IOSDeviceIntegrity instance; + private final IOSNative nativeInstance; + /** Guards the whole attest flow so concurrent callers cannot each burn a key. */ + private final Object flowLock = new Object(); + private long currentBackoff = MIN_BACKOFF_MILLIS; IOSDeviceIntegrity(IOSNative nativeInstance) { this.nativeInstance = nativeInstance; + instance = this; } boolean isSupported() { return nativeInstance.isAppAttestSupported(); } + /** + * Discards the stored key so the next request attests afresh. Called when the + * backend reports it does not recognise the key -- after a reinstall, a + * restore to a new device, or an OS-side invalidation. + */ + void resetAttestation() { + SecureStorage store = SecureStorage.getInstance(); + store.remove(KEY_ID); + store.remove(KEY_STATE); + store.remove(KEY_RETRY_AFTER); + synchronized (flowLock) { + currentBackoff = MIN_BACKOFF_MILLIS; + } + } + + String[] jailbreakSignals() { + try { + String signals = nativeInstance.iosJailbreakSignals(); + if (signals == null || signals.length() == 0) { + return new String[0]; + } + return com.codename1.io.Util.split(signals, ","); + } catch (Throwable t) { + return new String[0]; + } + } + AsyncResource requestToken(String nonce) { AsyncResource r = new AsyncResource(); if (!nativeInstance.isAppAttestSupported()) { @@ -67,52 +155,252 @@ AsyncResource requestToken(String nonce) { "App Attest is not supported on this device")); return r; } - int rid; - synchronized (REQUESTS) { - rid = nextRequestId++; - REQUESTS.put(Integer.valueOf(rid), r); + if (nonce == null || nonce.length() == 0) { + r.error(new IllegalArgumentException( + "App Attest requires a server-issued nonce; a client-generated " + + "one is not replay-safe")); + return r; + } + synchronized (flowLock) { + long retryAfter = readRetryAfter(); + if (retryAfter > System.currentTimeMillis()) { + r.error(new RuntimeException("App Attest is backing off after a throttle; " + + "retry in " + ((retryAfter - System.currentTimeMillis()) / 1000) + + "s")); + return r; + } + SecureStorage store = SecureStorage.getInstance(); + String keyId = store.get(KEY_ID); + if (keyId == null || keyId.length() == 0) { + // No key yet: generate one, then continue into attestation. + int rid = register(new PendingRequest(r, nonce, PendingRequest.OP_GENERATE_KEY, null)); + nativeInstance.appAttestGenerateKey(rid); + return r; + } + if (STATE_ATTESTED.equals(store.get(KEY_STATE))) { + assertWithKey(r, nonce, keyId); + } else { + attestKey(r, nonce, keyId); + } } - nativeInstance.requestAppAttestToken(rid, nonce); return r; } + // --- flow steps ------------------------------------------------------ + + private void attestKey(AsyncResource r, String nonce, String keyId) { + // The attestation binds to SHA-256 of the raw nonce. Hashing here rather + // than natively keeps a single hash implementation across both paths. + String hash = base64(Hash.sha256(bytes(nonce))); + int rid = register(new PendingRequest(r, nonce, PendingRequest.OP_ATTEST, keyId)); + nativeInstance.appAttestAttestKey(rid, keyId, hash); + } + + private void assertWithKey(AsyncResource r, String nonce, String keyId) { + String clientData = clientDataJson(nonce, keyId); + String hash = base64(Hash.sha256(bytes(clientData))); + PendingRequest pending = + new PendingRequest(r, nonce, PendingRequest.OP_ASSERT, keyId); + pending.clientData = clientData; + int rid = register(pending); + nativeInstance.appAttestGenerateAssertion(rid, keyId, hash); + } + + /** + * Minimal JSON, hand-built rather than pulled from a parser: it has to be + * byte-identical to what the server recomputes the hash over, so its exact + * serialization is part of the wire contract. + */ + private static String clientDataJson(String nonce, String keyId) { + return "{\"n\":\"" + escapeJson(nonce) + "\",\"k\":\"" + escapeJson(keyId) + "\"}"; + } + + private static String escapeJson(String s) { + StringBuilder sb = new StringBuilder(s.length()); + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + if (c == '"' || c == '\\') { + sb.append('\\').append(c); + } else if (c < 0x20) { + sb.append(' '); + } else { + sb.append(c); + } + } + return sb.toString(); + } + // ---- Callbacks invoked from native code (do not rename) ---------------- - /** Called from native when attestation succeeds with the opaque token. */ - public static void nativeAttestSuccess(final int requestId, final String token) { - final AsyncResource r = take(requestId); - if (r == null) { + /** Called from native once a fresh hardware key exists. */ + public static void nativeKeyGenerated(final int requestId, final String keyId) { + PendingRequest pending = take(requestId); + if (pending == null || instance == null) { + return; + } + if (keyId == null || keyId.length() == 0) { + fail(pending, "App Attest key generation returned no identifier"); + return; + } + SecureStorage store = SecureStorage.getInstance(); + store.set(KEY_ID, keyId); + store.set(KEY_STATE, STATE_NEW); + synchronized (instance.flowLock) { + instance.attestKey(pending.result, pending.nonce, keyId); + } + } + + /** Called from native with the attestation object for a newly attested key. */ + public static void nativeAttestationReady(final int requestId, final String attestationB64) { + PendingRequest pending = take(requestId); + if (pending == null) { return; } + if (attestationB64 == null) { + fail(pending, "App Attest attestation returned no data"); + return; + } + // Optimistic: Apple accepted the attestation, but only the backend can + // confirm it recorded the key. If it later rejects, the app calls + // DeviceIntegrity.resetAttestation() and we start over. + SecureStorage store = SecureStorage.getInstance(); + store.set(KEY_STATE, STATE_ATTESTED); + if (instance != null) { + synchronized (instance.flowLock) { + instance.currentBackoff = MIN_BACKOFF_MILLIS; + } + } + succeed(pending, TOKEN_PREFIX + ":attest:" + base64(bytes(pending.keyId)) + + ":" + attestationB64); + } + + /** Called from native with an assertion over an already attested key. */ + public static void nativeAssertionReady(final int requestId, final String assertionB64) { + PendingRequest pending = take(requestId); + if (pending == null) { + return; + } + if (assertionB64 == null) { + fail(pending, "App Attest assertion returned no data"); + return; + } + succeed(pending, TOKEN_PREFIX + ":assert:" + base64(bytes(pending.keyId)) + + ":" + assertionB64 + + ":" + base64(bytes(pending.clientData))); + } + + /** Called from native when any step fails. errorCode is the raw DCError code. */ + public static void nativeAttestError(final int requestId, final int errorCode, + final String msg) { + PendingRequest pending = take(requestId); + if (pending == null) { + return; + } + if (errorCode == DC_ERROR_INVALID_KEY && instance != null && !pending.retried) { + // The key is gone or was never valid. Wipe it and try once from + // scratch; a second failure is reported rather than looped. + instance.resetAttestation(); + synchronized (instance.flowLock) { + PendingRequest retry = new PendingRequest(pending.result, pending.nonce, + PendingRequest.OP_GENERATE_KEY, null); + retry.retried = true; + int rid = register(retry); + instance.nativeInstance.appAttestGenerateKey(rid); + } + return; + } + if (errorCode == DC_ERROR_SERVER_UNAVAILABLE && instance != null) { + // Never retry a throttle in a loop -- that is what gets an app's + // whole attestation budget suspended. + synchronized (instance.flowLock) { + long backoff = instance.currentBackoff; + SecureStorage.getInstance().set(KEY_RETRY_AFTER, + Long.toString(System.currentTimeMillis() + backoff)); + instance.currentBackoff = Math.min(backoff * 2, MAX_BACKOFF_MILLIS); + } + } + fail(pending, msg == null ? "App Attest failed (code " + errorCode + ")" : msg); + } + + // --- helpers --------------------------------------------------------- + + private static long readRetryAfter() { + try { + String v = SecureStorage.getInstance().get(KEY_RETRY_AFTER); + return v == null ? 0 : Long.parseLong(v); + } catch (Throwable t) { + return 0; + } + } + + private static int register(PendingRequest pending) { + synchronized (REQUESTS) { + int rid = nextRequestId++; + REQUESTS.put(Integer.valueOf(rid), pending); + return rid; + } + } + + private static PendingRequest take(int requestId) { + synchronized (REQUESTS) { + return REQUESTS.remove(Integer.valueOf(requestId)); + } + } + + private static void succeed(final PendingRequest pending, final String token) { Display.getInstance().callSerially(new Runnable() { - @Override public void run() { - if (!r.isDone()) { - r.complete(token); + if (!pending.result.isDone()) { + pending.result.complete(token); } } }); } - /** Called from native when key generation or attestation fails. */ - public static void nativeAttestError(final int requestId, final String msg) { - final AsyncResource r = take(requestId); - if (r == null) { - return; - } + private static void fail(final PendingRequest pending, final String msg) { Display.getInstance().callSerially(new Runnable() { - @Override public void run() { - if (!r.isDone()) { - r.error(new RuntimeException(msg == null ? "App Attest failed" : msg)); + if (!pending.result.isDone()) { + pending.result.error(new RuntimeException(msg)); } } }); } - private static AsyncResource take(int requestId) { - synchronized (REQUESTS) { - return REQUESTS.remove(Integer.valueOf(requestId)); + private static byte[] bytes(String s) { + if (s == null) { + return new byte[0]; + } + try { + return s.getBytes("UTF-8"); + } catch (java.io.UnsupportedEncodingException e) { + Log.e(e); + return s.getBytes(); + } + } + + private static String base64(byte[] data) { + return Base64.encodeNoNewline(data); + } + + /** One in-flight native operation and the state needed to finish or resume it. */ + private static final class PendingRequest { + static final int OP_GENERATE_KEY = 0; + static final int OP_ATTEST = 1; + static final int OP_ASSERT = 2; + + final AsyncResource result; + final String nonce; + final int op; + final String keyId; + String clientData; + boolean retried; + + PendingRequest(AsyncResource result, String nonce, int op, String keyId) { + this.result = result; + this.nonce = nonce; + this.op = op; + this.keyId = keyId; } } } diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSImplementation.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSImplementation.java index dc327326e4b..b990a7ead15 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSImplementation.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSImplementation.java @@ -4572,10 +4572,19 @@ public boolean isAttestationSupported() { @Override public com.codename1.util.AsyncResource requestIntegrityToken(String nonce) { + return deviceIntegrity().requestToken(nonce); + } + + @Override + public void resetAttestation() { + deviceIntegrity().resetAttestation(); + } + + private IOSDeviceIntegrity deviceIntegrity() { if (deviceIntegrity == null) { deviceIntegrity = new IOSDeviceIntegrity(nativeInstance); } - return deviceIntegrity.requestToken(nonce); + return deviceIntegrity; } @Override @@ -9821,7 +9830,36 @@ public Object connect(String url, boolean read, boolean write) throws IOExceptio public String[] getSSLCertificates(Object connection, String url) throws IOException { NetworkConnection conn = (NetworkConnection)connection; //conn.ensureConnection(); - return conn.getSSLCertificates(url); + return stripExtendedCertificateEntries(conn.getSSLCertificates(url)); + } + + /** + * The native side always emits the chain-grouping and public-key entries, + * because the certificate string is built once during the TLS handshake and + * cannot be regenerated on demand. Callers of the legacy flat form must not + * see them, or an existing checkSSLCertificates override that rejects on any + * unrecognised entry would start failing every request. + */ + private static String[] stripExtendedCertificateEntries(String[] entries) { + if (entries == null || entries.length == 0) { + return new String[0]; + } + java.util.ArrayList out = new java.util.ArrayList(entries.length); + for (int i = 0; i < entries.length; i++) { + String e = entries[i]; + if (e == null || e.startsWith("CHAIN:") || e.startsWith("SPKI-SHA-256:")) { + continue; + } + out.add(e); + } + return out.toArray(new String[out.size()]); + } + + @Override + public String[] getSSLCertificatesEx(Object connection, String url) throws IOException { + NetworkConnection conn = (NetworkConnection)connection; + String[] certs = conn.getSSLCertificates(url); + return certs == null ? new String[0] : certs; } @Override @@ -9829,6 +9867,11 @@ public boolean canGetSSLCertificates() { return true; } + @Override + public boolean canGetPublicKeyDigests() { + return true; + } + /** * Checking SSL certificates uses a native callback, instead of the direct approach * which is used in other ports. @@ -12518,10 +12561,53 @@ public boolean isPolygon() { @Override public boolean isJailbrokenDevice() { + if (getCompromiseReasons().length > 0) { + return true; + } + // Kept as a last resort, but note it only ever returns true when the app + // also declares cydia in ios.applicationQueriesSchemes -- on iOS 9 and up + // canOpenURL returns false for undeclared schemes regardless of what is + // installed. That is why it cannot be the primary signal. Boolean b = canExecute("cydia://package/com.example.package"); return b != null && b.booleanValue(); } + /** + * Real jailbreak and instrumentation signals, from the same native probes the + * {@code ios.detectJailbreak} launch gate uses -- but reported rather than + * fatal, so an app can degrade gracefully instead of being killed at launch. + */ + @Override + public String[] getCompromiseReasons() { + String[] signals = deviceIntegrity().jailbreakSignals(); + if (signals.length == 0) { + return signals; + } + java.util.ArrayList out = new java.util.ArrayList(); + boolean jailbreakReported = false; + for (int i = 0; i < signals.length; i++) { + String s = signals[i]; + if ("hookLib".equals(s) || "dyldInsert".equals(s)) { + if (!out.contains("frida")) { + // Reported under the cross-platform name for a hooking + // framework so app code does not need a per-platform branch. + out.add("frida"); + } + } else if ("traced".equals(s)) { + out.add("debugger"); + } else if (!jailbreakReported) { + jailbreakReported = true; + out.add("jailbreak"); + } + } + return out.toArray(new String[out.size()]); + } + + @Override + public boolean isDeviceCompromised() { + return getCompromiseReasons().length > 0; + } + @Override public void announceForAccessibility(final Component cmp, final String text) { IOSNative.announceForAccessibility(text); diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSNative.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSNative.java index b8477540459..4104cd1bbb0 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSNative.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSNative.java @@ -1009,14 +1009,39 @@ native void walletExtensionAddPassEntry(boolean remote, String identifier, Strin native boolean isAppAttestSupported(); /** - * Generates/uses an App Attest hardware key and produces an attestation bound - * to the SHA-256 of {@code nonce}. Native code calls back into - * {@code IOSDeviceIntegrity.nativeAttestSuccess(int, String)} or - * {@code IOSDeviceIntegrity.nativeAttestError(int, String)} with the same - * requestId. The success token is {@code base64(keyId):base64(attestationObject)} - * for the backend to verify with Apple. + * Generates a fresh App Attest hardware key. Calls back into + * {@code IOSDeviceIntegrity.nativeKeyGenerated(int, String)} with the key + * identifier, or {@code nativeAttestError(int, int, String)} on failure. + * + *

Apple rate-limits key generation, so this must happen once per install + * and the identifier must be persisted -- not once per request.

*/ - native void requestAppAttestToken(int requestId, String nonce); + native void appAttestGenerateKey(int requestId); + + /** + * Attests a previously generated key against a server challenge. Calls back + * into {@code IOSDeviceIntegrity.nativeAttestationReady(int, String)} with a + * base64 attestation object for the backend to verify with Apple. + * + * @param clientDataHashB64 base64 of the SHA-256 the attestation binds to, + * computed on the Java side so the attest and assert paths cannot + * disagree about what was hashed + */ + native void appAttestAttestKey(int requestId, String keyId, String clientDataHashB64); + + /** + * Produces an assertion over a previously attested key -- the cheap, + * unlimited operation that every request after the first should use. Calls + * back into {@code IOSDeviceIntegrity.nativeAssertionReady(int, String)}. + */ + native void appAttestGenerateAssertion(int requestId, String keyId, String clientDataHashB64); + + /** + * Comma separated jailbreak/hooking signal codes observed on this device, or + * an empty string when clean. Unlike the {@code ios.detectJailbreak} launch + * gate this never terminates the app. + */ + native String iosJailbreakSignals(); // --- CarPlay (CarPlay.framework) ---------------------------------------- // All gated natively by CN1_USE_CARPLAY (the build flips it on when the app references diff --git a/maven/core-unittests/src/test/java/com/codename1/io/SSLCertificateChainParsingTest.java b/maven/core-unittests/src/test/java/com/codename1/io/SSLCertificateChainParsingTest.java new file mode 100644 index 00000000000..55e82a76d31 --- /dev/null +++ b/maven/core-unittests/src/test/java/com/codename1/io/SSLCertificateChainParsingTest.java @@ -0,0 +1,145 @@ +/* + * Copyright (c) 2026, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ +package com.codename1.io; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Parsing of the richer, per-certificate form of the platform certificate list. + * Getting the grouping wrong would attribute a public key to the wrong + * certificate, which is a pin that silently checks the wrong thing. + */ +class SSLCertificateChainParsingTest { + + @Test + void groupsEntriesByChainDelimiter() { + ConnectionRequest.SSLCertificate[] certs = ConnectionRequest.parseGroupedCertificates( + new String[] { + "CHAIN:0", + "SHA-256:leafcert", + "SHA1:leafsha1", + "SPKI-SHA-256:leafspki", + "CHAIN:1", + "SHA-256:issuercert", + "SHA1:issuersha1", + "SPKI-SHA-256:issuerspki" + }); + + assertEquals(2, certs.length); + + assertEquals(0, certs[0].getChainIndex()); + assertTrue(certs[0].isLeaf()); + assertEquals("leafcert", certs[0].getCertificteUniqueKey()); + assertEquals("SHA-256", certs[0].getCertificteAlgorithm()); + assertEquals("leafspki", certs[0].getPublicKeyDigest()); + assertEquals("SHA-256", certs[0].getPublicKeyDigestAlgorithm()); + + assertEquals(1, certs[1].getChainIndex()); + assertFalse(certs[1].isLeaf()); + assertEquals("issuercert", certs[1].getCertificteUniqueKey()); + assertEquals("issuerspki", certs[1].getPublicKeyDigest()); + } + + @Test + void nonTypoAliasesReturnTheSameValues() { + ConnectionRequest.SSLCertificate[] certs = ConnectionRequest.parseGroupedCertificates( + new String[] {"CHAIN:0", "SHA-256:abc"}); + assertEquals(certs[0].getCertificteUniqueKey(), certs[0].getFingerprint()); + assertEquals(certs[0].getCertificteAlgorithm(), certs[0].getFingerprintAlgorithm()); + } + + /** + * A port may report digests without grouping. Treat what arrives as the leaf + * rather than dropping it. + */ + @Test + void entriesBeforeAnyDelimiterBecomeTheLeaf() { + ConnectionRequest.SSLCertificate[] certs = ConnectionRequest.parseGroupedCertificates( + new String[] {"SHA-256:abc", "SPKI-SHA-256:def"}); + assertEquals(1, certs.length); + assertEquals(0, certs[0].getChainIndex()); + assertEquals("abc", certs[0].getCertificteUniqueKey()); + assertEquals("def", certs[0].getPublicKeyDigest()); + } + + @Test + void certificateWithoutASpkiEntryReportsNullRatherThanGuessing() { + ConnectionRequest.SSLCertificate[] certs = ConnectionRequest.parseGroupedCertificates( + new String[] {"CHAIN:0", "SHA-256:abc", "SHA1:def"}); + assertEquals(1, certs.length); + assertNull(certs[0].getPublicKeyDigest()); + assertNull(certs[0].getPublicKeyDigestAlgorithm()); + } + + /** + * The first fingerprint wins, so the SHA1 entry that follows SHA-256 does not + * overwrite it. Pinning a SHA1 fingerprint by accident would be a downgrade. + */ + @Test + void firstFingerprintWinsWithinACertificateGroup() { + ConnectionRequest.SSLCertificate[] certs = ConnectionRequest.parseGroupedCertificates( + new String[] {"CHAIN:0", "SHA-256:strong", "SHA1:weak"}); + assertEquals("SHA-256", certs[0].getCertificteAlgorithm()); + assertEquals("strong", certs[0].getCertificteUniqueKey()); + } + + @Test + void malformedEntriesAreSkippedWithoutFailingTheChain() { + ConnectionRequest.SSLCertificate[] certs = ConnectionRequest.parseGroupedCertificates( + new String[] {null, "no-colon-here", "CHAIN:0", "SHA-256:abc"}); + assertEquals(1, certs.length); + assertEquals("abc", certs[0].getCertificteUniqueKey()); + } + + @Test + void unparseableChainIndexFallsBackToPositionalOrder() { + ConnectionRequest.SSLCertificate[] certs = ConnectionRequest.parseGroupedCertificates( + new String[] {"CHAIN:x", "SHA-256:a", "CHAIN:y", "SHA-256:b"}); + assertEquals(2, certs.length); + assertEquals(0, certs[0].getChainIndex()); + assertEquals(1, certs[1].getChainIndex()); + } + + @Test + void emptyInputYieldsEmptyChain() { + assertEquals(0, ConnectionRequest.parseGroupedCertificates(new String[0]).length); + } + + /** + * Base64 digests contain + / and = but never a comma, which matters because + * the iOS port ships the whole chain as one comma-joined string. + */ + @Test + void base64DigestsSurviveTheCommaJoinedTransport() { + String spki = "o+c2M5zOnK96U55rTfy2G9krOHRxnMwJ3esntXNFMdc="; + assertFalse(spki.contains(",")); + ConnectionRequest.SSLCertificate[] certs = ConnectionRequest.parseGroupedCertificates( + new String[] {"CHAIN:0", "SPKI-SHA-256:" + spki}); + assertEquals(spki, certs[0].getPublicKeyDigest()); + } +} diff --git a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java new file mode 100644 index 00000000000..a9994ff384b --- /dev/null +++ b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java @@ -0,0 +1,282 @@ +/* + * Copyright (c) 2026, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ +package com.codename1.security.shield; + +import org.junit.jupiter.api.Test; + +import java.util.Hashtable; +import java.util.Vector; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Covers the pure value types of the shield API. The behaviours asserted here are + * the ones an app relies on to decide whether to block a user, so a regression in + * any of them is a production incident rather than a cosmetic bug. + */ +class ShieldApiTest { + + // --- ShieldStatus --------------------------------------------------- + + @Test + void onlyOkIsSuccess() { + assertTrue(ShieldStatus.OK.isSuccess()); + assertFalse(ShieldStatus.UNPROTECTED.isSuccess()); + assertFalse(ShieldStatus.REJECTED.isSuccess()); + assertFalse(ShieldStatus.PIN_MISMATCH.isSuccess()); + } + + /** + * The distinction the whole API is built around: a service we could not reach + * is retryable, a device the service refused is not. + */ + @Test + void reachabilityFailuresAreTransientButRejectionIsNot() { + assertTrue(ShieldStatus.NO_NETWORK.isTransient()); + assertTrue(ShieldStatus.POOR_NETWORK.isTransient()); + assertTrue(ShieldStatus.SERVICE_DOWN.isTransient()); + assertTrue(ShieldStatus.RATE_LIMITED.isTransient()); + + assertFalse(ShieldStatus.REJECTED.isTransient()); + assertFalse(ShieldStatus.PIN_MISMATCH.isTransient()); + assertFalse(ShieldStatus.UNPROTECTED.isTransient()); + assertFalse(ShieldStatus.OK.isTransient()); + } + + @Test + void unknownStatusIdRoundTripsAsANonSuccess() { + ShieldStatus s = ShieldStatus.forId("somethingThisBuildPredates"); + assertEquals("somethingThisBuildPredates", s.getId()); + assertFalse(s.isSuccess()); + assertSame(ShieldStatus.RATE_LIMITED, ShieldStatus.forId("rateLimited")); + assertSame(ShieldStatus.NOT_INITIALIZED, ShieldStatus.forId(null)); + } + + // --- ShieldToken ---------------------------------------------------- + + @Test + void tokenIsValidOnlyWhileItHasValueSuccessAndTime() { + long now = System.currentTimeMillis(); + assertTrue(new ShieldToken("t", ShieldStatus.OK, now, 60000, null).isValid()); + assertFalse(new ShieldToken(null, ShieldStatus.OK, now, 60000, null).isValid()); + assertFalse(new ShieldToken("t", ShieldStatus.REJECTED, now, 60000, null).isValid()); + assertFalse(new ShieldToken("t", ShieldStatus.OK, now - 120000, 60000, null).isValid()); + } + + @Test + void expiredTokenReportsZeroRatherThanNegativeRemainingTime() { + ShieldToken t = new ShieldToken("t", ShieldStatus.OK, + System.currentTimeMillis() - 120000, 60000, null); + assertEquals(0, t.getMillisUntilExpiry()); + } + + @Test + void refreshTriggersOnceThresholdShareOfLifetimeIsUsed() { + long now = System.currentTimeMillis(); + // 10% used, 50% threshold -> no refresh yet. + assertFalse(new ShieldToken("t", ShieldStatus.OK, now - 1000, 10000, null) + .shouldRefresh(50)); + // 60% used -> refresh. + assertTrue(new ShieldToken("t", ShieldStatus.OK, now - 6000, 10000, null) + .shouldRefresh(50)); + } + + @Test + void aBoundTokenIsNotReusableForOtherRequests() { + ShieldToken bound = new ShieldToken("t", ShieldStatus.OK, + System.currentTimeMillis(), 60000, "digest-a"); + assertTrue(bound.isBoundTo("digest-a")); + assertFalse(bound.isBoundTo("digest-b")); + assertFalse(bound.isBoundTo(null)); + + ShieldToken unbound = new ShieldToken("t", ShieldStatus.OK, + System.currentTimeMillis(), 60000, null); + assertTrue(unbound.isBoundTo(null)); + assertFalse(unbound.isBoundTo("digest-a")); + } + + @Test + void tokenToStringNeverRendersTheTokenValue() { + String s = new ShieldToken("super-secret-token-value", ShieldStatus.OK, + System.currentTimeMillis(), 60000, null).toString(); + assertFalse(s.contains("super-secret-token-value"), + "token values must not be loggable via toString"); + } + + // --- HostPolicy / ShieldConfig -------------------------------------- + + @Test + void unregisteredHostsAreLeftCompletelyAlone() { + ShieldConfig c = new ShieldConfig().protect("api.example.com"); + assertSame(HostPolicy.UNPROTECTED, c.policyFor("cdn.other.com")); + assertTrue(HostPolicy.UNPROTECTED.isNoOp()); + assertSame(HostPolicy.UNPROTECTED, c.policyFor(null)); + } + + @Test + void wildcardCoversSubdomainsButNotTheApexOrSiblings() { + ShieldConfig c = new ShieldConfig().protect("*.example.com", HostPolicy.ENFORCED); + assertSame(HostPolicy.ENFORCED, c.policyFor("api.example.com")); + assertSame(HostPolicy.ENFORCED, c.policyFor("a.b.example.com")); + assertSame(HostPolicy.UNPROTECTED, c.policyFor("example.com")); + assertSame(HostPolicy.UNPROTECTED, c.policyFor("example.com.evil.test")); + } + + @Test + void exactHostWinsOverWildcard() { + ShieldConfig c = new ShieldConfig() + .protect("*.example.com", HostPolicy.PROTECTED) + .protect("secure.example.com", HostPolicy.ENFORCED); + assertSame(HostPolicy.ENFORCED, c.policyFor("secure.example.com")); + assertSame(HostPolicy.PROTECTED, c.policyFor("other.example.com")); + } + + @Test + void hostMatchingIsCaseInsensitive() { + ShieldConfig c = new ShieldConfig().protect("API.Example.COM"); + assertSame(HostPolicy.PROTECTED, c.policyFor("api.example.com")); + } + + @Test + void defaultsAreOpenAndNonBlocking() { + ShieldConfig c = new ShieldConfig(); + assertEquals(FailureMode.OPEN, c.getDefaultFailureMode()); + assertEquals(ShieldConfig.DEFAULT_TOKEN_HEADER, c.getTokenHeader()); + assertFalse(c.hasProtectedHosts()); + assertTrue(c.isCollectSignals()); + assertEquals(FailureMode.OPEN, HostPolicy.PROTECTED.getFailureMode()); + } + + // --- PinSet --------------------------------------------------------- + + private static PinSet pinSet(String host, String pin, long soft, long hard) { + Hashtable t = new Hashtable(); + Vector v = new Vector(); + v.addElement(pin); + t.put(host, v); + return new PinSet(t, 1, soft, hard); + } + + /** + * The never-brick rule. An unpinned host must report a match, or every request + * to it would start failing the moment pinning was switched on anywhere. + */ + @Test + void unpinnedHostAlwaysMatches() { + PinSet set = pinSet("api.example.com", "AAA", 0, 0); + assertTrue(set.matches("other.example.com", new String[] {"ZZZ"})); + assertTrue(PinSet.EMPTY.matches("api.example.com", new String[] {"ZZZ"})); + assertFalse(PinSet.EMPTY.isEnforcedFor("api.example.com")); + } + + @Test + void pinnedHostMatchesAnywhereInTheChain() { + PinSet set = pinSet("api.example.com", "INTERMEDIATE", 0, 0); + assertTrue(set.matches("api.example.com", + new String[] {"LEAF", "INTERMEDIATE", "ROOT"})); + } + + @Test + void pinnedHostWithNoMatchingChainEntryFails() { + PinSet set = pinSet("api.example.com", "AAA", 0, 0); + assertFalse(set.matches("api.example.com", new String[] {"BBB", "CCC"})); + assertFalse(set.matches("api.example.com", new String[0])); + assertFalse(set.matches("api.example.com", null)); + } + + /** + * A device that cannot reach the service for a long time must lose pinning + * rather than lose the app. + */ + @Test + void hardExpiredPinSetStopsEnforcingEntirely() { + long past = System.currentTimeMillis() - 1000; + PinSet expired = pinSet("api.example.com", "AAA", past, past); + assertTrue(expired.isExpired()); + assertFalse(expired.isEnforcedFor("api.example.com")); + assertTrue(expired.matches("api.example.com", new String[] {"WRONG"})); + } + + @Test + void staleButNotExpiredPinSetKeepsEnforcing() { + long now = System.currentTimeMillis(); + PinSet stale = pinSet("api.example.com", "AAA", now - 1000, now + 600000); + assertTrue(stale.isStale()); + assertFalse(stale.isExpired()); + assertTrue(stale.isEnforcedFor("api.example.com")); + assertFalse(stale.matches("api.example.com", new String[] {"WRONG"})); + } + + @Test + void pinWildcardsFollowTheSameRulesAsHostPolicies() { + PinSet set = pinSet("*.example.com", "AAA", 0, 0); + assertTrue(set.isEnforcedFor("api.example.com")); + assertFalse(set.isEnforcedFor("example.com")); + assertNull(set.pinsFor("other.test")); + } + + // --- ShieldSignals -------------------------------------------------- + + @Test + void repeatedSignalsOfTheSameKindCollapseInsteadOfAccumulating() { + ShieldSignals.clear(); + for (int i = 0; i < 500; i++) { + ShieldSignals.add(ShieldSignal.HOOK, 90, "attempt " + i); + } + ShieldSignal[] snapshot = ShieldSignals.snapshot(); + assertEquals(1, snapshot.length, + "a detector firing every frame must not be able to grow the bus"); + assertEquals("attempt 499", snapshot[0].getDetail()); + ShieldSignals.clear(); + } + + @Test + void signalBusIsBoundedAcrossDistinctIds() { + ShieldSignals.clear(); + for (int i = 0; i < 200; i++) { + ShieldSignals.add("signal-" + i, 10, null); + } + assertTrue(ShieldSignals.snapshot().length <= 32); + ShieldSignals.clear(); + } + + @Test + void severityIsClampedToTheDocumentedRange() { + assertEquals(100, new ShieldSignal("x", 5000, null).getSeverity()); + assertEquals(0, new ShieldSignal("x", -5, null).getSeverity()); + } + + @Test + void hasSignalAtLeastReflectsRecordedSeverities() { + ShieldSignals.clear(); + assertFalse(ShieldSignals.hasSignalAtLeast(1)); + ShieldSignals.add(ShieldSignal.EMULATOR, 30, null); + assertTrue(ShieldSignals.hasSignalAtLeast(30)); + assertFalse(ShieldSignals.hasSignalAtLeast(31)); + ShieldSignals.clear(); + } +} From 19abeb5e33268c2827e5394ddb05338a7a46123a Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Wed, 29 Jul 2026 10:39:13 +0300 Subject: [PATCH 02/96] Carry the shield status by id so ShieldException stays serializable SpotBugs SE_BAD_FIELD: IOException is serializable, so a non-serializable ShieldStatus field on ShieldException is both a static-analysis error and a latent null after a round trip -- which would break getStatus()'s never-null contract at exactly the moment someone is trying to work out why a request failed. Stores the id instead. ShieldStatus.forId resolves it back to the canonical constant, so identity comparisons and isTransient() still hold; tests pin that. Co-Authored-By: Claude Opus 5 (1M context) --- .../security/shield/ShieldException.java | 14 +++++++++--- .../security/shield/ShieldApiTest.java | 22 +++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldException.java b/CodenameOne/src/com/codename1/security/shield/ShieldException.java index a35b7dd3f2a..43070c41814 100644 --- a/CodenameOne/src/com/codename1/security/shield/ShieldException.java +++ b/CodenameOne/src/com/codename1/security/shield/ShieldException.java @@ -33,15 +33,23 @@ /// rejected", and those deserve very different UX. public class ShieldException extends IOException { - private final ShieldStatus status; + /// The status identifier rather than the [ShieldStatus] itself. + /// + /// `IOException` is serializable, and holding a non-serializable field on a + /// serializable class is both a static-analysis error and a latent null after a + /// round trip -- which would break [#getStatus()]'s never-null contract at exactly + /// the moment someone is trying to work out why a request failed. A `String` + /// survives serialization, and [ShieldStatus#forId(String)] resolves it back to the + /// canonical constant, so identity comparisons still hold. + private final String statusId; public ShieldException(ShieldStatus status, String message) { super(message); - this.status = status == null ? ShieldStatus.NOT_INITIALIZED : status; + this.statusId = (status == null ? ShieldStatus.NOT_INITIALIZED : status).getId(); } /// Why the operation failed. Never null. public ShieldStatus getStatus() { - return status; + return ShieldStatus.forId(statusId); } } diff --git a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java index a9994ff384b..2434736e3ad 100644 --- a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java +++ b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java @@ -76,6 +76,28 @@ void unknownStatusIdRoundTripsAsANonSuccess() { assertSame(ShieldStatus.NOT_INITIALIZED, ShieldStatus.forId(null)); } + /** + * ShieldException carries the status id rather than the object, because + * IOException is serializable. getStatus() must still hand back the + * canonical constant, or identity checks like isTransient() silently stop + * working. + */ + @Test + void exceptionStatusResolvesBackToTheCanonicalConstant() { + ShieldException e = new ShieldException(ShieldStatus.RATE_LIMITED, "slow down"); + assertSame(ShieldStatus.RATE_LIMITED, e.getStatus()); + assertTrue(e.getStatus().isTransient()); + + ShieldException pin = new ShieldException(ShieldStatus.PIN_MISMATCH, "bad chain"); + assertSame(ShieldStatus.PIN_MISMATCH, pin.getStatus()); + assertFalse(pin.getStatus().isTransient()); + } + + @Test + void exceptionStatusIsNeverNull() { + assertSame(ShieldStatus.NOT_INITIALIZED, new ShieldException(null, "x").getStatus()); + } + // --- ShieldToken ---------------------------------------------------- @Test From 6dc3c5eacbae38643f63ffddb1b515b4e23da361 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Wed, 29 Jul 2026 10:58:51 +0300 Subject: [PATCH 03/96] Fix App Attest error handling and guard ordering found in review DCError.invalidKey is 3, not 2 -- 2 is invalidInput. With the wrong constant an invalidated key never entered the reset-and-reattest branch, so a device whose key the OS had discarded would fail forever, while malformed input would pointlessly burn a fresh hardware key. Verified against the DeviceCheck header. Key generation is asynchronous, so holding the flow lock only until the native call was issued did not serialize anything: a second caller still saw no key and generated its own, spending a second key against Apple's per-device budget. Callers arriving mid-bootstrap now queue and assert against the key the first bootstrap establishes, which is unlimited. They are also released on failure -- otherwise they would wait forever. The retry marker was dropped when key generation handed off to attestation, so a recovery whose replacement key also reported invalidKey would recover again rather than surface the failure. The network guard ran before the offline-cache check, so a fail-closed guard could fail a request that needed no network at all by being unable to fetch a token while offline. Moved after the cache hit. Also: null-guard parseGroupedCertificates, since a port returning null would surface as an NPE on the network path rather than as an empty chain; reset secureScreen in the simulator's reset; and release the CFDataRef in getFingerprint/getFingerprint256, which leaked one certificate's worth of data per digest on every connection. Co-Authored-By: Claude Opus 5 (1M context) --- .../com/codename1/io/ConnectionRequest.java | 28 ++++--- .../codename1/impl/javase/JavaSEShield.java | 1 + .../nativeSources/NetworkConnectionImpl.m | 21 +++-- .../impl/ios/IOSDeviceIntegrity.java | 77 +++++++++++++++++-- 4 files changed, 106 insertions(+), 21 deletions(-) diff --git a/CodenameOne/src/com/codename1/io/ConnectionRequest.java b/CodenameOne/src/com/codename1/io/ConnectionRequest.java index 5559f46c1bd..1e2456cb253 100644 --- a/CodenameOne/src/com/codename1/io/ConnectionRequest.java +++ b/CodenameOne/src/com/codename1/io/ConnectionRequest.java @@ -954,15 +954,6 @@ boolean performOperationComplete() throws IOException { return true; } pinFailure = null; - NetworkGuard requestGuard = NetworkManager.getNetworkGuard(); - if (requestGuard != null) { - // Deliberately here rather than in initConnection(): that method is - // protected, and a subclass that overrides it without calling super - // would silently lose the decoration. Also deliberately not in - // NetworkThread.prepare(), which runs inside the queue lock where a - // blocking token fetch would stall every other request. - requestGuard.beforeRequest(this); - } if (cacheMode == CachingMode.OFFLINE || cacheMode == CachingMode.OFFLINE_FIRST) { InputStream is = null; //NOPMD CloseResource try { @@ -980,6 +971,20 @@ boolean performOperationComplete() throws IOException { Util.cleanup(is); } } + NetworkGuard requestGuard = NetworkManager.getNetworkGuard(); + if (requestGuard != null) { + // After the offline-cache check: a cache hit needs no network, so a + // fail-closed guard must not get the chance to block it by failing to + // fetch a token on a device that is offline. + // + // Deliberately here rather than in initConnection(): that method is + // protected, and a subclass that overrides it without calling super + // would silently lose the decoration. Also deliberately not in + // NetworkThread.prepare(), which runs inside the queue lock where a + // blocking token fetch would stall every other request. + requestGuard.beforeRequest(this); + } + CodenameOneImplementation impl = Util.getImplementation(); Object connection = null; input = null; @@ -1634,6 +1639,11 @@ public SSLCertificate[] getSSLCertificates() throws IOException { /// delimiter starting each certificate's group. Anything before the first delimiter is treated /// as the leaf, so a port that reports digests without grouping still yields usable data. static SSLCertificate[] parseGroupedCertificates(String[] entries) { + if (entries == null) { + // A port that returns null here would otherwise surface as an NPE on + // the network path, i.e. as an unrelated connection failure. + return new SSLCertificate[0]; + } java.util.Vector out = new java.util.Vector(); SSLCertificate current = null; int index = 0; diff --git a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEShield.java b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEShield.java index 5b1b35788f2..dc384fc8780 100644 --- a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEShield.java +++ b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEShield.java @@ -144,6 +144,7 @@ public static void reset() { serveExpiredToken = false; forcePinMismatch = false; failPinFetch = false; + secureScreen = false; } /** A human-readable dump for the menu's status dialog. */ diff --git a/Ports/iOSPort/nativeSources/NetworkConnectionImpl.m b/Ports/iOSPort/nativeSources/NetworkConnectionImpl.m index d2bbba98741..83d1d79e9a6 100644 --- a/Ports/iOSPort/nativeSources/NetworkConnectionImpl.m +++ b/Ports/iOSPort/nativeSources/NetworkConnectionImpl.m @@ -205,9 +205,15 @@ -(void)setConnectionId:(JAVA_INT)connId { } - (NSString*) getFingerprint: (SecCertificateRef) cert { - NSData* certData = (__bridge NSData*) SecCertificateCopyData(cert); + // SecCertificateCopyData follows the Copy rule, so the result is owned here. + // The bridged cast alone leaked it on every handshake. + CFDataRef certData = SecCertificateCopyData(cert); + if (certData == NULL) { + return @""; + } unsigned char sha1Bytes[CC_SHA1_DIGEST_LENGTH]; - CC_SHA1(certData.bytes, (int)certData.length, sha1Bytes); + CC_SHA1(CFDataGetBytePtr(certData), (CC_LONG)CFDataGetLength(certData), sha1Bytes); + CFRelease(certData); NSMutableString *fingerprint = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 3]; for (int i = 0; i < CC_SHA1_DIGEST_LENGTH; ++i) { [fingerprint appendFormat:@"%02x ", sha1Bytes[i]]; @@ -320,10 +326,15 @@ - (NSString*) getPublicKeyDigest: (SecCertificateRef) cert { } - (NSString*) getFingerprint256: (SecCertificateRef) cert { - NSData* keyData = (__bridge NSData*) SecCertificateCopyData(cert); - + // Same ownership rule as getFingerprint: this was leaking one certificate's + // worth of data per digest, on every connection. + CFDataRef keyData = SecCertificateCopyData(cert); + if (keyData == NULL) { + return @""; + } uint8_t digest[CC_SHA256_DIGEST_LENGTH]={0}; - CC_SHA256(keyData.bytes, keyData.length, digest); + CC_SHA256(CFDataGetBytePtr(keyData), (CC_LONG)CFDataGetLength(keyData), digest); + CFRelease(keyData); NSData *out=[NSData dataWithBytes:digest length:CC_SHA256_DIGEST_LENGTH]; NSString *hash=[out description]; hash = [hash stringByReplacingOccurrencesOfString:@" " withString:@""]; diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index 89b836c70b2..777a4475dc3 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -93,8 +93,14 @@ final class IOSDeviceIntegrity { private static final String STATE_NEW = "new"; private static final String STATE_ATTESTED = "attested"; - /** DCError.invalidKey -- the server or the OS no longer knows this key. */ - private static final int DC_ERROR_INVALID_KEY = 2; + // Ordinals from DeviceCheck's DCError enum, which declares no explicit + // values: unknownSystemFailure, featureUnsupported, invalidInput, + // invalidKey, serverUnavailable. Getting invalidKey wrong is quiet and + // expensive -- an invalidated key would never enter the reset-and-reattest + // branch, so the device would fail forever while malformed input would + // pointlessly burn a fresh key. + /** DCError.invalidKey -- the OS no longer recognises this key. */ + private static final int DC_ERROR_INVALID_KEY = 3; /** DCError.serverUnavailable -- Apple is throttling or unreachable. */ private static final int DC_ERROR_SERVER_UNAVAILABLE = 4; @@ -111,6 +117,10 @@ final class IOSDeviceIntegrity { /** Guards the whole attest flow so concurrent callers cannot each burn a key. */ private final Object flowLock = new Object(); private long currentBackoff = MIN_BACKOFF_MILLIS; + /** True while a generate-then-attest bootstrap is running. */ + private boolean bootstrapInFlight; + /** Callers that arrived mid-bootstrap and will assert once it completes. */ + private final java.util.Vector waitingForBootstrap = new java.util.Vector(); IOSDeviceIntegrity(IOSNative nativeInstance) { this.nativeInstance = nativeInstance; @@ -133,6 +143,8 @@ void resetAttestation() { store.remove(KEY_RETRY_AFTER); synchronized (flowLock) { currentBackoff = MIN_BACKOFF_MILLIS; + bootstrapInFlight = false; + failBootstrapWaiters("App Attest state was reset while a bootstrap was in flight"); } } @@ -172,8 +184,20 @@ AsyncResource requestToken(String nonce) { SecureStorage store = SecureStorage.getInstance(); String keyId = store.get(KEY_ID); if (keyId == null || keyId.length() == 0) { - // No key yet: generate one, then continue into attestation. - int rid = register(new PendingRequest(r, nonce, PendingRequest.OP_GENERATE_KEY, null)); + PendingRequest pending = + new PendingRequest(r, nonce, PendingRequest.OP_GENERATE_KEY, null); + if (bootstrapInFlight) { + // Key generation is asynchronous, so holding the lock only + // until the native call is issued is not enough: a second + // caller would still see no key and generate its own, + // burning a second hardware key against Apple's per-device + // budget. Queue instead, and assert once the first bootstrap + // lands -- assertions are unlimited. + waitingForBootstrap.addElement(pending); + return r; + } + bootstrapInFlight = true; + int rid = register(pending); nativeInstance.appAttestGenerateKey(rid); return r; } @@ -189,10 +213,16 @@ AsyncResource requestToken(String nonce) { // --- flow steps ------------------------------------------------------ private void attestKey(AsyncResource r, String nonce, String keyId) { + attestKey(r, nonce, keyId, false); + } + + private void attestKey(AsyncResource r, String nonce, String keyId, boolean retried) { // The attestation binds to SHA-256 of the raw nonce. Hashing here rather // than natively keeps a single hash implementation across both paths. String hash = base64(Hash.sha256(bytes(nonce))); - int rid = register(new PendingRequest(r, nonce, PendingRequest.OP_ATTEST, keyId)); + PendingRequest pending = new PendingRequest(r, nonce, PendingRequest.OP_ATTEST, keyId); + pending.retried = retried; + int rid = register(pending); nativeInstance.appAttestAttestKey(rid, keyId, hash); } @@ -246,7 +276,10 @@ public static void nativeKeyGenerated(final int requestId, final String keyId) { store.set(KEY_ID, keyId); store.set(KEY_STATE, STATE_NEW); synchronized (instance.flowLock) { - instance.attestKey(pending.result, pending.nonce, keyId); + // Carry the marker forward: without it a recovery attempt whose + // replacement key also reports invalidKey would recover again, and + // again, instead of surfacing the failure. + instance.attestKey(pending.result, pending.nonce, keyId, pending.retried); } } @@ -268,6 +301,11 @@ public static void nativeAttestationReady(final int requestId, final String atte if (instance != null) { synchronized (instance.flowLock) { instance.currentBackoff = MIN_BACKOFF_MILLIS; + instance.bootstrapInFlight = false; + // Callers that arrived mid-bootstrap now assert against the key + // this attestation established, rather than each attesting one + // of their own. + instance.drainBootstrapWaiters(pending.keyId); } } succeed(pending, TOKEN_PREFIX + ":attest:" + base64(bytes(pending.keyId)) @@ -319,7 +357,32 @@ public static void nativeAttestError(final int requestId, final int errorCode, instance.currentBackoff = Math.min(backoff * 2, MAX_BACKOFF_MILLIS); } } - fail(pending, msg == null ? "App Attest failed (code " + errorCode + ")" : msg); + String message = msg == null ? "App Attest failed (code " + errorCode + ")" : msg; + if (instance != null && pending.op != PendingRequest.OP_ASSERT) { + synchronized (instance.flowLock) { + instance.bootstrapInFlight = false; + instance.failBootstrapWaiters(message); + } + } + fail(pending, message); + } + + /** Assert for everyone who queued behind the bootstrap. Caller holds flowLock. */ + private void drainBootstrapWaiters(String keyId) { + while (!waitingForBootstrap.isEmpty()) { + PendingRequest waiting = (PendingRequest) waitingForBootstrap.elementAt(0); + waitingForBootstrap.removeElementAt(0); + assertWithKey(waiting.result, waiting.nonce, keyId); + } + } + + /** Caller holds flowLock. Leaving these unresolved would hang the callers. */ + private void failBootstrapWaiters(String message) { + while (!waitingForBootstrap.isEmpty()) { + PendingRequest waiting = (PendingRequest) waitingForBootstrap.elementAt(0); + waitingForBootstrap.removeElementAt(0); + fail(waiting, message); + } } // --- helpers --------------------------------------------------------- From f2e6472ccf943140c2ed85554fce5f6ce3ad48ba Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Wed, 29 Jul 2026 11:03:33 +0300 Subject: [PATCH 04/96] Build the shield simulation menu without adding it, like its siblings installShieldSimulationMenu added the menu to simulateMenu and also returned it, while installNfcSimulationMenu and installFoldableSimulationMenu only return. The menu was not actually added twice -- simulateMenu.removeAll() runs between the two calls, in the block that rebuilds the final menu order -- but the inconsistency made that non-obvious enough to read as a bug. Co-Authored-By: Claude Opus 5 (1M context) --- Ports/JavaSE/src/com/codename1/impl/javase/JavaSEPort.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEPort.java b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEPort.java index 33fd9ac3787..33fb9215a19 100644 --- a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEPort.java +++ b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEPort.java @@ -8452,7 +8452,9 @@ public void actionPerformed(ActionEvent ae) { }); shieldMenu.add(status); - simulateMenu.add(shieldMenu); + // Returned rather than added here, matching installNfcSimulationMenu and + // installFoldableSimulationMenu. The caller does the adding, in the + // removeAll-then-rebuild block that assembles the final menu order. return shieldMenu; } From 41c16d6f5a6e8b43770782cbb875a674be1e889d Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Wed, 29 Jul 2026 11:22:39 +0300 Subject: [PATCH 05/96] Honour the configured failure mode, refresh the chain, and measure time monotonically Four more from review. defaultFailureMode did nothing for hosts registered the short way. protect(host) stored the HostPolicy.PROTECTED constant, whose mode is always OPEN, so an app that asked to fail closed still sent requests without a token. The implicit policy is now built from the configured default. An explicitly supplied policy still wins. The certificate chain is cached on the request and survives retries and redirects, so the guard was vetting the previous connection's certificates: a request that first connected to a pinned host could then accept an unpinned certificate on a retry, and a redirect between differently pinned hosts could fail for no reason. Cleared at the start of each attempt. Token lifetime used System.currentTimeMillis(), which is the wall clock and therefore adjustable -- on a rooted device, by the attacker. That contradicted the class's own documented guarantee. Elapsed time now comes from System.nanoTime(); fetchedAt is kept for log correlation and is exposed and rendered rather than sitting unread. A null status defaulted to OK, so an engine bug could produce a token that reported itself valid and got attached to requests. It now defaults to a non-success status. Co-Authored-By: Claude Opus 5 (1M context) --- .../com/codename1/io/ConnectionRequest.java | 6 ++ .../security/shield/ShieldConfig.java | 21 +++++- .../security/shield/ShieldToken.java | 35 ++++++++- .../security/shield/ShieldApiTest.java | 75 +++++++++++++++++-- 4 files changed, 124 insertions(+), 13 deletions(-) diff --git a/CodenameOne/src/com/codename1/io/ConnectionRequest.java b/CodenameOne/src/com/codename1/io/ConnectionRequest.java index 1e2456cb253..9cc7abef61e 100644 --- a/CodenameOne/src/com/codename1/io/ConnectionRequest.java +++ b/CodenameOne/src/com/codename1/io/ConnectionRequest.java @@ -954,6 +954,12 @@ boolean performOperationComplete() throws IOException { return true; } pinFailure = null; + // Each attempt gets its own chain. This field is populated lazily and + // survives retries and redirects, so without clearing it the guard would + // vet the previous connection's certificates -- accepting an unpinned + // certificate on a retried request, or rejecting a redirect to a + // differently pinned host. + sslCertificates = null; if (cacheMode == CachingMode.OFFLINE || cacheMode == CachingMode.OFFLINE_FIRST) { InputStream is = null; //NOPMD CloseResource try { diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java b/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java index 3060313e787..e1ed7b6368d 100644 --- a/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java +++ b/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java @@ -101,14 +101,29 @@ public ShieldConfig collectSignals(boolean collect) { public ShieldConfig protect(String hostPattern, HostPolicy policy) { if (hostPattern != null && hostPattern.length() > 0) { hostPolicies.put(hostPattern.toLowerCase(), - policy == null ? HostPolicy.PROTECTED : policy); + policy == null ? implicitPolicy() : policy); } return this; } - /// Registers a host with [HostPolicy#PROTECTED]. + /// Registers a host with the default policy, which honours + /// [#defaultFailureMode(FailureMode)]. public ShieldConfig protect(String hostPattern) { - return protect(hostPattern, HostPolicy.PROTECTED); + return protect(hostPattern, implicitPolicy()); + } + + /// The policy used when a host is registered without an explicit one. + /// + /// Built from [#defaultFailureMode(FailureMode)] rather than returning the + /// [HostPolicy#PROTECTED] constant, whose mode is always + /// [FailureMode#OPEN] -- otherwise setting a fail-closed default would + /// silently do nothing for every host registered the short way, which is + /// most of them. + private HostPolicy implicitPolicy() { + if (defaultFailureMode == FailureMode.OPEN) { + return HostPolicy.PROTECTED; + } + return new HostPolicy(true, true, defaultFailureMode); } public String getEndpoint() { diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldToken.java b/CodenameOne/src/com/codename1/security/shield/ShieldToken.java index 43c9ee6d5f8..e6cdfa9ba45 100644 --- a/CodenameOne/src/com/codename1/security/shield/ShieldToken.java +++ b/CodenameOne/src/com/codename1/security/shield/ShieldToken.java @@ -44,13 +44,31 @@ public final class ShieldToken { private final long ttlMillis; private final String binding; + /// Monotonic reference captured at construction. `System.currentTimeMillis()` + /// is the wall clock: it can be stepped backwards or forwards, by the user or + /// by NTP, which would keep a lapsed token looking valid or expire a good one + /// early. Elapsed time is measured from here instead; `fetchedAt` is retained + /// only because it is meaningful to a human reading a log. + private final long fetchedNanos; + public ShieldToken(String value, ShieldStatus status, long fetchedAt, long ttlMillis, String binding) { + this(value, status, fetchedAt, ttlMillis, binding, System.nanoTime()); + } + + /// Test seam: lets a test place the token at a chosen point in its lifetime. + /// Not public, because an app supplying its own reference could only make a + /// lapsed token look fresh. + ShieldToken(String value, ShieldStatus status, long fetchedAt, + long ttlMillis, String binding, long fetchedNanos) { this.value = value; - this.status = status == null ? ShieldStatus.OK : status; + // A missing status is an engine bug, not a success. Defaulting to OK + // would make isValid() true and attach a token nobody vouched for. + this.status = status == null ? ShieldStatus.NOT_INITIALIZED : status; this.fetchedAt = fetchedAt; this.ttlMillis = ttlMillis; this.binding = binding; + this.fetchedNanos = fetchedNanos; } /// The opaque token to place in the request header. May be null when [#getStatus()] is not @@ -66,7 +84,8 @@ public ShieldStatus getStatus() { /// Milliseconds until this token stops being worth sending, or 0 once it has lapsed. public long getMillisUntilExpiry() { - long remaining = (fetchedAt + ttlMillis) - System.currentTimeMillis(); + long elapsed = (System.nanoTime() - fetchedNanos) / 1000000L; + long remaining = ttlMillis - elapsed; return remaining > 0 ? remaining : 0; } @@ -81,7 +100,7 @@ public boolean shouldRefresh(int thresholdPercent) { if (ttlMillis <= 0) { return true; } - long used = System.currentTimeMillis() - fetchedAt; + long used = (System.nanoTime() - fetchedNanos) / 1000000L; return used * 100 >= ttlMillis * thresholdPercent; } @@ -100,9 +119,19 @@ public boolean isBoundTo(String data) { return binding.equals(data); } + /// When this token was fetched, in wall-clock time. + /// + /// For correlating a client log with a server log, and nothing else -- + /// lifetime decisions use the monotonic reference instead, for the reasons + /// in the class documentation. + public long getFetchedAt() { + return fetchedAt; + } + /// Never renders the token value -- these strings end up in logs. public String toString() { return "ShieldToken[status=" + status.getId() + + ", fetchedAt=" + fetchedAt + ", validMs=" + getMillisUntilExpiry() + ", bound=" + (binding != null) + "]"; } diff --git a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java index 2434736e3ad..d31b83fe30d 100644 --- a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java +++ b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java @@ -106,25 +106,54 @@ void tokenIsValidOnlyWhileItHasValueSuccessAndTime() { assertTrue(new ShieldToken("t", ShieldStatus.OK, now, 60000, null).isValid()); assertFalse(new ShieldToken(null, ShieldStatus.OK, now, 60000, null).isValid()); assertFalse(new ShieldToken("t", ShieldStatus.REJECTED, now, 60000, null).isValid()); - assertFalse(new ShieldToken("t", ShieldStatus.OK, now - 120000, 60000, null).isValid()); + assertFalse(new ShieldToken("t", ShieldStatus.OK, now, 60000, null, + System.nanoTime() - 120_000_000_000L).isValid()); } @Test void expiredTokenReportsZeroRatherThanNegativeRemainingTime() { - ShieldToken t = new ShieldToken("t", ShieldStatus.OK, - System.currentTimeMillis() - 120000, 60000, null); + ShieldToken t = new ShieldToken("t", ShieldStatus.OK, System.currentTimeMillis(), + 60000, null, System.nanoTime() - 120_000_000_000L); assertEquals(0, t.getMillisUntilExpiry()); } + /** + * Lifetime is measured monotonically, so moving the wall clock cannot make a + * lapsed token look fresh -- which on a rooted device is something the + * attacker can simply do. + */ + @Test + void lifetimeIgnoresTheWallClock() { + // fetchedAt is deliberately far in the future; only the monotonic + // reference should matter. + ShieldToken t = new ShieldToken("t", ShieldStatus.OK, + System.currentTimeMillis() + 3_600_000L, 60000, null, System.nanoTime()); + assertTrue(t.isValid()); + assertTrue(t.getMillisUntilExpiry() > 0); + + ShieldToken lapsed = new ShieldToken("t", ShieldStatus.OK, + System.currentTimeMillis() + 3_600_000L, 60000, null, + System.nanoTime() - 120_000_000_000L); + assertFalse(lapsed.isValid()); + } + + /** A null status is an engine bug; treating it as OK would attach an unvouched token. */ + @Test + void aMissingStatusIsNotTreatedAsSuccess() { + ShieldToken t = new ShieldToken("t", null, System.currentTimeMillis(), 60000, null); + assertFalse(t.getStatus().isSuccess()); + assertFalse(t.isValid()); + } + @Test void refreshTriggersOnceThresholdShareOfLifetimeIsUsed() { long now = System.currentTimeMillis(); // 10% used, 50% threshold -> no refresh yet. - assertFalse(new ShieldToken("t", ShieldStatus.OK, now - 1000, 10000, null) - .shouldRefresh(50)); + assertFalse(new ShieldToken("t", ShieldStatus.OK, now, 10000, null, + System.nanoTime() - 1_000_000_000L).shouldRefresh(50)); // 60% used -> refresh. - assertTrue(new ShieldToken("t", ShieldStatus.OK, now - 6000, 10000, null) - .shouldRefresh(50)); + assertTrue(new ShieldToken("t", ShieldStatus.OK, now, 10000, null, + System.nanoTime() - 6_000_000_000L).shouldRefresh(50)); } @Test @@ -183,6 +212,38 @@ void hostMatchingIsCaseInsensitive() { assertSame(HostPolicy.PROTECTED, c.policyFor("api.example.com")); } + /** + * defaultFailureMode has to reach hosts registered the short way, which is + * most of them -- otherwise setting a fail-closed default silently does + * nothing, which is the worst possible outcome for a security setting. + */ + @Test + void defaultFailureModeAppliesToHostsRegisteredWithoutAnExplicitPolicy() { + ShieldConfig c = new ShieldConfig() + .defaultFailureMode(FailureMode.CLOSED) + .protect("api.example.com"); + HostPolicy p = c.policyFor("api.example.com"); + assertEquals(FailureMode.CLOSED, p.getFailureMode()); + assertTrue(p.isAttachToken()); + assertTrue(p.isEnforcePins()); + } + + @Test + void anExplicitPolicyStillWinsOverTheDefaultFailureMode() { + ShieldConfig c = new ShieldConfig() + .defaultFailureMode(FailureMode.CLOSED) + .protect("api.example.com", HostPolicy.PROTECTED); + assertEquals(FailureMode.OPEN, c.policyFor("api.example.com").getFailureMode()); + } + + @Test + void aNullPolicyAlsoPicksUpTheDefaultFailureMode() { + ShieldConfig c = new ShieldConfig() + .defaultFailureMode(FailureMode.CLOSED) + .protect("api.example.com", null); + assertEquals(FailureMode.CLOSED, c.policyFor("api.example.com").getFailureMode()); + } + @Test void defaultsAreOpenAndNonBlocking() { ShieldConfig c = new ShieldConfig(); From b4d167dd92a068db1ecbf6b76bb9c734bd217687 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Wed, 29 Jul 2026 11:40:57 +0300 Subject: [PATCH 06/96] Stop the shield header following cross-host redirects Three more from review. A redirect reuses the same request object with its headers intact, and attach() only ever added. A protected endpoint with an open redirect would therefore hand a replayable attestation token to whatever host it pointed at. The header is now cleared before the policy for the current host is evaluated, and re-added only if that host is protected. Adds ConnectionRequest.removeRequestHeader, since there was no way to remove a header at all. The earlier bootstrap fix left the STATE_NEW window open: between key generation persisting the identifier and its attestation completing, the key exists but is not attested, so a caller arriving then bypassed the bootstrapInFlight check and attested the same key again. Attestation is rate limited, so that costs real budget and races its own result. The check now happens before branching on key state, and a key that exists but was never attested is attested rather than replaced. iOS getCompromiseReasons dropped the Cydia probe, which for apps that declare the scheme was the only signal on a device where the file, dyld, write and tracing probes come back clean -- a regression against the previous behaviour. It is consulted again, through a helper rather than isJailbrokenDevice(), which now delegates the other way. Co-Authored-By: Claude Opus 5 (1M context) --- .../com/codename1/io/ConnectionRequest.java | 11 ++++++ .../codename1/security/shield/AppShield.java | 5 +++ .../impl/ios/IOSDeviceIntegrity.java | 26 +++++++++----- .../codename1/impl/ios/IOSImplementation.java | 36 +++++++++++++------ 4 files changed, 59 insertions(+), 19 deletions(-) diff --git a/CodenameOne/src/com/codename1/io/ConnectionRequest.java b/CodenameOne/src/com/codename1/io/ConnectionRequest.java index 9cc7abef61e..d2b009a0a79 100644 --- a/CodenameOne/src/com/codename1/io/ConnectionRequest.java +++ b/CodenameOne/src/com/codename1/io/ConnectionRequest.java @@ -651,6 +651,17 @@ public void addRequestHeader(String key, String value) { } } + /// Removes a header previously added with [#addRequestHeader(String, String)]. + /// + /// Needed because a redirect reuses the same request object with the same + /// headers: anything scoped to the original host has to be removable before + /// the retry, or it follows the redirect to wherever it points. + public void removeRequestHeader(String key) { + if (userHeaders != null && key != null) { + userHeaders.remove(key); + } + } + /// Adds the given header to the request that will be sent unless the header /// is already set to something else /// diff --git a/CodenameOne/src/com/codename1/security/shield/AppShield.java b/CodenameOne/src/com/codename1/security/shield/AppShield.java index 9bde5485ff9..0b2beddef86 100644 --- a/CodenameOne/src/com/codename1/security/shield/AppShield.java +++ b/CodenameOne/src/com/codename1/security/shield/AppShield.java @@ -201,6 +201,11 @@ public static void attach(ConnectionRequest request) throws ShieldException { } String host = hostOf(request.getUrl()); HostPolicy policy = policyFor(host); + // Always clear first. A redirect reuses this request object with its + // headers intact, so a protected endpoint with an open redirect would + // otherwise hand a replayable token to whatever host it points at. + // Re-adding below is conditional on the *current* host's policy. + request.removeRequestHeader(getConfig().getTokenHeader()); if (!policy.isAttachToken()) { return; } diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index 777a4475dc3..9d7248d8913 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -183,7 +183,14 @@ AsyncResource requestToken(String nonce) { } SecureStorage store = SecureStorage.getInstance(); String keyId = store.get(KEY_ID); - if (keyId == null || keyId.length() == 0) { + boolean attested = STATE_ATTESTED.equals(store.get(KEY_STATE)); + if (keyId == null || keyId.length() == 0 || !attested) { + // Checked before branching on the key state, not after: between + // key generation persisting the id and its attestation + // completing, the key exists but is not yet attested, and a + // caller arriving in that window would otherwise attest the same + // key a second time. Attestation is rate limited, so that costs + // real budget and races its own result. PendingRequest pending = new PendingRequest(r, nonce, PendingRequest.OP_GENERATE_KEY, null); if (bootstrapInFlight) { @@ -197,15 +204,18 @@ AsyncResource requestToken(String nonce) { return r; } bootstrapInFlight = true; - int rid = register(pending); - nativeInstance.appAttestGenerateKey(rid); + if (keyId != null && keyId.length() > 0) { + // A key exists but was never attested -- a previous attempt + // died between the two steps. Attest that key rather than + // generating another. + attestKey(r, nonce, keyId); + } else { + int rid = register(pending); + nativeInstance.appAttestGenerateKey(rid); + } return r; } - if (STATE_ATTESTED.equals(store.get(KEY_STATE))) { - assertWithKey(r, nonce, keyId); - } else { - attestKey(r, nonce, keyId); - } + assertWithKey(r, nonce, keyId); } return r; } diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSImplementation.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSImplementation.java index b990a7ead15..f96f3a2e6fa 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSImplementation.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSImplementation.java @@ -12561,15 +12561,23 @@ public boolean isPolygon() { @Override public boolean isJailbrokenDevice() { - if (getCompromiseReasons().length > 0) { - return true; + return getCompromiseReasons().length > 0; + } + + /** + * The legacy probe. Only ever returns true when the app also declares cydia + * in ios.applicationQueriesSchemes -- on iOS 9 and up canOpenURL returns + * false for undeclared schemes regardless of what is installed. That is why + * it cannot be the primary signal, but apps that do declare it would + * regress if it were dropped. + */ + private boolean cydiaProbe() { + try { + Boolean b = canExecute("cydia://package/com.example.package"); + return b != null && b.booleanValue(); + } catch (Throwable t) { + return false; } - // Kept as a last resort, but note it only ever returns true when the app - // also declares cydia in ios.applicationQueriesSchemes -- on iOS 9 and up - // canOpenURL returns false for undeclared schemes regardless of what is - // installed. That is why it cannot be the primary signal. - Boolean b = canExecute("cydia://package/com.example.package"); - return b != null && b.booleanValue(); } /** @@ -12580,11 +12588,17 @@ public boolean isJailbrokenDevice() { @Override public String[] getCompromiseReasons() { String[] signals = deviceIntegrity().jailbreakSignals(); - if (signals.length == 0) { - return signals; - } java.util.ArrayList out = new java.util.ArrayList(); boolean jailbreakReported = false; + if (signals.length == 0) { + // The native probes found nothing, but an app that declares the + // cydia scheme can still detect one this way. Dropping it here would + // regress isDeviceCompromised() for exactly those apps. + if (cydiaProbe()) { + out.add("jailbreak"); + } + return out.toArray(new String[out.size()]); + } for (int i = 0; i < signals.length; i++) { String s = signals[i]; if ("hookLib".equals(s) || "dyldInsert".equals(s)) { From cf0a137da354666fa8ca3c687595dda0e1875d47 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Wed, 29 Jul 2026 11:53:30 +0300 Subject: [PATCH 07/96] Install the network guard, without which protect() did nothing AppShield.init() never installed a NetworkGuard, so nothing consulted the host policies at all: ShieldConfig.protect() configured tokens and pinning that were only ever applied if an app called AppShield.attach() by hand. That is the documented core behaviour of the API, and it was dead code. Adds ShieldNetworkGuard, installed during init. It decorates protected hosts, asks for the richer certificate details only when a pin set actually covers the host, enforces pins through the engine, and drops the cached token when a protected host answers 401 or 403 -- which usually means it refused the token, so replaying it would fail again. Installed even with no engine present. The default engine issues no tokens and enforces no pins, so the guard is inert, and installing unconditionally keeps behaviour identical whether or not the enterprise engine was injected. An app that installed its own guard keeps it and gets a log line saying so, since the consequence is worth knowing. Also: invalid-key recovery started a replacement key generation without marking the bootstrap in flight, because resetAttestation() had just cleared the flag. A request arriving before the recovery callback would see no key and no bootstrap running, generate a second rate-limited key, and race the recovery. Co-Authored-By: Claude Opus 5 (1M context) --- .../codename1/security/shield/AppShield.java | 25 +++++ .../security/shield/ShieldNetworkGuard.java | 102 ++++++++++++++++++ .../impl/ios/IOSDeviceIntegrity.java | 5 + 3 files changed, 132 insertions(+) create mode 100644 CodenameOne/src/com/codename1/security/shield/ShieldNetworkGuard.java diff --git a/CodenameOne/src/com/codename1/security/shield/AppShield.java b/CodenameOne/src/com/codename1/security/shield/AppShield.java index 0b2beddef86..e0a9a93419a 100644 --- a/CodenameOne/src/com/codename1/security/shield/AppShield.java +++ b/CodenameOne/src/com/codename1/security/shield/AppShield.java @@ -24,6 +24,7 @@ import com.codename1.io.ConnectionRequest; import com.codename1.io.Log; +import com.codename1.io.NetworkManager; import com.codename1.security.shield.spi.ShieldEngine; import com.codename1.security.shield.spi.ShieldEngineRegistry; import com.codename1.ui.Display; @@ -108,6 +109,30 @@ public static void init(ShieldConfig cfg) { Log.e(t); setStatus(ShieldStatus.UNPROTECTED); } + installNetworkGuard(); + } + + /// Hooks the shield into the network stack, which is what makes + /// [ShieldConfig#protect(String, HostPolicy)] take effect on ordinary requests. Without it a + /// registered host would carry a policy nothing consults. + /// + /// Installed even when no engine is present: the guard is inert in that case (the default + /// engine issues no tokens and enforces no pins) and installing unconditionally keeps the + /// behaviour identical whether or not the enterprise engine was injected. + private static void installNetworkGuard() { + try { + NetworkManager.setNetworkGuard(new ShieldNetworkGuard()); + } catch (IllegalStateException e) { + // The slot seals after the first install. An app that installed its + // own guard keeps it; say so rather than failing startup, because the + // consequence is that protected hosts are not decorated automatically + // and that is worth knowing about. + Log.p("AppShield: a network guard is already installed, so protected hosts will " + + "not be decorated automatically. Call AppShield.attach(request) from " + + "your own guard if you need both."); + } catch (Throwable t) { + Log.e(t); + } } /// True when a real attestation engine is present and available. False in an open-source or diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldNetworkGuard.java b/CodenameOne/src/com/codename1/security/shield/ShieldNetworkGuard.java new file mode 100644 index 00000000000..f382545c004 --- /dev/null +++ b/CodenameOne/src/com/codename1/security/shield/ShieldNetworkGuard.java @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ +package com.codename1.security.shield; + +import com.codename1.io.ConnectionRequest; +import com.codename1.io.Log; +import com.codename1.io.NetworkGuard; +import com.codename1.security.shield.spi.ShieldEngineRegistry; +import java.io.IOException; + +/// Connects [AppShield] to the network stack. +/// +/// This is what makes [ShieldConfig#protect(String, HostPolicy)] mean anything: without a guard +/// installed, registering a host would configure a policy that nothing ever consults, and tokens +/// would only ever be attached by an app calling [AppShield#attach(ConnectionRequest)] by hand. +/// Installed once from [AppShield#init(ShieldConfig)]. +/// +/// Package-private: apps configure behaviour through [ShieldConfig], and an app-supplied guard +/// could only weaken this one. +final class ShieldNetworkGuard implements NetworkGuard { + + public void beforeRequest(ConnectionRequest request) throws IOException { + // Also clears the header when the host is not protected, which is what + // stops a token following a cross-host redirect. + AppShield.attach(request); + } + + public boolean isCertificateCheckRequired(String url) { + String host = AppShield.hostOf(url); + if (host == null || !AppShield.policyFor(host).isEnforcePins()) { + return false; + } + // Only ask for the richer certificate details when a pin set actually + // covers this host -- collecting them has a per-connection cost, and an + // unpinned host must be left completely alone. + return AppShield.getPinSet().isEnforcedFor(host); + } + + public void checkCertificates(ConnectionRequest request, + ConnectionRequest.SSLCertificate[] certificates) throws IOException { + String host = AppShield.hostOf(request.getUrl()); + if (host == null || !AppShield.policyFor(host).isEnforcePins()) { + return; + } + String[] spki = new String[certificates == null ? 0 : certificates.length]; + String[] certs = new String[spki.length]; + for (int i = 0; i < spki.length; i++) { + spki[i] = certificates[i].getPublicKeyDigest(); + certs[i] = certificates[i].getFingerprint(); + } + boolean ok; + try { + // Local and non-blocking by contract: on iOS this runs on the TLS + // delegate thread with the handshake held open. + ok = ShieldEngineRegistry.getEngine().verifyPins(host, spki, certs); + } catch (Throwable t) { + // A crash in pin comparison must not fail closed by accident. A real + // mismatch is reported as false, not thrown. + Log.e(t); + return; + } + if (!ok) { + throw new ShieldException(ShieldStatus.PIN_MISMATCH, + "The certificate chain presented by " + host + + " matched none of its configured pins"); + } + } + + public void afterResponse(ConnectionRequest request, int responseCode) { + if (responseCode != 401 && responseCode != 403) { + return; + } + String host = AppShield.hostOf(request.getUrl()); + if (host == null || !AppShield.policyFor(host).isAttachToken()) { + return; + } + // A protected host refusing the request usually means it refused the + // token. Dropping the cached one makes the next attempt re-attest rather + // than replay something already rejected. + AppShield.invalidateToken(); + } +} diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index 9d7248d8913..291e3d6d4d4 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -352,6 +352,11 @@ public static void nativeAttestError(final int requestId, final int errorCode, PendingRequest retry = new PendingRequest(pending.result, pending.nonce, PendingRequest.OP_GENERATE_KEY, null); retry.retried = true; + // resetAttestation() clears the flag, so set it again before + // starting the replacement: otherwise a request arriving before + // the recovery callback sees no key and no bootstrap running, + // generates a second rate-limited key and races this one. + instance.bootstrapInFlight = true; int rid = register(retry); instance.nativeInstance.appAttestGenerateKey(rid); } From de958958502980c724a1fc5ad8cccd95c8f27df6 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Wed, 29 Jul 2026 12:11:55 +0300 Subject: [PATCH 08/96] Do not assert before the backend has registered the key Three more from review. Queued callers were drained into assertions as soon as Apple returned the attestation object, but the backend has not seen it at that point -- the first caller's token is only completed afterwards, and registration happens after that. Those assertions would reference a key the server does not know, get rejected, and trigger a pointless invalid-key reset. They are now failed with a retryable message; by the time the app retries, the key is registered and the request costs one cheap assertion. addProtectedHost(host, null) had the same defect just fixed in ShieldConfig.protect: it stored the always-open HostPolicy.PROTECTED constant and ignored the configured default failure mode. The base64-decoded client data hash was allocated and never released on the non-ARC iOS build, leaking one NSData per integrity request for the life of the process. Released through the pool, guarded because ARC forbids an explicit autorelease and this file builds both ways. Co-Authored-By: Claude Opus 5 (1M context) --- .../codename1/security/shield/AppShield.java | 19 ++++++++++++++- Ports/iOSPort/nativeSources/IOSNative.m | 14 +++++++++++ .../impl/ios/IOSDeviceIntegrity.java | 23 ++++++++----------- 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/CodenameOne/src/com/codename1/security/shield/AppShield.java b/CodenameOne/src/com/codename1/security/shield/AppShield.java index e0a9a93419a..3e2f3892571 100644 --- a/CodenameOne/src/com/codename1/security/shield/AppShield.java +++ b/CodenameOne/src/com/codename1/security/shield/AppShield.java @@ -297,11 +297,28 @@ public static ShieldToken getCachedToken() { /// runtime. public static void addProtectedHost(String host, HostPolicy policy) { if (host != null && host.length() > 0) { + // Same rule as ShieldConfig.protect: an omitted policy has to pick up + // the configured default failure mode, or setting a fail-closed + // default silently does nothing on this path too. runtimeHosts.put(host.toLowerCase(), - policy == null ? HostPolicy.PROTECTED : policy); + policy == null ? implicitPolicy() : policy); } } + /// Registers a host with the default policy, honouring + /// [ShieldConfig#defaultFailureMode(FailureMode)]. + public static void addProtectedHost(String host) { + addProtectedHost(host, null); + } + + private static HostPolicy implicitPolicy() { + FailureMode mode = getConfig().getDefaultFailureMode(); + if (mode == FailureMode.OPEN) { + return HostPolicy.PROTECTED; + } + return new HostPolicy(true, true, mode); + } + /// The policy in force for a host. Returns [HostPolicy#UNPROTECTED] for anything not /// registered, which is the great majority of hosts an app talks to. public static HostPolicy policyFor(String host) { diff --git a/Ports/iOSPort/nativeSources/IOSNative.m b/Ports/iOSPort/nativeSources/IOSNative.m index 5593a06c931..541403ec651 100644 --- a/Ports/iOSPort/nativeSources/IOSNative.m +++ b/Ports/iOSPort/nativeSources/IOSNative.m @@ -14641,6 +14641,13 @@ void com_codename1_impl_ios_IOSNative_appAttestAttestKey___int_java_lang_String_ NSString *nsHash = (clientDataHashB64 == JAVA_NULL) ? nil : toNSString(CN1_THREAD_STATE_PASS_ARG clientDataHashB64); NSData *clientDataHash = nsHash == nil ? nil : [[NSData alloc] initWithBase64EncodedString:nsHash options:0]; +#ifndef CN1_USE_ARC + // initWithBase64EncodedString returns an owned object. The block below + // retains it for the duration of the call, so hand ownership to the pool + // rather than leaking one decoded hash per request. Guarded because ARC + // forbids an explicit autorelease, and this file builds both ways. + [clientDataHash autorelease]; +#endif if (nsKeyId == nil || clientDataHash == nil) { cn1AppAttestFail(requestId, nil, @"App Attest attestation missing key or hash"); POOL_END(); @@ -14675,6 +14682,13 @@ void com_codename1_impl_ios_IOSNative_appAttestGenerateAssertion___int_java_lang NSString *nsHash = (clientDataHashB64 == JAVA_NULL) ? nil : toNSString(CN1_THREAD_STATE_PASS_ARG clientDataHashB64); NSData *clientDataHash = nsHash == nil ? nil : [[NSData alloc] initWithBase64EncodedString:nsHash options:0]; +#ifndef CN1_USE_ARC + // initWithBase64EncodedString returns an owned object. The block below + // retains it for the duration of the call, so hand ownership to the pool + // rather than leaking one decoded hash per request. Guarded because ARC + // forbids an explicit autorelease, and this file builds both ways. + [clientDataHash autorelease]; +#endif if (nsKeyId == nil || clientDataHash == nil) { cn1AppAttestFail(requestId, nil, @"App Attest assertion missing key or hash"); POOL_END(); diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index 291e3d6d4d4..d4c0a0fe61f 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -312,10 +312,16 @@ public static void nativeAttestationReady(final int requestId, final String atte synchronized (instance.flowLock) { instance.currentBackoff = MIN_BACKOFF_MILLIS; instance.bootstrapInFlight = false; - // Callers that arrived mid-bootstrap now assert against the key - // this attestation established, rather than each attesting one - // of their own. - instance.drainBootstrapWaiters(pending.keyId); + // Deliberately NOT asserted here. Apple has returned the + // attestation object, but the backend has not seen it yet -- the + // first caller's token is only completed below, and registration + // happens after that. An assertion sent now would reference a key + // the server does not know, which it would reject and which would + // trigger a pointless invalid-key reset. Queued callers are told + // to retry instead; by then the key is registered and their + // request costs one cheap assertion. + instance.failBootstrapWaiters("App Attest is completing first-run " + + "registration for this device; retry shortly"); } } succeed(pending, TOKEN_PREFIX + ":attest:" + base64(bytes(pending.keyId)) @@ -382,15 +388,6 @@ public static void nativeAttestError(final int requestId, final int errorCode, fail(pending, message); } - /** Assert for everyone who queued behind the bootstrap. Caller holds flowLock. */ - private void drainBootstrapWaiters(String keyId) { - while (!waitingForBootstrap.isEmpty()) { - PendingRequest waiting = (PendingRequest) waitingForBootstrap.elementAt(0); - waitingForBootstrap.removeElementAt(0); - assertWithKey(waiting.result, waiting.nonce, keyId); - } - } - /** Caller holds flowLock. Leaving these unresolved would hang the callers. */ private void failBootstrapWaiters(String message) { while (!waitingForBootstrap.isEmpty()) { From 67750ab9376f69d9035408c7998cda60c6ec0de9 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Wed, 29 Jul 2026 12:38:42 +0300 Subject: [PATCH 09/96] Keep tokens off plaintext URLs, and stop over-eager invalidation Eight more from review. The token is a bearer credential, so it is no longer attached to a non-https URL. A downgrade redirect or a mistyped scheme would otherwise hand it to anyone on the path, and pinning cannot help when there is no certificate. Invalidation on any 401 or 403 was wrong: a protected API normally carries ordinary user authorization too, so an expired login would force a re-attest and push a client toward throttling exactly while it is already failing to log in. It now requires the backend to say it was the token it rejected, via a documented X-CN1-Attest-Reject response header. Reading it needed a generic seam, since the framework retains no response headers and the connection is closed by the time afterResponse runs -- so NetworkGuard now declares which headers it wants and they are captured while the connection is open. A fail-closed host in a build with no engine blocked requests outright, directly contradicting the degradation contract this API documents. Fail-closed now applies only when an engine is actually present. Relatedly, an unchecked exception from a pluggable engine escaped the ShieldException-only handler and blocked the request regardless of failure mode; it is now routed through the same path. headersFor() could hand a request-bound token to an unrelated browser navigation, which is exactly what binding exists to prevent. It now requires an unbound token. PinSet returned its backing Vector despite documenting itself immutable, so any caller of the public getPinSet() could empty it and silently switch enforcement off. Deep-copied in and out. ShieldSignals notified listeners while holding its monitor on the update path. Display.callSerially runs inline before the EDT is up, so a listener calling back into snapshot() would deadlock. iOS: resetAttestation left an outstanding bootstrap registered, whose callback could repopulate the key the reset had just deleted and race whatever started afterwards. Callbacks now carry a generation and stale ones are discarded. Co-Authored-By: Claude Opus 5 (1M context) --- .../com/codename1/io/ConnectionRequest.java | 31 ++++++++++ .../src/com/codename1/io/NetworkGuard.java | 11 +++- .../src/com/codename1/io/NetworkManager.java | 2 +- .../codename1/security/shield/AppShield.java | 57 ++++++++++++++++--- .../com/codename1/security/shield/PinSet.java | 39 ++++++++++++- .../security/shield/ShieldNetworkGuard.java | 18 ++++-- .../security/shield/ShieldSignals.java | 16 ++++-- .../impl/ios/IOSDeviceIntegrity.java | 32 +++++++++++ .../security/shield/ShieldApiTest.java | 36 ++++++++++++ 9 files changed, 221 insertions(+), 21 deletions(-) diff --git a/CodenameOne/src/com/codename1/io/ConnectionRequest.java b/CodenameOne/src/com/codename1/io/ConnectionRequest.java index d2b009a0a79..9a591e69606 100644 --- a/CodenameOne/src/com/codename1/io/ConnectionRequest.java +++ b/CodenameOne/src/com/codename1/io/ConnectionRequest.java @@ -1193,6 +1193,7 @@ boolean performOperationComplete() throws IOException { Preferences.set("cn1Etag" + createRequestURL(), etag); } readHeaders(connection); + captureGuardHeaders(connection); contentLength = impl.getContentLength(connection); timeSinceLastUpdate = System.currentTimeMillis(); @@ -1336,6 +1337,36 @@ protected boolean shouldConvertPostToGetOnRedirect() { protected void readHeaders(Object connection) throws IOException { } + /// Values of the headers the installed [NetworkGuard] asked for, captured while the connection + /// is still open because it is closed before `afterResponse` runs. + private String[] guardHeaders; + + private void captureGuardHeaders(Object connection) { + NetworkGuard guard = NetworkManager.getNetworkGuard(); + guardHeaders = null; + if (guard == null) { + return; + } + try { + String[] names = guard.interestingResponseHeaders(); + if (names == null || names.length == 0) { + return; + } + String[] values = new String[names.length]; + for (int i = 0; i < names.length; i++) { + values[i] = getHeader(connection, names[i]); + } + guardHeaders = values; + } catch (Throwable t) { + // Diagnostics for the guard must never fail the request. + Log.e(t); + } + } + + String[] getGuardHeaders() { + return guardHeaders; + } + /// Allows reading the headers from the connection by calling the getHeader() method when a response that isn't 200 OK is sent. /// /// #### Parameters diff --git a/CodenameOne/src/com/codename1/io/NetworkGuard.java b/CodenameOne/src/com/codename1/io/NetworkGuard.java index 6150c006875..630f023399d 100644 --- a/CodenameOne/src/com/codename1/io/NetworkGuard.java +++ b/CodenameOne/src/com/codename1/io/NetworkGuard.java @@ -68,11 +68,20 @@ public interface NetworkGuard { void checkCertificates(ConnectionRequest request, ConnectionRequest.SSLCertificate[] certificates) throws IOException; + /// Response header names this guard wants captured, or null for none. + /// + /// Asked once per response, while the connection is still open. The framework does not retain + /// arbitrary response headers, and by the time [#afterResponse] runs the connection has been + /// closed -- so anything a guard needs to see has to be named here first. + String[] interestingResponseHeaders(); + /// Called once the response code is known, including for failed requests. /// /// This is how a token layer learns its token was refused -- a 401 or 403 from a protected host /// usually means the cached token should be discarded before the next attempt. Must not throw; /// exceptions here are logged and swallowed so a telemetry problem cannot fail a request that /// otherwise succeeded. - void afterResponse(ConnectionRequest request, int responseCode); + /// @param headers values for the names returned by [#interestingResponseHeaders()], in the + /// same order; an entry is null when the response did not carry that header + void afterResponse(ConnectionRequest request, int responseCode, String[] headers); } diff --git a/CodenameOne/src/com/codename1/io/NetworkManager.java b/CodenameOne/src/com/codename1/io/NetworkManager.java index 5e400072bd5..f78d9abc1ce 100644 --- a/CodenameOne/src/com/codename1/io/NetworkManager.java +++ b/CodenameOne/src/com/codename1/io/NetworkManager.java @@ -1171,7 +1171,7 @@ private boolean runCurrentRequest(@Async.Execute ConnectionRequest req) { NetworkGuard guard = networkGuard; if (guard != null) { try { - guard.afterResponse(req, req.getResponseCode()); + guard.afterResponse(req, req.getResponseCode(), req.getGuardHeaders()); } catch (Throwable t) { // A guard's bookkeeping must never turn a completed // request into a failed one. diff --git a/CodenameOne/src/com/codename1/security/shield/AppShield.java b/CodenameOne/src/com/codename1/security/shield/AppShield.java index 3e2f3892571..d4e82146d6c 100644 --- a/CodenameOne/src/com/codename1/security/shield/AppShield.java +++ b/CodenameOne/src/com/codename1/security/shield/AppShield.java @@ -81,6 +81,15 @@ public final class AppShield { private static final Vector listeners = new Vector(); private static final Hashtable runtimeHosts = new Hashtable(); + /// Response header a backend sets to say it rejected the *attestation token*, as opposed to + /// the user's own credentials. + /// + /// Without it a 401 or 403 is ambiguous: protected APIs normally carry ordinary user + /// authorization too, and treating every such response as an attestation rejection would make + /// a client re-attest through an entire login failure. Emit it only when the token itself was + /// the problem; the value is ignored. + public static final String REJECT_HEADER = "X-CN1-Attest-Reject"; + private AppShield() { } @@ -224,7 +233,8 @@ public static void attach(ConnectionRequest request) throws ShieldException { if (request == null || !initialized) { return; } - String host = hostOf(request.getUrl()); + String url = request.getUrl(); + String host = hostOf(url); HostPolicy policy = policyFor(host); // Always clear first. A redirect reuses this request object with its // headers intact, so a protected endpoint with an open redirect would @@ -234,27 +244,54 @@ public static void attach(ConnectionRequest request) throws ShieldException { if (!policy.isAttachToken()) { return; } + if (!isSecure(url)) { + // The token is a bearer credential. Sending it in plaintext -- after a + // downgrade redirect, or a mistyped scheme -- hands it to anyone on the + // path, and pinning cannot help because there is no certificate to pin. + Log.p("AppShield: refusing to attach a token to a plaintext URL for " + + host + ". Use https for protected hosts."); + return; + } try { ShieldToken token = ShieldEngineRegistry.getEngine().fetchToken(null); - setStatus(token.getStatus()); - if (token.isValid()) { + setStatus(token == null ? ShieldStatus.SERVICE_DOWN : token.getStatus()); + if (token != null && token.isValid()) { request.addRequestHeader(getConfig().getTokenHeader(), token.getValue()); return; } - failOrContinue(policy, new ShieldException(token.getStatus(), + failOrContinue(policy, new ShieldException( + token == null ? ShieldStatus.SERVICE_DOWN : token.getStatus(), "No valid attestation token for " + host)); } catch (ShieldException e) { setStatus(e.getStatus()); failOrContinue(policy, e); + } catch (Throwable t) { + // An engine is pluggable code that can throw anything. Letting an + // unchecked failure escape would block the request regardless of the + // host's failure mode, which is the opposite of fail-open. + Log.e(t); + setStatus(ShieldStatus.SERVICE_DOWN); + failOrContinue(policy, new ShieldException(ShieldStatus.SERVICE_DOWN, + "Attestation engine failed for " + host)); } } + /// True for an absolute https URL. Anything else -- http, or a relative URL we cannot + /// classify -- is not somewhere a bearer token belongs. + static boolean isSecure(String url) { + return url != null && url.toLowerCase().startsWith("https://"); + } + private static void failOrContinue(HostPolicy policy, ShieldException e) throws ShieldException { - if (policy.getFailureMode() == FailureMode.CLOSED) { + // A build with no engine must never block a request: that is the + // degradation contract this API documents, and a fail-closed host in an + // open-source or unentitled build would otherwise break outright. + if (policy.getFailureMode() == FailureMode.CLOSED && isProtected()) { throw e; } Log.p("AppShield: continuing without a token (" + e.getStatus().getId() - + "); host policy is fail-open."); + + "); " + (isProtected() ? "host policy is fail-open." + : "no attestation engine is present, so nothing is enforced.")); } /// The headers a protected URL should carry, for network paths that do not go through @@ -273,8 +310,14 @@ public static Hashtable headersFor(String url) { if (!policyFor(hostOf(url)).isAttachToken()) { return out; } + if (!isSecure(url)) { + return out; + } ShieldToken token = getCachedToken(); - if (token != null && token.isValid()) { + // isBoundTo(null) as well as isValid(): a token minted for one specific + // request must not be handed to an unrelated navigation, which is the + // whole reason binding exists. + if (token != null && token.isValid() && token.isBoundTo(null)) { out.put(getConfig().getTokenHeader(), token.getValue()); } return out; diff --git a/CodenameOne/src/com/codename1/security/shield/PinSet.java b/CodenameOne/src/com/codename1/security/shield/PinSet.java index 7b417799158..7728c75de9e 100644 --- a/CodenameOne/src/com/codename1/security/shield/PinSet.java +++ b/CodenameOne/src/com/codename1/security/shield/PinSet.java @@ -61,7 +61,11 @@ public final class PinSet { /// @param softExpiry local millis after which a refresh should be attempted, 0 for never /// @param hardExpiry local millis after which the set is discarded entirely, 0 for never public PinSet(Hashtable hostToPins, int version, long softExpiry, long hardExpiry) { - this.hostToPins = hostToPins == null ? new Hashtable() : hostToPins; + // Deep copy. The set is reachable through the public AppShield.getPinSet(), + // and a caller that cleared the backing vectors would leave every host + // looking unpinned -- which silently disables enforcement rather than + // failing visibly. + this.hostToPins = copyOf(hostToPins); this.version = version; this.softExpiry = softExpiry; this.hardExpiry = hardExpiry; @@ -92,6 +96,11 @@ public boolean isEnforcedFor(String host) { return pins != null && !pins.isEmpty(); } + /// Number of hosts with at least one pin. Used by tests and diagnostics. + public int hostCount() { + return hostToPins.size(); + } + /// The pins registered for a host, honouring a leading `*.` wildcard, or null when the host is /// not pinned. public Vector pinsFor(String host) { @@ -101,20 +110,44 @@ public Vector pinsFor(String host) { String h = host.toLowerCase(); Object exact = hostToPins.get(h); if (exact != null) { - return (Vector) exact; + return copyOf((Vector) exact); } // Walk up the labels so a "*.example.com" entry covers "api.example.com". int dot = h.indexOf('.'); while (dot >= 0 && dot < h.length() - 1) { Object wild = hostToPins.get("*." + h.substring(dot + 1)); if (wild != null) { - return (Vector) wild; + return copyOf((Vector) wild); } dot = h.indexOf('.', dot + 1); } return null; } + private static Hashtable copyOf(Hashtable in) { + Hashtable out = new Hashtable(); + if (in == null) { + return out; + } + java.util.Enumeration keys = in.keys(); + while (keys.hasMoreElements()) { + Object key = keys.nextElement(); + Object value = in.get(key); + out.put(key, value instanceof Vector ? copyOf((Vector) value) : value); + } + return out; + } + + private static Vector copyOf(Vector in) { + Vector out = new Vector(); + if (in != null) { + for (int i = 0; i < in.size(); i++) { + out.addElement(in.elementAt(i)); + } + } + return out; + } + /// True when at least one of the supplied chain digests matches a pin for the host. /// /// Returns true when the host is not pinned at all -- "no opinion" must never be reported as a diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldNetworkGuard.java b/CodenameOne/src/com/codename1/security/shield/ShieldNetworkGuard.java index f382545c004..9f9be57befb 100644 --- a/CodenameOne/src/com/codename1/security/shield/ShieldNetworkGuard.java +++ b/CodenameOne/src/com/codename1/security/shield/ShieldNetworkGuard.java @@ -86,7 +86,11 @@ public void checkCertificates(ConnectionRequest request, } } - public void afterResponse(ConnectionRequest request, int responseCode) { + public String[] interestingResponseHeaders() { + return new String[] {AppShield.REJECT_HEADER}; + } + + public void afterResponse(ConnectionRequest request, int responseCode, String[] headers) { if (responseCode != 401 && responseCode != 403) { return; } @@ -94,9 +98,15 @@ public void afterResponse(ConnectionRequest request, int responseCode) { if (host == null || !AppShield.policyFor(host).isAttachToken()) { return; } - // A protected host refusing the request usually means it refused the - // token. Dropping the cached one makes the next attempt re-attest rather - // than replay something already rejected. + // Only when the backend says it was the *token* it rejected. A protected + // API usually also carries ordinary user authorization, so an expired + // login or a plain permission denial is a 401/403 that has nothing to do + // with attestation -- re-attesting on every one of those would push a + // client into rate limiting precisely while it is already failing to log + // in. + if (headers == null || headers.length == 0 || headers[0] == null) { + return; + } AppShield.invalidateToken(); } } diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldSignals.java b/CodenameOne/src/com/codename1/security/shield/ShieldSignals.java index 6a08e395890..7a4efd76898 100644 --- a/CodenameOne/src/com/codename1/security/shield/ShieldSignals.java +++ b/CodenameOne/src/com/codename1/security/shield/ShieldSignals.java @@ -52,18 +52,24 @@ public static void add(ShieldSignal signal) { return; } synchronized (signals) { + boolean replaced = false; for (int i = 0; i < signals.size(); i++) { if (((ShieldSignal) signals.elementAt(i)).getId().equals(signal.getId())) { signals.setElementAt(signal, i); - notifyListeners(signal); - return; + replaced = true; + break; } } - if (signals.size() >= MAX_SIGNALS) { - signals.removeElementAt(0); + if (!replaced) { + if (signals.size() >= MAX_SIGNALS) { + signals.removeElementAt(0); + } + signals.addElement(signal); } - signals.addElement(signal); } + // Outside the lock on every path. Display.callSerially runs the task + // inline when the EDT is not up yet, so a listener that calls back into + // snapshot() would deadlock on the monitor we were still holding. notifyListeners(signal); } diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index d4c0a0fe61f..1a0ff1e9004 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -119,6 +119,13 @@ final class IOSDeviceIntegrity { private long currentBackoff = MIN_BACKOFF_MILLIS; /** True while a generate-then-attest bootstrap is running. */ private boolean bootstrapInFlight; + /** + * Bumped by every reset. A callback carrying an older generation belongs to a + * bootstrap that was abandoned, and acting on it would repopulate the key a + * reset just deleted -- racing whatever flow started afterwards to persist a + * different key. + */ + private int generation; /** Callers that arrived mid-bootstrap and will assert once it completes. */ private final java.util.Vector waitingForBootstrap = new java.util.Vector(); @@ -144,6 +151,8 @@ void resetAttestation() { synchronized (flowLock) { currentBackoff = MIN_BACKOFF_MILLIS; bootstrapInFlight = false; + // Any callback still outstanding now belongs to an abandoned flow. + generation++; failBootstrapWaiters("App Attest state was reset while a bootstrap was in flight"); } } @@ -278,6 +287,10 @@ public static void nativeKeyGenerated(final int requestId, final String keyId) { if (pending == null || instance == null) { return; } + if (isStale(pending)) { + fail(pending, "App Attest state was reset while this request was in flight"); + return; + } if (keyId == null || keyId.length() == 0) { fail(pending, "App Attest key generation returned no identifier"); return; @@ -299,6 +312,10 @@ public static void nativeAttestationReady(final int requestId, final String atte if (pending == null) { return; } + if (isStale(pending)) { + fail(pending, "App Attest state was reset while this request was in flight"); + return; + } if (attestationB64 == null) { fail(pending, "App Attest attestation returned no data"); return; @@ -409,6 +426,9 @@ private static long readRetryAfter() { } private static int register(PendingRequest pending) { + if (instance != null) { + pending.generation = instance.generation; + } synchronized (REQUESTS) { int rid = nextRequestId++; REQUESTS.put(Integer.valueOf(rid), pending); @@ -416,6 +436,15 @@ private static int register(PendingRequest pending) { } } + /** + * True when this callback belongs to a flow a reset has since abandoned. Its + * results must be discarded rather than written back, or it would resurrect + * the key the reset deleted. + */ + private static boolean isStale(PendingRequest pending) { + return instance != null && pending.generation != instance.generation; + } + private static PendingRequest take(int requestId) { synchronized (REQUESTS) { return REQUESTS.remove(Integer.valueOf(requestId)); @@ -470,6 +499,9 @@ private static final class PendingRequest { final String keyId; String clientData; boolean retried; + /// Which reset generation this request was registered under. A callback + /// carrying an older one belongs to an abandoned flow. + int generation; PendingRequest(AsyncResource result, String nonce, int op, String keyId) { this.result = result; diff --git a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java index d31b83fe30d..607ba007c75 100644 --- a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java +++ b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java @@ -254,6 +254,16 @@ void defaultsAreOpenAndNonBlocking() { assertEquals(FailureMode.OPEN, HostPolicy.PROTECTED.getFailureMode()); } + /** A bearer token has no business travelling in plaintext. */ + @Test + void onlyHttpsUrlsAreConsideredSecure() { + assertTrue(AppShield.isSecure("https://api.example.com/x")); + assertTrue(AppShield.isSecure("HTTPS://api.example.com/x")); + assertFalse(AppShield.isSecure("http://api.example.com/x")); + assertFalse(AppShield.isSecure("/relative/path")); + assertFalse(AppShield.isSecure(null)); + } + // --- PinSet --------------------------------------------------------- private static PinSet pinSet(String host, String pin, long soft, long hard) { @@ -322,6 +332,32 @@ void pinWildcardsFollowTheSameRulesAsHostPolicies() { assertNull(set.pinsFor("other.test")); } + /** + * The set is reachable through the public AppShield.getPinSet(), so a caller + * that mutated it could silently switch enforcement off rather than fail + * visibly. + */ + @Test + void pinSetCannotBeDisabledByMutatingWhatItReturns() { + Hashtable t = new Hashtable(); + Vector v = new Vector(); + v.addElement("AAA"); + t.put("api.example.com", v); + PinSet set = new PinSet(t, 1, 0, 0); + + // Mutating the table passed to the constructor must not reach inside. + v.removeAllElements(); + t.clear(); + assertTrue(set.isEnforcedFor("api.example.com")); + assertEquals(1, set.hostCount()); + + // Nor must mutating what pinsFor() hands back. + set.pinsFor("api.example.com").removeAllElements(); + assertTrue(set.isEnforcedFor("api.example.com")); + assertFalse(set.matches("api.example.com", new String[] {"WRONG"})); + assertTrue(set.matches("api.example.com", new String[] {"AAA"})); + } + // --- ShieldSignals -------------------------------------------------- @Test From 735483c34e88eea3d54ab0deb203d8a160d86f15 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Wed, 29 Jul 2026 13:46:54 +0300 Subject: [PATCH 10/96] Fix an MCP transport leak, SPKI collection and attestation registration CI surfaced a pre-existing race in the MCP server that my added tests shifted the ordering enough to expose. The reader thread reads liveness off the server's shared fields, so a stopped-then-restarted server left the old thread serving the transport it had been given: it blocked in readMessage forever and never released the process-wide registration, and every later server in the JVM was refused with "already open on port". The thread now tracks the transport it was handed and closes it once the server has moved on. isRunning() joins the other synchronized accesses, which SpotBugs asked for. Review findings, all confirmed against the code: - Collect SPKI digests even when the request already enabled certificate checks. The early return meant a request that called setCheckSSLCertificates(true) handed the guard a chain with no public key digests, so a guard pinning the SPKI rejected a valid chain. - Capture guard headers before returning from an error response. setReadResponseForErrors(false) returns before the capture ran, so a 401/403 -- the response a token layer most needs to see -- never reached afterResponse and the rejected token stayed cached. - Hold iOS assertions until registration is acknowledged. Apple's callback persisted "attested", so a caller arriving in the window before any backend had recorded the key sailed past the waiting queue and asserted against a key the server could not resolve. The key is now "pending" until DeviceIntegrity.confirmAttestation(), with a grace window so a consumer that never acknowledges still works. The JavaSE smoke test's Maven Central retry now backs off progressively; a flat 30s is inside the window a 429 is still rate limiting in, so all three attempts failed for the same reason. Co-Authored-By: Claude Opus 5 (1M context) --- .../impl/CodenameOneImplementation.java | 7 + .../com/codename1/io/ConnectionRequest.java | 18 ++- .../src/com/codename1/mcp/MCPServer.java | 57 ++++++-- .../codename1/security/DeviceIntegrity.java | 17 +++ .../shield/spi/DefaultEngineContext.java | 8 ++ .../security/shield/spi/EngineContext.java | 5 + CodenameOne/src/com/codename1/ui/Display.java | 6 + .../com/codename1/impl/javase/JavaSEPort.java | 5 + .../impl/ios/IOSDeviceIntegrity.java | 112 +++++++++++++-- .../codename1/impl/ios/IOSImplementation.java | 5 + .../mcp/MCPLoopbackTransportOpenTest.java | 136 ++++++++++++++++++ scripts/run-javase-cef-ffmpeg-smoke.py | 24 ++-- 12 files changed, 363 insertions(+), 37 deletions(-) diff --git a/CodenameOne/src/com/codename1/impl/CodenameOneImplementation.java b/CodenameOne/src/com/codename1/impl/CodenameOneImplementation.java index d2cb55765fb..361ed4d7104 100644 --- a/CodenameOne/src/com/codename1/impl/CodenameOneImplementation.java +++ b/CodenameOne/src/com/codename1/impl/CodenameOneImplementation.java @@ -11272,6 +11272,13 @@ public String[] getEnabledAccessibilityServices() { public void resetAttestation() { } + /// Acknowledges that a verifying backend has recorded the attested key, so subsequent requests can + /// take the cheap assertion path. See + /// [com.codename1.security.DeviceIntegrity#confirmAttestation()]. No-op where attestation holds no + /// client-side key. + public void confirmAttestation() { + } + /// Returns digests of the certificates the running application is actually signed with, so a build /// can be compared against the identity it was built under and repackaging can be reported. /// diff --git a/CodenameOne/src/com/codename1/io/ConnectionRequest.java b/CodenameOne/src/com/codename1/io/ConnectionRequest.java index 9a591e69606..b2555b0f587 100644 --- a/CodenameOne/src/com/codename1/io/ConnectionRequest.java +++ b/CodenameOne/src/com/codename1/io/ConnectionRequest.java @@ -929,25 +929,26 @@ boolean checkCertificatesNativeCallback() { /// True when the certificate chain for this request should be fetched and vetted, either /// because the request opted in itself or because the installed [NetworkGuard] pins this host. private boolean shouldInspectCertificates() { - if (checkSSLCertificates) { - return true; - } NetworkGuard guard = NetworkManager.getNetworkGuard(); if (guard == null) { - return false; + return checkSSLCertificates; } try { if (guard.isCertificateCheckRequired(url)) { // Ask the platform for the richer chain details (public key // digests, per-certificate grouping). Off by default so every - // existing caller keeps seeing byte-identical data. + // existing caller keeps seeing byte-identical data. Asked even + // when the request already opted in on its own: otherwise a + // request that calls setCheckSSLCertificates(true) hands the + // guard a chain with no public key digests at all, and a guard + // pinning the SPKI would reject a perfectly valid chain. collectPublicKeyDigests = true; return true; } } catch (Throwable t) { Log.e(t); } - return false; + return checkSSLCertificates; } /// Performs the actual network request on behalf of the network manager @@ -1180,6 +1181,11 @@ boolean performOperationComplete() throws IOException { responseErrorMessge = impl.getResponseMessage(connection); handleErrorResponseCode(responseCode, responseErrorMessge); if (!isReadResponseForErrors()) { + // Capture before the early return. A 401/403 is exactly the + // response a guard needs to see -- it is how a token layer + // learns its token was refused -- and this branch is the + // common configuration for the requests that carry one. + captureGuardHeaders(connection); return true; } } diff --git a/CodenameOne/src/com/codename1/mcp/MCPServer.java b/CodenameOne/src/com/codename1/mcp/MCPServer.java index 386568ba327..51acdea382e 100644 --- a/CodenameOne/src/com/codename1/mcp/MCPServer.java +++ b/CodenameOne/src/com/codename1/mcp/MCPServer.java @@ -128,7 +128,7 @@ public void setScreenshotEnabled(boolean screenshotEnabled) { this.screenshotEnabled = screenshotEnabled; } - public boolean isRunning() { + public synchronized boolean isRunning() { return running; } @@ -139,10 +139,15 @@ public synchronized void start(MCPTransport transport) { } this.transport = transport; running = true; + // Captured for the thread rather than read back off the field. A later start() + // replaces the field, and a reader thread that followed it would end up serving a + // transport the server no longer considers its own -- while the transport it was + // actually given was never closed. + final MCPTransport mine = transport; Thread readerThread = new Thread(new Runnable() { @Override public void run() { - runLoop(); + runLoop(mine); } }, "cn1-mcp-server"); readerThread.start(); @@ -156,9 +161,34 @@ public synchronized void stop() { } } - private void runLoop() { + /// True while `t` is still the transport this server is serving over. False once stop() + /// has run, or once a restart handed the server a different transport -- in both cases + /// the thread holding `t` owns it alone and has to close it itself. + private synchronized boolean isCurrent(MCPTransport t) { + return running && transport == t; // NOPMD identity is the question, not equality + } + + /// Clears the running flag, but only on behalf of the transport that is still current. + /// A thread unwinding from a superseded transport must not stop a server that has since + /// been restarted over a new one. + private synchronized void stopIfCurrent(MCPTransport t) { + if (transport == t) { // NOPMD identity: is this still our transport? + running = false; + } + } + + private void runLoop(MCPTransport t) { + // Opening is deferred to this thread, so by the time it happens the server may + // already have been stopped or restarted. Either way stop()'s close() ran against a + // transport that had not opened yet and therefore released nothing, so opening now + // would leave a transport registered with nobody left to close it. That registration + // is process-wide: every later open() is refused on the grounds that an agent is + // already being served. + if (!isCurrent(t)) { + return; + } try { - transport.open(); + t.open(); } catch (IOException ex) { // The transport failed to start listening. Log defensively: the CN1 Log // routes through the platform implementation, which may not be registered @@ -169,13 +199,20 @@ private void runLoop() { } catch (Throwable logErr) { System.err.println("[cn1.mcp] transport open failed: " + ex); } - running = false; + stopIfCurrent(t); + t.close(); return; } - while (running) { + if (!isCurrent(t)) { + // Same window, the far side of it: the server moved on while open() was in + // flight. Undo the registration this thread just took. + t.close(); + return; + } + while (isCurrent(t)) { String line; try { - line = transport.readMessage(); + line = t.readMessage(); } catch (IOException ex) { break; } @@ -188,14 +225,14 @@ private void runLoop() { String response = handleMessage(line); if (response != null) { try { - transport.writeMessage(response); + t.writeMessage(response); } catch (IOException ex) { break; } } } - running = false; - transport.close(); + stopIfCurrent(t); + t.close(); } /// Handles one inbound JSON-RPC message and returns the response line, or null diff --git a/CodenameOne/src/com/codename1/security/DeviceIntegrity.java b/CodenameOne/src/com/codename1/security/DeviceIntegrity.java index 371372902d5..bee358e871b 100644 --- a/CodenameOne/src/com/codename1/security/DeviceIntegrity.java +++ b/CodenameOne/src/com/codename1/security/DeviceIntegrity.java @@ -127,6 +127,23 @@ public static void resetAttestation() { Display.getInstance().resetAttestation(); } + /// Tells the attestation layer that your backend has recorded the attested key, so later requests + /// can use cheap assertions instead of attesting again. + /// + /// This matters on iOS. The first token of a device's life is an attestation, which carries the + /// public key; every token after it is an assertion, which carries only the key's identifier. An + /// assertion sent before the backend has stored that public key is unresolvable, and the natural + /// reading of that rejection -- the key is invalid -- would throw away a key that was perfectly + /// good and burn one of Apple's rate limited attestations replacing it. So requests made between + /// the attestation and this acknowledgement are refused with a retry hint rather than asserted. + /// + /// Call it once, after the response accepting the attestation token. Not calling it is safe but + /// slower: the client assumes registration succeeded after a short grace period. No-op on Android + /// and where attestation is unsupported. + public static void confirmAttestation() { + Display.getInstance().confirmAttestation(); + } + /// Non-exiting RASP check. Returns true when the device shows signs of being rooted, jailbroken, /// running under dynamic instrumentation (e.g. Frida) or otherwise tampered. Unlike the /// `android.rootCheck` / `ios.detectJailbreak` launch gates this never terminates the app, so it is diff --git a/CodenameOne/src/com/codename1/security/shield/spi/DefaultEngineContext.java b/CodenameOne/src/com/codename1/security/shield/spi/DefaultEngineContext.java index 45c8067e222..35c1c8a3756 100644 --- a/CodenameOne/src/com/codename1/security/shield/spi/DefaultEngineContext.java +++ b/CodenameOne/src/com/codename1/security/shield/spi/DefaultEngineContext.java @@ -66,6 +66,14 @@ public void resetPlatformAttestation() { } } + public void confirmPlatformAttestation() { + try { + DeviceIntegrity.confirmAttestation(); + } catch (Throwable t) { + Log.e(t); + } + } + public String[] getPlatformCompromiseReasons() { try { String[] r = DeviceIntegrity.getCompromiseReasons(); diff --git a/CodenameOne/src/com/codename1/security/shield/spi/EngineContext.java b/CodenameOne/src/com/codename1/security/shield/spi/EngineContext.java index 824217b8c2a..a5e7f27757d 100644 --- a/CodenameOne/src/com/codename1/security/shield/spi/EngineContext.java +++ b/CodenameOne/src/com/codename1/security/shield/spi/EngineContext.java @@ -51,6 +51,11 @@ public interface EngineContext { /// Used when the service reports that the device's attestation key is unknown to it. void resetPlatformAttestation(); + /// Acknowledges that the verifying service recorded the attested key, releasing the client to use + /// cheap assertions from here on. Call it once the service has accepted an attestation token; until + /// then the platform refuses to assert against a key the service cannot yet resolve. + void confirmPlatformAttestation(); + /// Platform-detected compromise reasons, such as `root` or `frida`. String[] getPlatformCompromiseReasons(); diff --git a/CodenameOne/src/com/codename1/ui/Display.java b/CodenameOne/src/com/codename1/ui/Display.java index 26c6232b16f..f7543cfc97f 100644 --- a/CodenameOne/src/com/codename1/ui/Display.java +++ b/CodenameOne/src/com/codename1/ui/Display.java @@ -6865,6 +6865,12 @@ public void resetAttestation() { impl.resetAttestation(); } + /// Acknowledges that a backend recorded the attested key. See + /// `com.codename1.security.DeviceIntegrity#confirmAttestation()`. + public void confirmAttestation() { + impl.confirmAttestation(); + } + /// Returns digests of the certificates the running app is signed with. Low level hook for the /// attestation layer, which reports them to a verifying service; an on-device comparison proves /// nothing on its own. Empty where the platform has no such concept. diff --git a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEPort.java b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEPort.java index 33fb9215a19..99f766d3268 100644 --- a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEPort.java +++ b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEPort.java @@ -19050,6 +19050,11 @@ public void resetAttestation() { // Nothing is cached in the simulator; the menu is the state. } + @Override + public void confirmAttestation() { + // No client-side key here either, so there is nothing to acknowledge. + } + @Override public boolean isDeviceCompromised() { return JavaSEShield.simReasons().length > 0; diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index 1a0ff1e9004..720b2e2a39e 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -90,8 +90,28 @@ final class IOSDeviceIntegrity { private static final String KEY_STATE = "cn1.appattest.state"; private static final String KEY_RETRY_AFTER = "cn1.appattest.retryAfter"; + private static final String KEY_PENDING_SINCE = "cn1.appattest.pendingSince"; + private static final String STATE_NEW = "new"; private static final String STATE_ATTESTED = "attested"; + /** + * Apple returned the attestation object, but no backend has acknowledged + * recording the key yet. See {@link #confirmAttestation()}. + */ + private static final String STATE_PENDING = "pending"; + + /** + * How long a key may sit unacknowledged before it is treated as registered + * anyway. + * + *

Needed because acknowledgement is optional: a caller using + * {@code DeviceIntegrity.requestIntegrityToken} directly, without the shield + * engine, never calls {@link #confirmAttestation()}. Without an expiry such + * an app would sit in the pending state forever and re-attest on every + * request, which is exactly the rate-limit burn this class exists to avoid. + * A minute comfortably covers a registration round trip on a slow link.

+ */ + private static final long REGISTRATION_GRACE_MILLIS = 60L * 1000L; // Ordinals from DeviceCheck's DCError enum, which declares no explicit // values: unknownSystemFailure, featureUnsupported, invalidInput, @@ -148,6 +168,7 @@ void resetAttestation() { store.remove(KEY_ID); store.remove(KEY_STATE); store.remove(KEY_RETRY_AFTER); + store.remove(KEY_PENDING_SINCE); synchronized (flowLock) { currentBackoff = MIN_BACKOFF_MILLIS; bootstrapInFlight = false; @@ -157,6 +178,29 @@ void resetAttestation() { } } + /** + * Acknowledges that a backend has recorded this device's attested public key, + * moving it from {@code pending} to {@code attested} so subsequent requests + * take the cheap assertion path. + * + *

Until this is called -- or the grace window expires -- requests are + * refused with a retry hint rather than asserting. An assertion references a + * key by identifier only, so one sent before the server has the public key is + * simply unknown to it: the server rejects it, the app reads that as an + * invalid key, and resets a key that was in fact perfectly good. The whole + * point of attest-once is not to burn keys that way.

+ */ + void confirmAttestation() { + SecureStorage store = SecureStorage.getInstance(); + if (!STATE_PENDING.equals(store.get(KEY_STATE))) { + return; + } + synchronized (flowLock) { + store.set(KEY_STATE, STATE_ATTESTED); + store.remove(KEY_PENDING_SINCE); + } + } + String[] jailbreakSignals() { try { String signals = nativeInstance.iosJailbreakSignals(); @@ -192,7 +236,26 @@ AsyncResource requestToken(String nonce) { } SecureStorage store = SecureStorage.getInstance(); String keyId = store.get(KEY_ID); - boolean attested = STATE_ATTESTED.equals(store.get(KEY_STATE)); + String state = store.get(KEY_STATE); + if (STATE_PENDING.equals(state) && keyId != null && keyId.length() > 0) { + if (registrationGraceRemaining() > 0) { + // The key is attested with Apple but no backend has confirmed + // recording it. Asserting now would present a key identifier + // the server cannot resolve, which reads as an invalid key and + // costs a needless reset -- so refuse briefly instead. + r.error(new RuntimeException("App Attest is completing first-run " + + "registration for this device; retry shortly")); + return r; + } + // Nobody acknowledged within the window. Either the consumer does + // not participate in acknowledgement at all, or its registration + // call was lost. Assume registered rather than re-attesting on + // every request forever. + store.set(KEY_STATE, STATE_ATTESTED); + store.remove(KEY_PENDING_SINCE); + state = STATE_ATTESTED; + } + boolean attested = STATE_ATTESTED.equals(state); if (keyId == null || keyId.length() == 0 || !attested) { // Checked before branching on the key state, not after: between // key generation persisting the id and its attestation @@ -229,6 +292,29 @@ AsyncResource requestToken(String nonce) { return r; } + /** + * Milliseconds left in the registration grace window, or 0 once it has run + * out. A pending timestamp from the future -- a clock the user moved back -- + * is treated as expired rather than as an unbounded wait. + */ + private static long registrationGraceRemaining() { + String since = SecureStorage.getInstance().get(KEY_PENDING_SINCE); + if (since == null || since.length() == 0) { + return 0; + } + long started; + try { + started = Long.parseLong(since); + } catch (NumberFormatException e) { + return 0; + } + long elapsed = System.currentTimeMillis() - started; + if (elapsed < 0 || elapsed >= REGISTRATION_GRACE_MILLIS) { + return 0; + } + return REGISTRATION_GRACE_MILLIS - elapsed; + } + // --- flow steps ------------------------------------------------------ private void attestKey(AsyncResource r, String nonce, String keyId) { @@ -320,23 +406,23 @@ public static void nativeAttestationReady(final int requestId, final String atte fail(pending, "App Attest attestation returned no data"); return; } - // Optimistic: Apple accepted the attestation, but only the backend can - // confirm it recorded the key. If it later rejects, the app calls - // DeviceIntegrity.resetAttestation() and we start over. + // Apple accepted the attestation, but only the backend can confirm it + // recorded the key, so the key becomes pending rather than attested. + // Every caller -- queued or newly arriving -- is told to retry until + // confirmAttestation() lands or the grace window expires; otherwise a + // caller arriving right after this callback would sail past the queue and + // assert against a key the server has never seen. If the backend later + // rejects it, the app calls DeviceIntegrity.resetAttestation() instead. SecureStorage store = SecureStorage.getInstance(); - store.set(KEY_STATE, STATE_ATTESTED); + store.set(KEY_STATE, STATE_PENDING); + store.set(KEY_PENDING_SINCE, Long.toString(System.currentTimeMillis())); if (instance != null) { synchronized (instance.flowLock) { instance.currentBackoff = MIN_BACKOFF_MILLIS; instance.bootstrapInFlight = false; - // Deliberately NOT asserted here. Apple has returned the - // attestation object, but the backend has not seen it yet -- the - // first caller's token is only completed below, and registration - // happens after that. An assertion sent now would reference a key - // the server does not know, which it would reject and which would - // trigger a pointless invalid-key reset. Queued callers are told - // to retry instead; by then the key is registered and their - // request costs one cheap assertion. + // Deliberately NOT asserted here, for the reason above. Queued + // callers are told to retry; by then the key is registered and + // their request costs one cheap assertion. instance.failBootstrapWaiters("App Attest is completing first-run " + "registration for this device; retry shortly"); } diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSImplementation.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSImplementation.java index f96f3a2e6fa..9c480bc7a51 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSImplementation.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSImplementation.java @@ -4580,6 +4580,11 @@ public void resetAttestation() { deviceIntegrity().resetAttestation(); } + @Override + public void confirmAttestation() { + deviceIntegrity().confirmAttestation(); + } + private IOSDeviceIntegrity deviceIntegrity() { if (deviceIntegrity == null) { deviceIntegrity = new IOSDeviceIntegrity(nativeInstance); diff --git a/maven/core-unittests/src/test/java/com/codename1/mcp/MCPLoopbackTransportOpenTest.java b/maven/core-unittests/src/test/java/com/codename1/mcp/MCPLoopbackTransportOpenTest.java index c9985e747b4..6872b64af19 100644 --- a/maven/core-unittests/src/test/java/com/codename1/mcp/MCPLoopbackTransportOpenTest.java +++ b/maven/core-unittests/src/test/java/com/codename1/mcp/MCPLoopbackTransportOpenTest.java @@ -29,6 +29,7 @@ import java.io.IOException; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -98,4 +99,139 @@ void closingTheTransportStopsTheListenerRatherThanOnlyFlaggingIt() throws Except "close must stop the loopback listener on its own port, got: " + implementation.getStoppedListeners()); } + + /// A transport that parks inside open() until the test lets it through, so the test can + /// stop the server at a known point in the reader thread's progress. + private static final class SlowOpenTransport implements MCPTransport { + private final Object gate = new Object(); + private boolean entered; + private boolean released; + /// When set, readMessage parks until close(), so the server stays up as a real one + /// would instead of ending its loop the instant it starts. + private boolean holdReads; + volatile boolean opened; + volatile int closeCount; + + SlowOpenTransport holdingReads() { + holdReads = true; + return this; + } + + void awaitEntered() throws InterruptedException { + synchronized (gate) { + while (!entered) { + gate.wait(5000); + } + } + } + + void release() { + synchronized (gate) { + released = true; + gate.notifyAll(); + } + } + + public void open() throws IOException { + synchronized (gate) { + entered = true; + gate.notifyAll(); + while (!released) { + try { + gate.wait(5000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IOException("interrupted"); + } + } + } + opened = true; + } + + public String readMessage() throws IOException { + synchronized (gate) { + while (holdReads && closeCount == 0) { + try { + gate.wait(5000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IOException("interrupted"); + } + } + } + return null; + } + + public void writeMessage(String message) throws IOException { + } + + public void close() { + synchronized (gate) { + closeCount++; + gate.notifyAll(); + } + } + } + + @Test + void stoppingWhileTheReaderThreadIsStillOpeningStillClosesTheTransport() throws Exception { + // start() hands the transport to a reader thread, so stop() can run before that + // thread finishes opening it. stop()'s close() then has nothing to release, and the + // transport goes on to finish opening with nobody left to close it. For the real + // socket transport that strands the process-wide registration, and every later + // server in the same JVM is refused with "already open on port". + MCPServer server = new MCPServer(); + SlowOpenTransport transport = new SlowOpenTransport(); + server.start(transport); + transport.awaitEntered(); + server.stop(); + transport.release(); + + // stop() already closed once, finding nothing open. The point of the fix is the + // second close, issued by the reader thread once it notices the server went away + // while it was opening -- that is what releases the registration it just took. + for (int i = 0; i < 500 && (!transport.opened || transport.closeCount < 2); i++) { + Thread.sleep(10); + } + assertTrue(transport.opened, "the reader thread should have completed open()"); + assertTrue(transport.closeCount >= 2, + "the reader thread must close the transport it opened after stop() had " + + "already run, got closeCount=" + transport.closeCount); + assertFalse(server.isRunning(), "the server should not be running after stop()"); + } + + @Test + void restartingWhileTheOldReaderIsStillOpeningDoesNotOrphanTheOldTransport() throws Exception { + // The shape that actually stranded the registration in CI. The reader thread for a + // stopped server was still parked in open(); by the time it got through, a restart + // had replaced the server's transport. Reading liveness off the server's shared + // fields, that thread saw "running" -- so it served the transport it had been given, + // blocked in readMessage forever, and never closed it. Its process-wide registration + // then outlived the test class, and the next server on that port was refused. + MCPServer server = new MCPServer(); + SlowOpenTransport orphan = new SlowOpenTransport(); + server.start(orphan); + orphan.awaitEntered(); + server.stop(); + + SlowOpenTransport replacement = new SlowOpenTransport().holdingReads(); + server.start(replacement); + replacement.awaitEntered(); + replacement.release(); + + // Only now does the first thread get out of open(), into a server that is running + // again -- over a different transport. + orphan.release(); + + for (int i = 0; i < 500 && (!orphan.opened || orphan.closeCount < 2); i++) { + Thread.sleep(10); + } + assertTrue(orphan.closeCount >= 2, + "the superseded transport must be closed by its own reader thread, got " + + "closeCount=" + orphan.closeCount); + assertTrue(server.isRunning(), + "the superseded thread must not stop the server that replaced it"); + + server.stop(); + } } diff --git a/scripts/run-javase-cef-ffmpeg-smoke.py b/scripts/run-javase-cef-ffmpeg-smoke.py index 8f21f2ab9a9..7909643c2e9 100644 --- a/scripts/run-javase-cef-ffmpeg-smoke.py +++ b/scripts/run-javase-cef-ffmpeg-smoke.py @@ -133,12 +133,18 @@ def install_runtime(): "-Plocal-dev-javase", "install", ] - # Maven Central intermittently serves 403/5xx from the runner's CDN edge and - # Maven treats those as PERMANENT resolution failures (observed: junit-bom - # 403 killed the whole smoke 53s in, before any project code built). Retry - # the install a couple of times with a pause -- a genuine build failure - # fails identically on every attempt. - attempts = 3 + # Maven Central intermittently serves 403/429/5xx from the runner's CDN edge + # and Maven treats those as PERMANENT resolution failures (observed: junit-bom + # 403 killed the whole smoke 53s in, before any project code built, and later + # a 429 killed it three times in a row while reading the root POM). Retry the + # install with a growing pause -- a genuine build failure fails identically on + # every attempt. + # + # The backoff has to grow. A flat 30s wait is inside the window Central is + # still rate limiting in, so all three attempts fail for the same reason and + # the retry buys nothing; a 429 in particular wants minutes, not seconds. + backoffs = [30, 120, 300] + attempts = len(backoffs) + 1 for attempt in range(1, attempts + 1): try: run(cmd, log_name="build-runtime.log", timeout=1800) @@ -146,9 +152,11 @@ def install_runtime(): except RuntimeError: if attempt == attempts: raise + delay = backoffs[attempt - 1] log(f"runtime install failed (attempt {attempt}/{attempts}); " - "retrying in 30s in case Maven Central was throwing transient 403/5xx") - time.sleep(30) + f"retrying in {delay}s in case Maven Central was throwing " + "transient 403/429/5xx") + time.sleep(delay) def run_smoke_app(video_file: Path, screenshot_path: Path, status_path: Path): From 907297eedb10f26e087c41ae43e2ef0cc827ae3f Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Wed, 29 Jul 2026 14:32:57 +0300 Subject: [PATCH 11/96] Guard the failure path, and stop asserting for a discarded key SpotBugs on the iOS port flagged the UTF-8 fallback in bytes(): falling back to the platform default would be worse than failing, since those bytes are hashed and the server recomputes the hash over UTF-8. UTF-8 is mandatory on every JVM, so the branch is unreachable and now says so. Two review findings: - nativeAssertionReady had no staleness check, unlike the key and attestation callbacks. A reset landing while an assertion was in flight -- typically because a concurrent request learned the backend does not recognise the key -- would still hand back an assertion for the key that was just discarded. - isProtected() called the pluggable engine's isAvailable() unguarded, and failOrContinue() consults it. An engine that throws there after partial initialization would turn a fail-open host into a blocked request, inverting the degradation contract. Unanswerable now means unprotected. setup-workspace.sh raises Maven's local-repository lock timeout. Both of its builds run with -T 1C, and the default 30s wait is not always enough on a cold cache under a loaded runner -- the Android script job died with "Could not acquire lock(s)" having compiled nothing wrong. Waiting longer costs nothing when there is no contention. Co-Authored-By: Claude Opus 5 (1M context) --- .../codename1/security/shield/AppShield.java | 11 ++++++++++- .../codename1/impl/ios/IOSDeviceIntegrity.java | 17 ++++++++++++++--- scripts/setup-workspace.sh | 11 +++++++++-- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/CodenameOne/src/com/codename1/security/shield/AppShield.java b/CodenameOne/src/com/codename1/security/shield/AppShield.java index d4e82146d6c..c7e15fa1cf6 100644 --- a/CodenameOne/src/com/codename1/security/shield/AppShield.java +++ b/CodenameOne/src/com/codename1/security/shield/AppShield.java @@ -147,7 +147,16 @@ private static void installNetworkGuard() { /// True when a real attestation engine is present and available. False in an open-source or /// unentitled build, and in the simulator unless simulation is switched on. public static boolean isProtected() { - return ShieldEngineRegistry.getEngine().isAvailable(); + // Guarded because the engine is pluggable and this is consulted on the failure + // path: a partially initialized engine whose isAvailable() throws would turn a + // fail-open host into a blocked request, which is the exact inversion the + // degradation contract promises will not happen. Unanswerable means unprotected. + try { + return ShieldEngineRegistry.getEngine().isAvailable(); + } catch (Throwable t) { + Log.e(t); + return false; + } } /// The active engine's name, for diagnostics and support logs. diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index 720b2e2a39e..4257760e189 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -22,7 +22,6 @@ */ package com.codename1.impl.ios; -import com.codename1.io.Log; import com.codename1.security.Hash; import com.codename1.security.SecureStorage; import com.codename1.ui.Display; @@ -437,6 +436,14 @@ public static void nativeAssertionReady(final int requestId, final String assert if (pending == null) { return; } + if (isStale(pending)) { + // A reset landed while this assertion was in flight -- typically because a + // concurrent request learned the backend does not recognise the key. Handing + // back an assertion for the key that was just discarded would send the + // server the very thing it already rejected. + fail(pending, "App Attest state was reset while this request was in flight"); + return; + } if (assertionB64 == null) { fail(pending, "App Attest assertion returned no data"); return; @@ -564,8 +571,12 @@ private static byte[] bytes(String s) { try { return s.getBytes("UTF-8"); } catch (java.io.UnsupportedEncodingException e) { - Log.e(e); - return s.getBytes(); + // Every JVM is required to support UTF-8, so this cannot happen. Falling + // back to the platform default would be worse than failing: these bytes are + // hashed and the server recomputes the same hash over UTF-8, so a silent + // re-encode would produce an attestation that never verifies and no + // indication of why. + throw new IllegalStateException("UTF-8 is unavailable on this VM", e); } } diff --git a/scripts/setup-workspace.sh b/scripts/setup-workspace.sh index 0e51853980b..1171f7dcc83 100755 --- a/scripts/setup-workspace.sh +++ b/scripts/setup-workspace.sh @@ -225,13 +225,20 @@ if [ ! -d "$CN1_BINARIES/.git" ]; then git clone --depth=1 --filter=blob:none https://github.com/codenameone/cn1-binaries "$CN1_BINARIES" fi +# Both builds below run with -T 1C, so several modules install into the local +# repository at once and contend for its lock. Maven's default wait is 30 seconds, +# and on a cold cache under a loaded runner that is not always enough -- the build +# then dies with "Could not acquire lock(s)" having compiled nothing wrong. Waiting +# longer costs nothing when there is no contention. +MVN_LOCK_ARGS="-Daether.syncContext.named.time=300 -Daether.syncContext.named.timeUnit=SECONDS" + log "Building Codename One core modules" -"$MAVEN_HOME/bin/mvn" -f maven/pom.xml -T 1C -Dmaven.javadoc.skip=true -Dmaven.source.skip=true -DskipTests -Djava.awt.headless=true -Dcn1.binaries="$CN1_BINARIES" -Dcodename1.platform=javase -P local-dev-javase,compile-android,!download-cn1-binaries install "$@" +"$MAVEN_HOME/bin/mvn" -f maven/pom.xml $MVN_LOCK_ARGS -T 1C -Dmaven.javadoc.skip=true -Dmaven.source.skip=true -DskipTests -Djava.awt.headless=true -Dcn1.binaries="$CN1_BINARIES" -Dcodename1.platform=javase -P local-dev-javase,compile-android,!download-cn1-binaries install "$@" log "Building Codename One Maven plugin" "$MAVEN_HOME/bin/mvn" -f maven/pom.xml \ -pl codenameone-maven-plugin -am \ - -T 1C -Dmaven.javadoc.skip=true -Dmaven.source.skip=true \ + $MVN_LOCK_ARGS -T 1C -Dmaven.javadoc.skip=true -Dmaven.source.skip=true \ -DskipTests -Djava.awt.headless=true \ -Dcn1.binaries="$CN1_BINARIES" \ -P !download-cn1-binaries \ From cba30f1a6a070f48ef2ee242d0273e1caa570798 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Wed, 29 Jul 2026 15:05:01 +0300 Subject: [PATCH 12/96] Satisfy the PMD gate for the new shield code The SpotBugs failure had been masking these: the quality gate prints and exits on SpotBugs first, so the PMD wall only appeared once SpotBugs was clean. All of it is in code this branch added. AvoidUsingVolatile is the one with substance. NetworkManager's guard field was volatile; the gate bans the modifier outright, and the core is built for Java 5 semantics anyway. getNetworkGuard() is now synchronized on the same monitor the two writers take, so the lock is what publishes the write to the network thread. The rest is mechanical: @Override on the interface implementations across the shield package, foreach for the indexed array loops, literals-first in the WebSocket reserved-header check, and the unnecessary java.util.Vector qualification in parseGroupedCertificates. PreserveStackTrace is annotated rather than restructured, on the throw line where PMD anchors it. The exception being thrown is not a new one: it is the original pin failure, recorded by the certificate callback because that callback can only answer with a boolean. Rethrowing it is what preserves its stack trace instead of losing it behind a generic connection error. Verified by running .github/scripts/generate-quality-report.py locally rather than through another CI round trip. Co-Authored-By: Claude Opus 5 (1M context) --- .../src/com/codename1/impl/WebSocketImpl.java | 6 +++--- .../src/com/codename1/io/ConnectionRequest.java | 11 +++++++---- .../src/com/codename1/io/NetworkManager.java | 10 +++++++--- .../com/codename1/security/shield/AppShield.java | 10 ++++++---- .../com/codename1/security/shield/HostPolicy.java | 1 + .../src/com/codename1/security/shield/PinSet.java | 5 +++-- .../security/shield/ShieldNetworkGuard.java | 5 +++++ .../codename1/security/shield/ShieldSignal.java | 1 + .../codename1/security/shield/ShieldSignals.java | 5 +++-- .../codename1/security/shield/ShieldStatus.java | 9 ++++++--- .../com/codename1/security/shield/ShieldToken.java | 1 + .../security/shield/spi/DefaultEngineContext.java | 11 +++++++++++ .../security/shield/spi/UnprotectedEngine.java | 14 ++++++++++++-- 13 files changed, 66 insertions(+), 23 deletions(-) diff --git a/CodenameOne/src/com/codename1/impl/WebSocketImpl.java b/CodenameOne/src/com/codename1/impl/WebSocketImpl.java index 2c2267b2d7f..29b204300bc 100644 --- a/CodenameOne/src/com/codename1/impl/WebSocketImpl.java +++ b/CodenameOne/src/com/codename1/impl/WebSocketImpl.java @@ -120,9 +120,9 @@ protected final void appendRequestHeaders(StringBuilder req) { private static boolean isReservedHandshakeHeader(String name) { String n = name.toLowerCase(); - return n.equals("host") || n.equals("upgrade") || n.equals("connection") - || n.equals("sec-websocket-key") || n.equals("sec-websocket-version") - || n.equals("sec-websocket-protocol") || n.equals("content-length"); + return "host".equals(n) || "upgrade".equals(n) || "connection".equals(n) + || "sec-websocket-key".equals(n) || "sec-websocket-version".equals(n) + || "sec-websocket-protocol".equals(n) || "content-length".equals(n); } private static boolean containsCrLf(String s) { diff --git a/CodenameOne/src/com/codename1/io/ConnectionRequest.java b/CodenameOne/src/com/codename1/io/ConnectionRequest.java index b2555b0f587..c68d4bd44ae 100644 --- a/CodenameOne/src/com/codename1/io/ConnectionRequest.java +++ b/CodenameOne/src/com/codename1/io/ConnectionRequest.java @@ -1243,7 +1243,11 @@ boolean performOperationComplete() throws IOException { if (pinFailure != null) { IOException cause = pinFailure; pinFailure = null; - throw cause; + // Not a new exception: this is the original pin failure, recorded by the + // certificate callback because that callback can only answer with a + // boolean. Rethrowing it here is what preserves its stack trace, rather + // than losing it behind the generic connection error. + throw cause; //NOPMD rethrow of the recorded cause, see above } throw ioe; } finally { @@ -1698,11 +1702,10 @@ static SSLCertificate[] parseGroupedCertificates(String[] entries) { // the network path, i.e. as an unrelated connection failure. return new SSLCertificate[0]; } - java.util.Vector out = new java.util.Vector(); + Vector out = new Vector(); SSLCertificate current = null; int index = 0; - for (int i = 0; i < entries.length; i++) { - String entry = entries[i]; + for (String entry : entries) { if (entry == null) { continue; } diff --git a/CodenameOne/src/com/codename1/io/NetworkManager.java b/CodenameOne/src/com/codename1/io/NetworkManager.java index f78d9abc1ce..b3d9302f481 100644 --- a/CodenameOne/src/com/codename1/io/NetworkManager.java +++ b/CodenameOne/src/com/codename1/io/NetworkManager.java @@ -207,7 +207,11 @@ public static NetworkManager getInstance() { return INSTANCE; } - private static volatile NetworkGuard networkGuard; + /// Read through [#getNetworkGuard()], which synchronizes on the same monitor the two + /// writers below take. Not `volatile`: the core is built for Java 5 semantics and the + /// repo's PMD gate forbids the modifier outright, so the lock is what publishes the + /// write to the network thread. + private static NetworkGuard networkGuard; private static boolean networkGuardSealed; /// Installs the app-wide [NetworkGuard]. @@ -231,7 +235,7 @@ public static void setNetworkGuard(NetworkGuard guard) { } /// The installed guard, or null when none was installed. - public static NetworkGuard getNetworkGuard() { + public static synchronized NetworkGuard getNetworkGuard() { return networkGuard; } @@ -1168,7 +1172,7 @@ private boolean runCurrentRequest(@Async.Execute ConnectionRequest req) { if (requestWasCompleted) { req.complete = true; } - NetworkGuard guard = networkGuard; + NetworkGuard guard = getNetworkGuard(); if (guard != null) { try { guard.afterResponse(req, req.getResponseCode(), req.getGuardHeaders()); diff --git a/CodenameOne/src/com/codename1/security/shield/AppShield.java b/CodenameOne/src/com/codename1/security/shield/AppShield.java index c7e15fa1cf6..30dedf8d0fd 100644 --- a/CodenameOne/src/com/codename1/security/shield/AppShield.java +++ b/CodenameOne/src/com/codename1/security/shield/AppShield.java @@ -199,6 +199,7 @@ public static AsyncResource fetchToken(final String bindingData) { return result; } Display.getInstance().scheduleBackgroundTask(new Runnable() { + @Override public void run() { try { ShieldToken token = ShieldEngineRegistry.getEngine().fetchToken(bindingData); @@ -416,8 +417,8 @@ public static ShieldSignal[] getSignals() { fromEngine = new ShieldSignal[0]; } if (fromEngine != null) { - for (int i = 0; i < fromEngine.length; i++) { - ShieldSignals.add(fromEngine[i]); + for (ShieldSignal s : fromEngine) { + ShieldSignals.add(s); } } return ShieldSignals.snapshot(); @@ -488,9 +489,10 @@ private static final class StatusDispatch implements Runnable { this.status = status; } + @Override public void run() { - for (int i = 0; i < targets.length; i++) { - targets[i].statusChanged(status); + for (ShieldListener target : targets) { + target.statusChanged(status); } } } diff --git a/CodenameOne/src/com/codename1/security/shield/HostPolicy.java b/CodenameOne/src/com/codename1/security/shield/HostPolicy.java index f76ddbf5db4..4c4f0cfde83 100644 --- a/CodenameOne/src/com/codename1/security/shield/HostPolicy.java +++ b/CodenameOne/src/com/codename1/security/shield/HostPolicy.java @@ -74,6 +74,7 @@ public boolean isNoOp() { return !attachToken && !enforcePins; } + @Override public String toString() { return "HostPolicy[token=" + attachToken + ", pins=" + enforcePins + ", onFailure=" + failureMode + "]"; diff --git a/CodenameOne/src/com/codename1/security/shield/PinSet.java b/CodenameOne/src/com/codename1/security/shield/PinSet.java index 7728c75de9e..ba112b1d4db 100644 --- a/CodenameOne/src/com/codename1/security/shield/PinSet.java +++ b/CodenameOne/src/com/codename1/security/shield/PinSet.java @@ -160,8 +160,8 @@ public boolean matches(String host, String[] chainSpkiDigests) { return false; } Vector pins = pinsFor(host); - for (int i = 0; i < chainSpkiDigests.length; i++) { - if (chainSpkiDigests[i] != null && pins.contains(chainSpkiDigests[i])) { + for (String digest : chainSpkiDigests) { + if (digest != null && pins.contains(digest)) { return true; } } @@ -173,6 +173,7 @@ public boolean isEmpty() { return hostToPins.isEmpty(); } + @Override public String toString() { return "PinSet[version=" + version + ", hosts=" + hostToPins.size() + ", stale=" + isStale() + ", expired=" + isExpired() + "]"; diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldNetworkGuard.java b/CodenameOne/src/com/codename1/security/shield/ShieldNetworkGuard.java index 9f9be57befb..61283554621 100644 --- a/CodenameOne/src/com/codename1/security/shield/ShieldNetworkGuard.java +++ b/CodenameOne/src/com/codename1/security/shield/ShieldNetworkGuard.java @@ -39,12 +39,14 @@ /// could only weaken this one. final class ShieldNetworkGuard implements NetworkGuard { + @Override public void beforeRequest(ConnectionRequest request) throws IOException { // Also clears the header when the host is not protected, which is what // stops a token following a cross-host redirect. AppShield.attach(request); } + @Override public boolean isCertificateCheckRequired(String url) { String host = AppShield.hostOf(url); if (host == null || !AppShield.policyFor(host).isEnforcePins()) { @@ -56,6 +58,7 @@ public boolean isCertificateCheckRequired(String url) { return AppShield.getPinSet().isEnforcedFor(host); } + @Override public void checkCertificates(ConnectionRequest request, ConnectionRequest.SSLCertificate[] certificates) throws IOException { String host = AppShield.hostOf(request.getUrl()); @@ -86,10 +89,12 @@ public void checkCertificates(ConnectionRequest request, } } + @Override public String[] interestingResponseHeaders() { return new String[] {AppShield.REJECT_HEADER}; } + @Override public void afterResponse(ConnectionRequest request, int responseCode, String[] headers) { if (responseCode != 401 && responseCode != 403) { return; diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldSignal.java b/CodenameOne/src/com/codename1/security/shield/ShieldSignal.java index 13c6c6f4a15..d8d0cdc7b2a 100644 --- a/CodenameOne/src/com/codename1/security/shield/ShieldSignal.java +++ b/CodenameOne/src/com/codename1/security/shield/ShieldSignal.java @@ -79,6 +79,7 @@ public long getTimestamp() { return timestamp; } + @Override public String toString() { return id + "(" + severity + (detail == null ? "" : ", " + detail) + ")"; } diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldSignals.java b/CodenameOne/src/com/codename1/security/shield/ShieldSignals.java index 7a4efd76898..51116188873 100644 --- a/CodenameOne/src/com/codename1/security/shield/ShieldSignals.java +++ b/CodenameOne/src/com/codename1/security/shield/ShieldSignals.java @@ -145,9 +145,10 @@ private static final class SignalDispatch implements Runnable { this.signal = signal; } + @Override public void run() { - for (int i = 0; i < targets.length; i++) { - targets[i].signalRaised(signal); + for (ShieldListener target : targets) { + target.signalRaised(signal); } } } diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldStatus.java b/CodenameOne/src/com/codename1/security/shield/ShieldStatus.java index b2e2c891d90..bf43f91eec1 100644 --- a/CodenameOne/src/com/codename1/security/shield/ShieldStatus.java +++ b/CodenameOne/src/com/codename1/security/shield/ShieldStatus.java @@ -105,18 +105,20 @@ public static ShieldStatus forId(String id) { if (id == null) { return NOT_INITIALIZED; } - for (int i = 0; i < KNOWN.length; i++) { - if (KNOWN[i].id.equals(id)) { - return KNOWN[i]; + for (ShieldStatus known : KNOWN) { + if (known.id.equals(id)) { + return known; } } return new ShieldStatus(id, false); } + @Override public String toString() { return id; } + @Override public boolean equals(Object o) { if (this == o) { return true; @@ -127,6 +129,7 @@ public boolean equals(Object o) { return id.equals(((ShieldStatus) o).id); } + @Override public int hashCode() { return id.hashCode(); } diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldToken.java b/CodenameOne/src/com/codename1/security/shield/ShieldToken.java index e6cdfa9ba45..d8fc4f250d9 100644 --- a/CodenameOne/src/com/codename1/security/shield/ShieldToken.java +++ b/CodenameOne/src/com/codename1/security/shield/ShieldToken.java @@ -129,6 +129,7 @@ public long getFetchedAt() { } /// Never renders the token value -- these strings end up in logs. + @Override public String toString() { return "ShieldToken[status=" + status.getId() + ", fetchedAt=" + fetchedAt diff --git a/CodenameOne/src/com/codename1/security/shield/spi/DefaultEngineContext.java b/CodenameOne/src/com/codename1/security/shield/spi/DefaultEngineContext.java index 35c1c8a3756..ad05f995161 100644 --- a/CodenameOne/src/com/codename1/security/shield/spi/DefaultEngineContext.java +++ b/CodenameOne/src/com/codename1/security/shield/spi/DefaultEngineContext.java @@ -42,14 +42,17 @@ final class DefaultEngineContext implements EngineContext { private DefaultEngineContext() { } + @Override public SecureStorage getSecureStorage() { return SecureStorage.getInstance(); } + @Override public AsyncResource requestPlatformAttestation(String nonce) { return DeviceIntegrity.requestIntegrityToken(nonce); } + @Override public boolean isPlatformAttestationSupported() { try { return DeviceIntegrity.isAttestationSupported(); @@ -58,6 +61,7 @@ public boolean isPlatformAttestationSupported() { } } + @Override public void resetPlatformAttestation() { try { DeviceIntegrity.resetAttestation(); @@ -66,6 +70,7 @@ public void resetPlatformAttestation() { } } + @Override public void confirmPlatformAttestation() { try { DeviceIntegrity.confirmAttestation(); @@ -74,6 +79,7 @@ public void confirmPlatformAttestation() { } } + @Override public String[] getPlatformCompromiseReasons() { try { String[] r = DeviceIntegrity.getCompromiseReasons(); @@ -83,6 +89,7 @@ public String[] getPlatformCompromiseReasons() { } } + @Override public String[] getEnabledAccessibilityServices() { try { String[] r = DeviceIntegrity.getEnabledAccessibilityServices(); @@ -92,6 +99,7 @@ public String[] getEnabledAccessibilityServices() { } } + @Override public String[] getAppSignerDigests() { try { String[] r = Display.getInstance().getAppSignerDigests(); @@ -101,6 +109,7 @@ public String[] getAppSignerDigests() { } } + @Override public String getProperty(String key, String defaultValue) { try { return Display.getInstance().getProperty(key, defaultValue); @@ -109,10 +118,12 @@ public String getProperty(String key, String defaultValue) { } } + @Override public void log(String message) { Log.p(message); } + @Override public void publishSignal(ShieldSignal signal) { ShieldSignals.add(signal); } diff --git a/CodenameOne/src/com/codename1/security/shield/spi/UnprotectedEngine.java b/CodenameOne/src/com/codename1/security/shield/spi/UnprotectedEngine.java index d29b2b8aed7..bf0f546c140 100644 --- a/CodenameOne/src/com/codename1/security/shield/spi/UnprotectedEngine.java +++ b/CodenameOne/src/com/codename1/security/shield/spi/UnprotectedEngine.java @@ -54,14 +54,17 @@ final class UnprotectedEngine implements ShieldEngine { private UnprotectedEngine() { } + @Override public String getName() { return "unprotected"; } + @Override public boolean isAvailable() { return false; } + @Override public void initialize(EngineContext ctx, ShieldConfig config) { if (ctx != null) { ctx.log("AppShield: no attestation engine registered; running unprotected. " @@ -69,23 +72,28 @@ public void initialize(EngineContext ctx, ShieldConfig config) { } } + @Override public ShieldToken fetchToken(String bindingData) throws ShieldException { throw new ShieldException(ShieldStatus.UNPROTECTED, "This build has no attestation engine, so no token can be issued."); } + @Override public ShieldToken getCachedToken() { return null; } + @Override public boolean verifyPins(String host, String[] spkiDigests, String[] certDigests) { return true; } + @Override public PinSet getPinSet() { return PinSet.EMPTY; } + @Override public ShieldSignal[] collectSignals() { String[] reasons; try { @@ -99,8 +107,8 @@ public ShieldSignal[] collectSignals() { return new ShieldSignal[0]; } Vector out = new Vector(); - for (int i = 0; i < reasons.length; i++) { - ShieldSignal s = toSignal(reasons[i]); + for (String reason : reasons) { + ShieldSignal s = toSignal(reason); if (s != null) { out.addElement(s); } @@ -131,9 +139,11 @@ private static ShieldSignal toSignal(String reason) { return new ShieldSignal(reason, 50, null); } + @Override public void invalidate() { } + @Override public void shutdown() { } } From 2a9246ccc5b30e473ed44ee77e4c216460e35638 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Wed, 29 Jul 2026 15:59:10 +0300 Subject: [PATCH 13/96] Close five races and bypasses in the attestation client iOS App Attest callbacks: - The staleness check and the state writes it guards now happen inside flowLock, the lock resetAttestation() takes. Split apart, a reset landing between them deleted the key and bumped the generation, these writes put the deleted key back, and attestKey() then registered it under the new generation -- so every later staleness check accepted the bootstrap that was supposed to have been abandoned. fail() only schedules onto the EDT, so calling it under the lock is safe. - SecureStorage.set() results are checked. A refused keychain write left the key attested and delivered for registration while the next request could not find it, so it generated another rate-limited hardware key, and another, until Apple throttled the device -- the exact failure attest-once exists to avoid. A failed write now aborts the bootstrap and fails the queued waiters with it. Pinning on iOS: setInsecure(true) returned from the authentication challenge before the chain was ever offered to Java, so a host with enforced pins accepted any certificate at all as long as the request happened to be insecure. The chain is now collected and vetoed first; only after Java approves does the insecure path substitute useCredential for default handling. That keeps setInsecure meaning "override OS trust evaluation for a self-signed server" rather than "stop looking". Unaffected for ordinary insecure requests, which return true from the callback when no inspection is required. Android SecureStorage: - plainPrefs() resolves through getContext() instead of requiring an Activity. A port initialized from a background service has no Activity but does have a context, and this tier exists precisely so a background caller can read a cached secret -- get() called it outside its try/catch, so Secrets.get() crashed rather than returning the value. - The whole load-check-generate on CN1PlainKey is serialized, not just the generation. Two first writers could each generate under the alias, and the second generation replaces the key the first already encrypted with, leaving that ciphertext permanently undecryptable. Co-Authored-By: Claude Opus 5 (1M context) --- .../impl/android/AndroidSecureStorage.java | 108 +++++++++++++----- .../nativeSources/NetworkConnectionImpl.m | 25 ++-- .../impl/ios/IOSDeviceIntegrity.java | 92 ++++++++++----- 3 files changed, 160 insertions(+), 65 deletions(-) diff --git a/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java b/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java index 556273f1664..3089c805405 100644 --- a/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java +++ b/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java @@ -99,6 +99,13 @@ public final class AndroidSecureStorage extends SecureStorage { */ private static final String PLAIN_KEY_ID = "CN1PlainKey"; private static final String PLAIN_PREFS = "CN1PlainSecureStorage"; + + /** + * Serializes load-check-generate on the non-prompting key, and the shared + * AndroidKeyStore handle with it. Static because the keystore alias is + * process-wide, so two instances would race just as two threads would. + */ + private static final Object PLAIN_KEY_LOCK = new Object(); private static final int GCM_TAG_BITS = 128; private KeyStore keyStore; @@ -220,7 +227,11 @@ public boolean set(String account, String value) { Cipher c = Cipher.getInstance("AES/GCM/NoPadding"); c.init(Cipher.ENCRYPT_MODE, key); byte[] enc = c.doFinal(value.getBytes("UTF-8")); - plainPrefs().edit() + SharedPreferences prefs = plainPrefs(); + if (prefs == null) { + return false; + } + prefs.edit() .putString(account, Base64.encodeToString(c.getIV(), Base64.NO_WRAP) + ":" + Base64.encodeToString(enc, Base64.NO_WRAP)) .apply(); @@ -243,7 +254,11 @@ public String get(String account) { if (Build.VERSION.SDK_INT < 23) { return legacyPlainGet(account); } - String stored = plainPrefs().getString(account, null); + SharedPreferences prefs = plainPrefs(); + if (prefs == null) { + return null; + } + String stored = prefs.getString(account, null); if (stored == null) { return null; } @@ -282,13 +297,30 @@ public boolean remove(String account) { if (account == null) { return false; } - plainPrefs().edit().remove(account).apply(); + SharedPreferences prefs = plainPrefs(); + if (prefs == null) { + return false; + } + prefs.edit().remove(account).apply(); return true; } + /** + * The preferences file, resolved from the application context rather than an + * Activity. + * + *

A port initialized from a background service has no Activity but does + * have a context, and this tier exists precisely so a background caller can + * read a cached secret without prompting. Requiring an Activity would make + * {@code get()} throw there -- outside its try/catch, so the caller crashes + * rather than reading the value it asked for.

+ */ private SharedPreferences plainPrefs() { - return AndroidNativeUtil.getActivity() - .getApplicationContext() + Context ctx = AndroidNativeUtil.getContext(); + if (ctx == null) { + return null; + } + return ctx.getApplicationContext() .getSharedPreferences(PLAIN_PREFS, Context.MODE_PRIVATE); } @@ -298,31 +330,43 @@ private SharedPreferences plainPrefs() { * generation fails. */ private SecretKey plainKey(boolean create) throws Exception { - keyStore().load(null); - SecretKey existing = (SecretKey) keyStore.getKey(PLAIN_KEY_ID, null); - if (existing != null || !create) { - return existing; - } - KeyGenerator gen = KeyGenerator.getInstance( - KeyProperties.KEY_ALGORITHM_AES, ANDROID_KEY_STORE); - gen.init(new KeyGenParameterSpec.Builder(PLAIN_KEY_ID, - KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT) - .setBlockModes(KeyProperties.BLOCK_MODE_GCM) - .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE) - .setKeySize(256) - .setRandomizedEncryptionRequired(true) - .build()); - gen.generateKey(); - return (SecretKey) keyStore.getKey(PLAIN_KEY_ID, null); + // The whole load-check-generate sequence is serialized, not just the + // generation. Two first writers that each saw the alias absent would each + // generate under it, and the second generation replaces the key the first + // one had already encrypted with -- leaving that ciphertext permanently + // undecryptable. The shared KeyStore is not thread safe either. + synchronized (PLAIN_KEY_LOCK) { + keyStore().load(null); + SecretKey existing = (SecretKey) keyStore.getKey(PLAIN_KEY_ID, null); + if (existing != null || !create) { + return existing; + } + KeyGenerator gen = KeyGenerator.getInstance( + KeyProperties.KEY_ALGORITHM_AES, ANDROID_KEY_STORE); + gen.init(new KeyGenParameterSpec.Builder(PLAIN_KEY_ID, + KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT) + .setBlockModes(KeyProperties.BLOCK_MODE_GCM) + .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE) + .setKeySize(256) + .setRandomizedEncryptionRequired(true) + .build()); + gen.generateKey(); + return (SecretKey) keyStore.getKey(PLAIN_KEY_ID, null); + } } private void resetPlainKey() { - try { - keyStore().deleteEntry(PLAIN_KEY_ID); - } catch (KeyStoreException e) { - Log.e(e); + synchronized (PLAIN_KEY_LOCK) { + try { + keyStore().deleteEntry(PLAIN_KEY_ID); + } catch (KeyStoreException e) { + Log.e(e); + } + } + SharedPreferences prefs = plainPrefs(); + if (prefs != null) { + prefs.edit().clear().apply(); } - plainPrefs().edit().clear().apply(); } // API 22 and below have no KeyGenParameterSpec. The preferences file is @@ -331,7 +375,11 @@ private void resetPlainKey() { private boolean legacyPlainSet(String account, String value) { warnLegacyPlainStorage(); try { - plainPrefs().edit() + SharedPreferences prefs = plainPrefs(); + if (prefs == null) { + return false; + } + prefs.edit() .putString(account, Base64.encodeToString( value.getBytes("UTF-8"), Base64.NO_WRAP)) .apply(); @@ -344,7 +392,11 @@ private boolean legacyPlainSet(String account, String value) { private String legacyPlainGet(String account) { warnLegacyPlainStorage(); - String stored = plainPrefs().getString(account, null); + SharedPreferences prefs = plainPrefs(); + if (prefs == null) { + return null; + } + String stored = prefs.getString(account, null); if (stored == null) { return null; } diff --git a/Ports/iOSPort/nativeSources/NetworkConnectionImpl.m b/Ports/iOSPort/nativeSources/NetworkConnectionImpl.m index 83d1d79e9a6..412a99070f4 100644 --- a/Ports/iOSPort/nativeSources/NetworkConnectionImpl.m +++ b/Ports/iOSPort/nativeSources/NetworkConnectionImpl.m @@ -166,11 +166,12 @@ -(void) connection: (NSURLConnection*)connection willSendRequestForAuthenticatio SecTrustRef trustRef = [[challenge protectionSpace] serverTrust]; SecTrustEvaluate(trustRef, NULL); NSMutableString* certs = [NSMutableString string]; - if (insecure) { - [[challenge sender] useCredential:[NSURLCredential credentialForTrust:[[challenge protectionSpace] serverTrust]] forAuthenticationChallenge:challenge]; - return; - } - //[connection cancel]; + // The chain is collected and offered to Java even for an insecure request. An + // insecure request asks us to accept a certificate the OS would reject -- a + // self-signed development server -- and that is a decision about OS trust + // evaluation, not a decision to stop looking. Returning here meant a host with + // enforced pins accepted any certificate at all as long as the request happened + // to be insecure, which is the opposite of what pinning is for. CFIndex count = SecTrustGetCertificateCount(trustRef); for (int i=0; i Date: Wed, 29 Jul 2026 17:09:01 +0300 Subject: [PATCH 14/96] Fold hostnames by ASCII, and close two more reset races Host matching used String.toLowerCase(), which folds by device locale. Under the Turkish locale an uppercase ASCII I becomes the dotless i, so a request to API.example.com stops matching a policy registered as api.example.com -- and the failure is silent: the host simply looks unprotected, so no token is attached and no pin is enforced, on exactly the devices the developer never tests with. All seven sites in the shield package now go through ShieldHosts, which folds ASCII by hand. That removes the locale from the question rather than passing Locale.ENGLISH everywhere, and keeps working on the ports with a reduced java.util.Locale. iOS App Attest, two more of the same family as the last round: - resetAttestation() removed the stored state before taking flowLock, leaving a window where a callback could take the lock, pass its staleness check -- the generation had not been bumped yet -- and write the discarded key straight back. The reset then marked only that callback stale and left the resurrected key for the next bootstrap. The lock is now held across the removals and the generation bump. - A failed KEY_PENDING_SINCE write was ignored, and registrationGraceRemaining() reads a missing timestamp as an expired window -- so the next request promoted the key to attested and asserted against a key no backend had acknowledged. It now rolls the state back to new and fails the bootstrap. Co-Authored-By: Claude Opus 5 (1M context) --- .../codename1/security/shield/AppShield.java | 8 +- .../com/codename1/security/shield/PinSet.java | 2 +- .../security/shield/ShieldConfig.java | 4 +- .../security/shield/ShieldHosts.java | 81 +++++++++++++++++++ .../impl/ios/IOSDeviceIntegrity.java | 31 +++++-- 5 files changed, 113 insertions(+), 13 deletions(-) create mode 100644 CodenameOne/src/com/codename1/security/shield/ShieldHosts.java diff --git a/CodenameOne/src/com/codename1/security/shield/AppShield.java b/CodenameOne/src/com/codename1/security/shield/AppShield.java index 30dedf8d0fd..9504bdbedfd 100644 --- a/CodenameOne/src/com/codename1/security/shield/AppShield.java +++ b/CodenameOne/src/com/codename1/security/shield/AppShield.java @@ -289,7 +289,7 @@ public static void attach(ConnectionRequest request) throws ShieldException { /// True for an absolute https URL. Anything else -- http, or a relative URL we cannot /// classify -- is not somewhere a bearer token belongs. static boolean isSecure(String url) { - return url != null && url.toLowerCase().startsWith("https://"); + return ShieldHosts.startsWithIgnoreCase(url, "https://"); } private static void failOrContinue(HostPolicy policy, ShieldException e) throws ShieldException { @@ -353,7 +353,7 @@ public static void addProtectedHost(String host, HostPolicy policy) { // Same rule as ShieldConfig.protect: an omitted policy has to pick up // the configured default failure mode, or setting a fail-closed // default silently does nothing on this path too. - runtimeHosts.put(host.toLowerCase(), + runtimeHosts.put(ShieldHosts.normalize(host), policy == null ? implicitPolicy() : policy); } } @@ -378,7 +378,7 @@ public static HostPolicy policyFor(String host) { if (host == null) { return HostPolicy.UNPROTECTED; } - Object runtime = runtimeHosts.get(host.toLowerCase()); + Object runtime = runtimeHosts.get(ShieldHosts.normalize(host)); if (runtime != null) { return (HostPolicy) runtime; } @@ -526,7 +526,7 @@ static String hostOf(String url) { if (colon >= 0 && authority.indexOf(']') < colon) { authority = authority.substring(0, colon); } - return authority.length() == 0 ? null : authority.toLowerCase(); + return authority.length() == 0 ? null : ShieldHosts.normalize(authority); } private static com.codename1.security.shield.spi.EngineContext contextForEngine() { diff --git a/CodenameOne/src/com/codename1/security/shield/PinSet.java b/CodenameOne/src/com/codename1/security/shield/PinSet.java index ba112b1d4db..f28b8c8d650 100644 --- a/CodenameOne/src/com/codename1/security/shield/PinSet.java +++ b/CodenameOne/src/com/codename1/security/shield/PinSet.java @@ -107,7 +107,7 @@ public Vector pinsFor(String host) { if (host == null) { return null; } - String h = host.toLowerCase(); + String h = ShieldHosts.normalize(host); Object exact = hostToPins.get(h); if (exact != null) { return copyOf((Vector) exact); diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java b/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java index e1ed7b6368d..6fc70b2cb28 100644 --- a/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java +++ b/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java @@ -100,7 +100,7 @@ public ShieldConfig collectSignals(boolean collect) { /// subdomains. Hosts not registered here are never touched. public ShieldConfig protect(String hostPattern, HostPolicy policy) { if (hostPattern != null && hostPattern.length() > 0) { - hostPolicies.put(hostPattern.toLowerCase(), + hostPolicies.put(ShieldHosts.normalize(hostPattern), policy == null ? implicitPolicy() : policy); } return this; @@ -152,7 +152,7 @@ public HostPolicy policyFor(String host) { if (host == null) { return HostPolicy.UNPROTECTED; } - String h = host.toLowerCase(); + String h = ShieldHosts.normalize(host); Object exact = hostPolicies.get(h); if (exact != null) { return (HostPolicy) exact; diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldHosts.java b/CodenameOne/src/com/codename1/security/shield/ShieldHosts.java new file mode 100644 index 00000000000..2cdb7f6a69b --- /dev/null +++ b/CodenameOne/src/com/codename1/security/shield/ShieldHosts.java @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ +package com.codename1.security.shield; + +/// Case folding for hostnames and URL schemes, fixed to ASCII. +/// +/// #### Why not `String.toLowerCase()` +/// +/// It folds using the device's locale. Under the Turkish locale an uppercase ASCII `I` becomes the +/// dotless `ı`, so a request to `API.example.com` stops matching a policy registered as +/// `api.example.com`. The consequence is not a display glitch: the host silently looks unprotected, +/// so no token is attached and no pin is enforced -- on precisely the devices whose users the +/// developer never tests with. +/// +/// `toLowerCase(Locale.ENGLISH)` would fix it too, but hostnames and URL schemes are ASCII by +/// definition, so folding them by hand removes the locale from the question entirely and keeps this +/// working on the ports with a reduced `java.util.Locale`. +final class ShieldHosts { + + private ShieldHosts() { + } + + /// Lowercases the ASCII letters and leaves every other character alone. Null in, null out. + static String normalize(String s) { + if (s == null) { + return null; + } + int len = s.length(); + StringBuilder sb = null; + for (int i = 0; i < len; i++) { + char c = s.charAt(i); + if (c >= 'A' && c <= 'Z') { + if (sb == null) { + sb = new StringBuilder(len); + sb.append(s, 0, i); + } + sb.append((char) (c + 32)); + } else if (sb != null) { + sb.append(c); + } + } + return sb == null ? s : sb.toString(); + } + + /// True when the URL carries the given lowercase ASCII scheme prefix, whatever case it is in. + static boolean startsWithIgnoreCase(String value, String lowerPrefix) { + if (value == null || lowerPrefix == null || value.length() < lowerPrefix.length()) { + return false; + } + for (int i = 0; i < lowerPrefix.length(); i++) { + char c = value.charAt(i); + if (c >= 'A' && c <= 'Z') { + c = (char) (c + 32); + } + if (c != lowerPrefix.charAt(i)) { + return false; + } + } + return true; + } +} diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index 83825c4a1f0..29399abd3ed 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -163,12 +163,18 @@ boolean isSupported() { * restore to a new device, or an OS-side invalidation. */ void resetAttestation() { - SecureStorage store = SecureStorage.getInstance(); - store.remove(KEY_ID); - store.remove(KEY_STATE); - store.remove(KEY_RETRY_AFTER); - store.remove(KEY_PENDING_SINCE); + // The lock is taken BEFORE the removals, so deleting the state and invalidating + // the generation are one step. Removing first left a window in which a callback + // could take the lock, pass its staleness check -- the generation had not been + // bumped yet -- and write the discarded key straight back. The reset would then + // mark only that callback stale and leave the resurrected key behind for the + // next bootstrap to find. synchronized (flowLock) { + SecureStorage store = SecureStorage.getInstance(); + store.remove(KEY_ID); + store.remove(KEY_STATE); + store.remove(KEY_RETRY_AFTER); + store.remove(KEY_PENDING_SINCE); currentBackoff = MIN_BACKOFF_MILLIS; bootstrapInFlight = false; // Any callback still outstanding now belongs to an abandoned flow. @@ -446,7 +452,20 @@ public static void nativeAttestationReady(final int requestId, final String atte "App Attest could not record its attestation state"); return; } - store.set(KEY_PENDING_SINCE, Long.toString(System.currentTimeMillis())); + if (!store.set(KEY_PENDING_SINCE, Long.toString(System.currentTimeMillis()))) { + // Part of the same state transition, not a nicety: with no timestamp, + // registrationGraceRemaining() reads the window as already expired, so + // the very next request promotes the key to attested and asserts against + // a key no backend has acknowledged -- the first-use rejection and + // pointless key reset this state exists to prevent. Roll back to new so + // the key is attested again rather than used prematurely. + store.set(KEY_STATE, STATE_NEW); + instance.bootstrapInFlight = false; + fail(pending, "App Attest could not record its registration deadline"); + instance.failBootstrapWaiters( + "App Attest could not record its registration deadline"); + return; + } instance.currentBackoff = MIN_BACKOFF_MILLIS; instance.bootstrapInFlight = false; // Deliberately NOT asserted here, for the reason above. Queued callers From 598df78867ddba66a97fe954a5a8f262912c8d89 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Wed, 29 Jul 2026 17:24:49 +0300 Subject: [PATCH 15/96] Close the recovery race, stale guard state, and a legacy read Invalid-key recovery discarded the identity and claimed the replacement bootstrap in two separate lock acquisitions. In the gap a concurrent requestToken() saw no key and no bootstrap running, started its own generation, and then the recovery path started another -- two rate-limited hardware keys racing to become the persisted identity. resetLocked() lets both happen under one acquisition. A reused ConnectionRequest retained its response code and guard headers, so an attempt that failed before reaching a response, or completed from the offline cache, replayed the earlier 401 and its rejection header to afterResponse() -- invalidating a token that was never refused. Both are cleared at the top of each attempt, and afterResponse only runs when the attempt actually observed a response. A value first written on API 22 or below has no IV separator, so after an OS upgrade to 23+ the AES path read it and reported the secret missing forever. It is now decoded and re-stored encrypted, so the upgrade costs one migration rather than a cached credential. Co-Authored-By: Claude Opus 5 (1M context) --- .../com/codename1/io/ConnectionRequest.java | 18 ++++++++ .../src/com/codename1/io/NetworkManager.java | 2 +- .../impl/android/AndroidSecureStorage.java | 21 ++++++++- .../impl/ios/IOSDeviceIntegrity.java | 45 ++++++++++++------- 4 files changed, 67 insertions(+), 19 deletions(-) diff --git a/CodenameOne/src/com/codename1/io/ConnectionRequest.java b/CodenameOne/src/com/codename1/io/ConnectionRequest.java index c68d4bd44ae..3c5ba958130 100644 --- a/CodenameOne/src/com/codename1/io/ConnectionRequest.java +++ b/CodenameOne/src/com/codename1/io/ConnectionRequest.java @@ -972,6 +972,12 @@ boolean performOperationComplete() throws IOException { // certificate on a retried request, or rejecting a redirect to a // differently pinned host. sslCertificates = null; + // Same reasoning for the guard's view of the response. A ConnectionRequest can + // be reused, and a retained 401 plus its rejection header would be replayed to + // afterResponse() by an attempt that failed before reaching a response or was + // served from the cache -- invalidating a token that was never refused. + guardHeaders = null; + guardResponseCaptured = false; if (cacheMode == CachingMode.OFFLINE || cacheMode == CachingMode.OFFLINE_FIRST) { InputStream is = null; //NOPMD CloseResource try { @@ -1351,9 +1357,15 @@ protected void readHeaders(Object connection) throws IOException { /// is still open because it is closed before `afterResponse` runs. private String[] guardHeaders; + /// Whether the current attempt got far enough to have a response of its own. Guards the + /// reuse case: without it a retained response code from an earlier attempt is reported + /// as though it belonged to this one. + private boolean guardResponseCaptured; + private void captureGuardHeaders(Object connection) { NetworkGuard guard = NetworkManager.getNetworkGuard(); guardHeaders = null; + guardResponseCaptured = true; if (guard == null) { return; } @@ -1377,6 +1389,12 @@ String[] getGuardHeaders() { return guardHeaders; } + /// True when this attempt actually observed a response, so its code and headers describe + /// this request rather than a previous use of the same object. + boolean hasGuardResponse() { + return guardResponseCaptured; + } + /// Allows reading the headers from the connection by calling the getHeader() method when a response that isn't 200 OK is sent. /// /// #### Parameters diff --git a/CodenameOne/src/com/codename1/io/NetworkManager.java b/CodenameOne/src/com/codename1/io/NetworkManager.java index b3d9302f481..1bf744f59a7 100644 --- a/CodenameOne/src/com/codename1/io/NetworkManager.java +++ b/CodenameOne/src/com/codename1/io/NetworkManager.java @@ -1173,7 +1173,7 @@ private boolean runCurrentRequest(@Async.Execute ConnectionRequest req) { req.complete = true; } NetworkGuard guard = getNetworkGuard(); - if (guard != null) { + if (guard != null && req.hasGuardResponse()) { try { guard.afterResponse(req, req.getResponseCode(), req.getGuardHeaders()); } catch (Throwable t) { diff --git a/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java b/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java index 3089c805405..7d7b20b375f 100644 --- a/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java +++ b/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java @@ -264,7 +264,16 @@ public String get(String account) { } int sep = stored.indexOf(':'); if (sep < 0) { - return null; + // No IV separator, so this was written by legacyPlainSet on API 22 or below + // and the device has since been upgraded to 23+. Reporting it missing would + // silently discard a cached credential across an OS upgrade the user did not + // choose to lose anything by. Decode it and re-store it encrypted, so this + // only happens once. + String legacy = decodeLegacyPlain(stored); + if (legacy != null) { + set(account, legacy); + } + return legacy; } try { SecretKey key = plainKey(false); @@ -390,6 +399,16 @@ private boolean legacyPlainSet(String account, String value) { } } + /** The obfuscated-only form written on API 22 and below, or null if unreadable. */ + private String decodeLegacyPlain(String stored) { + try { + return new String(Base64.decode(stored, Base64.NO_WRAP), "UTF-8"); + } catch (Throwable t) { + Log.e(t); + return null; + } + } + private String legacyPlainGet(String account) { warnLegacyPlainStorage(); SharedPreferences prefs = plainPrefs(); diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index 29399abd3ed..f205ed5d8b5 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -170,19 +170,25 @@ void resetAttestation() { // mark only that callback stale and leave the resurrected key behind for the // next bootstrap to find. synchronized (flowLock) { - SecureStorage store = SecureStorage.getInstance(); - store.remove(KEY_ID); - store.remove(KEY_STATE); - store.remove(KEY_RETRY_AFTER); - store.remove(KEY_PENDING_SINCE); - currentBackoff = MIN_BACKOFF_MILLIS; - bootstrapInFlight = false; - // Any callback still outstanding now belongs to an abandoned flow. - generation++; - failBootstrapWaiters("App Attest state was reset while a bootstrap was in flight"); + resetLocked(); } } + /// The reset itself, for callers that must not release the lock between discarding + /// the old identity and starting the replacement. Caller holds `flowLock`. + private void resetLocked() { + SecureStorage store = SecureStorage.getInstance(); + store.remove(KEY_ID); + store.remove(KEY_STATE); + store.remove(KEY_RETRY_AFTER); + store.remove(KEY_PENDING_SINCE); + currentBackoff = MIN_BACKOFF_MILLIS; + bootstrapInFlight = false; + // Any callback still outstanding now belongs to an abandoned flow. + generation++; + failBootstrapWaiters("App Attest state was reset while a bootstrap was in flight"); + } + /** * Acknowledges that a backend has recorded this device's attested public key, * moving it from {@code pending} to {@code attested} so subsequent requests @@ -514,17 +520,22 @@ public static void nativeAttestError(final int requestId, final int errorCode, return; } if (errorCode == DC_ERROR_INVALID_KEY && instance != null && !pending.retried) { - // The key is gone or was never valid. Wipe it and try once from - // scratch; a second failure is reported rather than looped. - instance.resetAttestation(); + // The key is gone or was never valid. Wipe it and try once from scratch; a + // second failure is reported rather than looped. + // + // Discarding the identity and claiming the replacement bootstrap happen in + // ONE lock acquisition. Calling resetAttestation() and then reacquiring left + // a gap in which a concurrent requestToken() saw no key and no bootstrap + // running, started its own generation, and then this path started another -- + // two rate-limited hardware keys racing to become the persisted identity. synchronized (instance.flowLock) { + instance.resetLocked(); PendingRequest retry = new PendingRequest(pending.result, pending.nonce, PendingRequest.OP_GENERATE_KEY, null); retry.retried = true; - // resetAttestation() clears the flag, so set it again before - // starting the replacement: otherwise a request arriving before - // the recovery callback sees no key and no bootstrap running, - // generates a second rate-limited key and races this one. + // resetLocked() clears the flag, so set it again before starting the + // replacement -- still under the same lock, so nothing observes the + // cleared state. instance.bootstrapInFlight = true; int rid = register(retry); instance.nativeInstance.appAttestGenerateKey(rid); From 29c5da884350a4c93be2518fc75f042b9c223adf Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Wed, 29 Jul 2026 18:31:41 +0300 Subject: [PATCH 16/96] Restore ASCII-only sources, and close three more locale/lock gaps The Android port build failed on "unmappable character for encoding ASCII": my ShieldHosts doc comment contained a literal dotless i while explaining the very bug it exists to prevent. javac compiles this tree as ASCII, so the character is now described rather than shown. Three review findings: - nativeAttestError never checked the generation, so a stale callback could reset an identity a newer bootstrap had just established and burn another rate-limited key replacing it -- and a stale non-invalid-key error could clear bootstrapInFlight for a generation it knew nothing about, letting a concurrent request start a second key alongside the first. Stale errors are now rejected before touching any shared state. - confirmAttestation read the pending state outside the lock and transitioned inside it, so a reset and replacement landing in between would be stamped attested while Apple had not attested the new key. The check moved inside the same block. - ShieldHosts now drops a terminal DNS root dot. "api.example.com." is a valid absolute name resolving to the same host, so keeping the dot left a normally registered policy unmatched -- silently, as ever: no token, no pin. WebSocketImpl's reserved-header check still used toLowerCase(), which is the same locale bug: under Turkish, CONNECTION does not fold to connection, so a reserved header would be emitted alongside the handshake's own and produce a conflicting or rejected handshake. Co-Authored-By: Claude Opus 5 (1M context) --- .../src/com/codename1/impl/WebSocketImpl.java | 17 +++++++++++- .../security/shield/ShieldHosts.java | 18 ++++++++++--- .../impl/ios/IOSDeviceIntegrity.java | 27 ++++++++++++++++--- 3 files changed, 53 insertions(+), 9 deletions(-) diff --git a/CodenameOne/src/com/codename1/impl/WebSocketImpl.java b/CodenameOne/src/com/codename1/impl/WebSocketImpl.java index 29b204300bc..82ede32afca 100644 --- a/CodenameOne/src/com/codename1/impl/WebSocketImpl.java +++ b/CodenameOne/src/com/codename1/impl/WebSocketImpl.java @@ -119,12 +119,27 @@ protected final void appendRequestHeaders(StringBuilder req) { } private static boolean isReservedHandshakeHeader(String name) { - String n = name.toLowerCase(); + // ASCII folding, not toLowerCase(): under the Turkish and Azerbaijani locales an + // uppercase I folds to a dotless letter outside ASCII, so a header spelled + // CONNECTION would not be recognised as reserved and would be emitted alongside + // the handshake's own -- producing a conflicting or rejected opening handshake + // while the API promises reserved names are ignored. + String n = asciiLower(name); return "host".equals(n) || "upgrade".equals(n) || "connection".equals(n) || "sec-websocket-key".equals(n) || "sec-websocket-version".equals(n) || "sec-websocket-protocol".equals(n) || "content-length".equals(n); } + /// Lowercases ASCII letters only, so the result never depends on the device locale. + private static String asciiLower(String s) { + StringBuilder sb = new StringBuilder(s.length()); + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + sb.append(c >= 'A' && c <= 'Z' ? (char) (c + 32) : c); + } + return sb.toString(); + } + private static boolean containsCrLf(String s) { return s.indexOf('\r') >= 0 || s.indexOf('\n') >= 0; } diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldHosts.java b/CodenameOne/src/com/codename1/security/shield/ShieldHosts.java index 2cdb7f6a69b..e6c62045c5e 100644 --- a/CodenameOne/src/com/codename1/security/shield/ShieldHosts.java +++ b/CodenameOne/src/com/codename1/security/shield/ShieldHosts.java @@ -26,9 +26,10 @@ /// /// #### Why not `String.toLowerCase()` /// -/// It folds using the device's locale. Under the Turkish locale an uppercase ASCII `I` becomes the -/// dotless `ı`, so a request to `API.example.com` stops matching a policy registered as -/// `api.example.com`. The consequence is not a display glitch: the host silently looks unprotected, +/// It folds using the device's locale. Under the Turkish locale an uppercase ASCII `I` becomes a +/// dotless lowercase letter outside ASCII, so a request to `API.example.com` stops matching a +/// policy registered as `api.example.com`. The consequence is not a display glitch: the host +/// silently looks unprotected, /// so no token is attached and no pin is enforced -- on precisely the devices whose users the /// developer never tests with. /// @@ -40,11 +41,20 @@ final class ShieldHosts { private ShieldHosts() { } - /// Lowercases the ASCII letters and leaves every other character alone. Null in, null out. + /// Lowercases the ASCII letters and drops a terminal DNS root dot. Null in, null out. + /// + /// `https://api.example.com./` is a valid absolute name that resolves identically to + /// `api.example.com`, so keeping the dot would leave a normally registered policy unmatched -- + /// and the failure is the silent kind: no token attached, no pin enforced. Applied to + /// configured, runtime, request and pin hosts alike, since they all pass through here. static String normalize(String s) { if (s == null) { return null; } + // Only one, and never on a bare "." -- an empty host is not an improvement. + if (s.length() > 1 && s.charAt(s.length() - 1) == '.') { + s = s.substring(0, s.length() - 1); + } int len = s.length(); StringBuilder sb = null; for (int i = 0; i < len; i++) { diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index f205ed5d8b5..a4ea6c44411 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -202,11 +202,15 @@ private void resetLocked() { * point of attest-once is not to burn keys that way.

*/ void confirmAttestation() { - SecureStorage store = SecureStorage.getInstance(); - if (!STATE_PENDING.equals(store.get(KEY_STATE))) { - return; - } synchronized (flowLock) { + // Re-read inside the lock. Checking first and transitioning afterwards let a + // reset and a replacement key land in between, so this would stamp + // STATE_ATTESTED over a STATE_NEW key Apple has not attested yet -- and the + // next request would assert against it. + SecureStorage store = SecureStorage.getInstance(); + if (!STATE_PENDING.equals(store.get(KEY_STATE))) { + return; + } store.set(KEY_STATE, STATE_ATTESTED); store.remove(KEY_PENDING_SINCE); } @@ -519,6 +523,21 @@ public static void nativeAttestError(final int requestId, final int errorCode, if (pending == null) { return; } + if (instance != null) { + boolean stale; + synchronized (instance.flowLock) { + stale = isStale(pending); + } + if (stale) { + // Belongs to a flow that was already abandoned. Acting on it would reset + // the identity a newer bootstrap just established and burn another + // rate-limited key replacing it -- and for a non-invalid-key error it + // would clear bootstrapInFlight for a generation it knows nothing about, + // letting a concurrent request start a second key alongside the first. + fail(pending, "App Attest state was reset while this request was in flight"); + return; + } + } if (errorCode == DC_ERROR_INVALID_KEY && instance != null && !pending.retried) { // The key is gone or was never valid. Wipe it and try once from scratch; a // second failure is reported rather than looped. From 693bab71ae00993ead5fb4f638e14aa3c78c241e Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Wed, 29 Jul 2026 19:47:20 +0300 Subject: [PATCH 17/96] Give the request hook back its legacy certificate view The comment claimed the request's own checkSSLCertificates hook ran unchanged, and it did not: once a guard pinned the host, the hook was handed the grouped view, which yields one object per certificate and keeps only the first fingerprint. An app pinning a SHA-1 value, or simply counting entries, would have started rejecting chains it has always accepted. The hook now always sees the flat legacy view and the enriched one goes only to the guard, on both the direct and the iOS native callback paths. A fail-closed host that received a plaintext URL returned without a token, which protected the token but broke the policy's own promise to refuse requests lacking one -- and sent the body over plaintext instead. It now goes through failOrContinue, so a closed policy aborts. Two more iOS atomicity gaps of the same family as the last round: - nativeAttestError checked the generation under the lock and then released it before acting, so a reset landing in between would have its replacement bootstrap discarded by the older callback. The check and every mutation the callback performs now sit in one acquisition, including the trailing in-flight clear that was still outside it. - succeed() only schedules completion onto the EDT, so a reset between the check and the runnable still delivered an assertion for the discarded key. The generation is rechecked at publication. Co-Authored-By: Claude Opus 5 (1M context) --- .../com/codename1/io/ConnectionRequest.java | 36 ++++-- .../codename1/security/shield/AppShield.java | 8 ++ .../impl/ios/IOSDeviceIntegrity.java | 116 ++++++++++-------- 3 files changed, 101 insertions(+), 59 deletions(-) diff --git a/CodenameOne/src/com/codename1/io/ConnectionRequest.java b/CodenameOne/src/com/codename1/io/ConnectionRequest.java index 3c5ba958130..8c1639de83d 100644 --- a/CodenameOne/src/com/codename1/io/ConnectionRequest.java +++ b/CodenameOne/src/com/codename1/io/ConnectionRequest.java @@ -912,7 +912,12 @@ boolean checkCertificatesNativeCallback() { checkSSLCertificates(certs); NetworkGuard guard = NetworkManager.getNetworkGuard(); if (guard != null) { - guard.checkCertificates(this, certs); + // Same split as the non-callback path: the hook above sees the flat view + // it has always seen, the guard gets the enriched one when the platform + // can produce it. + SSLCertificate[] forGuard = _connection == null + ? null : guardSSLCertificates(_connection, url); + guard.checkCertificates(this, forGuard == null ? certs : forGuard); } return !shouldStop(); } catch (IOException ex) { @@ -1068,12 +1073,15 @@ boolean performOperationComplete() throws IOException { // empty POST bodies. !Util.getImplementation().checkSSLCertificatesRequiresCallbackFromNative()) { sslCertificates = getSSLCertificatesImpl(connection, url); - // The request's own hook runs first and unchanged, so an app that - // already pins by overriding it keeps working; the guard layers on. + // The request's own hook runs first and on the legacy flat view, so an + // app that already pins by overriding it keeps working; the guard layers + // on, and only the guard sees the enriched per-certificate form. checkSSLCertificates(sslCertificates); NetworkGuard certGuard = NetworkManager.getNetworkGuard(); if (certGuard != null) { - certGuard.checkCertificates(this, sslCertificates); + SSLCertificate[] forGuard = guardSSLCertificates(connection, url); + certGuard.checkCertificates(this, + forGuard == null ? sslCertificates : forGuard); } if (shouldStop()) { return true; @@ -1762,11 +1770,25 @@ static SSLCertificate[] parseGroupedCertificates(String[] entries) { return arr; } - private SSLCertificate[] getSSLCertificatesImpl(Object connection, String url) throws IOException { + /// The enriched per-certificate view, or null when this platform or request cannot produce + /// one. Only the [NetworkGuard] sees this; see [#getSSLCertificatesImpl] for why. + private SSLCertificate[] guardSSLCertificates(Object connection, String url) + throws IOException { CodenameOneImplementation impl = Util.getImplementation(); - if (collectPublicKeyDigests && impl.canGetPublicKeyDigests()) { - return parseGroupedCertificates(impl.getSSLCertificatesEx(connection, url)); + if (!collectPublicKeyDigests || !impl.canGetPublicKeyDigests()) { + return null; } + return parseGroupedCertificates(impl.getSSLCertificatesEx(connection, url)); + } + + /// The flat, one-entry-per-fingerprint view every existing caller has always seen. + /// + /// Deliberately not the grouped form even when the guard asked for it: grouping yields one + /// object per certificate and keeps only the first fingerprint, so a hook that pins a SHA-1 + /// value -- or simply counts entries -- would start rejecting a chain it has always accepted. + /// The enriched view exists for the guard and goes only to the guard. + private SSLCertificate[] getSSLCertificatesImpl(Object connection, String url) throws IOException { + CodenameOneImplementation impl = Util.getImplementation(); String[] sslCerts = impl.getSSLCertificates(connection, url); SSLCertificate[] out = new SSLCertificate[sslCerts.length]; int i = 0; diff --git a/CodenameOne/src/com/codename1/security/shield/AppShield.java b/CodenameOne/src/com/codename1/security/shield/AppShield.java index 9504bdbedfd..99fc5757587 100644 --- a/CodenameOne/src/com/codename1/security/shield/AppShield.java +++ b/CodenameOne/src/com/codename1/security/shield/AppShield.java @@ -258,8 +258,16 @@ public static void attach(ConnectionRequest request) throws ShieldException { // The token is a bearer credential. Sending it in plaintext -- after a // downgrade redirect, or a mistyped scheme -- hands it to anyone on the // path, and pinning cannot help because there is no certificate to pin. + // + // Routed through the failure mode rather than simply returning: a + // fail-closed host promises to refuse requests that carry no valid token, + // and silently sending the body over plaintext instead is the one outcome + // that policy exists to rule out. Log.p("AppShield: refusing to attach a token to a plaintext URL for " + host + ". Use https for protected hosts."); + failOrContinue(policy, new ShieldException(ShieldStatus.REJECTED, + "AppShield: " + host + " is a protected host but the request is " + + "plaintext, so no token can be attached safely. Use https.")); return; } try { diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index a4ea6c44411..ac3f1fbb335 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -524,60 +524,51 @@ public static void nativeAttestError(final int requestId, final int errorCode, return; } if (instance != null) { - boolean stale; + // The staleness check and every mutation this callback performs sit inside + // ONE acquisition. Checking under the lock and then releasing it let a reset + // bump the generation and start a replacement in between, and this callback + // would then discard that replacement -- or, for a non-invalid-key error, + // clear the in-flight flag and waiters belonging to it. synchronized (instance.flowLock) { - stale = isStale(pending); - } - if (stale) { - // Belongs to a flow that was already abandoned. Acting on it would reset - // the identity a newer bootstrap just established and burn another - // rate-limited key replacing it -- and for a non-invalid-key error it - // would clear bootstrapInFlight for a generation it knows nothing about, - // letting a concurrent request start a second key alongside the first. - fail(pending, "App Attest state was reset while this request was in flight"); - return; - } - } - if (errorCode == DC_ERROR_INVALID_KEY && instance != null && !pending.retried) { - // The key is gone or was never valid. Wipe it and try once from scratch; a - // second failure is reported rather than looped. - // - // Discarding the identity and claiming the replacement bootstrap happen in - // ONE lock acquisition. Calling resetAttestation() and then reacquiring left - // a gap in which a concurrent requestToken() saw no key and no bootstrap - // running, started its own generation, and then this path started another -- - // two rate-limited hardware keys racing to become the persisted identity. - synchronized (instance.flowLock) { - instance.resetLocked(); - PendingRequest retry = new PendingRequest(pending.result, pending.nonce, - PendingRequest.OP_GENERATE_KEY, null); - retry.retried = true; - // resetLocked() clears the flag, so set it again before starting the - // replacement -- still under the same lock, so nothing observes the - // cleared state. - instance.bootstrapInFlight = true; - int rid = register(retry); - instance.nativeInstance.appAttestGenerateKey(rid); - } - return; - } - if (errorCode == DC_ERROR_SERVER_UNAVAILABLE && instance != null) { - // Never retry a throttle in a loop -- that is what gets an app's - // whole attestation budget suspended. - synchronized (instance.flowLock) { - long backoff = instance.currentBackoff; - SecureStorage.getInstance().set(KEY_RETRY_AFTER, - Long.toString(System.currentTimeMillis() + backoff)); - instance.currentBackoff = Math.min(backoff * 2, MAX_BACKOFF_MILLIS); + if (isStale(pending)) { + fail(pending, + "App Attest state was reset while this request was in flight"); + return; + } + if (errorCode == DC_ERROR_INVALID_KEY && !pending.retried) { + // The key is gone or was never valid. Wipe it and try once from + // scratch; a second failure is reported rather than looped. + instance.resetLocked(); + PendingRequest retry = new PendingRequest(pending.result, pending.nonce, + PendingRequest.OP_GENERATE_KEY, null); + retry.retried = true; + // resetLocked() clears the flag, so set it again before starting the + // replacement -- still under the same lock, so nothing observes the + // cleared state. + instance.bootstrapInFlight = true; + int rid = register(retry); + instance.nativeInstance.appAttestGenerateKey(rid); + return; + } + if (errorCode == DC_ERROR_SERVER_UNAVAILABLE) { + // Never retry a throttle in a loop -- that is what gets an app's + // whole attestation budget suspended. + long backoff = instance.currentBackoff; + SecureStorage.getInstance().set(KEY_RETRY_AFTER, + Long.toString(System.currentTimeMillis() + backoff)); + instance.currentBackoff = Math.min(backoff * 2, MAX_BACKOFF_MILLIS); + } + if (pending.op != PendingRequest.OP_ASSERT) { + // Still the same acquisition: releasing here and reacquiring would + // reopen the window on this mutation alone, clearing the in-flight + // flag of whatever bootstrap had started meanwhile. + instance.bootstrapInFlight = false; + instance.failBootstrapWaiters( + msg == null ? "App Attest failed (code " + errorCode + ")" : msg); + } } } String message = msg == null ? "App Attest failed (code " + errorCode + ")" : msg; - if (instance != null && pending.op != PendingRequest.OP_ASSERT) { - synchronized (instance.flowLock) { - instance.bootstrapInFlight = false; - instance.failBootstrapWaiters(message); - } - } fail(pending, message); } @@ -627,12 +618,33 @@ private static PendingRequest take(int requestId) { } } + /** + * Completes a caller with a token, unless a reset overtook it. + * + *

Completion is scheduled onto the EDT, so between the callback deciding the + * token is good and the caller receiving it, another request can reset attestation + * -- and the caller would then be handed an assertion for the key that was just + * discarded, which is exactly what the staleness checks exist to suppress. The + * generation is therefore checked again at the moment of publication.

+ */ private static void succeed(final PendingRequest pending, final String token) { Display.getInstance().callSerially(new Runnable() { public void run() { - if (!pending.result.isDone()) { - pending.result.complete(token); + if (pending.result.isDone()) { + return; + } + if (instance != null) { + boolean stale; + synchronized (instance.flowLock) { + stale = isStale(pending); + } + if (stale) { + pending.result.error(new RuntimeException("App Attest state was " + + "reset while this request was in flight")); + return; + } } + pending.result.complete(token); } }); } From 3c7e541dbdae44d3a3bb2abd9989a011a1122b21 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Wed, 29 Jul 2026 20:03:50 +0300 Subject: [PATCH 18/96] Key WebSocket headers case-insensitively, fail a refused reset HTTP header names are case-insensitive but the handshake map was keyed by whatever spelling the caller used, so header("Authorization", v) followed by header("authorization", null) left the credential in place, and setting both emitted the field twice and left the server to choose. Not a thing to leave to chance for an Authorization header. Keyed by the folded name now, storing the caller's spelling alongside the value so emission is unchanged. resetAttestation ignored what the keychain returned. A refused deletion still advanced the generation and reported the reset complete, so the next request could reload the very key the backend asked the client to discard and carry on asserting with it. The two entries carrying the identity are now checked: if they survive, the reset does not happen at all -- state left visibly unchanged so the caller can retry -- and the invalid-key recovery path fails rather than starting a replacement alongside an identity it could not remove. Co-Authored-By: Claude Opus 5 (1M context) --- .../src/com/codename1/impl/WebSocketImpl.java | 26 ++++++++++---- .../impl/ios/IOSDeviceIntegrity.java | 35 ++++++++++++++++--- 2 files changed, 50 insertions(+), 11 deletions(-) diff --git a/CodenameOne/src/com/codename1/impl/WebSocketImpl.java b/CodenameOne/src/com/codename1/impl/WebSocketImpl.java index 82ede32afca..f1c16f3573e 100644 --- a/CodenameOne/src/com/codename1/impl/WebSocketImpl.java +++ b/CodenameOne/src/com/codename1/impl/WebSocketImpl.java @@ -83,16 +83,26 @@ public final void setRequestHeader(String name, String value) { if (name == null || name.length() == 0) { return; } + // Keyed by the folded name, because HTTP header names are case-insensitive. + // Keying by the spelling the caller happened to use meant + // header("Authorization", v) followed by header("authorization", null) left the + // credential in place, and setting both emitted the field twice -- leaving the + // server to pick one, which is not a thing to leave to chance for an + // Authorization header. The caller's spelling is kept for emission. + String key = asciiLower(name); if (value == null) { - requestHeaders.remove(name); + requestHeaders.remove(key); } else { - requestHeaders.put(name, value); + requestHeaders.put(key, new String[] {name, value}); } } - /// The extra handshake headers, keyed by name. Never null; ports read this - /// while building the handshake. Ports must not emit an entry whose name - /// collides with a header the handshake sets itself. + /// The extra handshake headers, keyed by the ASCII-folded name. Each value is a + /// two-element `String[]` of `{ name as the caller spelled it, value }`, so lookups + /// and removals are case-insensitive the way HTTP is while emission preserves the + /// caller's capitalization. Never null; ports read this while building the + /// handshake. Ports must not emit an entry whose name collides with a header the + /// handshake sets itself. protected final java.util.Hashtable requestHeaders() { return requestHeaders; } @@ -106,8 +116,10 @@ protected final java.util.Hashtable requestHeaders() { protected final void appendRequestHeaders(StringBuilder req) { java.util.Enumeration keys = requestHeaders.keys(); while (keys.hasMoreElements()) { - String name = (String) keys.nextElement(); - String value = (String) requestHeaders.get(name); + String key = (String) keys.nextElement(); + String[] pair = (String[]) requestHeaders.get(key); + String name = pair[0]; + String value = pair[1]; if (isReservedHandshakeHeader(name)) { continue; } diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index ac3f1fbb335..39573fc85dd 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -22,6 +22,7 @@ */ package com.codename1.impl.ios; +import com.codename1.io.Log; import com.codename1.security.Hash; import com.codename1.security.SecureStorage; import com.codename1.ui.Display; @@ -170,7 +171,14 @@ void resetAttestation() { // mark only that callback stale and leave the resurrected key behind for the // next bootstrap to find. synchronized (flowLock) { - resetLocked(); + try { + resetLocked(); + } catch (IllegalStateException e) { + // Public API entry point, so the failure is logged rather than thrown at + // an app that asked us to forget a key. The state is untouched, so the + // next attempt can retry it. + Log.e(e); + } } } @@ -178,10 +186,19 @@ void resetAttestation() { /// the old identity and starting the replacement. Caller holds `flowLock`. private void resetLocked() { SecureStorage store = SecureStorage.getInstance(); - store.remove(KEY_ID); - store.remove(KEY_STATE); + // The two that carry the identity are checked. If the keychain refuses to + // delete them the reset has not happened, and advancing the generation anyway + // would report success while the next request reloads the very key the backend + // asked us to discard -- and keeps asserting with it. Better to leave the state + // visibly unchanged so the caller fails and can retry. + boolean cleared = store.remove(KEY_ID); + cleared &= store.remove(KEY_STATE); store.remove(KEY_RETRY_AFTER); store.remove(KEY_PENDING_SINCE); + if (!cleared) { + throw new IllegalStateException("App Attest could not discard its stored key; " + + "the keychain refused the deletion"); + } currentBackoff = MIN_BACKOFF_MILLIS; bootstrapInFlight = false; // Any callback still outstanding now belongs to an abandoned flow. @@ -538,7 +555,17 @@ public static void nativeAttestError(final int requestId, final int errorCode, if (errorCode == DC_ERROR_INVALID_KEY && !pending.retried) { // The key is gone or was never valid. Wipe it and try once from // scratch; a second failure is reported rather than looped. - instance.resetLocked(); + try { + instance.resetLocked(); + } catch (IllegalStateException e) { + // Nothing was discarded, so starting a replacement would leave + // two identities and the old one still usable. Fail the caller. + instance.bootstrapInFlight = false; + fail(pending, "App Attest could not discard the rejected key"); + instance.failBootstrapWaiters( + "App Attest could not discard the rejected key"); + return; + } PendingRequest retry = new PendingRequest(pending.result, pending.nonce, PendingRequest.OP_GENERATE_KEY, null); retry.retried = true; From 226624094ffcbce9d5c867a9b715f98d14ab4a1f Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Wed, 29 Jul 2026 20:21:05 +0300 Subject: [PATCH 19/96] Invalidate on partial reset, and retry a flaky Central resolution The two keychain deletions in resetLocked are not atomic, so a partial failure left half the identity gone while the method reported the state untouched -- and a callback from the rejected identity could still pass its staleness check and act on it. The generation is now advanced before the failure is raised: whatever the keychain managed, no outstanding callback belongs to the current flow any more. The pending-state rollback was itself unchecked. If storage accepted pending, rejected the timestamp, and then rejected the rollback, the key sat pending with no deadline -- which reads as an expired grace window, so the next request would promote it and assert against a key no backend had seen. That case now discards the identity outright rather than leaving state that reads as ready. succeed() checks staleness under the lock and completes outside it. The completion is deliberately not held under the lock: complete() runs the app's own listeners synchronously, and holding the attestation lock across arbitrary application code is how the deadlock earlier in this class happened. The residual window yields an assertion the backend rejects, which the existing reset-and-retry path already recovers from; a deadlocked EDT has no recovery at all. The Windows cross-compile job had no retry around its Maven build and died on a Central connection reset while resolving a build extension, before compiling anything. Same growing backoff as the JavaSE smoke script, for the same reason. Co-Authored-By: Claude Opus 5 (1M context) --- .github/workflows/windows-cross-compile.yml | 19 ++++++- .../impl/ios/IOSDeviceIntegrity.java | 50 +++++++++++++++---- 2 files changed, 57 insertions(+), 12 deletions(-) diff --git a/.github/workflows/windows-cross-compile.yml b/.github/workflows/windows-cross-compile.yml index 71f15dd1474..040c1f2e13c 100644 --- a/.github/workflows/windows-cross-compile.yml +++ b/.github/workflows/windows-cross-compile.yml @@ -106,8 +106,23 @@ jobs: - name: Build codename1-core + the Windows port (JDK 8) run: | cd maven - JAVA_HOME="$JDK_8_HOME" mvn -B -pl windows -am -DskipTests \ - '-Dmaven.javadoc.skip=true' '-Plocal-dev-javase' install + # Maven Central intermittently resets the connection or throttles the runner, + # and Maven treats that as a permanent resolution failure -- observed killing + # this job while resolving a build extension, before any project code compiled. + # The backoff grows because a flat retry lands inside the same window a 429 is + # still rate limiting in. A genuine build failure fails identically every time. + for delay in 30 120 300 0; do + if JAVA_HOME="$JDK_8_HOME" mvn -B -pl windows -am -DskipTests \ + '-Dmaven.javadoc.skip=true' '-Plocal-dev-javase' install; then + break + fi + if [ "$delay" = "0" ]; then + echo "core + Windows port build failed after all retries" + exit 1 + fi + echo "build failed; retrying in ${delay}s in case Maven Central was flaky" + sleep "$delay" + done test -f core/target/classes/com/codename1/ui/Form.class test -f windows/target/classes/com/codename1/impl/windows/WindowsImplementation.class diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index 39573fc85dd..f720993541d 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -191,11 +191,19 @@ private void resetLocked() { // would report success while the next request reloads the very key the backend // asked us to discard -- and keeps asserting with it. Better to leave the state // visibly unchanged so the caller fails and can retry. - boolean cleared = store.remove(KEY_ID); - cleared &= store.remove(KEY_STATE); + boolean idGone = store.remove(KEY_ID); + boolean stateGone = store.remove(KEY_STATE); store.remove(KEY_RETRY_AFTER); store.remove(KEY_PENDING_SINCE); - if (!cleared) { + if (!idGone || !stateGone) { + // The two deletions are not atomic, so a partial failure leaves half the + // identity gone. Treating that as "untouched" would let a callback from the + // rejected identity still pass its staleness check and act on inconsistent + // state, so the generation is advanced first: whatever the keychain did, no + // outstanding callback belongs to the current flow any more. + generation++; + bootstrapInFlight = false; + failBootstrapWaiters("App Attest could not fully discard its stored key"); throw new IllegalStateException("App Attest could not discard its stored key; " + "the keychain refused the deletion"); } @@ -486,7 +494,19 @@ public static void nativeAttestationReady(final int requestId, final String atte // a key no backend has acknowledged -- the first-use rejection and // pointless key reset this state exists to prevent. Roll back to new so // the key is attested again rather than used prematurely. - store.set(KEY_STATE, STATE_NEW); + if (!store.set(KEY_STATE, STATE_NEW)) { + // The rollback failed too, so the key would sit pending with no + // deadline and be promoted on the next request. Discard the identity + // outright rather than leave a state that reads as ready. + try { + instance.resetLocked(); + } catch (IllegalStateException ignored) { + // resetLocked already advanced the generation and failed the + // waiters; nothing further to do but report to this caller. + } + fail(pending, "App Attest could not record its registration deadline"); + return; + } instance.bootstrapInFlight = false; fail(pending, "App Attest could not record its registration deadline"); instance.failBootstrapWaiters( @@ -660,12 +680,22 @@ public void run() { if (pending.result.isDone()) { return; } - if (instance != null) { - boolean stale; - synchronized (instance.flowLock) { - stale = isStale(pending); - } - if (stale) { + if (instance == null) { + pending.result.complete(token); + return; + } + // The staleness check happens under the lock; the completion itself does + // not, and that is deliberate. complete() runs the app's own listeners + // synchronously, and holding the attestation lock across arbitrary + // application code is how the deadlock earlier in this class happened. + // + // The residual window is a reset landing between the check and the + // handover. What the caller then holds is an assertion for a key the + // backend no longer knows, which it rejects; the app resets and retries, + // which is the recovery path that already exists. A deadlocked EDT has + // no such recovery, so the trade runs this way round. + synchronized (instance.flowLock) { + if (isStale(pending)) { pending.result.error(new RuntimeException("App Attest state was " + "reset while this request was in flight")); return; From 38323ed3828c9c12087e07a18bf9b024cc2254f8 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Wed, 29 Jul 2026 20:39:12 +0300 Subject: [PATCH 20/96] Keep the throttle deadline when the keychain refuses the write requestToken reads the backoff deadline from storage only, so a refused write left it existing purely as a duration nobody consulted -- and the next caller went straight back to a throttled App Attest service, which is how a suspension gets extended rather than waited out. The deadline is now held in memory alongside the keychain, whichever is later wins, and a reset clears both. Co-Authored-By: Claude Opus 5 (1M context) --- .../impl/ios/IOSDeviceIntegrity.java | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index f720993541d..7c911c1ef35 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -137,6 +137,16 @@ final class IOSDeviceIntegrity { /** Guards the whole attest flow so concurrent callers cannot each burn a key. */ private final Object flowLock = new Object(); private long currentBackoff = MIN_BACKOFF_MILLIS; + /** + * The throttle deadline, held in memory as well as in the keychain. + * + *

{@code requestToken} reads the deadline from storage, so a refused write left + * the backoff existing only as a duration nobody consulted -- and the next caller + * went straight back to a throttled App Attest service, which is how a suspension + * gets extended rather than waited out. This survives at least until the process + * dies, which is the case the keychain was covering.

+ */ + private long retryAfterFallback; /** True while a generate-then-attest bootstrap is running. */ private boolean bootstrapInFlight; /** @@ -208,6 +218,7 @@ private void resetLocked() { + "the keychain refused the deletion"); } currentBackoff = MIN_BACKOFF_MILLIS; + retryAfterFallback = 0L; bootstrapInFlight = false; // Any callback still outstanding now belongs to an abandoned flow. generation++; @@ -267,7 +278,7 @@ AsyncResource requestToken(String nonce) { return r; } synchronized (flowLock) { - long retryAfter = readRetryAfter(); + long retryAfter = Math.max(readRetryAfter(), retryAfterFallback); if (retryAfter > System.currentTimeMillis()) { r.error(new RuntimeException("App Attest is backing off after a throttle; " + "retry in " + ((retryAfter - System.currentTimeMillis()) / 1000) @@ -599,10 +610,14 @@ public static void nativeAttestError(final int requestId, final int errorCode, } if (errorCode == DC_ERROR_SERVER_UNAVAILABLE) { // Never retry a throttle in a loop -- that is what gets an app's - // whole attestation budget suspended. + // whole attestation budget suspended. Recorded in memory as well as + // in the keychain, because a refused write must not mean no backoff. long backoff = instance.currentBackoff; + long deadline = System.currentTimeMillis() + backoff; + instance.retryAfterFallback = Math.max(instance.retryAfterFallback, + deadline); SecureStorage.getInstance().set(KEY_RETRY_AFTER, - Long.toString(System.currentTimeMillis() + backoff)); + Long.toString(deadline)); instance.currentBackoff = Math.min(backoff * 2, MAX_BACKOFF_MILLIS); } if (pending.op != PendingRequest.OP_ASSERT) { From da97f4fd217febb777384800ab1b5ff94edec70a Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Wed, 29 Jul 2026 20:55:04 +0300 Subject: [PATCH 21/96] Ask the guard only about URLs it selected, and reset the backoff shouldInspectCertificates returns true when the request opted in on its own, and both certificate paths then called the guard regardless -- so a guard that had explicitly declined a URL was still handed its chain and could reject a host it wanted nothing to do with. The answer is now remembered and the guard is only consulted for URLs it asked about. A successful assertion resets the App Attest backoff. Only the failure path touched it, so the doubling accumulated across the process lifetime: outages separated by long stretches of successful requests compounded until a single transient failure imposed the full hour. Attestation already did this; assertion is the steady-state path and needs it more. Co-Authored-By: Claude Opus 5 (1M context) --- .../src/com/codename1/io/ConnectionRequest.java | 15 ++++++++++++--- .../codename1/impl/ios/IOSDeviceIntegrity.java | 11 +++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/CodenameOne/src/com/codename1/io/ConnectionRequest.java b/CodenameOne/src/com/codename1/io/ConnectionRequest.java index 8c1639de83d..08f9183428c 100644 --- a/CodenameOne/src/com/codename1/io/ConnectionRequest.java +++ b/CodenameOne/src/com/codename1/io/ConnectionRequest.java @@ -911,10 +911,11 @@ boolean checkCertificatesNativeCallback() { SSLCertificate[] certs = getSSLCertificates(); checkSSLCertificates(certs); NetworkGuard guard = NetworkManager.getNetworkGuard(); - if (guard != null) { + if (guard != null && guardWantsCertificates) { // Same split as the non-callback path: the hook above sees the flat view // it has always seen, the guard gets the enriched one when the platform - // can produce it. + // can produce it. And only for URLs the guard asked about -- the request + // may have opted into inspection by itself. SSLCertificate[] forGuard = _connection == null ? null : guardSSLCertificates(_connection, url); guard.checkCertificates(this, forGuard == null ? certs : forGuard); @@ -933,13 +934,21 @@ boolean checkCertificatesNativeCallback() { /// True when the certificate chain for this request should be fetched and vetted, either /// because the request opted in itself or because the installed [NetworkGuard] pins this host. + /// Whether the guard asked to see this URL's chain, as answered by + /// [#shouldInspectCertificates()]. Kept so the guard is only handed a chain for hosts + /// it selected: a request can opt into inspection on its own, and a guard that + /// declined this URL must not then be asked to judge it. + private boolean guardWantsCertificates; + private boolean shouldInspectCertificates() { + guardWantsCertificates = false; NetworkGuard guard = NetworkManager.getNetworkGuard(); if (guard == null) { return checkSSLCertificates; } try { if (guard.isCertificateCheckRequired(url)) { + guardWantsCertificates = true; // Ask the platform for the richer chain details (public key // digests, per-certificate grouping). Off by default so every // existing caller keeps seeing byte-identical data. Asked even @@ -1078,7 +1087,7 @@ boolean performOperationComplete() throws IOException { // on, and only the guard sees the enriched per-certificate form. checkSSLCertificates(sslCertificates); NetworkGuard certGuard = NetworkManager.getNetworkGuard(); - if (certGuard != null) { + if (certGuard != null && guardWantsCertificates) { SSLCertificate[] forGuard = guardSSLCertificates(connection, url); certGuard.checkCertificates(this, forGuard == null ? sslCertificates : forGuard); diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index 7c911c1ef35..2505d402600 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -559,6 +559,17 @@ public static void nativeAssertionReady(final int requestId, final String assert fail(pending, "App Attest assertion returned no data"); return; } + if (instance != null) { + synchronized (instance.flowLock) { + // A working assertion means Apple is answering again, so the throttle + // sequence starts over. Without this the doubling only ever accumulated: + // outages separated by months of successful requests would compound + // until one transient failure imposed the full hour. + instance.currentBackoff = MIN_BACKOFF_MILLIS; + instance.retryAfterFallback = 0L; + SecureStorage.getInstance().remove(KEY_RETRY_AFTER); + } + } succeed(pending, TOKEN_PREFIX + ":assert:" + base64(bytes(pending.keyId)) + ":" + assertionB64 + ":" + base64(bytes(pending.clientData))); From 782103af560fd9f726fbaab8902678db69ad9998 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Wed, 29 Jul 2026 21:12:56 +0300 Subject: [PATCH 22/96] Fail closed for a registered engine, and serialize the iOS coordinator failOrContinue keyed on isProtected(), which asks whether attestation can run right now. An engine may legitimately report itself unavailable -- an unsupported device, a failed initialization -- and that is exactly when a fail-closed host must refuse, not the moment to stop enforcing. It now keys on whether an engine was registered at all, so only a build with no engine is exempt, which is the documented degradation contract. deviceIntegrity() was an unsynchronized lazy init. Two concurrent first requests each built a coordinator with its own flowLock and bootstrapInFlight, so neither saw the other's bootstrap -- two rate-limited hardware keys, and two sets of callbacks racing to persist an identity through a shared static the constructor had just overwritten. Co-Authored-By: Claude Opus 5 (1M context) --- .../src/com/codename1/security/shield/AppShield.java | 10 ++++++++-- .../security/shield/spi/ShieldEngineRegistry.java | 8 +++++++- .../src/com/codename1/impl/ios/IOSImplementation.java | 11 ++++++++++- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/CodenameOne/src/com/codename1/security/shield/AppShield.java b/CodenameOne/src/com/codename1/security/shield/AppShield.java index 99fc5757587..1df5c8d64a6 100644 --- a/CodenameOne/src/com/codename1/security/shield/AppShield.java +++ b/CodenameOne/src/com/codename1/security/shield/AppShield.java @@ -304,11 +304,17 @@ private static void failOrContinue(HostPolicy policy, ShieldException e) throws // A build with no engine must never block a request: that is the // degradation contract this API documents, and a fail-closed host in an // open-source or unentitled build would otherwise break outright. - if (policy.getFailureMode() == FailureMode.CLOSED && isProtected()) { + // Registered, not available. An engine may legitimately report itself + // unavailable -- an unsupported device, a failed initialization -- and that is + // exactly when a fail-closed host must refuse, not the moment to stop + // enforcing. Only a build with no engine at all is exempt, which is the + // degradation contract the open-source path documents. + boolean enginePresent = ShieldEngineRegistry.isEngineRegistered(); + if (policy.getFailureMode() == FailureMode.CLOSED && enginePresent) { throw e; } Log.p("AppShield: continuing without a token (" + e.getStatus().getId() - + "); " + (isProtected() ? "host policy is fail-open." + + "); " + (enginePresent ? "host policy is fail-open." : "no attestation engine is present, so nothing is enforced.")); } diff --git a/CodenameOne/src/com/codename1/security/shield/spi/ShieldEngineRegistry.java b/CodenameOne/src/com/codename1/security/shield/spi/ShieldEngineRegistry.java index cb4c8419a10..05d09810d13 100644 --- a/CodenameOne/src/com/codename1/security/shield/spi/ShieldEngineRegistry.java +++ b/CodenameOne/src/com/codename1/security/shield/spi/ShieldEngineRegistry.java @@ -82,7 +82,13 @@ public static EngineContext getDefaultContext() { return DefaultEngineContext.INSTANCE; } - /// True when a real engine was registered. + /// True when a real engine was registered, whatever it currently reports about its + /// availability. + /// + /// Distinct from [com.codename1.security.shield.AppShield#isProtected()], which asks + /// whether attestation can run *right now*. A fail-closed host must refuse when a + /// registered engine cannot attest -- that is the case it exists for -- and relax only + /// for a build that has no engine at all. public static boolean isEngineRegistered() { synchronized (ShieldEngineRegistry.class) { return engine != null; diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSImplementation.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSImplementation.java index 9c480bc7a51..1b51df43b0c 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSImplementation.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSImplementation.java @@ -4585,7 +4585,16 @@ public void confirmAttestation() { deviceIntegrity().confirmAttestation(); } - private IOSDeviceIntegrity deviceIntegrity() { + /** + * The App Attest coordinator, created once. + * + *

Synchronized because two concurrent first requests would otherwise each build + * one. Each carries its own {@code flowLock} and {@code bootstrapInFlight}, so + * neither would see the other's bootstrap -- two rate-limited hardware keys, and + * two sets of callbacks racing to persist an identity -- while the constructor + * overwrites the shared static the native callbacks dispatch through.

+ */ + private synchronized IOSDeviceIntegrity deviceIntegrity() { if (deviceIntegrity == null) { deviceIntegrity = new IOSDeviceIntegrity(nativeInstance); } From 08aea2dc5a49df2b69e271e0af42cf048dbaa2cb Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Wed, 29 Jul 2026 21:29:02 +0300 Subject: [PATCH 23/96] Acknowledge a specific attested key, not whatever is pending confirmAttestation() promoted whichever key happened to be pending. A backend response for an earlier attestation can arrive after a reset has already replaced the identity, and acknowledging it marked a key attested that the backend has never seen -- so the next assertion is rejected and costs another reset, which is the loop the pending state exists to break. It now takes the key identifier the response acknowledged and transitions only when that is still the stored key. The signature change runs through DeviceIntegrity, Display, CodenameOneImplementation, EngineContext and the ports; the API is new in this branch, so nothing shipped depends on the old shape. Co-Authored-By: Claude Opus 5 (1M context) --- .../impl/CodenameOneImplementation.java | 2 +- .../com/codename1/security/DeviceIntegrity.java | 16 +++++++++++----- .../shield/spi/DefaultEngineContext.java | 4 ++-- .../security/shield/spi/EngineContext.java | 2 +- CodenameOne/src/com/codename1/ui/Display.java | 4 ++-- .../com/codename1/impl/javase/JavaSEPort.java | 2 +- .../codename1/impl/ios/IOSDeviceIntegrity.java | 13 ++++++++++++- .../codename1/impl/ios/IOSImplementation.java | 4 ++-- 8 files changed, 32 insertions(+), 15 deletions(-) diff --git a/CodenameOne/src/com/codename1/impl/CodenameOneImplementation.java b/CodenameOne/src/com/codename1/impl/CodenameOneImplementation.java index 361ed4d7104..d8b71a7cd7d 100644 --- a/CodenameOne/src/com/codename1/impl/CodenameOneImplementation.java +++ b/CodenameOne/src/com/codename1/impl/CodenameOneImplementation.java @@ -11276,7 +11276,7 @@ public void resetAttestation() { /// take the cheap assertion path. See /// [com.codename1.security.DeviceIntegrity#confirmAttestation()]. No-op where attestation holds no /// client-side key. - public void confirmAttestation() { + public void confirmAttestation(String keyId) { } /// Returns digests of the certificates the running application is actually signed with, so a build diff --git a/CodenameOne/src/com/codename1/security/DeviceIntegrity.java b/CodenameOne/src/com/codename1/security/DeviceIntegrity.java index bee358e871b..2fd64e50128 100644 --- a/CodenameOne/src/com/codename1/security/DeviceIntegrity.java +++ b/CodenameOne/src/com/codename1/security/DeviceIntegrity.java @@ -137,11 +137,17 @@ public static void resetAttestation() { /// good and burn one of Apple's rate limited attestations replacing it. So requests made between /// the attestation and this acknowledgement are refused with a retry hint rather than asserted. /// - /// Call it once, after the response accepting the attestation token. Not calling it is safe but - /// slower: the client assumes registration succeeded after a short grace period. No-op on Android - /// and where attestation is unsupported. - public static void confirmAttestation() { - Display.getInstance().confirmAttestation(); + /// Call it once, after the response accepting the attestation token, passing the key that + /// response acknowledged. Not calling it is safe but slower: the client assumes registration + /// succeeded after a short grace period. No-op on Android and where attestation is + /// unsupported. + /// @param keyId the key identifier your backend recorded -- the middle field of the + /// `cn1aa1:attest::` token it accepted, base64-decoded. + /// Naming it matters: a response for an earlier attestation can arrive after + /// the key has already been replaced, and acknowledging that would mark a key + /// attested which the backend has never seen. + public static void confirmAttestation(String keyId) { + Display.getInstance().confirmAttestation(keyId); } /// Non-exiting RASP check. Returns true when the device shows signs of being rooted, jailbroken, diff --git a/CodenameOne/src/com/codename1/security/shield/spi/DefaultEngineContext.java b/CodenameOne/src/com/codename1/security/shield/spi/DefaultEngineContext.java index ad05f995161..0a64d704a60 100644 --- a/CodenameOne/src/com/codename1/security/shield/spi/DefaultEngineContext.java +++ b/CodenameOne/src/com/codename1/security/shield/spi/DefaultEngineContext.java @@ -71,9 +71,9 @@ public void resetPlatformAttestation() { } @Override - public void confirmPlatformAttestation() { + public void confirmPlatformAttestation(String keyId) { try { - DeviceIntegrity.confirmAttestation(); + DeviceIntegrity.confirmAttestation(keyId); } catch (Throwable t) { Log.e(t); } diff --git a/CodenameOne/src/com/codename1/security/shield/spi/EngineContext.java b/CodenameOne/src/com/codename1/security/shield/spi/EngineContext.java index a5e7f27757d..2ec982522b5 100644 --- a/CodenameOne/src/com/codename1/security/shield/spi/EngineContext.java +++ b/CodenameOne/src/com/codename1/security/shield/spi/EngineContext.java @@ -54,7 +54,7 @@ public interface EngineContext { /// Acknowledges that the verifying service recorded the attested key, releasing the client to use /// cheap assertions from here on. Call it once the service has accepted an attestation token; until /// then the platform refuses to assert against a key the service cannot yet resolve. - void confirmPlatformAttestation(); + void confirmPlatformAttestation(String keyId); /// Platform-detected compromise reasons, such as `root` or `frida`. String[] getPlatformCompromiseReasons(); diff --git a/CodenameOne/src/com/codename1/ui/Display.java b/CodenameOne/src/com/codename1/ui/Display.java index f7543cfc97f..4941cf7eb7e 100644 --- a/CodenameOne/src/com/codename1/ui/Display.java +++ b/CodenameOne/src/com/codename1/ui/Display.java @@ -6867,8 +6867,8 @@ public void resetAttestation() { /// Acknowledges that a backend recorded the attested key. See /// `com.codename1.security.DeviceIntegrity#confirmAttestation()`. - public void confirmAttestation() { - impl.confirmAttestation(); + public void confirmAttestation(String keyId) { + impl.confirmAttestation(keyId); } /// Returns digests of the certificates the running app is signed with. Low level hook for the diff --git a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEPort.java b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEPort.java index 99f766d3268..7647e1dbdbe 100644 --- a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEPort.java +++ b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEPort.java @@ -19051,7 +19051,7 @@ public void resetAttestation() { } @Override - public void confirmAttestation() { + public void confirmAttestation(String keyId) { // No client-side key here either, so there is nothing to acknowledge. } diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index 2505d402600..50c1f457a21 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -237,7 +237,10 @@ private void resetLocked() { * invalid key, and resets a key that was in fact perfectly good. The whole * point of attest-once is not to burn keys that way.

*/ - void confirmAttestation() { + void confirmAttestation(String keyId) { + if (keyId == null || keyId.length() == 0) { + return; + } synchronized (flowLock) { // Re-read inside the lock. Checking first and transitioning afterwards let a // reset and a replacement key land in between, so this would stamp @@ -247,6 +250,14 @@ void confirmAttestation() { if (!STATE_PENDING.equals(store.get(KEY_STATE))) { return; } + // And it must acknowledge THIS key. A response for an earlier attestation + // can arrive after a reset has already replaced the identity, and promoting + // on that would mark a key attested that the backend has never seen -- so + // the next assertion is rejected and costs another reset, which is the loop + // this state exists to break. + if (!keyId.equals(store.get(KEY_ID))) { + return; + } store.set(KEY_STATE, STATE_ATTESTED); store.remove(KEY_PENDING_SINCE); } diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSImplementation.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSImplementation.java index 1b51df43b0c..3dc1f0979ed 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSImplementation.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSImplementation.java @@ -4581,8 +4581,8 @@ public void resetAttestation() { } @Override - public void confirmAttestation() { - deviceIntegrity().confirmAttestation(); + public void confirmAttestation(String keyId) { + deviceIntegrity().confirmAttestation(keyId); } /** From 4bddbdf4bcb4b2a00b4896a548305a87a9b0ae6d Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Wed, 29 Jul 2026 21:46:10 +0300 Subject: [PATCH 24/96] Treat an absent chain as unavailable, and hold the key across the write The iOS contract lets the native TLS callback hand back an empty certificate array when the chain is not in the TLS cache. The guard converted that into empty digest arrays and asked the engine to verify them, and an enforced host has no pin matching nothing -- so a perfectly good cached connection failed with PIN_MISMATCH naming a certificate nobody had seen. Pinning fails open on unavailability everywhere else in this design; an absent chain now follows the same rule. AndroidSecureStorage held PLAIN_KEY_LOCK for the key lookup and released it before encrypting and storing. A concurrent resetPlainKey() could then delete the alias and clear the preferences in between, so the writer reported success while its ciphertext was encrypted under a key that no longer existed -- unreadable forever, silently. The lock now spans the whole use-and-persist on both set and get, and the reset's own two steps. Co-Authored-By: Claude Opus 5 (1M context) --- .../security/shield/ShieldNetworkGuard.java | 11 ++- .../impl/android/AndroidSecureStorage.java | 69 +++++++++++-------- 2 files changed, 52 insertions(+), 28 deletions(-) diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldNetworkGuard.java b/CodenameOne/src/com/codename1/security/shield/ShieldNetworkGuard.java index 61283554621..650b378a1cf 100644 --- a/CodenameOne/src/com/codename1/security/shield/ShieldNetworkGuard.java +++ b/CodenameOne/src/com/codename1/security/shield/ShieldNetworkGuard.java @@ -65,7 +65,16 @@ public void checkCertificates(ConnectionRequest request, if (host == null || !AppShield.policyFor(host).isEnforcePins()) { return; } - String[] spki = new String[certificates == null ? 0 : certificates.length]; + if (certificates == null || certificates.length == 0) { + // The iOS contract explicitly allows an empty array when the chain is not + // available from the TLS cache. That is "we did not see a certificate", not + // "we saw a wrong one" -- and reporting a mismatch would fail a perfectly + // good cached connection with a PIN_MISMATCH naming a certificate nobody + // observed. Pinning fails open on unavailability everywhere else in this + // design; this is the same rule. + return; + } + String[] spki = new String[certificates.length]; String[] certs = new String[spki.length]; for (int i = 0; i < spki.length; i++) { spki[i] = certificates[i].getPublicKeyDigest(); diff --git a/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java b/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java index 7d7b20b375f..6077aaee3c7 100644 --- a/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java +++ b/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java @@ -220,22 +220,29 @@ public boolean set(String account, String value) { return legacyPlainSet(account, value); } try { - SecretKey key = plainKey(true); - if (key == null) { - return false; - } - Cipher c = Cipher.getInstance("AES/GCM/NoPadding"); - c.init(Cipher.ENCRYPT_MODE, key); - byte[] enc = c.doFinal(value.getBytes("UTF-8")); - SharedPreferences prefs = plainPrefs(); - if (prefs == null) { - return false; + // The whole use-and-persist runs under the same lock a reset takes. + // Releasing it after the lookup let a concurrent resetPlainKey() delete the + // alias and clear the preferences between here and the write, so this + // reported success while storing ciphertext under a key that no longer + // exists -- unreadable forever, and silently so. + synchronized (PLAIN_KEY_LOCK) { + SecretKey key = plainKey(true); + if (key == null) { + return false; + } + Cipher c = Cipher.getInstance("AES/GCM/NoPadding"); + c.init(Cipher.ENCRYPT_MODE, key); + byte[] enc = c.doFinal(value.getBytes("UTF-8")); + SharedPreferences prefs = plainPrefs(); + if (prefs == null) { + return false; + } + prefs.edit() + .putString(account, Base64.encodeToString(c.getIV(), Base64.NO_WRAP) + + ":" + Base64.encodeToString(enc, Base64.NO_WRAP)) + .apply(); + return true; } - prefs.edit() - .putString(account, Base64.encodeToString(c.getIV(), Base64.NO_WRAP) - + ":" + Base64.encodeToString(enc, Base64.NO_WRAP)) - .apply(); - return true; } catch (InvalidKeyException e) { // Includes KeyPermanentlyInvalidatedException. resetPlainKey(); @@ -276,15 +283,19 @@ public String get(String account) { return legacy; } try { - SecretKey key = plainKey(false); - if (key == null) { - return null; + // Same reasoning as set(): a reset landing mid-read would otherwise + // invalidate the key between the lookup and the decrypt. + synchronized (PLAIN_KEY_LOCK) { + SecretKey key = plainKey(false); + if (key == null) { + return null; + } + byte[] iv = Base64.decode(stored.substring(0, sep), Base64.NO_WRAP); + byte[] enc = Base64.decode(stored.substring(sep + 1), Base64.NO_WRAP); + Cipher c = Cipher.getInstance("AES/GCM/NoPadding"); + c.init(Cipher.DECRYPT_MODE, key, new GCMParameterSpec(GCM_TAG_BITS, iv)); + return new String(c.doFinal(enc), "UTF-8"); } - byte[] iv = Base64.decode(stored.substring(0, sep), Base64.NO_WRAP); - byte[] enc = Base64.decode(stored.substring(sep + 1), Base64.NO_WRAP); - Cipher c = Cipher.getInstance("AES/GCM/NoPadding"); - c.init(Cipher.DECRYPT_MODE, key, new GCMParameterSpec(GCM_TAG_BITS, iv)); - return new String(c.doFinal(enc), "UTF-8"); } catch (InvalidKeyException e) { // The key was invalidated out from under us (device-wide credential // change, or the Samsung 8.0.0 quirk documented on the biometric @@ -365,16 +376,20 @@ private SecretKey plainKey(boolean create) throws Exception { } private void resetPlainKey() { + // Deleting the key and dropping the ciphertexts it protected are one step, under + // the same lock readers and writers hold. Clearing outside it left a window + // where a writer had already encrypted under the old key and was about to store + // a value this was about to wipe -- or worse, stored it just after. synchronized (PLAIN_KEY_LOCK) { try { keyStore().deleteEntry(PLAIN_KEY_ID); } catch (KeyStoreException e) { Log.e(e); } - } - SharedPreferences prefs = plainPrefs(); - if (prefs != null) { - prefs.edit().clear().apply(); + SharedPreferences prefs = plainPrefs(); + if (prefs != null) { + prefs.edit().clear().apply(); + } } } From 7c5cefbebe9b6fb9e8f7136055b974229b93cd97 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Wed, 29 Jul 2026 22:07:31 +0300 Subject: [PATCH 25/96] Fall back to the flat chain instead of skipping enforcement getSSLCertificatesEx returns an empty array on any failure, and the guard now reads empty as "no chain available" and fails open -- so a hiccup in the enriched path silently disabled pinning while the legacy fingerprints were still perfectly obtainable. An empty enriched view is reported as null, which makes the caller fall back to the flat list and enforce on certificate digests rather than on nothing. Co-Authored-By: Claude Opus 5 (1M context) --- CodenameOne/src/com/codename1/io/ConnectionRequest.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/CodenameOne/src/com/codename1/io/ConnectionRequest.java b/CodenameOne/src/com/codename1/io/ConnectionRequest.java index 08f9183428c..e289ac81b99 100644 --- a/CodenameOne/src/com/codename1/io/ConnectionRequest.java +++ b/CodenameOne/src/com/codename1/io/ConnectionRequest.java @@ -1787,7 +1787,14 @@ private SSLCertificate[] guardSSLCertificates(Object connection, String url) if (!collectPublicKeyDigests || !impl.canGetPublicKeyDigests()) { return null; } - return parseGroupedCertificates(impl.getSSLCertificatesEx(connection, url)); + SSLCertificate[] enriched = + parseGroupedCertificates(impl.getSSLCertificatesEx(connection, url)); + // Null, not an empty array, when the richer form produced nothing. Ports return + // empty on any failure, and the guard reads empty as "no chain available" and + // fails open -- so a hiccup in the enriched path would silently disable pinning + // while the flat fingerprints were still perfectly obtainable. Null makes the + // caller fall back to those instead of to no enforcement. + return enriched.length == 0 ? null : enriched; } /// The flat, one-entry-per-fingerprint view every existing caller has always seen. From 7706da86cf43117819e6f13b4fc9b6da1992a3ed Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Wed, 29 Jul 2026 22:17:33 +0300 Subject: [PATCH 26/96] Remove headers case-insensitively, and do not resubmit a spent key removeRequestHeader matched the exact key only. HTTP header names are case-insensitive, so a token added as "X-CN1-Attest" and removed as "x-cn1-attest" survived -- and initConnection emits whatever is left, so on a redirect from a protected host to an unprotected one the bearer token went to the redirect target. That is precisely what the removal exists to prevent. Folding is ASCII-only, so it does not depend on the device locale. App Attest attestation is one-time and rate limited, but a key whose attestation was interrupted still looked identical to one that had never been submitted -- so the next launch resubmitted it, Apple rejected the spent key, and the failure repeated on every request until something reset. A marker is now written before the call and cleared when the outcome is recorded, so a resumed attempt discards the key and starts clean instead. One rate-limited key is spent either way; this way the device recovers by itself. Co-Authored-By: Claude Opus 5 (1M context) --- .../com/codename1/io/ConnectionRequest.java | 46 ++++++++++++++++++- .../impl/ios/IOSDeviceIntegrity.java | 36 +++++++++++++-- 2 files changed, 76 insertions(+), 6 deletions(-) diff --git a/CodenameOne/src/com/codename1/io/ConnectionRequest.java b/CodenameOne/src/com/codename1/io/ConnectionRequest.java index e289ac81b99..78204c21867 100644 --- a/CodenameOne/src/com/codename1/io/ConnectionRequest.java +++ b/CodenameOne/src/com/codename1/io/ConnectionRequest.java @@ -657,9 +657,51 @@ public void addRequestHeader(String key, String value) { /// headers: anything scoped to the original host has to be removable before /// the retry, or it follows the redirect to wherever it points. public void removeRequestHeader(String key) { - if (userHeaders != null && key != null) { - userHeaders.remove(key); + if (userHeaders == null || key == null) { + return; + } + userHeaders.remove(key); + // And any other spelling of it. HTTP header names are case-insensitive, so a + // token added as "X-CN1-Attest" and removed as "x-cn1-attest" would survive an + // exact-key removal -- and initConnection emits everything left, which on a + // redirect from a protected host to an unprotected one means handing the bearer + // token to the redirect target. That is the case this method exists to prevent. + Vector matches = null; + Enumeration keys = userHeaders.keys(); + while (keys.hasMoreElements()) { + String existing = (String) keys.nextElement(); + if (existing != null && existing.length() == key.length() + && equalsIgnoreAsciiCase(existing, key)) { + if (matches == null) { + matches = new Vector(); + } + matches.addElement(existing); + } } + if (matches != null) { + for (int i = 0; i < matches.size(); i++) { + userHeaders.remove(matches.elementAt(i)); + } + } + } + + /// ASCII-only case-insensitive comparison, so the result never depends on the device locale -- + /// under the Turkish locale an uppercase `I` does not fold to `i`. + private static boolean equalsIgnoreAsciiCase(String a, String b) { + for (int i = 0; i < a.length(); i++) { + char x = a.charAt(i); + char y = b.charAt(i); + if (x >= 'A' && x <= 'Z') { + x = (char) (x + 32); + } + if (y >= 'A' && y <= 'Z') { + y = (char) (y + 32); + } + if (x != y) { + return false; + } + } + return true; } /// Adds the given header to the request that will be sent unless the header diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index 50c1f457a21..e8a15a96c89 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -91,6 +91,15 @@ final class IOSDeviceIntegrity { private static final String KEY_RETRY_AFTER = "cn1.appattest.retryAfter"; private static final String KEY_PENDING_SINCE = "cn1.appattest.pendingSince"; + /** + * Set immediately before {@code attestKey}, cleared once the result is recorded. + * + *

Attestation is one-time and rate limited, so a key that has already been + * submitted must not be submitted again. Finding this marker on a later launch means + * the app died between Apple accepting the attestation and us persisting that fact + * -- and the key may well be spent, so the only safe move is a fresh one.

+ */ + private static final String KEY_ATTEST_STARTED = "cn1.appattest.attestStarted"; private static final String STATE_NEW = "new"; private static final String STATE_ATTESTED = "attested"; @@ -205,6 +214,7 @@ private void resetLocked() { boolean stateGone = store.remove(KEY_STATE); store.remove(KEY_RETRY_AFTER); store.remove(KEY_PENDING_SINCE); + store.remove(KEY_ATTEST_STARTED); if (!idGone || !stateGone) { // The two deletions are not atomic, so a partial failure leaves half the // identity gone. Treating that as "untouched" would let a callback from the @@ -338,11 +348,25 @@ AsyncResource requestToken(String nonce) { return r; } bootstrapInFlight = true; - if (keyId != null && keyId.length() > 0) { - // A key exists but was never attested -- a previous attempt - // died between the two steps. Attest that key rather than - // generating another. + if (keyId != null && keyId.length() > 0 + && store.get(KEY_ATTEST_STARTED) == null) { + // A key exists but attestation was never started for it -- the + // previous attempt died between generating and submitting. Attest + // that key rather than burning another. attestKey(r, nonce, keyId); + } else if (keyId != null && keyId.length() > 0) { + // Attestation WAS started for this key and we never recorded the + // outcome, so Apple may already have consumed it. Re-submitting a + // spent key is rejected and the failure repeats on every request + // until something resets, so discard it and start clean. One + // rate-limited key is spent either way; this way the device recovers. + store.remove(KEY_ID); + store.remove(KEY_STATE); + store.remove(KEY_ATTEST_STARTED); + PendingRequest fresh = new PendingRequest(r, nonce, + PendingRequest.OP_GENERATE_KEY, null); + int rid = register(fresh); + nativeInstance.appAttestGenerateKey(rid); } else { int rid = register(pending); nativeInstance.appAttestGenerateKey(rid); @@ -387,6 +411,9 @@ private void attestKey(AsyncResource r, String nonce, String keyId, bool // The attestation binds to SHA-256 of the raw nonce. Hashing here rather // than natively keeps a single hash implementation across both paths. String hash = base64(Hash.sha256(bytes(nonce))); + // Recorded BEFORE the call, so a crash between here and the result is + // distinguishable from never having tried. + SecureStorage.getInstance().set(KEY_ATTEST_STARTED, "1"); PendingRequest pending = new PendingRequest(r, nonce, PendingRequest.OP_ATTEST, keyId); pending.retried = retried; int rid = register(pending); @@ -458,6 +485,7 @@ public static void nativeKeyGenerated(final int requestId, final String keyId) { // and leave nothing half-written behind. store.remove(KEY_ID); store.remove(KEY_STATE); + store.remove(KEY_ATTEST_STARTED); instance.bootstrapInFlight = false; fail(pending, "App Attest could not store its key identifier"); instance.failBootstrapWaiters( From 0d53327911596cc66b776c0c51d0e725592482b7 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Wed, 29 Jul 2026 22:30:01 +0300 Subject: [PATCH 27/96] Commit preference writes, guard the native callback, persist the retry SharedPreferences.apply() is asynchronous, so a write could land after a reset that ran once the lock was released -- ciphertext persisted under a key already deleted, unreadable and silent. Holding the lock is only atomic if the persist finishes inside it, so both the write and the reset's clear now use commit(). The iOS native certificate callback did not guard the guard. An unchecked exception there runs on the TLS delegate thread with the handshake open and would take the process with it; a guard that crashed has observed no mismatch, so it now fails open while an IOException still vetoes. The one-shot invalid-key recovery lived on the in-flight request, so the next caller was reconstructed as a first attempt and could reset and burn another rate-limited key -- repeatedly. It is persisted now, and cleared by a successful attestation or an explicit reset. Co-Authored-By: Claude Opus 5 (1M context) --- .../src/com/codename1/io/ConnectionRequest.java | 10 +++++++++- .../impl/android/AndroidSecureStorage.java | 14 ++++++++++---- .../codename1/impl/ios/IOSDeviceIntegrity.java | 17 ++++++++++++++++- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/CodenameOne/src/com/codename1/io/ConnectionRequest.java b/CodenameOne/src/com/codename1/io/ConnectionRequest.java index 78204c21867..727e7b9e2ad 100644 --- a/CodenameOne/src/com/codename1/io/ConnectionRequest.java +++ b/CodenameOne/src/com/codename1/io/ConnectionRequest.java @@ -960,7 +960,15 @@ boolean checkCertificatesNativeCallback() { // may have opted into inspection by itself. SSLCertificate[] forGuard = _connection == null ? null : guardSSLCertificates(_connection, url); - guard.checkCertificates(this, forGuard == null ? certs : forGuard); + try { + guard.checkCertificates(this, forGuard == null ? certs : forGuard); + } catch (RuntimeException t) { + // This runs on the iOS TLS delegate thread with the handshake open, + // so an unchecked exception escaping would take the process with it. + // A guard that crashes has not observed a mismatch, so it fails open; + // an IOException is a real veto and is left to propagate. + Log.e(t); + } } return !shouldStop(); } catch (IOException ex) { diff --git a/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java b/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java index 6077aaee3c7..bd0e5e55217 100644 --- a/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java +++ b/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java @@ -237,11 +237,14 @@ public boolean set(String account, String value) { if (prefs == null) { return false; } - prefs.edit() + // commit(), not apply(): apply() is asynchronous, so the write could land + // on disk after a reset that ran once this lock was released -- storing + // ciphertext under a key that had already been deleted. Holding the lock + // is only atomic if the persist finishes inside it. + return prefs.edit() .putString(account, Base64.encodeToString(c.getIV(), Base64.NO_WRAP) + ":" + Base64.encodeToString(enc, Base64.NO_WRAP)) - .apply(); - return true; + .commit(); } } catch (InvalidKeyException e) { // Includes KeyPermanentlyInvalidatedException. @@ -388,7 +391,10 @@ private void resetPlainKey() { } SharedPreferences prefs = plainPrefs(); if (prefs != null) { - prefs.edit().clear().apply(); + // Also commit(), for the same reason: this method's whole purpose is to + // make the key deletion and the ciphertext deletion one step, and an + // asynchronous clear can be reordered after a writer's pending write. + prefs.edit().clear().commit(); } } } diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index e8a15a96c89..2268fee0e71 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -100,6 +100,15 @@ final class IOSDeviceIntegrity { * -- and the key may well be spent, so the only safe move is a fresh one.

*/ private static final String KEY_ATTEST_STARTED = "cn1.appattest.attestStarted"; + /** + * Marks the one-shot invalid-key recovery as already spent. + * + *

Persisted rather than carried on the in-flight request: {@code pending.retried} + * dies with that request, so the next caller was reconstructed as a first attempt + * and allowed to reset and burn another rate-limited key -- repeatedly, once the OS + * has decided it dislikes this device's keys.

+ */ + private static final String KEY_RECOVERY_SPENT = "cn1.appattest.recoverySpent"; private static final String STATE_NEW = "new"; private static final String STATE_ATTESTED = "attested"; @@ -215,6 +224,7 @@ private void resetLocked() { store.remove(KEY_RETRY_AFTER); store.remove(KEY_PENDING_SINCE); store.remove(KEY_ATTEST_STARTED); + store.remove(KEY_RECOVERY_SPENT); if (!idGone || !stateGone) { // The two deletions are not atomic, so a partial failure leaves half the // identity gone. Treating that as "untouched" would let a callback from the @@ -633,7 +643,9 @@ public static void nativeAttestError(final int requestId, final int errorCode, "App Attest state was reset while this request was in flight"); return; } - if (errorCode == DC_ERROR_INVALID_KEY && !pending.retried) { + boolean recoverySpent = pending.retried + || SecureStorage.getInstance().get(KEY_RECOVERY_SPENT) != null; + if (errorCode == DC_ERROR_INVALID_KEY && !recoverySpent) { // The key is gone or was never valid. Wipe it and try once from // scratch; a second failure is reported rather than looped. try { @@ -647,6 +659,9 @@ public static void nativeAttestError(final int requestId, final int errorCode, "App Attest could not discard the rejected key"); return; } + // Recorded before the replacement starts, so a request arriving after + // this process dies still sees the recovery as used. + SecureStorage.getInstance().set(KEY_RECOVERY_SPENT, "1"); PendingRequest retry = new PendingRequest(pending.result, pending.nonce, PendingRequest.OP_GENERATE_KEY, null); retry.retried = true; From 86ad845d6e2d6005e6b73fa833cee8fa4e20449d Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Wed, 29 Jul 2026 22:39:25 +0300 Subject: [PATCH 28/96] Check the marker write, and stop sharing the KeyStore across tiers The attestation-start marker write was unchecked, so a refused keychain write let attestKey proceed anyway -- recreating exactly the case the marker exists to catch: the app dies after Apple consumes the one-time attestation, the next launch sees a key with no marker, and resubmits the spent key. It now fails the request instead. The non-prompting tier used the shared AndroidKeyStore instance, which the biometric tier touches without PLAIN_KEY_LOCK. KeyStore is not thread safe, so that traded a race inside one tier for a race across two, surfacing as intermittent keystore errors neither tier's code explains. This tier now uses its own instance, which avoids widening the lock into the biometric path. Co-Authored-By: Claude Opus 5 (1M context) --- .../impl/android/AndroidSecureStorage.java | 20 +++++++++++++----- .../impl/ios/IOSDeviceIntegrity.java | 21 +++++++++++++++++-- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java b/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java index bd0e5e55217..41c708f95e8 100644 --- a/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java +++ b/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java @@ -359,8 +359,14 @@ private SecretKey plainKey(boolean create) throws Exception { // one had already encrypted with -- leaving that ciphertext permanently // undecryptable. The shared KeyStore is not thread safe either. synchronized (PLAIN_KEY_LOCK) { - keyStore().load(null); - SecretKey existing = (SecretKey) keyStore.getKey(PLAIN_KEY_ID, null); + // A KeyStore instance of this tier's own. The biometric tier touches the + // shared one without PLAIN_KEY_LOCK, and KeyStore is not thread safe, so + // sharing it here would trade a race inside this tier for a race across the + // two -- surfacing as intermittent keystore errors that neither tier's code + // would explain. Widening this lock into the biometric path would be worse. + KeyStore ks = KeyStore.getInstance(ANDROID_KEY_STORE); + ks.load(null); + SecretKey existing = (SecretKey) ks.getKey(PLAIN_KEY_ID, null); if (existing != null || !create) { return existing; } @@ -374,7 +380,7 @@ private SecretKey plainKey(boolean create) throws Exception { .setRandomizedEncryptionRequired(true) .build()); gen.generateKey(); - return (SecretKey) keyStore.getKey(PLAIN_KEY_ID, null); + return (SecretKey) ks.getKey(PLAIN_KEY_ID, null); } } @@ -385,8 +391,12 @@ private void resetPlainKey() { // a value this was about to wipe -- or worse, stored it just after. synchronized (PLAIN_KEY_LOCK) { try { - keyStore().deleteEntry(PLAIN_KEY_ID); - } catch (KeyStoreException e) { + // Same reasoning as plainKey(): this tier does not touch the shared + // KeyStore instance. + KeyStore ks = KeyStore.getInstance(ANDROID_KEY_STORE); + ks.load(null); + ks.deleteEntry(PLAIN_KEY_ID); + } catch (Exception e) { Log.e(e); } SharedPreferences prefs = plainPrefs(); diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index 2268fee0e71..f3b6ae63e4d 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -422,8 +422,25 @@ private void attestKey(AsyncResource r, String nonce, String keyId, bool // than natively keeps a single hash implementation across both paths. String hash = base64(Hash.sha256(bytes(nonce))); // Recorded BEFORE the call, so a crash between here and the result is - // distinguishable from never having tried. - SecureStorage.getInstance().set(KEY_ATTEST_STARTED, "1"); + // distinguishable from never having tried -- and the write is checked, because + // proceeding without the marker recreates exactly the case it exists to catch: + // the app dies after Apple consumes the one-time attestation, the next launch + // sees a key with no marker, and submits the spent key again. + if (!SecureStorage.getInstance().set(KEY_ATTEST_STARTED, "1")) { + bootstrapInFlight = false; + failBootstrapWaiters("App Attest could not record that attestation had started"); + final AsyncResource target = r; + Display.getInstance().callSerially(new Runnable() { + public void run() { + if (!target.isDone()) { + target.error(new RuntimeException("App Attest could not record that " + + "attestation had started, so it was not attempted rather " + + "than risk spending the key untracked")); + } + } + }); + return; + } PendingRequest pending = new PendingRequest(r, nonce, PendingRequest.OP_ATTEST, keyId); pending.retried = retried; int rid = register(pending); From 4a001ce067ac0fe8d16b815aa469883ff4bb4cfd Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Thu, 30 Jul 2026 01:09:33 +0300 Subject: [PATCH 29/96] Honour the exhausted recovery, and clear it on success Two halves of the same marker, both wrong in opposite directions. The interrupted-attestation branch never consulted KEY_RECOVERY_SPENT, so once a replacement key also failed, every later request came back through it, discarded the key and generated another -- exactly the loop the marker exists to stop. That case now reports instead, pointing at resetAttestation(). And the success path never cleared the marker -- my earlier edit for that did not land. A device whose recovery had succeeded would then refuse a future replacement when iOS legitimately invalidated the new key, leaving every assertion failing until the app reset by hand. Co-Authored-By: Claude Opus 5 (1M context) --- .../impl/ios/IOSDeviceIntegrity.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index f3b6ae63e4d..a4057e57273 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -364,6 +364,24 @@ AsyncResource requestToken(String nonce) { // previous attempt died between generating and submitting. Attest // that key rather than burning another. attestKey(r, nonce, keyId); + } else if (keyId != null && keyId.length() > 0 + && store.get(KEY_RECOVERY_SPENT) != null) { + // The interrupted key belongs to a recovery that has already been + // used once. Discarding it here would generate yet another + // rate-limited key, and would do so on every subsequent request -- + // the exact loop the spent marker exists to stop. Report instead. + bootstrapInFlight = false; + final AsyncResource exhausted = r; + Display.getInstance().callSerially(new Runnable() { + public void run() { + if (!exhausted.isDone()) { + exhausted.error(new RuntimeException("App Attest could not " + + "establish a usable key on this device; call " + + "DeviceIntegrity.resetAttestation() to try again")); + } + } + }); + return r; } else if (keyId != null && keyId.length() > 0) { // Attestation WAS started for this key and we never recorded the // outcome, so Apple may already have consumed it. Re-submitting a @@ -590,6 +608,11 @@ public static void nativeAttestationReady(final int requestId, final String atte "App Attest could not record its registration deadline"); return; } + // The recovery this key came from, if any, is now complete. Leaving the + // marker would refuse a future replacement when iOS legitimately invalidates + // THIS key, and every later assertion would fail until the app reset by hand. + store.remove(KEY_ATTEST_STARTED); + store.remove(KEY_RECOVERY_SPENT); instance.currentBackoff = MIN_BACKOFF_MILLIS; instance.bootstrapInFlight = false; // Deliberately NOT asserted here, for the reason above. Queued callers From 1a9dd40dd85f75ff1a8b70fb0baccd252466f9c0 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Thu, 30 Jul 2026 01:20:03 +0300 Subject: [PATCH 30/96] Run the request hook only when the request asked for it shouldInspectCertificates() is now also true when the guard pins the host, and both certificate paths then called the request's own hook regardless -- so a subclass that overrides checkSSLCertificates and deliberately disables it could reject or mutate requests purely because App Shield covers that host. The hook's contract is that it runs when the request opted in, and it is gated on that again; the guard runs independently. UnrecoverableKeyException on a write is handled like an invalid key. It fell into the generic catch, leaving the unusable alias installed, so every later set() returned false indefinitely and only a read happened to clear it -- an app that only writes could never store anything again. Co-Authored-By: Claude Opus 5 (1M context) --- .../com/codename1/io/ConnectionRequest.java | 20 ++++++++++++++----- .../impl/android/AndroidSecureStorage.java | 7 +++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/CodenameOne/src/com/codename1/io/ConnectionRequest.java b/CodenameOne/src/com/codename1/io/ConnectionRequest.java index 727e7b9e2ad..068f7f4caba 100644 --- a/CodenameOne/src/com/codename1/io/ConnectionRequest.java +++ b/CodenameOne/src/com/codename1/io/ConnectionRequest.java @@ -951,7 +951,10 @@ boolean checkCertificatesNativeCallback() { } try { SSLCertificate[] certs = getSSLCertificates(); - checkSSLCertificates(certs); + if (checkSSLCertificates) { + // Same gate as the non-callback path. + checkSSLCertificates(certs); + } NetworkGuard guard = NetworkManager.getNetworkGuard(); if (guard != null && guardWantsCertificates) { // Same split as the non-callback path: the hook above sees the flat view @@ -1132,10 +1135,17 @@ boolean performOperationComplete() throws IOException { // empty POST bodies. !Util.getImplementation().checkSSLCertificatesRequiresCallbackFromNative()) { sslCertificates = getSSLCertificatesImpl(connection, url); - // The request's own hook runs first and on the legacy flat view, so an - // app that already pins by overriding it keeps working; the guard layers - // on, and only the guard sees the enriched per-certificate form. - checkSSLCertificates(sslCertificates); + // Only when the request asked for it. shouldInspectCertificates() is now + // also true when the guard pins the host, and running the hook on that + // basis would let a subclass that deliberately disabled it reject or + // mutate requests purely because App Shield covers the host. The hook's + // contract is that it runs when the request opted in. + if (checkSSLCertificates) { + // The legacy flat view, so an app that already pins by overriding it + // keeps working; only the guard sees the enriched per-certificate + // form. + checkSSLCertificates(sslCertificates); + } NetworkGuard certGuard = NetworkManager.getNetworkGuard(); if (certGuard != null && guardWantsCertificates) { SSLCertificate[] forGuard = guardSSLCertificates(connection, url); diff --git a/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java b/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java index 41c708f95e8..3eb5d35a9f8 100644 --- a/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java +++ b/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java @@ -250,6 +250,13 @@ public boolean set(String account, String value) { // Includes KeyPermanentlyInvalidatedException. resetPlainKey(); return false; + } catch (UnrecoverableKeyException e) { + // Handled like an invalid key rather than falling into the generic catch: + // leaving the unusable alias installed made every later write return false + // for good, and only a read happened to clear it -- so an app that only ever + // writes could never store anything again. + resetPlainKey(); + return false; } catch (Throwable t) { Log.e(t); return false; From 08cbb049b4a5fd83e956bb70facc66e66dd0a15e Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Thu, 30 Jul 2026 01:32:32 +0300 Subject: [PATCH 31/96] Resolve implicit host policies late, and hold the spent marker in memory ShieldConfig snapshotted the default failure mode when a host was registered without an explicit policy, so .protect(h).defaultFailureMode( CLOSED) left that host fail-open while the finished config reported a closed default. A builder gets chained in whatever order reads well, so implicit registrations are now recorded as implicit and resolved against the default at read time. The recovery-spent marker's write result was ignored, so a refused keychain write let every later request discard the replacement key and generate another. It is mirrored in memory, which bounds the one-shot recovery for the life of the process even when the keychain will not persist it. Co-Authored-By: Claude Opus 5 (1M context) --- .../security/shield/ShieldConfig.java | 55 +++++++++++++++---- .../impl/ios/IOSDeviceIntegrity.java | 16 +++++- 2 files changed, 58 insertions(+), 13 deletions(-) diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java b/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java index 6fc70b2cb28..e23edcc5918 100644 --- a/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java +++ b/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java @@ -50,6 +50,9 @@ public final class ShieldConfig { private String endpoint = DEFAULT_ENDPOINT; private String tokenHeader = DEFAULT_TOKEN_HEADER; private FailureMode defaultFailureMode = FailureMode.OPEN; + /// Hosts registered without an explicit policy. Their policy is resolved from the default at + /// read time, so it does not depend on the order the builder was called in. + private final java.util.Vector implicitHosts = new java.util.Vector(); private int refreshThresholdPercent = 50; private boolean collectSignals = true; private final Hashtable hostPolicies = new Hashtable(); @@ -100,8 +103,20 @@ public ShieldConfig collectSignals(boolean collect) { /// subdomains. Hosts not registered here are never touched. public ShieldConfig protect(String hostPattern, HostPolicy policy) { if (hostPattern != null && hostPattern.length() > 0) { - hostPolicies.put(ShieldHosts.normalize(hostPattern), - policy == null ? implicitPolicy() : policy); + String key = ShieldHosts.normalize(hostPattern); + if (policy == null) { + // Recorded as implicit rather than resolved now. A builder is chained in + // whatever order reads well, so `.protect(h).defaultFailureMode(CLOSED)` + // must mean the same thing as the reverse -- snapshotting the default at + // registration time left the host fail-open while the finished config + // reported a closed default, which is the kind of disagreement nobody + // finds until it matters. + hostPolicies.remove(key); + implicitHosts.addElement(key); + } else { + implicitHosts.removeElement(key); + hostPolicies.put(key, policy); + } } return this; } @@ -109,7 +124,7 @@ public ShieldConfig protect(String hostPattern, HostPolicy policy) { /// Registers a host with the default policy, which honours /// [#defaultFailureMode(FailureMode)]. public ShieldConfig protect(String hostPattern) { - return protect(hostPattern, implicitPolicy()); + return protect(hostPattern, null); } /// The policy used when a host is registered without an explicit one. @@ -153,28 +168,48 @@ public HostPolicy policyFor(String host) { return HostPolicy.UNPROTECTED; } String h = ShieldHosts.normalize(host); - Object exact = hostPolicies.get(h); + HostPolicy exact = policyForKey(h); if (exact != null) { - return (HostPolicy) exact; + return exact; } int dot = h.indexOf('.'); while (dot >= 0 && dot < h.length() - 1) { - Object wild = hostPolicies.get("*." + h.substring(dot + 1)); + HostPolicy wild = policyForKey("*." + h.substring(dot + 1)); if (wild != null) { - return (HostPolicy) wild; + return wild; } dot = h.indexOf('.', dot + 1); } return HostPolicy.UNPROTECTED; } + /// The policy registered for an exact key, or null. Implicit registrations resolve against the + /// default as it stands now, not as it stood when they were registered. + private HostPolicy policyForKey(String key) { + Object explicit = hostPolicies.get(key); + if (explicit != null) { + return (HostPolicy) explicit; + } + return implicitHosts.contains(key) ? implicitPolicy() : null; + } + /// True when at least one host is registered, so callers can skip work entirely. public boolean hasProtectedHosts() { - return !hostPolicies.isEmpty(); + return !hostPolicies.isEmpty() || !implicitHosts.isEmpty(); } - /// The registered host patterns. + /// The registered host patterns, explicit and implicit alike. public Enumeration protectedHosts() { - return hostPolicies.keys(); + java.util.Vector all = new java.util.Vector(); + Enumeration keys = hostPolicies.keys(); + while (keys.hasMoreElements()) { + all.addElement(keys.nextElement()); + } + for (int i = 0; i < implicitHosts.size(); i++) { + if (!all.contains(implicitHosts.elementAt(i))) { + all.addElement(implicitHosts.elementAt(i)); + } + } + return all.elements(); } } diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index a4057e57273..f28fb7ad42d 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -165,6 +165,9 @@ final class IOSDeviceIntegrity { * dies, which is the case the keychain was covering.

*/ private long retryAfterFallback; + /// Mirrors [#KEY_RECOVERY_SPENT] in memory, so a refused keychain write still bounds the + /// one-shot recovery for the life of the process rather than letting it repeat per request. + private boolean recoverySpentInMemory; /** True while a generate-then-attest bootstrap is running. */ private boolean bootstrapInFlight; /** @@ -225,6 +228,7 @@ private void resetLocked() { store.remove(KEY_PENDING_SINCE); store.remove(KEY_ATTEST_STARTED); store.remove(KEY_RECOVERY_SPENT); + recoverySpentInMemory = false; if (!idGone || !stateGone) { // The two deletions are not atomic, so a partial failure leaves half the // identity gone. Treating that as "untouched" would let a callback from the @@ -365,7 +369,8 @@ AsyncResource requestToken(String nonce) { // that key rather than burning another. attestKey(r, nonce, keyId); } else if (keyId != null && keyId.length() > 0 - && store.get(KEY_RECOVERY_SPENT) != null) { + && (recoverySpentInMemory + || store.get(KEY_RECOVERY_SPENT) != null)) { // The interrupted key belongs to a recovery that has already been // used once. Discarding it here would generate yet another // rate-limited key, and would do so on every subsequent request -- @@ -613,6 +618,7 @@ public static void nativeAttestationReady(final int requestId, final String atte // THIS key, and every later assertion would fail until the app reset by hand. store.remove(KEY_ATTEST_STARTED); store.remove(KEY_RECOVERY_SPENT); + instance.recoverySpentInMemory = false; instance.currentBackoff = MIN_BACKOFF_MILLIS; instance.bootstrapInFlight = false; // Deliberately NOT asserted here, for the reason above. Queued callers @@ -683,7 +689,7 @@ public static void nativeAttestError(final int requestId, final int errorCode, "App Attest state was reset while this request was in flight"); return; } - boolean recoverySpent = pending.retried + boolean recoverySpent = pending.retried || instance.recoverySpentInMemory || SecureStorage.getInstance().get(KEY_RECOVERY_SPENT) != null; if (errorCode == DC_ERROR_INVALID_KEY && !recoverySpent) { // The key is gone or was never valid. Wipe it and try once from @@ -700,7 +706,11 @@ public static void nativeAttestError(final int requestId, final int errorCode, return; } // Recorded before the replacement starts, so a request arriving after - // this process dies still sees the recovery as used. + // this process dies still sees the recovery as used. The in-memory + // copy is set regardless: if the keychain refuses the write, the + // one-shot limit still holds for the life of the process rather than + // letting every later request burn another key. + instance.recoverySpentInMemory = true; SecureStorage.getInstance().set(KEY_RECOVERY_SPENT, "1"); PendingRequest retry = new PendingRequest(pending.result, pending.nonce, PendingRequest.OP_GENERATE_KEY, null); From c22f16730843754226d4605ce933fb1a2c0ca953 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Thu, 30 Jul 2026 01:42:34 +0300 Subject: [PATCH 32/96] Use a per-start generation, and confirm the marker deletion MCPServer's reader thread identified its own run by transport identity, so a caller that stopped and restarted with the SAME transport instance left the old thread looking current -- it could run on beside the replacement and eventually stop the server and close the transport underneath it. Each start now carries a generation and the thread checks both. The spent-recovery marker deletion result is honoured: if the keychain refuses it, the in-memory copy stays set so the process keeps treating the recovery as spent. Forgetting it while the persisted marker survived would refuse the one-shot replacement the next time iOS legitimately invalidated the key, leaving every assertion failing until the app reset by hand. Co-Authored-By: Claude Opus 5 (1M context) --- .../src/com/codename1/mcp/MCPServer.java | 33 ++++++++++++------- .../impl/ios/IOSDeviceIntegrity.java | 10 ++++-- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/CodenameOne/src/com/codename1/mcp/MCPServer.java b/CodenameOne/src/com/codename1/mcp/MCPServer.java index 51acdea382e..b1b2212c523 100644 --- a/CodenameOne/src/com/codename1/mcp/MCPServer.java +++ b/CodenameOne/src/com/codename1/mcp/MCPServer.java @@ -80,6 +80,9 @@ private static boolean isSupportedProtocol(String version) { private String serverVersion = "1.0"; private boolean screenshotEnabled = true; private boolean running; + /// Bumped by every start(), so a reader thread can tell its own run from a later one that + /// happens to have been handed the same transport instance. + private int startGeneration; private MCPTransport transport; private MCPVerbosity verbosity = MCPVerbosity.OFF; @@ -139,6 +142,8 @@ public synchronized void start(MCPTransport transport) { } this.transport = transport; running = true; + startGeneration++; + final int generation = startGeneration; // Captured for the thread rather than read back off the field. A later start() // replaces the field, and a reader thread that followed it would end up serving a // transport the server no longer considers its own -- while the transport it was @@ -147,7 +152,7 @@ public synchronized void start(MCPTransport transport) { Thread readerThread = new Thread(new Runnable() { @Override public void run() { - runLoop(mine); + runLoop(mine, generation); } }, "cn1-mcp-server"); readerThread.start(); @@ -164,27 +169,33 @@ public synchronized void stop() { /// True while `t` is still the transport this server is serving over. False once stop() /// has run, or once a restart handed the server a different transport -- in both cases /// the thread holding `t` owns it alone and has to close it itself. - private synchronized boolean isCurrent(MCPTransport t) { - return running && transport == t; // NOPMD identity is the question, not equality + private synchronized boolean isCurrent(MCPTransport t, int generation) { + // The generation as well as the identity. A caller that stops and restarts with + // the SAME transport instance would otherwise leave the old reader thread looking + // current, so it could run on beside the replacement and eventually stop the + // server and close the transport underneath it. + return running && startGeneration == generation + && transport == t; // NOPMD identity is the question, not equality } /// Clears the running flag, but only on behalf of the transport that is still current. /// A thread unwinding from a superseded transport must not stop a server that has since /// been restarted over a new one. - private synchronized void stopIfCurrent(MCPTransport t) { - if (transport == t) { // NOPMD identity: is this still our transport? + private synchronized void stopIfCurrent(MCPTransport t, int generation) { + if (startGeneration == generation + && transport == t) { // NOPMD identity: is this still our transport? running = false; } } - private void runLoop(MCPTransport t) { + private void runLoop(MCPTransport t, int generation) { // Opening is deferred to this thread, so by the time it happens the server may // already have been stopped or restarted. Either way stop()'s close() ran against a // transport that had not opened yet and therefore released nothing, so opening now // would leave a transport registered with nobody left to close it. That registration // is process-wide: every later open() is refused on the grounds that an agent is // already being served. - if (!isCurrent(t)) { + if (!isCurrent(t, generation)) { return; } try { @@ -199,17 +210,17 @@ private void runLoop(MCPTransport t) { } catch (Throwable logErr) { System.err.println("[cn1.mcp] transport open failed: " + ex); } - stopIfCurrent(t); + stopIfCurrent(t, generation); t.close(); return; } - if (!isCurrent(t)) { + if (!isCurrent(t, generation)) { // Same window, the far side of it: the server moved on while open() was in // flight. Undo the registration this thread just took. t.close(); return; } - while (isCurrent(t)) { + while (isCurrent(t, generation)) { String line; try { line = t.readMessage(); @@ -231,7 +242,7 @@ private void runLoop(MCPTransport t) { } } } - stopIfCurrent(t); + stopIfCurrent(t, generation); t.close(); } diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index f28fb7ad42d..cc68b32c2b0 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -617,8 +617,14 @@ public static void nativeAttestationReady(final int requestId, final String atte // marker would refuse a future replacement when iOS legitimately invalidates // THIS key, and every later assertion would fail until the app reset by hand. store.remove(KEY_ATTEST_STARTED); - store.remove(KEY_RECOVERY_SPENT); - instance.recoverySpentInMemory = false; + // The in-memory copy is cleared unconditionally, but the persisted marker is + // only forgotten once the keychain confirms the deletion. A stale marker + // would refuse the one-shot replacement the next time iOS legitimately + // invalidates this key, leaving every assertion failing until the app reset + // by hand -- so if the delete fails, the marker stays true here too and the + // process keeps behaving as though the recovery were spent, which is the + // conservative direction. + instance.recoverySpentInMemory = !store.remove(KEY_RECOVERY_SPENT); instance.currentBackoff = MIN_BACKOFF_MILLIS; instance.bootstrapInFlight = false; // Deliberately NOT asserted here, for the reason above. Queued callers From 4bba0eeca6157dee2c966dbdb65eae07242a6ba8 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Thu, 30 Jul 2026 02:13:53 +0300 Subject: [PATCH 33/96] Compose the shield guard, and stop a stale MCP reader closing a live transport NetworkManager holds one guard and seals the slot, so an app with its own guard had no way to keep the shield's. The advice it printed -- call attach() from your own guard -- restores the token header and silently drops the certificate callbacks, so HostPolicy.isEnforcePins() stopped being enforced and nothing about the app's behaviour said so. AppShield.getNetworkGuard() now returns the shield's guard so an app can delegate to it, documented with the whole delegation including the interestingResponseHeaders/afterResponse positional pairing, and the recovery message points at it. MCPServer's stale-run teardown closed the transport unconditionally. A stop()/start() pair over the same transport instance therefore had the old reader close the transport the restarted server was serving over: the generation check correctly kept it from stopping the server, then it killed the server anyway one line later. Ownership and the close are now decided together under the lock -- close when still current, close when superseded by a replacement holding a different transport (nobody else can, and a leaked transport stays registered process-wide), leave it alone when the replacement reused this one. The test parks each reader on its own ticket so a superseded reader can be released while the current one stays blocked, which is the only way to observe what the stale thread does on its way out; it fails on the previous code. The two anonymous Runnables in IOSDeviceIntegrity's instance methods captured the enclosing instance for as long as the EDT queue held them, for no reason. Extracted as a static failResource(), which is also what SpotBugs was asking for. Co-Authored-By: Claude Opus 5 (1M context) --- .../src/com/codename1/mcp/MCPServer.java | 44 +++++-- .../codename1/security/shield/AppShield.java | 73 ++++++++++-- .../security/shield/ShieldNetworkGuard.java | 6 +- .../impl/ios/IOSDeviceIntegrity.java | 42 +++---- .../mcp/MCPLoopbackTransportOpenTest.java | 112 ++++++++++++++++++ .../security/shield/ShieldApiTest.java | 26 ++++ 6 files changed, 263 insertions(+), 40 deletions(-) diff --git a/CodenameOne/src/com/codename1/mcp/MCPServer.java b/CodenameOne/src/com/codename1/mcp/MCPServer.java index b1b2212c523..2b34d8e04d8 100644 --- a/CodenameOne/src/com/codename1/mcp/MCPServer.java +++ b/CodenameOne/src/com/codename1/mcp/MCPServer.java @@ -178,14 +178,32 @@ private synchronized boolean isCurrent(MCPTransport t, int generation) { && transport == t; // NOPMD identity is the question, not equality } - /// Clears the running flag, but only on behalf of the transport that is still current. - /// A thread unwinding from a superseded transport must not stop a server that has since - /// been restarted over a new one. - private synchronized void stopIfCurrent(MCPTransport t, int generation) { + /// Clears the running flag when this thread is still the current run, and answers the + /// only other question a thread unwinding from `t` has: does it close `t`? + /// + /// Both halves have to be decided together under this lock. Deciding "am I current" and + /// then closing outside it leaves a window where a `stop()`/`start(sameTransport)` pair + /// hands `t` to a fresh generation -- and the old thread, correctly declining to stop + /// the *server*, would still close the transport the replacement is now serving over, + /// killing a server that had just been restarted. + /// + /// Three cases, only one of which closes nothing: + /// + /// - still current: this run is over, so clear `running` and close. + /// - superseded, and the replacement holds a different transport: nobody else can close + /// `t`, so this thread must. Leaking it is not benign -- an open transport stays + /// registered process-wide and every later `open()` is refused. + /// - superseded, and the replacement holds this same transport: leave it alone. It is + /// the live server's transport now, and the replacement thread will close it. + /// + /// @return true when the caller owns the close of `t` + private synchronized boolean releaseIfCurrent(MCPTransport t, int generation) { if (startGeneration == generation && transport == t) { // NOPMD identity: is this still our transport? running = false; + return true; } + return transport != t; // NOPMD identity: reused by the replacement, or orphaned? } private void runLoop(MCPTransport t, int generation) { @@ -210,14 +228,19 @@ private void runLoop(MCPTransport t, int generation) { } catch (Throwable logErr) { System.err.println("[cn1.mcp] transport open failed: " + ex); } - stopIfCurrent(t, generation); - t.close(); + if (releaseIfCurrent(t, generation)) { + t.close(); + } return; } if (!isCurrent(t, generation)) { // Same window, the far side of it: the server moved on while open() was in - // flight. Undo the registration this thread just took. - t.close(); + // flight. Undo the registration this thread just took -- unless the server + // moved on by restarting over this very transport, in which case it is not + // ours to undo. + if (releaseIfCurrent(t, generation)) { + t.close(); + } return; } while (isCurrent(t, generation)) { @@ -242,8 +265,9 @@ private void runLoop(MCPTransport t, int generation) { } } } - stopIfCurrent(t, generation); - t.close(); + if (releaseIfCurrent(t, generation)) { + t.close(); + } } /// Handles one inbound JSON-RPC message and returns the response line, or null diff --git a/CodenameOne/src/com/codename1/security/shield/AppShield.java b/CodenameOne/src/com/codename1/security/shield/AppShield.java index 1df5c8d64a6..ce318fecf8a 100644 --- a/CodenameOne/src/com/codename1/security/shield/AppShield.java +++ b/CodenameOne/src/com/codename1/security/shield/AppShield.java @@ -24,6 +24,7 @@ import com.codename1.io.ConnectionRequest; import com.codename1.io.Log; +import com.codename1.io.NetworkGuard; import com.codename1.io.NetworkManager; import com.codename1.security.shield.spi.ShieldEngine; import com.codename1.security.shield.spi.ShieldEngineRegistry; @@ -77,6 +78,7 @@ public final class AppShield { private static ShieldConfig config; private static boolean initialized; + private static NetworkGuard guard; private static ShieldStatus lastStatus = ShieldStatus.NOT_INITIALIZED; private static final Vector listeners = new Vector(); private static final Hashtable runtimeHosts = new Hashtable(); @@ -130,20 +132,75 @@ public static void init(ShieldConfig cfg) { /// behaviour identical whether or not the enterprise engine was injected. private static void installNetworkGuard() { try { - NetworkManager.setNetworkGuard(new ShieldNetworkGuard()); + NetworkManager.setNetworkGuard(getNetworkGuard()); } catch (IllegalStateException e) { - // The slot seals after the first install. An app that installed its - // own guard keeps it; say so rather than failing startup, because the - // consequence is that protected hosts are not decorated automatically - // and that is worth knowing about. - Log.p("AppShield: a network guard is already installed, so protected hosts will " - + "not be decorated automatically. Call AppShield.attach(request) from " - + "your own guard if you need both."); + // The slot seals after the first install. An app that installed its own guard + // keeps it; say so rather than failing startup, and point at the composition + // that gets everything back -- not just the token. Advising attach() alone + // would restore header decoration while quietly dropping pin enforcement, + // which is the half of the shield an app cannot notice is missing. + Log.p("AppShield: a network guard is already installed, so protected hosts are " + + "not decorated or pinned automatically. Delegate to " + + "AppShield.getNetworkGuard() from your own guard to restore both; " + + "see the AppShield.getNetworkGuard() documentation."); } catch (Throwable t) { Log.e(t); } } + /// The shield's own [NetworkGuard], for an app that has to install a guard of its own. + /// + /// [NetworkManager] holds a single guard and seals the slot on first install, so an app + /// with its own guard leaves no room for the shield's. Delegating to this one is the + /// supported way to have both. Delegate every method, not only + /// [NetworkGuard#beforeRequest(ConnectionRequest)]: attaching the token is the visible + /// half of the shield, and the certificate callbacks are the half that enforces + /// [HostPolicy#isEnforcePins()]. An app that forwards only `beforeRequest` gets tokens + /// and no pinning, and nothing about its behaviour says so. + /// + /// ```java + /// final NetworkGuard shield = AppShield.getNetworkGuard(); + /// NetworkManager.setNetworkGuard(new NetworkGuard() { + /// public void beforeRequest(ConnectionRequest r) throws IOException { + /// myOwnHeaders(r); + /// shield.beforeRequest(r); + /// } + /// public boolean isCertificateCheckRequired(String url) { + /// return myOwnCheckNeeded(url) || shield.isCertificateCheckRequired(url); + /// } + /// public void checkCertificates(ConnectionRequest r, + /// ConnectionRequest.SSLCertificate[] c) throws IOException { + /// myOwnCheck(r, c); + /// shield.checkCertificates(r, c); + /// } + /// public String[] interestingResponseHeaders() { + /// return concat(myOwnHeaderNames(), shield.interestingResponseHeaders()); + /// } + /// public void afterResponse(ConnectionRequest r, int code, String[] headers) { + /// shield.afterResponse(r, code, headers); + /// } + /// }); + /// AppShield.init(cfg); + /// ``` + /// + /// Note that `interestingResponseHeaders()` has to be the union of both guards' names, and + /// that the `headers` array handed to `afterResponse` is positional against it -- so a + /// composing guard must pass the shield the slice that corresponds to the shield's own + /// names, in that order. Installing the shield's guard directly, by calling + /// [#init(ShieldConfig)] before installing anything of your own, avoids the bookkeeping + /// entirely and is what most apps should do. + /// + /// Safe to call before [#init(ShieldConfig)]; the returned guard reads the configuration + /// live rather than capturing it. + public static NetworkGuard getNetworkGuard() { + synchronized (AppShield.class) { + if (guard == null) { + guard = new ShieldNetworkGuard(); + } + return guard; + } + } + /// True when a real attestation engine is present and available. False in an open-source or /// unentitled build, and in the simulator unless simulation is switched on. public static boolean isProtected() { diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldNetworkGuard.java b/CodenameOne/src/com/codename1/security/shield/ShieldNetworkGuard.java index 650b378a1cf..6eceedd9804 100644 --- a/CodenameOne/src/com/codename1/security/shield/ShieldNetworkGuard.java +++ b/CodenameOne/src/com/codename1/security/shield/ShieldNetworkGuard.java @@ -35,8 +35,10 @@ /// would only ever be attached by an app calling [AppShield#attach(ConnectionRequest)] by hand. /// Installed once from [AppShield#init(ShieldConfig)]. /// -/// Package-private: apps configure behaviour through [ShieldConfig], and an app-supplied guard -/// could only weaken this one. +/// The class stays package-private -- apps configure behaviour through [ShieldConfig], and there +/// is nothing here to subclass or reconfigure. The *instance* is reachable through +/// [AppShield#getNetworkGuard()], because [com.codename1.io.NetworkManager] holds one guard and +/// an app that needs its own has to be able to delegate to this one rather than displace it. final class ShieldNetworkGuard implements NetworkGuard { @Override diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index cc68b32c2b0..a5d5c3ea876 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -376,16 +376,8 @@ AsyncResource requestToken(String nonce) { // rate-limited key, and would do so on every subsequent request -- // the exact loop the spent marker exists to stop. Report instead. bootstrapInFlight = false; - final AsyncResource exhausted = r; - Display.getInstance().callSerially(new Runnable() { - public void run() { - if (!exhausted.isDone()) { - exhausted.error(new RuntimeException("App Attest could not " - + "establish a usable key on this device; call " - + "DeviceIntegrity.resetAttestation() to try again")); - } - } - }); + failResource(r, "App Attest could not establish a usable key on this " + + "device; call DeviceIntegrity.resetAttestation() to try again"); return r; } else if (keyId != null && keyId.length() > 0) { // Attestation WAS started for this key and we never recorded the @@ -452,16 +444,8 @@ private void attestKey(AsyncResource r, String nonce, String keyId, bool if (!SecureStorage.getInstance().set(KEY_ATTEST_STARTED, "1")) { bootstrapInFlight = false; failBootstrapWaiters("App Attest could not record that attestation had started"); - final AsyncResource target = r; - Display.getInstance().callSerially(new Runnable() { - public void run() { - if (!target.isDone()) { - target.error(new RuntimeException("App Attest could not record that " - + "attestation had started, so it was not attempted rather " - + "than risk spending the key untracked")); - } - } - }); + failResource(r, "App Attest could not record that attestation had started, so it " + + "was not attempted rather than risk spending the key untracked"); return; } PendingRequest pending = new PendingRequest(r, nonce, PendingRequest.OP_ATTEST, keyId); @@ -842,6 +826,24 @@ public void run() { }); } + /** + * Fails a caller that has no PendingRequest yet, on the EDT. + * + *

Static, and not merely for tidiness: the callers are instance methods, so an + * anonymous Runnable written inline there captures the enclosing IOSDeviceIntegrity. + * That reference outlives the call for as long as the EDT queue holds the Runnable, + * and it is a reference nothing here needs.

+ */ + private static void failResource(final AsyncResource r, final String msg) { + Display.getInstance().callSerially(new Runnable() { + public void run() { + if (!r.isDone()) { + r.error(new RuntimeException(msg)); + } + } + }); + } + private static void fail(final PendingRequest pending, final String msg) { Display.getInstance().callSerially(new Runnable() { public void run() { diff --git a/maven/core-unittests/src/test/java/com/codename1/mcp/MCPLoopbackTransportOpenTest.java b/maven/core-unittests/src/test/java/com/codename1/mcp/MCPLoopbackTransportOpenTest.java index 6872b64af19..d27fbdc20e0 100644 --- a/maven/core-unittests/src/test/java/com/codename1/mcp/MCPLoopbackTransportOpenTest.java +++ b/maven/core-unittests/src/test/java/com/codename1/mcp/MCPLoopbackTransportOpenTest.java @@ -29,6 +29,7 @@ import java.io.IOException; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -173,6 +174,117 @@ public void close() { } } + /// A transport that can be opened more than once, and whose readMessage parks each + /// reader thread on its own ticket. That is what lets a test release a *superseded* + /// reader while the current one stays parked, which is the only way to observe what the + /// superseded thread does on its way out. + private static final class ReusableTransport implements MCPTransport { + private final Object gate = new Object(); + private int nextTicket; + private int parked; + private int releaseBelow; + volatile int openCount; + volatile int closeCount; + + void awaitParked(int n) throws InterruptedException { + synchronized (gate) { + while (parked < n) { + gate.wait(5000); + } + } + } + + /// Lets the readers holding the first `n` tickets out of readMessage, leaving any + /// later reader parked. + void releaseFirst(int n) { + synchronized (gate) { + releaseBelow = n; + gate.notifyAll(); + } + } + + public void open() throws IOException { + synchronized (gate) { + openCount++; + } + } + + public String readMessage() throws IOException { + int ticket; + synchronized (gate) { + ticket = nextTicket++; + parked++; + gate.notifyAll(); + while (releaseBelow <= ticket) { + try { + gate.wait(5000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IOException("interrupted"); + } + } + } + return null; + } + + public void writeMessage(String message) throws IOException { + } + + public void close() { + synchronized (gate) { + closeCount++; + gate.notifyAll(); + } + } + } + + private static int liveReaderThreads() { + int n = 0; + for (Thread t : Thread.getAllStackTraces().keySet()) { + if (t.isAlive() && "cn1-mcp-server".equals(t.getName())) { + n++; + } + } + return n; + } + + @Test + void aSupersededReaderMustNotCloseATransportTheRestartIsNowServing() throws Exception { + // Restarting over the SAME transport instance. Declining to clear `running` is only + // half of what a superseded thread owes the replacement: if it still closes the + // transport on its way out, it closes the one the restarted server is serving over, + // and a server that came back up is dead again a moment later. + MCPServer server = new MCPServer(); + ReusableTransport transport = new ReusableTransport(); + server.start(transport); + transport.awaitParked(1); + + server.stop(); + assertEquals(1, transport.closeCount, "stop() closes the transport it was serving"); + + // Same instance, which is what a caller that keeps one transport around does. + server.start(transport); + transport.awaitParked(2); + assertEquals(2, transport.openCount, "the restart should have reopened the transport"); + + // Only the superseded reader is let go. The current one stays parked, so anything + // that happens to the transport from here is the superseded thread's doing. + transport.releaseFirst(1); + for (int i = 0; i < 500 && liveReaderThreads() > 1; i++) { + Thread.sleep(10); + } + assertEquals(1, liveReaderThreads(), + "the superseded reader thread should have exited"); + + assertEquals(1, transport.closeCount, + "the superseded reader must leave the restarted server's transport open"); + assertTrue(server.isRunning(), + "the restarted server must still be serving after the old reader unwinds"); + + server.stop(); + transport.releaseFirst(2); + } + @Test void stoppingWhileTheReaderThreadIsStillOpeningStillClosesTheTransport() throws Exception { // start() hands the transport to a reader thread, so stop() can run before that diff --git a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java index 607ba007c75..d3042fffe9d 100644 --- a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java +++ b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java @@ -22,6 +22,7 @@ */ package com.codename1.security.shield; +import com.codename1.io.NetworkGuard; import org.junit.jupiter.api.Test; import java.util.Hashtable; @@ -29,6 +30,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -389,6 +391,30 @@ void severityIsClampedToTheDocumentedRange() { assertEquals(0, new ShieldSignal("x", -5, null).getSeverity()); } + // --- guard composition ---------------------------------------------- + + @Test + void theShieldGuardIsReachableAndStableSoAnAppCanDelegateToIt() { + // NetworkManager holds exactly one guard and seals the slot, so an app with its own + // guard can only keep the shield by delegating to it. Before this was reachable, the + // documented recovery was "call attach() yourself", which restores the token header + // and silently drops the certificate callbacks that enforce isEnforcePins() -- the + // half of the shield whose absence looks exactly like success. + NetworkGuard g = AppShield.getNetworkGuard(); + assertNotNull(g, "the shield's guard must be reachable for composition"); + assertSame(g, AppShield.getNetworkGuard(), + "delegating apps hold on to the guard, so it has to be the same instance"); + + // The pin callbacks are the point: they must be present on the returned type, not + // only on a package-private class an app cannot name. + assertFalse(g.isCertificateCheckRequired("https://unregistered.example/x"), + "an unregistered host must not trigger certificate collection"); + String[] interesting = g.interestingResponseHeaders(); + assertNotNull(interesting, + "a composing guard has to be able to union these with its own"); + assertTrue(interesting.length > 0); + } + @Test void hasSignalAtLeastReflectsRecordedSeverities() { ShieldSignals.clear(); From b4efd02fdf51ba38fcf9962a14168fc7651c1437 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Thu, 30 Jul 2026 02:30:42 +0300 Subject: [PATCH 34/96] Reuse the App Attest key when Apple says it never processed the attestation serverUnavailable recorded the backoff and left the started marker in place, so after the deadline the interrupted-attestation branch treated an explicit retryable failure as an unknown outcome, discarded a perfectly good rate-limited hardware key and generated another. Repeated often enough -- once per backoff through an Apple outage -- the recovery throttles the device more thoroughly than the outage did. The marker means "submitted, outcome unknown", and discarding the key is the right answer to that, because a spent one-time attestation cannot be resubmitted. serverUnavailable is not that: Apple has told us it did not process the attestation, so the key is untouched and the marker has nothing left to protect. Cleared for that code alone, on the attest operation alone. A failed removal leaves it in place, which is the conservative state the branch reached before. Co-Authored-By: Claude Opus 5 (1M context) --- .../codename1/impl/ios/IOSDeviceIntegrity.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index a5d5c3ea876..4bec4a436e9 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -724,6 +724,21 @@ public static void nativeAttestError(final int requestId, final int errorCode, SecureStorage.getInstance().set(KEY_RETRY_AFTER, Long.toString(deadline)); instance.currentBackoff = Math.min(backoff * 2, MAX_BACKOFF_MILLIS); + if (pending.op == PendingRequest.OP_ATTEST) { + // Apple told us it did not process this attestation, so the key + // is untouched and the started marker has nothing left to + // protect. Clearing it is what makes the next attempt re-attest + // THIS key: the marker means "submitted, outcome unknown", and + // the interrupted-attestation branch answers an unknown outcome + // by discarding the key, since a spent one-time attestation + // cannot be resubmitted. Applied to an explicit + // serverUnavailable, that reasoning burns a rate-limited + // hardware key on every Apple outage -- once per backoff, until + // the device is throttled by the recovery rather than by the + // outage. A failed removal leaves the marker in place, which is + // the conservative state this branch used to reach anyway. + SecureStorage.getInstance().remove(KEY_ATTEST_STARTED); + } } if (pending.op != PendingRequest.OP_ASSERT) { // Still the same acquisition: releasing here and reacquiring would From 0d605094af6de54cb0d2161a0abf1b7c82d80dfb Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Thu, 30 Jul 2026 02:52:27 +0300 Subject: [PATCH 35/96] Release the bootstrap queue when a native callback returns nothing nativeKeyGenerated with no key identifier, and nativeAttestationReady with no attestation, failed only the request that started the bootstrap. bootstrapInFlight stayed set, so every caller already queued behind it -- and every caller that arrived afterwards and was queued precisely because a bootstrap looked live -- waited on a bootstrap that had already stopped, with nothing left to release them. The mid-flow storage failures in both callbacks got this right; the two give-up-early branches did not. Both now go through one helper that clears the flag and fails the waiters under the same lock a reset takes. A stale request still only fails itself: a reset has already cleared the flag and failed the waiters, and clearing it again would clear the flag belonging to the replacement bootstrap that reset started. Co-Authored-By: Claude Opus 5 (1M context) --- .../impl/ios/IOSDeviceIntegrity.java | 37 ++++++++++++++++++- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index 4bec4a436e9..a4208ebe34f 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -490,6 +490,36 @@ private static String escapeJson(String s) { // ---- Callbacks invoked from native code (do not rename) ---------------- + /** + * Fails a request and the bootstrap it was driving. + * + *

For the branches that give up before touching storage. Failing only the + * initiating request there left {@code bootstrapInFlight} set, so every caller + * already queued behind it -- and every caller that arrived afterwards and was queued + * because a bootstrap looked live -- waited on a bootstrap that had already stopped, + * with nothing left to release them. The mid-flow storage failures got this right; + * these two did not.

+ * + *

A stale request only fails itself: a reset has already cleared the flag and + * failed the waiters, and clearing it again would clear the flag belonging to the + * replacement bootstrap that reset started.

+ */ + private static void failBootstrapAttempt(PendingRequest pending, String msg) { + if (instance == null) { + fail(pending, msg); + return; + } + synchronized (instance.flowLock) { + if (isStale(pending)) { + fail(pending, "App Attest state was reset while this request was in flight"); + return; + } + instance.bootstrapInFlight = false; + fail(pending, msg); + instance.failBootstrapWaiters(msg); + } + } + /** Called from native once a fresh hardware key exists. */ public static void nativeKeyGenerated(final int requestId, final String keyId) { PendingRequest pending = take(requestId); @@ -497,7 +527,8 @@ public static void nativeKeyGenerated(final int requestId, final String keyId) { return; } if (keyId == null || keyId.length() == 0) { - fail(pending, "App Attest key generation returned no identifier"); + failBootstrapAttempt(pending, + "App Attest key generation returned no identifier"); return; } // The staleness check and the writes it guards happen under the same lock a @@ -540,7 +571,9 @@ public static void nativeAttestationReady(final int requestId, final String atte return; } if (attestationB64 == null) { - fail(pending, "App Attest attestation returned no data"); + // Same cleanup as key generation returning nothing: this bootstrap is over, + // and the queue behind it has to be told. + failBootstrapAttempt(pending, "App Attest attestation returned no data"); return; } if (instance == null) { From a88c9a8c33fe33009109c2757c83831b459287c2 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Thu, 30 Jul 2026 03:11:22 +0300 Subject: [PATCH 36/96] Hold one lock across the assertion callback's staleness check and what it clears nativeAssertionReady checked staleness under the lock, released it, and then reacquired it to reset the throttle state. A reset landing in between let the old callback clear currentBackoff, retryAfterFallback and the stored retry deadline on behalf of the replacement generation -- and if that replacement had already recorded a serverUnavailable, its deadline went with them, while succeed() went on to reject this token as stale anyway. So the next request walked straight back into a service Apple had just told the device to stay away from, which is how an app gets its attestation budget suspended. One acquisition now covers both, which is the rule nativeAttestError already states and follows. The empty-payload check moved above it: it is a test of an argument, not of shared state, and doing it first keeps the lock holding only what the generation guards. Co-Authored-By: Claude Opus 5 (1M context) --- .../impl/ios/IOSDeviceIntegrity.java | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index a4208ebe34f..3fe27c033cc 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -660,7 +660,19 @@ public static void nativeAssertionReady(final int requestId, final String assert if (pending == null) { return; } + if (assertionB64 == null) { + fail(pending, "App Attest assertion returned no data"); + return; + } if (instance != null) { + // ONE acquisition covering the staleness check and everything it guards, for + // the reason spelled out on nativeAttestError. Split in two, a reset landing + // between them let this callback clear the throttle state belonging to the + // replacement generation: if that replacement had already recorded a + // serverUnavailable, its deadline was erased here while succeed() went on to + // reject this token as stale anyway -- so the next request went straight back + // at a service Apple had just told us to stay away from, which is how an app + // gets its whole attestation budget suspended. synchronized (instance.flowLock) { if (isStale(pending)) { // A reset landed while this assertion was in flight -- typically @@ -671,14 +683,6 @@ public static void nativeAssertionReady(final int requestId, final String assert "App Attest state was reset while this request was in flight"); return; } - } - } - if (assertionB64 == null) { - fail(pending, "App Attest assertion returned no data"); - return; - } - if (instance != null) { - synchronized (instance.flowLock) { // A working assertion means Apple is answering again, so the throttle // sequence starts over. Without this the doubling only ever accumulated: // outages separated by months of successful requests would compound From f599ebfb1a750ea4500a754cc01dca00f7617ec7 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Thu, 30 Jul 2026 03:26:30 +0300 Subject: [PATCH 37/96] Close the transport under the monitor, and keep the key on any answered failure releaseIfCurrent decided ownership under the lock and returned; the caller closed afterwards. A reader reaching EOF clears running, a restart over the same transport lands in that gap, and the close then lands on the replacement's transport -- the same outcome the ownership check was added to prevent, moved a few instructions later. The decision and the close now happen in one synchronized method, which is what stop() already does. Attestation kept its start marker for every error except serverUnavailable, so a persistent invalidInput or featureUnsupported discarded a still-unconsumed hardware key and generated another on every request, with nothing bounding it. Reaching that callback at all means Apple answered, so the outcome is not unknown -- and for anything but invalidKey, which resets separately, the key was not consumed. The marker is cleared for all of them, including unknownSystemFailure: if that one did land after Apple consumed the attestation, resubmitting is answered with invalidKey and self-corrects through the one-shot recovery, which is cheaper than a fresh rate-limited key per request forever. Co-Authored-By: Claude Opus 5 (1M context) --- .../src/com/codename1/mcp/MCPServer.java | 32 +++++++++------- .../impl/ios/IOSDeviceIntegrity.java | 38 +++++++++++-------- 2 files changed, 42 insertions(+), 28 deletions(-) diff --git a/CodenameOne/src/com/codename1/mcp/MCPServer.java b/CodenameOne/src/com/codename1/mcp/MCPServer.java index 2b34d8e04d8..5250eef5868 100644 --- a/CodenameOne/src/com/codename1/mcp/MCPServer.java +++ b/CodenameOne/src/com/codename1/mcp/MCPServer.java @@ -196,14 +196,26 @@ private synchronized boolean isCurrent(MCPTransport t, int generation) { /// - superseded, and the replacement holds this same transport: leave it alone. It is /// the live server's transport now, and the replacement thread will close it. /// - /// @return true when the caller owns the close of `t` - private synchronized boolean releaseIfCurrent(MCPTransport t, int generation) { + /// The close happens here, under the monitor, rather than being reported back to a + /// caller that closes afterwards. Deciding and then closing outside the lock leaves + /// the very gap this method exists to remove: a reader reaching EOF clears `running`, + /// a restart over the same transport lands in the gap, and the close then lands on + /// the replacement's transport. + private synchronized void releaseAndCloseIfCurrent(MCPTransport t, int generation) { + boolean mine; if (startGeneration == generation && transport == t) { // NOPMD identity: is this still our transport? running = false; - return true; + mine = true; + } else { + mine = transport != t; // NOPMD identity: reused by the replacement, or orphaned? + } + if (mine) { + // Closing under the monitor matches stop(), which is synchronized and closes + // the same way. A transport's close() releases a socket; it does not call + // back into the server, so there is nothing here to deadlock against. + t.close(); } - return transport != t; // NOPMD identity: reused by the replacement, or orphaned? } private void runLoop(MCPTransport t, int generation) { @@ -228,9 +240,7 @@ private void runLoop(MCPTransport t, int generation) { } catch (Throwable logErr) { System.err.println("[cn1.mcp] transport open failed: " + ex); } - if (releaseIfCurrent(t, generation)) { - t.close(); - } + releaseAndCloseIfCurrent(t, generation); return; } if (!isCurrent(t, generation)) { @@ -238,9 +248,7 @@ private void runLoop(MCPTransport t, int generation) { // flight. Undo the registration this thread just took -- unless the server // moved on by restarting over this very transport, in which case it is not // ours to undo. - if (releaseIfCurrent(t, generation)) { - t.close(); - } + releaseAndCloseIfCurrent(t, generation); return; } while (isCurrent(t, generation)) { @@ -265,9 +273,7 @@ private void runLoop(MCPTransport t, int generation) { } } } - if (releaseIfCurrent(t, generation)) { - t.close(); - } + releaseAndCloseIfCurrent(t, generation); } /// Handles one inbound JSON-RPC message and returns the response line, or null diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index 3fe27c033cc..dbd67bc6d33 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -761,21 +761,29 @@ public static void nativeAttestError(final int requestId, final int errorCode, SecureStorage.getInstance().set(KEY_RETRY_AFTER, Long.toString(deadline)); instance.currentBackoff = Math.min(backoff * 2, MAX_BACKOFF_MILLIS); - if (pending.op == PendingRequest.OP_ATTEST) { - // Apple told us it did not process this attestation, so the key - // is untouched and the started marker has nothing left to - // protect. Clearing it is what makes the next attempt re-attest - // THIS key: the marker means "submitted, outcome unknown", and - // the interrupted-attestation branch answers an unknown outcome - // by discarding the key, since a spent one-time attestation - // cannot be resubmitted. Applied to an explicit - // serverUnavailable, that reasoning burns a rate-limited - // hardware key on every Apple outage -- once per backoff, until - // the device is throttled by the recovery rather than by the - // outage. A failed removal leaves the marker in place, which is - // the conservative state this branch used to reach anyway. - SecureStorage.getInstance().remove(KEY_ATTEST_STARTED); - } + } + if (pending.op == PendingRequest.OP_ATTEST) { + // The marker means "submitted, outcome unknown", and the + // interrupted-attestation branch answers an unknown outcome by + // discarding the key, because a spent one-time attestation cannot be + // resubmitted. But reaching this callback at all means Apple + // answered: the outcome is known, and for anything other than + // invalidKey -- which is handled above by resetting -- the key was + // not consumed. Leaving the marker set therefore burned a + // rate-limited hardware key on every request for as long as the + // condition lasted, whether that was an outage or a persistently + // invalid input. + // + // unknownSystemFailure is the one code that could in principle have + // landed after Apple consumed the attestation. It is cleared anyway: + // resubmitting a consumed key is answered with invalidKey, which + // routes into the one-shot recovery above and self-corrects, whereas + // keeping the marker costs a fresh hardware key per request with + // nothing bounding it. + // + // A failed removal leaves the marker in place, which is the + // conservative state this branch used to reach anyway. + SecureStorage.getInstance().remove(KEY_ATTEST_STARTED); } if (pending.op != PendingRequest.OP_ASSERT) { // Still the same acquisition: releasing here and reacquiring would From c917081f6fe943e51169ab6bc971fef83e390288 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Thu, 30 Jul 2026 03:51:49 +0300 Subject: [PATCH 38/96] Make the simulator's shield switches act, and bound the DER walk Simulate > App Shield wrote fields nothing read. No engine is registered in the simulator, so the inert default answered every call: Force Pin Mismatch left requests succeeding, the status dialog agreed the state was "forcing mismatch", and the fail-closed branch the switch exists to reach was unreachable. A control that reports a state it does not cause is worse than no control -- it is a test that passes for the wrong reason. JavaSEShieldEngine implements the SPI against those toggles: outcomes arrive as their own ShieldStatus, an expired token is actually expired (validity is answered from a monotonic reading, so backdating fetchedAt would not have done it), a failed pin fetch yields no enforcement rather than a mismatch, and the forced mismatch is one-shot because the switch says "on next request". The pin set covers the app's registered hosts, which is load-bearing rather than decoration: with no pins for a host the guard never asks verifyPins() at all. Registered when a toggle is switched on -- including one restored from preferences -- and never merely because the menu exists, since sealing the registry at startup would make every app run in the simulator report itself protected. It initializes itself there as well as from AppShield.init(), because the two happen in either order and a null config means no pinned hosts, which is the same do-nothing switch reached by a different route. cn1ReadDerTlv now bounds its arithmetic against what is left in the buffer rather than by forming sums that could wrap. I could not construct an input that overreads on the old code -- rejecting more than four length bytes already caps the content length below what a 64-bit NSUInteger can wrap -- so this is defence in depth, not a fix for a reachable bug. A standalone harness over short-form, long-form, oversized, truncated, exact-fit, past-the-end and walked-offset cases confirms the behaviour is unchanged for everything valid. The App Attest start marker is also remembered in memory when the keychain refuses to drop it, so a refused removal does not put the device straight back to burning one rate-limited key per request. Co-Authored-By: Claude Opus 5 (1M context) --- .../com/codename1/impl/javase/JavaSEPort.java | 35 ++- .../impl/javase/JavaSEShieldEngine.java | 267 ++++++++++++++++++ .../nativeSources/NetworkConnectionImpl.m | 15 +- .../impl/ios/IOSDeviceIntegrity.java | 36 ++- .../impl/javase/JavaSEShieldEngineTest.java | 136 +++++++++ 5 files changed, 480 insertions(+), 9 deletions(-) create mode 100644 Ports/JavaSE/src/com/codename1/impl/javase/JavaSEShieldEngine.java create mode 100644 maven/javase/src/test/java/com/codename1/impl/javase/JavaSEShieldEngineTest.java diff --git a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEPort.java b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEPort.java index 7647e1dbdbe..344c8782dd7 100644 --- a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEPort.java +++ b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEPort.java @@ -8446,7 +8446,17 @@ public void set(boolean v) { status.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { - JOptionPane.showMessageDialog(canvas, JavaSEShield.describe(), + // The engine line matters: every toggle below the attestation section + // is read by the simulator engine, so "not registered" is the + // difference between a switch that acts and a switch that only shows. + JOptionPane.showMessageDialog(canvas, JavaSEShield.describe() + + "Simulator engine: " + + (com.codename1.security.shield.spi.ShieldEngineRegistry + .isEngineRegistered() + ? com.codename1.security.shield.spi.ShieldEngineRegistry + .getEngine().getName() + " (registered)" + : "not registered -- switch on any toggle above to install it") + + "\n", "App Shield Simulation", JOptionPane.INFORMATION_MESSAGE); } }); @@ -8467,17 +8477,36 @@ private JCheckBoxMenuItem shieldToggle(final Preferences pref, String label, final String prefKey, final ShieldToggleSink sink) { final JCheckBoxMenuItem item = new JCheckBoxMenuItem(label, pref.getBoolean(prefKey, false)); - sink.set(item.isSelected()); + applyShieldToggle(sink, item.isSelected()); item.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { - sink.set(item.isSelected()); + applyShieldToggle(sink, item.isSelected()); pref.putBoolean(prefKey, item.isSelected()); } }); return item; } + /** + * Applies a shield toggle and, when it is being switched ON, makes sure the + * simulator engine is registered. + * + *

The token and pinning toggles are read by {@link JavaSEShieldEngine}, and with + * no engine registered the inert default answers instead -- so the switches set a + * field, the status dialog reported the field, and nothing behaved differently. + * Registration happens here rather than when the menu is built, because sealing the + * registry at startup would make every app run in the simulator report itself as + * protected. It also covers a toggle restored from preferences, since a developer + * who left one armed expects it to still be armed.

+ */ + private void applyShieldToggle(ShieldToggleSink sink, boolean value) { + sink.set(value); + if (value) { + JavaSEShieldEngine.ensureRegistered(); + } + } + private JMenu installNfcSimulationMenu(JMenu simulateMenu, final Preferences pref) { JMenu nfcMenu = new JMenu("NFC"); diff --git a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEShieldEngine.java b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEShieldEngine.java new file mode 100644 index 00000000000..5f2c6785e7c --- /dev/null +++ b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEShieldEngine.java @@ -0,0 +1,267 @@ +/* + * Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ +package com.codename1.impl.javase; + +import com.codename1.io.Log; +import com.codename1.security.shield.AppShield; +import com.codename1.security.shield.FailureMode; +import com.codename1.security.shield.HostPolicy; +import com.codename1.security.shield.PinSet; +import com.codename1.security.shield.ShieldConfig; +import com.codename1.security.shield.ShieldException; +import com.codename1.security.shield.ShieldSignal; +import com.codename1.security.shield.ShieldStatus; +import com.codename1.security.shield.ShieldToken; +import com.codename1.security.shield.spi.EngineContext; +import com.codename1.security.shield.spi.ShieldEngine; +import com.codename1.security.shield.spi.ShieldEngineRegistry; +import java.util.Hashtable; +import java.util.Vector; + +/** + * The engine behind {@code Simulate > App Shield}. + * + *

The toggles in that menu described outcomes nothing produced: a developer could + * switch on Force Pin Mismatch, watch the status dialog agree that pinning was + * "forcing mismatch", and see every request succeed -- because no engine was registered + * in the simulator, so the inert default answered every call and the fail-closed branch + * the switch exists to reach was unreachable. A switch that reports a state it does not + * cause is worse than no switch: it is a test that passes for the wrong reason.

+ * + *

Registered on demand, the first time a developer touches that menu, and never + * automatically -- a simulator that reported {@code isProtected()} true out of the box + * would change what every existing app does when run there. Tokens are stamped + * {@link #SIMULATED_MARKER} so nothing can mistake one for the real thing, and every + * one of them says so in its own value.

+ */ +public final class JavaSEShieldEngine implements ShieldEngine { + + /** + * Present in every simulated token. + * + *

These are minted locally with no attestation behind them at all, so a backend + * that ever sees one is talking to a simulator. It is in the token value rather than + * only in a header because the value is the part that gets copied into a curl command + * and pasted into a bug report.

+ */ + public static final String SIMULATED_MARKER = "cn1-simulated"; + + private ShieldConfig config; + private ShieldToken cached; + + /** + * Installs the simulator engine, once, if nothing has claimed the slot. + * + *

Called when a shield simulation is switched on -- including one restored from + * preferences at startup, because a developer who left "Force Pin Mismatch" armed + * expects it to still be armed. Not called merely because the menu exists: the + * registry seals on first registration and a simulator that always reported + * {@code isProtected()} true would change what every app does when run there.

+ * + *

The engine is initialized here as well as by + * {@code AppShield.init(ShieldConfig)}, because the two can happen in either order. + * An app that called {@code init()} at startup and a developer who opens the menu + * afterwards would otherwise leave this holding a null config -- and a null config + * means no pinned hosts, which means {@code verifyPins()} is never reached and the + * force switch quietly does nothing. That is the exact failure this class exists to + * remove, so it must not be reintroduced by ordering.

+ */ + public static void ensureRegistered() { + if (ShieldEngineRegistry.isEngineRegistered()) { + return; + } + JavaSEShieldEngine engine = new JavaSEShieldEngine(); + try { + ShieldEngineRegistry.setEngine(engine); + } catch (IllegalStateException alreadySealed) { + // A real engine got there first -- a build-server bootstrap, or a test. The + // simulator does not displace it. + return; + } catch (RuntimeException other) { + Log.e(other); + return; + } + try { + engine.initialize(ShieldEngineRegistry.getDefaultContext(), + AppShield.getConfig()); + } catch (RuntimeException e) { + Log.e(e); + } + } + + public String getName() { + return "simulator"; + } + + public boolean isAvailable() { + return JavaSEShield.attestationSupported; + } + + public void initialize(EngineContext ctx, ShieldConfig cfg) { + this.config = cfg; + } + + public ShieldToken fetchToken(String bindingData) throws ShieldException { + if (!JavaSEShield.attestationSupported) { + throw new ShieldException(ShieldStatus.UNPROTECTED, + "The simulated platform reports no attestation support"); + } + switch (JavaSEShield.attestOutcome) { + case FAIL_REJECTED: + throw new ShieldException(ShieldStatus.REJECTED, + "Simulated: the service rejected this device"); + case FAIL_NO_NETWORK: + throw new ShieldException(ShieldStatus.NO_NETWORK, + "Simulated: no network"); + case FAIL_SERVICE_DOWN: + throw new ShieldException(ShieldStatus.SERVICE_DOWN, + "Simulated: the attestation service is down"); + case FAIL_RATE_LIMITED: + throw new ShieldException(ShieldStatus.RATE_LIMITED, + "Simulated: rate limited"); + case UNSUPPORTED: + throw new ShieldException(ShieldStatus.UNPROTECTED, + "Simulated: this platform has no attestation"); + default: + break; + } + long ttl = (long) Math.max(1, JavaSEShield.tokenTtlSeconds) * 1000L; + long fetchedAt = System.currentTimeMillis(); + if (JavaSEShield.serveExpiredToken) { + // Handed out already lapsed rather than with a short lifetime, so a test does + // not have to wait for it. isValid() is answered from a monotonic reading + // taken at construction, so backdating fetchedAt alone would not do it. + ttl = 0L; + } + cached = new ShieldToken(SIMULATED_MARKER + "." + Long.toHexString(fetchedAt) + + (bindingData == null ? "" : "." + Integer.toHexString(bindingData.hashCode())), + ShieldStatus.OK, fetchedAt, ttl, bindingData); + return cached; + } + + public ShieldToken getCachedToken() { + return cached; + } + + public boolean verifyPins(String host, String[] spkiDigests, String[] certDigests) { + if (!JavaSEShield.forcePinMismatch) { + return true; + } + // One shot, because the switch is labelled "on next request". Leaving it armed + // would fail every subsequent request too, and a developer testing a recovery + // path would be testing a permanently broken app instead. + JavaSEShield.forcePinMismatch = false; + return false; + } + + public PinSet getPinSet() { + if (JavaSEShield.failPinFetch) { + // An unavailable pin set is not a mismatch. Pinning fails OPEN on + // unavailability everywhere in this design, and the simulator has to be able + // to demonstrate that rather than assert it. + return new PinSet(new Hashtable(), 0, 0L, 0L); + } + // Every host the app registered, pinned to a digest no real chain can produce. + // Enforcement is what makes verifyPins() run at all, so without this the force + // switch would still have nothing to act on: PinSet.isEnforcedFor() is false for + // a host with no pins, and ShieldNetworkGuard checks that before asking. + Hashtable hostToPins = new Hashtable(); + if (config != null) { + java.util.Enumeration hosts = config.protectedHosts(); + while (hosts.hasMoreElements()) { + String host = (String) hosts.nextElement(); + if (host == null || host.startsWith("*.")) { + // A wildcard is a policy pattern, not a host a chain is served for. + continue; + } + Vector pins = new Vector(); + pins.addElement(SIMULATED_PIN); + hostToPins.put(host, pins); + } + } + long now = System.currentTimeMillis(); + return new PinSet(hostToPins, 1, now + DAY_MILLIS, now + 30L * DAY_MILLIS); + } + + public ShieldSignal[] collectSignals() { + String[] reasons = JavaSEShield.simReasons(); + Vector out = new Vector(); + for (int i = 0; i < reasons.length; i++) { + out.addElement(new ShieldSignal(reasons[i], severityFor(reasons[i]), + "simulated")); + } + String[] accessibility = JavaSEShield.simAccessibility(); + for (int i = 0; i < accessibility.length; i++) { + out.addElement(new ShieldSignal(ShieldSignal.ACCESSIBILITY, 60, + accessibility[i])); + } + ShieldSignal[] arr = new ShieldSignal[out.size()]; + out.copyInto(arr); + return arr; + } + + public void invalidate() { + cached = null; + } + + public void shutdown() { + cached = null; + } + + private static int severityFor(String reason) { + if (ShieldSignal.HOOK.equals(reason) || "frida".equals(reason)) { + return 90; + } + if (ShieldSignal.ROOT.equals(reason) || ShieldSignal.JAILBREAK.equals(reason)) { + return 70; + } + if (ShieldSignal.REPACKAGED.equals(reason)) { + return 80; + } + if (ShieldSignal.DEBUGGER.equals(reason)) { + return 50; + } + return 30; + } + + private static final long DAY_MILLIS = 24L * 60L * 60L * 1000L; + + /** + * A digest no live chain can match, so an enforced host fails when the switch is on. + * + *

Deliberately not a real digest: if it ever collided with a host's actual key the + * force switch would silently stop working, which is the failure mode this whole + * class exists to remove.

+ */ + private static final String SIMULATED_PIN = + "c2ltdWxhdGVkLXBpbi1uby1yZWFsLWNoYWluLW1hdGNoZXMtdGhpcw=="; + + /** + * The policy a simulated host gets when the app registered none, so the menu's + * pinning switches have something to act on even in an app that only called + * {@code AppShield.init()}. + */ + static HostPolicy simulatedPolicy() { + return new HostPolicy(true, true, FailureMode.CLOSED); + } +} diff --git a/Ports/iOSPort/nativeSources/NetworkConnectionImpl.m b/Ports/iOSPort/nativeSources/NetworkConnectionImpl.m index 412a99070f4..58c3ffb6855 100644 --- a/Ports/iOSPort/nativeSources/NetworkConnectionImpl.m +++ b/Ports/iOSPort/nativeSources/NetworkConnectionImpl.m @@ -238,19 +238,23 @@ - (NSString*) getFingerprint: (SecCertificateRef) cert { */ static BOOL cn1ReadDerTlv(const uint8_t* buf, NSUInteger len, NSUInteger off, uint8_t* tag, NSUInteger* headerLen, NSUInteger* totalLen) { - if (off + 2 > len) { + // off is walked forward by the caller, so an off past the end must not be able to + // make `off + 2` wrap back into range before the comparison. + if (off > len || len - off < 2) { return NO; } *tag = buf[off]; NSUInteger n = buf[off + 1]; if (n < 0x80) { *headerLen = 2; + // n < 0x80 and len - off >= 2, so this cannot overflow; the range check below + // is what decides whether the TLV actually fits. *totalLen = 2 + n; } else { NSUInteger countBytes = n & 0x7f; // 0x80 is indefinite length, which DER forbids; more than 4 length bytes // would mean a certificate larger than anything we will ever be handed. - if (countBytes == 0 || countBytes > 4 || off + 2 + countBytes > len) { + if (countBytes == 0 || countBytes > 4 || len - off - 2 < countBytes) { return NO; } NSUInteger contentLen = 0; @@ -258,6 +262,13 @@ static BOOL cn1ReadDerTlv(const uint8_t* buf, NSUInteger len, NSUInteger off, contentLen = (contentLen << 8) | buf[off + 2 + i]; } *headerLen = 2 + countBytes; + // Checked against what is actually left rather than by forming the sum first. + // A crafted length wraps NSUInteger, `off + *totalLen <= len` then passes, and + // the caller hands the wrapped value to CC_SHA256, which reads past the buffer. + // The certificate comes off the wire, so it is attacker-shaped by definition. + if (contentLen > len - off - *headerLen) { + return NO; + } *totalLen = *headerLen + contentLen; } return off + *totalLen <= len; diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index dbd67bc6d33..ec17f083e9c 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -168,6 +168,20 @@ final class IOSDeviceIntegrity { /// Mirrors [#KEY_RECOVERY_SPENT] in memory, so a refused keychain write still bounds the /// one-shot recovery for the life of the process rather than letting it repeat per request. private boolean recoverySpentInMemory; + + /** + * The key whose attestation Apple has already answered, when the keychain refused to + * drop its start marker. + * + *

The marker means "submitted, outcome unknown", and clearing it is what stops the + * next request discarding a perfectly good key. A refused removal therefore puts the + * device straight back into burning one rate-limited hardware key per request -- the + * failure the clearing was added to prevent, reached through the storage layer + * instead. Held in memory so at least the life of this process is covered; a restart + * still reads the marker and discards the key once, which is the pre-existing + * behaviour and bounded.

+ */ + private String attestAnsweredForKey; /** True while a generate-then-attest bootstrap is running. */ private boolean bootstrapInFlight; /** @@ -229,6 +243,7 @@ private void resetLocked() { store.remove(KEY_ATTEST_STARTED); store.remove(KEY_RECOVERY_SPENT); recoverySpentInMemory = false; + attestAnsweredForKey = null; if (!idGone || !stateGone) { // The two deletions are not atomic, so a partial failure leaves half the // identity gone. Treating that as "untouched" would let a callback from the @@ -363,7 +378,8 @@ AsyncResource requestToken(String nonce) { } bootstrapInFlight = true; if (keyId != null && keyId.length() > 0 - && store.get(KEY_ATTEST_STARTED) == null) { + && (store.get(KEY_ATTEST_STARTED) == null + || keyId.equals(attestAnsweredForKey))) { // A key exists but attestation was never started for it -- the // previous attempt died between generating and submitting. Attest // that key rather than burning another. @@ -388,6 +404,8 @@ AsyncResource requestToken(String nonce) { store.remove(KEY_ID); store.remove(KEY_STATE); store.remove(KEY_ATTEST_STARTED); + // The discarded key is the one the in-memory marker named. + attestAnsweredForKey = null; PendingRequest fresh = new PendingRequest(r, nonce, PendingRequest.OP_GENERATE_KEY, null); int rid = register(fresh); @@ -551,6 +569,7 @@ public static void nativeKeyGenerated(final int requestId, final String keyId) { store.remove(KEY_ID); store.remove(KEY_STATE); store.remove(KEY_ATTEST_STARTED); + instance.attestAnsweredForKey = null; instance.bootstrapInFlight = false; fail(pending, "App Attest could not store its key identifier"); instance.failBootstrapWaiters( @@ -634,6 +653,8 @@ public static void nativeAttestationReady(final int requestId, final String atte // marker would refuse a future replacement when iOS legitimately invalidates // THIS key, and every later assertion would fail until the app reset by hand. store.remove(KEY_ATTEST_STARTED); + // Attested, so there is no interrupted attestation left to remember. + instance.attestAnsweredForKey = null; // The in-memory copy is cleared unconditionally, but the persisted marker is // only forgotten once the keychain confirms the deletion. A stale marker // would refuse the one-shot replacement the next time iOS legitimately @@ -781,9 +802,16 @@ public static void nativeAttestError(final int requestId, final int errorCode, // keeping the marker costs a fresh hardware key per request with // nothing bounding it. // - // A failed removal leaves the marker in place, which is the - // conservative state this branch used to reach anyway. - SecureStorage.getInstance().remove(KEY_ATTEST_STARTED); + // The removal is checked. If the keychain refuses it, the marker + // stays and the next request reads it as an unknown outcome -- back + // to discarding a reusable key and generating another, which is the + // whole failure this clearing exists to prevent. Remembering the + // answered key in memory covers the life of this process; a restart + // still reads the marker and discards once, which is where this + // branch started and is bounded. + if (!SecureStorage.getInstance().remove(KEY_ATTEST_STARTED)) { + instance.attestAnsweredForKey = pending.keyId; + } } if (pending.op != PendingRequest.OP_ASSERT) { // Still the same acquisition: releasing here and reacquiring would diff --git a/maven/javase/src/test/java/com/codename1/impl/javase/JavaSEShieldEngineTest.java b/maven/javase/src/test/java/com/codename1/impl/javase/JavaSEShieldEngineTest.java new file mode 100644 index 00000000000..273de3fee9e --- /dev/null +++ b/maven/javase/src/test/java/com/codename1/impl/javase/JavaSEShieldEngineTest.java @@ -0,0 +1,136 @@ +package com.codename1.impl.javase; + +import com.codename1.security.shield.PinSet; +import com.codename1.security.shield.ShieldConfig; +import com.codename1.security.shield.ShieldException; +import com.codename1.security.shield.ShieldToken; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * The simulator's shield switches have to change behaviour, not just state. + * + *

Every assertion here failed before {@link JavaSEShieldEngine} existed, for the same + * reason: the menu wrote a field in {@link JavaSEShield}, the status dialog read the + * field back, and nothing in the request path consulted either -- so "Force Pin Mismatch" + * left requests succeeding and the fail-closed branch it exists to reach was unreachable. + * A control that reports a state it does not cause is worse than no control.

+ */ +class JavaSEShieldEngineTest { + + private JavaSEShieldEngine engine; + + @BeforeEach + void setUp() { + JavaSEShield.reset(); + engine = new JavaSEShieldEngine(); + engine.initialize(null, new ShieldConfig() + .protect("api.example.com") + .protect("*.wild.example.com")); + } + + @AfterEach + void tearDown() { + JavaSEShield.reset(); + } + + @Test + void forcingAPinMismatchActuallyFailsTheCheck() { + assertTrue(engine.verifyPins("api.example.com", new String[] {"whatever"}, + new String[] {"whatever"}), "an unarmed simulator must not fail pins"); + + JavaSEShield.forcePinMismatch = true; + assertFalse(engine.verifyPins("api.example.com", new String[] {"whatever"}, + new String[] {"whatever"}), "the switch has to reach the pin check"); + } + + @Test + void theForcedMismatchIsOneShot() { + // The switch is labelled "on next request". Left armed, a developer testing what + // happens after a mismatch would be testing a permanently broken app instead. + JavaSEShield.forcePinMismatch = true; + assertFalse(engine.verifyPins("api.example.com", new String[0], new String[0])); + assertTrue(engine.verifyPins("api.example.com", new String[0], new String[0]), + "the second request should be back to normal"); + assertFalse(JavaSEShield.forcePinMismatch, "and the menu should show it disarmed"); + } + + @Test + void registeredHostsAreEnforcedSoTheMismatchHasSomethingToActOn() { + // Without pins for the host, ShieldNetworkGuard never asks verifyPins() at all -- + // PinSet.isEnforcedFor() is false for a host with no pins. So a pin set that + // covers the app's own hosts is load-bearing for the force switch, not decoration. + PinSet pins = engine.getPinSet(); + assertTrue(pins.isEnforcedFor("api.example.com"), + "a registered host must be enforced or the mismatch switch does nothing"); + assertFalse(pins.isEnforcedFor("unregistered.example.com"), + "a host the app never registered must be left alone"); + } + + @Test + void failingThePinFetchYieldsNoEnforcementRatherThanAMismatch() { + // Pinning fails OPEN on unavailability everywhere in this design; the simulator + // has to be able to demonstrate that rather than have it asserted in a doc. + JavaSEShield.failPinFetch = true; + PinSet pins = engine.getPinSet(); + assertFalse(pins.isEnforcedFor("api.example.com"), + "an unavailable pin set is not a mismatch"); + } + + @Test + void aSimulatedTokenIsMarkedAndHonoursTheConfiguredLifetime() throws Exception { + JavaSEShield.tokenTtlSeconds = 120; + ShieldToken token = engine.fetchToken(null); + + assertNotNull(token); + assertTrue(token.isValid(), "a fresh simulated token should be usable"); + assertTrue(token.getValue().contains(JavaSEShieldEngine.SIMULATED_MARKER), + "a backend that ever sees one of these is talking to a simulator, and the " + + "value is the part that ends up pasted into a bug report"); + assertTrue(token.getMillisUntilExpiry() > 0); + assertEquals(token, engine.getCachedToken()); + } + + @Test + void servingAnExpiredTokenProducesOneThatIsActuallyExpired() throws Exception { + // Backdating fetchedAt alone would not do it: validity is answered from a + // monotonic reading taken at construction, precisely so a device clock cannot + // make a lapsed token look fresh. + JavaSEShield.serveExpiredToken = true; + ShieldToken token = engine.fetchToken(null); + assertFalse(token.isValid(), "the switch has to produce a token that fails"); + assertEquals(0L, token.getMillisUntilExpiry()); + } + + @Test + void eachSimulatedFailureCarriesItsOwnStatus() { + // The distinction between "cannot reach the shield" and "the shield says no" is + // the most important thing in the API, so each outcome has to arrive as itself. + JavaSEShield.attestOutcome = JavaSEShield.AttestOutcome.FAIL_REJECTED; + assertEquals(com.codename1.security.shield.ShieldStatus.REJECTED, + assertThrows(ShieldException.class, () -> engine.fetchToken(null)).getStatus()); + + JavaSEShield.attestOutcome = JavaSEShield.AttestOutcome.FAIL_NO_NETWORK; + assertEquals(com.codename1.security.shield.ShieldStatus.NO_NETWORK, + assertThrows(ShieldException.class, () -> engine.fetchToken(null)).getStatus()); + + JavaSEShield.attestOutcome = JavaSEShield.AttestOutcome.FAIL_RATE_LIMITED; + assertEquals(com.codename1.security.shield.ShieldStatus.RATE_LIMITED, + assertThrows(ShieldException.class, () -> engine.fetchToken(null)).getStatus()); + } + + @Test + void simulatedDeviceSignalsReachTheEngine() { + JavaSEShield.simHooked = true; + JavaSEShield.simUntrustedAccessibility = true; + assertEquals(2, engine.collectSignals().length, + "the device-signal switches feed what the server is told"); + } +} From 10f8ffde155ef221f5f17661f63d15f07b490d42 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Thu, 30 Jul 2026 03:56:20 +0300 Subject: [PATCH 39/96] Give the new test its header, and stop a Maven Central reset failing setup check-copyright-headers was right: JavaSEShieldEngineTest had none. javase-simulator-tests died in Setup workspace on "Connection reset" fetching a build plugin from Maven Central, before any project code compiled. Maven treats that as a permanent resolution failure, so the job reported a broken build for a network blip. The two installs in setup-workspace.sh now go through a retry with a growing delay -- growing because a flat retry lands inside the same window a 429 is still rate limiting in. A genuine build failure fails identically on every attempt and still propagates, which I checked with a stub mvn for both the fails-then-succeeds and the always-fails paths. Co-Authored-By: Claude Opus 5 (1M context) --- .../impl/javase/JavaSEShieldEngineTest.java | 22 ++++++++++++++++ scripts/setup-workspace.sh | 25 +++++++++++++++++-- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/maven/javase/src/test/java/com/codename1/impl/javase/JavaSEShieldEngineTest.java b/maven/javase/src/test/java/com/codename1/impl/javase/JavaSEShieldEngineTest.java index 273de3fee9e..4f384e16a4f 100644 --- a/maven/javase/src/test/java/com/codename1/impl/javase/JavaSEShieldEngineTest.java +++ b/maven/javase/src/test/java/com/codename1/impl/javase/JavaSEShieldEngineTest.java @@ -1,3 +1,25 @@ +/* + * Copyright (c) 2026, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ package com.codename1.impl.javase; import com.codename1.security.shield.PinSet; diff --git a/scripts/setup-workspace.sh b/scripts/setup-workspace.sh index 1171f7dcc83..eedbdff6068 100755 --- a/scripts/setup-workspace.sh +++ b/scripts/setup-workspace.sh @@ -232,11 +232,32 @@ fi # longer costs nothing when there is no contention. MVN_LOCK_ARGS="-Daether.syncContext.named.time=300 -Daether.syncContext.named.timeUnit=SECONDS" +# Maven Central resets the connection or throttles a runner often enough to matter, +# and Maven treats that as a PERMANENT resolution failure -- observed killing this +# script while fetching a build plugin, before any project code compiled. The delay +# grows because a flat retry lands inside the same window a 429 is still rate +# limiting in. A real build failure fails identically every attempt, so this costs +# one extra run of a broken build and rescues a green one. +mvn_retry() { + local delay + for delay in 30 120 300 0; do + if "$MAVEN_HOME/bin/mvn" "$@"; then + return 0 + fi + if [ "$delay" = "0" ]; then + log "maven failed after all retries" + return 1 + fi + log "maven failed; retrying in ${delay}s in case Maven Central was flaky" + sleep "$delay" + done +} + log "Building Codename One core modules" -"$MAVEN_HOME/bin/mvn" -f maven/pom.xml $MVN_LOCK_ARGS -T 1C -Dmaven.javadoc.skip=true -Dmaven.source.skip=true -DskipTests -Djava.awt.headless=true -Dcn1.binaries="$CN1_BINARIES" -Dcodename1.platform=javase -P local-dev-javase,compile-android,!download-cn1-binaries install "$@" +mvn_retry -f maven/pom.xml $MVN_LOCK_ARGS -T 1C -Dmaven.javadoc.skip=true -Dmaven.source.skip=true -DskipTests -Djava.awt.headless=true -Dcn1.binaries="$CN1_BINARIES" -Dcodename1.platform=javase -P local-dev-javase,compile-android,!download-cn1-binaries install "$@" log "Building Codename One Maven plugin" -"$MAVEN_HOME/bin/mvn" -f maven/pom.xml \ +mvn_retry -f maven/pom.xml \ -pl codenameone-maven-plugin -am \ $MVN_LOCK_ARGS -T 1C -Dmaven.javadoc.skip=true -Dmaven.source.skip=true \ -DskipTests -Djava.awt.headless=true \ From 2551b156c8a8ad41549f50a672daad368162b9bf Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Thu, 30 Jul 2026 04:16:18 +0300 Subject: [PATCH 40/96] Keep an exhausted invalid-key attestation terminal The answered-failure cleanup cleared the start marker for every reported error. Reaching it with invalidKey means the reset branch above declined, so the one-shot recovery is already spent and this key is both known bad and known unreplaceable -- and clearing the marker made the next request read it as a reusable key whose attestation had never started, and submit it to Apple again. Once per request, against a rate limit, instead of surfacing the exhausted recovery the caller needs to act on. invalidKey is now excluded from that cleanup, and from the in-memory answered marker with it. Co-Authored-By: Claude Opus 5 (1M context) --- .../com/codename1/impl/ios/IOSDeviceIntegrity.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index ec17f083e9c..0f0e3481f91 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -783,7 +783,16 @@ public static void nativeAttestError(final int requestId, final int errorCode, Long.toString(deadline)); instance.currentBackoff = Math.min(backoff * 2, MAX_BACKOFF_MILLIS); } - if (pending.op == PendingRequest.OP_ATTEST) { + if (pending.op == PendingRequest.OP_ATTEST + && errorCode != DC_ERROR_INVALID_KEY) { + // invalidKey is excluded. Reaching here with it means the reset + // branch above declined -- the one-shot recovery is already spent -- + // so this key is known bad and known unreplaceable. Clearing the + // marker would make the next request read it as a reusable key with + // an unstarted attestation and submit it to Apple again, once per + // request, instead of reporting the exhausted recovery the caller + // needs to see. Terminal has to stay terminal. + // // The marker means "submitted, outcome unknown", and the // interrupted-attestation branch answers an unknown outcome by // discarding the key, because a spent one-time attestation cannot be From 52a2ddfda753e68735104efc9796b131a1c51664 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Thu, 30 Jul 2026 04:36:25 +0300 Subject: [PATCH 41/96] Keep the terminal markers until the key is confirmed gone, and pin wildcards resetLocked cleared the attestation-start and recovery-spent markers before it checked whether the identity deletions had succeeded. A keychain that deleted KEY_STATE and refused KEY_ID therefore left a known-invalid key with no state and no start marker -- which the next request reads as a freshly generated key whose attestation never began, and submits to Apple. Every request after it does the same, against a rate limit, with the one-shot recovery marker gone too so nothing stops the loop. Those markers are now cleared only once both identity deletions are confirmed. The simulated pin set dropped wildcard hosts, so an app registering "*.example.com" -- which is the common way to do it -- had nothing enforced and Force Pin Mismatch still did nothing. PinSet.isEnforcedFor already resolves a concrete host against a "*." entry, so the exclusion was mine and wrong. Co-Authored-By: Claude Opus 5 (1M context) --- .../impl/javase/JavaSEShieldEngine.java | 8 ++++++-- .../codename1/impl/ios/IOSDeviceIntegrity.java | 17 +++++++++++++---- .../impl/javase/JavaSEShieldEngineTest.java | 8 ++++++++ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEShieldEngine.java b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEShieldEngine.java index 5f2c6785e7c..9ce98b22162 100644 --- a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEShieldEngine.java +++ b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEShieldEngine.java @@ -190,10 +190,14 @@ public PinSet getPinSet() { java.util.Enumeration hosts = config.protectedHosts(); while (hosts.hasMoreElements()) { String host = (String) hosts.nextElement(); - if (host == null || host.startsWith("*.")) { - // A wildcard is a policy pattern, not a host a chain is served for. + if (host == null) { continue; } + // Wildcards included. PinSet.isEnforcedFor() resolves "api.example.com" + // against a "*.example.com" entry, so skipping them left every app that + // registers its hosts by pattern -- which is the common way to do it -- + // with nothing enforced and the force switch doing nothing, which is the + // whole failure this engine exists to remove. Vector pins = new Vector(); pins.addElement(SIMULATED_PIN); hostToPins.put(host, pins); diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index 0f0e3481f91..aed86b60d49 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -240,10 +240,19 @@ private void resetLocked() { boolean stateGone = store.remove(KEY_STATE); store.remove(KEY_RETRY_AFTER); store.remove(KEY_PENDING_SINCE); - store.remove(KEY_ATTEST_STARTED); - store.remove(KEY_RECOVERY_SPENT); - recoverySpentInMemory = false; - attestAnsweredForKey = null; + // The markers that make a surviving key terminal are cleared only once the key + // itself is confirmed gone. Clearing them first meant a keychain that deleted + // KEY_STATE and refused KEY_ID left a known-invalid key behind with no state and + // no start marker -- which the next request reads as a freshly generated key + // whose attestation never began, and submits to Apple. Every request after it + // does the same, against a rate limit, with the one-shot recovery marker also + // gone so nothing stops the loop. + if (idGone && stateGone) { + store.remove(KEY_ATTEST_STARTED); + store.remove(KEY_RECOVERY_SPENT); + recoverySpentInMemory = false; + attestAnsweredForKey = null; + } if (!idGone || !stateGone) { // The two deletions are not atomic, so a partial failure leaves half the // identity gone. Treating that as "untouched" would let a callback from the diff --git a/maven/javase/src/test/java/com/codename1/impl/javase/JavaSEShieldEngineTest.java b/maven/javase/src/test/java/com/codename1/impl/javase/JavaSEShieldEngineTest.java index 4f384e16a4f..a37abdc7f52 100644 --- a/maven/javase/src/test/java/com/codename1/impl/javase/JavaSEShieldEngineTest.java +++ b/maven/javase/src/test/java/com/codename1/impl/javase/JavaSEShieldEngineTest.java @@ -94,6 +94,14 @@ void registeredHostsAreEnforcedSoTheMismatchHasSomethingToActOn() { "a registered host must be enforced or the mismatch switch does nothing"); assertFalse(pins.isEnforcedFor("unregistered.example.com"), "a host the app never registered must be left alone"); + // Wildcards too. Registering by pattern is the common way to do it, and + // PinSet.isEnforcedFor() resolves a concrete host against a "*." entry -- so + // dropping them left those apps with nothing enforced and the force switch + // doing nothing. + assertTrue(pins.isEnforcedFor("api.wild.example.com"), + "a host covered by a registered wildcard must be enforced too"); + assertFalse(pins.isEnforcedFor("wild.example.com"), + "but the apex is not covered by its own wildcard"); } @Test From c4677c76b1f235e4f00d7682b7c1ae5431c39532 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Thu, 30 Jul 2026 04:52:30 +0300 Subject: [PATCH 42/96] Arm the simulator engine from the outcome menu, and disarm the spent one-shot Selecting an attestation result -- or restoring a non-default one at startup -- only set a field. With no engine registered, AppShield still asked the inert default, so a developer who chose FAIL_REJECTED got UNPROTECTED: the same control-that-does-nothing the engine was added to remove, left in the one part of the menu the engine did not cover. The outcome radio group and the attestation-supported checkbox now register it too. The forced mismatch cleared the engine's flag and left the checkbox and the stored preference true, so the menu claimed a mismatch was still armed, the next click disarmed instead of arming, and the next launch restored one that had already fired. The engine now spends it through JavaSEShield, which notifies the port to clear both. Co-Authored-By: Claude Opus 5 (1M context) --- .../com/codename1/impl/javase/JavaSEPort.java | 39 +++++++++++++++++-- .../codename1/impl/javase/JavaSEShield.java | 22 +++++++++++ .../impl/javase/JavaSEShieldEngine.java | 7 +++- .../impl/javase/JavaSEShieldEngineTest.java | 21 ++++++++++ 4 files changed, 84 insertions(+), 5 deletions(-) diff --git a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEPort.java b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEPort.java index 344c8782dd7..e867994ca12 100644 --- a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEPort.java +++ b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEPort.java @@ -8340,6 +8340,10 @@ private JMenu installShieldSimulationMenu(JMenu simulateMenu, final Preferences public void actionPerformed(ActionEvent ae) { JavaSEShield.attestationSupported = supported.isSelected(); pref.putBoolean("ShieldSim.supported", supported.isSelected()); + // Unchecking this is a simulation like any other -- "this platform has no + // attestation" is an outcome an app has to handle -- so it arms the + // engine as well. + JavaSEShieldEngine.ensureRegistered(); } }); shieldMenu.add(supported); @@ -8353,6 +8357,13 @@ public void actionPerformed(ActionEvent ae) { if (outcome.name().equals(storedOutcome)) { item.setSelected(true); JavaSEShield.attestOutcome = outcome; + // Restored non-default state arms the engine too, for the same reason a + // restored checkbox does: a developer who left FAIL_REJECTED selected + // expects the next run to fail, and without an engine registered + // AppShield asks the inert default and reports UNPROTECTED instead. + if (outcome != JavaSEShield.AttestOutcome.PASS) { + JavaSEShieldEngine.ensureRegistered(); + } } outcomeGroup.add(item); item.addActionListener(new ActionListener() { @@ -8360,6 +8371,10 @@ public void actionPerformed(ActionEvent ae) { public void actionPerformed(ActionEvent ae) { JavaSEShield.attestOutcome = outcome; pref.put("ShieldSim.outcome", outcome.name()); + // Selecting any outcome registers the engine, PASS included: PASS is + // "hand out a simulated token", which the inert default does not do + // either. Only the untouched menu leaves the simulator alone. + JavaSEShieldEngine.ensureRegistered(); } }); outcomeMenu.add(item); @@ -8425,13 +8440,31 @@ public void set(boolean v) { })); // The one branch that is otherwise effectively untestable. - shieldMenu.add(shieldToggle(pref, "Force Pin Mismatch On Next Request", - "ShieldSim.pinMismatch", new ShieldToggleSink() { + final JCheckBoxMenuItem pinMismatch = shieldToggle(pref, + "Force Pin Mismatch On Next Request", "ShieldSim.pinMismatch", + new ShieldToggleSink() { @Override public void set(boolean v) { JavaSEShield.forcePinMismatch = v; } - })); + }); + // It is a one-shot, so when the engine spends it the menu and the stored + // preference have to follow. Otherwise the checkbox says a mismatch is armed + // when it is not, the next click disarms instead of arming, and the next launch + // restores a mismatch that already fired. + JavaSEShield.onForcePinMismatchConsumed = new Runnable() { + @Override + public void run() { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + pinMismatch.setSelected(false); + pref.putBoolean("ShieldSim.pinMismatch", false); + } + }); + } + }; + shieldMenu.add(pinMismatch); shieldMenu.add(shieldToggle(pref, "Fail Pin Fetch", "ShieldSim.pinFetchFail", new ShieldToggleSink() { @Override diff --git a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEShield.java b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEShield.java index dc384fc8780..5b7bfc0e3a1 100644 --- a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEShield.java +++ b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEShield.java @@ -98,6 +98,27 @@ public enum AttestOutcome { /** Simulate being unable to fetch a pin set. Must never fail a request. */ public static boolean failPinFetch; + /** + * Notified when the one-shot forced mismatch is spent, so the menu can disarm too. + * + *

Without it the engine cleared its own flag and the checkbox and the stored + * preference stayed true: the menu claimed a mismatch was still armed, the next click + * disarmed it instead of scheduling another, and restarting the simulator re-armed + * one that had already fired. A control that reports a state it does not cause is + * the failure this whole engine exists to remove, so it must not reappear in the + * engine's own bookkeeping.

+ */ + public static Runnable onForcePinMismatchConsumed; + + /** Clears the forced mismatch and lets the menu know, so the two cannot disagree. */ + static void consumeForcePinMismatch() { + forcePinMismatch = false; + Runnable r = onForcePinMismatchConsumed; + if (r != null) { + r.run(); + } + } + /** True when the window is displaying a screen marked secure. */ public static boolean secureScreen; @@ -145,6 +166,7 @@ public static void reset() { forcePinMismatch = false; failPinFetch = false; secureScreen = false; + onForcePinMismatchConsumed = null; } /** A human-readable dump for the menu's status dialog. */ diff --git a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEShieldEngine.java b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEShieldEngine.java index 9ce98b22162..9aa755491de 100644 --- a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEShieldEngine.java +++ b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEShieldEngine.java @@ -169,8 +169,11 @@ public boolean verifyPins(String host, String[] spkiDigests, String[] certDigest } // One shot, because the switch is labelled "on next request". Leaving it armed // would fail every subsequent request too, and a developer testing a recovery - // path would be testing a permanently broken app instead. - JavaSEShield.forcePinMismatch = false; + // path would be testing a permanently broken app instead. Through + // consumeForcePinMismatch so the menu and the stored preference disarm with the + // flag -- clearing the field alone left the checkbox claiming it was still armed + // and re-armed it on the next launch. + JavaSEShield.consumeForcePinMismatch(); return false; } diff --git a/maven/javase/src/test/java/com/codename1/impl/javase/JavaSEShieldEngineTest.java b/maven/javase/src/test/java/com/codename1/impl/javase/JavaSEShieldEngineTest.java index a37abdc7f52..44cb36135b2 100644 --- a/maven/javase/src/test/java/com/codename1/impl/javase/JavaSEShieldEngineTest.java +++ b/maven/javase/src/test/java/com/codename1/impl/javase/JavaSEShieldEngineTest.java @@ -84,6 +84,27 @@ void theForcedMismatchIsOneShot() { assertFalse(JavaSEShield.forcePinMismatch, "and the menu should show it disarmed"); } + @Test + void spendingTheForcedMismatchTellsTheMenuToDisarm() { + // The engine clearing its own field is not enough: the checkbox and the stored + // preference live in the port, so without a notification the menu claims a + // mismatch is still armed, the next click disarms instead of arming, and the + // next launch restores one that already fired. + final int[] notified = {0}; + JavaSEShield.onForcePinMismatchConsumed = new Runnable() { + public void run() { + notified[0]++; + } + }; + JavaSEShield.forcePinMismatch = true; + + engine.verifyPins("api.example.com", new String[0], new String[0]); + assertEquals(1, notified[0], "the menu has to be told the one-shot was spent"); + + engine.verifyPins("api.example.com", new String[0], new String[0]); + assertEquals(1, notified[0], "and only when there was something to spend"); + } + @Test void registeredHostsAreEnforcedSoTheMismatchHasSomethingToActOn() { // Without pins for the host, ShieldNetworkGuard never asks verifyPins() at all -- From a34ee58f709414a1153e8b12341274d7f5bdf1a1 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Thu, 30 Jul 2026 05:14:16 +0300 Subject: [PATCH 43/96] Refuse to attest from a half-deleted identity, and two more simulator gaps A partial discard leaves an identity with no markers to make it terminal -- a successfully attested key deliberately has neither a start marker nor a spent one -- so the next request read the retained key as never submitted and attested it again, spending a rate-limited attempt on a key Apple had already attested and the backend had already rejected, once per request. Nothing persisted can say "this key is finished" while the keychain is refusing writes, so the state is held in memory and attestation refuses with an explanation until a reset succeeds. The recovery-spent write result is no longer ignored. The in-memory copy covers this process, but the replacement key's own start marker persists -- so after a restart the next launch found a key with no record that a recovery had been spent and started another one, a rate-limited key per launch caused by the write meant to bound it. Without the marker there is no replacement. A restored "Attestation Supported = false" armed the flag and no engine, since the action listener does not fire during construction -- so a fail-closed host was let through instead of exercising the unsupported-device rejection the setting exists to simulate. Co-Authored-By: Claude Opus 5 (1M context) --- .../com/codename1/impl/javase/JavaSEPort.java | 8 +++ .../impl/ios/IOSDeviceIntegrity.java | 51 ++++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEPort.java b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEPort.java index e867994ca12..865c8cc3781 100644 --- a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEPort.java +++ b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEPort.java @@ -8335,6 +8335,14 @@ private JMenu installShieldSimulationMenu(JMenu simulateMenu, final Preferences final JCheckBoxMenuItem supported = new JCheckBoxMenuItem("Attestation Supported", pref.getBoolean("ShieldSim.supported", true)); JavaSEShield.attestationSupported = supported.isSelected(); + // A restored FALSE is a simulation left switched on, exactly like a restored + // checkbox elsewhere in this menu -- the action listener does not fire during + // construction, so without this the flag is set and no engine answers, and a + // fail-closed host is let through instead of exercising the unsupported-device + // rejection the setting exists to simulate. True is the default and arms nothing. + if (!supported.isSelected()) { + JavaSEShieldEngine.ensureRegistered(); + } supported.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index aed86b60d49..b904f57c7e7 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -182,6 +182,18 @@ final class IOSDeviceIntegrity { * behaviour and bounded.

*/ private String attestAnsweredForKey; + + /** + * Set when the keychain refused to delete part of a discarded identity. + * + *

Attestation refuses outright while it is set. The alternative is worse than a + * failing app: half an identity with no markers reads as a fresh key whose + * attestation never began, so every request would spend a rate-limited attempt on a + * key that is already attested and already rejected. Cleared by a reset that + * succeeds, and not persisted -- there is nowhere to persist it, since the failure + * being recorded is that persistence is not working.

+ */ + private boolean discardFailed; /** True while a generate-then-attest bootstrap is running. */ private boolean bootstrapInFlight; /** @@ -254,6 +266,15 @@ private void resetLocked() { attestAnsweredForKey = null; } if (!idGone || !stateGone) { + // The identity survives in part, and there may be nothing left to make it + // terminal: a successfully attested key has no start marker and no spent + // marker, by design, so the next request would read the retained key as + // never submitted and attest it again -- a rate-limited attempt on a key + // Apple has already attested, repeated per request. Nothing persisted can + // express "this key is finished" once the keychain is refusing writes, so + // the state is held in memory and requestToken refuses until a reset + // succeeds or the process restarts. + discardFailed = true; // The two deletions are not atomic, so a partial failure leaves half the // identity gone. Treating that as "untouched" would let a callback from the // rejected identity still pass its staleness check and act on inconsistent @@ -337,6 +358,18 @@ AsyncResource requestToken(String nonce) { return r; } synchronized (flowLock) { + if (discardFailed) { + // A previous discard left half an identity behind because the keychain + // refused a deletion. Attesting from here spends a rate-limited attempt + // on a key that is already attested and already rejected, once per + // request, so the app is told plainly instead. resetAttestation() clears + // it if the keychain recovers. + r.error(new RuntimeException("App Attest could not discard a rejected key " + + "and cannot safely generate another; call " + + "DeviceIntegrity.resetAttestation() once the device's keychain " + + "is writable again")); + return r; + } long retryAfter = Math.max(readRetryAfter(), retryAfterFallback); if (retryAfter > System.currentTimeMillis()) { r.error(new RuntimeException("App Attest is backing off after a throttle; " @@ -768,7 +801,23 @@ public static void nativeAttestError(final int requestId, final int errorCode, // one-shot limit still holds for the life of the process rather than // letting every later request burn another key. instance.recoverySpentInMemory = true; - SecureStorage.getInstance().set(KEY_RECOVERY_SPENT, "1"); + if (!SecureStorage.getInstance().set(KEY_RECOVERY_SPENT, "1")) { + // No replacement without the marker. The in-memory copy only + // covers this process, and the replacement's own start marker + // DOES persist -- so after a restart the next launch would find a + // discarded-looking key with no record that a recovery had been + // spent, and start another one. That is a rate-limited hardware + // key per launch, caused by the very write that was meant to + // bound it. Failing here costs this caller one request, and the + // device is left with no key at all, so a later attempt starts + // cleanly rather than compounding. + instance.bootstrapInFlight = false; + fail(pending, "App Attest could not record that its one-time " + + "recovery had been used, so it was not attempted"); + instance.failBootstrapWaiters("App Attest could not record that " + + "its one-time recovery had been used"); + return; + } PendingRequest retry = new PendingRequest(pending.result, pending.nonce, PendingRequest.OP_GENERATE_KEY, null); retry.retried = true; From 9b164ac2cbb7082514953c43a06d20ab2ebd7420 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Thu, 30 Jul 2026 05:41:38 +0300 Subject: [PATCH 44/96] Reopen the real loopback transport, and refuse Content-Type as the token header MCPLoopbackSocketTransport.close() sets its closed flag permanently and open() never cleared it, so a stop()/start() over one instance -- which the server supports, and which is how a caller holding a single transport restarts -- opened successfully and then had its first readMessage() return null immediately, shutting the restarted server straight back down. My restart tests used a double, which agreed with whatever the double was written to do; there is now one against the production class. ConnectionRequest.addRequestHeader special-cases Content-Type into the request's own content type rather than the header map, so configuring it as the token header would replace the media type AND survive removeRequestHeader -- meaning the token follows a redirect off a protected host and is handed to whatever is there. Refused, case-insensitively. Co-Authored-By: Claude Opus 5 (1M context) --- .../mcp/MCPLoopbackSocketTransport.java | 18 ++++++++++++ .../security/shield/ShieldConfig.java | 15 ++++++++++ .../mcp/MCPLoopbackTransportOpenTest.java | 29 +++++++++++++++++++ .../security/shield/ShieldApiTest.java | 19 ++++++++++++ 4 files changed, 81 insertions(+) diff --git a/CodenameOne/src/com/codename1/mcp/MCPLoopbackSocketTransport.java b/CodenameOne/src/com/codename1/mcp/MCPLoopbackSocketTransport.java index 5e715ae6fea..2616f77cb69 100644 --- a/CodenameOne/src/com/codename1/mcp/MCPLoopbackSocketTransport.java +++ b/CodenameOne/src/com/codename1/mcp/MCPLoopbackSocketTransport.java @@ -118,6 +118,16 @@ public void open() throws IOException { } active = this; } + // Reopening the same instance has to start a session, not resume a closed one. + // close() sets this permanently and nothing cleared it, so a stop()/start() pair + // over one transport -- which the server explicitly supports, and which is how a + // caller that keeps a single transport around restarts -- opened successfully and + // then had its first readMessage() return null immediately, shutting the restarted + // server straight back down. Cleared here rather than in close(), because a + // transport that has been closed and not reopened must keep refusing reads. + synchronized (lock) { + closed = false; + } try { listening = Socket.listenLoopback(port, Connection.class); } catch (RuntimeException ex) { @@ -351,6 +361,14 @@ private boolean isClosed() { } } + /// Test seam: has this transport been closed and not reopened? Package-private + /// because the reopen semantics are otherwise only observable through a real socket. + boolean isClosedForTest() { + synchronized (lock) { + return closed; + } + } + @Override public void close() { Socket.StopListening l; diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java b/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java index e23edcc5918..11a28cbd48f 100644 --- a/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java +++ b/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java @@ -68,8 +68,23 @@ public ShieldConfig endpoint(String url) { /// Overrides the header used to carry the token. Change this only if it collides with /// something already in use on your backend. + /// + /// `Content-Type` is refused. [com.codename1.io.ConnectionRequest#addRequestHeader] + /// special-cases that name into the request's own content type rather than the header + /// map, so the token would replace the request's media type and -- because the removal + /// path only clears the map -- would survive a redirect to an unprotected host and be + /// handed to it. A leak with no symptom on the way there. + /// + /// @throws IllegalArgumentException if the name cannot carry a token safely public ShieldConfig tokenHeader(String name) { if (name != null && name.length() > 0) { + if ("content-type".equals(ShieldHosts.normalize(name))) { + throw new IllegalArgumentException("Content-Type cannot carry the " + + "attestation token: it is not stored as an ordinary header, so it " + + "cannot be cleared when a request redirects off a protected host, " + + "and the token would follow the redirect. Use a header of your " + + "own, or leave the default " + DEFAULT_TOKEN_HEADER + "."); + } this.tokenHeader = name; } return this; diff --git a/maven/core-unittests/src/test/java/com/codename1/mcp/MCPLoopbackTransportOpenTest.java b/maven/core-unittests/src/test/java/com/codename1/mcp/MCPLoopbackTransportOpenTest.java index d27fbdc20e0..1f52ad1c3e3 100644 --- a/maven/core-unittests/src/test/java/com/codename1/mcp/MCPLoopbackTransportOpenTest.java +++ b/maven/core-unittests/src/test/java/com/codename1/mcp/MCPLoopbackTransportOpenTest.java @@ -32,6 +32,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -248,6 +249,34 @@ private static int liveReaderThreads() { return n; } + @Test + void theProductionLoopbackTransportCanBeReopened() throws Exception { + // The restart tests above use a double, which cannot see this: the real + // transport's close() sets its closed flag permanently, and open() never cleared + // it -- so a stop()/start() over one instance opened fine and then had its first + // readMessage() return null immediately, shutting the restarted server straight + // back down. Asserted against the production class rather than a stand-in, + // because a double will agree with whatever the double was written to do. + MCPLoopbackSocketTransport transport = new MCPLoopbackSocketTransport(0); + transport.close(); + // Reading a closed transport ends the session -- that part is correct. + assertNull(transport.readMessage(), "a closed transport must stop serving"); + try { + transport.open(); + } catch (IOException platformCannotBind) { + // No loopback server socket in this test JVM. The reopen semantics are still + // what this test is about, and they are visible without a real socket. + transport.close(); + return; + } + try { + assertFalse(transport.isClosedForTest(), + "reopening has to start a session, not resume the closed one"); + } finally { + transport.close(); + } + } + @Test void aSupersededReaderMustNotCloseATransportTheRestartIsNowServing() throws Exception { // Restarting over the SAME transport instance. Declining to clear `running` is only diff --git a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java index d3042fffe9d..6b88ebb37dc 100644 --- a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java +++ b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java @@ -33,6 +33,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; /** @@ -391,6 +392,24 @@ void severityIsClampedToTheDocumentedRange() { assertEquals(0, new ShieldSignal("x", -5, null).getSeverity()); } + @Test + void contentTypeIsRefusedAsTheTokenHeader() { + // ConnectionRequest.addRequestHeader special-cases Content-Type into the + // request's own content type rather than the header map, so the token would + // replace the media type AND survive removeRequestHeader -- which means it + // follows a redirect off a protected host and is handed to whatever is there. + assertThrows(IllegalArgumentException.class, + () -> new ShieldConfig().tokenHeader("Content-Type")); + assertThrows(IllegalArgumentException.class, + () -> new ShieldConfig().tokenHeader("content-TYPE"), + "the check has to be case-insensitive, like the header itself"); + + // Everything else still works, including the default. + assertEquals("X-Mine", new ShieldConfig().tokenHeader("X-Mine").getTokenHeader()); + assertEquals(ShieldConfig.DEFAULT_TOKEN_HEADER, + new ShieldConfig().getTokenHeader()); + } + // --- guard composition ---------------------------------------------- @Test From 84640f4222d2a197553eac6e846cd9f4fff97773 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Fri, 31 Jul 2026 15:14:04 +0300 Subject: [PATCH 45/96] Serialize transport reopen, and record a spent recovery before discarding the key Two generations could be inside one transport's open() at the same time: a stop()/start() over the same instance while the old reader was still parked in open(). The same-transport rule then correctly declines to close it on the way out, so the instance was left holding two listeners with one handle -- the first leaked and outlived stop(). Opening is now serialized per server on a lock that is deliberately not the server monitor (open() blocks, and holding the monitor across it would make stop() wait on what it is stopping), and the loopback transport refuses a second open while one is live, since that handle is the only reference to the listener. The recovery-spent marker is written BEFORE the identity is discarded. Discarding first and then failing to record left no key at all AND no record that a recovery had happened, so the next request took the plain generate-key path -- which consults neither marker -- and burned a fresh rate-limited hardware key on every request, and on every launch once the in-memory copy was gone. Writing first means a refused write costs one request and changes nothing else. resetLocked gains a keepSpentMarker flag so the reset that IS the recovery does not erase the marker it was just told to honour. Co-Authored-By: Claude Opus 5 (1M context) --- .../mcp/MCPLoopbackSocketTransport.java | 10 +++ .../src/com/codename1/mcp/MCPServer.java | 21 +++++- .../impl/ios/IOSDeviceIntegrity.java | 64 +++++++++++-------- 3 files changed, 68 insertions(+), 27 deletions(-) diff --git a/CodenameOne/src/com/codename1/mcp/MCPLoopbackSocketTransport.java b/CodenameOne/src/com/codename1/mcp/MCPLoopbackSocketTransport.java index 2616f77cb69..c0e6e22fd15 100644 --- a/CodenameOne/src/com/codename1/mcp/MCPLoopbackSocketTransport.java +++ b/CodenameOne/src/com/codename1/mcp/MCPLoopbackSocketTransport.java @@ -128,6 +128,16 @@ public void open() throws IOException { synchronized (lock) { closed = false; } + // One listener per instance. The server serializes opens, but this handle is the + // only reference to the listener -- overwriting it strands the previous one, and + // a stranded listener survives close() and keeps the port bound. Refusing is the + // honest answer: the caller asked to open something that is already open. + synchronized (lock) { + if (listening != null) { + throw new IOException("This MCP transport is already listening on port " + + port); + } + } try { listening = Socket.listenLoopback(port, Connection.class); } catch (RuntimeException ex) { diff --git a/CodenameOne/src/com/codename1/mcp/MCPServer.java b/CodenameOne/src/com/codename1/mcp/MCPServer.java index 5250eef5868..467d6aa7720 100644 --- a/CodenameOne/src/com/codename1/mcp/MCPServer.java +++ b/CodenameOne/src/com/codename1/mcp/MCPServer.java @@ -218,6 +218,12 @@ private synchronized void releaseAndCloseIfCurrent(MCPTransport t, int generatio } } + /// Held across `open()` so two generations cannot be inside one transport's open at + /// the same time. Deliberately NOT the server monitor: `open()` blocks -- it binds a + /// listener -- and holding the monitor across it would make `stop()` wait on the + /// thing it is trying to stop. + private final Object openLock = new Object(); + private void runLoop(MCPTransport t, int generation) { // Opening is deferred to this thread, so by the time it happens the server may // already have been stopped or restarted. Either way stop()'s close() ran against a @@ -229,7 +235,20 @@ private void runLoop(MCPTransport t, int generation) { return; } try { - t.open(); + // Serialized per server. A stop()/start() over the SAME transport while the + // old reader is still parked inside open() would otherwise have both + // generations open one instance: the same-transport rule then correctly + // declines to close it on the way out, and the transport is left holding two + // listeners with one handle for them -- so the first is leaked and outlives + // stop(). The superseded thread finds itself stale the moment it gets the + // lock, and unwinds without opening anything. + synchronized (openLock) { + if (!isCurrent(t, generation)) { + releaseAndCloseIfCurrent(t, generation); + return; + } + t.open(); + } } catch (IOException ex) { // The transport failed to start listening. Log defensively: the CN1 Log // routes through the platform implementation, which may not be registered diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index b904f57c7e7..f79c695c38f 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -242,6 +242,14 @@ void resetAttestation() { /// The reset itself, for callers that must not release the lock between discarding /// the old identity and starting the replacement. Caller holds `flowLock`. private void resetLocked() { + resetLocked(false); + } + + /// @param keepSpentMarker true when this reset is the discard step of a recovery + /// that has already recorded itself as spent. Clearing the marker there would + /// undo the one-shot limit at the exact moment it starts applying -- the + /// replacement key would look like a first recovery all over again. + private void resetLocked(boolean keepSpentMarker) { SecureStorage store = SecureStorage.getInstance(); // The two that carry the identity are checked. If the keychain refuses to // delete them the reset has not happened, and advancing the generation anyway @@ -261,9 +269,12 @@ private void resetLocked() { // gone so nothing stops the loop. if (idGone && stateGone) { store.remove(KEY_ATTEST_STARTED); - store.remove(KEY_RECOVERY_SPENT); - recoverySpentInMemory = false; attestAnsweredForKey = null; + discardFailed = false; + if (!keepSpentMarker) { + store.remove(KEY_RECOVERY_SPENT); + recoverySpentInMemory = false; + } } if (!idGone || !stateGone) { // The identity survives in part, and there may be nothing left to make it @@ -784,8 +795,32 @@ public static void nativeAttestError(final int requestId, final int errorCode, if (errorCode == DC_ERROR_INVALID_KEY && !recoverySpent) { // The key is gone or was never valid. Wipe it and try once from // scratch; a second failure is reported rather than looped. + // + // The spent marker is written BEFORE the identity is discarded, which + // is the ordering the whole one-shot limit rests on. Discarding first + // and then failing to record left no key at all AND no record that a + // recovery had happened -- so the next request took the plain + // generate-key path, which consults neither marker, and every request + // (and every launch, since the in-memory copy does not survive one) + // burned another rate-limited hardware key. Writing first means a + // refusal costs one request and changes nothing else: the rejected + // key is still there, and the terminal flag below stops it being + // attested again in this process. + instance.recoverySpentInMemory = true; + if (!SecureStorage.getInstance().set(KEY_RECOVERY_SPENT, "1")) { + instance.discardFailed = true; + instance.bootstrapInFlight = false; + fail(pending, "App Attest could not record that its one-time " + + "recovery had been used, so it was not attempted"); + instance.failBootstrapWaiters("App Attest could not record that " + + "its one-time recovery had been used"); + return; + } try { - instance.resetLocked(); + // Keeps the marker written a moment ago: this reset IS the + // recovery, so clearing it here would let the replacement look + // like a first recovery all over again. + instance.resetLocked(true); } catch (IllegalStateException e) { // Nothing was discarded, so starting a replacement would leave // two identities and the old one still usable. Fail the caller. @@ -795,29 +830,6 @@ public static void nativeAttestError(final int requestId, final int errorCode, "App Attest could not discard the rejected key"); return; } - // Recorded before the replacement starts, so a request arriving after - // this process dies still sees the recovery as used. The in-memory - // copy is set regardless: if the keychain refuses the write, the - // one-shot limit still holds for the life of the process rather than - // letting every later request burn another key. - instance.recoverySpentInMemory = true; - if (!SecureStorage.getInstance().set(KEY_RECOVERY_SPENT, "1")) { - // No replacement without the marker. The in-memory copy only - // covers this process, and the replacement's own start marker - // DOES persist -- so after a restart the next launch would find a - // discarded-looking key with no record that a recovery had been - // spent, and start another one. That is a rate-limited hardware - // key per launch, caused by the very write that was meant to - // bound it. Failing here costs this caller one request, and the - // device is left with no key at all, so a later attempt starts - // cleanly rather than compounding. - instance.bootstrapInFlight = false; - fail(pending, "App Attest could not record that its one-time " - + "recovery had been used, so it was not attempted"); - instance.failBootstrapWaiters("App Attest could not record that " - + "its one-time recovery had been used"); - return; - } PendingRequest retry = new PendingRequest(pending.result, pending.nonce, PendingRequest.OP_GENERATE_KEY, null); retry.retried = true; From 872d84faa6074478e29da436f851868f4c7a9003 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Fri, 31 Jul 2026 20:07:54 +0300 Subject: [PATCH 46/96] Escape control characters in the nonce instead of erasing them clientData is what the assertion signs over, and the server recomputes the same hash from the challenge it issued. Replacing a control character with a space rewrote the challenge on only one side of that comparison, so a nonce carrying one produced an assertion the server could never verify -- and two different challenges, "a\nb" and "a b", produced byte-identical clientData. The one-to-one binding between a challenge and the bytes signed over it is the entire point of the nonce. Control characters now get JSON escapes: the short forms where they exist and \u00XX otherwise, hand-rolled because String.format is not in the CLDC-era subset and the serialization is part of the wire contract. Verified against a harness carrying every code point below 0x80 through clientData and back out with a JSON string reader: 132 nonces round-trip exactly, a nonce shaped like ",\"k\":\" cannot forge the key id, and the collision the old code produced is gone. Also carries the per-transport reopen lock, which the last commit had as a server-wide one: a restart over a DIFFERENT transport has nothing to serialize against, and making it wait behind a superseded open that may never return deadlocked the restart. Co-Authored-By: Claude Opus 5 (1M context) --- .../src/com/codename1/mcp/MCPServer.java | 29 ++++++++-------- .../impl/ios/IOSDeviceIntegrity.java | 33 ++++++++++++++++++- quality-report.md | 12 +++++++ 3 files changed, 59 insertions(+), 15 deletions(-) create mode 100644 quality-report.md diff --git a/CodenameOne/src/com/codename1/mcp/MCPServer.java b/CodenameOne/src/com/codename1/mcp/MCPServer.java index 467d6aa7720..03842bfcebd 100644 --- a/CodenameOne/src/com/codename1/mcp/MCPServer.java +++ b/CodenameOne/src/com/codename1/mcp/MCPServer.java @@ -218,12 +218,6 @@ private synchronized void releaseAndCloseIfCurrent(MCPTransport t, int generatio } } - /// Held across `open()` so two generations cannot be inside one transport's open at - /// the same time. Deliberately NOT the server monitor: `open()` blocks -- it binds a - /// listener -- and holding the monitor across it would make `stop()` wait on the - /// thing it is trying to stop. - private final Object openLock = new Object(); - private void runLoop(MCPTransport t, int generation) { // Opening is deferred to this thread, so by the time it happens the server may // already have been stopped or restarted. Either way stop()'s close() ran against a @@ -235,14 +229,21 @@ private void runLoop(MCPTransport t, int generation) { return; } try { - // Serialized per server. A stop()/start() over the SAME transport while the - // old reader is still parked inside open() would otherwise have both - // generations open one instance: the same-transport rule then correctly - // declines to close it on the way out, and the transport is left holding two - // listeners with one handle for them -- so the first is leaked and outlives - // stop(). The superseded thread finds itself stale the moment it gets the - // lock, and unwinds without opening anything. - synchronized (openLock) { + // Serialized per TRANSPORT, on that transport's own monitor. A stop()/start() + // over the SAME transport while the old reader is still parked inside open() + // would otherwise have both generations open one instance: the same-transport + // rule then correctly declines to close it on the way out, and the transport + // is left holding two listeners with one handle for them -- so the first is + // leaked and outlives stop(). The superseded thread finds itself stale the + // moment it gets the lock, and unwinds without opening anything. + // + // Per transport and not per server, which is what a server-wide lock got + // wrong: a restart over a DIFFERENT transport has nothing to serialize + // against, and making it wait behind a superseded open that may never return + // deadlocks the restart -- the replacement never opens, so nothing wakes the + // thread that would release it. Not the server monitor either: open() blocks, + // and holding that across it would make stop() wait on what it is stopping. + synchronized (t) { if (!isCurrent(t, generation)) { releaseAndCloseIfCurrent(t, generation); return; diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index f79c695c38f..24cb7ebade6 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -544,14 +544,40 @@ private static String clientDataJson(String nonce, String keyId) { return "{\"n\":\"" + escapeJson(nonce) + "\",\"k\":\"" + escapeJson(keyId) + "\"}"; } + /** + * JSON string escaping that is reversible. + * + *

Control characters are escaped, not replaced. Substituting a space for them + * lost information: a nonce containing a newline and the same nonce containing a + * space produced identical clientData, so the assertion no longer bound the exact + * challenge the server issued -- and the server, recomputing the hash over what it + * sent, would not match it either. The one-to-one binding between a challenge and + * the bytes signed over it is the whole point of the nonce.

+ */ private static String escapeJson(String s) { StringBuilder sb = new StringBuilder(s.length()); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == '"' || c == '\\') { sb.append('\\').append(c); + } else if (c == '\n') { + sb.append("\\n"); + } else if (c == '\r') { + sb.append("\\r"); + } else if (c == '\t') { + sb.append("\\t"); + } else if (c == '\b') { + sb.append("\\b"); + } else if (c == '\f') { + sb.append("\\f"); } else if (c < 0x20) { - sb.append(' '); + // Everything else below 0x20 has no short form and must be \\u-escaped. + // Hand-rolled rather than String.format, which the CLDC-era core does + // not have -- and the exact serialization is part of the wire contract, + // so it has to be predictable rather than locale-dependent. + sb.append("\\u00"); + sb.append(HEX[(c >> 4) & 0xf]); + sb.append(HEX[c & 0xf]); } else { sb.append(c); } @@ -559,6 +585,11 @@ private static String escapeJson(String s) { return sb.toString(); } + private static final char[] HEX = { + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' + }; + // ---- Callbacks invoked from native code (do not rename) ---------------- /** diff --git a/quality-report.md b/quality-report.md new file mode 100644 index 00000000000..67aebc851ce --- /dev/null +++ b/quality-report.md @@ -0,0 +1,12 @@ +## ✅ Continuous Quality Report + +### Test & Coverage +- ✅ **Tests:** 2984 total, 0 failed, 0 skipped +- ⚠️ Coverage report not generated. + +### Static Analysis +- ✅ SpotBugs: no findings (report was not generated by the build). +- ✅ **PMD:** 0 findings (no issues) +- ⚠️ Checkstyle report not generated. + +_Generated automatically by the PR CI workflow._ From 037e319747feb2e37651285f39fed51555e7d183 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sat, 1 Aug 2026 22:24:36 +0700 Subject: [PATCH 47/96] Do not run the native callbacks while retaining them The static initializer invoked each App Attest callback with no-op values to keep the ParparVM dead-code eliminator from stripping them. It ran their real bodies, during class initialization, before the static fields declared below the block were assigned -- static initializers run in textual order -- so take() synchronized on a null REQUESTS map and threw. The class initializes on the EDT, via getCompromiseReasons(). So the NPE reached Display's EDT handler, which shows a modal error dialog: HelloCodenameOne hung at launch on a dialog nobody could dismiss, and every iOS and Mac screenshot job timed out having delivered zero screenshots. The app log carries the whole story: Exception: java.lang.NullPointerException - null at com_codename1_impl_ios_IOSDeviceIntegrity.take:982 at com_codename1_impl_ios_IOSDeviceIntegrity.nativeKeyGenerated:627 at com_codename1_impl_ios_IOSImplementation.deviceIntegrity:4599 A reference the optimizer can see is all that was ever needed, so the calls now sit behind a branch that is never taken. The flag is a plain non-final boolean because javac deletes the body of an if on a constant false, which would take the very references this exists to keep. IOSBiometrics has the identical shape and the identical latent failure -- its block also precedes its REQUESTS field, so touching biometrics on iOS would have failed class initialization the same way. Fixed alongside, since it is the same two lines and this branch is what proved the pattern bites. IOSBluetooth already placed its block after the fields and says why; IOSCarPlayCallbacks and IOSSurfaceCallbacks already use a guard flag. Co-Authored-By: Claude Opus 5 (1M context) --- .../com/codename1/impl/ios/IOSBiometrics.java | 27 ++++++++++++--- .../impl/ios/IOSDeviceIntegrity.java | 33 ++++++++++++++++--- 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSBiometrics.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSBiometrics.java index 62ed7e02570..ebd5acf1641 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSBiometrics.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSBiometrics.java @@ -42,16 +42,33 @@ *

The native side dispatches results back via the static * {@link #nativeAuthSuccess(int)} / {@link #nativeAuthError(int, int, String)} * methods on this class. To stop the ParparVM dead-code eliminator from - * stripping these (no Java caller exists), the static initializer invokes - * each with no-op values --- the same idiom used by the original - * FingerprintScanner cn1lib.

+ * stripping these (no Java caller exists), the static initializer holds a + * reference to each behind a branch that is never taken.

*/ public final class IOSBiometrics extends Biometrics { + /** + * Never true. Exists so the call sites below are real call sites. + * + *

Not {@code final}, and not a compile-time constant: javac deletes the body + * of an {@code if} on a constant false, which would take the very references + * this is here to keep.

+ */ + private static boolean retainNativeCallbacks; + static { // Prevents the iOS VM optimizer from eliding these callbacks. - nativeAuthSuccess(-1); - nativeAuthError(-1, 0, null); + // + // Guarded rather than invoked, which is what the FingerprintScanner cn1lib + // idiom this was copied from did. Invoking them for real ran the callback + // bodies during class initialization, before REQUESTS below was assigned -- + // static initializers run in textual order -- so take() locked on a null map + // and the class failed to initialize. A reference the optimizer can see is + // all that was ever needed. + if (retainNativeCallbacks) { + nativeAuthSuccess(-1); + nativeAuthError(-1, 0, null); + } } // Map request id -> pending AsyncResource. Static because the native diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index 24cb7ebade6..d9b49d409f3 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -76,12 +76,35 @@ */ final class IOSDeviceIntegrity { + /** + * Never true. Exists so the call sites below are real call sites. + * + *

Not {@code final}, and not a compile-time constant: javac deletes the body of + * an {@code if} on a constant false, which would take the very references this is + * here to keep. A plain field is opaque to it, and the translator's elimination pass + * works on reachability in the bytecode rather than on whether the branch can be + * taken -- so the callbacks survive and nothing runs.

+ */ + private static boolean retainNativeCallbacks; + static { - // Prevents the iOS VM optimizer from eliding these native callbacks. - nativeKeyGenerated(-1, null); - nativeAttestationReady(-1, null); - nativeAssertionReady(-1, null); - nativeAttestError(-1, -1, null); + // Prevents the iOS VM optimizer from eliding these native callbacks: no Java + // caller exists, and without a reference they translate to empty stubs and the + // native dispatch silently does nothing. + // + // Guarded rather than invoked. Calling them for real ran the callback bodies + // during class initialization -- before the static fields below this block were + // assigned, since static initializers run in textual order -- so take() locked + // on a null REQUESTS map and threw. The class initializes on the EDT, so that + // NPE reached Display's EDT handler, which shows a modal error dialog: the app + // hung on a dialog nobody could dismiss the first time anything touched device + // integrity. A reference the optimizer can see is all that was ever needed. + if (retainNativeCallbacks) { + nativeKeyGenerated(-1, null); + nativeAttestationReady(-1, null); + nativeAssertionReady(-1, null); + nativeAttestError(-1, -1, null); + } } static final String TOKEN_PREFIX = "cn1aa1"; From f050250144a98db7c4921154d7195700cd488afb Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sat, 1 Aug 2026 22:27:13 +0700 Subject: [PATCH 48/96] Use the guard-flag retention idiom rather than a never-taken branch Same fix, stronger guarantee. A branch on a field nothing ever assigns is something an optimizer is entitled to fold away, taking the reference with it -- silently, leaving the native dispatch calling an empty stub, which is the exact failure the retention exists to prevent. The call has to be unconditional. So both classes now follow IOSCarPlayCallbacks and IOSSurfaceCallbacks: invoke each callback for real, with a static flag set across the block that makes every callback return before it touches anything. Co-Authored-By: Claude Opus 5 (1M context) --- .../com/codename1/impl/ios/IOSBiometrics.java | 37 +++++------ .../impl/ios/IOSDeviceIntegrity.java | 63 ++++++++++++------- 2 files changed, 58 insertions(+), 42 deletions(-) diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSBiometrics.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSBiometrics.java index ebd5acf1641..7119bc73ae1 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSBiometrics.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSBiometrics.java @@ -42,33 +42,28 @@ *

The native side dispatches results back via the static * {@link #nativeAuthSuccess(int)} / {@link #nativeAuthError(int, int, String)} * methods on this class. To stop the ParparVM dead-code eliminator from - * stripping these (no Java caller exists), the static initializer holds a - * reference to each behind a branch that is never taken.

+ * stripping these (no Java caller exists), the static initializer invokes each + * once; they return immediately while that is happening.

*/ public final class IOSBiometrics extends Biometrics { /** - * Never true. Exists so the call sites below are real call sites. - * - *

Not {@code final}, and not a compile-time constant: javac deletes the body - * of an {@code if} on a constant false, which would take the very references - * this is here to keep.

+ * True only while the retention calls below are running, so each callback returns + * before it touches anything. The same idiom as {@code IOSSurfaceCallbacks}. */ - private static boolean retainNativeCallbacks; + private static boolean dceGuard; static { // Prevents the iOS VM optimizer from eliding these callbacks. // - // Guarded rather than invoked, which is what the FingerprintScanner cn1lib - // idiom this was copied from did. Invoking them for real ran the callback - // bodies during class initialization, before REQUESTS below was assigned -- - // static initializers run in textual order -- so take() locked on a null map - // and the class failed to initialize. A reference the optimizer can see is - // all that was ever needed. - if (retainNativeCallbacks) { - nativeAuthSuccess(-1); - nativeAuthError(-1, 0, null); - } + // The guard is what makes the calls harmless, and it was missing: they ran + // their real bodies during class initialization, before REQUESTS below was + // assigned -- static initializers run in textual order -- so take() locked on + // a null map and the class failed to initialize. + dceGuard = true; + nativeAuthSuccess(-1); + nativeAuthError(-1, 0, null); + dceGuard = false; } // Map request id -> pending AsyncResource. Static because the native @@ -140,6 +135,9 @@ public boolean stopAuthentication() { /** Called from native when the LAContext.evaluatePolicy succeeds. */ public static void nativeAuthSuccess(final int requestId) { + if (dceGuard) { + return; + } final AsyncResource r = take(requestId); if (r == null) { return; @@ -156,6 +154,9 @@ public void run() { /** Called from native when evaluatePolicy fails or is cancelled. */ public static void nativeAuthError(final int requestId, final int errorCode, final String msg) { + if (dceGuard) { + return; + } final AsyncResource r = take(requestId); if (r == null) { return; diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index d9b49d409f3..d6bed0d2783 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -69,42 +69,45 @@ *

ParparVM note

* *

The native side dispatches results back through the static callbacks - * below. As with {@link IOSBiometrics}, the static initializer invokes each with - * no-op values so the dead-code eliminator does not strip them -- no Java caller - * exists, and without a reachable reference they become empty stubs and the - * native call silently does nothing.

+ * below. As with {@link IOSBiometrics}, the static initializer invokes each once + * so the dead-code eliminator does not strip them -- no Java caller exists, and + * without a reachable reference they become empty stubs and the native call + * silently does nothing. Each returns immediately while that is happening; see + * {@code dceGuard}.

*/ final class IOSDeviceIntegrity { /** - * Never true. Exists so the call sites below are real call sites. + * True only while the retention calls below are running, so each callback returns + * before it touches anything. * - *

Not {@code final}, and not a compile-time constant: javac deletes the body of - * an {@code if} on a constant false, which would take the very references this is - * here to keep. A plain field is opaque to it, and the translator's elimination pass - * works on reachability in the bytecode rather than on whether the branch can be - * taken -- so the callbacks survive and nothing runs.

+ *

The same idiom as {@code IOSCarPlayCallbacks} and {@code IOSSurfaceCallbacks}, + * and it is deliberately not the more obvious {@code if (neverTrue) { call(); }}: + * the call has to be unconditional for the eliminator to be certain to keep it, + * whereas a branch on a field nothing ever assigns is something an optimizer is + * entitled to fold away -- taking the reference with it, silently, and leaving the + * native dispatch calling an empty stub.

*/ - private static boolean retainNativeCallbacks; + private static boolean dceGuard; static { // Prevents the iOS VM optimizer from eliding these native callbacks: no Java // caller exists, and without a reference they translate to empty stubs and the // native dispatch silently does nothing. // - // Guarded rather than invoked. Calling them for real ran the callback bodies - // during class initialization -- before the static fields below this block were - // assigned, since static initializers run in textual order -- so take() locked - // on a null REQUESTS map and threw. The class initializes on the EDT, so that - // NPE reached Display's EDT handler, which shows a modal error dialog: the app - // hung on a dialog nobody could dismiss the first time anything touched device - // integrity. A reference the optimizer can see is all that was ever needed. - if (retainNativeCallbacks) { - nativeKeyGenerated(-1, null); - nativeAttestationReady(-1, null); - nativeAssertionReady(-1, null); - nativeAttestError(-1, -1, null); - } + // The guard is what makes the calls harmless. Without it they ran their real + // bodies during class initialization -- before the static fields below this + // block were assigned, since static initializers run in textual order -- so + // take() synchronized on a null REQUESTS map and threw. This class initializes + // on the EDT, so that NPE reached Display's EDT handler, which shows a modal + // error dialog: the app hung at launch on a dialog nobody could dismiss, the + // first time anything asked about device integrity. + dceGuard = true; + nativeKeyGenerated(-1, null); + nativeAttestationReady(-1, null); + nativeAssertionReady(-1, null); + nativeAttestError(-1, -1, null); + dceGuard = false; } static final String TOKEN_PREFIX = "cn1aa1"; @@ -647,6 +650,9 @@ private static void failBootstrapAttempt(PendingRequest pending, String msg) { /** Called from native once a fresh hardware key exists. */ public static void nativeKeyGenerated(final int requestId, final String keyId) { + if (dceGuard) { + return; + } PendingRequest pending = take(requestId); if (pending == null || instance == null) { return; @@ -692,6 +698,9 @@ public static void nativeKeyGenerated(final int requestId, final String keyId) { /** Called from native with the attestation object for a newly attested key. */ public static void nativeAttestationReady(final int requestId, final String attestationB64) { + if (dceGuard) { + return; + } PendingRequest pending = take(requestId); if (pending == null) { return; @@ -784,6 +793,9 @@ public static void nativeAttestationReady(final int requestId, final String atte /** Called from native with an assertion over an already attested key. */ public static void nativeAssertionReady(final int requestId, final String assertionB64) { + if (dceGuard) { + return; + } PendingRequest pending = take(requestId); if (pending == null) { return; @@ -828,6 +840,9 @@ public static void nativeAssertionReady(final int requestId, final String assert /** Called from native when any step fails. errorCode is the raw DCError code. */ public static void nativeAttestError(final int requestId, final int errorCode, final String msg) { + if (dceGuard) { + return; + } PendingRequest pending = take(requestId); if (pending == null) { return; From 2df7c846a5e410ba1117f9f7160d4eff85ca42da Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sat, 1 Aug 2026 22:41:39 +0700 Subject: [PATCH 49/96] Close the restart race inside the transport lock, pin runtime hosts, and narrow the jailbreak API Four review findings. The per-transport lock covered open() but not the teardown that follows it, so a superseded thread released the monitor and only then decided what to tear down. The replacement could acquire the monitor first and call open() while the stale listener was still registered -- and a transport that refuses a second listener, which is the fix for the earlier leak, answers that with an IOException whose catch stops the server the restart had just started. The whole open now happens inside the critical section, and a thread unwinding from its own open closes that open unconditionally: releaseAndCloseIfCurrent's "leave it, the replacement owns it" rule is right for the read loop and wrong here, because the replacement is still blocked and has opened nothing. The simulator built its pin set from the startup ShieldConfig alone, so a backend registered later through AppShield.addProtectedHost() had no simulated pin. Unpinned means PinSet.isEnforcedFor() is false, which means the certificate check is skipped, which means "Force Pin Mismatch On Next Request" could not reach it -- the one host an app went out of its way to register was the one host whose failure path could not be rehearsed. AppShield now exposes every protected host, config and runtime alike. cn1ReadDerTlv compared off + *totalLen against len, and that sum can wrap for a large off -- the check meant to stop an overread would have been what allowed it. Compared against len - off instead, which cannot wrap because off <= len is the function's precondition. Verified with a standalone harness: 23,396,355 cases over every small buffer, offset and two-byte header, plus the wrapped-off case, with zero out-of-range accepts. isJailbrokenDevice() returned true for any compromise reason, and a clean device with Xcode attached reports `traced` -> `debugger`. That made a plain debug session look like a jailbreak to a specifically documented API. It now looks for the jailbreak reason only; aggregation stays isDeviceCompromised()'s job. Co-Authored-By: Claude Opus 5 (1M context) --- .../src/com/codename1/mcp/MCPServer.java | 113 ++++++++++------ .../codename1/security/shield/AppShield.java | 30 +++++ .../impl/javase/JavaSEShieldEngine.java | 21 ++- .../nativeSources/NetworkConnectionImpl.m | 7 +- .../codename1/impl/ios/IOSImplementation.java | 22 +++- .../mcp/MCPLoopbackTransportOpenTest.java | 121 ++++++++++++++++++ .../impl/javase/JavaSEShieldEngineTest.java | 24 ++++ 7 files changed, 295 insertions(+), 43 deletions(-) diff --git a/CodenameOne/src/com/codename1/mcp/MCPServer.java b/CodenameOne/src/com/codename1/mcp/MCPServer.java index 03842bfcebd..0f39727bed6 100644 --- a/CodenameOne/src/com/codename1/mcp/MCPServer.java +++ b/CodenameOne/src/com/codename1/mcp/MCPServer.java @@ -218,6 +218,30 @@ private synchronized void releaseAndCloseIfCurrent(MCPTransport t, int generatio } } + /// Unwinds an open this thread performed and must not keep: clears `running` when the + /// server is still on this run, and closes `t` either way. + /// + /// Unconditionally, which is the one thing it does differently from + /// `releaseAndCloseIfCurrent`, and the difference is the whole reason it exists. That + /// method's same-transport rule -- leave `t` alone, the replacement owns it -- is + /// right for a thread unwinding from the read loop, where the replacement has already + /// opened the transport and closing it would kill a live server. It is wrong here: + /// this runs inside the transport's monitor, so the replacement is still blocked and + /// has opened nothing, and the listener registered on `t` is this thread's alone. + /// Declining to close it left the replacement's own `open()` refused with "already + /// listening" -- and that IOException stopped the server the restart had just brought + /// up. + /// + /// Only ever called while holding `t`'s monitor, which is what makes "the replacement + /// cannot have opened yet" true rather than merely likely. + private synchronized void discardOwnOpen(MCPTransport t, int generation) { + if (startGeneration == generation + && transport == t) { // NOPMD identity: is this still our transport? + running = false; + } + t.close(); + } + private void runLoop(MCPTransport t, int generation) { // Opening is deferred to this thread, so by the time it happens the server may // already have been stopped or restarted. Either way stop()'s close() ran against a @@ -228,48 +252,59 @@ private void runLoop(MCPTransport t, int generation) { if (!isCurrent(t, generation)) { return; } - try { - // Serialized per TRANSPORT, on that transport's own monitor. A stop()/start() - // over the SAME transport while the old reader is still parked inside open() - // would otherwise have both generations open one instance: the same-transport - // rule then correctly declines to close it on the way out, and the transport - // is left holding two listeners with one handle for them -- so the first is - // leaked and outlives stop(). The superseded thread finds itself stale the - // moment it gets the lock, and unwinds without opening anything. - // - // Per transport and not per server, which is what a server-wide lock got - // wrong: a restart over a DIFFERENT transport has nothing to serialize - // against, and making it wait behind a superseded open that may never return - // deadlocks the restart -- the replacement never opens, so nothing wakes the - // thread that would release it. Not the server monitor either: open() blocks, - // and holding that across it would make stop() wait on what it is stopping. - synchronized (t) { - if (!isCurrent(t, generation)) { - releaseAndCloseIfCurrent(t, generation); - return; - } - t.open(); + // Serialized per TRANSPORT, on that transport's own monitor, and the WHOLE + // open -- the attempt, its failure path, and the teardown of a superseded one -- + // happens inside. A stop()/start() over the SAME transport while the old reader + // is still parked in open() would otherwise have both generations open one + // instance: the same-transport rule then correctly declines to close it on the + // way out, and the transport is left holding two listeners with one handle for + // them, so the first is leaked and outlives stop(). + // + // The teardown has to be in here too, not after. Releasing the monitor first let + // the replacement acquire it and call open() while this thread's now-stale + // listener was still registered -- and the transport refuses a second listener, + // so the replacement took an IOException and stopped the server it had just + // started. Holding the monitor until the stale one is actually closed is what + // makes "the replacement may open" mean what it says. + // + // Per transport and not per server, which is what a server-wide lock got wrong: + // a restart over a DIFFERENT transport has nothing to serialize against, and + // making it wait behind a superseded open that may never return deadlocks the + // restart -- the replacement never opens, so nothing wakes the thread that would + // release it. Not the server monitor either: open() blocks, and holding that + // across it would make stop() wait on what it is stopping. The server monitor is + // taken inside this one (by isCurrent and releaseAndCloseIfCurrent) and never the + // other way round -- stop() closes through the transport's own internal lock, not + // its monitor -- so the nesting has one direction only. + synchronized (t) { + if (!isCurrent(t, generation)) { + releaseAndCloseIfCurrent(t, generation); + return; } - } catch (IOException ex) { - // The transport failed to start listening. Log defensively: the CN1 Log - // routes through the platform implementation, which may not be registered - // yet when the server is auto-started early in Display.init(), and a raw - // NullPointerException here would silently kill the reader thread. try { - Log.e(ex); - } catch (Throwable logErr) { - System.err.println("[cn1.mcp] transport open failed: " + ex); + t.open(); + } catch (IOException ex) { + // The transport failed to start listening. Log defensively: the CN1 Log + // routes through the platform implementation, which may not be registered + // yet when the server is auto-started early in Display.init(), and a raw + // NullPointerException here would silently kill the reader thread. + try { + Log.e(ex); + } catch (Throwable logErr) { + System.err.println("[cn1.mcp] transport open failed: " + ex); + } + // A failed open can still have registered something -- the loopback + // transport claims the process-wide slot before it binds -- so this + // unwinds the same way a successful one does. + discardOwnOpen(t, generation); + return; + } + if (!isCurrent(t, generation)) { + // Same window, the far side of it: the server moved on while open() was + // in flight. The listener now on `t` is this thread's and has to go. + discardOwnOpen(t, generation); + return; } - releaseAndCloseIfCurrent(t, generation); - return; - } - if (!isCurrent(t, generation)) { - // Same window, the far side of it: the server moved on while open() was in - // flight. Undo the registration this thread just took -- unless the server - // moved on by restarting over this very transport, in which case it is not - // ours to undo. - releaseAndCloseIfCurrent(t, generation); - return; } while (isCurrent(t, generation)) { String line; diff --git a/CodenameOne/src/com/codename1/security/shield/AppShield.java b/CodenameOne/src/com/codename1/security/shield/AppShield.java index ce318fecf8a..015376d12f3 100644 --- a/CodenameOne/src/com/codename1/security/shield/AppShield.java +++ b/CodenameOne/src/com/codename1/security/shield/AppShield.java @@ -30,6 +30,7 @@ import com.codename1.security.shield.spi.ShieldEngineRegistry; import com.codename1.ui.Display; import com.codename1.util.AsyncResource; +import java.util.Enumeration; import java.util.Hashtable; import java.util.Vector; @@ -435,6 +436,35 @@ public static void addProtectedHost(String host) { addProtectedHost(host, null); } + /// Every host currently protected: the ones [ShieldConfig] carried into + /// [#init(ShieldConfig)], plus anything registered since through + /// [#addProtectedHost(String, HostPolicy)]. + /// + /// The config alone is not the answer to "what is protected", and treating it as such + /// is a quiet way to lose the runtime registrations. An engine building a pin set from + /// the config would leave a backend discovered at runtime with no pins, so + /// [PinSet#isEnforcedFor] returns false for it and the certificate check is skipped + /// entirely -- the host the app went out of its way to register is the one host not + /// pinned. + public static Enumeration protectedHosts() { + Vector all = new Vector(); + Enumeration configured = getConfig().protectedHosts(); + while (configured.hasMoreElements()) { + Object host = configured.nextElement(); + if (host != null && !all.contains(host)) { + all.addElement(host); + } + } + Enumeration runtime = runtimeHosts.keys(); + while (runtime.hasMoreElements()) { + Object host = runtime.nextElement(); + if (host != null && !all.contains(host)) { + all.addElement(host); + } + } + return all.elements(); + } + private static HostPolicy implicitPolicy() { FailureMode mode = getConfig().getDefaultFailureMode(); if (mode == FailureMode.OPEN) { diff --git a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEShieldEngine.java b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEShieldEngine.java index 9aa755491de..fc9950de88b 100644 --- a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEShieldEngine.java +++ b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEShieldEngine.java @@ -189,11 +189,28 @@ public PinSet getPinSet() { // switch would still have nothing to act on: PinSet.isEnforcedFor() is false for // a host with no pins, and ShieldNetworkGuard checks that before asking. Hashtable hostToPins = new Hashtable(); + // Both sources, not just the ShieldConfig handed to initialize(). That config is a + // snapshot of what the app declared up front and never learns about + // AppShield.addProtectedHost() -- so a backend discovered at runtime, which is + // exactly the case that call exists for, had no simulated pin, hence no + // enforcement, hence no way for "Force Pin Mismatch On Next Request" to reach it. + // The one pin-failure path a developer cannot produce any other way was the one + // the simulator could not produce either. + // + // The config is still read directly rather than only through AppShield, because + // an engine can be initialized with a config that AppShield was never handed -- + // which is how the tests drive it, and losing that would trade one blind spot + // for another. + Vector sources = new Vector(); if (config != null) { - java.util.Enumeration hosts = config.protectedHosts(); + sources.addElement(config.protectedHosts()); + } + sources.addElement(AppShield.protectedHosts()); + for (int i = 0; i < sources.size(); i++) { + java.util.Enumeration hosts = (java.util.Enumeration) sources.elementAt(i); while (hosts.hasMoreElements()) { String host = (String) hosts.nextElement(); - if (host == null) { + if (host == null || hostToPins.containsKey(host)) { continue; } // Wildcards included. PinSet.isEnforcedFor() resolves "api.example.com" diff --git a/Ports/iOSPort/nativeSources/NetworkConnectionImpl.m b/Ports/iOSPort/nativeSources/NetworkConnectionImpl.m index 58c3ffb6855..8b533ab89d9 100644 --- a/Ports/iOSPort/nativeSources/NetworkConnectionImpl.m +++ b/Ports/iOSPort/nativeSources/NetworkConnectionImpl.m @@ -271,7 +271,12 @@ static BOOL cn1ReadDerTlv(const uint8_t* buf, NSUInteger len, NSUInteger off, } *totalLen = *headerLen + contentLen; } - return off + *totalLen <= len; + // Against what is left, not by forming off + *totalLen. That sum can wrap + // NSUInteger for a large off, and a wrapped sum compares small -- so the check + // that exists to stop an overread would be the thing that let it through. The + // subtraction cannot wrap: off <= len is this function's precondition and is + // re-established on the short-form path above. + return off <= len && *totalLen <= len - off; } /** diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSImplementation.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSImplementation.java index 3dc1f0979ed..c9265d25d63 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSImplementation.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSImplementation.java @@ -12573,9 +12573,29 @@ public boolean isPolygon() { } } + /** + * The documented jailbreak/root check, and only that. + * + *

Deliberately not "any compromise reason". {@code getCompromiseReasons()} also + * reports a debugger, and a clean device with Xcode attached emits {@code traced} -- + * so a plain debug session made this return true. That is a specifically documented + * jailbreak API with callers that branch on it, and telling them a developer's own + * device is jailbroken every time they hit Run is a regression, not extra vigilance. + * Aggregating the rest is {@link #isDeviceCompromised()}'s job.

+ * + *

Hooking is left out for the same reason it is reported separately: a hooking + * framework is evidence of instrumentation, which usually accompanies a jailbreak but + * is not one, and callers that want the broader question have the broader method.

+ */ @Override public boolean isJailbrokenDevice() { - return getCompromiseReasons().length > 0; + String[] reasons = getCompromiseReasons(); + for (int i = 0; i < reasons.length; i++) { + if ("jailbreak".equals(reasons[i])) { + return true; + } + } + return false; } /** diff --git a/maven/core-unittests/src/test/java/com/codename1/mcp/MCPLoopbackTransportOpenTest.java b/maven/core-unittests/src/test/java/com/codename1/mcp/MCPLoopbackTransportOpenTest.java index 1f52ad1c3e3..da0a044006e 100644 --- a/maven/core-unittests/src/test/java/com/codename1/mcp/MCPLoopbackTransportOpenTest.java +++ b/maven/core-unittests/src/test/java/com/codename1/mcp/MCPLoopbackTransportOpenTest.java @@ -314,6 +314,127 @@ void aSupersededReaderMustNotCloseATransportTheRestartIsNowServing() throws Exce transport.releaseFirst(2); } + /// A transport that refuses a second listener, as the real one does, and whose first + /// open can be held open on demand. + private static final class SingleListenerTransport implements MCPTransport { + private final Object gate = new Object(); + private boolean listening; + private boolean holdNextOpen = true; + private boolean released; + private boolean openEntered; + volatile int openCount; + volatile int closeCount; + volatile IOException openFailure; + + void awaitOpenEntered() throws InterruptedException { + synchronized (gate) { + while (!openEntered) { + gate.wait(5000); + } + } + } + + void releaseOpen() { + synchronized (gate) { + released = true; + gate.notifyAll(); + } + } + + public void open() throws IOException { + boolean hold; + synchronized (gate) { + openEntered = true; + hold = holdNextOpen; + holdNextOpen = false; + gate.notifyAll(); + while (hold && !released) { + try { + gate.wait(5000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IOException("interrupted"); + } + } + // The refusal the real transport makes: one listener per instance, and a + // second is an error rather than a silent overwrite. + if (listening) { + IOException failure = new IOException("This MCP transport is already " + + "listening"); + openFailure = failure; + throw failure; + } + listening = true; + openCount++; + gate.notifyAll(); + } + } + + public String readMessage() throws IOException { + synchronized (gate) { + while (listening) { + try { + gate.wait(5000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IOException("interrupted"); + } + } + } + return null; + } + + public void writeMessage(String message) throws IOException { + } + + public void close() { + synchronized (gate) { + listening = false; + closeCount++; + gate.notifyAll(); + } + } + } + + /// The restart must not be refused by the listener the superseded thread opened. + /// + /// Holding the transport's monitor only across `open()` was not enough. The + /// superseded thread released it and then decided what to tear down, so the + /// replacement could acquire the monitor first and call `open()` while the stale + /// listener was still registered -- and a transport that refuses a second listener + /// (which is the fix for the earlier leak) answers that with an IOException, whose + /// catch stops the server the restart had just brought up. A restart that reports + /// success and is dead is worse than the leak it replaced. + @Test + void aRestartIsNotRefusedByTheListenerTheSupersededThreadOpened() throws Exception { + MCPServer server = new MCPServer(); + SingleListenerTransport transport = new SingleListenerTransport(); + + // Generation 1 is parked inside open(). + server.start(transport); + transport.awaitOpenEntered(); + + // Restart over the SAME instance while it is still in there. + server.stop(); + server.start(transport); + + // Now let the first open finish. It registers a listener, discovers it is stale, + // and has to close it before the replacement is allowed to open. + transport.releaseOpen(); + + for (int i = 0; i < 500 && transport.openCount < 2; i++) { + Thread.sleep(10); + } + assertNull(transport.openFailure, + "the replacement's open() must not be refused by the stale listener"); + assertEquals(2, transport.openCount, + "the replacement should have opened the transport for itself"); + assertTrue(server.isRunning(), + "the restarted server must survive the superseded thread unwinding"); + + server.stop(); + } + @Test void stoppingWhileTheReaderThreadIsStillOpeningStillClosesTheTransport() throws Exception { // start() hands the transport to a reader thread, so stop() can run before that diff --git a/maven/javase/src/test/java/com/codename1/impl/javase/JavaSEShieldEngineTest.java b/maven/javase/src/test/java/com/codename1/impl/javase/JavaSEShieldEngineTest.java index 44cb36135b2..1a84567c9bf 100644 --- a/maven/javase/src/test/java/com/codename1/impl/javase/JavaSEShieldEngineTest.java +++ b/maven/javase/src/test/java/com/codename1/impl/javase/JavaSEShieldEngineTest.java @@ -22,6 +22,7 @@ */ package com.codename1.impl.javase; +import com.codename1.security.shield.AppShield; import com.codename1.security.shield.PinSet; import com.codename1.security.shield.ShieldConfig; import com.codename1.security.shield.ShieldException; @@ -125,6 +126,29 @@ void registeredHostsAreEnforcedSoTheMismatchHasSomethingToActOn() { "but the apex is not covered by its own wildcard"); } + /// A backend registered after init has to be enforced too. + /// + /// AppShield.addProtectedHost() exists for a host discovered at runtime, and a pin set + /// built only from the startup ShieldConfig never sees it. The consequence is not + /// cosmetic: ShieldNetworkGuard asks PinSet.isEnforcedFor() before it asks + /// verifyPins(), so an unpinned host skips the certificate check entirely and "Force + /// Pin Mismatch On Next Request" cannot reach it. The one host the app went out of its + /// way to register was the one host whose failure path could not be rehearsed. + @Test + void aHostRegisteredAtRuntimeIsEnforcedToo() { + String host = "discovered.example.com"; + assertFalse(engine.getPinSet().isEnforcedFor(host), + "the fixture must start with this host unregistered, or this proves nothing"); + + AppShield.addProtectedHost(host); + assertTrue(engine.getPinSet().isEnforcedFor(host), + "a runtime registration must reach the simulated pin set"); + + JavaSEShield.forcePinMismatch = true; + assertFalse(engine.verifyPins(host, new String[0], new String[0]), + "and the force switch has to be able to fail it"); + } + @Test void failingThePinFetchYieldsNoEnforcementRatherThanAMismatch() { // Pinning fails OPEN on unavailability everywhere in this design; the simulator From d9e14cb13a999a6233c03fec29c1127138b0bb8e Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sat, 1 Aug 2026 23:00:19 +0700 Subject: [PATCH 50/96] Keep the App Attest identity readable when a deletion fails, and always run the Cydia probe Three findings, all about a partial failure looking like a clean one. resetLocked deleted KEY_ID and KEY_STATE unconditionally, so a keychain that refused the first and accepted the second left an already-attested key behind with no state. The in-memory terminal flag covers that for the current process and does not survive a restart -- and on the next launch requestToken() reads a key with no state as freshly generated and submits it to Apple. That is a rate-limited attestation of a key Apple has already attested, once per launch, indefinitely. The state now goes only after the identifier is confirmed gone, so a surviving key stays readable as what it is. The explicit reset cleared recoverySpentInMemory whether or not the persisted marker actually went, and reported success. A later invalidKey then found the recovery already spent and refused the one replacement it is allowed -- which is exactly what resetAttestation() was called to restore. The flag now follows what the keychain did, and a failed removal is reported as a failed reset. Deliberately not the terminal discardFailed state: the identity is gone and requests can proceed, so refusing every request over a marker would be a larger outage than the thing it is reacting to. getCompromiseReasons() ran the Cydia probe only when the native probes found nothing, and those two sets of evidence are independent -- the native side reads hard-coded paths and dyld, the scheme catches installs those miss. So a single unrelated signal suppressed it, and a debugger alone was enough: `traced` made the list non-empty, the probe was skipped, and a jailbreak only Cydia can see went unreported. A developer running under Xcode was being told their jailbroken device was clean. Co-Authored-By: Claude Opus 5 (1M context) --- .../impl/ios/IOSDeviceIntegrity.java | 36 +++++++++++++++++-- .../codename1/impl/ios/IOSImplementation.java | 19 +++++----- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index d6bed0d2783..f750f3d74f7 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -283,7 +283,15 @@ private void resetLocked(boolean keepSpentMarker) { // asked us to discard -- and keeps asserting with it. Better to leave the state // visibly unchanged so the caller fails and can retry. boolean idGone = store.remove(KEY_ID); - boolean stateGone = store.remove(KEY_STATE); + // The state goes only once the identifier is confirmed gone, and the ordering is + // the whole point. Deleted unconditionally, a keychain that refused KEY_ID and + // accepted KEY_STATE left an already-attested key behind with no state at all -- + // and the in-memory terminal flag that covers this does not survive a restart. + // On the next launch requestToken() reads a key with no state as freshly + // generated and submits it to Apple, which is a rate-limited attestation of a + // key Apple has already attested, once per launch, forever. Leaving the state in + // place keeps the surviving key readable as what it actually is. + boolean stateGone = idGone && store.remove(KEY_STATE); store.remove(KEY_RETRY_AFTER); store.remove(KEY_PENDING_SINCE); // The markers that make a surviving key terminal are cleared only once the key @@ -293,15 +301,37 @@ private void resetLocked(boolean keepSpentMarker) { // whose attestation never began, and submits to Apple. Every request after it // does the same, against a rate limit, with the one-shot recovery marker also // gone so nothing stops the loop. + boolean markerGone = true; if (idGone && stateGone) { store.remove(KEY_ATTEST_STARTED); attestAnsweredForKey = null; discardFailed = false; if (!keepSpentMarker) { - store.remove(KEY_RECOVERY_SPENT); - recoverySpentInMemory = false; + // Checked, and the in-memory flag follows what the keychain actually + // did. Clearing it regardless reported a successful reset while the + // persisted marker survived -- so the next invalidKey found the recovery + // already spent and refused the one replacement it is allowed, which is + // precisely what resetAttestation() was called to restore. A reset that + // does not restore the thing it promises has to say so. + markerGone = store.remove(KEY_RECOVERY_SPENT); + recoverySpentInMemory = !markerGone; } } + if (idGone && stateGone && !markerGone) { + // The identity is gone, so requests can proceed and a fresh key will be + // generated -- deliberately NOT the terminal discardFailed state, which + // would refuse every request over a marker. What is wrong is narrower: the + // one-shot recovery still reads as spent, which is the documented behaviour + // of that marker rather than a new failure. The caller is told the reset + // did not do everything it was asked to, and can retry it. + generation++; + bootstrapInFlight = false; + failBootstrapWaiters("App Attest could not clear its recovery marker"); + throw new IllegalStateException("App Attest discarded its stored key but " + + "could not clear the spent-recovery marker; the keychain refused " + + "the deletion, so a replacement key would still be refused its " + + "one recovery attempt"); + } if (!idGone || !stateGone) { // The identity survives in part, and there may be nothing left to make it // terminal: a successfully attested key has no start marker and no spent diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSImplementation.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSImplementation.java index c9265d25d63..3c3abc95d05 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSImplementation.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSImplementation.java @@ -12624,15 +12624,6 @@ public String[] getCompromiseReasons() { String[] signals = deviceIntegrity().jailbreakSignals(); java.util.ArrayList out = new java.util.ArrayList(); boolean jailbreakReported = false; - if (signals.length == 0) { - // The native probes found nothing, but an app that declares the - // cydia scheme can still detect one this way. Dropping it here would - // regress isDeviceCompromised() for exactly those apps. - if (cydiaProbe()) { - out.add("jailbreak"); - } - return out.toArray(new String[out.size()]); - } for (int i = 0; i < signals.length; i++) { String s = signals[i]; if ("hookLib".equals(s) || "dyldInsert".equals(s)) { @@ -12648,6 +12639,16 @@ public String[] getCompromiseReasons() { out.add("jailbreak"); } } + // Always, not only when the native probes found nothing. Those two sets of + // evidence are independent: the native side checks hard-coded paths and dyld, + // and the cydia scheme catches installs those miss -- so gating one on the other + // being empty meant a single unrelated signal suppressed it. A debugger alone + // was enough: `traced` made the list non-empty, the probe was skipped, and a + // jailbreak only cydia could see went unreported. Which is how a developer + // running under Xcode ends up being told their jailbroken device is clean. + if (!jailbreakReported && cydiaProbe()) { + out.add("jailbreak"); + } return out.toArray(new String[out.size()]); } From ae7e497dea2e7c4a74b2bb1c051c8d9bb2eaff5d Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sat, 1 Aug 2026 23:43:46 +0700 Subject: [PATCH 51/96] Merge master, and deliver a late-attached health listener on the EDT The merge brings in HealthEdtDeliveryTest, which fails intermittently under this branch's CI. It is not this branch's bug but it is this branch's red, and the mechanism is worth having fixed. AsyncResource.ready runs its callback immediately, on the registering thread, when the resource has already settled. EdtResult's guarantee therefore covered only listeners attached BEFORE completion -- and the facade actions that answer without a backend (openHealthSettings, openProviderSetup) complete before they return, so a caller cannot attach in time. Which thread the callback ran on came down to whether the EDT had drained the completion hop yet: on the EDT on an idle machine, off it on a loaded one. On the CI runner that test ran directly after a class that took 944 seconds. EdtResult.ready now marshals a late registration onto the EDT. Deliberately not the same for except. Reading the error out of an already-failed resource by registering a callback and looking at what it captured is an established idiom here -- HealthFallbackTest.errorOf and BtTestUtil both do it, and it depends on that call being synchronous; marshalling it broke fifteen tests. The asymmetry is the honest one: this contract exists so a callback that acts on a VALUE is on the EDT, and introspecting a failure that already happened is not that. aListenerAttachedAfterCompletionStillArrivesOnTheEdt settles the resource and waits for the hop before attaching, so it takes the late path every time rather than by luck. Confirmed it fails with the override removed. Full core suite: 4725 tests green. Co-Authored-By: Claude Opus 5 (1M context) --- .../com/codename1/impl/health/EdtResult.java | 38 +++++++++++++++ .../health/HealthEdtDeliveryTest.java | 46 +++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/CodenameOne/src/com/codename1/impl/health/EdtResult.java b/CodenameOne/src/com/codename1/impl/health/EdtResult.java index 780a1d2c79e..9649d57df15 100644 --- a/CodenameOne/src/com/codename1/impl/health/EdtResult.java +++ b/CodenameOne/src/com/codename1/impl/health/EdtResult.java @@ -22,6 +22,9 @@ */ package com.codename1.impl.health; +import com.codename1.util.AsyncResource; +import com.codename1.util.SuccessCallback; +import com.codename1.util.EasyThread; import com.codename1.ui.Display; /// The resource every public health operation hands back: one outcome, @@ -47,6 +50,41 @@ /// completes another resource does not queue a runnable per link. public final class EdtResult extends OneShot { + /// Late registration is delivered on the EDT too. + /// + /// `AsyncResource.ready` runs the callback immediately, on the registering thread, + /// when the resource has already settled -- so the guarantee this class exists to + /// make held only for listeners attached before completion. Every facade action that + /// answers without a backend (`openHealthSettings`, `openProviderSetup`) completes + /// the resource before returning it, so the caller CANNOT attach in time, and which + /// thread the callback ran on came down to whether the EDT had drained the hop yet. + /// Off the EDT on a busy machine, on it on an idle one -- a callback that is usually + /// on the EDT is exactly the thing the class doc calls not-a-design. + /// + /// Deliberately NOT done for `except`. Reading the error out of an already-failed + /// resource by registering a callback and looking at what it captured is an + /// established idiom here -- `HealthFallbackTest.errorOf` and `BtTestUtil` both do + /// it, and it depends on that call being synchronous. The asymmetry is the honest + /// one: this contract exists so a callback that acts on a VALUE -- updates a label, + /// touches a form -- is on the EDT, and introspecting a failure that has already + /// happened is not that. + @Override + public AsyncResource ready(SuccessCallback callback, EasyThread t) { + if (isDone() && !isCancelled() && Display.isInitialized() + && !Display.getInstance().isEdt()) { + final SuccessCallback target = callback; + final EasyThread thread = t; + Display.getInstance().callSerially(new Runnable() { + @Override + public void run() { + EdtResult.super.ready(target, thread); + } + }); + return this; + } + return super.ready(callback, t); + } + @Override public void complete(T value) { if (Display.getInstance().isEdt()) { diff --git a/maven/core-unittests/src/test/java/com/codename1/health/HealthEdtDeliveryTest.java b/maven/core-unittests/src/test/java/com/codename1/health/HealthEdtDeliveryTest.java index c51fc2e35d6..1c0b5230da7 100644 --- a/maven/core-unittests/src/test/java/com/codename1/health/HealthEdtDeliveryTest.java +++ b/maven/core-unittests/src/test/java/com/codename1/health/HealthEdtDeliveryTest.java @@ -320,6 +320,52 @@ public void run() { }, true); } + /// A listener attached AFTER the result settled still arrives on the EDT. + /// + /// This is the same guarantee as the tests above, at the one moment it used not to + /// hold. `AsyncResource.ready` invokes the callback immediately, on the registering + /// thread, when the resource is already done -- so EdtResult's contract covered only + /// listeners attached before completion. The facade actions complete before + /// returning, so a caller cannot attach in time, and which thread the callback ran + /// on came down to whether the EDT had drained the hop yet: on the EDT on an idle + /// machine, off it on a loaded one. `aFacadeActionDeliversOnTheEdt` therefore passed + /// or failed by timing. This one settles the resource and waits for the hop to drain + /// before attaching, so the late path is the only one it can take. + @Test + void aListenerAttachedAfterCompletionStillArrivesOnTheEdt() { + final Landing landing = new Landing(); + CN.invokeAndBlock(new Runnable() { + public void run() { + assertFalse(CN.isEdt(), "the operation must start off the EDT"); + AsyncResource settled = Health.getInstance().openHealthSettings(); + // Wait for the completion hop itself, so the registration below is + // unambiguously late rather than merely probably late. + long deadline = System.currentTimeMillis() + 10_000L; + while (!settled.isDone() && System.currentTimeMillis() < deadline) { + try { + Thread.sleep(10L); + } catch (InterruptedException ex) { + return; + } + } + assertTrue(settled.isDone(), "the fixture needs a settled resource"); + settled.onResult(landing); + deadline = System.currentTimeMillis() + 10_000L; + while (!landing.arrived.get() && System.currentTimeMillis() < deadline) { + try { + Thread.sleep(10L); + } catch (InterruptedException ex) { + return; + } + } + } + }); + assertTrue(landing.arrived.get(), "the callback must arrive"); + assertTrue(landing.onEdt.get(), + "a result must arrive on the EDT even when the listener was attached " + + "after it settled"); + } + @Test void aDeleteDeliversOnTheEdt() { final FakeHealthStore store = new FakeHealthStore(); From 04de358fa5268c3f733a85a74af15d69befc849f Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sat, 1 Aug 2026 23:56:42 +0700 Subject: [PATCH 52/96] Wait for the component-inspector details panel before capturing, and adopt the populated reference javase-simulator-tests passed on the commit before the master merge and failed on the merge, with a diff confined to the lower "Component Details" panel: the new capture shows its ten field labels, the stored reference shows it empty. That panel populates a moment after the inspector opens and nothing was waiting for it. The warmup is a fixed floor, and the two existing readiness predicates only ask whether the desktop and the device window have painted -- so the capture landed on either side of the population depending on how loaded the runner was. The suite passed and failed on successive runs of identical code, which is worse than a wrong expectation, because it teaches everyone to re-run it. So the harness now polls for the panel, in the same shape as the device-window predicate beside it, and the reference is updated to the populated state, which is what the test was always meant to be photographing. The threshold is measured rather than guessed: 3712 dark pixels in that region populated, 1512 empty -- the empty state is not blank, it keeps the tab label, the borders and the overflow button, which is why a plain is-it-flat check does not separate them. Nothing here is App Shield's; it is master's harness, surfaced by merging master. Co-Authored-By: Claude Opus 5 (1M context) --- .../lib/SimulatorWindowModeVerifier.java | 51 +++++++++++++++++- .../javase-single-component-inspector.png | Bin 55098 -> 61490 bytes 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/scripts/javase/lib/SimulatorWindowModeVerifier.java b/scripts/javase/lib/SimulatorWindowModeVerifier.java index bf8370ec1b5..7ccb56b55d1 100644 --- a/scripts/javase/lib/SimulatorWindowModeVerifier.java +++ b/scripts/javase/lib/SimulatorWindowModeVerifier.java @@ -123,7 +123,8 @@ public static void main(String[] args) { // real failure). BufferedImage image = captureDesktop(); Instant renderDeadline = Instant.now().plusSeconds(30); - while ((isBlankOrFlat(image) || isSingleWindowDeviceMissing(parsed, image)) + while ((isBlankOrFlat(image) || isSingleWindowDeviceMissing(parsed, image) + || isComponentInspectorDetailsEmpty(parsed, image)) && Instant.now().isBefore(renderDeadline)) { Thread.sleep(500); image = captureDesktop(); @@ -197,6 +198,54 @@ private static boolean isBlankOrFlat(BufferedImage image) { return sampleColorCount(image) < 3; } + /** + * True while the component inspector's lower "Component Details" panel is still + * blank. + * + *

That panel populates a moment after the inspector opens, and nothing was + * waiting for it -- the warmup is a fixed floor and the existing predicates only + * ask whether the desktop and the device window have painted. So the capture landed + * on either side of the population depending on how loaded the runner was, and the + * stored reference happens to hold the empty one. A test that photographs a race + * reports its own timing rather than the product: this suite passed and failed on + * successive runs of identical code, which is worse than a wrong expectation because + * it teaches everyone to re-run it.

+ * + *

Same shape as the device-window predicate above: count dark pixels in the + * region the panel occupies, and keep polling until its field labels are there. The + * outer loop has a deadline, so a panel that genuinely never populates still fails + * on the assertion that follows rather than hanging.

+ */ + private static boolean isComponentInspectorDetailsEmpty(Args args, BufferedImage image) { + if (!"component-inspector".equals(args.scenario)) { + return false; + } + int xMax = Math.min(image.getWidth(), 700); + int yMin = Math.min(image.getHeight(), 660); + int yMax = Math.min(image.getHeight(), 960); + if (xMax <= 0 || yMax <= yMin) { + return false; + } + int darkPixels = 0; + for (int y = yMin; y < yMax; y++) { + for (int x = 0; x < xMax; x++) { + int rgb = image.getRGB(x, y); + int r = (rgb >> 16) & 0xff; + int g = (rgb >> 8) & 0xff; + int b = rgb & 0xff; + if (r < 90 && g < 90 && b < 90) { + darkPixels++; + } + } + } + // Measured on the two real captures rather than guessed: the populated panel + // gives 3712 dark pixels in this region, the empty one 1512 -- the empty state is + // not blank, it still carries the tab label, the borders and the overflow button, + // which is why a naive "is it flat" test does not separate them. The midpoint is + // clear of both by a wide margin. + return darkPixels < 2500; + } + private static boolean isSingleWindowDeviceMissing(Args args, BufferedImage image) { int darkPixels = countSingleWindowDevicePixels(args, image); return darkPixels >= 0 && darkPixels < minimumSingleWindowDevicePixels(args); diff --git a/scripts/javase/screenshots/javase-single-component-inspector.png b/scripts/javase/screenshots/javase-single-component-inspector.png index 11b57b43dcdf030df61a88d6d1e1b9a7dca575ad..a19089a3756e55dd7e719f46a8e2d3e353ee896b 100644 GIT binary patch literal 61490 zcmb5WcR&-_7dFhU1=j-WA|Qx>4HTq_2$8mkhz$^_QWeBVm(XieRF)n^1SP;KDjlQ- z2t}m@)F2|A5QRXb_fE)nCzAl~Z}Tee z+}YEY+1R$kvaxOQ+VU&-8(SeO9yYeyZ0Am^>3dlWlYHMew3-Wf#V%_Yh%JQP6-(Q{ z&-97gA=g91f}>lOW%$j9OmYREycN-SeJpjkmYn+Z`{pe&QbAScMAQWzY&&LNo1iQ>g}NWoY{m z5@&qVQp=cc$AXf@S1d0BG<_*XV+ z_7A&0e4sGW`EugKj~^*mwr>uLf!Kyz?S;tA&<}`~jcsC&_NWmPwxHN%U#b!owhuj~ z--Q0@+j4Y#X7gNM#tu}(lX|fs0WXZ}8&3zc$0WuR^~bj`a37tVwfnTUoA6J?DG|;+ z{d8C=!v4oW!hrsAspIvY6t9lpYFe5opL2SV##J5Yvh|N|uJ;T&N;u;4xwo);+G8VP zPx}bXmUb|*I@3Vs@~(9I+j|E&Q7Z3F_Nnqit5QH~Sf?-UMR3__Vb4eBwNfQGQE8m? zc2OHScS7ibhk^S$V{8glFVjjzu4T%~LMmjz!(L0qKp!=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P9V5&;ig1@bp zev71#l++7Uid~}qG?lI&{x(1}aTV7M$+8o?N*JHqAI(E3JsD;4Z~Wo>Qh#kIbiZ7-*ccJ8vrL%`@Y0KJ$3Bg~eyx%4n zIEO8t&v6x9hGYo2?{B03vKw7c5b<8?pG0#|I7oCMm+6p%~*M-&!+e0*QcdOtYtN(-D<16w~B3$t-cE#Fo8#}LC zm*6ouYG`>qF%%O+F|jHpHi(K&;zX>N2%8fbWFncW{P1X^u9zsXCaTSe!u#BB6BuLy zj(sp2`HbIYINPOfcRPwcE`8#$Z-k*uOGVw zZ&AYA`Jmk`MODO2H^W7K@Rfe8s06r)vykZS)vq(|j)q+PmFuAAb)pL3-IA?ViHMT9 z3qAxt7gj9fi$I2a1y70mnzNZz+u8xviF3+35}AGq6|xo;1UKEhLfvx`p5`_xg?_$w z6N`ThenoZ6&%Hyb>18$N$K8?URz`Fy1bb-ed_*T|+*zeXG@$luZYI{(nb}5+eWr From b96e68e05f0bad357dacf92e238b01c8c23647b8 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 00:03:36 +0700 Subject: [PATCH 53/96] Take the invalid-key decision inside the lock, and add the header the gate wants AndroidSecureStorage decided a key was invalid in a catch OUTSIDE the critical section that used it. So a delayed caller could reset a key that was no longer the one that failed it: another caller had already reset, a writer had created a fresh key and committed ciphertext under it, and this one then deleted that new key and cleared every stored value -- destroying data written after the failure it was reacting to. Both tiers now decide and reset inside the same lock. The interrupted-attestation branch in IOSDeviceIntegrity had the same ordering bug the reset path did: it removed KEY_ATTEST_STARTED whether or not KEY_ID actually went, so a keychain that refused the identifier left an outcome-unknown key looking like one that was never submitted. The next request handed it to attestKey again -- and if Apple had already consumed the original submission, that burns another rate-limited attempt and drops the device into invalid-key recovery for a reason that was never true. And SimulatorWindowModeVerifier gets the copyright header. It never had one; touching it in this PR is what brought it into the gate's scope. Co-Authored-By: Claude Opus 5 (1M context) --- .../impl/android/AndroidSecureStorage.java | 104 ++++++++++-------- .../impl/ios/IOSDeviceIntegrity.java | 16 ++- .../lib/SimulatorWindowModeVerifier.java | 22 ++++ 3 files changed, 97 insertions(+), 45 deletions(-) diff --git a/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java b/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java index 3eb5d35a9f8..727e06aede5 100644 --- a/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java +++ b/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java @@ -226,37 +226,45 @@ public boolean set(String account, String value) { // reported success while storing ciphertext under a key that no longer // exists -- unreadable forever, and silently so. synchronized (PLAIN_KEY_LOCK) { - SecretKey key = plainKey(true); - if (key == null) { + // The invalid-key DECISION is taken in here too, not in a catch outside + // the lock. Deciding out there let a delayed caller reset a key that was + // no longer the one that failed it: another caller had already reset, a + // writer had created a fresh key and committed ciphertext under it, and + // this one then deleted that new key and wiped every stored value -- + // destroying data written after the failure it was reacting to. + try { + SecretKey key = plainKey(true); + if (key == null) { + return false; + } + Cipher c = Cipher.getInstance("AES/GCM/NoPadding"); + c.init(Cipher.ENCRYPT_MODE, key); + byte[] enc = c.doFinal(value.getBytes("UTF-8")); + SharedPreferences prefs = plainPrefs(); + if (prefs == null) { + return false; + } + // commit(), not apply(): apply() is asynchronous, so the write could + // land on disk after a reset that ran once this lock was released -- + // storing ciphertext under a key that had already been deleted. + // Holding the lock is only atomic if the persist finishes inside it. + return prefs.edit() + .putString(account, Base64.encodeToString(c.getIV(), Base64.NO_WRAP) + + ":" + Base64.encodeToString(enc, Base64.NO_WRAP)) + .commit(); + } catch (InvalidKeyException e) { + // Includes KeyPermanentlyInvalidatedException. + resetPlainKey(); return false; - } - Cipher c = Cipher.getInstance("AES/GCM/NoPadding"); - c.init(Cipher.ENCRYPT_MODE, key); - byte[] enc = c.doFinal(value.getBytes("UTF-8")); - SharedPreferences prefs = plainPrefs(); - if (prefs == null) { + } catch (UnrecoverableKeyException e) { + // Handled like an invalid key rather than falling into the generic + // catch: leaving the unusable alias installed made every later write + // return false for good, and only a read happened to clear it -- so + // an app that only ever writes could never store anything again. + resetPlainKey(); return false; } - // commit(), not apply(): apply() is asynchronous, so the write could land - // on disk after a reset that ran once this lock was released -- storing - // ciphertext under a key that had already been deleted. Holding the lock - // is only atomic if the persist finishes inside it. - return prefs.edit() - .putString(account, Base64.encodeToString(c.getIV(), Base64.NO_WRAP) - + ":" + Base64.encodeToString(enc, Base64.NO_WRAP)) - .commit(); } - } catch (InvalidKeyException e) { - // Includes KeyPermanentlyInvalidatedException. - resetPlainKey(); - return false; - } catch (UnrecoverableKeyException e) { - // Handled like an invalid key rather than falling into the generic catch: - // leaving the unusable alias installed made every later write return false - // for good, and only a read happened to clear it -- so an app that only ever - // writes could never store anything again. - resetPlainKey(); - return false; } catch (Throwable t) { Log.e(t); return false; @@ -296,26 +304,34 @@ public String get(String account) { // Same reasoning as set(): a reset landing mid-read would otherwise // invalidate the key between the lookup and the decrypt. synchronized (PLAIN_KEY_LOCK) { - SecretKey key = plainKey(false); - if (key == null) { + // The invalid-key DECISION is taken in here too, not in a catch + // outside the lock. Deciding out there let a delayed caller reset a key + // that was no longer the one that failed it: another caller had already + // reset, a writer had created a fresh key and committed ciphertext under + // it, and this one then deleted that new key and wiped every stored + // value -- destroying data written after the failure it was reacting to. + try { + SecretKey key = plainKey(false); + if (key == null) { + return null; + } + byte[] iv = Base64.decode(stored.substring(0, sep), Base64.NO_WRAP); + byte[] enc = Base64.decode(stored.substring(sep + 1), Base64.NO_WRAP); + Cipher c = Cipher.getInstance("AES/GCM/NoPadding"); + c.init(Cipher.DECRYPT_MODE, key, new GCMParameterSpec(GCM_TAG_BITS, iv)); + return new String(c.doFinal(enc), "UTF-8"); + } catch (InvalidKeyException e) { + // The key was invalidated out from under us (device-wide credential + // change, or the Samsung 8.0.0 quirk documented on the biometric + // tier). Everything encrypted under it is unrecoverable, so drop + // the key and the ciphertexts rather than failing forever. + resetPlainKey(); + return null; + } catch (UnrecoverableKeyException e) { + resetPlainKey(); return null; } - byte[] iv = Base64.decode(stored.substring(0, sep), Base64.NO_WRAP); - byte[] enc = Base64.decode(stored.substring(sep + 1), Base64.NO_WRAP); - Cipher c = Cipher.getInstance("AES/GCM/NoPadding"); - c.init(Cipher.DECRYPT_MODE, key, new GCMParameterSpec(GCM_TAG_BITS, iv)); - return new String(c.doFinal(enc), "UTF-8"); } - } catch (InvalidKeyException e) { - // The key was invalidated out from under us (device-wide credential - // change, or the Samsung 8.0.0 quirk documented on the biometric - // tier). Everything encrypted under it is unrecoverable, so drop - // the key and the ciphertexts rather than failing forever. - resetPlainKey(); - return null; - } catch (UnrecoverableKeyException e) { - resetPlainKey(); - return null; } catch (Throwable t) { Log.e(t); return null; diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index f750f3d74f7..10d433ca1bb 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -510,7 +510,21 @@ AsyncResource requestToken(String nonce) { // spent key is rejected and the failure repeats on every request // until something resets, so discard it and start clean. One // rate-limited key is spent either way; this way the device recovers. - store.remove(KEY_ID); + // Ordered like resetLocked, and for the same reason: the markers + // that make a surviving key terminal go only once the key itself is + // confirmed gone. Removing KEY_ATTEST_STARTED while the keychain + // refused KEY_ID left an outcome-unknown key looking like one that + // was never submitted, so the next request handed it to attestKey + // again -- and if Apple had already consumed the original submission + // that burns another rate-limited attempt and drops the device into + // invalid-key recovery for a reason that was never true. + if (!store.remove(KEY_ID)) { + bootstrapInFlight = false; + failResource(r, "App Attest could not discard the key whose " + + "attestation was interrupted; the keychain refused the " + + "deletion"); + return r; + } store.remove(KEY_STATE); store.remove(KEY_ATTEST_STARTED); // The discarded key is the one the in-memory marker named. diff --git a/scripts/javase/lib/SimulatorWindowModeVerifier.java b/scripts/javase/lib/SimulatorWindowModeVerifier.java index 7ccb56b55d1..faea98c11d4 100644 --- a/scripts/javase/lib/SimulatorWindowModeVerifier.java +++ b/scripts/javase/lib/SimulatorWindowModeVerifier.java @@ -1,3 +1,25 @@ +/* + * Copyright (c) 2026, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ package com.codenameone.examples.javase.tests; import javax.imageio.ImageIO; From f35d46e376643aae1e79aa8f18e4b75cc62a663e Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 00:10:07 +0700 Subject: [PATCH 54/96] Refuse transport-owned headers as the token header Host, Content-Length, Transfer-Encoding and the hop-by-hop names belong to the HTTP transport, not to the caller. Depending on the platform a value put in one is overwritten from the request being sent, refused, or -- for the hop-by-hop family -- stripped in transit by a proxy behaving exactly as specified. None of which is visible on the client: attach() returns having set the header and reports success. The developer sees a working call and a backend insisting they are unauthenticated, which is a long way from the cause, so this is refused at configuration time rather than left to be discovered. An exact list, not a prefix match, so an app's own X-Connection-Id is fine. Co-Authored-By: Claude Opus 5 (1M context) --- .../security/shield/ShieldConfig.java | 38 ++++++++++++++++++- .../security/shield/ShieldApiTest.java | 29 ++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java b/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java index 11a28cbd48f..213ad9cb7d8 100644 --- a/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java +++ b/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java @@ -78,18 +78,54 @@ public ShieldConfig endpoint(String url) { /// @throws IllegalArgumentException if the name cannot carry a token safely public ShieldConfig tokenHeader(String name) { if (name != null && name.length() > 0) { - if ("content-type".equals(ShieldHosts.normalize(name))) { + String normalized = ShieldHosts.normalize(name); + if ("content-type".equals(normalized)) { throw new IllegalArgumentException("Content-Type cannot carry the " + "attestation token: it is not stored as an ordinary header, so it " + "cannot be cleared when a request redirects off a protected host, " + "and the token would follow the redirect. Use a header of your " + "own, or leave the default " + DEFAULT_TOKEN_HEADER + "."); } + for (int i = 0; i < TRANSPORT_HEADERS.length; i++) { + if (TRANSPORT_HEADERS[i].equals(normalized)) { + throw new IllegalArgumentException(name + " cannot carry the " + + "attestation token: it is connection or framing metadata, " + + "which the HTTP transport owns. Depending on the platform it " + + "is overwritten, refused, or acted on -- so attach() would " + + "report success while the backend received a request with no " + + "token, or a malformed one. Use a header of your own, or " + + "leave the default " + DEFAULT_TOKEN_HEADER + "."); + } + } this.tokenHeader = name; } return this; } + /// Header names the transport owns, so an attestation token put in one does not + /// arrive as a header at all. + /// + /// Three families, all lower-cased for comparison because header names are + /// case-insensitive: + /// + /// - framing (`content-length`, `transfer-encoding`) -- the transport computes these + /// from the body it is about to send, and a value that disagrees is either + /// discarded or produces a request the server rejects outright; + /// - routing (`host`) -- this selects the virtual host, so overwriting it sends the + /// request somewhere else entirely; + /// - hop-by-hop (`connection`, `keep-alive`, `proxy-connection`, `te`, `trailer`, + /// `upgrade`) -- defined to be consumed by the next hop and not forwarded, so the + /// token would be stripped in transit by a proxy that is behaving correctly. + /// + /// Refused rather than warned about, because the failure has no symptom on the + /// client: `attach()` returns having set the header, and the request reaches the + /// backend without a usable token. The developer sees a working call and a backend + /// that says they are unauthenticated. + private static final String[] TRANSPORT_HEADERS = { + "host", "content-length", "transfer-encoding", "connection", + "keep-alive", "proxy-connection", "te", "trailer", "upgrade" + }; + /// The failure mode applied to hosts registered without an explicit one. public ShieldConfig defaultFailureMode(FailureMode mode) { if (mode != null) { diff --git a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java index 6b88ebb37dc..58ec2408d01 100644 --- a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java +++ b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java @@ -410,6 +410,35 @@ void contentTypeIsRefusedAsTheTokenHeader() { new ShieldConfig().getTokenHeader()); } + /// Headers the transport owns are refused too. + /// + /// Framing and routing values are computed by the platform from the request it is + /// about to send, and hop-by-hop names are defined to be consumed by the next hop + /// and not forwarded. A token in any of them is overwritten, refused, or stripped in + /// transit -- and none of that is visible on the client, where attach() returns + /// having set the header. The developer sees a working call and a backend insisting + /// they are unauthenticated, which is a long way from the cause. + @Test + void transportOwnedHeadersAreRefusedAsTheTokenHeader() { + String[] refused = { + "Host", "Content-Length", "Transfer-Encoding", "Connection", + "Keep-Alive", "Proxy-Connection", "TE", "Trailer", "Upgrade" + }; + for (int i = 0; i < refused.length; i++) { + final String name = refused[i]; + assertThrows(IllegalArgumentException.class, + () -> new ShieldConfig().tokenHeader(name), + name + " is transport metadata and cannot carry the token"); + assertThrows(IllegalArgumentException.class, + () -> new ShieldConfig().tokenHeader(name.toLowerCase()), + "and the check is case-insensitive, like the header itself"); + } + // A name that merely looks similar is not refused: the list is exact, not a + // prefix match, or an app's own X-Connection-Id would be rejected for no reason. + assertEquals("X-Connection-Id", + new ShieldConfig().tokenHeader("X-Connection-Id").getTokenHeader()); + } + // --- guard composition ---------------------------------------------- @Test From a6060b873de0c44996891ef04daa90c4fb75b715 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 00:18:02 +0700 Subject: [PATCH 55/96] Clear the start marker before rolling back, and stop re-reporting an unchanged signal A deadline write that failed rolled the state back to new while leaving KEY_ATTEST_STARTED in place. Apple had already ACCEPTED that attestation, so the key was good -- but that combination reads as an interrupted attestation of unknown outcome, whose recovery is to discard the key and generate another. A failing timestamp write therefore cost a rate-limited hardware key on every attempt rather than a retry. The marker now goes first, so the worst case is re-attesting a key we still hold. ShieldSignals dropped nothing on a repeat, and AppShield.getSignals() re-adds everything collectSignals() returns -- so a signalRaised() listener that refreshes its view by calling getSignals() re-raised every signal it had just been told about, and notified itself. That is a spin, not a leak, and it is the natural way to write such a listener. A detector on a timer had the milder version: one runnable per poll per signal onto the EDT, an unbounded queue behind a bus documented as bounded. MAX_SIGNALS was bounding the wrong thing. An identical observation now notifies nobody; a changed severity or detail still does, because the detail is what tells one accessibility service from another under a single id. Co-Authored-By: Claude Opus 5 (1M context) --- .../security/shield/ShieldSignals.java | 22 +++++++++- .../impl/ios/IOSDeviceIntegrity.java | 16 +++++++- .../security/shield/ShieldApiTest.java | 41 +++++++++++++++++++ 3 files changed, 76 insertions(+), 3 deletions(-) diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldSignals.java b/CodenameOne/src/com/codename1/security/shield/ShieldSignals.java index 51116188873..8a61f065e73 100644 --- a/CodenameOne/src/com/codename1/security/shield/ShieldSignals.java +++ b/CodenameOne/src/com/codename1/security/shield/ShieldSignals.java @@ -54,7 +54,19 @@ public static void add(ShieldSignal signal) { synchronized (signals) { boolean replaced = false; for (int i = 0; i < signals.size(); i++) { - if (((ShieldSignal) signals.elementAt(i)).getId().equals(signal.getId())) { + ShieldSignal existing = (ShieldSignal) signals.elementAt(i); + if (existing.getId().equals(signal.getId())) { + // Nothing new to say. Re-reporting an identical observation is the + // normal case, not an edge one: AppShield.getSignals() re-adds + // everything collectSignals() returns, so a listener that refreshes + // its view by calling getSignals() notified itself, forever. Even + // without that, a detector polling on a timer queued a runnable per + // poll per signal onto the EDT -- an unbounded queue behind a bus + // whose whole selling point is that it is bounded. + if (existing.getSeverity() == signal.getSeverity() + && sameDetail(existing.getDetail(), signal.getDetail())) { + return; + } signals.setElementAt(signal, i); replaced = true; break; @@ -73,6 +85,14 @@ public static void add(ShieldSignal signal) { notifyListeners(signal); } + /// Whether two observations of one signal say the same thing. + /// + /// The detail is what distinguishes "accessibility service X" from "service Y" under + /// one id, so a change in it is a new observation and a repeat of it is not. + private static boolean sameDetail(String a, String b) { + return a == null ? b == null : a.equals(b); + } + /// Convenience overload for the common case. public static void add(String id, int severity, String detail) { add(new ShieldSignal(id, severity, detail)); diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index 10d433ca1bb..5b54af799db 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -788,8 +788,20 @@ public static void nativeAttestationReady(final int requestId, final String atte // registrationGraceRemaining() reads the window as already expired, so // the very next request promotes the key to attested and asserts against // a key no backend has acknowledged -- the first-use rejection and - // pointless key reset this state exists to prevent. Roll back to new so - // the key is attested again rather than used prematurely. + // pointless key reset this state exists to prevent. + // + // The start marker goes FIRST, and that ordering is the whole fix. Apple + // has already accepted this attestation, so the key is good; rolling the + // state back to new while KEY_ATTEST_STARTED was still there made the + // next request read it as an interrupted attestation of unknown outcome + // -- which discards the key and generates another rate-limited one. A + // deadline write that keeps failing then burns a fresh hardware key on + // every single attempt, which is the opposite of what a rollback is for. + // Clearing it first means the worst case is re-attesting a key we still + // hold, not replacing it. + store.remove(KEY_ATTEST_STARTED); + // Roll back to new so the key is attested again rather than used + // prematurely. if (!store.set(KEY_STATE, STATE_NEW)) { // The rollback failed too, so the key would sit pending with no // deadline and be promoted on the next request. Discard the identity diff --git a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java index 58ec2408d01..8d2e5649395 100644 --- a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java +++ b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java @@ -392,6 +392,47 @@ void severityIsClampedToTheDocumentedRange() { assertEquals(0, new ShieldSignal("x", -5, null).getSeverity()); } + /// Re-reporting an identical observation notifies nobody. + /// + /// AppShield.getSignals() re-adds everything collectSignals() returns, so a listener + /// that refreshes its view by calling getSignals() notified itself -- forever. And + /// a detector polling on a timer queued a runnable per poll per signal onto the EDT, + /// which is an unbounded queue behind a bus whose selling point is that it is + /// bounded. A repeat carries no information; a change does. + @Test + void anUnchangedSignalDoesNotNotifyAgain() { + final int[] notified = {0}; + ShieldListener listener = new ShieldListener() { + public void signalRaised(ShieldSignal signal) { + notified[0]++; + } + public void tokenRefreshed(ShieldToken token) { + } + public void statusChanged(ShieldStatus status) { + } + }; + AppShield.addListener(listener); + try { + String id = "test.repeat." + System.nanoTime(); + ShieldSignals.add(id, 50, "same"); + int afterFirst = notified[0]; + assertTrue(afterFirst >= 1, "the first observation has to be reported"); + + ShieldSignals.add(id, 50, "same"); + ShieldSignals.add(id, 50, "same"); + assertEquals(afterFirst, notified[0], + "an identical repeat says nothing new and must not notify"); + + // A changed severity or detail IS a new observation. + ShieldSignals.add(id, 90, "same"); + assertEquals(afterFirst + 1, notified[0], "a raised severity is news"); + ShieldSignals.add(id, 90, "different"); + assertEquals(afterFirst + 2, notified[0], "and so is a changed detail"); + } finally { + AppShield.removeListener(listener); + } + } + @Test void contentTypeIsRefusedAsTheTokenHeader() { // ConnectionRequest.addRequestHeader special-cases Content-Type into the From 0938019ce2b603b1eec5106d87ebe37856e3afdb Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 00:20:15 +0700 Subject: [PATCH 56/96] Leave the javase simulator screenshot suite exactly as master has it Backing out my change to it, because I read the race backwards. The capture on this branch's first post-merge run showed the inspector's lower Component Details panel populated where the stored reference has it empty, so I made the harness wait for it to populate and adopted the populated capture. The next run captured the EMPTY panel again -- the same code, the other side of the same race -- which means empty is the usual state and populated is the outlier, not the other way round. My wait then cost thirty seconds on every run and captured the empty state anyway, and my reference made every ordinary run fail. So scripts/javase is byte-identical to master again. The race is real and it is master's: `javase-single-component-inspector` photographs a panel that sometimes populates before the capture and usually does not, and nothing waits for either. Deciding which state that test should assert is a call about the inspector's behaviour, not about App Shield, and guessing at it from inside this PR is how I got here. Co-Authored-By: Claude Opus 5 (1M context) --- .../lib/SimulatorWindowModeVerifier.java | 73 +----------------- .../javase-single-component-inspector.png | Bin 61490 -> 55098 bytes 2 files changed, 1 insertion(+), 72 deletions(-) diff --git a/scripts/javase/lib/SimulatorWindowModeVerifier.java b/scripts/javase/lib/SimulatorWindowModeVerifier.java index faea98c11d4..bf8370ec1b5 100644 --- a/scripts/javase/lib/SimulatorWindowModeVerifier.java +++ b/scripts/javase/lib/SimulatorWindowModeVerifier.java @@ -1,25 +1,3 @@ -/* - * Copyright (c) 2026, Codename One and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Codename One designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Codename One through http://www.codenameone.com/ if you - * need additional information or have any questions. - */ package com.codenameone.examples.javase.tests; import javax.imageio.ImageIO; @@ -145,8 +123,7 @@ public static void main(String[] args) { // real failure). BufferedImage image = captureDesktop(); Instant renderDeadline = Instant.now().plusSeconds(30); - while ((isBlankOrFlat(image) || isSingleWindowDeviceMissing(parsed, image) - || isComponentInspectorDetailsEmpty(parsed, image)) + while ((isBlankOrFlat(image) || isSingleWindowDeviceMissing(parsed, image)) && Instant.now().isBefore(renderDeadline)) { Thread.sleep(500); image = captureDesktop(); @@ -220,54 +197,6 @@ private static boolean isBlankOrFlat(BufferedImage image) { return sampleColorCount(image) < 3; } - /** - * True while the component inspector's lower "Component Details" panel is still - * blank. - * - *

That panel populates a moment after the inspector opens, and nothing was - * waiting for it -- the warmup is a fixed floor and the existing predicates only - * ask whether the desktop and the device window have painted. So the capture landed - * on either side of the population depending on how loaded the runner was, and the - * stored reference happens to hold the empty one. A test that photographs a race - * reports its own timing rather than the product: this suite passed and failed on - * successive runs of identical code, which is worse than a wrong expectation because - * it teaches everyone to re-run it.

- * - *

Same shape as the device-window predicate above: count dark pixels in the - * region the panel occupies, and keep polling until its field labels are there. The - * outer loop has a deadline, so a panel that genuinely never populates still fails - * on the assertion that follows rather than hanging.

- */ - private static boolean isComponentInspectorDetailsEmpty(Args args, BufferedImage image) { - if (!"component-inspector".equals(args.scenario)) { - return false; - } - int xMax = Math.min(image.getWidth(), 700); - int yMin = Math.min(image.getHeight(), 660); - int yMax = Math.min(image.getHeight(), 960); - if (xMax <= 0 || yMax <= yMin) { - return false; - } - int darkPixels = 0; - for (int y = yMin; y < yMax; y++) { - for (int x = 0; x < xMax; x++) { - int rgb = image.getRGB(x, y); - int r = (rgb >> 16) & 0xff; - int g = (rgb >> 8) & 0xff; - int b = rgb & 0xff; - if (r < 90 && g < 90 && b < 90) { - darkPixels++; - } - } - } - // Measured on the two real captures rather than guessed: the populated panel - // gives 3712 dark pixels in this region, the empty one 1512 -- the empty state is - // not blank, it still carries the tab label, the borders and the overflow button, - // which is why a naive "is it flat" test does not separate them. The midpoint is - // clear of both by a wide margin. - return darkPixels < 2500; - } - private static boolean isSingleWindowDeviceMissing(Args args, BufferedImage image) { int darkPixels = countSingleWindowDevicePixels(args, image); return darkPixels >= 0 && darkPixels < minimumSingleWindowDevicePixels(args); diff --git a/scripts/javase/screenshots/javase-single-component-inspector.png b/scripts/javase/screenshots/javase-single-component-inspector.png index a19089a3756e55dd7e719f46a8e2d3e353ee896b..11b57b43dcdf030df61a88d6d1e1b9a7dca575ad 100644 GIT binary patch literal 55098 zcmb@u2{_d28$YZ%rA3^MkX@_nBr(>bLMbHsULh~=eeKz{@kDYbKlQAx~8jja6iv}CMKqX zmoHts&cw7gfr)9i&)%KjH%@}KoJ>sjnJ!;cH}tWd>i2)=+G!=|ld!H~Bua`15zRVw z)FSTQNlytZQhe{a43E{Md4WLOA3EVz?XQ!hg41t5Q?$9k6@ez0rLSx40)J&9 zn7+BLh9a8sbxF~CC@;X;G$WXq8q1{cO!~^?ae?0<(-K5Q%Y2fZ179h)ZvHXvXZ@&mE+YV? z@VUnJPlI6if=7o4xta3x_areT3*;>3GjHPq+pI2ZZ)>}K&gxX8y6z^6VM^RY$fN0s zBWwtTg^pgQlCFddlM-RwWKojaOJu2^AyVOY-kgEwkES~`^OI(mQx0}r3WgiR?X3?! zOL_L)&HCiV>vQ}N-}thxkaqKqtR`y#{4>v&p($#S`KGS!hHsNe;!Eef0=@iKf6rME z&Ox`QW}%O^SqM9|FW4%di0>>kik0O$a9?*r9b0Gm*E9Y!s;=A9Xp~+%d@*o9@*3RW zG-pjeax%LYt#4OpYTRyasgh;RcV0#9@=H^A0~a1W;g+3TjPs>FOlzCNIJzc{C_%&R zb5hUcG_~yB51n7l@C-y+_q+99D`=itgk*yyrR$wJJCWI?Xq1mCwyxZFBWF{H@aZo; z={)#ER@}ed*8(ytXHtS|(_HHAZH3_vqfMp4dGDn6+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{(=WEf(WiXzBd`YfS*@clF@z&wH~kVn z|8LJ7AIRx00$I06zbyZHw0D2XLgBZ1EdtDJH7hgajRy0-Keqeo#(ha@G(meB?yt~q zIAhXTHGz&Y%J%C!GPB;m!9bTea$!^Y0o|+Hbc({_%Or6QU$5u9z1?ULp0T52_2_!S z+3?U6xDP^m5C!?}FW39BeqZbbhm-i?A7A1D4)-OV!nnzocQOpTF?!U-}DQ`YB)fNB__D-(tt; z3vm*G=*iSJ!uU3y{2<1x-z~+ze!!SmPY@f&;cCC*ob9p;uRoA~R=lt|9!T!Ez8%_1Hs0Q*wPG+Hh zEJ0pIoJ4=rhnI1_@W`8jdutrLRkiTAziQuUECpK=+Vb1bK>Iv2OqWPasU7T From acf1dd6438c3efbaa22ca4f69554e65363eb7ab0 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 00:26:04 +0700 Subject: [PATCH 57/96] Refuse Cookie as the token header, and finish initializing before saying so Cookie is not transport framing, which is why it missed the first pass, and it fails worse than the names that are: the request overwrites the token itself. initConnection() emits userHeaders, then performOperationComplete() calls setHeader("Cookie", ...) with the generated cookie string under the same name -- so one stored cookie and the default handling, which is the ordinary configuration, is enough. attach() reports success and a fail-closed host sends a protected request with no token on it. And AppShield.init() published `initialized` to claim the job, while every other caller reads that field as "setup is complete". Two meanings on one field, and the gap between them is exactly when the shield can do least: a second init() returned as though everything were up, and a ConnectionRequest starting in that window found no NetworkGuard at all -- no token, no certificate check, and a fail-closed host failing open with nothing installed to object. `initializing` is now separate, a concurrent caller waits on the monitor, and the guard is installed before completion is published. The publish sits in a finally, so an engine that throws releases the waiters instead of parking them: a shield that fails to start must not be able to hang the app. Full core suite: 4727 tests green. Co-Authored-By: Claude Opus 5 (1M context) --- .../codename1/security/shield/AppShield.java | 52 +++++++++++++++---- .../security/shield/ShieldConfig.java | 9 +++- .../security/shield/ShieldApiTest.java | 5 +- 3 files changed, 55 insertions(+), 11 deletions(-) diff --git a/CodenameOne/src/com/codename1/security/shield/AppShield.java b/CodenameOne/src/com/codename1/security/shield/AppShield.java index 015376d12f3..cc009a3dc07 100644 --- a/CodenameOne/src/com/codename1/security/shield/AppShield.java +++ b/CodenameOne/src/com/codename1/security/shield/AppShield.java @@ -106,24 +106,58 @@ private AppShield() { /// inert. Calling it twice is a no-op. public static void init(ShieldConfig cfg) { synchronized (AppShield.class) { + // A concurrent caller WAITS rather than returning early. + // + // `initialized` used to be published before the engine was initialized and + // before the guard was installed, so a second init() returned as though + // setup were complete -- and, worse, a ConnectionRequest starting in that + // window found no network guard at all and sent a protected request with + // neither a token nor a pin check, including for a host configured to fail + // closed. The flag now means what its name says, and the window is closed by + // making anyone who arrives during setup wait for it rather than by making + // them guess. + while (initializing) { + try { + AppShield.class.wait(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return; + } + } if (initialized) { return; } config = cfg == null ? new ShieldConfig() : cfg; - initialized = true; + initializing = true; } - ShieldEngine engine = ShieldEngineRegistry.getEngine(); try { - engine.initialize(contextForEngine(), config); - setStatus(engine.isAvailable() ? ShieldStatus.OK : ShieldStatus.UNPROTECTED); - } catch (Throwable t) { - // A failure inside the engine must not stop the app from starting. - Log.e(t); - setStatus(ShieldStatus.UNPROTECTED); + ShieldEngine engine = ShieldEngineRegistry.getEngine(); + try { + engine.initialize(contextForEngine(), config); + setStatus(engine.isAvailable() ? ShieldStatus.OK : ShieldStatus.UNPROTECTED); + } catch (Throwable t) { + // A failure inside the engine must not stop the app from starting. + Log.e(t); + setStatus(ShieldStatus.UNPROTECTED); + } + // Installed BEFORE initialization is published, so there is no instant at + // which the shield claims to be up and a request can slip past unprotected. + installNetworkGuard(); + } finally { + synchronized (AppShield.class) { + initializing = false; + initialized = true; + AppShield.class.notifyAll(); + } } - installNetworkGuard(); } + /// True while [#init(ShieldConfig)] is between taking the job and finishing it. + /// + /// Separate from `initialized` because the two answer different questions, and + /// conflating them is what let a caller act on a half-built shield. + private static boolean initializing; + /// Hooks the shield into the network stack, which is what makes /// [ShieldConfig#protect(String, HostPolicy)] take effect on ordinary requests. Without it a /// registered host would carry a policy nothing consults. diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java b/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java index 213ad9cb7d8..d149e372b7d 100644 --- a/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java +++ b/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java @@ -123,7 +123,14 @@ public ShieldConfig tokenHeader(String name) { /// that says they are unauthenticated. private static final String[] TRANSPORT_HEADERS = { "host", "content-length", "transfer-encoding", "connection", - "keep-alive", "proxy-connection", "te", "trailer", "upgrade" + "keep-alive", "proxy-connection", "te", "trailer", "upgrade", + // Cookie is not transport framing, but it fails the same way and worse. + // ConnectionRequest emits userHeaders first and THEN calls setHeader("Cookie", + // ...) with the generated cookie string, so with cookie handling on and any + // stored cookie the token is overwritten by the request itself -- after + // attach() has reported success. A fail-closed host would then send a protected + // request with no token and no indication anything went wrong. + "cookie" }; /// The failure mode applied to hosts registered without an explicit one. diff --git a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java index 8d2e5649395..a3df05016d9 100644 --- a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java +++ b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java @@ -463,7 +463,10 @@ void contentTypeIsRefusedAsTheTokenHeader() { void transportOwnedHeadersAreRefusedAsTheTokenHeader() { String[] refused = { "Host", "Content-Length", "Transfer-Encoding", "Connection", - "Keep-Alive", "Proxy-Connection", "TE", "Trailer", "Upgrade" + "Keep-Alive", "Proxy-Connection", "TE", "Trailer", "Upgrade", + // Not framing, but overwritten by the request itself: ConnectionRequest + // emits userHeaders and then sets Cookie from its own cookie store. + "Cookie" }; for (int i = 0; i < refused.length; i++) { final String name = refused[i]; From 4f13127501474ac6bf776fb4521ed5c39b9f895a Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 00:47:42 +0700 Subject: [PATCH 58/96] Wait for initialization at the request paths, and keep an accepted App Attest key Installing the guard before publishing completion is right -- the guard must exist before anything can claim to be protected -- but it means the guard is reachable while `initialized` is still false, and every consumer of that flag read false as "there is no shield here". So the previous fix moved the window rather than closing it: before, a concurrent request found no guard; after, it found the guard and was waved straight through. attach(), headersFor() and fetchToken() now wait on an initialization in progress. They failed differently and all badly: a request sent untouched including for a fail-closed host, a BrowserComponent navigating a protected host unauthenticated, and a NOT_INITIALIZED error the app cannot tell from the real one. The notify is in a finally, so an engine that throws releases the waiters instead of parking a network thread. And the App Attest deadline failure keeps the key. Both earlier attempts sent an accepted key back through attestation -- rolling to STATE_NEW with the start marker present reads as an interrupted attestation and discards it; clearing the marker first only moved the failure, because a STATE_NEW key is submitted to attestKey again and Apple answers invalidKey for a one-time key it has already consumed, which triggers recovery and burns a replacement anyway. Apple accepted this attestation; the key is good; the only thing missing is a timestamp, and a timestamp is not worth a hardware key. It now lives in memory for this process, and after a restart the PENDING key is promoted and used -- the same fallback already applied when a consumer never acknowledges. Also the PMD foreach the gate wanted. Full core suite: 4727 green. Co-Authored-By: Claude Opus 5 (1M context) --- .../codename1/security/shield/AppShield.java | 44 +++++++++++- .../security/shield/ShieldConfig.java | 4 +- .../impl/ios/IOSDeviceIntegrity.java | 71 +++++++++++-------- quality-report.md | 2 +- 4 files changed, 86 insertions(+), 35 deletions(-) diff --git a/CodenameOne/src/com/codename1/security/shield/AppShield.java b/CodenameOne/src/com/codename1/security/shield/AppShield.java index cc009a3dc07..95307edb907 100644 --- a/CodenameOne/src/com/codename1/security/shield/AppShield.java +++ b/CodenameOne/src/com/codename1/security/shield/AppShield.java @@ -158,6 +158,25 @@ public static void init(ShieldConfig cfg) { /// conflating them is what let a caller act on a half-built shield. private static boolean initializing; + /// Blocks until an initialization in progress has finished. Returns at once when + /// none is, which is every call after startup. + /// + /// Safe from a network thread, which is where [#attach(ConnectionRequest)] runs by + /// contract, and it waits on the same monitor `init()` notifies -- so the wait ends + /// when setup does, including when the engine threw and the `finally` released it. + private static void awaitInitialization() { + synchronized (AppShield.class) { + while (initializing) { + try { + AppShield.class.wait(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return; + } + } + } + } + /// Hooks the shield into the network stack, which is what makes /// [ShieldConfig#protect(String, HostPolicy)] take effect on ordinary requests. Without it a /// registered host would carry a policy nothing consults. @@ -285,6 +304,10 @@ public static AsyncResource fetchToken() { /// @param bindingData the data to bind to, typically a digest of the request body public static AsyncResource fetchToken(final String bindingData) { final AsyncResource result = new AsyncResource(); + // Same window as attach(): a caller racing startup would otherwise be told the + // shield was never initialized, which is a lie that lasts milliseconds and an + // error the app has no way to distinguish from the real one. + awaitInitialization(); if (!initialized) { result.error(new ShieldException(ShieldStatus.NOT_INITIALIZED, "AppShield.init(...) has not been called")); @@ -332,7 +355,18 @@ public static void invalidateToken() { /// Honours the host's [FailureMode]: under [FailureMode#OPEN] a token failure leaves the /// request untouched, under [FailureMode#CLOSED] it propagates. public static void attach(ConnectionRequest request) throws ShieldException { - if (request == null || !initialized) { + if (request == null) { + return; + } + // Waits for an initialization already under way rather than treating it as + // "no shield". installNetworkGuard() necessarily runs before init() publishes + // completion -- the guard has to exist before anything can claim to be + // protected -- so a request that started concurrently reaches this method + // through the freshly installed guard while the flag is still false. Returning + // there sent a protected request untouched, including for a host configured to + // fail closed, which is exactly the request that must not go out unprotected. + awaitInitialization(); + if (!initialized) { return; } String url = request.getUrl(); @@ -420,7 +454,13 @@ private static void failOrContinue(HostPolicy policy, ShieldException e) throws /// visible to the framework and cannot be given a token or pinned. public static Hashtable headersFor(String url) { Hashtable out = new Hashtable(); - if (!initialized || url == null) { + if (url == null) { + return out; + } + // As in attach(): during startup this would silently return no headers, and a + // BrowserComponent navigating a protected host would load it unauthenticated. + awaitInitialization(); + if (!initialized) { return out; } if (!policyFor(hostOf(url)).isAttachToken()) { diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java b/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java index d149e372b7d..7c48ca69263 100644 --- a/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java +++ b/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java @@ -86,8 +86,8 @@ public ShieldConfig tokenHeader(String name) { + "and the token would follow the redirect. Use a header of your " + "own, or leave the default " + DEFAULT_TOKEN_HEADER + "."); } - for (int i = 0; i < TRANSPORT_HEADERS.length; i++) { - if (TRANSPORT_HEADERS[i].equals(normalized)) { + for (String reserved : TRANSPORT_HEADERS) { + if (reserved.equals(normalized)) { throw new IllegalArgumentException(name + " cannot carry the " + "attestation token: it is connection or framing metadata, " + "which the HTTP transport owns. Depending on the platform it " diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index 5b54af799db..b6b87b4c5a6 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -181,6 +181,14 @@ final class IOSDeviceIntegrity { /** Guards the whole attest flow so concurrent callers cannot each burn a key. */ private final Object flowLock = new Object(); private long currentBackoff = MIN_BACKOFF_MILLIS; + /** + * When this key's attestation was accepted, held here as well as in the keychain. + * + *

The keychain copy is what survives a restart; this one is what survives the + * keychain refusing the write. Without it an accepted key looked unregistered, and + * every route out of that state cost a rate-limited hardware key.

+ */ + private long pendingSinceInMemory; /** * The throttle deadline, held in memory as well as in the keychain. * @@ -552,6 +560,15 @@ AsyncResource requestToken(String nonce) { private static long registrationGraceRemaining() { String since = SecureStorage.getInstance().get(KEY_PENDING_SINCE); if (since == null || since.length() == 0) { + // The keychain refused the timestamp, and this process remembers when the + // key was accepted. Falling through to 0 here is what made the accepted key + // look unregistered and sent it back through attestation. + IOSDeviceIntegrity live = instance; + if (live != null && live.pendingSinceInMemory > 0) { + long left = REGISTRATION_GRACE_MILLIS + - (System.currentTimeMillis() - live.pendingSinceInMemory); + return left > 0 ? left : 0; + } return 0; } long started; @@ -783,38 +800,32 @@ public static void nativeAttestationReady(final int requestId, final String atte "App Attest could not record its attestation state"); return; } - if (!store.set(KEY_PENDING_SINCE, Long.toString(System.currentTimeMillis()))) { - // Part of the same state transition, not a nicety: with no timestamp, - // registrationGraceRemaining() reads the window as already expired, so - // the very next request promotes the key to attested and asserts against - // a key no backend has acknowledged -- the first-use rejection and - // pointless key reset this state exists to prevent. + long pendingSince = System.currentTimeMillis(); + // Held in memory whatever the keychain does, and set BEFORE the write is + // attempted so the fallback is already in place if it fails. + instance.pendingSinceInMemory = pendingSince; + if (!store.set(KEY_PENDING_SINCE, Long.toString(pendingSince))) { + // The key is KEPT, in the pending state it is already in. Nothing is + // rolled back and nothing is discarded. + // + // Two earlier attempts here were both wrong in the same direction -- + // they sent an accepted key back through attestation. Rolling the state + // to new while KEY_ATTEST_STARTED was present made it read as an + // interrupted attestation, which discards the key and mints another; + // clearing that marker first only moved the failure, because a key in + // STATE_NEW is submitted to attestKey again and Apple answers invalidKey + // for a one-time key it has already consumed -- which triggers recovery + // and burns a replacement anyway. Apple accepted this attestation. The + // key is good. The only thing missing is a timestamp. // - // The start marker goes FIRST, and that ordering is the whole fix. Apple - // has already accepted this attestation, so the key is good; rolling the - // state back to new while KEY_ATTEST_STARTED was still there made the - // next request read it as an interrupted attestation of unknown outcome - // -- which discards the key and generates another rate-limited one. A - // deadline write that keeps failing then burns a fresh hardware key on - // every single attempt, which is the opposite of what a rollback is for. - // Clearing it first means the worst case is re-attesting a key we still - // hold, not replacing it. + // So the timestamp lives in memory for this process (set above, before + // the write was attempted) and registrationGraceRemaining() falls back + // to it. Across a restart the key is found PENDING with no deadline, + // which the request path already handles: it promotes to attested and + // uses it, the same fallback applied when a consumer never acknowledges. + // That is right here too -- the key IS attested with Apple; only the + // backend's confirmation is unknown -- and it costs no rate-limited key. store.remove(KEY_ATTEST_STARTED); - // Roll back to new so the key is attested again rather than used - // prematurely. - if (!store.set(KEY_STATE, STATE_NEW)) { - // The rollback failed too, so the key would sit pending with no - // deadline and be promoted on the next request. Discard the identity - // outright rather than leave a state that reads as ready. - try { - instance.resetLocked(); - } catch (IllegalStateException ignored) { - // resetLocked already advanced the generation and failed the - // waiters; nothing further to do but report to this caller. - } - fail(pending, "App Attest could not record its registration deadline"); - return; - } instance.bootstrapInFlight = false; fail(pending, "App Attest could not record its registration deadline"); instance.failBootstrapWaiters( diff --git a/quality-report.md b/quality-report.md index 67aebc851ce..202c12df5c2 100644 --- a/quality-report.md +++ b/quality-report.md @@ -1,7 +1,7 @@ ## ✅ Continuous Quality Report ### Test & Coverage -- ✅ **Tests:** 2984 total, 0 failed, 0 skipped +- ✅ **Tests:** 4727 total, 0 failed, 0 skipped - ⚠️ Coverage report not generated. ### Static Analysis From 7c5d4c40a6bb9fa8086ed65c0c6aa814d1eb40d6 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 00:58:51 +0700 Subject: [PATCH 59/96] Hold the accepted key's state in memory when the keychain refuses to The pending-state write has the same failure as the deadline write below it, and my previous fix only covered the second one. Apple has already accepted the attestation when this runs; if the keychain refuses to record it, the persisted state still says "new" with a start marker present -- which the next request reads as an interrupted attestation of unknown outcome and answers by discarding the key and minting another rate-limited one. Simply failing the request did not avoid that; it walked straight into it. So the state is held in memory for this process, and the state read at the top of requestToken prefers what this process knows over what the keychain managed to store. The promotion out of pending is covered too: a refused STATE_ATTESTED write would otherwise send an accepted key back to attestKey on the next request. The general rule, now stated at each of these sites: the keychain copy is what survives a restart, and the in-memory copy is what survives the keychain refusing the write. Both are needed, and the one thing that must never happen is an accepted one-time key being submitted again -- Apple answers invalidKey, which spends the one-shot recovery and mints a replacement. Cleared in resetLocked with everything else the identity owned. Co-Authored-By: Claude Opus 5 (1M context) --- .../impl/ios/IOSDeviceIntegrity.java | 50 +++++++++++++++++-- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index b6b87b4c5a6..513cfc2cea5 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -189,6 +189,20 @@ final class IOSDeviceIntegrity { * every route out of that state cost a rate-limited hardware key.

*/ private long pendingSinceInMemory; + /** + * The key whose state this process holds in memory, when the keychain refused to + * hold it. + * + *

Apple accepts an attestation once. If the write recording that fact fails, the + * persisted state still describes a key that has never been submitted -- and acting + * on it re-submits an already-consumed one-time key, which Apple answers with + * invalidKey, which spends the one-shot recovery and mints a replacement. Every + * route out of a failed state write costs a rate-limited hardware key unless + * something remembers what actually happened.

+ */ + private String inMemoryStateKeyId; + /** The state {@link #inMemoryStateKeyId} is really in. */ + private String inMemoryState; /** * The throttle deadline, held in memory as well as in the keychain. * @@ -313,6 +327,10 @@ private void resetLocked(boolean keepSpentMarker) { if (idGone && stateGone) { store.remove(KEY_ATTEST_STARTED); attestAnsweredForKey = null; + // The identity is gone, so anything this process remembered about it is too. + inMemoryStateKeyId = null; + inMemoryState = null; + pendingSinceInMemory = 0L; discardFailed = false; if (!keepSpentMarker) { // Checked, and the in-memory flag follows what the keychain actually @@ -455,6 +473,13 @@ AsyncResource requestToken(String nonce) { SecureStorage store = SecureStorage.getInstance(); String keyId = store.get(KEY_ID); String state = store.get(KEY_STATE); + // What this process knows wins over what the keychain managed to store. The + // keychain copy is what survives a restart; this one is what survives the + // keychain refusing the write, and without it an accepted key reads as one + // that was never submitted. + if (keyId != null && keyId.equals(inMemoryStateKeyId) && inMemoryState != null) { + state = inMemoryState; + } if (STATE_PENDING.equals(state) && keyId != null && keyId.length() > 0) { if (registrationGraceRemaining() > 0) { // The key is attested with Apple but no backend has confirmed @@ -469,7 +494,14 @@ AsyncResource requestToken(String nonce) { // not participate in acknowledgement at all, or its registration // call was lost. Assume registered rather than re-attesting on // every request forever. - store.set(KEY_STATE, STATE_ATTESTED); + if (!store.set(KEY_STATE, STATE_ATTESTED)) { + // Same reasoning one step later: a promotion the keychain refused + // must not send an accepted key back to attestKey on the next + // request. Held in memory for this process; a restart re-reads + // whatever the keychain does hold. + inMemoryStateKeyId = keyId; + inMemoryState = STATE_ATTESTED; + } store.remove(KEY_PENDING_SINCE); state = STATE_ATTESTED; } @@ -791,9 +823,19 @@ public static void nativeAttestationReady(final int requestId, final String atte } SecureStorage store = SecureStorage.getInstance(); if (!store.set(KEY_STATE, STATE_PENDING)) { - // The key is attested with Apple but we cannot record that. Reporting - // success would leave the next request attesting the same key again, - // against Apple's rate limit, forever. + // The key is attested with Apple and the keychain will not record it. + // + // Reporting success would leave the next request attesting the same key + // again -- but so did simply failing, because the persisted state still + // says "new" with a start marker, which is read as an interrupted + // attestation and discards the key for another rate-limited one. Every + // route out of here spent a key until something remembered that Apple + // had already answered. So the state is held in memory for this process, + // exactly as the deadline below is, and this request is failed so the + // caller retries into a state that now describes the key correctly. + instance.inMemoryStateKeyId = pending.keyId; + instance.inMemoryState = STATE_PENDING; + instance.pendingSinceInMemory = System.currentTimeMillis(); instance.bootstrapInFlight = false; fail(pending, "App Attest could not record its attestation state"); instance.failBootstrapWaiters( From 3a756d3798941cd681dd396d62485ee48b707e2d Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 01:05:13 +0700 Subject: [PATCH 60/96] Retry the Windows clean-target Maven steps through a Central 429 The job died during dependency RESOLUTION -- "maven-clean-plugin:pom:3.2.0 (absent): status code: 429, reason phrase: Too Many Requests" -- before a line of this project was compiled. Nothing about it involves the code under test, and a re-run cannot be relied on to draw a runner whose address Central is not currently throttling. The same file already retries the Ninja install for an unrelated transient, with a comment naming the mechanism, so this follows that shape deliberately: a plain foreach with $LASTEXITCODE rather than a function and argument splatting, because that shape is proven on these runners and a workflow step is a poor place to discover a PowerShell quoting difference. Backoff is 30s then 90s rather than the 5 used for the pip blip, because a rate limit has to be waited out rather than retried through. Not App Shield's, and not something this branch changed -- but it is the branch's red, and re-running it would have been treating a named mechanism as noise. Co-Authored-By: Claude Opus 5 (1M context) --- .github/workflows/parparvm-tests-windows.yml | 37 +++++++++++++++++--- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/.github/workflows/parparvm-tests-windows.yml b/.github/workflows/parparvm-tests-windows.yml index 921efa9cd66..6c8071107d7 100644 --- a/.github/workflows/parparvm-tests-windows.yml +++ b/.github/workflows/parparvm-tests-windows.yml @@ -192,10 +192,39 @@ jobs: working-directory: vm shell: pwsh run: | - mvn -B clean package -pl JavaAPI -am -DskipTests - # Single-quote the -D args: PowerShell otherwise mangles the dotted - # property name (splitting it at the '.'). - mvn -B test -pl tests -am '-Dtest=CleanTargetIntegrationTest' '-Dsurefire.failIfNoSpecifiedTests=false' + # Retried, in the same spirit as the Ninja install above: Maven Central + # answers 429 Too Many Requests under load and the build dies during + # dependency RESOLUTION, before a line of this project is compiled. Nothing + # about the failure involves the code under test, and a fresh runner IP is + # not something a re-run can be relied on to produce. Backoff is in tens of + # seconds rather than the 5 used for the pip blip, because a rate limit needs + # waiting out rather than retrying through. + # Written in the same plain loop shape as the Ninja install above rather + # than with a function and splatting, because that shape is already proven + # on these runners and this is not the place to find out about a quoting + # difference. + $ok = $false + foreach ($delay in 0, 30, 90) { + if ($delay -gt 0) { + Write-Host "mvn failed; retrying in $delay s..." + Start-Sleep -Seconds $delay + } + mvn -B clean package -pl JavaAPI -am -DskipTests + if ($LASTEXITCODE -eq 0) { $ok = $true; break } + } + if (-not $ok) { throw "mvn clean package failed after all retries" } + $ok = $false + foreach ($delay in 0, 30, 90) { + if ($delay -gt 0) { + Write-Host "mvn failed; retrying in $delay s..." + Start-Sleep -Seconds $delay + } + # Single-quote the -D args: PowerShell otherwise mangles the dotted + # property name (splitting it at the '.'). + mvn -B test -pl tests -am '-Dtest=CleanTargetIntegrationTest' '-Dsurefire.failIfNoSpecifiedTests=false' + if ($LASTEXITCODE -eq 0) { $ok = $true; break } + } + if (-not $ok) { throw "mvn test failed after all retries" } env: JDK_8_HOME: ${{ env.JDK_8_HOME }} JDK_11_HOME: ${{ env.JDK_11_HOME }} From 221ede6c2d6d1d91f13835b749f6e3365837ea18 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 01:15:47 +0700 Subject: [PATCH 61/96] Hand back the attestation even when its metadata cannot be stored Apple attests a key once. Failing the request on a storage error threw that object away -- and it is the only thing in this callback that cannot be produced again, so the backend never received the key to register. After the grace window the client promotes it locally and starts asserting against a key the backend has never seen, which is rejected, which resets, which spends another rate-limited key. Reporting "a timestamp could not be stored" cost strictly more than reporting nothing at all. Both storage-failure branches now leave the critical section and fall through to the same succeed() the normal path uses, keeping the pending state in memory, so later callers take the registration-in-progress path exactly as they would have. Queued waiters get the same "retry shortly" they get on the normal path rather than a storage error, because that is what is actually true for them. This is the fourth report on these two writes and the previous three all treated the key as the thing worth saving. It was, and so is the attestation object -- which was being discarded the whole time to report the failure. Co-Authored-By: Claude Opus 5 (1M context) --- .../impl/ios/IOSDeviceIntegrity.java | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index 513cfc2cea5..92da1dbfb66 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -816,6 +816,10 @@ public static void nativeAttestationReady(final int requestId, final String atte // rejects it, the app calls DeviceIntegrity.resetAttestation() instead. // Same lock as the reset, for the same reason as nativeKeyGenerated: the // staleness check and the state it writes have to move together. + // Labelled so the storage-failure branches can leave the critical section and + // still hand the caller its attestation, which is the one thing in here that + // cannot be produced a second time. + shieldAttestState: synchronized (instance.flowLock) { if (isStale(pending)) { fail(pending, "App Attest state was reset while this request was in flight"); @@ -837,10 +841,21 @@ public static void nativeAttestationReady(final int requestId, final String atte instance.inMemoryState = STATE_PENDING; instance.pendingSinceInMemory = System.currentTimeMillis(); instance.bootstrapInFlight = false; - fail(pending, "App Attest could not record its attestation state"); - instance.failBootstrapWaiters( - "App Attest could not record its attestation state"); - return; + instance.failBootstrapWaiters("App Attest is completing first-run " + + "registration for this device; retry shortly"); + // The caller still gets the attestation, because it is the ONE thing + // here that cannot be produced again. Apple attests a key once; failing + // this request threw that object away, so the backend never received the + // key to register -- and after the grace window the client promotes it + // locally and starts asserting against a key the backend has never seen, + // which is rejected, which resets, which spends another rate-limited + // key. Discarding an irreplaceable result to report a storage problem + // costs strictly more than reporting nothing. + // + // Falls through to the same succeed() the normal path uses. The state is + // pending in memory, so later callers take the registration-in-progress + // path exactly as they would have. + break shieldAttestState; } long pendingSince = System.currentTimeMillis(); // Held in memory whatever the keychain does, and set BEFORE the write is @@ -869,10 +884,12 @@ public static void nativeAttestationReady(final int requestId, final String atte // backend's confirmation is unknown -- and it costs no rate-limited key. store.remove(KEY_ATTEST_STARTED); instance.bootstrapInFlight = false; - fail(pending, "App Attest could not record its registration deadline"); - instance.failBootstrapWaiters( - "App Attest could not record its registration deadline"); - return; + instance.failBootstrapWaiters("App Attest is completing first-run " + + "registration for this device; retry shortly"); + // Same reasoning as the state write above: the attestation object is + // one-time, so it goes to the caller rather than being discarded to + // report that a timestamp could not be stored. + break shieldAttestState; } // The recovery this key came from, if any, is now complete. Leaving the // marker would refuse a future replacement when iOS legitimately invalidates From 96c7cf10b355d287b864919a41c230a959553eb8 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 01:47:46 +0700 Subject: [PATCH 62/96] Install the shield guard before the engine, and honor a confirmation held in memory Publishing `initializing` only helps a request that reaches the code which waits on it, and the only thing that routes a request there is the network guard. Installed after engine.initialize(), the guard did not exist yet: a ConnectionRequest starting during a cold start found none at performOperationComplete() and opened the connection directly, so a fail-closed protected host could be called with neither a token nor a pin check for as long as the engine took to start. The guard reads its configuration live and every path through it waits, so installing it first turns that window into a wait. ShieldInitOrderTest holds an engine open inside initialize() and asserts both halves; both cases fail on the previous ordering. AppShield gains the resetForTesting() hook the registry and NetworkManager already have, since init() is one-shot and the ordering is what has now been wrong twice. confirmAttestation() read the persisted state only, so when the keychain refused the write recording an attestation the backend's acknowledgement was dropped -- permanently, because an acceptance is never re-sent. The key then waited out the grace window, or, if the app restarted first, was read as an interrupted attempt (the start marker is still there, because the path that clears it never ran) and discarded for another rate-limited key. It now accepts the in-memory pending state, persists STATE_ATTESTED, and finishes the bookkeeping that branch skipped. The unit-test job died reading the root POM on a Maven Central 429, so nothing about the branch was tested. retry.sh gains RETRY_ONLY_MATCHING and the step uses it: a resolution failure is retried, a failing test is not, so a re-run cannot launder a flake into a pass. Co-Authored-By: Claude Opus 5 (1M context) --- .github/workflows/pr.yml | 10 +- .../codename1/security/shield/AppShield.java | 37 ++- .../impl/ios/IOSDeviceIntegrity.java | 48 +++- .../codename1/io/NetworkGuardTestAccess.java | 42 +++ .../security/shield/ShieldInitOrderTest.java | 254 ++++++++++++++++++ .../shield/spi/ShieldEngineTestAccess.java | 42 +++ scripts/ci/retry.sh | 33 ++- 7 files changed, 452 insertions(+), 14 deletions(-) create mode 100644 maven/core-unittests/src/test/java/com/codename1/io/NetworkGuardTestAccess.java create mode 100644 maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldInitOrderTest.java create mode 100644 maven/core-unittests/src/test/java/com/codename1/security/shield/spi/ShieldEngineTestAccess.java diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 35632d23824..955a23b06ae 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -90,6 +90,14 @@ jobs: with: node-version: '20' - name: Run Unit Tests + # Maven Central answers 429 from the runner CDN edge often enough to kill + # this step outright -- it dies reading the root POM, before a line is + # compiled, so nothing about the branch is being tested when it happens. + # Retried through the shared helper, but ONLY for that failure shape: + # RETRY_ONLY_MATCHING keeps a failing test failing on the first attempt + # rather than letting a re-run launder a flake into a pass. + env: + RETRY_ONLY_MATCHING: 'status: (403|429|50[0-9])|Could not transfer artifact|Unresolveable build extension|Non-resolvable import POM' run: | MVN_GOAL="verify" MVN_ARGS="" @@ -98,7 +106,7 @@ jobs: MVN_ARGS="-Dspotbugs.skip=true -Dpmd.skip=true -Dcheckstyle.skip=true -Djacoco.skip=true" fi cd maven - mvn clean "$MVN_GOAL" -DunitTests=true -pl core-unittests -am -Dmaven.javadoc.skip=true -Plocal-dev-javase $MVN_ARGS + bash ../scripts/ci/retry.sh mvn clean "$MVN_GOAL" -DunitTests=true -pl core-unittests -am -Dmaven.javadoc.skip=true -Plocal-dev-javase $MVN_ARGS cd .. - name: Run push service-worker contract if: ${{ matrix.java-version == 8 }} diff --git a/CodenameOne/src/com/codename1/security/shield/AppShield.java b/CodenameOne/src/com/codename1/security/shield/AppShield.java index 95307edb907..f6828c84346 100644 --- a/CodenameOne/src/com/codename1/security/shield/AppShield.java +++ b/CodenameOne/src/com/codename1/security/shield/AppShield.java @@ -131,6 +131,20 @@ public static void init(ShieldConfig cfg) { initializing = true; } try { + // Installed FIRST, before the engine is given a chance to run. + // + // Publishing `initializing` is not enough on its own: a request that starts + // while the engine is still initializing only reaches awaitInitialization() + // if something routes it there, and the only thing that does is the guard. + // With the install last, a concurrent ConnectionRequest found a null guard at + // performOperationComplete(), skipped the shield entirely and opened the + // connection -- so a fail-closed protected host could be called with neither a + // token nor a pin check for as long as engine.initialize() took, which on a + // cold start is exactly when it takes longest. The guard reads the + // configuration live and every path through it waits for initialization, so + // installing it before the engine makes that window a wait rather than a + // bypass. + installNetworkGuard(); ShieldEngine engine = ShieldEngineRegistry.getEngine(); try { engine.initialize(contextForEngine(), config); @@ -140,9 +154,6 @@ public static void init(ShieldConfig cfg) { Log.e(t); setStatus(ShieldStatus.UNPROTECTED); } - // Installed BEFORE initialization is published, so there is no instant at - // which the shield claims to be up and a request can slip past unprotected. - installNetworkGuard(); } finally { synchronized (AppShield.class) { initializing = false; @@ -152,6 +163,26 @@ public static void init(ShieldConfig cfg) { } } + /// Test hook: puts the shield back to its pre-`init()` state. + /// + /// `init()` is deliberately one-shot, so without this the ordering it guarantees can + /// only be asserted once per JVM -- and the ordering is the thing that has been wrong + /// twice. Matches the hooks + /// [com.codename1.security.shield.spi.ShieldEngineRegistry] and + /// [com.codename1.io.NetworkManager] already carry for the same reason. + static void resetForTesting() { + synchronized (AppShield.class) { + config = null; + initialized = false; + initializing = false; + guard = null; + lastStatus = ShieldStatus.NOT_INITIALIZED; + runtimeHosts.clear(); + listeners.removeAllElements(); + AppShield.class.notifyAll(); + } + } + /// True while [#init(ShieldConfig)] is between taking the job and finishing it. /// /// Separate from `initialized` because the two answer different questions, and diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index 92da1dbfb66..9096b2a21d5 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -409,19 +409,51 @@ void confirmAttestation(String keyId) { // STATE_ATTESTED over a STATE_NEW key Apple has not attested yet -- and the // next request would assert against it. SecureStorage store = SecureStorage.getInstance(); - if (!STATE_PENDING.equals(store.get(KEY_STATE))) { + // It must acknowledge THIS key. A response for an earlier attestation can + // arrive after a reset has already replaced the identity, and promoting on + // that would mark a key attested that the backend has never seen -- so the + // next assertion is rejected and costs another reset, which is the loop this + // state exists to break. The in-memory identity counts as much as the + // persisted one: both are only ever set for the key this process is actually + // holding, and a reset clears both together. + boolean pendingInMemory = keyId.equals(inMemoryStateKeyId) + && STATE_PENDING.equals(inMemoryState); + if (!keyId.equals(store.get(KEY_ID)) && !pendingInMemory) { return; } - // And it must acknowledge THIS key. A response for an earlier attestation - // can arrive after a reset has already replaced the identity, and promoting - // on that would mark a key attested that the backend has never seen -- so - // the next assertion is rejected and costs another reset, which is the loop - // this state exists to break. - if (!keyId.equals(store.get(KEY_ID))) { + // What this process knows wins over what the keychain managed to store, the + // same rule the request path follows. + // + // When the write recording the attestation was refused, storage still says + // "new" -- so reading it alone dropped the backend's acknowledgement of a key + // Apple had already attested, and dropped it permanently: the acceptance is + // never re-sent. The key then sat until the grace window promoted it, or, if + // the app restarted first, was read as an interrupted attestation (the start + // marker is still there, because the path that clears it never ran) and + // discarded for another rate-limited key. Honouring the in-memory state costs + // nothing when the keychain is healthy and is the whole point of holding it. + if (!pendingInMemory && !STATE_PENDING.equals(store.get(KEY_STATE))) { return; } - store.set(KEY_STATE, STATE_ATTESTED); + if (!store.set(KEY_STATE, STATE_ATTESTED)) { + // Still a promotion, just one only this process knows about -- exactly + // what the grace-window promotion does when the keychain refuses it. + inMemoryStateKeyId = keyId; + inMemoryState = STATE_ATTESTED; + } store.remove(KEY_PENDING_SINCE); + // Then the rest of the bookkeeping the attestation callback does on its way + // out, because the branch that recorded a refused state write returned before + // reaching any of it. The start marker left behind makes an attested key look + // like an interrupted attempt on the next launch and costs a replacement key + // -- the precise outcome the in-memory state exists to avoid, arrived at one + // step later -- and a recovery marker left behind refuses the one-shot + // replacement the next time iOS legitimately invalidates this key. Doing it + // here rather than only there is harmless on the healthy path: it removes + // entries that are already gone. + store.remove(KEY_ATTEST_STARTED); + attestAnsweredForKey = null; + recoverySpentInMemory = !store.remove(KEY_RECOVERY_SPENT); } } diff --git a/maven/core-unittests/src/test/java/com/codename1/io/NetworkGuardTestAccess.java b/maven/core-unittests/src/test/java/com/codename1/io/NetworkGuardTestAccess.java new file mode 100644 index 00000000000..48a79abf8aa --- /dev/null +++ b/maven/core-unittests/src/test/java/com/codename1/io/NetworkGuardTestAccess.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2026, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ +package com.codename1.io; + +/** + * Reaches {@code NetworkManager}'s package-private test hook from a test that lives in + * another package. + * + *

The guard slot seals on first install, deliberately, so a test that installs one has + * to be able to put the slot back or it decides the behaviour of every test that runs + * after it in the same JVM.

+ */ +public final class NetworkGuardTestAccess { + + private NetworkGuardTestAccess() { + } + + /** Drops the installed guard and unseals the slot. */ + public static void reset() { + NetworkManager.resetNetworkGuardForTesting(); + } +} diff --git a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldInitOrderTest.java b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldInitOrderTest.java new file mode 100644 index 00000000000..c246f1853c1 --- /dev/null +++ b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldInitOrderTest.java @@ -0,0 +1,254 @@ +/* + * Copyright (c) 2026, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ +package com.codename1.security.shield; + +import com.codename1.io.ConnectionRequest; +import com.codename1.junit.UITestBase; +import com.codename1.io.NetworkGuard; +import com.codename1.io.NetworkGuardTestAccess; +import com.codename1.io.NetworkManager; +import com.codename1.security.shield.spi.EngineContext; +import com.codename1.security.shield.spi.ShieldEngine; +import com.codename1.security.shield.spi.ShieldEngineRegistry; +import com.codename1.security.shield.spi.ShieldEngineTestAccess; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * What a request that starts while {@code AppShield.init()} is still running gets. + * + *

The answer has to be "the shield, a moment later" and never "no shield at all". A + * cold start is the one moment when an app fires its first protected request and when + * initialization takes longest, so the window is not theoretical -- it is the common + * case. Both bugs this covers were invisible in normal use: everything succeeded, the + * request simply left without a token and without a pin check.

+ */ +class ShieldInitOrderTest extends UITestBase { + + /** Long enough that a real regression cannot pass by being fast. */ + private static final long GENEROUS_TIMEOUT_MS = 10000L; + + /** Short: it bounds how long a *correct* implementation is observed blocking. */ + private static final long BLOCKED_OBSERVATION_MS = 300L; + + private SlowEngine engine; + + @BeforeEach + void setUpShield() { + NetworkGuardTestAccess.reset(); + ShieldEngineTestAccess.reset(); + AppShield.resetForTesting(); + engine = new SlowEngine(); + } + + @AfterEach + void tearDownShield() { + engine.release(); + NetworkGuardTestAccess.reset(); + ShieldEngineTestAccess.reset(); + AppShield.resetForTesting(); + } + + /** + * The guard exists before the engine is given a chance to run. + * + *

Publishing an "initialization in progress" flag is not enough on its own. A + * concurrent request only reaches the code that waits on that flag if something routes + * it there, and the only thing that does is the network guard. Installed after + * {@code engine.initialize()}, the guard did not exist yet, so + * {@code ConnectionRequest.performOperationComplete()} found none and opened the + * connection directly -- no token, no pin check, for as long as the engine took to + * start.

+ */ + @Test + void aRequestStartingDuringInitializationFindsAGuardRatherThanNone() throws Exception { + Thread init = initOnAnotherThread(); + assertTrue(engine.entered.await(GENEROUS_TIMEOUT_MS, TimeUnit.MILLISECONDS), + "the engine should have been asked to initialize"); + + assertNotNull(NetworkManager.getNetworkGuard(), + "a request starting now would find no guard and skip the shield entirely"); + + engine.release(); + init.join(GENEROUS_TIMEOUT_MS); + assertFalse(init.isAlive(), "init() should have finished"); + } + + /** + * And going through that guard waits for initialization rather than sailing past it. + * + *

Installing the guard early is only safe because every path through it waits: the + * alternative -- a guard that answers while the engine is half-built -- would attach + * nothing and report success, which is the same hole one layer down.

+ */ + @Test + void thatGuardBlocksTheRequestUntilInitializationFinishes() throws Exception { + Thread init = initOnAnotherThread(); + assertTrue(engine.entered.await(GENEROUS_TIMEOUT_MS, TimeUnit.MILLISECONDS), + "the engine should have been asked to initialize"); + + NetworkGuard guard = NetworkManager.getNetworkGuard(); + assertNotNull(guard); + + final RecordingRequest request = new RecordingRequest(); + request.setUrl("https://api.example.com/secure"); + final CountDownLatch done = new CountDownLatch(1); + final AtomicReference failure = new AtomicReference(); + final NetworkGuard target = guard; + Thread caller = new Thread(new Runnable() { + public void run() { + try { + target.beforeRequest(request); + } catch (Throwable t) { + failure.set(t); + } + done.countDown(); + } + }, "shield-init-order-request"); + caller.setDaemon(true); + caller.start(); + + assertFalse(done.await(BLOCKED_OBSERVATION_MS, TimeUnit.MILLISECONDS), + "the request must wait for initialization, not proceed without a token"); + assertNull(request.attached(), + "and nothing should have been attached while the engine was still starting"); + + engine.release(); + assertTrue(done.await(GENEROUS_TIMEOUT_MS, TimeUnit.MILLISECONDS), + "the wait has to end when initialization does"); + assertNull(failure.get(), "the request should have succeeded: " + failure.get()); + assertEquals("token-from-a-fully-initialized-engine", request.attached(), + "once initialization finished the token belongs on the request"); + + init.join(GENEROUS_TIMEOUT_MS); + assertFalse(init.isAlive(), "init() should have finished"); + } + + private Thread initOnAnotherThread() { + ShieldEngineRegistry.setEngine(engine); + Thread t = new Thread(new Runnable() { + public void run() { + AppShield.init(new ShieldConfig() + .protect("api.example.com", HostPolicy.ENFORCED)); + } + }, "shield-init-order-init"); + t.setDaemon(true); + t.start(); + return t; + } + + /** + * Records what the shield attached. {@code ConnectionRequest} has no header getter, and + * adding one to the public API to serve a test would be the wrong direction. + */ + private static final class RecordingRequest extends ConnectionRequest { + + private String token; + + @Override + public void addRequestHeader(String key, String value) { + super.addRequestHeader(key, value); + if ("X-CN1-Attest".equals(key)) { + token = value; + } + } + + @Override + public void removeRequestHeader(String key) { + super.removeRequestHeader(key); + if ("X-CN1-Attest".equals(key)) { + token = null; + } + } + + String attached() { + return token; + } + } + + /** An engine whose {@code initialize()} is held open, which is the whole window. */ + private static final class SlowEngine implements ShieldEngine { + + private final CountDownLatch entered = new CountDownLatch(1); + private final CountDownLatch proceed = new CountDownLatch(1); + + void release() { + proceed.countDown(); + } + + public String getName() { + return "slow-test-engine"; + } + + public boolean isAvailable() { + return true; + } + + public void initialize(EngineContext ctx, ShieldConfig config) { + entered.countDown(); + try { + proceed.await(GENEROUS_TIMEOUT_MS, TimeUnit.MILLISECONDS); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + + public ShieldToken fetchToken(String bindingData) { + return new ShieldToken("token-from-a-fully-initialized-engine", + ShieldStatus.OK, System.currentTimeMillis(), 300000L, bindingData); + } + + public ShieldToken getCachedToken() { + return null; + } + + public boolean verifyPins(String host, String[] spkiDigests, String[] certDigests) { + return true; + } + + public PinSet getPinSet() { + return PinSet.EMPTY; + } + + public ShieldSignal[] collectSignals() { + return new ShieldSignal[0]; + } + + public void invalidate() { + } + + public void shutdown() { + } + } +} diff --git a/maven/core-unittests/src/test/java/com/codename1/security/shield/spi/ShieldEngineTestAccess.java b/maven/core-unittests/src/test/java/com/codename1/security/shield/spi/ShieldEngineTestAccess.java new file mode 100644 index 00000000000..21b71fbf3f1 --- /dev/null +++ b/maven/core-unittests/src/test/java/com/codename1/security/shield/spi/ShieldEngineTestAccess.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2026, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ +package com.codename1.security.shield.spi; + +/** + * Reaches {@code ShieldEngineRegistry}'s package-private test hook from a test in another + * package. + * + *

The registry seals on first registration, deliberately, so a test that registers an + * engine has to be able to unseal it or it decides what every later test in the same JVM + * sees.

+ */ +public final class ShieldEngineTestAccess { + + private ShieldEngineTestAccess() { + } + + /** Drops the registered engine and unseals the registry. */ + public static void reset() { + ShieldEngineRegistry.resetForTesting(); + } +} diff --git a/scripts/ci/retry.sh b/scripts/ci/retry.sh index f941e91a50f..d5e43beeced 100755 --- a/scripts/ci/retry.sh +++ b/scripts/ci/retry.sh @@ -13,8 +13,16 @@ set -uo pipefail # RETRY_ATTEMPTS (default 3) and RETRY_DELAY_SECONDS (default 30) override the # bounds; the command is always attempted at least once and the last attempt's # exit status is what this script returns. +# +# RETRY_ONLY_MATCHING is an extended regex that narrows retrying to failures +# whose output matches it -- for steps that RUN TESTS, where a blanket retry +# would quietly convert a flaky test into a pass and hide exactly the kind of +# race this repo requires to be root-caused. With it set, a failure that does not +# match is returned on the first attempt, so only the transient-resolution case +# gets a second chance. Leave it unset for steps that merely download and build. attempts="${RETRY_ATTEMPTS:-3}" delay="${RETRY_DELAY_SECONDS:-30}" +only_matching="${RETRY_ONLY_MATCHING:-}" if [ "$#" -eq 0 ]; then echo "retry.sh: no command given" >&2 @@ -35,9 +43,30 @@ esac # degenerate ranges, and this loop body must run exactly `attempts` times. status=0 attempt=1 +log="" +if [ -n "$only_matching" ]; then + log="$(mktemp)" + trap 'rm -f "$log"' EXIT +fi while [ "$attempt" -le "$attempts" ]; do - "$@" && exit 0 - status=$? + if [ -n "$only_matching" ]; then + # Streamed as well as captured, so the step's log reads exactly as it would + # without the wrapper. + "$@" 2>&1 | tee "$log" + status=$? + else + "$@" && exit 0 + status=$? + fi + if [ "$status" -eq 0 ]; then + exit 0 + fi + if [ -n "$only_matching" ] && ! grep -Eq "$only_matching" "$log"; then + echo "retry.sh: attempt ${attempt}/${attempts} failed with status ${status} and the" \ + "output does not match RETRY_ONLY_MATCHING, so this is a real failure, not a" \ + "transient one -- not retrying" >&2 + exit "$status" + fi if [ "$attempt" -lt "$attempts" ]; then echo "retry.sh: attempt ${attempt}/${attempts} failed with status ${status};" \ "retrying in ${delay}s (possible transient Maven Central 403/429/5xx)" >&2 From 200704c2b3414cc8d226a05ec93c95a705ac37d4 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 02:01:03 +0700 Subject: [PATCH 63/96] Clear the promoted state in memory, stop exiting on a debugger, reopen the socket transport confirmAttestation() persisted STATE_ATTESTED and left the old in-memory STATE_PENDING in place. The request path deliberately prefers what this process knows over what the keychain holds, so that stale value outvoted the write: a keychain that recovered in time to record the acceptance still answered "registration in progress" for the rest of the grace window, on a key the backend had already accepted. The promotion now updates the memory copy too, and drops the pending deadline with it. The native launch gate exited on any signal from cn1JailbreakSignals(), and that list grew `traced` so DeviceIntegrity could report a debugger. A clean physical device launched from Xcode is traced -- so with ios.detectJailbreak on, every ordinary debug session was terminated at launch. That reads as "the app crashes on device", and the fix a developer reaches for is turning the protection off. Reporting a debugger is still right; exiting on one is not, so the gate now acts only on the signals that say the device itself is compromised. MCPSocketTransport recorded `closed` permanently. A stop()/start() pair over one transport -- which MCPServer supports -- bound a socket, saw the retained flag, closed that socket and threw, so the restarted server stopped on its first breath while the restart reported success. Cleared on open() rather than in close(), so a transport that was closed and not reopened still refuses to read. A second open on a live transport is now refused as well: the listening socket is reachable only through that field, so overwriting it stranded a listener that survived close() and kept the port bound. Co-Authored-By: Claude Opus 5 (1M context) --- .../impl/javase/MCPSocketTransport.java | 25 ++++ .../nativeSources/CN1JailbreakDetector.m | 28 ++++- .../impl/ios/IOSDeviceIntegrity.java | 17 ++- .../javase/MCPSocketTransportReopenTest.java | 119 ++++++++++++++++++ 4 files changed, 184 insertions(+), 5 deletions(-) create mode 100644 maven/javase/src/test/java/com/codename1/impl/javase/MCPSocketTransportReopenTest.java diff --git a/Ports/JavaSE/src/com/codename1/impl/javase/MCPSocketTransport.java b/Ports/JavaSE/src/com/codename1/impl/javase/MCPSocketTransport.java index c43bf83b2df..715071089e4 100644 --- a/Ports/JavaSE/src/com/codename1/impl/javase/MCPSocketTransport.java +++ b/Ports/JavaSE/src/com/codename1/impl/javase/MCPSocketTransport.java @@ -76,6 +76,31 @@ public MCPTransport createSocketTransport(int port) { @Override public void open() throws IOException { + // Reopening this instance starts a session; it does not resume a closed one. + // + // close() set `closed` permanently and nothing cleared it, so a stop()/start() pair + // over one transport -- which MCPServer supports, and which is how a caller holding + // a single transport restarts -- bound a socket, saw the retained flag below, closed + // that socket and threw, stopping the restarted server on its first breath. Cleared + // here rather than in close(), because a transport that has been closed and not + // reopened must keep refusing reads. Same fix, same reasoning, as + // MCPLoopbackSocketTransport. + // + // Refusing a second open while one is live is part of it: the listening socket is + // reachable only through this field, so overwriting it strands a listener that + // survives close() and keeps the port bound. + synchronized (lock) { + if (serverSocket != null) { + throw new IOException("This MCP transport is already listening on port " + + port); + } + closed = false; + // A previous session's streams belong to a socket close() already shut. Left + // in place, readMessage() would read the old client before accepting the new + // one -- it recovers, but only by way of a read failure on a dead socket. + reader = null; + writer = null; + } // Bind to the loopback interface only so the MCP control channel is never exposed // to the local network. Backlog of one: a single agent attaches at a time, but the // listening socket stays bound across client sessions so an agent can disconnect and diff --git a/Ports/iOSPort/nativeSources/CN1JailbreakDetector.m b/Ports/iOSPort/nativeSources/CN1JailbreakDetector.m index d748ab6fa51..911336d8892 100644 --- a/Ports/iOSPort/nativeSources/CN1JailbreakDetector.m +++ b/Ports/iOSPort/nativeSources/CN1JailbreakDetector.m @@ -111,10 +111,34 @@ } #ifdef CN1_DETECT_JAILBREAK +/** + * Whether a signal says the DEVICE is compromised, as opposed to saying somebody is + * looking at the app. + * + * The probe list grew a `traced` signal so DeviceIntegrity could report a debugger, + * which is worth reporting -- attaching one is how a build gets instrumented. Exiting on + * it is a different matter: a clean physical device launched from Xcode is traced, so the + * launch gate terminated every ordinary debug session on a project that leaves + * ios.detectJailbreak on. That reads as "the app crashes on device", and the usual fix a + * developer reaches for is turning the protection off. + */ +static BOOL cn1IsJailbreakSignal(NSString *signal) { + return [signal isEqualToString:@"dyldInsert"] + || [signal isEqualToString:@"hookLib"] + || [signal isEqualToString:@"jailbreakFile"] + || [signal isEqualToString:@"restrictedWrite"]; +} + void cn1DetectJailbreakBypassesAndExit(void) { NSString *signals = cn1JailbreakSignals(); - if (signals.length > 0) { - NSLog(@"Jailbreak bypass detected: %@", signals); + NSMutableArray *fatal = [NSMutableArray array]; + for (NSString *signal in [signals componentsSeparatedByString:@","]) { + if (cn1IsJailbreakSignal(signal)) { + [fatal addObject:signal]; + } + } + if (fatal.count > 0) { + NSLog(@"Jailbreak bypass detected: %@", [fatal componentsJoinedByString:@","]); exit(0); } } diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index 9096b2a21d5..0aea715c754 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -435,12 +435,23 @@ void confirmAttestation(String keyId) { if (!pendingInMemory && !STATE_PENDING.equals(store.get(KEY_STATE))) { return; } - if (!store.set(KEY_STATE, STATE_ATTESTED)) { - // Still a promotion, just one only this process knows about -- exactly - // what the grace-window promotion does when the keychain refuses it. + // Promoted in memory whether or not the keychain takes it. If the write is + // refused this is the only record, exactly as it is for the grace-window + // promotion. If it succeeds, this is what stops the OLD in-memory value + // outvoting it: the request path deliberately prefers what this process knows + // over what the keychain holds, so a leftover STATE_PENDING kept answering + // "registration in progress" for the rest of the grace window on a key the + // backend had already accepted -- a keychain that recovered in time to record + // the acceptance still left the device refusing to use it. + boolean persisted = store.set(KEY_STATE, STATE_ATTESTED); + if (!persisted || pendingInMemory) { inMemoryStateKeyId = keyId; inMemoryState = STATE_ATTESTED; } + // The key is registered, so there is no first-run window left to wait out -- + // and leaving the deadline behind would make registrationGraceRemaining() + // report one. + pendingSinceInMemory = 0L; store.remove(KEY_PENDING_SINCE); // Then the rest of the bookkeeping the attestation callback does on its way // out, because the branch that recorded a refused state write returned before diff --git a/maven/javase/src/test/java/com/codename1/impl/javase/MCPSocketTransportReopenTest.java b/maven/javase/src/test/java/com/codename1/impl/javase/MCPSocketTransportReopenTest.java new file mode 100644 index 00000000000..e73222b3fdf --- /dev/null +++ b/maven/javase/src/test/java/com/codename1/impl/javase/MCPSocketTransportReopenTest.java @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2026, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ +package com.codename1.impl.javase; + +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Restarting an MCP server over one transport instance. + * + *

{@code MCPServer} supports {@code stop()} followed by {@code start(t)}, and a caller + * that holds a single transport around a restart is the ordinary way to use it. This + * transport recorded {@code closed} permanently: the second {@code open()} bound a socket, + * saw the retained flag, closed that socket and threw -- so the restarted server stopped on + * its first breath, with a message about being closed before it could listen. The failure + * is silent in the sense that matters: the restart reports success and the port is dead.

+ */ +class MCPSocketTransportReopenTest { + + @Test + void aTransportThatWasClosedCanBeOpenedAgain() throws Exception { + int port = freePort(); + MCPSocketTransport transport = new MCPSocketTransport(port); + transport.open(); + transport.close(); + + transport.open(); + try { + // Bound and listening: an agent can attach, and what it sends arrives. Before + // the fix this open() threw, so there was nothing to connect to. + Socket client = new Socket(InetAddress.getLoopbackAddress(), port); + try { + Writer w = new OutputStreamWriter(client.getOutputStream(), "UTF-8"); + w.write("{\"jsonrpc\":\"2.0\"}\n"); + w.flush(); + assertEquals("{\"jsonrpc\":\"2.0\"}", transport.readMessage(), + "a reopened transport has to serve a new session"); + } finally { + client.close(); + } + } finally { + transport.close(); + } + } + + /** + * A second open on a live transport is refused rather than stranding the listener. + * + *

The listening socket is reachable only through the transport's own field, so + * overwriting it leaves a listener nothing can close -- and it survives {@code close()} + * holding the port, which the next start then cannot bind.

+ */ + @Test + void openingATransportThatIsAlreadyListeningIsRefused() throws Exception { + int port = freePort(); + MCPSocketTransport transport = new MCPSocketTransport(port); + transport.open(); + try { + IOException refused = assertThrows(IOException.class, transport::open); + assertTrue(refused.getMessage().contains("already listening"), + "the refusal should say what is wrong, got: " + refused.getMessage()); + } finally { + transport.close(); + } + // And the port is genuinely free afterwards, which is what proves nothing was + // stranded. + ServerSocket rebind = new ServerSocket(port, 1, InetAddress.getLoopbackAddress()); + rebind.close(); + } + + /** A closed transport that is NOT reopened still refuses to read. */ + @Test + void aClosedTransportStillReadsNull() throws Exception { + MCPSocketTransport transport = new MCPSocketTransport(freePort()); + transport.open(); + transport.close(); + + assertEquals(null, transport.readMessage(), + "clearing the flag on open must not make close() stop meaning anything"); + } + + private static int freePort() throws IOException { + ServerSocket probe = new ServerSocket(0, 1, InetAddress.getLoopbackAddress()); + try { + return probe.getLocalPort(); + } finally { + probe.close(); + } + } +} From 04de9f583103da5563b54d6088d62fa1eb64e59d Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 02:16:57 +0700 Subject: [PATCH 64/96] Serialize MCP transport opens on a lock no transport can acquire MCPTransport is a public interface, so an implementation is entitled to write `synchronized void close()` -- and a transport that parks inside open() waiting for a client is the normal shape, since close() is what interrupts it. Together those hung the process: the reader thread held the transport's own monitor across open() and wanted the server monitor inside isCurrent(), while stop() held the server monitor and wanted the transport's monitor inside close(). Neither moves again, and the one call that could have ended the blocking open() is the one that is stuck. Opens are still serialized per transport instance -- that is what stops two generations opening one transport and leaking the first listener -- but on a lock the server owns and no transport can name. The ordering now has one direction: open lock, then server monitor, then whatever the transport locks internally. The locks are reference-counted rather than kept forever: a plain map grows an entry per transport instance the process ever serves, and dropping one while a thread is parked on it would let the next generation mint a second lock for the same transport, which is not serialization at all. MCPServerLockOrderTest stops a server whose transport is inside a blocking, self-synchronizing open(). It deadlocks on the previous code, so it asserts with a timeout rather than a join -- a regression fails the suite instead of hanging it. Co-Authored-By: Claude Opus 5 (1M context) --- .../src/com/codename1/mcp/MCPServer.java | 173 ++++++++++++------ .../codename1/mcp/MCPServerLockOrderTest.java | 118 ++++++++++++ 2 files changed, 236 insertions(+), 55 deletions(-) create mode 100644 maven/core-unittests/src/test/java/com/codename1/mcp/MCPServerLockOrderTest.java diff --git a/CodenameOne/src/com/codename1/mcp/MCPServer.java b/CodenameOne/src/com/codename1/mcp/MCPServer.java index 0f39727bed6..f1a7816cf72 100644 --- a/CodenameOne/src/com/codename1/mcp/MCPServer.java +++ b/CodenameOne/src/com/codename1/mcp/MCPServer.java @@ -232,8 +232,8 @@ private synchronized void releaseAndCloseIfCurrent(MCPTransport t, int generatio /// listening" -- and that IOException stopped the server the restart had just brought /// up. /// - /// Only ever called while holding `t`'s monitor, which is what makes "the replacement - /// cannot have opened yet" true rather than merely likely. + /// Only ever called while holding `t`'s open lock, which is what makes "the + /// replacement cannot have opened yet" true rather than merely likely. private synchronized void discardOwnOpen(MCPTransport t, int generation) { if (startGeneration == generation && transport == t) { // NOPMD identity: is this still our transport? @@ -242,6 +242,120 @@ private synchronized void discardOwnOpen(MCPTransport t, int generation) { t.close(); } + /// Opens `t` for this generation, serialized against any other generation opening the + /// same transport. Returns false when this thread is done and must not read. + /// + /// Serialized per TRANSPORT, and the WHOLE open -- the attempt, its failure path, and + /// the teardown of a superseded one -- happens inside. A stop()/start() over the SAME + /// transport while the old reader is still parked in open() would otherwise have both + /// generations open one instance: the same-transport rule then correctly declines to + /// close it on the way out, and the transport is left holding two listeners with one + /// handle for them, so the first is leaked and outlives stop(). + /// + /// The teardown has to be in here too, not after. Releasing the lock first let the + /// replacement acquire it and call open() while this thread's now-stale listener was + /// still registered -- and a transport refuses a second listener, so the replacement + /// took an IOException and stopped the server it had just started. + /// + /// Per transport and not per server, which is what a server-wide lock got wrong: a + /// restart over a DIFFERENT transport has nothing to serialize against, and making it + /// wait behind a superseded open that may never return deadlocks the restart. Not the + /// server monitor either: open() blocks, and holding that across it would make stop() + /// wait on what it is stopping. + /// + /// And deliberately NOT the transport's own monitor, which is what this used to be. + /// [MCPTransport] is a public interface: an implementation is entitled to write + /// `synchronized void close()`, and with such a transport the two locks were taken in + /// opposite orders -- this thread held the transport and waited for the server monitor + /// inside `isCurrent`, while `stop()` held the server monitor and waited for the + /// transport inside `close()`. Both threads park forever. The same ownership also + /// blocked the one call that can end a legitimately blocking `open()`: `close()` could + /// not run until `open()` returned, and `open()` was waiting for `close()`. A lock the + /// server owns and no transport can name has neither problem, and the ordering below + /// has one direction only -- open lock, then server monitor, then whatever the + /// transport locks internally. + private boolean openSerialized(MCPTransport t, int generation) { + Object openLock = acquireOpenLock(t); + try { + synchronized (openLock) { + if (!isCurrent(t, generation)) { + releaseAndCloseIfCurrent(t, generation); + return false; + } + try { + t.open(); + } catch (IOException ex) { + // The transport failed to start listening. Log defensively: the CN1 + // Log routes through the platform implementation, which may not be + // registered yet when the server is auto-started early in + // Display.init(), and a raw NullPointerException here would silently + // kill the reader thread. + try { + Log.e(ex); + } catch (Throwable logErr) { + System.err.println("[cn1.mcp] transport open failed: " + ex); + } + // A failed open can still have registered something -- the loopback + // transport claims the process-wide slot before it binds -- so this + // unwinds the same way a successful one does. + discardOwnOpen(t, generation); + return false; + } + if (!isCurrent(t, generation)) { + // Same window, the far side of it: the server moved on while open() + // was in flight. The listener now on `t` is this thread's and has to + // go. + discardOwnOpen(t, generation); + return false; + } + return true; + } + } finally { + releaseOpenLock(t); + } + } + + /// The per-transport open locks, and how many threads are currently holding a + /// reference to each. + /// + /// A plain map would grow one entry per transport instance the process ever serves. + /// The count is what makes removal safe: dropping an entry while a thread is parked on + /// that monitor would let the next generation mint a second lock for the same + /// transport, and two generations serializing on different objects are not serialized + /// at all. + private final List openLocks = new ArrayList(); + + private Object acquireOpenLock(MCPTransport t) { + synchronized (openLocks) { + for (int i = 0; i < openLocks.size(); i++) { + Object[] entry = openLocks.get(i); + if (entry[0] == t) { // NOPMD identity: one lock per transport INSTANCE + ((int[]) entry[2])[0]++; + return entry[1]; + } + } + Object lock = new Object(); + openLocks.add(new Object[] {t, lock, new int[] {1}}); + return lock; + } + } + + private void releaseOpenLock(MCPTransport t) { + synchronized (openLocks) { + for (int i = 0; i < openLocks.size(); i++) { + Object[] entry = openLocks.get(i); + if (entry[0] == t) { // NOPMD identity: one lock per transport INSTANCE + int[] users = (int[]) entry[2]; + users[0]--; + if (users[0] <= 0) { + openLocks.remove(i); + } + return; + } + } + } + } + private void runLoop(MCPTransport t, int generation) { // Opening is deferred to this thread, so by the time it happens the server may // already have been stopped or restarted. Either way stop()'s close() ran against a @@ -252,59 +366,8 @@ private void runLoop(MCPTransport t, int generation) { if (!isCurrent(t, generation)) { return; } - // Serialized per TRANSPORT, on that transport's own monitor, and the WHOLE - // open -- the attempt, its failure path, and the teardown of a superseded one -- - // happens inside. A stop()/start() over the SAME transport while the old reader - // is still parked in open() would otherwise have both generations open one - // instance: the same-transport rule then correctly declines to close it on the - // way out, and the transport is left holding two listeners with one handle for - // them, so the first is leaked and outlives stop(). - // - // The teardown has to be in here too, not after. Releasing the monitor first let - // the replacement acquire it and call open() while this thread's now-stale - // listener was still registered -- and the transport refuses a second listener, - // so the replacement took an IOException and stopped the server it had just - // started. Holding the monitor until the stale one is actually closed is what - // makes "the replacement may open" mean what it says. - // - // Per transport and not per server, which is what a server-wide lock got wrong: - // a restart over a DIFFERENT transport has nothing to serialize against, and - // making it wait behind a superseded open that may never return deadlocks the - // restart -- the replacement never opens, so nothing wakes the thread that would - // release it. Not the server monitor either: open() blocks, and holding that - // across it would make stop() wait on what it is stopping. The server monitor is - // taken inside this one (by isCurrent and releaseAndCloseIfCurrent) and never the - // other way round -- stop() closes through the transport's own internal lock, not - // its monitor -- so the nesting has one direction only. - synchronized (t) { - if (!isCurrent(t, generation)) { - releaseAndCloseIfCurrent(t, generation); - return; - } - try { - t.open(); - } catch (IOException ex) { - // The transport failed to start listening. Log defensively: the CN1 Log - // routes through the platform implementation, which may not be registered - // yet when the server is auto-started early in Display.init(), and a raw - // NullPointerException here would silently kill the reader thread. - try { - Log.e(ex); - } catch (Throwable logErr) { - System.err.println("[cn1.mcp] transport open failed: " + ex); - } - // A failed open can still have registered something -- the loopback - // transport claims the process-wide slot before it binds -- so this - // unwinds the same way a successful one does. - discardOwnOpen(t, generation); - return; - } - if (!isCurrent(t, generation)) { - // Same window, the far side of it: the server moved on while open() was - // in flight. The listener now on `t` is this thread's and has to go. - discardOwnOpen(t, generation); - return; - } + if (!openSerialized(t, generation)) { + return; } while (isCurrent(t, generation)) { String line; diff --git a/maven/core-unittests/src/test/java/com/codename1/mcp/MCPServerLockOrderTest.java b/maven/core-unittests/src/test/java/com/codename1/mcp/MCPServerLockOrderTest.java new file mode 100644 index 00000000000..c7b472534c6 --- /dev/null +++ b/maven/core-unittests/src/test/java/com/codename1/mcp/MCPServerLockOrderTest.java @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2026, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ +package com.codename1.mcp; + +import java.io.IOException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Stopping a server whose transport is still inside a blocking {@code open()}. + * + *

{@link MCPTransport} is a public interface, so an implementation is entitled to write + * {@code synchronized void close()} -- and a transport that waits for a client inside + * {@code open()} is the normal shape, since that is what {@code close()} exists to + * interrupt. Those two facts together used to hang the process: the reader thread held the + * transport's own monitor across {@code open()} and wanted the server monitor inside + * {@code isCurrent()}, while {@code stop()} held the server monitor and wanted the + * transport's monitor inside {@code close()}. Neither ever moves again, and the one call + * that could have ended the blocking {@code open()} is the one that is stuck.

+ * + *

Written with a timeout rather than a plain join, so a regression fails the suite + * instead of hanging it.

+ */ +class MCPServerLockOrderTest { + + /** Long enough that a slow machine cannot fail it; short enough to notice. */ + private static final long TIMEOUT_MS = 10000L; + + @Test + void stopEndsABlockingOpenOnATransportThatSynchronizesItself() throws Exception { + BlockingTransport transport = new BlockingTransport(); + MCPServer server = new MCPServer(); + server.start(transport); + + assertTrue(transport.opening.await(TIMEOUT_MS, TimeUnit.MILLISECONDS), + "the reader thread should have reached open()"); + + final CountDownLatch stopped = new CountDownLatch(1); + Thread stopper = new Thread(new Runnable() { + public void run() { + server.stop(); + stopped.countDown(); + } + }, "mcp-lock-order-stop"); + stopper.setDaemon(true); + stopper.start(); + + assertTrue(stopped.await(TIMEOUT_MS, TimeUnit.MILLISECONDS), + "stop() must not wait on a lock the blocked open() is holding"); + assertTrue(transport.closed.await(TIMEOUT_MS, TimeUnit.MILLISECONDS), + "and it has to actually reach close(), which is what ends the open()"); + assertFalse(server.isRunning()); + } + + /** + * The shape the interface allows and the fix has to tolerate: {@code open()} parks + * until {@code close()} says otherwise, and {@code close()} is synchronized on the + * transport. + */ + private static final class BlockingTransport implements MCPTransport { + + private final CountDownLatch opening = new CountDownLatch(1); + private final CountDownLatch closed = new CountDownLatch(1); + private final CountDownLatch release = new CountDownLatch(1); + + @Override + public void open() throws IOException { + opening.countDown(); + try { + // Waits for a client, as a real listening transport does. Bounded only so + // a failing run ends rather than parking a thread forever. + release.await(TIMEOUT_MS * 3, TimeUnit.MILLISECONDS); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IOException("interrupted"); + } + } + + @Override + public synchronized void close() { + release.countDown(); + closed.countDown(); + } + + @Override + public String readMessage() throws IOException { + return null; + } + + @Override + public void writeMessage(String message) throws IOException { + } + } +} From 5a106cc2cced0f3b60a2e528634158ea2ccfc2d5 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 02:26:45 +0700 Subject: [PATCH 65/96] Refuse the proxy-credential headers as the token header Proxy-Authorization and Proxy-Authenticate are hop-by-hop: a forward proxy consumes them as its own credentials and does not forward them, so a token placed in one never reaches the origin while attach() reports success -- and a fail-closed host sends a protected request with nothing attached. They are worth naming separately from the framing headers because they do not look like transport plumbing; they look like a place credentials belong, which is exactly why someone would choose one. The failure then appears only for users behind such a proxy, on a network nobody testing the app was on. Co-Authored-By: Claude Opus 5 (1M context) --- .../com/codename1/security/shield/ShieldConfig.java | 12 ++++++++++-- .../com/codename1/security/shield/ShieldApiTest.java | 5 +++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java b/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java index 7c48ca69263..3202171bcb2 100644 --- a/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java +++ b/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java @@ -114,8 +114,9 @@ public ShieldConfig tokenHeader(String name) { /// - routing (`host`) -- this selects the virtual host, so overwriting it sends the /// request somewhere else entirely; /// - hop-by-hop (`connection`, `keep-alive`, `proxy-connection`, `te`, `trailer`, - /// `upgrade`) -- defined to be consumed by the next hop and not forwarded, so the - /// token would be stripped in transit by a proxy that is behaving correctly. + /// `upgrade`, `proxy-authorization`, `proxy-authenticate`) -- defined to be + /// consumed by the next hop and not forwarded, so the token would be stripped in + /// transit by a proxy that is behaving correctly. /// /// Refused rather than warned about, because the failure has no symptom on the /// client: `attach()` returns having set the header, and the request reaches the @@ -124,6 +125,13 @@ public ShieldConfig tokenHeader(String name) { private static final String[] TRANSPORT_HEADERS = { "host", "content-length", "transfer-encoding", "connection", "keep-alive", "proxy-connection", "te", "trailer", "upgrade", + // The two proxy-credential headers are hop-by-hop like the rest, and worth + // naming because they do not LOOK like transport plumbing -- they look like a + // place credentials belong, which is exactly why someone would pick one. A + // forward proxy consumes them; the origin never sees the token, and the failure + // appears only once a user is behind such a proxy, on a network nobody testing + // this was on. + "proxy-authorization", "proxy-authenticate", // Cookie is not transport framing, but it fails the same way and worse. // ConnectionRequest emits userHeaders first and THEN calls setHeader("Cookie", // ...) with the generated cookie string, so with cookie handling on and any diff --git a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java index a3df05016d9..ae3031e29cb 100644 --- a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java +++ b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java @@ -464,6 +464,11 @@ void transportOwnedHeadersAreRefusedAsTheTokenHeader() { String[] refused = { "Host", "Content-Length", "Transfer-Encoding", "Connection", "Keep-Alive", "Proxy-Connection", "TE", "Trailer", "Upgrade", + // Hop-by-hop like the rest, and the two that do not look like plumbing: a + // forward proxy consumes them as its own credentials, so the origin never + // sees the token -- and only for users behind such a proxy, which is a + // network nobody testing this was on. + "Proxy-Authorization", "Proxy-Authenticate", // Not framing, but overwritten by the request itself: ConnectionRequest // emits userHeaders and then sets Cookie from its own cookie store. "Cookie" From ccc65246ee49316c2b5db2c6910916209fdb81d1 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 02:47:38 +0700 Subject: [PATCH 66/96] Iterate the open locks with foreach, which the PMD gate requires ForLoopCanBeForeach is on the forbidden list, and the Maven PMD run does not fail on it -- generate-quality-report.py does, after the build has otherwise passed. So the local gate to run for this class of change is the script, not mvn verify. The removal now takes the entry rather than an index, which is safe only because the loop returns immediately afterwards; noted where it happens. Co-Authored-By: Claude Opus 5 (1M context) --- .../src/com/codename1/mcp/MCPServer.java | 10 +++++----- quality-report.md | 20 +++++++++++++++---- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/CodenameOne/src/com/codename1/mcp/MCPServer.java b/CodenameOne/src/com/codename1/mcp/MCPServer.java index f1a7816cf72..6910c848e36 100644 --- a/CodenameOne/src/com/codename1/mcp/MCPServer.java +++ b/CodenameOne/src/com/codename1/mcp/MCPServer.java @@ -327,8 +327,7 @@ private boolean openSerialized(MCPTransport t, int generation) { private Object acquireOpenLock(MCPTransport t) { synchronized (openLocks) { - for (int i = 0; i < openLocks.size(); i++) { - Object[] entry = openLocks.get(i); + for (Object[] entry : openLocks) { if (entry[0] == t) { // NOPMD identity: one lock per transport INSTANCE ((int[]) entry[2])[0]++; return entry[1]; @@ -342,13 +341,14 @@ private Object acquireOpenLock(MCPTransport t) { private void releaseOpenLock(MCPTransport t) { synchronized (openLocks) { - for (int i = 0; i < openLocks.size(); i++) { - Object[] entry = openLocks.get(i); + for (Object[] entry : openLocks) { if (entry[0] == t) { // NOPMD identity: one lock per transport INSTANCE int[] users = (int[]) entry[2]; users[0]--; if (users[0] <= 0) { - openLocks.remove(i); + // Safe to remove while iterating only because this returns + // immediately afterwards. + openLocks.remove(entry); } return; } diff --git a/quality-report.md b/quality-report.md index 202c12df5c2..283e223d2bd 100644 --- a/quality-report.md +++ b/quality-report.md @@ -1,12 +1,24 @@ ## ✅ Continuous Quality Report ### Test & Coverage -- ✅ **Tests:** 4727 total, 0 failed, 0 skipped -- ⚠️ Coverage report not generated. +- ✅ **Tests:** 4730 total, 0 failed, 0 skipped +- 📊 **Line coverage:** 58.65% + - **Lowest covered classes** + - `com.codename1.mcp.MCPServer` – 0.00% + - `com.codename1.gaming.level.GameSceneView` – 0.00% + - `com.codename1.crash.CrashProtection` – 0.00% + - `com.codename1.payment.CommerceManager` – 0.00% + - `com.codename1.crash.PiiScrubber` – 0.00% + - `com.codename1.crash.CrashReportPayload` – 0.00% + - `com.codename1.security.shield.ShieldConfig` – 0.00% + - `com.codename1.vr.VRView` – 0.00% + - `com.codename1.appreview.RatingDialog` – 0.00% + - `com.codename1.calendar.DefaultCalendarHttpTransport` – 0.00% ### Static Analysis -- ✅ SpotBugs: no findings (report was not generated by the build). +- **SpotBugs** + - ✅ **core-unittests:** 0 findings (no issues) - ✅ **PMD:** 0 findings (no issues) -- ⚠️ Checkstyle report not generated. +- ✅ **Checkstyle:** 0 findings (no issues) _Generated automatically by the PR CI workflow._ From 112dabb8e554f3aa1b8e4acada24eec138572099 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 02:58:22 +0700 Subject: [PATCH 67/96] Promote through one method, so the grace path finishes what confirmation does There are two ways a key becomes registered: the backend acknowledging the attestation, and the grace window expiring without one -- which is documented and supported, not an error path. They had drifted apart, and the grace branch kept only the state and the deadline. A replacement key reaching it, its pending metadata write having failed and no backend acknowledging, was promoted with the recovery marker from the attempt that produced it still set: the next time iOS legitimately invalidated that key, its one-shot replacement was refused as already spent, and every assertion failed until the app reset attestation by hand. Both paths now call promoteToAttested, which records the in-memory copy, drops the pending deadline, clears the start marker and clears the recovery marker. Each of those steps costs a rate-limited hardware key when it is missing, which is why having two copies of the sequence was the wrong shape. Co-Authored-By: Claude Opus 5 (1M context) --- .../impl/ios/IOSDeviceIntegrity.java | 86 ++++++++++--------- quality-report.md | 21 ++--- 2 files changed, 52 insertions(+), 55 deletions(-) diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index 0aea715c754..6eb38bbb347 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -435,37 +435,45 @@ void confirmAttestation(String keyId) { if (!pendingInMemory && !STATE_PENDING.equals(store.get(KEY_STATE))) { return; } - // Promoted in memory whether or not the keychain takes it. If the write is - // refused this is the only record, exactly as it is for the grace-window - // promotion. If it succeeds, this is what stops the OLD in-memory value - // outvoting it: the request path deliberately prefers what this process knows - // over what the keychain holds, so a leftover STATE_PENDING kept answering - // "registration in progress" for the rest of the grace window on a key the - // backend had already accepted -- a keychain that recovered in time to record - // the acceptance still left the device refusing to use it. - boolean persisted = store.set(KEY_STATE, STATE_ATTESTED); - if (!persisted || pendingInMemory) { - inMemoryStateKeyId = keyId; - inMemoryState = STATE_ATTESTED; - } - // The key is registered, so there is no first-run window left to wait out -- - // and leaving the deadline behind would make registrationGraceRemaining() - // report one. - pendingSinceInMemory = 0L; - store.remove(KEY_PENDING_SINCE); - // Then the rest of the bookkeeping the attestation callback does on its way - // out, because the branch that recorded a refused state write returned before - // reaching any of it. The start marker left behind makes an attested key look - // like an interrupted attempt on the next launch and costs a replacement key - // -- the precise outcome the in-memory state exists to avoid, arrived at one - // step later -- and a recovery marker left behind refuses the one-shot - // replacement the next time iOS legitimately invalidates this key. Doing it - // here rather than only there is harmless on the healthy path: it removes - // entries that are already gone. - store.remove(KEY_ATTEST_STARTED); - attestAnsweredForKey = null; - recoverySpentInMemory = !store.remove(KEY_RECOVERY_SPENT); + promoteToAttested(store, keyId, pendingInMemory); + } + } + + /** + * Records that {@code keyId} is registered, and finishes everything that follows + * from it. + * + *

One method because there are two ways to arrive: the backend acknowledging the + * attestation, and the grace window expiring without an acknowledgement -- which is a + * documented, supported outcome, not an error path. They had drifted apart, and every + * step missing from one of them costs a rate-limited hardware key:

+ * + *
    + *
  • the in-memory copy, because the request path prefers what this process knows + * over what the keychain holds -- so a leftover {@code STATE_PENDING} outvotes a + * write that has just succeeded and keeps answering "registration in progress" on + * a key that is registered;
  • + *
  • the pending deadline, or {@code registrationGraceRemaining()} reports a window + * that has already been resolved;
  • + *
  • the start marker, or the next launch reads an attested key as an interrupted + * attestation and discards it for a replacement;
  • + *
  • the recovery marker, or the next time iOS legitimately invalidates this key the + * one-shot replacement is refused as already spent, and every assertion fails + * until the app resets attestation by hand.
  • + *
+ * + *

Harmless on the healthy path: it removes entries that are already gone.

+ */ + private void promoteToAttested(SecureStorage store, String keyId, boolean holdInMemory) { + if (!store.set(KEY_STATE, STATE_ATTESTED) || holdInMemory) { + inMemoryStateKeyId = keyId; + inMemoryState = STATE_ATTESTED; } + pendingSinceInMemory = 0L; + store.remove(KEY_PENDING_SINCE); + store.remove(KEY_ATTEST_STARTED); + attestAnsweredForKey = null; + recoverySpentInMemory = !store.remove(KEY_RECOVERY_SPENT); } String[] jailbreakSignals() { @@ -537,15 +545,15 @@ AsyncResource requestToken(String nonce) { // not participate in acknowledgement at all, or its registration // call was lost. Assume registered rather than re-attesting on // every request forever. - if (!store.set(KEY_STATE, STATE_ATTESTED)) { - // Same reasoning one step later: a promotion the keychain refused - // must not send an accepted key back to attestKey on the next - // request. Held in memory for this process; a restart re-reads - // whatever the keychain does hold. - inMemoryStateKeyId = keyId; - inMemoryState = STATE_ATTESTED; - } - store.remove(KEY_PENDING_SINCE); + // The same promotion confirmAttestation() performs, through the same + // method. Written out here, it kept only the state and the deadline -- + // so a replacement key that reached this branch (its pending metadata + // write having failed, and no backend acknowledging) was promoted with + // the recovery marker from the attempt that produced it still set. The + // next time iOS legitimately invalidated that key, its replacement was + // refused as already spent and every assertion failed until the app + // reset attestation by hand. + promoteToAttested(store, keyId, keyId.equals(inMemoryStateKeyId)); state = STATE_ATTESTED; } boolean attested = STATE_ATTESTED.equals(state); diff --git a/quality-report.md b/quality-report.md index 283e223d2bd..0ffc02c7287 100644 --- a/quality-report.md +++ b/quality-report.md @@ -1,24 +1,13 @@ ## ✅ Continuous Quality Report ### Test & Coverage -- ✅ **Tests:** 4730 total, 0 failed, 0 skipped -- 📊 **Line coverage:** 58.65% - - **Lowest covered classes** - - `com.codename1.mcp.MCPServer` – 0.00% - - `com.codename1.gaming.level.GameSceneView` – 0.00% - - `com.codename1.crash.CrashProtection` – 0.00% - - `com.codename1.payment.CommerceManager` – 0.00% - - `com.codename1.crash.PiiScrubber` – 0.00% - - `com.codename1.crash.CrashReportPayload` – 0.00% - - `com.codename1.security.shield.ShieldConfig` – 0.00% - - `com.codename1.vr.VRView` – 0.00% - - `com.codename1.appreview.RatingDialog` – 0.00% - - `com.codename1.calendar.DefaultCalendarHttpTransport` – 0.00% +- ⚠️ No test results were found. +- ⚠️ Coverage report not generated. ### Static Analysis - **SpotBugs** - - ✅ **core-unittests:** 0 findings (no issues) -- ✅ **PMD:** 0 findings (no issues) -- ✅ **Checkstyle:** 0 findings (no issues) + - ✅ **ios:** 0 findings (no issues) +- ⚠️ PMD report not generated. +- ⚠️ Checkstyle report not generated. _Generated automatically by the PR CI workflow._ From 4f7705bf99422a5d1429db196f5502568c1b3afc Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 03:30:52 +0700 Subject: [PATCH 68/96] Fail closed on an interrupted wait, clear renamed headers, refresh repeat signals awaitInitialization() returned quietly when the wait was interrupted, which is indistinguishable from "initialization finished": attach() then saw an uninitialized shield, took its early return, and the request went out with no token and no pin check. ConnectionRequest does not consult the interrupt flag either, so nothing further down stopped it -- and the request this happens to is a protected one racing startup, on a fail-closed host. The interrupt now yields a ShieldException routed through the host's failure mode, with the interrupt status preserved for whoever set it. attach() cleared only the currently configured header name. ShieldConfig is mutable and getConfig() hands out the live instance, so an app that renames its token header between attempts left the bearer token in the request under the OLD name -- and a redirect to an unprotected host carried it there, which is exactly what the clearing exists to prevent. Every name a token has been attached under is now cleared before each attempt. ShieldSignals dropped an identical repeat entirely, keeping the first object. The bus documents itself as holding the most recent observation, and a persistent signal -- a root, a hooking framework -- is re-reported on every poll, so the entry the engine and the server were shown kept the timestamp of the first sighting while the device was still compromised. The entry is replaced; only the notification is suppressed. The Windows clean-target step retried every failure, so an intermittent product regression could pass on attempt two and turn a blocking gate green. It now matches the output for a dependency-resolution error, as the PR unit-test step does, and anything else is terminal on its first occurrence. Co-Authored-By: Claude Opus 5 (1M context) --- .github/workflows/parparvm-tests-windows.yml | 14 ++- .../codename1/security/shield/AppShield.java | 96 +++++++++++++--- .../security/shield/ShieldSignals.java | 20 +++- .../security/shield/ShieldApiTest.java | 50 +++++++++ .../security/shield/ShieldInitOrderTest.java | 103 ++++++++++++++++-- quality-report.md | 21 +++- 6 files changed, 272 insertions(+), 32 deletions(-) diff --git a/.github/workflows/parparvm-tests-windows.yml b/.github/workflows/parparvm-tests-windows.yml index 6c8071107d7..aec07be46a2 100644 --- a/.github/workflows/parparvm-tests-windows.yml +++ b/.github/workflows/parparvm-tests-windows.yml @@ -213,16 +213,26 @@ jobs: if ($LASTEXITCODE -eq 0) { $ok = $true; break } } if (-not $ok) { throw "mvn clean package failed after all retries" } + # This one RUNS TESTS, so the retry is restricted to the failure shape it + # exists for. A blanket loop lets an intermittent product regression pass on + # attempt two and turns a blocking gate green -- which is the opposite of what + # a gate is for, and worse than the flake it was hiding. + $resolutionFailure = 'status: (403|429|50[0-9])|Could not transfer artifact|Unresolveable build extension|Non-resolvable import POM|Could not resolve dependencies' $ok = $false foreach ($delay in 0, 30, 90) { if ($delay -gt 0) { - Write-Host "mvn failed; retrying in $delay s..." + Write-Host "mvn failed on a dependency-resolution error; retrying in $delay s..." Start-Sleep -Seconds $delay } # Single-quote the -D args: PowerShell otherwise mangles the dotted # property name (splitting it at the '.'). - mvn -B test -pl tests -am '-Dtest=CleanTargetIntegrationTest' '-Dsurefire.failIfNoSpecifiedTests=false' + # Tee-Object keeps the log on the console AND gives us something to match. + mvn -B test -pl tests -am '-Dtest=CleanTargetIntegrationTest' '-Dsurefire.failIfNoSpecifiedTests=false' 2>&1 | + Tee-Object -Variable mvnOutput if ($LASTEXITCODE -eq 0) { $ok = $true; break } + if (-not ($mvnOutput -match $resolutionFailure)) { + throw "mvn test failed for a reason that is not a transient dependency-resolution error; not retrying" + } } if (-not $ok) { throw "mvn test failed after all retries" } env: diff --git a/CodenameOne/src/com/codename1/security/shield/AppShield.java b/CodenameOne/src/com/codename1/security/shield/AppShield.java index f6828c84346..40a7cf33d17 100644 --- a/CodenameOne/src/com/codename1/security/shield/AppShield.java +++ b/CodenameOne/src/com/codename1/security/shield/AppShield.java @@ -179,6 +179,9 @@ static void resetForTesting() { lastStatus = ShieldStatus.NOT_INITIALIZED; runtimeHosts.clear(); listeners.removeAllElements(); + synchronized (attachedHeaderNames) { + attachedHeaderNames.removeAllElements(); + } AppShield.class.notifyAll(); } } @@ -195,17 +198,27 @@ static void resetForTesting() { /// Safe from a network thread, which is where [#attach(ConnectionRequest)] runs by /// contract, and it waits on the same monitor `init()` notifies -- so the wait ends /// when setup does, including when the engine threw and the `finally` released it. - private static void awaitInitialization() { + /// False when the wait was cut short by an interrupt, which is NOT the same as the + /// shield being up. + /// + /// Returning quietly made the interrupt look like "initialization finished": the + /// caller then saw `initialized == false`, took its early return, and the request + /// went out with no token and no pin check -- on a fail-closed host, which is the one + /// request that must not. `ConnectionRequest` does not consult the interrupt flag + /// either, so nothing further down stopped it. The interrupt status is preserved for + /// whoever set it and the caller is told the shield could not be waited for. + private static boolean awaitInitialization() { synchronized (AppShield.class) { while (initializing) { try { AppShield.class.wait(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); - return; + return false; } } } + return true; } /// Hooks the shield into the network stack, which is what makes @@ -396,18 +409,33 @@ public static void attach(ConnectionRequest request) throws ShieldException { // through the freshly installed guard while the flag is still false. Returning // there sent a protected request untouched, including for a host configured to // fail closed, which is exactly the request that must not go out unprotected. - awaitInitialization(); - if (!initialized) { - return; - } + boolean waited = awaitInitialization(); String url = request.getUrl(); String host = hostOf(url); HostPolicy policy = policyFor(host); - // Always clear first. A redirect reuses this request object with its - // headers intact, so a protected endpoint with an open redirect would - // otherwise hand a replayable token to whatever host it points at. - // Re-adding below is conditional on the *current* host's policy. - request.removeRequestHeader(getConfig().getTokenHeader()); + // Always clear first, and clear EVERY name a token has been attached under. + // + // A redirect reuses this request object with its headers intact, so a protected + // endpoint with an open redirect would otherwise hand a replayable token to + // whatever host it points at. Removing only the currently configured name was + // not enough for that: ShieldConfig is mutable and getConfig() hands out the live + // instance, so an app that renames its token header between attempts leaves the + // bearer token sitting in the request under the OLD name -- and the redirect + // carries it to the new host. The set is tiny (one entry unless an app renames + // the header) and only ever grows when a name is actually used. + clearAttachedHeaders(request); + if (!waited) { + // Interrupted mid-wait. Nothing is known about the shield, so this is + // routed through the host's failure mode exactly like an engine that + // could not produce a token: fail-closed refuses, fail-open proceeds. + failOrContinue(policy, new ShieldException(ShieldStatus.NOT_INITIALIZED, + "AppShield: interrupted while waiting for initialization, so no " + + "token could be attached for " + host)); + return; + } + if (!initialized) { + return; + } if (!policy.isAttachToken()) { return; } @@ -431,7 +459,9 @@ public static void attach(ConnectionRequest request) throws ShieldException { ShieldToken token = ShieldEngineRegistry.getEngine().fetchToken(null); setStatus(token == null ? ShieldStatus.SERVICE_DOWN : token.getStatus()); if (token != null && token.isValid()) { - request.addRequestHeader(getConfig().getTokenHeader(), token.getValue()); + String header = getConfig().getTokenHeader(); + rememberAttachedHeader(header); + request.addRequestHeader(header, token.getValue()); return; } failOrContinue(policy, new ShieldException( @@ -451,6 +481,38 @@ public static void attach(ConnectionRequest request) throws ShieldException { } } + /// Every header name a token has been attached under, so all of them can be cleared + /// before the next attempt. + /// + /// Not a single "current name", because the current name is read from a live, + /// mutable [ShieldConfig]: the name that has to be removed is the one that was used + /// when the header was set, which may no longer be configured. + private static final Vector attachedHeaderNames = new Vector(); + + private static void rememberAttachedHeader(String name) { + if (name == null || name.length() == 0) { + return; + } + synchronized (attachedHeaderNames) { + if (!attachedHeaderNames.contains(name)) { + attachedHeaderNames.addElement(name); + } + } + } + + private static void clearAttachedHeaders(ConnectionRequest request) { + // The configured name as well as the used ones: an app that changes the header + // before the first attach still has to have that name cleared on a redirect, + // and a token attached by an app calling addRequestHeader itself is its own + // business but cheaper to clear than to reason about. + request.removeRequestHeader(getConfig().getTokenHeader()); + synchronized (attachedHeaderNames) { + for (int i = 0; i < attachedHeaderNames.size(); i++) { + request.removeRequestHeader((String) attachedHeaderNames.elementAt(i)); + } + } + } + /// True for an absolute https URL. Anything else -- http, or a relative URL we cannot /// classify -- is not somewhere a bearer token belongs. static boolean isSecure(String url) { @@ -490,7 +552,15 @@ public static Hashtable headersFor(String url) { } // As in attach(): during startup this would silently return no headers, and a // BrowserComponent navigating a protected host would load it unauthenticated. - awaitInitialization(); + if (!awaitInitialization()) { + // This method returns headers rather than throwing, so there is no + // fail-closed path available here -- the caller decides what to do with an + // empty map. Saying so beats a silent one: a WebView that loads a protected + // page unauthenticated looks exactly like one that loaded it correctly. + Log.p("AppShield: interrupted while waiting for initialization, so no " + + "headers were produced for " + hostOf(url)); + return out; + } if (!initialized) { return out; } diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldSignals.java b/CodenameOne/src/com/codename1/security/shield/ShieldSignals.java index 8a61f065e73..61eddf55811 100644 --- a/CodenameOne/src/com/codename1/security/shield/ShieldSignals.java +++ b/CodenameOne/src/com/codename1/security/shield/ShieldSignals.java @@ -47,6 +47,9 @@ private ShieldSignals() { /// Records an observation. Repeat reports of an id already present update that entry in place. /// Safe to call from any thread; listeners are notified on the EDT. + /// + /// An identical repeat still updates the stored entry -- the snapshot is meant to hold + /// the most recent observation of each signal -- but does not notify again. public static void add(ShieldSignal signal) { if (signal == null || signal.getId() == null) { return; @@ -63,12 +66,21 @@ public static void add(ShieldSignal signal) { // without that, a detector polling on a timer queued a runnable per // poll per signal onto the EDT -- an unbounded queue behind a bus // whose whole selling point is that it is bounded. - if (existing.getSeverity() == signal.getSeverity() - && sameDetail(existing.getDetail(), signal.getDetail())) { - return; - } + boolean sameObservation = existing.getSeverity() == signal.getSeverity() + && sameDetail(existing.getDetail(), signal.getDetail()); + // The entry is replaced either way, and only the NOTIFICATION is + // suppressed. Keeping the old object was a second bug hiding behind + // the first: this bus documents itself as holding the most recent + // observation of each signal, and a persistent one -- a root, a + // hooking framework -- is re-reported on every poll, so the entry the + // engine and the server were shown kept the timestamp of the first + // sighting hours after the fact. "When did this device last look + // compromised" is a question the answer is used for. signals.setElementAt(signal, i); replaced = true; + if (sameObservation) { + return; + } break; } } diff --git a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java index ae3031e29cb..f0da08f6649 100644 --- a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java +++ b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java @@ -512,6 +512,56 @@ void theShieldGuardIsReachableAndStableSoAnAppCanDelegateToIt() { assertTrue(interesting.length > 0); } + /** + * An identical repeat refreshes the stored observation even though it notifies + * nobody. + * + *

The bus documents itself as holding the most recent observation of each signal, + * and a persistent one -- a root, a hooking framework -- is re-reported on every + * poll. Keeping the first object meant the timestamp the engine and the server were + * shown stayed at the first sighting, hours after the fact, while the device was + * still compromised. "When did this device last look compromised" is a question the + * answer is used for.

+ */ + @Test + void anIdenticalRepeatRefreshesTheStoredObservationWithoutNotifying() { + ShieldSignals.clear(); + final int[] notifications = {0}; + ShieldListener counter = new ShieldListener() { + public void signalRaised(ShieldSignal signal) { + notifications[0]++; + } + + public void tokenRefreshed(ShieldToken token) { + } + + public void statusChanged(ShieldStatus status) { + } + }; + ShieldSignals.addListener(counter); + try { + ShieldSignal first = new ShieldSignal(ShieldSignal.ROOT, 70, "su"); + ShieldSignals.add(first); + assertEquals(1, notifications[0]); + + ShieldSignal later = new ShieldSignal(ShieldSignal.ROOT, 70, "su"); + ShieldSignals.add(later); + assertEquals(1, notifications[0], + "an identical repeat must not notify again"); + + ShieldSignal[] snapshot = ShieldSignals.snapshot(); + assertEquals(1, snapshot.length); + // Identity rather than the timestamp value: two observations a moment apart + // can carry the same millisecond, and what matters is which object the bus + // is holding. + assertSame(later, snapshot[0], + "the snapshot has to hold the most recent observation"); + } finally { + ShieldSignals.removeListener(counter); + ShieldSignals.clear(); + } + } + @Test void hasSignalAtLeastReflectsRecordedSeverities() { ShieldSignals.clear(); diff --git a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldInitOrderTest.java b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldInitOrderTest.java index c246f1853c1..50f7dc528ba 100644 --- a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldInitOrderTest.java +++ b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldInitOrderTest.java @@ -155,6 +155,92 @@ public void run() { assertFalse(init.isAlive(), "init() should have finished"); } + /** + * An interrupt while waiting for startup does not turn a fail-closed host into an + * open one. + * + *

The wait used to return quietly on interrupt, which is indistinguishable from + * "initialization finished" -- the caller then saw the shield as uninitialized, took + * its early return, and the request went out with no token and no pin check. + * {@code ConnectionRequest} does not consult the interrupt flag either, so nothing + * further down stopped it. The one request that must never leave unprotected is + * exactly this one.

+ */ + @Test + void anInterruptedWaitStillRefusesAFailClosedRequest() throws Exception { + Thread init = initOnAnotherThread(); + assertTrue(engine.entered.await(GENEROUS_TIMEOUT_MS, TimeUnit.MILLISECONDS), + "the engine should have been asked to initialize"); + + final NetworkGuard guard = NetworkManager.getNetworkGuard(); + final RecordingRequest request = new RecordingRequest(); + request.setUrl("https://api.example.com/secure"); + final CountDownLatch done = new CountDownLatch(1); + final AtomicReference outcome = new AtomicReference(); + Thread caller = new Thread(new Runnable() { + public void run() { + try { + guard.beforeRequest(request); + } catch (Throwable t) { + outcome.set(t); + } + done.countDown(); + } + }, "shield-interrupted-request"); + caller.setDaemon(true); + caller.start(); + + // Let it park in the wait, then interrupt it there. + assertFalse(done.await(BLOCKED_OBSERVATION_MS, TimeUnit.MILLISECONDS)); + caller.interrupt(); + + assertTrue(done.await(GENEROUS_TIMEOUT_MS, TimeUnit.MILLISECONDS), + "the interrupt has to end the wait"); + assertNull(request.attached(), + "nothing can be attached without an initialized shield"); + assertTrue(outcome.get() instanceof ShieldException, + "a fail-closed host must refuse the request rather than let it go out " + + "unprotected, got: " + outcome.get()); + assertEquals(ShieldStatus.NOT_INITIALIZED, + ((ShieldException) outcome.get()).getStatus()); + + engine.release(); + init.join(GENEROUS_TIMEOUT_MS); + } + + /** + * Renaming the token header does not leave the previous one on a reused request. + * + *

{@code ShieldConfig} is mutable and {@code getConfig()} hands out the live + * instance, so the name that has to be cleared is the one the header was SET under, + * which may no longer be the configured one. Clearing only the current name left a + * bearer token in the request under its old name -- and a redirect to an unprotected + * host carried it there, which is precisely what the clearing exists to prevent.

+ */ + @Test + void renamingTheTokenHeaderStillClearsTheOneAlreadyAttached() throws Exception { + Thread init = initOnAnotherThread(); + engine.release(); + init.join(GENEROUS_TIMEOUT_MS); + assertFalse(init.isAlive()); + + RecordingRequest request = new RecordingRequest(); + request.setUrl("https://api.example.com/secure"); + AppShield.attach(request); + assertEquals("token-from-a-fully-initialized-engine", request.attached(), + "the fixture must actually attach something, or this proves nothing"); + + // The app renames its header between attempts, then the request is redirected to + // a host the shield does not protect. + AppShield.getConfig().tokenHeader("X-Other-Attest"); + request.setUrl("https://unprotected.example.com/elsewhere"); + AppShield.attach(request); + + assertNull(request.headerValue("X-CN1-Attest"), + "the token attached under the old name must not survive to another host"); + assertNull(request.headerValue("X-Other-Attest")); + } + private Thread initOnAnotherThread() { ShieldEngineRegistry.setEngine(engine); Thread t = new Thread(new Runnable() { @@ -174,26 +260,27 @@ public void run() { */ private static final class RecordingRequest extends ConnectionRequest { - private String token; + private final java.util.Map headers = + new java.util.LinkedHashMap(); @Override public void addRequestHeader(String key, String value) { super.addRequestHeader(key, value); - if ("X-CN1-Attest".equals(key)) { - token = value; - } + headers.put(key, value); } @Override public void removeRequestHeader(String key) { super.removeRequestHeader(key); - if ("X-CN1-Attest".equals(key)) { - token = null; - } + headers.remove(key); } String attached() { - return token; + return headers.get("X-CN1-Attest"); + } + + String headerValue(String name) { + return headers.get(name); } } diff --git a/quality-report.md b/quality-report.md index 0ffc02c7287..9f3692b8795 100644 --- a/quality-report.md +++ b/quality-report.md @@ -1,13 +1,24 @@ ## ✅ Continuous Quality Report ### Test & Coverage -- ⚠️ No test results were found. -- ⚠️ Coverage report not generated. +- ✅ **Tests:** 4733 total, 0 failed, 0 skipped +- 📊 **Line coverage:** 58.52% + - **Lowest covered classes** + - `com.codename1.mcp.MCPServer` – 0.00% + - `com.codename1.gaming.level.GameSceneView` – 0.00% + - `com.codename1.security.shield.AppShield` – 0.00% + - `com.codename1.crash.CrashProtection` – 0.00% + - `com.codename1.payment.CommerceManager` – 0.00% + - `com.codename1.crash.PiiScrubber` – 0.00% + - `com.codename1.crash.CrashReportPayload` – 0.00% + - `com.codename1.security.shield.ShieldConfig` – 0.00% + - `com.codename1.vr.VRView` – 0.00% + - `com.codename1.appreview.RatingDialog` – 0.00% ### Static Analysis - **SpotBugs** - - ✅ **ios:** 0 findings (no issues) -- ⚠️ PMD report not generated. -- ⚠️ Checkstyle report not generated. + - ✅ **core-unittests:** 0 findings (no issues) +- ✅ **PMD:** 0 findings (no issues) +- ✅ **Checkstyle:** 0 findings (no issues) _Generated automatically by the PR CI workflow._ From 304b888b78f4a4ada960c618cccbbf6d7c1b46bb Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 03:49:18 +0700 Subject: [PATCH 69/96] Marshal every off-EDT registration, not only one that finds the result settled Testing isDone() first reproduced in miniature the very bug this class exists to fix: a background caller that found the resource unfinished fell through to the synchronous path, and a completion landing in the gap before it registered made AsyncResource.ready deliver on the background thread after all. The window is small and what it produces -- a health callback touching a form off the EDT -- is a repaint glitch or a corrupted layout that nobody traces back to here. Hopping unconditionally has no such gap: registration and completion then both happen on the EDT, so they are ordered by the EDT rather than by a check. Already on the EDT still registers inline, which is what keeps a callback chain from queueing a runnable per link. Co-Authored-By: Claude Opus 5 (1M context) --- .../com/codename1/impl/health/EdtResult.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/CodenameOne/src/com/codename1/impl/health/EdtResult.java b/CodenameOne/src/com/codename1/impl/health/EdtResult.java index 9649d57df15..d74a17aa802 100644 --- a/CodenameOne/src/com/codename1/impl/health/EdtResult.java +++ b/CodenameOne/src/com/codename1/impl/health/EdtResult.java @@ -50,7 +50,8 @@ /// completes another resource does not queue a runnable per link. public final class EdtResult extends OneShot { - /// Late registration is delivered on the EDT too. + /// EVERY off-EDT registration is marshalled, not only one that finds the resource + /// already settled. /// /// `AsyncResource.ready` runs the callback immediately, on the registering thread, /// when the resource has already settled -- so the guarantee this class exists to @@ -61,6 +62,18 @@ public final class EdtResult extends OneShot { /// Off the EDT on a busy machine, on it on an idle one -- a callback that is usually /// on the EDT is exactly the thing the class doc calls not-a-design. /// + /// Testing `isDone()` first reproduced that in miniature: a background caller that + /// found the resource unfinished fell through to the synchronous path, and a + /// completion landing in the gap before it registered made `AsyncResource.ready` + /// deliver on the background thread after all. The window is small and the failure it + /// produces -- a health callback touching a form off the EDT -- is a repaint glitch or + /// a corrupted layout that nobody traces back to here. Hopping unconditionally has no + /// such gap: registration and completion then both happen on the EDT, so they are + /// ordered by the EDT rather than by a check. + /// + /// Already on the EDT still registers inline, which is what keeps a callback chain + /// from queueing a runnable per link. + /// /// Deliberately NOT done for `except`. Reading the error out of an already-failed /// resource by registering a callback and looking at what it captured is an /// established idiom here -- `HealthFallbackTest.errorOf` and `BtTestUtil` both do @@ -70,7 +83,7 @@ public final class EdtResult extends OneShot { /// happened is not that. @Override public AsyncResource ready(SuccessCallback callback, EasyThread t) { - if (isDone() && !isCancelled() && Display.isInitialized() + if (!isCancelled() && Display.isInitialized() && !Display.getInstance().isEdt()) { final SuccessCallback target = callback; final EasyThread thread = t; From 9386057a99d8413437e101c10f4ec90e9bbf7eef Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 04:05:42 +0700 Subject: [PATCH 70/96] Ignore Sec-WebSocket-Extensions, which no reader here can decode Extensions negotiate what the FRAMES mean. Emitting the header let a caller ask for permessage-deflate; a compliant server then agrees, sets RSV1 and sends compressed payloads -- and the readers mask off the opcode and pass the bytes straight through, so text arrives as mojibake and binary arrives compressed. The symptom appears at the application, nowhere near the header that caused it, and only against servers that happen to offer the extension. Reserved alongside the other handshake-control fields until a port can actually inflate. Co-Authored-By: Claude Opus 5 (1M context) --- .../src/com/codename1/impl/WebSocketImpl.java | 12 ++++++- .../java/com/codename1/io/WebSocketTest.java | 34 +++++++++++++++++++ quality-report.md | 2 +- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/CodenameOne/src/com/codename1/impl/WebSocketImpl.java b/CodenameOne/src/com/codename1/impl/WebSocketImpl.java index f1c16f3573e..64b3104a26f 100644 --- a/CodenameOne/src/com/codename1/impl/WebSocketImpl.java +++ b/CodenameOne/src/com/codename1/impl/WebSocketImpl.java @@ -139,7 +139,17 @@ private static boolean isReservedHandshakeHeader(String name) { String n = asciiLower(name); return "host".equals(n) || "upgrade".equals(n) || "connection".equals(n) || "sec-websocket-key".equals(n) || "sec-websocket-version".equals(n) - || "sec-websocket-protocol".equals(n) || "content-length".equals(n); + || "sec-websocket-protocol".equals(n) + // Extensions negotiate what the FRAMES mean, and no reader here + // implements one. Emitting it let a caller ask for permessage-deflate; + // a compliant server then agrees, sets RSV1 and sends compressed + // payloads -- and the readers mask off the opcode and pass the bytes + // straight through, so text arrives as mojibake and binary arrives + // compressed. The failure appears at the application, far from the one + // header that caused it, and only against servers that happen to offer + // the extension. Ignored until a port can actually inflate. + || "sec-websocket-extensions".equals(n) + || "content-length".equals(n); } /// Lowercases ASCII letters only, so the result never depends on the device locale. diff --git a/maven/core-unittests/src/test/java/com/codename1/io/WebSocketTest.java b/maven/core-unittests/src/test/java/com/codename1/io/WebSocketTest.java index 95a53fdb7d4..cf10e563f2e 100644 --- a/maven/core-unittests/src/test/java/com/codename1/io/WebSocketTest.java +++ b/maven/core-unittests/src/test/java/com/codename1/io/WebSocketTest.java @@ -14,6 +14,7 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -190,6 +191,32 @@ void isSupportedDelegatesToImplementation() { assertEquals(true, WebSocket.isSupported()); } + /** + * Extension negotiation is ignored, like the other handshake-control headers. + * + *

Extensions change what the FRAMES mean, and no reader in this repo implements + * one. Emitting the header let a caller ask for permessage-deflate; a compliant + * server then agrees, sets RSV1 and sends compressed payloads -- and the readers mask + * off the opcode and pass the bytes through, so text arrives as mojibake and binary + * arrives compressed. The symptom shows up at the application, nowhere near the + * header that caused it, and only against servers that happen to offer the + * extension.

+ */ + @Test + void extensionNegotiationIsNotEmittedInTheHandshake() { + MockImpl impl = new MockImpl("wss://example.com/socket"); + impl.setRequestHeader("Sec-WebSocket-Extensions", "permessage-deflate"); + impl.setRequestHeader("SEC-WEBSOCKET-EXTENSIONS", "permessage-deflate"); + impl.setRequestHeader("X-Mine", "kept"); + + String emitted = impl.testHandshakeHeaders(); + + assertFalse(emitted.toLowerCase().contains("sec-websocket-extensions"), + "an extension this client cannot decode must not be negotiated: " + emitted); + assertTrue(emitted.contains("X-Mine: kept"), + "and an ordinary header still goes out: " + emitted); + } + private final class WebSocketingImpl extends TestCodenameOneImplementation { boolean supported = true; @@ -260,6 +287,13 @@ String[] testRequestedSubprotocols() { return super.requestedSubprotocols(); } + /// Test-only view of what the handshake would actually emit. + String testHandshakeHeaders() { + StringBuilder sb = new StringBuilder(); + super.appendRequestHeaders(sb); + return sb.toString(); + } + void testSelect(String protocol) { super.setSelectedSubprotocol(protocol); } diff --git a/quality-report.md b/quality-report.md index 9f3692b8795..8d5854a431a 100644 --- a/quality-report.md +++ b/quality-report.md @@ -1,7 +1,7 @@ ## ✅ Continuous Quality Report ### Test & Coverage -- ✅ **Tests:** 4733 total, 0 failed, 0 skipped +- ✅ **Tests:** 4734 total, 0 failed, 0 skipped - 📊 **Line coverage:** 58.52% - **Lowest covered classes** - `com.codename1.mcp.MCPServer` – 0.00% From 8e315abf560763df49937ca631ad219572495889 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 04:10:18 +0700 Subject: [PATCH 71/96] Give WebSocketTest the copyright header the gate requires The header check runs over files a PR modifies, and this one never had one -- so the first change to it since the gate landed is the one that fails. Added rather than excluded: the file is ours and the header is the rule. Co-Authored-By: Claude Opus 5 (1M context) --- .../java/com/codename1/io/WebSocketTest.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/maven/core-unittests/src/test/java/com/codename1/io/WebSocketTest.java b/maven/core-unittests/src/test/java/com/codename1/io/WebSocketTest.java index cf10e563f2e..ad57258fdc7 100644 --- a/maven/core-unittests/src/test/java/com/codename1/io/WebSocketTest.java +++ b/maven/core-unittests/src/test/java/com/codename1/io/WebSocketTest.java @@ -1,3 +1,25 @@ +/* + * Copyright (c) 2026, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ package com.codename1.io; import com.codename1.impl.WebSocketEventSink; From f36b1b3bbd05b58b1b73b354b352361ab5f926fe Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 04:23:41 +0700 Subject: [PATCH 72/96] Move fetchToken's wait into the background task fetchToken() is the asynchronous API and it waited for initialization before handing back its AsyncResource, so a caller on the EDT froze for the length of a cold start -- and an engine whose own initialization needs anything dispatched to the EDT deadlocked outright, the EDT parked waiting for the initialization that was waiting for the EDT. The wait moves inside the scheduled task, which keeps the property it was there for: a caller racing startup still must not be told the shield was never initialized, which is a lie that lasts milliseconds and an error an app cannot tell from the real one. Co-Authored-By: Claude Opus 5 (1M context) --- .../codename1/security/shield/AppShield.java | 30 +++++++++---- .../security/shield/ShieldInitOrderTest.java | 43 +++++++++++++++++++ quality-report.md | 2 +- 3 files changed, 65 insertions(+), 10 deletions(-) diff --git a/CodenameOne/src/com/codename1/security/shield/AppShield.java b/CodenameOne/src/com/codename1/security/shield/AppShield.java index 40a7cf33d17..0b802e0ab09 100644 --- a/CodenameOne/src/com/codename1/security/shield/AppShield.java +++ b/CodenameOne/src/com/codename1/security/shield/AppShield.java @@ -348,18 +348,30 @@ public static AsyncResource fetchToken() { /// @param bindingData the data to bind to, typically a digest of the request body public static AsyncResource fetchToken(final String bindingData) { final AsyncResource result = new AsyncResource(); - // Same window as attach(): a caller racing startup would otherwise be told the - // shield was never initialized, which is a lie that lasts milliseconds and an - // error the app has no way to distinguish from the real one. - awaitInitialization(); - if (!initialized) { - result.error(new ShieldException(ShieldStatus.NOT_INITIALIZED, - "AppShield.init(...) has not been called")); - return result; - } Display.getInstance().scheduleBackgroundTask(new Runnable() { @Override public void run() { + // The wait happens HERE, not before the task is scheduled. + // + // Same window attach() covers -- a caller racing startup must not be told + // the shield was never initialized, which is a lie that lasts + // milliseconds and an error the app cannot tell from the real one -- but + // this method is the asynchronous one, and waiting for it in the caller + // froze whatever thread asked. On the EDT that is a visible stall for the + // length of a cold start, and if the engine's own initialization needs + // anything dispatched to the EDT it is a deadlock: the EDT is parked + // waiting for the initialization that is waiting for the EDT. + if (!awaitInitialization()) { + result.error(new ShieldException(ShieldStatus.NOT_INITIALIZED, + "AppShield was still initializing and the wait was " + + "interrupted")); + return; + } + if (!initialized) { + result.error(new ShieldException(ShieldStatus.NOT_INITIALIZED, + "AppShield.init(...) has not been called")); + return; + } try { ShieldToken token = ShieldEngineRegistry.getEngine().fetchToken(bindingData); setStatus(token.getStatus()); diff --git a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldInitOrderTest.java b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldInitOrderTest.java index 50f7dc528ba..875ca8e9165 100644 --- a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldInitOrderTest.java +++ b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldInitOrderTest.java @@ -23,6 +23,7 @@ package com.codename1.security.shield; import com.codename1.io.ConnectionRequest; +import com.codename1.util.AsyncResource; import com.codename1.junit.UITestBase; import com.codename1.io.NetworkGuard; import com.codename1.io.NetworkGuardTestAccess; @@ -241,6 +242,48 @@ void renamingTheTokenHeaderStillClearsTheOneAlreadyAttached() throws Exception { assertNull(request.headerValue("X-Other-Attest")); } + /** + * The asynchronous fetch does not become synchronous during startup. + * + *

{@code fetchToken()} is documented asynchronous, and it waited for + * initialization before returning its {@code AsyncResource} -- so a caller on the EDT + * froze for the length of a cold start, and an engine whose own initialization needs + * anything dispatched to the EDT deadlocked outright: the EDT parked waiting for the + * initialization that is waiting for the EDT.

+ */ + @Test + void fetchTokenReturnsWhileInitializationIsStillRunning() throws Exception { + Thread init = initOnAnotherThread(); + assertTrue(engine.entered.await(GENEROUS_TIMEOUT_MS, TimeUnit.MILLISECONDS), + "the engine should have been asked to initialize"); + + final AsyncResource[] handle = new AsyncResource[1]; + final CountDownLatch returned = new CountDownLatch(1); + Thread caller = new Thread(new Runnable() { + public void run() { + handle[0] = AppShield.fetchToken(); + returned.countDown(); + } + }, "shield-fetch-during-init"); + caller.setDaemon(true); + caller.start(); + + assertTrue(returned.await(GENEROUS_TIMEOUT_MS, TimeUnit.MILLISECONDS), + "fetchToken must hand back its resource without waiting for startup"); + assertNotNull(handle[0]); + assertFalse(handle[0].isDone(), + "and it cannot be finished yet -- the engine has not started"); + + engine.release(); + init.join(GENEROUS_TIMEOUT_MS); + long deadline = System.currentTimeMillis() + GENEROUS_TIMEOUT_MS; + while (!handle[0].isDone() && System.currentTimeMillis() < deadline) { + Thread.sleep(10L); + } + assertTrue(handle[0].isDone(), "the resource has to settle once startup finishes"); + assertEquals("token-from-a-fully-initialized-engine", handle[0].get().getValue()); + } + private Thread initOnAnotherThread() { ShieldEngineRegistry.setEngine(engine); Thread t = new Thread(new Runnable() { diff --git a/quality-report.md b/quality-report.md index 8d5854a431a..67a71da821c 100644 --- a/quality-report.md +++ b/quality-report.md @@ -1,7 +1,7 @@ ## ✅ Continuous Quality Report ### Test & Coverage -- ✅ **Tests:** 4734 total, 0 failed, 0 skipped +- ✅ **Tests:** 4735 total, 0 failed, 0 skipped - 📊 **Line coverage:** 58.52% - **Lowest covered classes** - `com.codename1.mcp.MCPServer` – 0.00% From 8c2790928d410aeb0b09d636def8f45dcc698046 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 04:38:31 +0700 Subject: [PATCH 73/96] Clear only the headers this request was given, and stop headersFor blocking The remembered header names were process-global and cleared from every request, as was the currently configured name. An app whose token header is also one an unprotected service legitimately expects -- X-API-Key being the obvious case -- therefore had the shield quietly delete that service's header on a request the shield has nothing to do with, presenting as the other service rejecting the call. Each attachment is now recorded against the request it was made on, with weak keys so a dropped request is not held alive and dead entries swept on every attach. headersFor() is documented as never blocking and exists for BrowserComponent, which calls it on the EDT -- so waiting for initialization froze the UI for a cold start and deadlocked outright against an engine whose initialization dispatches to the EDT. A synchronous method returning a map cannot say "later", so it answers now and logs when it had to answer without a token, which is the case an app can fix by calling init() before navigating. Co-Authored-By: Claude Opus 5 (1M context) --- .../codename1/security/shield/AppShield.java | 111 +++++++++++++----- .../security/shield/ShieldInitOrderTest.java | 31 +++++ quality-report.md | 4 +- 3 files changed, 116 insertions(+), 30 deletions(-) diff --git a/CodenameOne/src/com/codename1/security/shield/AppShield.java b/CodenameOne/src/com/codename1/security/shield/AppShield.java index 0b802e0ab09..f933ed82cef 100644 --- a/CodenameOne/src/com/codename1/security/shield/AppShield.java +++ b/CodenameOne/src/com/codename1/security/shield/AppShield.java @@ -472,7 +472,7 @@ public static void attach(ConnectionRequest request) throws ShieldException { setStatus(token == null ? ShieldStatus.SERVICE_DOWN : token.getStatus()); if (token != null && token.isValid()) { String header = getConfig().getTokenHeader(); - rememberAttachedHeader(header); + rememberAttachedHeader(request, header); request.addRequestHeader(header, token.getValue()); return; } @@ -493,34 +493,80 @@ public static void attach(ConnectionRequest request) throws ShieldException { } } - /// Every header name a token has been attached under, so all of them can be cleared - /// before the next attempt. + /// The header name a token was attached under, per REQUEST. /// - /// Not a single "current name", because the current name is read from a live, - /// mutable [ShieldConfig]: the name that has to be removed is the one that was used - /// when the header was set, which may no longer be configured. + /// A single "current name" is not enough: [ShieldConfig] is mutable and + /// [#getConfig()] hands out the live instance, so the name that has to be removed on + /// a redirect is the one used when the header was set, which may no longer be + /// configured. A process-wide list of every name ever used is too much: the shield + /// would then strip that name from EVERY request, so an app whose token header is + /// also a header some unprotected service legitimately expects -- `X-API-Key` is the + /// obvious one -- would find the shield quietly deleting it on the way out, on a + /// request the shield has nothing to do with. + /// + /// So the name is remembered against the request it was attached to. Weak keys, + /// because a request that is dropped rather than redirected must not be held alive + /// by this, and the dead entries are swept on every attach so the list cannot grow + /// without bound in a long-lived app. private static final Vector attachedHeaderNames = new Vector(); - private static void rememberAttachedHeader(String name) { - if (name == null || name.length() == 0) { + /// One remembered attachment: which request, and the name used. + private static final class AttachedHeader { + + private final java.lang.ref.WeakReference request; + private final String name; + + AttachedHeader(ConnectionRequest request, String name) { + this.request = new java.lang.ref.WeakReference(request); + this.name = name; + } + + ConnectionRequest get() { + return (ConnectionRequest) request.get(); + } + } + + private static void rememberAttachedHeader(ConnectionRequest request, String name) { + if (request == null || name == null || name.length() == 0) { return; } synchronized (attachedHeaderNames) { - if (!attachedHeaderNames.contains(name)) { - attachedHeaderNames.addElement(name); + sweepAttachedHeaders(); + for (int i = 0; i < attachedHeaderNames.size(); i++) { + AttachedHeader entry = (AttachedHeader) attachedHeaderNames.elementAt(i); + if (entry.get() == request && name.equals(entry.name)) { // NOPMD identity + return; + } } + attachedHeaderNames.addElement(new AttachedHeader(request, name)); } } private static void clearAttachedHeaders(ConnectionRequest request) { - // The configured name as well as the used ones: an app that changes the header - // before the first attach still has to have that name cleared on a redirect, - // and a token attached by an app calling addRequestHeader itself is its own - // business but cheaper to clear than to reason about. - request.removeRequestHeader(getConfig().getTokenHeader()); + // ONLY what this request was given. Clearing the configured name unconditionally + // was the same mistake one step smaller: it reached every request, so an app whose + // token header is also one an unprotected service expects lost that service's + // header on a call the shield has nothing to do with. A header the shield did not + // attach is the app's, whatever it is called. synchronized (attachedHeaderNames) { - for (int i = 0; i < attachedHeaderNames.size(); i++) { - request.removeRequestHeader((String) attachedHeaderNames.elementAt(i)); + for (int i = attachedHeaderNames.size() - 1; i >= 0; i--) { + AttachedHeader entry = (AttachedHeader) attachedHeaderNames.elementAt(i); + ConnectionRequest owner = entry.get(); + if (owner == null) { + attachedHeaderNames.removeElementAt(i); + } else if (owner == request) { // NOPMD identity: this request, not an equal one + request.removeRequestHeader(entry.name); + attachedHeaderNames.removeElementAt(i); + } + } + } + } + + /// Drops entries whose request has been collected. Called under the lock. + private static void sweepAttachedHeaders() { + for (int i = attachedHeaderNames.size() - 1; i >= 0; i--) { + if (((AttachedHeader) attachedHeaderNames.elementAt(i)).get() == null) { + attachedHeaderNames.removeElementAt(i); } } } @@ -562,18 +608,27 @@ public static Hashtable headersFor(String url) { if (url == null) { return out; } - // As in attach(): during startup this would silently return no headers, and a - // BrowserComponent navigating a protected host would load it unauthenticated. - if (!awaitInitialization()) { - // This method returns headers rather than throwing, so there is no - // fail-closed path available here -- the caller decides what to do with an - // empty map. Saying so beats a silent one: a WebView that loads a protected - // page unauthenticated looks exactly like one that loaded it correctly. - Log.p("AppShield: interrupted while waiting for initialization, so no " - + "headers were produced for " + hostOf(url)); - return out; - } + // Deliberately does NOT wait for initialization. + // + // This method is documented as never blocking and is called from the EDT -- + // BrowserComponent is its reason for existing -- so waiting here froze the UI for + // the length of a cold start, and an engine whose initialization dispatches + // anything to the EDT deadlocked: the EDT parked on the initialization that was + // waiting for the EDT. A synchronous method that returns a map has no way to say + // "later", so the only honest options are to answer now or to hang, and hanging + // the UI is not an option. + // + // The cost is real and belongs in the log rather than in silence: a + // BrowserComponent navigating a protected host during startup loads it without a + // token, and that looks exactly like a page that loaded correctly. Apps that + // navigate to a protected host at launch should call init() before doing so, or + // use fetchToken(), which does wait -- on a background thread. if (!initialized) { + if (initializing) { + Log.p("AppShield: headersFor(" + hostOf(url) + ") was called while " + + "initialization is still running, so no token is attached. Call " + + "AppShield.init(...) before navigating to a protected host."); + } return out; } if (!policyFor(hostOf(url)).isAttachToken()) { diff --git a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldInitOrderTest.java b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldInitOrderTest.java index 875ca8e9165..d972febab7e 100644 --- a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldInitOrderTest.java +++ b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldInitOrderTest.java @@ -284,6 +284,37 @@ public void run() { assertEquals("token-from-a-fully-initialized-engine", handle[0].get().getValue()); } + /** + * The cleanup only touches the request the shield actually decorated. + * + *

The remembered names were process-global, so every request lost them -- and an + * app whose token header is also one an unprotected service legitimately expects + * ({@code X-API-Key} being the obvious case) found the shield quietly deleting that + * service's header on a request the shield has nothing to do with. Removing a header + * an app set itself is a bug that presents as the other service rejecting the call.

+ */ + @Test + void anotherRequestKeepsAHeaderTheAppSetItself() throws Exception { + Thread init = initOnAnotherThread(); + engine.release(); + init.join(GENEROUS_TIMEOUT_MS); + + RecordingRequest protectedRequest = new RecordingRequest(); + protectedRequest.setUrl("https://api.example.com/secure"); + AppShield.attach(protectedRequest); + assertEquals("token-from-a-fully-initialized-engine", protectedRequest.attached()); + + // A different request, to a host the shield does not protect, carrying a header + // of the app's own that happens to share the token header's name. + RecordingRequest ownRequest = new RecordingRequest(); + ownRequest.setUrl("https://elsewhere.example.com/thing"); + ownRequest.addRequestHeader("X-CN1-Attest", "the-app-put-this-here"); + AppShield.attach(ownRequest); + + assertEquals("the-app-put-this-here", ownRequest.headerValue("X-CN1-Attest"), + "the shield must not strip a header it did not attach to this request"); + } + private Thread initOnAnotherThread() { ShieldEngineRegistry.setEngine(engine); Thread t = new Thread(new Runnable() { diff --git a/quality-report.md b/quality-report.md index 67a71da821c..9bc5360f39d 100644 --- a/quality-report.md +++ b/quality-report.md @@ -1,8 +1,8 @@ ## ✅ Continuous Quality Report ### Test & Coverage -- ✅ **Tests:** 4735 total, 0 failed, 0 skipped -- 📊 **Line coverage:** 58.52% +- ✅ **Tests:** 4736 total, 0 failed, 0 skipped +- 📊 **Line coverage:** 58.51% - **Lowest covered classes** - `com.codename1.mcp.MCPServer` – 0.00% - `com.codename1.gaming.level.GameSceneView` – 0.00% From 28534df6f629894527ad22062d2c9ef91aa9b079 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 04:48:06 +0700 Subject: [PATCH 74/96] Record an accepted attestation durably, not only in process memory When the write recording an accepted attestation is refused, the key is attested with Apple and storage still says "new" with a start marker. The in-memory copy covers this process; a restart does not have it, so the next launch reads an accepted key as an interrupted attempt and discards it for another rate-limited one -- every launch, for as long as the keychain stays unwilling. The acceptance is now also written to a separate keychain item, which is the point: the state write has just failed, and a keychain can refuse one item and take another it has never seen. The request path recognizes it and treats the key as pending registration rather than as an unknown outcome, so the grace window promotes it exactly as it would have. If that write is refused too, the in-memory copy is what is left and the loss is bounded to one key instead of one per launch. It is distinct from the two markers next to it and means the opposite of both: KEY_ATTEST_STARTED is "submitted, outcome unknown", attestAnsweredForKey is "Apple answered with an error that did not consume the key", and this is "the one-time attestation is spent and it succeeded". Cleared wherever the identity is finished or discarded. Co-Authored-By: Claude Opus 5 (1M context) --- .../impl/ios/IOSDeviceIntegrity.java | 41 +++++++++++++++++++ quality-report.md | 21 +++------- 2 files changed, 46 insertions(+), 16 deletions(-) diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index 6eb38bbb347..e32434b560b 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -136,6 +136,23 @@ final class IOSDeviceIntegrity { */ private static final String KEY_RECOVERY_SPENT = "cn1.appattest.recoverySpent"; + /** + * The key Apple has ACCEPTED an attestation for, when the state write that should + * have recorded that failed. + * + *

Distinct from {@link #KEY_ATTEST_STARTED}, which means "submitted, outcome + * unknown", and from {@link #attestAnsweredForKey}, which means "Apple answered with + * an error that did not consume the key". This one means the opposite of both: the + * one-time attestation is spent and it succeeded.

+ * + *

Written as a separate item precisely because the state write has just been + * refused -- a keychain can fail one item and take another, and the alternative is + * relying on process memory for a fact that costs a rate-limited hardware key every + * time it is forgotten. A restart with only the in-memory copy read an accepted key + * as an interrupted attempt and discarded it.

+ */ + private static final String KEY_ATTEST_ACCEPTED = "cn1.appattest.attestAccepted"; + private static final String STATE_NEW = "new"; private static final String STATE_ATTESTED = "attested"; /** @@ -326,6 +343,10 @@ private void resetLocked(boolean keepSpentMarker) { boolean markerGone = true; if (idGone && stateGone) { store.remove(KEY_ATTEST_STARTED); + // The acceptance belonged to the identity that has just gone with it. Left + // behind, it would name a key that no longer exists -- harmless while the + // identifier is absent, and wrong the moment a replacement is generated. + store.remove(KEY_ATTEST_ACCEPTED); attestAnsweredForKey = null; // The identity is gone, so anything this process remembered about it is too. inMemoryStateKeyId = null; @@ -472,6 +493,7 @@ private void promoteToAttested(SecureStorage store, String keyId, boolean holdIn pendingSinceInMemory = 0L; store.remove(KEY_PENDING_SINCE); store.remove(KEY_ATTEST_STARTED); + store.remove(KEY_ATTEST_ACCEPTED); attestAnsweredForKey = null; recoverySpentInMemory = !store.remove(KEY_RECOVERY_SPENT); } @@ -531,6 +553,15 @@ AsyncResource requestToken(String nonce) { if (keyId != null && keyId.equals(inMemoryStateKeyId) && inMemoryState != null) { state = inMemoryState; } + // And the durable record of an acceptance whose state write was refused. This + // is what makes the in-memory copy above survive a restart: without it the + // next launch finds STATE_NEW plus a start marker, reads an accepted key as + // an interrupted attempt, and discards it for another rate-limited one -- + // every launch, for as long as the keychain stays unwilling. + if (keyId != null && !STATE_ATTESTED.equals(state) + && keyId.equals(store.get(KEY_ATTEST_ACCEPTED))) { + state = STATE_PENDING; + } if (STATE_PENDING.equals(state) && keyId != null && keyId.length() > 0) { if (registrationGraceRemaining() > 0) { // The key is attested with Apple but no backend has confirmed @@ -618,6 +649,7 @@ AsyncResource requestToken(String nonce) { } store.remove(KEY_STATE); store.remove(KEY_ATTEST_STARTED); + store.remove(KEY_ATTEST_ACCEPTED); // The discarded key is the one the in-memory marker named. attestAnsweredForKey = null; PendingRequest fresh = new PendingRequest(r, nonce, @@ -891,6 +923,15 @@ public static void nativeAttestationReady(final int requestId, final String atte instance.inMemoryStateKeyId = pending.keyId; instance.inMemoryState = STATE_PENDING; instance.pendingSinceInMemory = System.currentTimeMillis(); + // And durably, in a DIFFERENT item, because process memory does not + // survive the restart this is most likely to be followed by: storage + // still says "new" with a start marker, which the next launch reads as an + // interrupted attestation and answers by discarding the key -- spending + // another rate-limited one on a key Apple has already accepted. A + // keychain that refused the state write can still take an item it has + // never seen; if it refuses this too, the in-memory copy is what is left + // and the loss is bounded to one key rather than one per launch. + store.set(KEY_ATTEST_ACCEPTED, pending.keyId); instance.bootstrapInFlight = false; instance.failBootstrapWaiters("App Attest is completing first-run " + "registration for this device; retry shortly"); diff --git a/quality-report.md b/quality-report.md index 9bc5360f39d..0ffc02c7287 100644 --- a/quality-report.md +++ b/quality-report.md @@ -1,24 +1,13 @@ ## ✅ Continuous Quality Report ### Test & Coverage -- ✅ **Tests:** 4736 total, 0 failed, 0 skipped -- 📊 **Line coverage:** 58.51% - - **Lowest covered classes** - - `com.codename1.mcp.MCPServer` – 0.00% - - `com.codename1.gaming.level.GameSceneView` – 0.00% - - `com.codename1.security.shield.AppShield` – 0.00% - - `com.codename1.crash.CrashProtection` – 0.00% - - `com.codename1.payment.CommerceManager` – 0.00% - - `com.codename1.crash.PiiScrubber` – 0.00% - - `com.codename1.crash.CrashReportPayload` – 0.00% - - `com.codename1.security.shield.ShieldConfig` – 0.00% - - `com.codename1.vr.VRView` – 0.00% - - `com.codename1.appreview.RatingDialog` – 0.00% +- ⚠️ No test results were found. +- ⚠️ Coverage report not generated. ### Static Analysis - **SpotBugs** - - ✅ **core-unittests:** 0 findings (no issues) -- ✅ **PMD:** 0 findings (no issues) -- ✅ **Checkstyle:** 0 findings (no issues) + - ✅ **ios:** 0 findings (no issues) +- ⚠️ PMD report not generated. +- ⚠️ Checkstyle report not generated. _Generated automatically by the PR CI workflow._ From 10fc6cbb118d1cf62761cec5605d2978268795ca Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 05:03:53 +0700 Subject: [PATCH 75/96] Reject the two header names an Android request rewrites on its way out Accept-Encoding is cleared on a HEAD request to work around a platform bug, and X-HTTP-Method-Override is set when the PATCH fallback is in use. Both overwrite whatever was there, after the request's own headers are in place, so a token in one of them is replaced between attach() reporting success and the request going out -- and a fail-closed host proceeds having attached nothing. Worse than a plain collision because it is conditional: one HTTP method, one platform. It would pass every test that did not happen to use that exact shape and fail in production for the requests that do. Co-Authored-By: Claude Opus 5 (1M context) --- .../security/shield/ShieldConfig.java | 17 +++++++++++++-- .../security/shield/ShieldApiTest.java | 5 +++++ quality-report.md | 21 ++++++++++++++----- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java b/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java index 3202171bcb2..4373e0ac099 100644 --- a/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java +++ b/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java @@ -105,7 +105,7 @@ public ShieldConfig tokenHeader(String name) { /// Header names the transport owns, so an attestation token put in one does not /// arrive as a header at all. /// - /// Three families, all lower-cased for comparison because header names are + /// Four families, all lower-cased for comparison because header names are /// case-insensitive: /// /// - framing (`content-length`, `transfer-encoding`) -- the transport computes these @@ -116,7 +116,13 @@ public ShieldConfig tokenHeader(String name) { /// - hop-by-hop (`connection`, `keep-alive`, `proxy-connection`, `te`, `trailer`, /// `upgrade`, `proxy-authorization`, `proxy-authenticate`) -- defined to be /// consumed by the next hop and not forwarded, so the token would be stripped in - /// transit by a proxy that is behaving correctly. + /// transit by a proxy that is behaving correctly; + /// - written by a port on the way out (`accept-encoding`, `x-http-method-override`) + /// -- the Android implementation clears `Accept-Encoding` on a HEAD request to work + /// around a platform bug, and sets `X-HTTP-Method-Override` itself when its PATCH + /// fallback is in use. Both overwrite whatever was there, so a token in one of them + /// is replaced between `attach()` reporting success and the request going out, and + /// only on the platform and request shape that triggers it. /// /// Refused rather than warned about, because the failure has no symptom on the /// client: `attach()` returns having set the header, and the request reaches the @@ -132,6 +138,13 @@ public ShieldConfig tokenHeader(String name) { // appears only once a user is behind such a proxy, on a network nobody testing // this was on. "proxy-authorization", "proxy-authenticate", + // Not transport metadata: these are written by a port. Android clears + // Accept-Encoding on HEAD around a platform bug and sets X-HTTP-Method-Override + // for its PATCH fallback, both after the request's own headers are in place. The + // failure is worse than a plain collision because it is conditional -- one HTTP + // method, one platform -- so it would pass every test that did not happen to use + // that shape. + "accept-encoding", "x-http-method-override", // Cookie is not transport framing, but it fails the same way and worse. // ConnectionRequest emits userHeaders first and THEN calls setHeader("Cookie", // ...) with the generated cookie string, so with cookie handling on and any diff --git a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java index f0da08f6649..2b1639c66a5 100644 --- a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java +++ b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java @@ -469,6 +469,11 @@ void transportOwnedHeadersAreRefusedAsTheTokenHeader() { // sees the token -- and only for users behind such a proxy, which is a // network nobody testing this was on. "Proxy-Authorization", "Proxy-Authenticate", + // Written by a port on the way out: Android clears Accept-Encoding on HEAD + // around a platform bug, and sets X-HTTP-Method-Override for its PATCH + // fallback. Conditional on the method and the platform, so a token in one of + // them survives every test that does not use that exact shape. + "Accept-Encoding", "X-HTTP-Method-Override", // Not framing, but overwritten by the request itself: ConnectionRequest // emits userHeaders and then sets Cookie from its own cookie store. "Cookie" diff --git a/quality-report.md b/quality-report.md index 0ffc02c7287..9bc5360f39d 100644 --- a/quality-report.md +++ b/quality-report.md @@ -1,13 +1,24 @@ ## ✅ Continuous Quality Report ### Test & Coverage -- ⚠️ No test results were found. -- ⚠️ Coverage report not generated. +- ✅ **Tests:** 4736 total, 0 failed, 0 skipped +- 📊 **Line coverage:** 58.51% + - **Lowest covered classes** + - `com.codename1.mcp.MCPServer` – 0.00% + - `com.codename1.gaming.level.GameSceneView` – 0.00% + - `com.codename1.security.shield.AppShield` – 0.00% + - `com.codename1.crash.CrashProtection` – 0.00% + - `com.codename1.payment.CommerceManager` – 0.00% + - `com.codename1.crash.PiiScrubber` – 0.00% + - `com.codename1.crash.CrashReportPayload` – 0.00% + - `com.codename1.security.shield.ShieldConfig` – 0.00% + - `com.codename1.vr.VRView` – 0.00% + - `com.codename1.appreview.RatingDialog` – 0.00% ### Static Analysis - **SpotBugs** - - ✅ **ios:** 0 findings (no issues) -- ⚠️ PMD report not generated. -- ⚠️ Checkstyle report not generated. + - ✅ **core-unittests:** 0 findings (no issues) +- ✅ **PMD:** 0 findings (no issues) +- ✅ **Checkstyle:** 0 findings (no issues) _Generated automatically by the PR CI workflow._ From 2f18db59d766a9e13bad7055b8d7b0b226ab3337 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 05:10:04 +0700 Subject: [PATCH 76/96] Release the open lock through an iterator rather than during a foreach The foreach version could not throw: it returned before the iterator was touched again. But that made its safety depend on a `return` three lines away, which is the kind of coupling a later edit breaks with nothing to say so -- and the only symptom would be a ConcurrentModificationException on the MCP start/stop path. Iterator.remove is safe on its own terms. Co-Authored-By: Claude Opus 5 (1M context) --- CodenameOne/src/com/codename1/mcp/MCPServer.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/CodenameOne/src/com/codename1/mcp/MCPServer.java b/CodenameOne/src/com/codename1/mcp/MCPServer.java index 6910c848e36..3132c269366 100644 --- a/CodenameOne/src/com/codename1/mcp/MCPServer.java +++ b/CodenameOne/src/com/codename1/mcp/MCPServer.java @@ -341,14 +341,18 @@ private Object acquireOpenLock(MCPTransport t) { private void releaseOpenLock(MCPTransport t) { synchronized (openLocks) { - for (Object[] entry : openLocks) { + // Iterator.remove rather than List.remove during a foreach. The foreach + // version could not actually throw -- it returned before the iterator was + // touched again -- but that made its safety depend on a `return` three lines + // away, which is the kind of coupling a later edit breaks without anything + // saying so. This shape is safe on its own terms. + for (java.util.Iterator it = openLocks.iterator(); it.hasNext();) { + Object[] entry = it.next(); if (entry[0] == t) { // NOPMD identity: one lock per transport INSTANCE int[] users = (int[]) entry[2]; users[0]--; if (users[0] <= 0) { - // Safe to remove while iterating only because this returns - // immediately afterwards. - openLocks.remove(entry); + it.remove(); } return; } From 285b8ec5bf6dc1dcf42b2093c8dc5c6ae0bc3f55 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 05:31:18 +0700 Subject: [PATCH 77/96] Keep an attestation nobody took delivery of, rather than promoting a key the backend never saw A caller can cancel the AsyncResource while Apple is working. The callback still recorded the key as pending, and succeed() correctly declined to complete a resource that was already done -- so the attestation object, the one thing here that cannot be produced twice, was dropped. The grace window then promoted a key the backend had never seen: every assertion rejected, a reset, another rate-limited key. It is retained now, with the nonce it was made over, and handed to a retry that asks for the same challenge. A retry carrying a different nonce cannot use it -- an attestation covers one challenge and the backend checks that -- so the key is discarded there and replaced instead of being promoted into guaranteed rejection. That still costs one key, once and deterministically, rather than one plus a round of failures. The local SpotBugs gate caught a redundant null check on the nonce in the first version of this; requestToken has already rejected a null one by that point. Co-Authored-By: Claude Opus 5 (1M context) --- .../impl/ios/IOSDeviceIntegrity.java | 84 ++++++++++++++++++- quality-report.md | 21 ++--- 2 files changed, 87 insertions(+), 18 deletions(-) diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index e32434b560b..e6b388455f1 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -248,6 +248,25 @@ final class IOSDeviceIntegrity { */ private String attestAnsweredForKey; + /** + * The one-time attestation nobody took delivery of, and the request it was made for. + * + *

A caller can cancel the {@code AsyncResource} while Apple is working. The + * callback still records the key as pending, but the attestation object -- the one + * thing here that cannot be produced twice -- was on its way to a caller that is no + * longer listening, and was simply dropped. The grace window then promoted a key the + * backend had never seen, so every assertion was rejected, which reset and spent + * another rate-limited key.

+ * + *

Held so a retry can still take delivery. The nonce is part of it because an + * attestation is made over one challenge: handing it to a request carrying a + * different nonce would produce a token the backend rejects, which is the failure + * this exists to avoid rather than a way out of it.

+ */ + private String undeliveredAttestKeyId; + private String undeliveredAttestNonce; + private String undeliveredAttestToken; + /** * Set when the keychain refused to delete part of a discarded identity. * @@ -349,6 +368,9 @@ private void resetLocked(boolean keepSpentMarker) { store.remove(KEY_ATTEST_ACCEPTED); attestAnsweredForKey = null; // The identity is gone, so anything this process remembered about it is too. + undeliveredAttestKeyId = null; + undeliveredAttestNonce = null; + undeliveredAttestToken = null; inMemoryStateKeyId = null; inMemoryState = null; pendingSinceInMemory = 0L; @@ -562,6 +584,33 @@ AsyncResource requestToken(String nonce) { && keyId.equals(store.get(KEY_ATTEST_ACCEPTED))) { state = STATE_PENDING; } + if (STATE_PENDING.equals(state) && keyId != null && keyId.length() > 0 + && keyId.equals(undeliveredAttestKeyId)) { + // An attestation was produced for this key and nobody took it -- the + // caller cancelled while Apple was working. It cannot be produced again, + // so it is handed to the retry that asks for the same challenge. + if (nonce.equals(undeliveredAttestNonce)) { + String token = undeliveredAttestToken; + undeliveredAttestKeyId = null; + undeliveredAttestNonce = null; + undeliveredAttestToken = null; + r.complete(token); + return r; + } + // A different challenge, so the retained object is worthless: an + // attestation is made over one nonce and the backend checks it. What must + // NOT happen is the grace window promoting this key -- the backend has + // never seen it, so every assertion would be rejected, and the reset that + // follows costs a rate-limited key anyway on top of the failures. Discard + // it here instead: one key, once, deterministically, with the next lines + // generating its replacement. + undeliveredAttestKeyId = null; + undeliveredAttestNonce = null; + undeliveredAttestToken = null; + resetLocked(); + keyId = null; + state = null; + } if (STATE_PENDING.equals(state) && keyId != null && keyId.length() > 0) { if (registrationGraceRemaining() > 0) { // The key is attested with Apple but no backend has confirmed @@ -1005,8 +1054,22 @@ public static void nativeAttestationReady(final int requestId, final String atte instance.failBootstrapWaiters("App Attest is completing first-run " + "registration for this device; retry shortly"); } - succeed(pending, TOKEN_PREFIX + ":attest:" + base64(bytes(pending.keyId)) - + ":" + attestationB64); + String attestToken = TOKEN_PREFIX + ":attest:" + base64(bytes(pending.keyId)) + + ":" + attestationB64; + if (instance != null) { + // Retained until somebody takes delivery. The caller may have cancelled while + // Apple was working, and succeed() correctly declines to complete a resource + // that is already done -- but the attestation object is the one thing here + // that cannot be produced again, so dropping it left a key the backend could + // never register, which the grace window then promoted into assertions that + // are rejected and a reset that costs another rate-limited key. + synchronized (instance.flowLock) { + instance.undeliveredAttestKeyId = pending.keyId; + instance.undeliveredAttestNonce = pending.nonce; + instance.undeliveredAttestToken = attestToken; + } + } + succeed(pending, attestToken, true); } /** Called from native with an assertion over an already attested key. */ @@ -1249,6 +1312,16 @@ private static PendingRequest take(int requestId) { * generation is therefore checked again at the moment of publication.

*/ private static void succeed(final PendingRequest pending, final String token) { + succeed(pending, token, false); + } + + /// @param attestation true when `token` carries the one-time attestation object, so + /// the retained copy can be released once a caller has actually taken it. A + /// caller that cancelled never does, and the copy is what lets the next + /// request finish the registration instead of promoting a key the backend has + /// never seen. + private static void succeed(final PendingRequest pending, final String token, + final boolean attestation) { Display.getInstance().callSerially(new Runnable() { public void run() { if (pending.result.isDone()) { @@ -1274,6 +1347,13 @@ public void run() { + "reset while this request was in flight")); return; } + if (attestation && token.equals(instance.undeliveredAttestToken)) { + // Somebody is taking delivery, so there is nothing left to + // retain. Released under the lock the retry path reads it under. + instance.undeliveredAttestKeyId = null; + instance.undeliveredAttestNonce = null; + instance.undeliveredAttestToken = null; + } } pending.result.complete(token); } diff --git a/quality-report.md b/quality-report.md index 9bc5360f39d..0ffc02c7287 100644 --- a/quality-report.md +++ b/quality-report.md @@ -1,24 +1,13 @@ ## ✅ Continuous Quality Report ### Test & Coverage -- ✅ **Tests:** 4736 total, 0 failed, 0 skipped -- 📊 **Line coverage:** 58.51% - - **Lowest covered classes** - - `com.codename1.mcp.MCPServer` – 0.00% - - `com.codename1.gaming.level.GameSceneView` – 0.00% - - `com.codename1.security.shield.AppShield` – 0.00% - - `com.codename1.crash.CrashProtection` – 0.00% - - `com.codename1.payment.CommerceManager` – 0.00% - - `com.codename1.crash.PiiScrubber` – 0.00% - - `com.codename1.crash.CrashReportPayload` – 0.00% - - `com.codename1.security.shield.ShieldConfig` – 0.00% - - `com.codename1.vr.VRView` – 0.00% - - `com.codename1.appreview.RatingDialog` – 0.00% +- ⚠️ No test results were found. +- ⚠️ Coverage report not generated. ### Static Analysis - **SpotBugs** - - ✅ **core-unittests:** 0 findings (no issues) -- ✅ **PMD:** 0 findings (no issues) -- ✅ **Checkstyle:** 0 findings (no issues) + - ✅ **ios:** 0 findings (no issues) +- ⚠️ PMD report not generated. +- ⚠️ Checkstyle report not generated. _Generated automatically by the PR CI workflow._ From 60545af4a9305452298545af221ff2b0ca0591f8 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 05:45:07 +0700 Subject: [PATCH 78/96] Record an undelivered attestation durably, and release it only when delivery wins The retained attestation lived only in memory, so a process that died between the callback and the handover -- or between a cancellation and the retry -- lost the fact that the key was never delivered. The next launch found it pending, waited out the grace window, promoted it, and asserted against a key the backend has never seen: rejected every time, then a reset, which costs another rate-limited key on top of the one already wasted. Only the fact is persisted, not the object. An attestation is made over one challenge and a later launch asks for a new one, so a stored copy could never be used across a restart; what matters is that the key must not be promoted. Where the retained object cannot help, the key is discarded and replaced on the spot -- one key, once, deterministically. Releasing the copy also moved to after delivery actually wins. AsyncResource.complete() does not refuse an already-cancelled resource -- it sets the value and fires the success callback -- so a cancellation landing between the check and the handover was both overwritten and taken as delivery, throwing away the one thing here that cannot be produced again for a caller that never received it. The resource is re-checked immediately before completing, and the copy is released only when isCancelled() is false afterwards. Co-Authored-By: Claude Opus 5 (1M context) --- .../impl/ios/IOSDeviceIntegrity.java | 83 +++++++++++++++---- 1 file changed, 66 insertions(+), 17 deletions(-) diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index e6b388455f1..21be158b290 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -153,6 +153,20 @@ final class IOSDeviceIntegrity { */ private static final String KEY_ATTEST_ACCEPTED = "cn1.appattest.attestAccepted"; + /** + * The key whose attestation was produced and never handed to anyone. + * + *

The attestation object itself is only worth keeping for this process: it is made + * over one challenge, and a later launch asks for a new one, so the retained copy + * cannot be used across a restart even if it were stored. What must survive is the + * FACT -- that this key's one-time attestation went nowhere, so the backend has never + * seen it. Without that, the next launch finds a pending key, waits out the grace + * window, promotes it, and asserts against a key nobody registered: rejected every + * time, then a reset, which costs another rate-limited key on top of the one already + * wasted.

+ */ + private static final String KEY_ATTEST_UNDELIVERED = "cn1.appattest.attestUndelivered"; + private static final String STATE_NEW = "new"; private static final String STATE_ATTESTED = "attested"; /** @@ -371,6 +385,7 @@ private void resetLocked(boolean keepSpentMarker) { undeliveredAttestKeyId = null; undeliveredAttestNonce = null; undeliveredAttestToken = null; + store.remove(KEY_ATTEST_UNDELIVERED); inMemoryStateKeyId = null; inMemoryState = null; pendingSinceInMemory = 0L; @@ -516,6 +531,11 @@ private void promoteToAttested(SecureStorage store, String keyId, boolean holdIn store.remove(KEY_PENDING_SINCE); store.remove(KEY_ATTEST_STARTED); store.remove(KEY_ATTEST_ACCEPTED); + // Registered, so whatever happened to the delivery no longer matters. + store.remove(KEY_ATTEST_UNDELIVERED); + undeliveredAttestKeyId = null; + undeliveredAttestNonce = null; + undeliveredAttestToken = null; attestAnsweredForKey = null; recoverySpentInMemory = !store.remove(KEY_RECOVERY_SPENT); } @@ -584,29 +604,36 @@ AsyncResource requestToken(String nonce) { && keyId.equals(store.get(KEY_ATTEST_ACCEPTED))) { state = STATE_PENDING; } - if (STATE_PENDING.equals(state) && keyId != null && keyId.length() > 0 - && keyId.equals(undeliveredAttestKeyId)) { + // The durable marker counts as much as the in-memory copy: after a restart it + // is the only thing that knows this key's one-time attestation went nowhere. + boolean neverDelivered = keyId != null && keyId.length() > 0 + && (keyId.equals(undeliveredAttestKeyId) + || keyId.equals(store.get(KEY_ATTEST_UNDELIVERED))); + if (STATE_PENDING.equals(state) && neverDelivered) { // An attestation was produced for this key and nobody took it -- the // caller cancelled while Apple was working. It cannot be produced again, // so it is handed to the retry that asks for the same challenge. - if (nonce.equals(undeliveredAttestNonce)) { + if (undeliveredAttestToken != null + && nonce.equals(undeliveredAttestNonce)) { String token = undeliveredAttestToken; undeliveredAttestKeyId = null; undeliveredAttestNonce = null; undeliveredAttestToken = null; + store.remove(KEY_ATTEST_UNDELIVERED); r.complete(token); return r; } - // A different challenge, so the retained object is worthless: an - // attestation is made over one nonce and the backend checks it. What must - // NOT happen is the grace window promoting this key -- the backend has - // never seen it, so every assertion would be rejected, and the reset that - // follows costs a rate-limited key anyway on top of the failures. Discard - // it here instead: one key, once, deterministically, with the next lines - // generating its replacement. + // A different challenge, or a restart that lost the object entirely. Either + // way the retained copy cannot help: an attestation is made over one nonce + // and the backend checks it. What must NOT happen is the grace window + // promoting this key -- the backend has never seen it, so every assertion + // would be rejected, and the reset that follows costs a rate-limited key + // anyway on top of the failures. Discard it here instead: one key, once, + // deterministically, with the next lines generating its replacement. undeliveredAttestKeyId = null; undeliveredAttestNonce = null; undeliveredAttestToken = null; + store.remove(KEY_ATTEST_UNDELIVERED); resetLocked(); keyId = null; state = null; @@ -1067,6 +1094,12 @@ public static void nativeAttestationReady(final int requestId, final String atte instance.undeliveredAttestKeyId = pending.keyId; instance.undeliveredAttestNonce = pending.nonce; instance.undeliveredAttestToken = attestToken; + // And durably, before anyone can take delivery. Only the fact, not the + // object: an attestation covers one challenge and a later launch asks for + // a new one, so a stored copy could never be used across a restart -- + // whereas knowing the key was never delivered is what stops the next + // launch promoting it into assertions the backend must reject. + SecureStorage.getInstance().set(KEY_ATTEST_UNDELIVERED, pending.keyId); } } succeed(pending, attestToken, true); @@ -1347,15 +1380,31 @@ public void run() { + "reset while this request was in flight")); return; } - if (attestation && token.equals(instance.undeliveredAttestToken)) { - // Somebody is taking delivery, so there is nothing left to - // retain. Released under the lock the retry path reads it under. - instance.undeliveredAttestKeyId = null; - instance.undeliveredAttestNonce = null; - instance.undeliveredAttestToken = null; - } + } + // Re-checked immediately before the handover. A cancellation landing + // between the first check and here would otherwise be overwritten: + // AsyncResource.complete() does not refuse an already-cancelled resource, + // it sets the value and fires the success callback anyway. + if (pending.result.isDone()) { + return; } pending.result.complete(token); + // Released only once delivery has actually won, and never on the strength + // of the check above alone: a cancellation racing that window leaves + // isCancelled() true afterwards, and dropping the retained copy there + // would throw away the one thing here that cannot be produced again -- + // for a caller that never received it. + if (attestation && !pending.result.isCancelled()) { + synchronized (instance.flowLock) { + if (token.equals(instance.undeliveredAttestToken)) { + instance.undeliveredAttestKeyId = null; + instance.undeliveredAttestNonce = null; + instance.undeliveredAttestToken = null; + SecureStorage.getInstance().remove(KEY_ATTEST_UNDELIVERED); + } + } + } + return; } }); } From 353a1246918159d034f532175ec0e8442bf833e5 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 05:58:28 +0700 Subject: [PATCH 79/96] Claim the result once, so cancelling and delivering cannot both win Checking isDone() and then completing left a window that AsyncResource cannot close from outside: complete() does not refuse a cancelled resource and cancel() does not refuse a completed one, so a cancellation landing in between let the application receive and submit an attestation while this class concluded delivery had lost and kept the copy for a retry. What falls into that window is the one object here that cannot be produced again. The resource this class hands out is its own type now, with a single atomic claim that cancel(), deliver() and fail() all take. Only these results are affected, so no other caller sees the stricter cancel(), and the retained attestation is released exactly when delivery is the claim that won. headersFor() also read `initialized` outside the monitor. Its writes happen under it, so there was no happens-before: a caller on another thread could go on seeing a shield that had finished starting as one that had not, and navigate a protected host without a token indefinitely. Snapshotted under the lock -- the contract is that it does not BLOCK, which is a different promise from not synchronizing. Co-Authored-By: Claude Opus 5 (1M context) --- .../codename1/security/shield/AppShield.java | 16 +++- .../impl/ios/IOSDeviceIntegrity.java | 93 +++++++++++++------ quality-report.md | 21 ++++- 3 files changed, 96 insertions(+), 34 deletions(-) diff --git a/CodenameOne/src/com/codename1/security/shield/AppShield.java b/CodenameOne/src/com/codename1/security/shield/AppShield.java index f933ed82cef..b173b349e6c 100644 --- a/CodenameOne/src/com/codename1/security/shield/AppShield.java +++ b/CodenameOne/src/com/codename1/security/shield/AppShield.java @@ -623,8 +623,20 @@ public static Hashtable headersFor(String url) { // token, and that looks exactly like a page that loaded correctly. Apps that // navigate to a protected host at launch should call init() before doing so, or // use fetchToken(), which does wait -- on a background thread. - if (!initialized) { - if (initializing) { + // Read under the monitor, and only read -- no wait. Without it there is no + // happens-before with init()'s writes, so a caller on another thread could go on + // seeing `initialized == false` after startup finished and quietly navigate a + // protected host without a token, indefinitely. The synchronized block costs an + // uncontended lock and keeps the never-blocking contract, which is a different + // promise from the never-synchronizing one nobody made. + boolean ready; + boolean starting; + synchronized (AppShield.class) { + ready = initialized; + starting = initializing; + } + if (!ready) { + if (starting) { Log.p("AppShield: headersFor(" + hostOf(url) + ") was called while " + "initialization is still running, so no token is attached. Call " + "AppShield.init(...) before navigating to a protected host."); diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index 21be158b290..f7ea3fce896 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -553,7 +553,7 @@ String[] jailbreakSignals() { } AsyncResource requestToken(String nonce) { - AsyncResource r = new AsyncResource(); + OneShotResource r = new OneShotResource(); if (!nativeInstance.isAppAttestSupported()) { r.error(new UnsupportedOperationException( "App Attest is not supported on this device")); @@ -777,11 +777,11 @@ private static long registrationGraceRemaining() { // --- flow steps ------------------------------------------------------ - private void attestKey(AsyncResource r, String nonce, String keyId) { + private void attestKey(OneShotResource r, String nonce, String keyId) { attestKey(r, nonce, keyId, false); } - private void attestKey(AsyncResource r, String nonce, String keyId, boolean retried) { + private void attestKey(OneShotResource r, String nonce, String keyId, boolean retried) { // The attestation binds to SHA-256 of the raw nonce. Hashing here rather // than natively keeps a single hash implementation across both paths. String hash = base64(Hash.sha256(bytes(nonce))); @@ -803,7 +803,7 @@ private void attestKey(AsyncResource r, String nonce, String keyId, bool nativeInstance.appAttestAttestKey(rid, keyId, hash); } - private void assertWithKey(AsyncResource r, String nonce, String keyId) { + private void assertWithKey(OneShotResource r, String nonce, String keyId) { String clientData = clientDataJson(nonce, keyId); String hash = base64(Hash.sha256(bytes(clientData))); PendingRequest pending = @@ -1105,6 +1105,54 @@ public static void nativeAttestationReady(final int requestId, final String atte succeed(pending, attestToken, true); } + /** + * The resource handed to a caller, where cancelling and delivering cannot both win. + * + *

{@link AsyncResource#complete} does not refuse an already-cancelled resource -- + * it writes the value and fires the success callback -- and {@code cancel()} does not + * refuse an already-completed one either. Checking one before doing the other narrows + * the window without closing it, and what falls into it is the one object here that + * cannot be produced again: the application may have received and submitted an + * attestation while this class concluded delivery had lost and kept it for a retry, + * or the reverse. A claim taken once decides it.

+ * + *

Only this class's own results are of this type, so nothing outside it can be + * affected by the stricter cancel().

+ */ + static final class OneShotResource extends AsyncResource { + + private final java.util.concurrent.atomic.AtomicBoolean claimed = + new java.util.concurrent.atomic.AtomicBoolean(); + + @Override + public boolean cancel(boolean mayInterruptIfRunning) { + if (!claimed.compareAndSet(false, true)) { + // Delivery got here first. Reporting the cancellation as refused is the + // truth -- the caller's success callback has run or is about to. + return false; + } + return super.cancel(mayInterruptIfRunning); + } + + /** True when this call is the one that delivered. */ + boolean deliver(String value) { + if (!claimed.compareAndSet(false, true)) { + return false; + } + super.complete(value); + return true; + } + + /** True when this call is the one that failed it. */ + boolean fail(Throwable t) { + if (!claimed.compareAndSet(false, true)) { + return false; + } + super.error(t); + return true; + } + } + /** Called from native with an assertion over an already attested key. */ public static void nativeAssertionReady(final int requestId, final String assertionB64) { if (dceGuard) { @@ -1361,7 +1409,7 @@ public void run() { return; } if (instance == null) { - pending.result.complete(token); + pending.result.deliver(token); return; } // The staleness check happens under the lock; the completion itself does @@ -1376,25 +1424,20 @@ public void run() { // no such recovery, so the trade runs this way round. synchronized (instance.flowLock) { if (isStale(pending)) { - pending.result.error(new RuntimeException("App Attest state was " + pending.result.fail(new RuntimeException("App Attest state was " + "reset while this request was in flight")); return; } } - // Re-checked immediately before the handover. A cancellation landing - // between the first check and here would otherwise be overwritten: - // AsyncResource.complete() does not refuse an already-cancelled resource, - // it sets the value and fires the success callback anyway. - if (pending.result.isDone()) { + // One claim decides it. A check followed by a completion left a window + // where a cancellation could win the check and the completion could + // happen anyway -- AsyncResource.complete() does not refuse a cancelled + // resource -- so the application received the attestation while this + // class concluded delivery had lost and kept it for a retry. + if (!pending.result.deliver(token)) { return; } - pending.result.complete(token); - // Released only once delivery has actually won, and never on the strength - // of the check above alone: a cancellation racing that window leaves - // isCancelled() true afterwards, and dropping the retained copy there - // would throw away the one thing here that cannot be produced again -- - // for a caller that never received it. - if (attestation && !pending.result.isCancelled()) { + if (attestation) { synchronized (instance.flowLock) { if (token.equals(instance.undeliveredAttestToken)) { instance.undeliveredAttestKeyId = null; @@ -1417,12 +1460,10 @@ public void run() { * That reference outlives the call for as long as the EDT queue holds the Runnable, * and it is a reference nothing here needs.

*/ - private static void failResource(final AsyncResource r, final String msg) { + private static void failResource(final OneShotResource r, final String msg) { Display.getInstance().callSerially(new Runnable() { public void run() { - if (!r.isDone()) { - r.error(new RuntimeException(msg)); - } + r.fail(new RuntimeException(msg)); } }); } @@ -1430,9 +1471,7 @@ public void run() { private static void fail(final PendingRequest pending, final String msg) { Display.getInstance().callSerially(new Runnable() { public void run() { - if (!pending.result.isDone()) { - pending.result.error(new RuntimeException(msg)); - } + pending.result.fail(new RuntimeException(msg)); } }); } @@ -1463,7 +1502,7 @@ private static final class PendingRequest { static final int OP_ATTEST = 1; static final int OP_ASSERT = 2; - final AsyncResource result; + final OneShotResource result; final String nonce; final int op; final String keyId; @@ -1473,7 +1512,7 @@ private static final class PendingRequest { /// carrying an older one belongs to an abandoned flow. int generation; - PendingRequest(AsyncResource result, String nonce, int op, String keyId) { + PendingRequest(OneShotResource result, String nonce, int op, String keyId) { this.result = result; this.nonce = nonce; this.op = op; diff --git a/quality-report.md b/quality-report.md index 0ffc02c7287..f7ae0ca0bb0 100644 --- a/quality-report.md +++ b/quality-report.md @@ -1,13 +1,24 @@ ## ✅ Continuous Quality Report ### Test & Coverage -- ⚠️ No test results were found. -- ⚠️ Coverage report not generated. +- ✅ **Tests:** 4736 total, 0 failed, 0 skipped +- 📊 **Line coverage:** 58.51% + - **Lowest covered classes** + - `com.codename1.mcp.MCPServer` – 0.00% + - `com.codename1.security.shield.AppShield` – 0.00% + - `com.codename1.gaming.level.GameSceneView` – 0.00% + - `com.codename1.crash.CrashProtection` – 0.00% + - `com.codename1.payment.CommerceManager` – 0.00% + - `com.codename1.crash.PiiScrubber` – 0.00% + - `com.codename1.crash.CrashReportPayload` – 0.00% + - `com.codename1.security.shield.ShieldConfig` – 0.00% + - `com.codename1.vr.VRView` – 0.00% + - `com.codename1.appreview.RatingDialog` – 0.00% ### Static Analysis - **SpotBugs** - - ✅ **ios:** 0 findings (no issues) -- ⚠️ PMD report not generated. -- ⚠️ Checkstyle report not generated. + - ✅ **core-unittests:** 0 findings (no issues) +- ✅ **PMD:** 0 findings (no issues) +- ✅ **Checkstyle:** 0 findings (no issues) _Generated automatically by the PR CI workflow._ From 26df1bd79012acffe005d74763aaf86ab6d48319 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 06:14:02 +0700 Subject: [PATCH 80/96] Make "attested but undelivered" a state, so it cannot half-persist The undelivered marker was a second keychain item written after the state. A keychain that took the state and refused the marker left a key reading as ordinary pending, so the next launch waited out the grace window and promoted a key the backend has never seen -- the failure the marker exists to prevent, reached through the gap between two writes. It is a VALUE of the state item now: pendingUndelivered, promoted to pending the moment a caller takes delivery. One write cannot half-succeed. And when that promotion is refused, the process remembers it. Otherwise the persisted state still says nobody received the attestation, and the next request discards a key the backend may already have registered -- on the strength of a fact this process knows to be out of date. Same shape as the other in-memory overrides here: the keychain copy survives a restart, this one survives the keychain saying no. confirmAttestation accepts either spelling, because a backend confirming the key is proof somebody took delivery whatever this device managed to record about it. Co-Authored-By: Claude Opus 5 (1M context) --- .../impl/ios/IOSDeviceIntegrity.java | 88 ++++++++++++++----- quality-report.md | 21 ++--- 2 files changed, 71 insertions(+), 38 deletions(-) diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index f7ea3fce896..99768328332 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -165,7 +165,19 @@ final class IOSDeviceIntegrity { * time, then a reset, which costs another rate-limited key on top of the one already * wasted.

*/ - private static final String KEY_ATTEST_UNDELIVERED = "cn1.appattest.attestUndelivered"; + /** + * The state of a key whose attestation has been produced but not handed to anyone. + * + *

A VALUE of the state item rather than an item of its own, which is the whole + * point: the two facts -- Apple attested this key, and nobody has received the + * object -- move together or not at all. Written separately, a keychain that took the + * state and refused the marker left a key that reads as ordinary pending, so the next + * launch waited out the grace window and promoted a key the backend has never seen. + * One write cannot half-succeed.

+ * + *

Promoted to {@link #STATE_PENDING} the moment a caller takes delivery.

+ */ + private static final String STATE_PENDING_UNDELIVERED = "pendingUndelivered"; private static final String STATE_NEW = "new"; private static final String STATE_ATTESTED = "attested"; @@ -277,6 +289,18 @@ final class IOSDeviceIntegrity { * different nonce would produce a token the backend rejects, which is the failure * this exists to avoid rather than a way out of it.

*/ + /** + * The key this process knows was delivered, when the keychain refused to record it. + * + *

Delivery promotes {@link #STATE_PENDING_UNDELIVERED} to {@link #STATE_PENDING}. + * If that write is refused, the persisted state still says nobody received the + * attestation -- and the next request would discard a key the backend may already + * have registered, on the strength of a fact this process knows to be out of date. + * Same shape as every other in-memory override here: the keychain copy survives a + * restart, this one survives the keychain saying no.

+ */ + private String deliveredAttestKeyId; + private String undeliveredAttestKeyId; private String undeliveredAttestNonce; private String undeliveredAttestToken; @@ -385,7 +409,7 @@ private void resetLocked(boolean keepSpentMarker) { undeliveredAttestKeyId = null; undeliveredAttestNonce = null; undeliveredAttestToken = null; - store.remove(KEY_ATTEST_UNDELIVERED); + deliveredAttestKeyId = null; inMemoryStateKeyId = null; inMemoryState = null; pendingSinceInMemory = 0L; @@ -475,7 +499,8 @@ void confirmAttestation(String keyId) { // persisted one: both are only ever set for the key this process is actually // holding, and a reset clears both together. boolean pendingInMemory = keyId.equals(inMemoryStateKeyId) - && STATE_PENDING.equals(inMemoryState); + && (STATE_PENDING.equals(inMemoryState) + || STATE_PENDING_UNDELIVERED.equals(inMemoryState)); if (!keyId.equals(store.get(KEY_ID)) && !pendingInMemory) { return; } @@ -490,7 +515,11 @@ void confirmAttestation(String keyId) { // marker is still there, because the path that clears it never ran) and // discarded for another rate-limited key. Honouring the in-memory state costs // nothing when the keychain is healthy and is the whole point of holding it. - if (!pendingInMemory && !STATE_PENDING.equals(store.get(KEY_STATE))) { + // Either spelling. A backend confirming the key is proof somebody took + // delivery, whatever this device managed to record about it. + String persisted = store.get(KEY_STATE); + if (!pendingInMemory && !STATE_PENDING.equals(persisted) + && !STATE_PENDING_UNDELIVERED.equals(persisted)) { return; } promoteToAttested(store, keyId, pendingInMemory); @@ -532,7 +561,7 @@ private void promoteToAttested(SecureStorage store, String keyId, boolean holdIn store.remove(KEY_ATTEST_STARTED); store.remove(KEY_ATTEST_ACCEPTED); // Registered, so whatever happened to the delivery no longer matters. - store.remove(KEY_ATTEST_UNDELIVERED); + deliveredAttestKeyId = null; undeliveredAttestKeyId = null; undeliveredAttestNonce = null; undeliveredAttestToken = null; @@ -604,12 +633,15 @@ AsyncResource requestToken(String nonce) { && keyId.equals(store.get(KEY_ATTEST_ACCEPTED))) { state = STATE_PENDING; } - // The durable marker counts as much as the in-memory copy: after a restart it - // is the only thing that knows this key's one-time attestation went nowhere. - boolean neverDelivered = keyId != null && keyId.length() > 0 - && (keyId.equals(undeliveredAttestKeyId) - || keyId.equals(store.get(KEY_ATTEST_UNDELIVERED))); - if (STATE_PENDING.equals(state) && neverDelivered) { + // Delivery that the keychain refused to record still happened. Without this + // the state says nobody received the attestation and the branch below + // discards a key the backend may already have registered. + if (STATE_PENDING_UNDELIVERED.equals(state) && keyId != null + && keyId.equals(deliveredAttestKeyId)) { + state = STATE_PENDING; + } + if (STATE_PENDING_UNDELIVERED.equals(state) && keyId != null + && keyId.length() > 0) { // An attestation was produced for this key and nobody took it -- the // caller cancelled while Apple was working. It cannot be produced again, // so it is handed to the retry that asks for the same challenge. @@ -619,7 +651,7 @@ AsyncResource requestToken(String nonce) { undeliveredAttestKeyId = null; undeliveredAttestNonce = null; undeliveredAttestToken = null; - store.remove(KEY_ATTEST_UNDELIVERED); + store.set(KEY_STATE, STATE_PENDING); r.complete(token); return r; } @@ -633,7 +665,6 @@ AsyncResource requestToken(String nonce) { undeliveredAttestKeyId = null; undeliveredAttestNonce = null; undeliveredAttestToken = null; - store.remove(KEY_ATTEST_UNDELIVERED); resetLocked(); keyId = null; state = null; @@ -985,7 +1016,7 @@ public static void nativeAttestationReady(final int requestId, final String atte return; } SecureStorage store = SecureStorage.getInstance(); - if (!store.set(KEY_STATE, STATE_PENDING)) { + if (!store.set(KEY_STATE, STATE_PENDING_UNDELIVERED)) { // The key is attested with Apple and the keychain will not record it. // // Reporting success would leave the next request attesting the same key @@ -997,7 +1028,7 @@ public static void nativeAttestationReady(final int requestId, final String atte // exactly as the deadline below is, and this request is failed so the // caller retries into a state that now describes the key correctly. instance.inMemoryStateKeyId = pending.keyId; - instance.inMemoryState = STATE_PENDING; + instance.inMemoryState = STATE_PENDING_UNDELIVERED; instance.pendingSinceInMemory = System.currentTimeMillis(); // And durably, in a DIFFERENT item, because process memory does not // survive the restart this is most likely to be followed by: storage @@ -1094,12 +1125,12 @@ public static void nativeAttestationReady(final int requestId, final String atte instance.undeliveredAttestKeyId = pending.keyId; instance.undeliveredAttestNonce = pending.nonce; instance.undeliveredAttestToken = attestToken; - // And durably, before anyone can take delivery. Only the fact, not the - // object: an attestation covers one challenge and a later launch asks for - // a new one, so a stored copy could never be used across a restart -- - // whereas knowing the key was never delivered is what stops the next - // launch promoting it into assertions the backend must reject. - SecureStorage.getInstance().set(KEY_ATTEST_UNDELIVERED, pending.keyId); + // The durable half is the STATE the callback above wrote: + // STATE_PENDING_UNDELIVERED. Only the fact is persisted, never the + // object -- an attestation covers one challenge and a later launch asks + // for a new one, so a stored copy could not be used across a restart + // anyway, whereas knowing nobody received it is what stops the next + // launch promoting a key the backend has never seen. } } succeed(pending, attestToken, true); @@ -1440,10 +1471,23 @@ public void run() { if (attestation) { synchronized (instance.flowLock) { if (token.equals(instance.undeliveredAttestToken)) { + String keyId = instance.undeliveredAttestKeyId; instance.undeliveredAttestKeyId = null; instance.undeliveredAttestNonce = null; instance.undeliveredAttestToken = null; - SecureStorage.getInstance().remove(KEY_ATTEST_UNDELIVERED); + // Somebody has it, so the key is pending registration rather + // than pending delivery. A refused write leaves the persisted + // state saying nobody received it, which would have the next + // request discard a key the backend may already know -- so + // this process remembers what it did. + SecureStorage store = SecureStorage.getInstance(); + if (STATE_PENDING_UNDELIVERED.equals(store.get(KEY_STATE)) + && !store.set(KEY_STATE, STATE_PENDING)) { + instance.deliveredAttestKeyId = keyId; + } + if (STATE_PENDING_UNDELIVERED.equals(instance.inMemoryState)) { + instance.inMemoryState = STATE_PENDING; + } } } } diff --git a/quality-report.md b/quality-report.md index f7ae0ca0bb0..0ffc02c7287 100644 --- a/quality-report.md +++ b/quality-report.md @@ -1,24 +1,13 @@ ## ✅ Continuous Quality Report ### Test & Coverage -- ✅ **Tests:** 4736 total, 0 failed, 0 skipped -- 📊 **Line coverage:** 58.51% - - **Lowest covered classes** - - `com.codename1.mcp.MCPServer` – 0.00% - - `com.codename1.security.shield.AppShield` – 0.00% - - `com.codename1.gaming.level.GameSceneView` – 0.00% - - `com.codename1.crash.CrashProtection` – 0.00% - - `com.codename1.payment.CommerceManager` – 0.00% - - `com.codename1.crash.PiiScrubber` – 0.00% - - `com.codename1.crash.CrashReportPayload` – 0.00% - - `com.codename1.security.shield.ShieldConfig` – 0.00% - - `com.codename1.vr.VRView` – 0.00% - - `com.codename1.appreview.RatingDialog` – 0.00% +- ⚠️ No test results were found. +- ⚠️ Coverage report not generated. ### Static Analysis - **SpotBugs** - - ✅ **core-unittests:** 0 findings (no issues) -- ✅ **PMD:** 0 findings (no issues) -- ✅ **Checkstyle:** 0 findings (no issues) + - ✅ **ios:** 0 findings (no issues) +- ⚠️ PMD report not generated. +- ⚠️ Checkstyle report not generated. _Generated automatically by the PR CI workflow._ From a56e25932c008374f34975b4f8d79be4db7c43bc Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 06:24:48 +0700 Subject: [PATCH 81/96] Record the delivery on the retry path too, not only the scheduled one Two paths hand the retained attestation over: the scheduled delivery and the retry that arrives with the same challenge. Only the first recorded what it did when the keychain refused the promotion, so a refused write on the retry left the persisted state saying nobody had received the attestation -- and the next request discarded a key the caller was already registering, spending another rate-limited one. Same fix as the other path, which is the point: a handover is a handover, and both places that perform one have to leave the same trace. Co-Authored-By: Claude Opus 5 (1M context) --- .../com/codename1/impl/ios/IOSDeviceIntegrity.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index 99768328332..d5e93342ace 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -651,7 +651,18 @@ AsyncResource requestToken(String nonce) { undeliveredAttestKeyId = null; undeliveredAttestNonce = null; undeliveredAttestToken = null; - store.set(KEY_STATE, STATE_PENDING); + // The same promotion the scheduled delivery does, including what to + // do when the keychain refuses it: this caller is walking away with + // the attestation, so a persisted state still saying nobody received + // it would have the NEXT request discard a key the backend may + // already have registered. Two paths hand this object over, and both + // have to record that they did. + if (!store.set(KEY_STATE, STATE_PENDING)) { + deliveredAttestKeyId = keyId; + } + if (STATE_PENDING_UNDELIVERED.equals(inMemoryState)) { + inMemoryState = STATE_PENDING; + } r.complete(token); return r; } From fcc86d4d205bd0bc2c82d4706cfe0e7b5f536740 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 06:37:16 +0700 Subject: [PATCH 82/96] Rehydrate the acceptance fallback as undelivered, which is what it records KEY_ATTEST_ACCEPTED is written in exactly one place: the branch where the state write was refused, which is before anyone could have taken delivery. Reading it back as ordinary pending asserted the opposite -- and since the pending deadline lived only in memory, the grace window was already over by the next launch, so the key was promoted immediately and started asserting against something the backend has never seen. The residual I described as three failures deep was in fact the worst outcome of the three, not the mildest. It now rehydrates as pendingUndelivered, so the same-challenge retry can still take delivery and anything else discards and replaces the key deterministically. A persisted STATE_PENDING wins over it, because that value is only ever written once a caller has the attestation. Co-Authored-By: Claude Opus 5 (1M context) --- .../com/codename1/impl/ios/IOSDeviceIntegrity.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index d5e93342ace..f0ab12331c3 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -629,9 +629,19 @@ AsyncResource requestToken(String nonce) { // next launch finds STATE_NEW plus a start marker, reads an accepted key as // an interrupted attempt, and discards it for another rate-limited one -- // every launch, for as long as the keychain stays unwilling. + // + // It rehydrates as UNDELIVERED, not as pending. This marker is written in + // exactly one place: the branch where the state write was refused, which is + // before anyone could have taken delivery. Reading it back as ordinary + // pending asserted the opposite -- and with the pending deadline also lost + // with the process, the grace window was already over, so the next request + // promoted the key immediately and started asserting against something the + // backend has never seen. A persisted STATE_PENDING wins over it, because + // that value is only ever written once a caller has the attestation. if (keyId != null && !STATE_ATTESTED.equals(state) + && !STATE_PENDING.equals(state) && keyId.equals(store.get(KEY_ATTEST_ACCEPTED))) { - state = STATE_PENDING; + state = STATE_PENDING_UNDELIVERED; } // Delivery that the keychain refused to record still happened. Without this // the state says nobody received the attestation and the branch below From bebaf5b42a5c845a6f5fdc5bda1abb6346906334 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 06:50:29 +0700 Subject: [PATCH 83/96] Retain the attestation in the same critical section that publishes the state The state saying "attested, nobody has it" was published in one critical section and the object that satisfies it retained in another. A request arriving in the gap read that state, found no object to hand over, and answered the only way the state allows -- by discarding a key Apple had just accepted, which costs a rate-limited one and fails the request that produced it. The two are one fact, so anything that can observe either now observes both. Co-Authored-By: Claude Opus 5 (1M context) --- .../impl/ios/IOSDeviceIntegrity.java | 38 +++++++++---------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index f0ab12331c3..34702e18cf2 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -1030,6 +1030,8 @@ public static void nativeAttestationReady(final int requestId, final String atte // Labelled so the storage-failure branches can leave the critical section and // still hand the caller its attestation, which is the one thing in here that // cannot be produced a second time. + String attestToken = TOKEN_PREFIX + ":attest:" + base64(bytes(pending.keyId)) + + ":" + attestationB64; shieldAttestState: synchronized (instance.flowLock) { if (isStale(pending)) { @@ -1037,6 +1039,15 @@ public static void nativeAttestationReady(final int requestId, final String atte return; } SecureStorage store = SecureStorage.getInstance(); + // Retained in the SAME critical section that publishes the undelivered state, + // and before it. Split across two, a request arriving in the gap read + // "attested, nobody has it" with no object to hand over -- and answered the + // only way that state allows, by discarding a key Apple had just accepted. + // The two facts are one fact, so anything that can observe either has to + // observe both. + instance.undeliveredAttestKeyId = pending.keyId; + instance.undeliveredAttestNonce = pending.nonce; + instance.undeliveredAttestToken = attestToken; if (!store.set(KEY_STATE, STATE_PENDING_UNDELIVERED)) { // The key is attested with Apple and the keychain will not record it. // @@ -1133,27 +1144,12 @@ public static void nativeAttestationReady(final int requestId, final String atte instance.failBootstrapWaiters("App Attest is completing first-run " + "registration for this device; retry shortly"); } - String attestToken = TOKEN_PREFIX + ":attest:" + base64(bytes(pending.keyId)) - + ":" + attestationB64; - if (instance != null) { - // Retained until somebody takes delivery. The caller may have cancelled while - // Apple was working, and succeed() correctly declines to complete a resource - // that is already done -- but the attestation object is the one thing here - // that cannot be produced again, so dropping it left a key the backend could - // never register, which the grace window then promoted into assertions that - // are rejected and a reset that costs another rate-limited key. - synchronized (instance.flowLock) { - instance.undeliveredAttestKeyId = pending.keyId; - instance.undeliveredAttestNonce = pending.nonce; - instance.undeliveredAttestToken = attestToken; - // The durable half is the STATE the callback above wrote: - // STATE_PENDING_UNDELIVERED. Only the fact is persisted, never the - // object -- an attestation covers one challenge and a later launch asks - // for a new one, so a stored copy could not be used across a restart - // anyway, whereas knowing nobody received it is what stops the next - // launch promoting a key the backend has never seen. - } - } + // The retention above is what a concurrent request needs; the durable half is the + // state the block wrote. Only the fact is persisted, never the object -- an + // attestation covers one challenge and a later launch asks for a new one, so a + // stored copy could not be used across a restart anyway, whereas knowing nobody + // received it is what stops the next launch promoting a key the backend has never + // seen. succeed(pending, attestToken, true); } From cdd6627fdc8f38cac9d73167e6c0ffc255cd1158 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 07:02:42 +0700 Subject: [PATCH 84/96] Record the delivery even when the undelivered state never persisted The promotion was conditional on finding pendingUndelivered in storage, so the case where THAT write had also failed -- storage still on "new", the acceptance recorded in its own item -- skipped both the promotion and the in-memory note. A restart then rehydrated the acceptance as undelivered with no retained object left, and discarded a key the backend may already have registered. Attempted now for any state short of already-attested, with the in-memory note kept when it fails. Delivery is delivery; what storage managed to record about the step before it does not change that. Co-Authored-By: Claude Opus 5 (1M context) --- .../src/com/codename1/impl/ios/IOSDeviceIntegrity.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index 34702e18cf2..4997de46edb 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -1497,8 +1497,16 @@ public void run() { // state saying nobody received it, which would have the next // request discard a key the backend may already know -- so // this process remembers what it did. + // Attempted whatever the stored state currently says, short + // of already-attested. Conditioning it on finding + // pendingUndelivered there meant the case where that write + // had ALSO failed -- storage still on "new", the acceptance + // recorded in its own item -- skipped the promotion and the + // in-memory note both. A restart then rehydrated the + // acceptance as undelivered with no retained object left, and + // discarded a key the backend may have registered. SecureStorage store = SecureStorage.getInstance(); - if (STATE_PENDING_UNDELIVERED.equals(store.get(KEY_STATE)) + if (!STATE_ATTESTED.equals(store.get(KEY_STATE)) && !store.set(KEY_STATE, STATE_PENDING)) { instance.deliveredAttestKeyId = keyId; } From 737b894587a19f04534ab48c85f4149c9af23d40 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 07:06:17 +0700 Subject: [PATCH 85/96] Run the Android app build with --stacktrace The Health Connect job failed once in :app:packageDebug, inside PackageAndroidArtifact$IncrementalSplitterRunnable, and the log names the task and nothing else: without --stacktrace Gradle prints "a failure occurred while executing" and swallows the cause. The workspace is gone by the time anyone reads it, so there is nothing left to investigate. This does not fix that failure and does not pretend to. It makes the next occurrence diagnosable, which is the part I can do deterministically from here -- the same commit passed this job before and after, and the only change in it was an iOS-port Java file that the Android packaging step never sees. Co-Authored-By: Claude Opus 5 (1M context) --- scripts/build-android-app.sh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/scripts/build-android-app.sh b/scripts/build-android-app.sh index 3dda2eb77d6..075f8e69f17 100755 --- a/scripts/build-android-app.sh +++ b/scripts/build-android-app.sh @@ -192,7 +192,13 @@ export JAVA_HOME="${JDK_HOME:-$JAVA17_HOME}" yes | "$ANDROID_SDK_ROOT/cmdline-tools/latest/bin/sdkmanager" "platforms;android-36" "build-tools;36.0.0" >/dev/null 2>&1 || ba_log "Warning: unable to install Android SDK 36 components" yes | "$ANDROID_SDK_ROOT/cmdline-tools/latest/bin/sdkmanager" --licenses >/dev/null 2>&1 || true fi - ./gradlew --no-daemon assembleDebug + # --stacktrace, always. A packaging failure inside + # PackageAndroidArtifact$IncrementalSplitterRunnable reports only "a failure + # occurred while executing" without it, so the CI log names the task and nothing + # else -- and the workspace is gone by the time anyone reads it. It costs nothing + # on a successful build and is the difference between diagnosing the next + # occurrence and guessing at it. + ./gradlew --no-daemon --stacktrace assembleDebug ) export JAVA_HOME="$ORIGINAL_JAVA_HOME" From 8e17d636b0271c396d47f69bca8234375e926e0a Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 07:11:33 +0700 Subject: [PATCH 86/96] Retry and verify the Windows ffmpeg install, which failed silently The chocolatey community feed answered 504 for the ffmpeg package, chocolatey carried on, and the step reported success -- so the first sign of trouble was the smoke test dying three steps later with "FileNotFoundError: [WinError 2] The system cannot find the file specified", which names neither ffmpeg nor the feed. The 504 is not ours to fix. Turning it into an unreadable failure in somebody else's test is: an install step that does not confirm the tool it installed is where a transient outage became a mystery. It retries with backoff on the shape that is actually transient, then checks ffmpeg is on PATH and fails with a message naming the likely cause, so the next occurrence reads as what it is. Co-Authored-By: Claude Opus 5 (1M context) --- .github/workflows/javase-cef-ffmpeg-smoke.yml | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/.github/workflows/javase-cef-ffmpeg-smoke.yml b/.github/workflows/javase-cef-ffmpeg-smoke.yml index ce43520cdcb..0a44edee2bb 100644 --- a/.github/workflows/javase-cef-ffmpeg-smoke.yml +++ b/.github/workflows/javase-cef-ffmpeg-smoke.yml @@ -70,7 +70,27 @@ jobs: - name: Install ffmpeg on Windows if: runner.os == 'Windows' shell: powershell - run: choco install ffmpeg -y + run: | + # Retried, and then VERIFIED. The community feed answers 504 often enough to + # matter, and chocolatey can report success after failing to fetch -- so the + # first sign of trouble was the smoke test dying three steps later with + # "FileNotFoundError: [WinError 2] The system cannot find the file specified", + # which names neither ffmpeg nor the feed. An install step that does not + # confirm the tool it installed is how a transient upstream outage becomes an + # unreadable failure in somebody else's test. + $ok = $false + foreach ($delay in 0, 30, 90) { + if ($delay -gt 0) { + Write-Host "choco install ffmpeg failed; retrying in $delay s..." + Start-Sleep -Seconds $delay + } + choco install ffmpeg -y --no-progress + if (Get-Command ffmpeg -ErrorAction SilentlyContinue) { $ok = $true; break } + } + if (-not $ok) { + throw "ffmpeg is not on PATH after installing it. The chocolatey feed is the usual reason -- look for a 504 above." + } + ffmpeg -version - name: Run JavaSE CEF/FFmpeg smoke test env: From 3d4382ea0f3198e1f68fba6c1e79a2dae82b3fb5 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 07:31:52 +0700 Subject: [PATCH 87/96] Claim the retry handoff, and refuse two more headers a port rewrites The same-nonce retry handed the retained attestation over with the inherited complete(), leaving the one-shot claim unspent -- so a caller that cancelled the resource it had just been given still won, while the branch had already cleared the only retained copy and promoted the key. Nobody registers it, and the grace window then asserts against a key the backend has never seen. It goes through deliver() now and touches nothing unless the claim is won. User-Agent joins the reserved list: JavaSEPort rewrites it to a fixed BlackBerry string for any URL containing facebook.com, so a token there works everywhere until the one request that does not. And the cookie header is refused by NAME rather than by constant. ConnectionRequest.setCookieHeader renames it app-wide, so an app that renames it and then picks the same name for the token loses the token to its own cookie string -- written after the request's headers, exactly as the default name is. Checked at configuration time and again in attach(), because the rename can happen after the token header was chosen. Co-Authored-By: Claude Opus 5 (1M context) --- .../codename1/security/shield/AppShield.java | 15 +++++++++ .../security/shield/ShieldConfig.java | 20 ++++++++++++ .../impl/ios/IOSDeviceIntegrity.java | 11 +++++-- .../security/shield/ShieldApiTest.java | 32 +++++++++++++++++++ quality-report.md | 21 +++++++++--- 5 files changed, 92 insertions(+), 7 deletions(-) diff --git a/CodenameOne/src/com/codename1/security/shield/AppShield.java b/CodenameOne/src/com/codename1/security/shield/AppShield.java index b173b349e6c..086cad19e28 100644 --- a/CodenameOne/src/com/codename1/security/shield/AppShield.java +++ b/CodenameOne/src/com/codename1/security/shield/AppShield.java @@ -472,6 +472,21 @@ public static void attach(ConnectionRequest request) throws ShieldException { setStatus(token == null ? ShieldStatus.SERVICE_DOWN : token.getStatus()); if (token != null && token.isValid()) { String header = getConfig().getTokenHeader(); + // Re-checked here, not only at configuration time: the cookie header name + // is a runtime setting, so an app can rename it AFTER choosing a token + // header and land on the same name. The cookie string is written after + // the request's own headers, so the token would be overwritten and this + // method would report success anyway. + if (ShieldHosts.normalize(header).equals( + ShieldHosts.normalize(ConnectionRequest.getCookieHeader()))) { + Log.p("AppShield: the token header " + header + " is now this app's " + + "cookie header, so the token would be overwritten before " + + "the request goes out. Change one of the two."); + failOrContinue(policy, new ShieldException(ShieldStatus.REJECTED, + "AppShield: the token header collides with the cookie " + + "header, so no token can be attached for " + host)); + return; + } rememberAttachedHeader(request, header); request.addRequestHeader(header, token.getValue()); return; diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java b/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java index 4373e0ac099..bf1f367f284 100644 --- a/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java +++ b/CodenameOne/src/com/codename1/security/shield/ShieldConfig.java @@ -22,6 +22,7 @@ */ package com.codename1.security.shield; +import com.codename1.io.ConnectionRequest; import java.util.Enumeration; import java.util.Hashtable; @@ -86,6 +87,20 @@ public ShieldConfig tokenHeader(String name) { + "and the token would follow the redirect. Use a header of your " + "own, or leave the default " + DEFAULT_TOKEN_HEADER + "."); } + // The cookie header NAME is configurable at runtime + // (ConnectionRequest.setCookieHeader), so a hard-coded "cookie" in the list + // below is only the default. An app that renames it and then picks the same + // name for the token loses the token to its own cookie string -- written + // after the request's own headers, exactly as the default name is. + if (ShieldHosts.normalize(ConnectionRequest.getCookieHeader()) + .equals(normalized)) { + throw new IllegalArgumentException(name + " cannot carry the " + + "attestation token: it is this app's cookie header, which " + + "ConnectionRequest writes from its own cookie store after the " + + "request's headers are in place -- so the token would be " + + "overwritten after attach() reported success. Use a header of " + + "your own, or leave the default " + DEFAULT_TOKEN_HEADER + "."); + } for (String reserved : TRANSPORT_HEADERS) { if (reserved.equals(normalized)) { throw new IllegalArgumentException(name + " cannot carry the " @@ -145,6 +160,11 @@ public ShieldConfig tokenHeader(String name) { // method, one platform -- so it would pass every test that did not happen to use // that shape. "accept-encoding", "x-http-method-override", + // And User-Agent, which JavaSEPort rewrites to a fixed BlackBerry string for any + // URL containing facebook.com -- an old patch for getting a readable login page. + // Conditional on the HOST, so a token there works everywhere until the one + // request that matters. + "user-agent", // Cookie is not transport framing, but it fails the same way and worse. // ConnectionRequest emits userHeaders first and THEN calls setHeader("Cookie", // ...) with the generated cookie string, so with cookie handling on and any diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index 4997de46edb..d3c8fec275e 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -657,7 +657,15 @@ AsyncResource requestToken(String nonce) { // so it is handed to the retry that asks for the same challenge. if (undeliveredAttestToken != null && nonce.equals(undeliveredAttestNonce)) { - String token = undeliveredAttestToken; + // Through the one-shot claim, like the scheduled delivery. Calling + // the inherited complete() left the claim unspent, so a caller that + // cancelled the resource it had just been handed still won -- while + // this branch had already cleared the only retained copy and promoted + // the key. Nobody registers it, and the grace window then starts + // asserting against a key the backend has never seen. + if (!r.deliver(undeliveredAttestToken)) { + return r; + } undeliveredAttestKeyId = null; undeliveredAttestNonce = null; undeliveredAttestToken = null; @@ -673,7 +681,6 @@ AsyncResource requestToken(String nonce) { if (STATE_PENDING_UNDELIVERED.equals(inMemoryState)) { inMemoryState = STATE_PENDING; } - r.complete(token); return r; } // A different challenge, or a restart that lost the object entirely. Either diff --git a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java index 2b1639c66a5..8b9b1f85225 100644 --- a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java +++ b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java @@ -474,6 +474,9 @@ void transportOwnedHeadersAreRefusedAsTheTokenHeader() { // fallback. Conditional on the method and the platform, so a token in one of // them survives every test that does not use that exact shape. "Accept-Encoding", "X-HTTP-Method-Override", + // JavaSEPort rewrites User-Agent to a fixed BlackBerry string for any URL + // containing facebook.com, which makes the loss conditional on the host. + "User-Agent", // Not framing, but overwritten by the request itself: ConnectionRequest // emits userHeaders and then sets Cookie from its own cookie store. "Cookie" @@ -493,6 +496,35 @@ void transportOwnedHeadersAreRefusedAsTheTokenHeader() { new ShieldConfig().tokenHeader("X-Connection-Id").getTokenHeader()); } + /** + * The cookie header's NAME is a runtime setting, so the refusal cannot be a constant. + * + *

{@code ConnectionRequest.setCookieHeader} renames it app-wide. An app that + * renames it and then picks the same name for the token loses the token to its own + * cookie string, which is written after the request's headers -- the same way the + * default name loses it, and just as silently.

+ */ + @Test + void aRenamedCookieHeaderIsRefusedTooAndTheDefaultIsRestored() { + String original = com.codename1.io.ConnectionRequest.getCookieHeader(); + try { + com.codename1.io.ConnectionRequest.setCookieHeader("X-App-Session"); + assertThrows(IllegalArgumentException.class, + () -> new ShieldConfig().tokenHeader("X-App-Session"), + "the configured cookie header cannot carry the token either"); + assertThrows(IllegalArgumentException.class, + () -> new ShieldConfig().tokenHeader("x-app-session"), + "and the comparison is case-insensitive like the header"); + // The literal Cookie header stays refused whatever the app renamed its own + // to: it is a header with its own meaning, and the static entry covers it. + // The dynamic check is in addition to that list, not instead of it. + assertThrows(IllegalArgumentException.class, + () -> new ShieldConfig().tokenHeader("Cookie")); + } finally { + com.codename1.io.ConnectionRequest.setCookieHeader(original); + } + } + // --- guard composition ---------------------------------------------- @Test diff --git a/quality-report.md b/quality-report.md index 0ffc02c7287..429aa8e8dcc 100644 --- a/quality-report.md +++ b/quality-report.md @@ -1,13 +1,24 @@ ## ✅ Continuous Quality Report ### Test & Coverage -- ⚠️ No test results were found. -- ⚠️ Coverage report not generated. +- ✅ **Tests:** 4737 total, 0 failed, 0 skipped +- 📊 **Line coverage:** 58.51% + - **Lowest covered classes** + - `com.codename1.mcp.MCPServer` – 0.00% + - `com.codename1.security.shield.AppShield` – 0.00% + - `com.codename1.gaming.level.GameSceneView` – 0.00% + - `com.codename1.crash.CrashProtection` – 0.00% + - `com.codename1.payment.CommerceManager` – 0.00% + - `com.codename1.crash.PiiScrubber` – 0.00% + - `com.codename1.crash.CrashReportPayload` – 0.00% + - `com.codename1.security.shield.ShieldConfig` – 0.00% + - `com.codename1.vr.VRView` – 0.00% + - `com.codename1.appreview.RatingDialog` – 0.00% ### Static Analysis - **SpotBugs** - - ✅ **ios:** 0 findings (no issues) -- ⚠️ PMD report not generated. -- ⚠️ Checkstyle report not generated. + - ✅ **core-unittests:** 0 findings (no issues) +- ✅ **PMD:** 0 findings (no issues) +- ✅ **Checkstyle:** 0 findings (no issues) _Generated automatically by the PR CI workflow._ From 645e09c6aedd3f95b1440fa26338d3cc907ecd16 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 07:51:13 +0700 Subject: [PATCH 88/96] Make the header removal complete, and retry the Windows build only on resolution removeRequestHeader scanned the header map, but addRequestHeader routes Content-Type to a dedicated field -- so removing it removed nothing and initConnection went on emitting it. A removal that silently does not remove is worse than an unsupported one, because the caller has been told otherwise. It resets the field and the explicitly-set flag, which is the state a request that never mentioned it is in. attach() also has to clear the token header's other spellings on the request it is about to decorate. Header names are case-insensitive, so adding ours beside an app's existing x-cn1-attest left two fields on the wire for the backend or an intermediary to choose between, with attach() reporting success. Done at the attach site rather than in the general cleanup, which must not touch a request the shield is not decorating. And the Windows cross-compile retry loop matched every failure. An intermittent compiler or packaging regression could pass on a later attempt and turn a blocking gate green, and the retries reused the previous attempt's outputs. It now matches the output for a dependency-resolution error, retries from clean, and reads PIPESTATUS rather than the pipeline's status -- tee succeeds even when Maven does not, and this step sets no pipefail. Co-Authored-By: Claude Opus 5 (1M context) --- .github/workflows/windows-cross-compile.yml | 27 ++++++++++++-- .../com/codename1/io/ConnectionRequest.java | 16 ++++++++- .../codename1/security/shield/AppShield.java | 8 +++++ .../security/shield/ShieldInitOrderTest.java | 36 ++++++++++++++++++- quality-report.md | 6 ++-- 5 files changed, 85 insertions(+), 8 deletions(-) diff --git a/.github/workflows/windows-cross-compile.yml b/.github/workflows/windows-cross-compile.yml index 040c1f2e13c..8c82797c94c 100644 --- a/.github/workflows/windows-cross-compile.yml +++ b/.github/workflows/windows-cross-compile.yml @@ -111,16 +111,37 @@ jobs: # this job while resolving a build extension, before any project code compiled. # The backoff grows because a flat retry lands inside the same window a 429 is # still rate limiting in. A genuine build failure fails identically every time. + # Retried ONLY for that failure shape. A blanket loop lets an intermittent + # compiler, generator or packaging regression pass on a later attempt and turn + # a blocking gate green, which is worse than the flake it hides -- and the + # retries reuse the previous attempt's target directories, so a partial output + # can decide the result. Matching the output keeps a real failure terminal on + # its first occurrence, and the retry starts from clean. + resolution='status: (403|429|50[0-9])|Could not transfer artifact|Unresolveable build extension|Non-resolvable import POM|Could not resolve dependencies' + goal=install for delay in 30 120 300 0; do - if JAVA_HOME="$JDK_8_HOME" mvn -B -pl windows -am -DskipTests \ - '-Dmaven.javadoc.skip=true' '-Plocal-dev-javase' install; then + # PIPESTATUS, not the pipeline's status: tee succeeds even when mvn does not, + # and this step does not set pipefail. Reading the wrong one would make every + # attempt look successful, which is the opposite failure to the one being + # fixed and would hide everything. + JAVA_HOME="$JDK_8_HOME" mvn -B -pl windows -am -DskipTests \ + '-Dmaven.javadoc.skip=true' '-Plocal-dev-javase' $goal 2>&1 \ + | tee /tmp/windows-cross-build.log + status=${PIPESTATUS[0]} + if [ "$status" -eq 0 ]; then break fi + if ! grep -Eq "$resolution" /tmp/windows-cross-build.log; then + echo "core + Windows port build failed for a reason that is not a transient" + echo "dependency-resolution error; not retrying" + exit 1 + fi if [ "$delay" = "0" ]; then echo "core + Windows port build failed after all retries" exit 1 fi - echo "build failed; retrying in ${delay}s in case Maven Central was flaky" + echo "Maven Central looks flaky; retrying in ${delay}s from clean" + goal="clean install" sleep "$delay" done test -f core/target/classes/com/codename1/ui/Form.class diff --git a/CodenameOne/src/com/codename1/io/ConnectionRequest.java b/CodenameOne/src/com/codename1/io/ConnectionRequest.java index 068f7f4caba..6a2bd0e2e4c 100644 --- a/CodenameOne/src/com/codename1/io/ConnectionRequest.java +++ b/CodenameOne/src/com/codename1/io/ConnectionRequest.java @@ -657,7 +657,21 @@ public void addRequestHeader(String key, String value) { /// headers: anything scoped to the original host has to be removable before /// the retry, or it follows the redirect to wherever it points. public void removeRequestHeader(String key) { - if (userHeaders == null || key == null) { + if (key == null) { + return; + } + if ("content-type".equalsIgnoreCase(key)) { + // addRequestHeader routes this one to a dedicated field rather than the + // header map, so scanning the map alone left it set and initConnection went + // on emitting it -- a removal that removed nothing, which is worse than an + // unsupported one because the caller has been told otherwise. Back to the + // default value and the not-explicitly-set flag, which is the state a request + // that never mentioned it is in. + contentType = "application/x-www-form-urlencoded; charset=UTF-8"; + contentTypeSetExplicitly = false; + return; + } + if (userHeaders == null) { return; } userHeaders.remove(key); diff --git a/CodenameOne/src/com/codename1/security/shield/AppShield.java b/CodenameOne/src/com/codename1/security/shield/AppShield.java index 086cad19e28..e5625c10824 100644 --- a/CodenameOne/src/com/codename1/security/shield/AppShield.java +++ b/CodenameOne/src/com/codename1/security/shield/AppShield.java @@ -472,6 +472,14 @@ public static void attach(ConnectionRequest request) throws ShieldException { setStatus(token == null ? ShieldStatus.SERVICE_DOWN : token.getStatus()); if (token != null && token.isValid()) { String header = getConfig().getTokenHeader(); + // Any spelling of it the app may already have set goes first. Header + // names are case-insensitive, so adding ours beside an existing + // "x-cn1-attest" leaves two fields on the wire and lets the backend or an + // intermediary pick the stale one -- while attach() reports success. + // Done HERE rather than in the general cleanup, which must not touch a + // request the shield is not attaching to: this is the one request that is + // about to receive a token under this name. + request.removeRequestHeader(header); // Re-checked here, not only at configuration time: the cookie header name // is a runtime setting, so an app can rename it AFTER choosing a token // header and land on the same name. The cookie string is written after diff --git a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldInitOrderTest.java b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldInitOrderTest.java index d972febab7e..55c984e8bdd 100644 --- a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldInitOrderTest.java +++ b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldInitOrderTest.java @@ -315,6 +315,31 @@ void anotherRequestKeepsAHeaderTheAppSetItself() throws Exception { "the shield must not strip a header it did not attach to this request"); } + /** + * A differently-cased copy of the token header does not survive beside the real one. + * + *

Header names are case-insensitive, so adding ours next to an app's existing + * {@code x-cn1-attest} leaves two fields on the wire and lets the backend or an + * intermediary pick the stale one -- while {@code attach()} reports success and a + * fail-closed host is satisfied.

+ */ + @Test + void aDifferentlyCasedTokenHeaderIsReplacedRatherThanDuplicated() throws Exception { + Thread init = initOnAnotherThread(); + engine.release(); + init.join(GENEROUS_TIMEOUT_MS); + + RecordingRequest request = new RecordingRequest(); + request.setUrl("https://api.example.com/secure"); + request.addRequestHeader("x-cn1-attest", "a-stale-value-the-app-set"); + + AppShield.attach(request); + + assertNull(request.headerValue("x-cn1-attest"), + "the app's spelling must not survive beside the shield's"); + assertEquals("token-from-a-fully-initialized-engine", request.attached()); + } + private Thread initOnAnotherThread() { ShieldEngineRegistry.setEngine(engine); Thread t = new Thread(new Runnable() { @@ -346,7 +371,16 @@ public void addRequestHeader(String key, String value) { @Override public void removeRequestHeader(String key) { super.removeRequestHeader(key); - headers.remove(key); + // Case-insensitively, like the real one. A double that only removes the exact + // spelling reports a header still present that the request no longer holds, + // which is a test failing for a property the code has. + java.util.Iterator it = headers.keySet().iterator(); + while (it.hasNext()) { + String existing = it.next(); + if (existing != null && existing.equalsIgnoreCase(key)) { + it.remove(); + } + } } String attached() { diff --git a/quality-report.md b/quality-report.md index 429aa8e8dcc..f637f3550a6 100644 --- a/quality-report.md +++ b/quality-report.md @@ -1,9 +1,10 @@ ## ✅ Continuous Quality Report ### Test & Coverage -- ✅ **Tests:** 4737 total, 0 failed, 0 skipped -- 📊 **Line coverage:** 58.51% +- ✅ **Tests:** 4738 total, 0 failed, 0 skipped +- 📊 **Line coverage:** 58.14% - **Lowest covered classes** + - `com.codename1.io.ConnectionRequest` – 0.00% - `com.codename1.mcp.MCPServer` – 0.00% - `com.codename1.security.shield.AppShield` – 0.00% - `com.codename1.gaming.level.GameSceneView` – 0.00% @@ -13,7 +14,6 @@ - `com.codename1.crash.CrashReportPayload` – 0.00% - `com.codename1.security.shield.ShieldConfig` – 0.00% - `com.codename1.vr.VRView` – 0.00% - - `com.codename1.appreview.RatingDialog` – 0.00% ### Static Analysis - **SpotBugs** From f9bec7ebe6c5729a22da51f2fa89f91bb40919a1 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 08:39:58 +0700 Subject: [PATCH 89/96] Honour a cancelled token fetch, and make secure-storage writes durable AsyncResource.complete() does not consult the cancelled flag -- it stores the value, marks the resource done and runs the success callback regardless -- so a caller that gave up while the attestation round trip was in flight still had its ready callback invoked when the answer arrived, and the error branches did the same. Cancelling means the caller has stopped listening, which the rest of the framework is built on and tests. fetchToken now hands back a resource where cancellation and delivery claim the same slot and the loser does nothing; both public entry points are overridden, not only the paths this class uses, because the object is handed to application code. AndroidSecureStorage answered true for a removal it had only queued. apply() persists on a background thread, so an app clearing a credential on logout and then being killed -- which on Android is how a process usually ends -- finds it back on the next launch. The removal commits and returns what the commit says, under the same lock as the write and the reset. The three other places in the file with the same shape went with it: the legacy API-22 write and both halves of the prompting tier, where an apply() could also persist a ciphertext whose IV had not landed. Two CI failures, both with a mechanism rather than a re-run. packageDebug was dying intermittently in OutOfMemoryError inside PackageAndroidArtifact -- the --stacktrace added last round is what showed it. This script runs --no-daemon, so merging, dexing and packaging share one 2048m JVM and the packaging step, which reads each entry into a byte[], is simply last in line; it now asks for 4096m, which the runner has. The Android BrowserComponent screenshot emitted a blank white frame because the readiness check looked only for bright pixels: it was written for iOS, where an uncomposited peer is BLACK, and on Android an unpainted WebView is WHITE and satisfies it on the first try. It now requires the fixture's dark background as well as its light text, which describes the fixture rather than one platform's failure colour, and neither blank state can satisfy it. Core suite 4739 green; SpotBugs 0 across core-unittests, android, ios and the maven plugin; PMD gate clean. Co-Authored-By: Claude Opus 5 (1M context) --- .../codename1/security/shield/AppShield.java | 54 ++++++++++++++++++- .../impl/android/AndroidSecureStorage.java | 40 ++++++++++---- .../security/shield/ShieldInitOrderTest.java | 40 ++++++++++++++ quality-report.md | 12 ++--- scripts/build-android-app.sh | 23 ++++++++ .../tests/BrowserComponentScreenshotTest.java | 28 ++++++++-- 6 files changed, 176 insertions(+), 21 deletions(-) diff --git a/CodenameOne/src/com/codename1/security/shield/AppShield.java b/CodenameOne/src/com/codename1/security/shield/AppShield.java index e5625c10824..0d1a38ac355 100644 --- a/CodenameOne/src/com/codename1/security/shield/AppShield.java +++ b/CodenameOne/src/com/codename1/security/shield/AppShield.java @@ -347,7 +347,7 @@ public static AsyncResource fetchToken() { /// /// @param bindingData the data to bind to, typically a digest of the request body public static AsyncResource fetchToken(final String bindingData) { - final AsyncResource result = new AsyncResource(); + final AsyncResource result = new TokenResource(); Display.getInstance().scheduleBackgroundTask(new Runnable() { @Override public void run() { @@ -858,6 +858,58 @@ private static void setStatus(ShieldStatus status) { Display.getInstance().callSerially(new StatusDispatch(copy, status)); } + /// The token handle handed back to callers, where exactly one of cancellation and + /// delivery wins. + /// + /// [AsyncResource#complete(Object)] does not consult the cancelled flag: it stores the + /// value, marks the resource done and runs the success callback regardless. So a + /// caller that cancelled -- the screen was closed, the user backed out -- still had + /// its `ready` callback invoked when the attestation round trip finished a moment + /// later, and the error branches did the same through [AsyncResource#error(Throwable)]. + /// That contradicts the contract the rest of the framework is built on and tests, and + /// this is the one place where a late callback fires against a screen that has gone. + /// + /// The claim is taken by whichever arrives first, and the loser does nothing. Both + /// entry points are overridden rather than only the internal ones, because this object + /// is handed to application code that can call either. + private static final class TokenResource extends AsyncResource { + private boolean claimed; + + private boolean claim() { + synchronized (this) { + if (claimed) { + return false; + } + claimed = true; + return true; + } + } + + @Override + public boolean cancel(boolean mayInterruptIfRunning) { + if (!claim()) { + // Already delivered. The base class answers false for a resource that is + // done, and so does this. + return false; + } + return super.cancel(mayInterruptIfRunning); + } + + @Override + public void complete(ShieldToken value) { + if (claim()) { + super.complete(value); + } + } + + @Override + public void error(Throwable t) { + if (claim()) { + super.error(t); + } + } + } + private static final class StatusDispatch implements Runnable { private final ShieldListener[] targets; private final ShieldStatus status; diff --git a/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java b/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java index 727e06aede5..5186df66ba3 100644 --- a/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java +++ b/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java @@ -148,11 +148,14 @@ public Boolean run(Cipher c) throws Exception { SharedPreferences sp = AndroidNativeUtil.getActivity() .getApplicationContext() .getSharedPreferences(PREFS, Context.MODE_PRIVATE); - sp.edit() + // commit(), so the Boolean this hands back is a statement about the disk. The + // pair of entries is also all-or-nothing that way: apply() could persist a + // ciphertext whose IV had not landed, which decrypts to nothing on the next + // launch and looks to the caller like a value it successfully stored. + return Boolean.valueOf(sp.edit() .putString("v_" + account, Base64.encodeToString(enc, Base64.DEFAULT)) .putString("iv_" + account, Base64.encodeToString(c.getIV(), Base64.DEFAULT)) - .apply(); - return Boolean.TRUE; + .commit()); } } @@ -197,8 +200,12 @@ public AsyncResource remove(String reason, String account) { SharedPreferences sp = AndroidNativeUtil.getActivity() .getApplicationContext() .getSharedPreferences(PREFS, Context.MODE_PRIVATE); - sp.edit().remove("v_" + account).remove("iv_" + account).apply(); - result.complete(Boolean.TRUE); + // And the prompting tier deletes durably too. This is the credential a logout + // clears; reporting it gone while the removal sits in memory means it comes back + // if the process is killed before the write lands, which on Android is how a + // process usually ends. + result.complete(Boolean.valueOf( + sp.edit().remove("v_" + account).remove("iv_" + account).commit())); return result; } @@ -347,8 +354,18 @@ public boolean remove(String account) { if (prefs == null) { return false; } - prefs.edit().remove(account).apply(); - return true; + // commit(), and its answer is this method's answer. apply() persists on a + // background thread, so returning true said the credential was gone while the + // deletion was still in memory: an app that removes a token on logout and is then + // killed -- which is the ordinary way an Android process ends -- finds it back on + // the next launch. A removal that reports success has to have happened, and this + // is the one operation where the caller cannot verify it later by reading. + // + // Under the same lock as the write and the reset, so a removal cannot be + // interleaved with a set that recreates the entry it was clearing. + synchronized (PLAIN_KEY_LOCK) { + return prefs.edit().remove(account).commit(); + } } /** @@ -442,11 +459,14 @@ private boolean legacyPlainSet(String account, String value) { if (prefs == null) { return false; } - prefs.edit() + // Same reason the encrypted tier commits: this returns whether the value was + // stored, and with apply() it returned that before it was true. The legacy + // path is weaker on confidentiality by construction; it does not get to be + // weaker on the one thing the API actually promises. + return prefs.edit() .putString(account, Base64.encodeToString( value.getBytes("UTF-8"), Base64.NO_WRAP)) - .apply(); - return true; + .commit(); } catch (IOException e) { Log.e(e); return false; diff --git a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldInitOrderTest.java b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldInitOrderTest.java index 55c984e8bdd..26aeccf64d6 100644 --- a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldInitOrderTest.java +++ b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldInitOrderTest.java @@ -284,6 +284,46 @@ public void run() { assertEquals("token-from-a-fully-initialized-engine", handle[0].get().getValue()); } + /** + * A cancelled fetch stays cancelled, whatever the engine answers afterwards. + * + *

{@code AsyncResource.complete()} does not consult the cancelled flag -- it stores + * the value, marks the resource done and runs the success callback regardless -- so a + * caller that gave up while the attestation round trip was in flight still had its + * {@code ready} callback invoked when the answer arrived. Cancelling means the caller + * has stopped listening, and this is the one place in the shield where the late + * callback lands on a screen that has gone.

+ */ + @Test + void aCancelledTokenFetchNeverDeliversItsResult() throws Exception { + Thread init = initOnAnotherThread(); + assertTrue(engine.entered.await(GENEROUS_TIMEOUT_MS, TimeUnit.MILLISECONDS), + "the engine should have been asked to initialize"); + + AsyncResource handle = AppShield.fetchToken(); + final CountDownLatch delivered = new CountDownLatch(1); + handle.ready(new com.codename1.util.SuccessCallback() { + public void onSucess(ShieldToken value) { + delivered.countDown(); + } + }); + handle.except(new com.codename1.util.SuccessCallback() { + public void onSucess(Throwable value) { + delivered.countDown(); + } + }); + + assertTrue(handle.cancel(true), "the caller gives up before the engine answers"); + + // And now the engine answers, which is the whole point: the token arrives, and + // nobody is told. + engine.release(); + init.join(GENEROUS_TIMEOUT_MS); + assertFalse(delivered.await(500L, TimeUnit.MILLISECONDS), + "a cancelled fetch must deliver neither a value nor an error"); + assertTrue(handle.isCancelled(), "and it stays cancelled rather than completing"); + } + /** * The cleanup only touches the request the shield actually decorated. * diff --git a/quality-report.md b/quality-report.md index f637f3550a6..4c028dbe61d 100644 --- a/quality-report.md +++ b/quality-report.md @@ -1,19 +1,19 @@ ## ✅ Continuous Quality Report ### Test & Coverage -- ✅ **Tests:** 4738 total, 0 failed, 0 skipped -- 📊 **Line coverage:** 58.14% +- ✅ **Tests:** 4739 total, 0 failed, 0 skipped +- 📊 **Line coverage:** 58.84% - **Lowest covered classes** - - `com.codename1.io.ConnectionRequest` – 0.00% - - `com.codename1.mcp.MCPServer` – 0.00% - - `com.codename1.security.shield.AppShield` – 0.00% - `com.codename1.gaming.level.GameSceneView` – 0.00% - `com.codename1.crash.CrashProtection` – 0.00% - `com.codename1.payment.CommerceManager` – 0.00% - `com.codename1.crash.PiiScrubber` – 0.00% - `com.codename1.crash.CrashReportPayload` – 0.00% - - `com.codename1.security.shield.ShieldConfig` – 0.00% - `com.codename1.vr.VRView` – 0.00% + - `com.codename1.appreview.RatingDialog` – 0.00% + - `com.codename1.calendar.DefaultCalendarHttpTransport` – 0.00% + - `com.codename1.security.Secrets` – 0.00% + - `com.codename1.calendar.OidcCalendarTokenProvider` – 0.00% ### Static Analysis - **SpotBugs** diff --git a/scripts/build-android-app.sh b/scripts/build-android-app.sh index 075f8e69f17..e51fa1fcc87 100755 --- a/scripts/build-android-app.sh +++ b/scripts/build-android-app.sh @@ -150,6 +150,29 @@ grep -q '^android.useAndroidX=' "$GRADLE_PROPS" 2>/dev/null || echo 'android.use grep -q '^android.enableJetifier=' "$GRADLE_PROPS" 2>/dev/null || echo 'android.enableJetifier=true' >> "$GRADLE_PROPS" grep -q '^android.suppressUnsupportedCompileSdk=' "$GRADLE_PROPS" 2>/dev/null || echo 'android.suppressUnsupportedCompileSdk=36' >> "$GRADLE_PROPS" +# More heap than the builder's generated default, for this script only. +# +# The generated project asks for -Xmx2048m, which was written for a build that +# forks its heavy work out. This script runs --no-daemon, so resource merging, +# dexing and packaging all happen in one single-use JVM, and packageDebug reads +# each entry into a byte[] as it writes it -- the last allocation in the +# sequence, on a heap the earlier steps have already filled and fragmented. +# That is why the failure appeared as an intermittent OutOfMemoryError inside +# PackageAndroidArtifact rather than a build that never worked: identical +# inputs, decided by GC timing. Nothing here caps the JVM below the runner's +# memory, so the headroom is free. +# +# Overwritten rather than appended: java.util.Properties would take the last +# occurrence, but a file listing the same key twice with different values is a +# trap for whoever reads it next. +if grep -q '^org.gradle.jvmargs=' "$GRADLE_PROPS" 2>/dev/null; then + sed -i.bak 's/^org.gradle.jvmargs=.*/org.gradle.jvmargs=-Xmx4096m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8/' "$GRADLE_PROPS" + rm -f "$GRADLE_PROPS.bak" +else + echo 'org.gradle.jvmargs=-Xmx4096m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8' >> "$GRADLE_PROPS" +fi +ba_log "Gradle JVM args: $(grep '^org.gradle.jvmargs=' "$GRADLE_PROPS")" + APP_BUILD_GRADLE="$GRADLE_PROJECT_DIR/app/build.gradle" ROOT_BUILD_GRADLE="$GRADLE_PROJECT_DIR/build.gradle" PATCH_GRADLE_SOURCE_PATH="$SCRIPT_DIR/android/lib" diff --git a/scripts/hellocodenameone/common/src/main/java/com/codenameone/examples/hellocodenameone/tests/BrowserComponentScreenshotTest.java b/scripts/hellocodenameone/common/src/main/java/com/codenameone/examples/hellocodenameone/tests/BrowserComponentScreenshotTest.java index 23918b1b5ae..7b41de7db0c 100644 --- a/scripts/hellocodenameone/common/src/main/java/com/codenameone/examples/hellocodenameone/tests/BrowserComponentScreenshotTest.java +++ b/scripts/hellocodenameone/common/src/main/java/com/codenameone/examples/hellocodenameone/tests/BrowserComponentScreenshotTest.java @@ -172,6 +172,17 @@ private boolean containsRenderedBrowserContent(Image screen) { int bandWidth = right - left; int bandHeight = bottom - top; + // Both colours, not either one. + // + // The fixture is white and cyan text on a near-black background, and this + // originally looked only for the bright pixels because the case it was written + // for was iOS, where a peer that has not reached the Metal surface is BLACK -- + // brightness alone separates the two there. On Android an unpainted WebView is + // WHITE, which passes a brightness test on its own the very first time it is + // asked, so the harness emitted a blank white frame and the run failed as a + // screenshot mismatch rather than waiting the further quarter-second the peer + // needed. Requiring the dark background as well describes the fixture instead of + // one platform's failure colour, and neither blank state can satisfy it. if (visualBand == null || visualBand.getWidth() != bandWidth || visualBand.getHeight() != bandHeight) { visualBand = new RGBImage(new int[bandWidth * bandHeight], bandWidth, bandHeight); @@ -179,7 +190,12 @@ private boolean containsRenderedBrowserContent(Image screen) { screen.toRGB(visualBand, 0, 0, left, top, bandWidth, bandHeight); int[] rgb = visualBand.getRGB(); int requiredBrightPixels = Math.max(32, bandWidth / 20); + // The text occupies a small part of the band and the background the rest, so a + // quarter of the pixels is a comfortable floor for a genuinely rendered page and + // unreachable for a white one. + int requiredDarkPixels = Math.max(32, (bandWidth * bandHeight) / 4); int brightPixels = 0; + int darkPixels = 0; for (int y = 0; y < bandHeight; y++) { int rowOffset = y * bandWidth; for (int x = 0; x < bandWidth; x++) { @@ -188,12 +204,16 @@ private boolean containsRenderedBrowserContent(Image screen) { int g = (color >> 8) & 0xff; int b = color & 0xff; // The local fixture contains white and cyan text on a dark - // background. The black uncomposited peer contains neither. + // background. The black uncomposited peer has the dark and none of + // the text; the white unpainted one has neither. if ((r > 160 && g > 160 && b > 160) || (g > 120 && b > 160 && b > r + 30)) { - if (++brightPixels >= requiredBrightPixels) { - return true; - } + brightPixels++; + } else if (r < 48 && g < 48 && b < 48) { + darkPixels++; + } + if (brightPixels >= requiredBrightPixels && darkPixels >= requiredDarkPixels) { + return true; } } } From 753e2ac95973d76432b52f17ed59f3cbedd8c809 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 15:01:27 +0700 Subject: [PATCH 90/96] Marshal the combined health registration, error half included AsyncResource.onResult is ready() followed by except(), and only the first of those was overridden here -- so a worker thread registering on a resource that had already failed ran the error half immediately, on that worker. onResult is the application-facing form, so what that produces is an app handling a health error by touching a form off the EDT: the exact thing this class exists to prevent, reached through the other half of the same method. except() on its own stays synchronous. Reading the error out of an already-failed resource by registering a callback and looking at what it captured is an established idiom here and depends on that call being synchronous, and introspecting a failure is not the same act as handling one. Test settles a read into a failure, waits for it, then registers from a worker and asserts the error arrives on the EDT. It fails on the parent commit. Core suite 4740 green, SpotBugs 0, PMD gate clean. Co-Authored-By: Claude Opus 5 (1M context) --- .../com/codename1/impl/health/EdtResult.java | 30 ++++++++++++ .../health/HealthEdtDeliveryTest.java | 49 +++++++++++++++++++ quality-report.md | 4 +- 3 files changed, 81 insertions(+), 2 deletions(-) diff --git a/CodenameOne/src/com/codename1/impl/health/EdtResult.java b/CodenameOne/src/com/codename1/impl/health/EdtResult.java index d74a17aa802..720ff5b2786 100644 --- a/CodenameOne/src/com/codename1/impl/health/EdtResult.java +++ b/CodenameOne/src/com/codename1/impl/health/EdtResult.java @@ -23,6 +23,7 @@ package com.codename1.impl.health; import com.codename1.util.AsyncResource; +import com.codename1.util.AsyncResult; import com.codename1.util.SuccessCallback; import com.codename1.util.EasyThread; import com.codename1.ui.Display; @@ -98,6 +99,35 @@ public void run() { return super.ready(callback, t); } + /// The combined registration is marshalled whole, error branch included. + /// + /// `AsyncResource.onResult` is `ready(...)` followed by `except(...)`, and only the + /// first of those is overridden here -- so a worker thread registering on a resource + /// that had already failed ran the error half immediately, on that worker. This is the + /// application-facing form of the callback, so the failure it produces is an app + /// handling an error by touching a form off the EDT: the exact thing the class exists + /// to prevent, reached through the other half of the same method. + /// + /// `except` on its own stays synchronous, which is what the exemption above is about. + /// Reading the error out of an already-failed resource by registering a callback and + /// looking at what it captured depends on it, and introspecting a failure is not the + /// same act as handling one. + @Override + public void onResult(AsyncResult onResult) { + if (!isCancelled() && Display.isInitialized() + && !Display.getInstance().isEdt()) { + final AsyncResult target = onResult; + Display.getInstance().callSerially(new Runnable() { + @Override + public void run() { + EdtResult.super.onResult(target); + } + }); + return; + } + super.onResult(onResult); + } + @Override public void complete(T value) { if (Display.getInstance().isEdt()) { diff --git a/maven/core-unittests/src/test/java/com/codename1/health/HealthEdtDeliveryTest.java b/maven/core-unittests/src/test/java/com/codename1/health/HealthEdtDeliveryTest.java index 1c0b5230da7..122084712ef 100644 --- a/maven/core-unittests/src/test/java/com/codename1/health/HealthEdtDeliveryTest.java +++ b/maven/core-unittests/src/test/java/com/codename1/health/HealthEdtDeliveryTest.java @@ -366,6 +366,55 @@ public void run() { + "after it settled"); } + /// And a listener attached after a FAILURE arrives on the EDT too. + /// + /// `onResult` is `ready` followed by `except`, and only the first was marshalled -- + /// so this exact sequence, a worker registering on a resource that had already + /// failed, ran the error half synchronously on that worker. An app handling a health + /// error by showing a dialog or updating a label was then touching the UI off the + /// EDT, which is what this class exists to prevent, reached through the other half of + /// the same method. + @Test + void aListenerAttachedAfterAFailureAlsoArrivesOnTheEdt() { + final FakeHealthStore store = new FakeHealthStore(); + store.holdRead = true; + final Landing> landing = + new Landing>(); + CN.invokeAndBlock(new Runnable() { + public void run() { + assertFalse(CN.isEdt(), "the operation must start off the EDT"); + AsyncResource> failed = store.readSamples( + new SampleQuery() + .addType(HealthDataType.STEPS) + .setTimeRange(HealthTimeRange.between(0L, 1000L))); + store.heldRead.error(new IllegalStateException("the backend said no")); + long deadline = System.currentTimeMillis() + 10_000L; + while (!failed.isDone() && System.currentTimeMillis() < deadline) { + try { + Thread.sleep(10L); + } catch (InterruptedException ex) { + return; + } + } + assertTrue(failed.isDone(), "the fixture needs a settled failure"); + failed.onResult(landing); + deadline = System.currentTimeMillis() + 10_000L; + while (!landing.arrived.get() && System.currentTimeMillis() < deadline) { + try { + Thread.sleep(10L); + } catch (InterruptedException ex) { + return; + } + } + } + }); + assertTrue(landing.arrived.get(), "the callback must arrive"); + assertTrue(landing.failed.get(), "and it must be the error half"); + assertTrue(landing.onEdt.get(), + "an error delivered to a late listener is still a callback an app " + + "handles by touching the UI, so it belongs on the EDT"); + } + @Test void aDeleteDeliversOnTheEdt() { final FakeHealthStore store = new FakeHealthStore(); diff --git a/quality-report.md b/quality-report.md index 4c028dbe61d..db46513c79f 100644 --- a/quality-report.md +++ b/quality-report.md @@ -1,8 +1,8 @@ ## ✅ Continuous Quality Report ### Test & Coverage -- ✅ **Tests:** 4739 total, 0 failed, 0 skipped -- 📊 **Line coverage:** 58.84% +- ✅ **Tests:** 4740 total, 0 failed, 0 skipped +- 📊 **Line coverage:** 58.85% - **Lowest covered classes** - `com.codename1.gaming.level.GameSceneView` – 0.00% - `com.codename1.crash.CrashProtection` – 0.00% From 9f7957fe80e1306a77ef3bae0b1eb75112af68e8 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 16:53:37 +0700 Subject: [PATCH 91/96] Reserve a retained attestation for its own caller, and dispatch status under one lock An attestation cannot be produced twice, so the one whose caller cancelled is retained for the retry that asks for the same challenge. But retaining and delivering are not simultaneous: the callback retains the copy and queues the handover onto the EDT, so for the length of that hop the token is retained AND still reachable by the caller it was made for. A request arriving in that window took it, and both callers then submitted the same replay-protected attestation -- the second submission is rejected, and an app that reads a rejection as a bad key resets one the backend had just registered. The retained copy now remembers which resource it was produced for and is only handed on once that resource has lost its claim; a request arriving while the delivery is still undecided is told to retry rather than given a second copy. Losing the claim is now distinguishable from spending it on a delivery, which is what makes the question answerable. Separately, AppShield stored a status and enqueued its notification as two steps, so two network threads could interleave as: A stores, B stores, B enqueues, A enqueues. Listeners then finished on A while getStatus() already answered B -- a UI left saying "service down" for a shield that is fine, with nothing to correct it until the next transition. Both happen under the one lock now. The status test is staged rather than stressed: racing two threads at a window this narrow did not reproduce the old behaviour once in three runs, and a test that cannot fail on the unfixed code is not a regression test. Holding the listener monitor stops the first transition exactly where the enqueue happens, which makes "has the second one stored its status yet" a question with an answer. Core suite 4741 green, SpotBugs 0 on core-unittests and the iOS port, PMD gate clean. Co-Authored-By: Claude Opus 5 (1M context) --- .../codename1/security/shield/AppShield.java | 36 ++++++-- .../impl/ios/IOSDeviceIntegrity.java | 65 +++++++++++++++ .../security/shield/ShieldApiTest.java | 1 + .../security/shield/ShieldInitOrderTest.java | 83 +++++++++++++++++++ quality-report.md | 4 +- 5 files changed, 179 insertions(+), 10 deletions(-) diff --git a/CodenameOne/src/com/codename1/security/shield/AppShield.java b/CodenameOne/src/com/codename1/security/shield/AppShield.java index 0d1a38ac355..e978a3694e4 100644 --- a/CodenameOne/src/com/codename1/security/shield/AppShield.java +++ b/CodenameOne/src/com/codename1/security/shield/AppShield.java @@ -837,25 +837,45 @@ public static void removeListener(ShieldListener l) { // Internals // ----------------------------------------------------------------- + /// Test hook: drives a status transition, so the ordering between the transition and + /// the notification can be asserted from a test rather than only reasoned about. + static void setStatusForTesting(ShieldStatus status) { + setStatus(status); + } + private static void setStatus(ShieldStatus status) { if (status == null) { return; } - ShieldListener[] copy; + // The transition and its dispatch happen under one lock, so the order listeners + // are told in is the order the transitions happened in. + // + // Storing the status and enqueueing the notification as two steps let two network + // threads interleave: A stores, B stores and enqueues, A enqueues. Listeners then + // finished on A while getStatus() already answered B -- a UI showing "service + // down" for a shield that is fine, or the reverse, with nothing further to correct + // it because the next transition is the one after that. + // + // Holding the monitor across callSerially is safe and deliberate: it only appends + // to the EDT queue, and nothing on that path calls back into this class. The + // alternative -- sequence numbers and dropped stale dispatches -- gives listeners + // the right final state but silently swallows intermediate ones, and a listener + // that logs or counts transitions has every reason to want them all. synchronized (AppShield.class) { if (status.equals(lastStatus)) { return; } lastStatus = status; - } - synchronized (listeners) { - if (listeners.isEmpty()) { - return; + ShieldListener[] copy; + synchronized (listeners) { + if (listeners.isEmpty()) { + return; + } + copy = new ShieldListener[listeners.size()]; + listeners.copyInto(copy); } - copy = new ShieldListener[listeners.size()]; - listeners.copyInto(copy); + Display.getInstance().callSerially(new StatusDispatch(copy, status)); } - Display.getInstance().callSerially(new StatusDispatch(copy, status)); } /// The token handle handed back to callers, where exactly one of cancellation and diff --git a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java index d3c8fec275e..bd6352d1cc2 100644 --- a/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java +++ b/Ports/iOSPort/src/com/codename1/impl/ios/IOSDeviceIntegrity.java @@ -305,6 +305,20 @@ final class IOSDeviceIntegrity { private String undeliveredAttestNonce; private String undeliveredAttestToken; + /** + * The resource the retained attestation was produced FOR, while its delivery is still + * undecided. + * + *

The retained copy is meant for the caller that walked away, and "walked away" + * has to be a decided fact. Delivery is queued onto the EDT, so between the callback + * and that runnable the token is retained and the original caller may still get it -- + * a second request arriving in that window used to take the copy and hand it to + * itself, and both callers then submitted the same replay-protected attestation. The + * duplicate is rejected, and an app that treats a rejection as a bad key resets one + * the backend had just registered.

+ */ + private OneShotResource undeliveredAttestResult; + /** * Set when the keychain refused to delete part of a discarded identity. * @@ -409,6 +423,7 @@ private void resetLocked(boolean keepSpentMarker) { undeliveredAttestKeyId = null; undeliveredAttestNonce = null; undeliveredAttestToken = null; + undeliveredAttestResult = null; deliveredAttestKeyId = null; inMemoryStateKeyId = null; inMemoryState = null; @@ -565,6 +580,7 @@ private void promoteToAttested(SecureStorage store, String keyId, boolean holdIn undeliveredAttestKeyId = null; undeliveredAttestNonce = null; undeliveredAttestToken = null; + undeliveredAttestResult = null; attestAnsweredForKey = null; recoverySpentInMemory = !store.remove(KEY_RECOVERY_SPENT); } @@ -657,6 +673,24 @@ AsyncResource requestToken(String nonce) { // so it is handed to the retry that asks for the same challenge. if (undeliveredAttestToken != null && nonce.equals(undeliveredAttestNonce)) { + // Only once the delivery it was produced for has actually lost. + // + // Retaining and delivering are not simultaneous: the callback retains + // the copy and queues the handover onto the EDT, so for the length of + // that hop the token is retained AND still reachable by its original + // caller. A request arriving in that window took the copy, and both + // callers then submitted the same attestation -- which is + // replay-protected, so the second submission is rejected, and an app + // that reads a rejection as a bad key resets one the backend had just + // registered. Asking the resource itself makes this a fact about a + // decided claim rather than a guess about an undecided one. + if (undeliveredAttestResult != null + && !undeliveredAttestResult.lostItsClaim()) { + r.error(new RuntimeException("an App Attest attestation for this " + + "challenge is being handed to another caller; retry " + + "once it has finished")); + return r; + } // Through the one-shot claim, like the scheduled delivery. Calling // the inherited complete() left the claim unspent, so a caller that // cancelled the resource it had just been handed still won -- while @@ -669,6 +703,7 @@ AsyncResource requestToken(String nonce) { undeliveredAttestKeyId = null; undeliveredAttestNonce = null; undeliveredAttestToken = null; + undeliveredAttestResult = null; // The same promotion the scheduled delivery does, including what to // do when the keychain refuses it: this caller is walking away with // the attestation, so a persisted state still saying nobody received @@ -693,6 +728,7 @@ AsyncResource requestToken(String nonce) { undeliveredAttestKeyId = null; undeliveredAttestNonce = null; undeliveredAttestToken = null; + undeliveredAttestResult = null; resetLocked(); keyId = null; state = null; @@ -1055,6 +1091,10 @@ public static void nativeAttestationReady(final int requestId, final String atte instance.undeliveredAttestKeyId = pending.keyId; instance.undeliveredAttestNonce = pending.nonce; instance.undeliveredAttestToken = attestToken; + // And who it is for, until that is decided. Delivery is queued onto the EDT, + // so the copy is retained for a stretch during which the original caller can + // still receive it. + instance.undeliveredAttestResult = pending.result; if (!store.set(KEY_STATE, STATE_PENDING_UNDELIVERED)) { // The key is attested with Apple and the keychain will not record it. // @@ -1179,6 +1219,27 @@ static final class OneShotResource extends AsyncResource { private final java.util.concurrent.atomic.AtomicBoolean claimed = new java.util.concurrent.atomic.AtomicBoolean(); + /** + * Whether the claim was spent by a DELIVERY, as opposed to a cancellation or a + * failure. Written before the value is published and read from another thread, so + * volatile. + */ + private volatile boolean delivered; + + /** + * True once this resource can never receive the attestation: its claim is spent + * and it was not spent by handing the value over. + * + *

What the retained-attestation branch needs to know. An attestation cannot be + * produced twice, so the retained copy exists for the caller that cancelled -- + * but "cancelled" has to mean the delivery LOST, not merely that a delivery is + * still queued. Asking this way makes the answer a fact about a claim that has + * already been decided rather than a guess about one that has not.

+ */ + boolean lostItsClaim() { + return claimed.get() && !delivered; + } + @Override public boolean cancel(boolean mayInterruptIfRunning) { if (!claimed.compareAndSet(false, true)) { @@ -1194,6 +1255,9 @@ boolean deliver(String value) { if (!claimed.compareAndSet(false, true)) { return false; } + // Before the value is published, so a thread that sees the claim spent never + // sees it as a loss when it was a win. + delivered = true; super.complete(value); return true; } @@ -1499,6 +1563,7 @@ public void run() { instance.undeliveredAttestKeyId = null; instance.undeliveredAttestNonce = null; instance.undeliveredAttestToken = null; + instance.undeliveredAttestResult = null; // Somebody has it, so the key is pending registration rather // than pending delivery. A refused write leaves the persisted // state saying nobody received it, which would have the next diff --git a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java index 8b9b1f85225..79d7beba2e3 100644 --- a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java +++ b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldApiTest.java @@ -608,4 +608,5 @@ void hasSignalAtLeastReflectsRecordedSeverities() { assertFalse(ShieldSignals.hasSignalAtLeast(31)); ShieldSignals.clear(); } + } diff --git a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldInitOrderTest.java b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldInitOrderTest.java index 26aeccf64d6..e8a396415ea 100644 --- a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldInitOrderTest.java +++ b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldInitOrderTest.java @@ -486,4 +486,87 @@ public void invalidate() { public void shutdown() { } } + + /** + * A status transition and the notification for it are one step, not two. + * + *

Storing the status and enqueueing its callback used to be separate, so two + * network threads could interleave as: A stores, B stores, B enqueues, A enqueues. + * Listeners then finished on A while {@link AppShield#getStatus()} already answered + * B -- a UI left saying "service down" for a shield that is fine, with nothing to + * correct it until the next transition.

+ * + *

Staged rather than stressed. The window is between two statements inside one + * method, and a loop racing two threads at it did not reproduce the old behaviour + * here even once in three runs -- a test that cannot fail on the unfixed code is not + * a regression test. Holding the listener monitor stops the first transition exactly + * where the enqueue happens, which makes the question "has the second one stored its + * status yet" answerable rather than probable.

+ */ + @Test + void aStatusIsNotStoredWhileAnEarlierOneIsStillBeingDispatched() throws Exception { + AppShield.addListener(new RecordingListener()); + java.lang.reflect.Field listenersField = + AppShield.class.getDeclaredField("listeners"); + listenersField.setAccessible(true); + Object listenerLock = listenersField.get(null); + java.lang.reflect.Field statusField = + AppShield.class.getDeclaredField("lastStatus"); + statusField.setAccessible(true); + + final CountDownLatch firstEntered = new CountDownLatch(1); + final CountDownLatch secondStarted = new CountDownLatch(1); + Thread first; + Thread second; + synchronized (listenerLock) { + first = new Thread(new Runnable() { + public void run() { + firstEntered.countDown(); + AppShield.setStatusForTesting(ShieldStatus.SERVICE_DOWN); + } + }, "shield-status-first"); + first.setDaemon(true); + first.start(); + assertTrue(firstEntered.await(GENEROUS_TIMEOUT_MS, TimeUnit.MILLISECONDS)); + // It is now inside setStatus, blocked where the notification is enqueued. + Thread.sleep(BLOCKED_OBSERVATION_MS); + + second = new Thread(new Runnable() { + public void run() { + secondStarted.countDown(); + AppShield.setStatusForTesting(ShieldStatus.OK); + } + }, "shield-status-second"); + second.setDaemon(true); + second.start(); + assertTrue(secondStarted.await(GENEROUS_TIMEOUT_MS, TimeUnit.MILLISECONDS)); + Thread.sleep(BLOCKED_OBSERVATION_MS); + + // Read directly rather than through getStatus(), which would block on the + // monitor the first thread is holding and deadlock this test. + assertEquals(ShieldStatus.SERVICE_DOWN, statusField.get(null), + "the second transition must not be stored while the first one has " + + "not finished announcing itself -- that is the reordering: " + + "listeners end on the older status while getStatus reports the " + + "newer one"); + } + + first.join(GENEROUS_TIMEOUT_MS); + second.join(GENEROUS_TIMEOUT_MS); + assertEquals(ShieldStatus.OK, AppShield.getStatus(), + "and both transitions still happen, in order"); + } + + /** Records nothing; its presence is what makes setStatus reach the dispatch. */ + private static final class RecordingListener implements ShieldListener { + + public void signalRaised(ShieldSignal signal) { + } + + public void tokenRefreshed(ShieldToken token) { + } + + public void statusChanged(ShieldStatus status) { + } + } } diff --git a/quality-report.md b/quality-report.md index db46513c79f..281dc1f0891 100644 --- a/quality-report.md +++ b/quality-report.md @@ -1,8 +1,8 @@ ## ✅ Continuous Quality Report ### Test & Coverage -- ✅ **Tests:** 4740 total, 0 failed, 0 skipped -- 📊 **Line coverage:** 58.85% +- ✅ **Tests:** 4741 total, 0 failed, 0 skipped +- 📊 **Line coverage:** 58.90% - **Lowest covered classes** - `com.codename1.gaming.level.GameSceneView` – 0.00% - `com.codename1.crash.CrashProtection` – 0.00% From 328ed957094fc138e859032e0cb2b2d3de9256fe Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 22:05:38 +0700 Subject: [PATCH 92/96] Fence a restarted MCP reader, drop superseded signal notifications, and stop calling a Mac jailbroken Three things, one of which was breaking CI. isJailbrokenDevice() used to be the cydia probe, which never answers on a modern iOS, and this branch rewired it to the real detector. On a Mac -- Catalyst, and the mac-native screenshot suite -- that detector fires: /bin/bash and /usr/sbin/sshd ship with macOS and /private is writable, so the two probes that describe an escaped iOS sandbox both hit on a stock machine. The smoke app asks the question at startup and refuses to launch, which is why the suite emitted nothing at all and timed out waiting for a completion marker. Those two probes are now iOS-only; the instrumentation ones on either side stay, because an injected dylib means the same thing wherever it is loaded. Verified by compiling the file for Catalyst and running it natively: jailbreakFile before, nothing after. A restart over the same transport instance could leave two readers on one stream. Both production transports clear their closed flag in open(), so a reader still parked in readMessage() from the previous generation is looking at a live stream again the moment the replacement opens, and can take the new client's first frame. The replacement now waits for the previous reader, and every read re-checks whether its server is still current before handling anything. The wait is bounded at two seconds, which the first version was not, and that cost me a three-hour hang: MCPTransport is a public interface and an implementation may park readMessage() until something other than close() releases it -- the loopback test transport is exactly that shape -- so waiting forever deadlocks the restart against a thread only the caller can end. stop() closes first, so a real socket unwinds in microseconds and the bound is never observed. And a superseded signal observation is no longer announced. Storing and enqueueing cannot be merged here, because callSerially runs inline before the EDT is up and notifying under the signal monitor would run application listeners while holding it -- so the dispatch checks on arrival whether it still describes what the bus holds. Listeners can no longer end on an observation snapshot() has already replaced. Core suite 4743 green, SpotBugs 0 on core-unittests and the iOS port, PMD gate clean. Co-Authored-By: Claude Opus 5 (1M context) --- .../src/com/codename1/mcp/MCPServer.java | 112 ++++++++++- .../security/shield/ShieldSignals.java | 47 ++++- .../nativeSources/CN1JailbreakDetector.m | 8 + .../mcp/MCPRestartReaderFenceTest.java | 140 ++++++++++++++ .../shield/ShieldSignalOrderTest.java | 179 ++++++++++++++++++ quality-report.md | 6 +- 6 files changed, 483 insertions(+), 9 deletions(-) create mode 100644 maven/core-unittests/src/test/java/com/codename1/mcp/MCPRestartReaderFenceTest.java create mode 100644 maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldSignalOrderTest.java diff --git a/CodenameOne/src/com/codename1/mcp/MCPServer.java b/CodenameOne/src/com/codename1/mcp/MCPServer.java index 3132c269366..932d22bf424 100644 --- a/CodenameOne/src/com/codename1/mcp/MCPServer.java +++ b/CodenameOne/src/com/codename1/mcp/MCPServer.java @@ -325,6 +325,87 @@ private boolean openSerialized(MCPTransport t, int generation) { /// at all. private final List openLocks = new ArrayList(); + /// Transports with a reader still inside their loop, and which generation it belongs + /// to. + /// + /// A restart over the SAME transport instance is the case this exists for. Both + /// production transports clear their closed flag in open(), so a reader still parked + /// in readMessage() from the previous generation is looking at a live stream again the + /// moment the replacement opens -- and it can take a frame the new client sent. The + /// frame is then handled by a loop that belongs to a stopped server, or dropped + /// entirely; the new session simply never sees it, which reads as a client that hangs. + /// + /// stop() closes the transport before any of this, so the stale read unwinds + /// immediately and the wait below is measured in the time that takes rather than in + /// anything the client controls. + private final List activeReaders = new ArrayList(); + + /// How long a replacement waits for the previous reader of the same transport before + /// opening anyway. + /// + /// Bounded rather than indefinite, and the bound is the whole design. `stop()` closes + /// the transport first, so a reader blocked on a real socket unwinds in microseconds + /// and this wait is never observed. [MCPTransport] is a public interface, though, and + /// an implementation is entitled to a `readMessage()` that parks until something other + /// than `close()` releases it -- the loopback test transport is exactly that. Waiting + /// forever for such a reader deadlocks the restart against a thread only the caller + /// can end, which is worse than the overlap this is guarding against: the loop already + /// re-checks `isCurrent` after every read, so a stale reader cannot HANDLE anything, + /// and what is left is a frame it might swallow. + private static final long READER_HANDOVER_WAIT_MS = 2000L; + + /// Registers this generation as the reader of {@code t}, waiting out any older one. + private void awaitSoleReader(MCPTransport t, int generation) { + synchronized (activeReaders) { + long deadline = System.currentTimeMillis() + READER_HANDOVER_WAIT_MS; + for (;;) { + Object[] found = null; + for (Object[] entry : activeReaders) { + if (entry[0] == t) { // NOPMD identity: one reader per INSTANCE + found = entry; + break; + } + } + if (found == null) { + activeReaders.add(new Object[] {t, Integer.valueOf(generation)}); + return; + } + if (((Integer) found[1]).intValue() == generation) { + return; + } + long remaining = deadline - System.currentTimeMillis(); + if (remaining <= 0L) { + // The previous reader is not coming back on its own. Taking the + // registration over rather than leaving it to the departed generation + // keeps a third restart waiting for THIS thread, which is the one + // actually on the transport. + found[1] = Integer.valueOf(generation); + return; + } + try { + activeReaders.wait(remaining); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + return; + } + } + } + } + + private void releaseReader(MCPTransport t, int generation) { + synchronized (activeReaders) { + for (java.util.Iterator it = activeReaders.iterator(); it.hasNext();) { + Object[] entry = it.next(); + if (entry[0] == t // NOPMD identity: one reader per INSTANCE + && ((Integer) entry[1]).intValue() == generation) { + it.remove(); + break; + } + } + activeReaders.notifyAll(); + } + } + private Object acquireOpenLock(MCPTransport t) { synchronized (openLocks) { for (Object[] entry : openLocks) { @@ -370,9 +451,27 @@ private void runLoop(MCPTransport t, int generation) { if (!isCurrent(t, generation)) { return; } - if (!openSerialized(t, generation)) { - return; + // Before opening, not after: a reader from the previous generation of this same + // transport instance may still be parked in readMessage(), and open() clears the + // flag that would have ended it. Opening first would put two readers on one + // stream, and the frame the new client sends can go to the one that no longer + // belongs to anybody. + awaitSoleReader(t, generation); + try { + if (!isCurrent(t, generation)) { + return; + } + if (!openSerialized(t, generation)) { + return; + } + readUntilClosed(t, generation); + } finally { + releaseReader(t, generation); } + releaseAndCloseIfCurrent(t, generation); + } + + private void readUntilClosed(MCPTransport t, int generation) { while (isCurrent(t, generation)) { String line; try { @@ -380,6 +479,14 @@ private void runLoop(MCPTransport t, int generation) { } catch (IOException ex) { break; } + // Re-checked on the far side of the read as well as the near side. A read + // blocks for as long as the client is quiet, which is most of the time, so + // "was this server current when the read started" says nothing about whether + // it still is when the read returns -- and handling a request for a server + // that has been stopped answers on a transport somebody else may now own. + if (!isCurrent(t, generation)) { + break; + } if (line == null) { break; } @@ -395,7 +502,6 @@ private void runLoop(MCPTransport t, int generation) { } } } - releaseAndCloseIfCurrent(t, generation); } /// Handles one inbound JSON-RPC message and returns the response line, or null diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldSignals.java b/CodenameOne/src/com/codename1/security/shield/ShieldSignals.java index 61eddf55811..686c1b1a2b0 100644 --- a/CodenameOne/src/com/codename1/security/shield/ShieldSignals.java +++ b/CodenameOne/src/com/codename1/security/shield/ShieldSignals.java @@ -91,9 +91,19 @@ public static void add(ShieldSignal signal) { signals.addElement(signal); } } - // Outside the lock on every path. Display.callSerially runs the task - // inline when the EDT is not up yet, so a listener that calls back into - // snapshot() would deadlock on the monitor we were still holding. + // Outside the lock on every path. Display.callSerially runs the task inline when + // the EDT is not up yet, so notifying in here would run application listeners + // while holding the signal monitor -- and a listener that waits on anything which + // needs that monitor deadlocks. + // + // Which is why the ordering problem is solved at the far end instead. Two workers + // reporting different observations of one id could interleave as: A stores, B + // stores over it, B enqueues, A enqueues -- and listeners then finished on A while + // snapshot() already answered B, with nothing later guaranteed to correct them. + // The dispatch checks on arrival whether it still describes the current + // observation and drops itself if it does not, so the last thing a listener is + // told is always what the bus holds. A superseded observation is not worth + // announcing: it was already wrong when it was queued. notifyListeners(signal); } @@ -168,6 +178,30 @@ private static void notifyListeners(ShieldSignal signal) { Display.getInstance().callSerially(new SignalDispatch(copy, signal)); } + /** + * Whether this is still the observation the bus holds for its id. + * + *

Identity, not equality: the entry is replaced by the object that superseded it, + * so anything else under that id means a newer report has been stored and has queued + * its own notification.

+ */ + static boolean isCurrentObservation(ShieldSignal signal) { + synchronized (signals) { + for (int i = 0; i < signals.size(); i++) { + ShieldSignal existing = (ShieldSignal) signals.elementAt(i); + if (existing.getId().equals(signal.getId())) { + // NOPMD identity is the question: a newer report REPLACES the entry, + // so anything but the same object means this one has been superseded. + // Equality would call two distinct reports of the same observation + // current, which is the case this exists to tell apart. + return existing == signal; // NOPMD + } + } + } + // Cleared, or evicted by the bound. Either way nothing is claiming it now. + return false; + } + private static final class SignalDispatch implements Runnable { private final ShieldListener[] targets; private final ShieldSignal signal; @@ -179,6 +213,13 @@ private static final class SignalDispatch implements Runnable { @Override public void run() { + // Checked here rather than at enqueue time, because the point is the state + // at delivery: a report that has been superseded between being queued and + // arriving would otherwise leave listeners holding an observation the bus + // itself no longer has. + if (!isCurrentObservation(signal)) { + return; + } for (ShieldListener target : targets) { target.signalRaised(signal); } diff --git a/Ports/iOSPort/nativeSources/CN1JailbreakDetector.m b/Ports/iOSPort/nativeSources/CN1JailbreakDetector.m index 911336d8892..f2c695de4fd 100644 --- a/Ports/iOSPort/nativeSources/CN1JailbreakDetector.m +++ b/Ports/iOSPort/nativeSources/CN1JailbreakDetector.m @@ -70,6 +70,13 @@ } } + // The two filesystem probes below describe an iOS sandbox that has been broken out + // of, and a Mac is not that sandbox. /bin/bash and /usr/sbin/sshd ship with macOS and + // /private is writable there, so on Mac Catalyst both fire on a stock machine -- and + // an app that asks isJailbrokenDevice() at startup, as ours does, refuses to launch on + // every Mac. The instrumentation probes on either side of this stay, because an + // injected dylib or a hooking library means the same thing wherever it is loaded. +#if !TARGET_OS_MACCATALYST && !TARGET_OS_OSX // Files that only exist once the sandbox has been broken out of. NSArray *restrictedPaths = @[ @"/Applications/Cydia.app", @@ -96,6 +103,7 @@ [fileManager removeItemAtPath:testPath error:nil]; [signals addObject:@"restrictedWrite"]; } +#endif // A debugger or instrumentation tool attached to the process. struct kinfo_proc info; diff --git a/maven/core-unittests/src/test/java/com/codename1/mcp/MCPRestartReaderFenceTest.java b/maven/core-unittests/src/test/java/com/codename1/mcp/MCPRestartReaderFenceTest.java new file mode 100644 index 00000000000..66c365e96b5 --- /dev/null +++ b/maven/core-unittests/src/test/java/com/codename1/mcp/MCPRestartReaderFenceTest.java @@ -0,0 +1,140 @@ +/* + * Copyright (c) 2026, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ +package com.codename1.mcp; + +import java.io.IOException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * A restart over the same transport instance must not leave two readers on one stream. + * + *

Both production transports clear their closed flag in {@code open()}, so a reader + * still parked in {@code readMessage()} from the previous generation is looking at a live + * stream again the moment the replacement opens. It can then take a frame the new client + * sent -- handled by a loop belonging to a stopped server, or dropped entirely, which + * presents as a client whose first request never gets an answer.

+ * + *

The transport here is deliberately the awkward-but-legal shape: {@code readMessage()} + * parks until {@code close()} releases it, and reopening makes it readable again.

+ */ +class MCPRestartReaderFenceTest { + + /** Long enough that a slow machine cannot fail it; short enough to notice. */ + private static final long TIMEOUT_MS = 10000L; + + @Test + void theSecondGenerationWaitsForTheFirstReaderToLeave() throws Exception { + SlowReaderTransport transport = new SlowReaderTransport(); + MCPServer server = new MCPServer(); + server.start(transport); + + assertTrue(transport.reading.await(TIMEOUT_MS, TimeUnit.MILLISECONDS), + "the first generation should be inside readMessage()"); + + // A read that does not unwind the instant close() is called. Real ones usually + // do -- a closed socket ends the read -- but "usually" is the whole problem: the + // fence exists for the interval between close() and the read noticing, and this + // fixture is that interval held open so it can be observed. + server.stop(); + server.start(transport); + Thread.sleep(300L); + + assertEquals(1, transport.opensSeen.get(), + "the replacement must not open the transport while a reader from the " + + "previous generation is still inside it -- open() clears the closed " + + "flag, so that reader is looking at a live stream again and can take " + + "the new client's first frame"); + assertFalse(transport.openedWithReaderInside, + "and if it ever does, the two generations are sharing one stream"); + + transport.letTheStaleReadFinish(); + assertTrue(transport.reopened.await(TIMEOUT_MS, TimeUnit.MILLISECONDS), + "once the stale reader leaves, the replacement gets its own open()"); + + transport.letTheStaleReadFinish(); + server.stop(); + assertFalse(server.isRunning()); + } + + /** + * Parks in {@code readMessage()} until the test says otherwise, and becomes readable + * again on reopen -- which is exactly what makes a stale reader dangerous. + */ + private static final class SlowReaderTransport implements MCPTransport { + + private final CountDownLatch reading = new CountDownLatch(1); + private final CountDownLatch reopened = new CountDownLatch(1); + private final AtomicInteger opensSeen = new AtomicInteger(); + private final AtomicInteger readersInside = new AtomicInteger(); + private volatile boolean openedWithReaderInside; + private volatile CountDownLatch release = new CountDownLatch(1); + + void letTheStaleReadFinish() { + release.countDown(); + } + + @Override + public void open() throws IOException { + if (readersInside.get() > 0) { + openedWithReaderInside = true; + } + // A fresh gate per open, like a transport clearing its closed flag. + release = new CountDownLatch(1); + if (opensSeen.incrementAndGet() >= 2) { + reopened.countDown(); + } + } + + @Override + public synchronized void close() { + // Deliberately does NOT end the parked read. See the test. + } + + @Override + public String readMessage() throws IOException { + CountDownLatch gate = release; + readersInside.incrementAndGet(); + reading.countDown(); + try { + gate.await(TIMEOUT_MS * 3, TimeUnit.MILLISECONDS); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IOException("interrupted"); + } finally { + readersInside.decrementAndGet(); + } + return null; + } + + @Override + public void writeMessage(String message) throws IOException { + } + } +} diff --git a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldSignalOrderTest.java b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldSignalOrderTest.java new file mode 100644 index 00000000000..f37e831cd57 --- /dev/null +++ b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldSignalOrderTest.java @@ -0,0 +1,179 @@ +/* + * Copyright (c) 2026, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ +package com.codename1.security.shield; + +import com.codename1.junit.UITestBase; +import com.codename1.ui.Display; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * What a listener is holding when the dust settles has to be what the bus holds. + * + *

Storing an observation and enqueueing its notification are separate steps, and they + * cannot be merged: {@code Display.callSerially} runs the task inline before the EDT is + * up, so notifying under the signal monitor would run application listeners while holding + * it. Two workers reporting different observations of one signal could therefore + * interleave as A stores, B stores over it, B enqueues, A enqueues -- and listeners + * finished on A while {@code snapshot()} already answered B, with nothing later + * guaranteed to correct them. A device that reported a hooking framework and then a clean + * state, in that order, would leave an app looking at the compromise indefinitely; the + * reverse leaves it looking at the clean one.

+ */ +class ShieldSignalOrderTest extends UITestBase { + + private static final long GENEROUS_TIMEOUT_MS = 10000L; + + private final List seen = new ArrayList(); + private ShieldListener listener; + + @BeforeEach + void attach() { + ShieldSignals.clear(); + listener = new ShieldListener() { + public void signalRaised(ShieldSignal signal) { + synchronized (seen) { + seen.add(signal); + } + } + + public void tokenRefreshed(ShieldToken token) { + } + + public void statusChanged(ShieldStatus status) { + } + }; + ShieldSignals.addListener(listener); + } + + @AfterEach + void detach() { + ShieldSignals.removeListener(listener); + ShieldSignals.clear(); + } + + /** + * A superseded observation is never announced, whichever order the two notifications + * were queued in. + * + *

Staged through the listener monitor, which the notification path takes after the + * signal has already been stored. That parks the first report exactly where its + * notification is enqueued, so the second can overtake it deterministically -- racing + * two threads at a window this narrow is not something a test can rely on.

+ */ + @Test + void aListenerNeverEndsOnAnObservationTheBusHasReplaced() throws Exception { + java.lang.reflect.Field listenersField = + ShieldSignals.class.getDeclaredField("listeners"); + listenersField.setAccessible(true); + Object listenerLock = listenersField.get(null); + + final CountDownLatch firstIn = new CountDownLatch(1); + final CountDownLatch secondIn = new CountDownLatch(1); + Thread first; + Thread second; + synchronized (listenerLock) { + first = new Thread(new Runnable() { + public void run() { + firstIn.countDown(); + ShieldSignals.add(ShieldSignal.HOOK, 90, "frida"); + } + }, "shield-signal-first"); + first.setDaemon(true); + first.start(); + assertTrue(firstIn.await(GENEROUS_TIMEOUT_MS, TimeUnit.MILLISECONDS)); + Thread.sleep(200L); + + second = new Thread(new Runnable() { + public void run() { + secondIn.countDown(); + ShieldSignals.add(ShieldSignal.HOOK, 10, "gone"); + } + }, "shield-signal-second"); + second.setDaemon(true); + second.start(); + assertTrue(secondIn.await(GENEROUS_TIMEOUT_MS, TimeUnit.MILLISECONDS)); + Thread.sleep(200L); + } + first.join(GENEROUS_TIMEOUT_MS); + second.join(GENEROUS_TIMEOUT_MS); + drainTheEventQueue(); + + ShieldSignal current = null; + for (ShieldSignal s : ShieldSignals.snapshot()) { + if (ShieldSignal.HOOK.equals(s.getId())) { + current = s; + } + } + assertEquals(10, current == null ? -1 : current.getSeverity(), + "the fixture needs the second report to be the one the bus holds"); + + synchronized (seen) { + assertEquals(1, countFor(ShieldSignal.HOOK), + "the superseded report was already wrong when it was queued, so it " + + "is not announced: " + seen); + assertEquals(10, lastFor(ShieldSignal.HOOK), + "and what the listener is left holding is what snapshot() answers"); + } + } + + private int countFor(String id) { + int n = 0; + for (ShieldSignal s : seen) { + if (id.equals(s.getId())) { + n++; + } + } + return n; + } + + private int lastFor(String id) { + int severity = -1; + for (ShieldSignal s : seen) { + if (id.equals(s.getId())) { + severity = s.getSeverity(); + } + } + return severity; + } + + /** Waits for everything queued so far to have run, without blocking the EDT. */ + private void drainTheEventQueue() throws Exception { + final CountDownLatch drained = new CountDownLatch(1); + Display.getInstance().callSerially(new Runnable() { + public void run() { + drained.countDown(); + } + }); + assertTrue(drained.await(GENEROUS_TIMEOUT_MS, TimeUnit.MILLISECONDS), + "the dispatch queue has to drain for this to mean anything"); + } +} diff --git a/quality-report.md b/quality-report.md index 281dc1f0891..0bb6725ed04 100644 --- a/quality-report.md +++ b/quality-report.md @@ -1,8 +1,8 @@ ## ✅ Continuous Quality Report ### Test & Coverage -- ✅ **Tests:** 4741 total, 0 failed, 0 skipped -- 📊 **Line coverage:** 58.90% +- ✅ **Tests:** 4743 total, 0 failed, 0 skipped +- 📊 **Line coverage:** 58.86% - **Lowest covered classes** - `com.codename1.gaming.level.GameSceneView` – 0.00% - `com.codename1.crash.CrashProtection` – 0.00% @@ -11,9 +11,9 @@ - `com.codename1.crash.CrashReportPayload` – 0.00% - `com.codename1.vr.VRView` – 0.00% - `com.codename1.appreview.RatingDialog` – 0.00% + - `com.codename1.security.shield.ShieldSignals` – 0.00% - `com.codename1.calendar.DefaultCalendarHttpTransport` – 0.00% - `com.codename1.security.Secrets` – 0.00% - - `com.codename1.calendar.OidcCalendarTokenProvider` – 0.00% ### Static Analysis - **SpotBugs** From 7e4e92ff42e520248f029a7769537cb18508366a Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Sun, 2 Aug 2026 22:08:22 +0700 Subject: [PATCH 93/96] Use a markdown comment for the new signal helper The Java 25 docs gate refuses classic Javadoc markers in CodenameOne and CLDC11, and I wrote the new helper the old way. Same text, /// form. Co-Authored-By: Claude Opus 5 (1M context) --- .../com/codename1/security/shield/ShieldSignals.java | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/CodenameOne/src/com/codename1/security/shield/ShieldSignals.java b/CodenameOne/src/com/codename1/security/shield/ShieldSignals.java index 686c1b1a2b0..5bc6448f503 100644 --- a/CodenameOne/src/com/codename1/security/shield/ShieldSignals.java +++ b/CodenameOne/src/com/codename1/security/shield/ShieldSignals.java @@ -178,13 +178,11 @@ private static void notifyListeners(ShieldSignal signal) { Display.getInstance().callSerially(new SignalDispatch(copy, signal)); } - /** - * Whether this is still the observation the bus holds for its id. - * - *

Identity, not equality: the entry is replaced by the object that superseded it, - * so anything else under that id means a newer report has been stored and has queued - * its own notification.

- */ + /// Whether this is still the observation the bus holds for its id. + /// + /// Identity, not equality: the entry is replaced by the object that superseded it, so + /// anything else under that id means a newer report has been stored and has queued its + /// own notification. static boolean isCurrentObservation(ShieldSignal signal) { synchronized (signals) { for (int i = 0; i < signals.size(); i++) { From fa7c94412a7f1d81b8aab6dd120af973cd762a87 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Mon, 3 Aug 2026 09:56:38 +0700 Subject: [PATCH 94/96] Four findings from review bodies I had never been reading My check for outstanding feedback queried review THREADS, and these arrived as review bodies -- so I reported these PRs as having nothing open while four findings sat in them. The mechanism is now part of the sweep; the findings are below. One cipher per biometric operation. The prompt is raised from a UI runnable, so an operation is in flight from the moment it initialises its cipher until that runnable executes -- and with one instance field a second set() or get() starting in that window re-initialised the same object, handing the first prompt the second operation's cipher. Wrong mode, or the wrong account's IV, and then the failure handler read that as an invalidated key. Which is the second finding: that handler deleted the single keystore key behind every biometric account for ANY failure -- a malformed stored value, an Activity that went away mid-prompt -- making every other entry permanently unreadable while telling the caller its key had been revoked. Only the two shapes that mean the key itself is finished delete it now: the exception Android raises, and the Samsung 8.0.0 quirk of a cipher that initialises and then fails inside doFinal with a keystore error underneath, which is what the original workaround was for. A loopback connection belongs to the listener that accepted it. The callback resolved the process-wide active transport when it RAN, so a transport that accepted a client just before closing handed those streams to whatever opened next -- a client of the stopped listener taking over the new server's session. The binding is captured when the socket API constructs the callback, which is at accept time, and a connection whose listener has since closed is released rather than adopted. And two in the simulator's secure storage: the key derivation is locked on the class rather than the instance, because JavaSEPort.getSecureStorage() builds its singleton without synchronization and two instances would each generate a salt into the same shared node -- whichever lost leaving permanently undecryptable ciphertext; and the literal zero byte in the source is now an escape, which is what makes git treat the file as text again. It had been classified as binary, so diffs reported "- -", grep matched nothing, and the two greps I ran while investigating this file silently returned empty. Core suite 4744 green, SpotBugs 0 on core-unittests, android and ios, PMD gate clean, markdown-docs gate clean. The MCP regression test runs the callback on its own thread with a deadline: under the old behaviour it parks forever, and a test that hangs the suite is worse than one that fails. Co-Authored-By: Claude Opus 5 (1M context) --- .../mcp/MCPLoopbackSocketTransport.java | 50 ++++++- .../impl/android/AndroidSecureStorage.java | 136 +++++++++++++----- .../impl/javase/JavaSESecureStorage.java | Bin 8027 -> 9537 bytes .../mcp/MCPLoopbackSocketTransportTest.java | 67 +++++++++ quality-report.md | 6 +- 5 files changed, 215 insertions(+), 44 deletions(-) diff --git a/CodenameOne/src/com/codename1/mcp/MCPLoopbackSocketTransport.java b/CodenameOne/src/com/codename1/mcp/MCPLoopbackSocketTransport.java index c0e6e22fd15..2f296b82105 100644 --- a/CodenameOne/src/com/codename1/mcp/MCPLoopbackSocketTransport.java +++ b/CodenameOne/src/com/codename1/mcp/MCPLoopbackSocketTransport.java @@ -154,6 +154,18 @@ public void open() throws IOException { } } + /// Test hook: makes this transport the one a freshly constructed [Connection] binds + /// to, without binding a real socket. + /// + /// The binding is what a test needs to reach -- `open()` requires loopback server + /// sockets, which not every environment running these tests has -- and the field it + /// sets is the same one `open()` sets. + static void setActiveForTesting(MCPLoopbackSocketTransport t) { + synchronized (MCPLoopbackSocketTransport.class) { + active = t; + } + } + /// Releases the process-wide registration, but only when it is still this transport's. private void clearActiveIfOurs() { synchronized (MCPLoopbackSocketTransport.class) { @@ -416,6 +428,26 @@ public static final class Connection extends SocketConnection { /// instead of once per retry. Only ever touched from the listener thread. private static String lastReportedError; + /// The transport whose listener accepted THIS connection. + /// + /// Captured when the socket API constructs the callback, which happens on the + /// listener thread as the connection is accepted -- not when the callback body + /// later runs, which is where it used to be read. Between those two moments a + /// transport can close and another can take the process-wide slot, and the + /// already-accepted streams were then attached to the new one: a client of the + /// listener that has stopped replaces the session of the server that has just + /// started. Binding the callback to its own acceptor makes that impossible to + /// express. + private final MCPLoopbackSocketTransport acceptedBy; + + /// Public and no-argument because [Socket#listenLoopback] constructs this + /// reflectively; the binding above is what the constructor exists for. + public Connection() { + synchronized (MCPLoopbackSocketTransport.class) { + acceptedBy = active; + } + } + @Override public void connectionError(int errorCode, String message) { // Without this the failure is silent: startSocketServer returns, the server @@ -438,13 +470,23 @@ public void connectionError(int errorCode, String message) { @Override public void connectionEstablished(InputStream is, OutputStream os) { - MCPLoopbackSocketTransport transport; - synchronized (MCPLoopbackSocketTransport.class) { - transport = active; - } + MCPLoopbackSocketTransport transport = acceptedBy; if (transport == null) { + closeQuietly(is); + closeQuietly(os); return; } + // Closed between accepting this connection and running this callback. The + // streams belong to a session nobody is serving, so they are closed rather + // than handed to whichever transport is open now -- which would let a client + // of the stopped listener take over the new server's session. + synchronized (transport.lock) { + if (transport.closed) { + closeQuietly(is); + closeQuietly(os); + return; + } + } transport.attach(is, os); // Hold this callback thread for the life of the session: the socket API closes // the streams as soon as it returns, and the server reads them from its own diff --git a/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java b/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java index 5186df66ba3..2cd9d8ee3f7 100644 --- a/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java +++ b/Ports/Android/src/com/codename1/impl/android/AndroidSecureStorage.java @@ -51,6 +51,7 @@ import java.security.cert.CertificateException; import javax.crypto.Cipher; +import javax.crypto.IllegalBlockSizeException; import javax.crypto.KeyGenerator; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; @@ -110,7 +111,6 @@ public final class AndroidSecureStorage extends SecureStorage { private KeyStore keyStore; private KeyGenerator keyGenerator; - private Cipher cipher; private boolean keyRevoked; private CancellationSignal cancellationSignal; @@ -537,9 +537,13 @@ private void runAuthenticatedCipher(final String reason, final String accoun return; } } - if (!initCipher(mode, account)) { + Cipher operationCipher = initCipher(mode, account); + if (operationCipher == null) { if (mode == Cipher.ENCRYPT_MODE) { - if (!createKey() || !initCipher(mode, account)) { + if (createKey()) { + operationCipher = initCipher(mode, account); + } + if (operationCipher == null) { failResult(result, BiometricError.UNKNOWN, "Failed to initialise cipher"); return; } @@ -549,15 +553,19 @@ private void runAuthenticatedCipher(final String reason, final String accoun return; } } + // Carried as a parameter from here on. It belongs to this operation and to no + // other, which is what stops a concurrent call from handing its cipher to this + // prompt. if (Build.VERSION.SDK_INT >= 29) { - promptBiometric29(reason, mode, account, result, work); + promptBiometric29(reason, mode, account, result, work, operationCipher); } else { - promptBiometricLegacy(mode, account, result, work); + promptBiometricLegacy(mode, account, operationCipher, result, work); } } private void promptBiometric29(final String reason, final int mode, final String account, - final AsyncResource result, final CipherWork work) { + final AsyncResource result, final CipherWork work, + final Cipher operationCipher) { AndroidBiometrics.runOnUi(new Runnable() { @Override public void run() { @@ -570,7 +578,7 @@ public void run() { AndroidNativeUtil.getActivity(), reason == null ? "Authenticate" : reason, null, null, "Cancel", - cipher, + operationCipher, cs, new BiometricsApi29.CipherAuthCallback() { @Override @@ -591,6 +599,7 @@ public void onError(int errorCode, String errString) { } private void promptBiometricLegacy(final int mode, final String account, + final Cipher operationCipher, final AsyncResource result, final CipherWork work) { AndroidBiometrics.runOnUi(new Runnable() { @Override @@ -608,7 +617,7 @@ public void run() { final CancellationSignal cs = new CancellationSignal(); cancellationSignal = cs; FingerprintManager.CryptoObject crypto = - new FingerprintManager.CryptoObject(cipher); + new FingerprintManager.CryptoObject(operationCipher); fpm.authenticate(crypto, cs, 0, new FingerprintManager.AuthenticationCallback() { int failures; @@ -643,14 +652,24 @@ private void runCipherWork(Cipher authedCipher, CipherWork work, V v = work.run(authedCipher); succeedResult(result, v); } catch (Throwable t) { - // Samsung 8.0.0 quirk: the cipher passes init but doFinal fails - // with a key-invalidated error. Delete the key and let the caller - // retry the entire operation. - // https://issuetracker.google.com/u/0/issues/65578763 - removePermanentlyInvalidatedKey(); - cipher = null; - failResult(result, BiometricError.KEY_REVOKED, - "Cipher operation failed; key invalidated: " + t.getMessage()); + // Only a failure that says the KEY is finished deletes the key. + // + // There is one keystore key behind every biometric account, so this catch + // used to answer a malformed stored value, or an Activity that went away + // mid-prompt, by destroying every other entry in the store -- permanently, + // and while telling the caller its key had been revoked when it had not. + // The Samsung 8.0.0 quirk this was written for is still handled: a cipher + // that initialises and then fails inside doFinal with a keystore error + // underneath is that case, and isKeyInvalidation recognises it. + if (isKeyInvalidation(t)) { + removePermanentlyInvalidatedKey(); + failResult(result, BiometricError.KEY_REVOKED, + "Cipher operation failed; key invalidated: " + t.getMessage()); + } else { + Log.e(t); + failResult(result, BiometricError.UNKNOWN, + "Cipher operation failed: " + t.getMessage()); + } } } @@ -762,58 +781,101 @@ private SecretKey getSecretKey() { return null; } - private Cipher cipher() { - if (cipher == null) { - try { - cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES - + "/" + KeyProperties.BLOCK_MODE_CBC - + "/" + KeyProperties.ENCRYPTION_PADDING_PKCS7); - } catch (NoSuchAlgorithmException e) { - throw new RuntimeException("Cipher init failed", e); - } catch (NoSuchPaddingException e) { - throw new RuntimeException("Cipher init failed", e); - } + /** + * A NEW cipher every time, never a shared field. + * + *

The prompt is raised from a UI runnable, so an operation is in flight from the + * moment it initialises its cipher until that runnable runs. With one instance field, + * a second {@code set()} or {@code get()} starting in that window re-initialised the + * same object and the first prompt was handed the second operation's cipher -- wrong + * mode, or the wrong account's IV. The work then failed, and the failure handler read + * that as an invalidated key and deleted the one key every biometric entry shares.

+ */ + private Cipher newCipher() { + try { + return Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + + "/" + KeyProperties.BLOCK_MODE_CBC + + "/" + KeyProperties.ENCRYPTION_PADDING_PKCS7); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException("Cipher init failed", e); + } catch (NoSuchPaddingException e) { + throw new RuntimeException("Cipher init failed", e); } - return cipher; } - private boolean initCipher(int mode, String account) { + /** The initialised cipher for this one operation, or null if it could not be made. */ + private Cipher initCipher(int mode, String account) { try { SecretKey key = getSecretKey(); if (key == null) { - return false; + return null; } + Cipher c = newCipher(); if (mode == Cipher.ENCRYPT_MODE) { - cipher().init(mode, key); + c.init(mode, key); } else { SharedPreferences sp = AndroidNativeUtil.getActivity() .getApplicationContext() .getSharedPreferences(PREFS, Context.MODE_PRIVATE); byte[] iv = Base64.decode(sp.getString("iv_" + account, ""), Base64.DEFAULT); - cipher().init(mode, key, new IvParameterSpec(iv)); + c.init(mode, key, new IvParameterSpec(iv)); } - return true; + return c; } catch (KeyPermanentlyInvalidatedException e) { removePermanentlyInvalidatedKey(); - return false; + return null; } catch (InvalidKeyException e) { Log.e(e); - return false; + return null; } catch (InvalidAlgorithmParameterException e) { Log.e(e); - return false; + return null; } } private void removePermanentlyInvalidatedKey() { try { keyStore().deleteEntry(KEY_ID); - cipher = null; } catch (KeyStoreException e) { Log.e(e); } } + /** + * Whether a failure means the keystore key is gone, as opposed to this one operation + * having failed. + * + *

The distinction is the whole point. There is ONE key behind every biometric + * account, so deleting it on any failure -- a malformed stored value, an Activity that + * went away mid-prompt, a null passed into the work -- made every other entry + * permanently unreadable, and told the caller its key had been revoked when it had + * not. Only two shapes say the key itself is finished: the exception Android raises + * for it, and the Samsung 8.0.0 quirk where a cipher initialises and then fails inside + * doFinal with a keystore error underneath.

+ * + *

https://issuetracker.google.com/u/0/issues/65578763

+ */ + private static boolean isKeyInvalidation(Throwable t) { + // Bounded rather than while(cause != null): a self-referential cause is rare and + // a hang inside a failure handler is worse than a missed classification. + Throwable c = t; + for (int depth = 0; c != null && depth < 8; depth++) { + if (c instanceof KeyPermanentlyInvalidatedException) { + return true; + } + if (c instanceof IllegalBlockSizeException + && c.getCause() instanceof KeyStoreException) { + return true; + } + Throwable next = c.getCause(); + if (next == c) { + break; + } + c = next; + } + return false; + } + /** Lambda-stand-in for Java 5 source level: cipher op that may throw. */ private interface CipherWork { V run(Cipher c) throws Exception; diff --git a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSESecureStorage.java b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSESecureStorage.java index 608602e72d7751c6581b80cb57cfebd4a4c34eea..2fca4a4afe04b82308b445502a4d7b367c0d1506 100644 GIT binary patch delta 1446 zcmZux%Zk)M6h#*zKERdYjE~E#dd7aZ)^QLY8x>Z({(>ChvRkLVu} z{DS!o!L^^^xs~)tgMp;d>D0OBo_kJy9i6@Uakkq|4i2u690&A*gIRKQe#p%?vXZtht<&=uTQuj?=pb4D#jr>pEgfg z<6=@y^O(Lp)xKbQrIzaW<$E8}c+TmVbshLP`Jm=J*#EwP)`3+5SA)_KY%C|#5E8yl z6jx4{!8>!RlJRax(Nk)0me^KATBwuULCG!6Oo)lJvi2O?I}{DR2lP&bnax;OZos``h%4orVDQ3H*%~$a5do-n1x9whPv+W2n)1vC#z0U| zD)}XcCU^jV!#OtY1A6LssRh&fz`4AZHZZuy)Rab-jNND%lA1^rhlO&TkWxH*MNKWH z{h`BtE5^%Zg|{>j6=#tXL=-u=S>qMKv^n@HY{nNqz<10e+Qpe=ypWPc5+sTCXK0Vy)!lB4w zfI(S0<$y+47)DQEW<)&+2`Q~vq9>{a)pQ!~6}|TZrK6pdmgyVVnx9N7*a!f53q&B$W0>G}_A|Ay?#Q;C*hwIo_d|d?k5HT5OKE zX63TBTd=KA24M?l%hzXVAQ$kEj)*{j9M&n&i)Eh^Hcp0 z2kfF%9T_Pw`9srf#^}UrZD%%^rH_Ni((7UVt3dK6j1LSK!_N3ZP|?i$G_NHl_$gvg n;d4L|3;=Z4);N$gssC7ZqoV7wKFAipvOc?e`{(U@&o2B0LS)xR delta 47 zcmX@;b=z*kL1~eKqRg_yl2nD_%Dm)^qWrwfs??Op)8q^{zn1^R$fB*F#IX61ay%md DThe callback used to resolve the process-wide active transport at execution + * time. A transport that accepted a client just before closing therefore handed those + * already-accepted streams to whatever opened next -- a client of the stopped + * listener taking over the new server's session, which is the one thing the + * single-agent rule exists to prevent.

+ */ + @Test + void aConnectionAcceptedByOneTransportIsNotHandedToTheNext() throws Exception { + MCPLoopbackSocketTransport accepting = new MCPLoopbackSocketTransport(47811); + MCPLoopbackSocketTransport.setActiveForTesting(accepting); + // Constructed while `accepting` is the listener, which is when the socket API + // creates it: as the connection is accepted. + MCPLoopbackSocketTransport.Connection connection = + new MCPLoopbackSocketTransport.Connection(); + + accepting.close(); + transport = new MCPLoopbackSocketTransport(47811); + MCPLoopbackSocketTransport.setActiveForTesting(transport); + // The replacement's own client, already talking. + transport.attach(new ByteArrayInputStream("{\"id\":42}\n".getBytes("UTF-8")), + new ByteArrayOutputStream()); + + final boolean[] orphanClosed = {false}; + final InputStream orphanIn = new InputStream() { + private final byte[] data = "{\"id\":99}\n".getBytes("UTF-8"); + private int pos; + + @Override + public int read() { + return pos < data.length ? data[pos++] & 0xff : -1; + } + + @Override + public void close() { + orphanClosed[0] = true; + } + }; + // On its own thread with a deadline. The callback deliberately parks for the life + // of a session it accepts, so under the old behaviour -- where it adopted whatever + // transport was open -- this call never returns. A regression has to fail the test + // rather than hang the suite. + final MCPLoopbackSocketTransport.Connection handoff = connection; + Thread callback = new Thread(new Runnable() { + public void run() { + handoff.connectionEstablished(orphanIn, new ByteArrayOutputStream()); + } + }, "mcp-orphan-connection"); + callback.setDaemon(true); + callback.start(); + callback.join(5000L); + assertFalse(callback.isAlive(), + "a connection whose listener has closed is released, not parked as if it " + + "were serving a session"); + + assertEquals("{\"id\":42}", transport.readMessage(), + "the replacement must still be serving ITS client -- an attach from the " + + "previous listener's connection would have dropped it"); + assertTrue(orphanClosed[0], + "and the orphaned connection is closed rather than left half-attached"); + MCPLoopbackSocketTransport.setActiveForTesting(null); + } } diff --git a/quality-report.md b/quality-report.md index 0bb6725ed04..46cb50025cf 100644 --- a/quality-report.md +++ b/quality-report.md @@ -1,8 +1,8 @@ ## ✅ Continuous Quality Report ### Test & Coverage -- ✅ **Tests:** 4743 total, 0 failed, 0 skipped -- 📊 **Line coverage:** 58.86% +- ✅ **Tests:** 4744 total, 0 failed, 0 skipped +- 📊 **Line coverage:** 58.91% - **Lowest covered classes** - `com.codename1.gaming.level.GameSceneView` – 0.00% - `com.codename1.crash.CrashProtection` – 0.00% @@ -11,9 +11,9 @@ - `com.codename1.crash.CrashReportPayload` – 0.00% - `com.codename1.vr.VRView` – 0.00% - `com.codename1.appreview.RatingDialog` – 0.00% - - `com.codename1.security.shield.ShieldSignals` – 0.00% - `com.codename1.calendar.DefaultCalendarHttpTransport` – 0.00% - `com.codename1.security.Secrets` – 0.00% + - `com.codename1.calendar.OidcCalendarTokenProvider` – 0.00% ### Static Analysis - **SpotBugs** From 97e7ae9d5c781a6d4005fe4574466913fa708a5e Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Mon, 3 Aug 2026 10:18:12 +0700 Subject: [PATCH 95/96] Settle a stale status at delivery, and reject header names that are not tokens Holding the class monitor across callSerially was the wrong fix and would have been a worse bug than the one it closed: callSerially runs the task INLINE before the EDT is up, so it would have run application listeners under that monitor -- and a listener that touches the shield, or waits on a thread that does, deadlocks against attach(), which waits on the same monitor for initialization. The transition is written under the lock and the dispatch is queued outside it again; staleness is settled where ShieldSignals settles it, by a dispatch that drops itself when it no longer describes the current status. The test moved with the invariant. It no longer asserts that a second transition cannot be stored -- that is exactly what is now allowed -- but that a superseded status is never announced and that what a listener is left holding is what getStatus reports. It fails on the unfixed code with the listener ending on serviceUnavailable. Header names are validated as HTTP field tokens before anything else is decided about them. Screening only for CR and LF let "Sec-WebSocket-Extensions " -- one trailing space -- past the reserved-name comparison, and a lenient server trims that and negotiates permessage-deflate. No reader here looks at RSV1 or inflates anything, so every frame after that arrives as garbage. Rejected rather than trimmed: a caller who wrote a trailing space meant one header and the server would read another, and repairing that quietly is how the two ends stop agreeing about what was sent. And the simulator's secure storage flushes before reporting success, on both the write and the removal. Preferences writes back on its own schedule, and the simulator is killed abruptly all the time -- by the run button, by the IDE -- so "stored" meant "in memory" and a cleared credential could come back. Core suite 4746 green, SpotBugs 0, PMD and markdown-docs gates clean. Co-Authored-By: Claude Opus 5 (1M context) --- .../src/com/codename1/impl/WebSocketImpl.java | 37 +++++++- .../codename1/security/shield/AppShield.java | 44 +++++++--- .../impl/javase/JavaSESecureStorage.java | 15 ++++ .../java/com/codename1/io/WebSocketTest.java | 42 +++++++++ .../security/shield/ShieldInitOrderTest.java | 88 +++++++++++-------- quality-report.md | 2 +- 6 files changed, 176 insertions(+), 52 deletions(-) diff --git a/CodenameOne/src/com/codename1/impl/WebSocketImpl.java b/CodenameOne/src/com/codename1/impl/WebSocketImpl.java index 64b3104a26f..49a70903bc0 100644 --- a/CodenameOne/src/com/codename1/impl/WebSocketImpl.java +++ b/CodenameOne/src/com/codename1/impl/WebSocketImpl.java @@ -120,16 +120,51 @@ protected final void appendRequestHeaders(StringBuilder req) { String[] pair = (String[]) requestHeaders.get(key); String name = pair[0]; String value = pair[1]; + // The NAME is checked for being a legal field-name token before anything is + // decided about it. Screening only for CR and LF let "Sec-WebSocket-Extensions " + // -- one trailing space -- past the reserved-name comparison, and a lenient + // server trims that and negotiates the extension anyway. These readers do not + // process RSV1 or inflate payloads, so a compressed frame arrives as garbage: + // the reserved list exists precisely to stop that being negotiable, and a + // comparison that any non-token character walks around is not a list. + if (!isFieldNameToken(name)) { + continue; + } if (isReservedHandshakeHeader(name)) { continue; } - if (containsCrLf(name) || containsCrLf(value)) { + if (containsCrLf(value)) { continue; } req.append(name).append(": ").append(value).append("\r\n"); } } + /// Whether this is a legal HTTP field name -- RFC 9110 token, so no spaces, no + /// separators, nothing outside printable ASCII. + /// + /// Rejecting rather than trimming: a caller that wrote a trailing space meant one + /// header and a lenient server would read another, and quietly repairing the + /// difference is how the two ends stop agreeing about what was sent. + private static boolean isFieldNameToken(String name) { + if (name == null || name.length() == 0) { + return false; + } + for (int i = 0; i < name.length(); i++) { + char c = name.charAt(i); + if (c <= 0x20 || c >= 0x7f) { + return false; + } + if (c == '(' || c == ')' || c == '<' || c == '>' || c == '@' || c == ',' + || c == ';' || c == ':' || c == '\\' || c == '"' || c == '/' + || c == '[' || c == ']' || c == '?' || c == '=' || c == '{' + || c == '}') { + return false; + } + } + return true; + } + private static boolean isReservedHandshakeHeader(String name) { // ASCII folding, not toLowerCase(): under the Turkish and Azerbaijani locales an // uppercase I folds to a dotless letter outside ASCII, so a header spelled diff --git a/CodenameOne/src/com/codename1/security/shield/AppShield.java b/CodenameOne/src/com/codename1/security/shield/AppShield.java index e978a3694e4..9dc2d31a1f2 100644 --- a/CodenameOne/src/com/codename1/security/shield/AppShield.java +++ b/CodenameOne/src/com/codename1/security/shield/AppShield.java @@ -847,26 +847,26 @@ private static void setStatus(ShieldStatus status) { if (status == null) { return; } - // The transition and its dispatch happen under one lock, so the order listeners - // are told in is the order the transitions happened in. + // The transition happens under the lock; the dispatch is queued outside it. // - // Storing the status and enqueueing the notification as two steps let two network - // threads interleave: A stores, B stores and enqueues, A enqueues. Listeners then - // finished on A while getStatus() already answered B -- a UI showing "service - // down" for a shield that is fine, or the reverse, with nothing further to correct - // it because the next transition is the one after that. + // Both halves matter. Storing the status and enqueueing its notification as two + // unsynchronized steps let two network threads interleave -- A stores, B stores + // and enqueues, A enqueues -- and listeners then finished on A while getStatus() + // already answered B, with nothing later to correct them. But holding the monitor + // across callSerially is not the fix: callSerially runs the task INLINE when the + // EDT is not up, so it would run application listeners under this class's monitor, + // and a listener that touches the shield -- or waits on a thread that does -- + // deadlocks against attach(), which waits on the same monitor for initialization. // - // Holding the monitor across callSerially is safe and deliberate: it only appends - // to the EDT queue, and nothing on that path calls back into this class. The - // alternative -- sequence numbers and dropped stale dispatches -- gives listeners - // the right final state but silently swallows intermediate ones, and a listener - // that logs or counts transitions has every reason to want them all. + // So staleness is settled at delivery instead, exactly as ShieldSignals does it: a + // dispatch that no longer describes the current status drops itself. A superseded + // status was already wrong when it was queued. + ShieldListener[] copy; synchronized (AppShield.class) { if (status.equals(lastStatus)) { return; } lastStatus = status; - ShieldListener[] copy; synchronized (listeners) { if (listeners.isEmpty()) { return; @@ -874,7 +874,17 @@ private static void setStatus(ShieldStatus status) { copy = new ShieldListener[listeners.size()]; listeners.copyInto(copy); } - Display.getInstance().callSerially(new StatusDispatch(copy, status)); + } + Display.getInstance().callSerially(new StatusDispatch(copy, status)); + } + + /// Whether this is still the status the shield holds. + /// + /// Read under the same monitor the transition is written under, so a dispatch either + /// sees the value it was queued for or a newer one -- never a half-written state. + static boolean isCurrentStatus(ShieldStatus status) { + synchronized (AppShield.class) { + return status.equals(lastStatus); } } @@ -941,6 +951,12 @@ private static final class StatusDispatch implements Runnable { @Override public void run() { + // Checked here rather than at enqueue time, because what matters is the state + // at delivery: a transition superseded between being queued and arriving would + // otherwise leave listeners holding a status the shield itself no longer has. + if (!isCurrentStatus(status)) { + return; + } for (ShieldListener target : targets) { target.statusChanged(status); } diff --git a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSESecureStorage.java b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSESecureStorage.java index 2fca4a4afe0..a8b1ec1f161 100644 --- a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSESecureStorage.java +++ b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSESecureStorage.java @@ -138,6 +138,13 @@ public boolean set(String account, String value) { Base64.Encoder b64 = Base64.getEncoder(); plainPrefs.put(VALUE_PREFIX + account, b64.encodeToString(c.getIV()) + ":" + b64.encodeToString(enc)); + // flush(), and its outcome is this method's answer. Preferences writes back + // on its own schedule, so returning true said the secret was stored while it + // was still only in memory -- and the simulator is killed abruptly all the + // time, by the run button and by the IDE. The same reasoning as the Android + // tier committing rather than applying: a write that reports success has to + // have happened. + plainPrefs.flush(); return true; } catch (Exception e) { Log.e(e); @@ -177,6 +184,14 @@ public boolean remove(String account) { return false; } plainPrefs.remove(VALUE_PREFIX + account); + try { + // Same for the removal, and it matters more: this is the credential a logout + // clears, so an unflushed deletion is one that comes back on the next launch. + plainPrefs.flush(); + } catch (BackingStoreException e) { + Log.e(e); + return false; + } return true; } diff --git a/maven/core-unittests/src/test/java/com/codename1/io/WebSocketTest.java b/maven/core-unittests/src/test/java/com/codename1/io/WebSocketTest.java index ad57258fdc7..5d7a63f47e6 100644 --- a/maven/core-unittests/src/test/java/com/codename1/io/WebSocketTest.java +++ b/maven/core-unittests/src/test/java/com/codename1/io/WebSocketTest.java @@ -239,6 +239,48 @@ void extensionNegotiationIsNotEmittedInTheHandshake() { "and an ordinary header still goes out: " + emitted); } + /** + * A reserved header cannot be smuggled past the list by adding a space. + * + *

The comparison screened names only for CR and LF, so + * {@code "Sec-WebSocket-Extensions "} did not match the reserved name and was + * emitted. Lenient servers trim that and negotiate the extension -- and these readers + * neither look at RSV1 nor inflate anything, so every compressed frame afterwards is + * garbage. A list that any non-token character walks around is not a list.

+ */ + @Test + void aReservedHeaderWithTrailingSpaceIsNotEmitted() { + MockImpl impl = new MockImpl("wss://example.com/socket"); + impl.setRequestHeader("Sec-WebSocket-Extensions ", "permessage-deflate"); + impl.setRequestHeader("Sec-WebSocket-Extensions\t", "permessage-deflate"); + impl.setRequestHeader("X-Fine", "yes"); + + String emitted = impl.testHandshakeHeaders(); + + assertFalse(emitted.toLowerCase().contains("permessage-deflate"), + "a compression extension these readers cannot decode must not reach the " + + "wire, whatever the name was padded with: " + emitted); + assertTrue(emitted.contains("X-Fine: yes"), + "and an ordinary header is unaffected: " + emitted); + } + + /** Names that are not field tokens are refused rather than repaired. */ + @Test + void aHeaderNameThatIsNotATokenIsRefused() { + MockImpl impl = new MockImpl("wss://example.com/socket"); + impl.setRequestHeader("X Bad", "1"); + impl.setRequestHeader("X:Bad", "2"); + impl.setRequestHeader("X\u00e9", "3"); + impl.setRequestHeader("X-Good", "4"); + + String emitted = impl.testHandshakeHeaders(); + + assertFalse(emitted.contains("X Bad"), emitted); + assertFalse(emitted.contains("X:Bad"), emitted); + assertFalse(emitted.contains("3"), emitted); + assertTrue(emitted.contains("X-Good: 4"), emitted); + } + private final class WebSocketingImpl extends TestCodenameOneImplementation { boolean supported = true; diff --git a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldInitOrderTest.java b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldInitOrderTest.java index e8a396415ea..04a2f8e3c5a 100644 --- a/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldInitOrderTest.java +++ b/maven/core-unittests/src/test/java/com/codename1/security/shield/ShieldInitOrderTest.java @@ -488,78 +488,93 @@ public void shutdown() { } /** - * A status transition and the notification for it are one step, not two. + * Listeners never finish on a status the shield has already replaced. * - *

Storing the status and enqueueing its callback used to be separate, so two - * network threads could interleave as: A stores, B stores, B enqueues, A enqueues. - * Listeners then finished on A while {@link AppShield#getStatus()} already answered - * B -- a UI left saying "service down" for a shield that is fine, with nothing to - * correct it until the next transition.

+ *

Two network threads could interleave as: A stores, B stores and enqueues, A + * enqueues -- and listeners then held A while {@link AppShield#getStatus()} answered + * B, with nothing later guaranteed to correct them.

* - *

Staged rather than stressed. The window is between two statements inside one - * method, and a loop racing two threads at it did not reproduce the old behaviour - * here even once in three runs -- a test that cannot fail on the unfixed code is not - * a regression test. Holding the listener monitor stops the first transition exactly - * where the enqueue happens, which makes the question "has the second one stored its - * status yet" answerable rather than probable.

+ *

Fixed at delivery rather than by holding the lock across the enqueue, because + * {@code Display.callSerially} runs the task inline before the EDT is up: dispatching + * under the monitor would run application listeners while holding it, and a listener + * that touches the shield -- or waits on a thread that does -- deadlocks against + * {@code attach()}, which waits on that same monitor. So the dispatch checks on + * arrival whether it still describes the current status.

+ * + *

Staged through the listener monitor, which the transition takes after storing + * the status and before queueing anything. That parks the first transition where its + * notification is prepared, so the second overtakes it deterministically -- racing two + * threads at a window this narrow is not something a test can rely on.

*/ @Test - void aStatusIsNotStoredWhileAnEarlierOneIsStillBeingDispatched() throws Exception { - AppShield.addListener(new RecordingListener()); + void aSupersededStatusIsNeverAnnounced() throws Exception { + final java.util.List seen = + java.util.Collections.synchronizedList(new java.util.ArrayList()); + AppShield.addListener(new RecordingListener(seen)); java.lang.reflect.Field listenersField = AppShield.class.getDeclaredField("listeners"); listenersField.setAccessible(true); Object listenerLock = listenersField.get(null); - java.lang.reflect.Field statusField = - AppShield.class.getDeclaredField("lastStatus"); - statusField.setAccessible(true); - final CountDownLatch firstEntered = new CountDownLatch(1); - final CountDownLatch secondStarted = new CountDownLatch(1); + final CountDownLatch firstIn = new CountDownLatch(1); + final CountDownLatch secondIn = new CountDownLatch(1); Thread first; Thread second; synchronized (listenerLock) { first = new Thread(new Runnable() { public void run() { - firstEntered.countDown(); + firstIn.countDown(); AppShield.setStatusForTesting(ShieldStatus.SERVICE_DOWN); } }, "shield-status-first"); first.setDaemon(true); first.start(); - assertTrue(firstEntered.await(GENEROUS_TIMEOUT_MS, TimeUnit.MILLISECONDS)); - // It is now inside setStatus, blocked where the notification is enqueued. + assertTrue(firstIn.await(GENEROUS_TIMEOUT_MS, TimeUnit.MILLISECONDS)); Thread.sleep(BLOCKED_OBSERVATION_MS); second = new Thread(new Runnable() { public void run() { - secondStarted.countDown(); + secondIn.countDown(); AppShield.setStatusForTesting(ShieldStatus.OK); } }, "shield-status-second"); second.setDaemon(true); second.start(); - assertTrue(secondStarted.await(GENEROUS_TIMEOUT_MS, TimeUnit.MILLISECONDS)); + assertTrue(secondIn.await(GENEROUS_TIMEOUT_MS, TimeUnit.MILLISECONDS)); Thread.sleep(BLOCKED_OBSERVATION_MS); - - // Read directly rather than through getStatus(), which would block on the - // monitor the first thread is holding and deadlock this test. - assertEquals(ShieldStatus.SERVICE_DOWN, statusField.get(null), - "the second transition must not be stored while the first one has " - + "not finished announcing itself -- that is the reordering: " - + "listeners end on the older status while getStatus reports the " - + "newer one"); } - first.join(GENEROUS_TIMEOUT_MS); second.join(GENEROUS_TIMEOUT_MS); - assertEquals(ShieldStatus.OK, AppShield.getStatus(), - "and both transitions still happen, in order"); + + final CountDownLatch drained = new CountDownLatch(1); + com.codename1.ui.Display.getInstance().callSerially(new Runnable() { + public void run() { + drained.countDown(); + } + }); + assertTrue(drained.await(GENEROUS_TIMEOUT_MS, TimeUnit.MILLISECONDS), + "the dispatch queue has to drain for this to mean anything"); + + assertEquals(ShieldStatus.OK, AppShield.getStatus()); + synchronized (seen) { + assertFalse(seen.isEmpty(), "the surviving transition is announced"); + assertEquals(ShieldStatus.OK, seen.get(seen.size() - 1), + "and what a listener is left holding is what getStatus reports: " + seen); + assertFalse(seen.contains(ShieldStatus.SERVICE_DOWN), + "a superseded status was already wrong when it was queued, so it is " + + "not announced at all: " + seen); + } } - /** Records nothing; its presence is what makes setStatus reach the dispatch. */ + /** Records what listeners were actually told, in order. */ private static final class RecordingListener implements ShieldListener { + private final java.util.List seen; + + RecordingListener(java.util.List seen) { + this.seen = seen; + } + public void signalRaised(ShieldSignal signal) { } @@ -567,6 +582,7 @@ public void tokenRefreshed(ShieldToken token) { } public void statusChanged(ShieldStatus status) { + seen.add(status); } } } diff --git a/quality-report.md b/quality-report.md index 46cb50025cf..e636738277b 100644 --- a/quality-report.md +++ b/quality-report.md @@ -1,7 +1,7 @@ ## ✅ Continuous Quality Report ### Test & Coverage -- ✅ **Tests:** 4744 total, 0 failed, 0 skipped +- ✅ **Tests:** 4746 total, 0 failed, 0 skipped - 📊 **Line coverage:** 58.91% - **Lowest covered classes** - `com.codename1.gaming.level.GameSceneView` – 0.00% From 6e20107587f000e42912b3e767fc12c2bd80e76b Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Mon, 3 Aug 2026 10:24:37 +0700 Subject: [PATCH 96/96] Import BackingStoreException in the simulator secure storage The flush() I added references it and the import never landed, so every job that compiles the JavaSE port failed. Verified by building the port rather than by reading: the class compiles now. Co-Authored-By: Claude Opus 5 (1M context) --- .../src/com/codename1/impl/javase/JavaSESecureStorage.java | 1 + 1 file changed, 1 insertion(+) diff --git a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSESecureStorage.java b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSESecureStorage.java index a8b1ec1f161..bd4f4404246 100644 --- a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSESecureStorage.java +++ b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSESecureStorage.java @@ -33,6 +33,7 @@ import java.security.SecureRandom; import java.util.Base64; +import java.util.prefs.BackingStoreException; import javax.crypto.Cipher; import javax.crypto.SecretKey;