From a0d9505134fd046aefa3296285fce1b124163937 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 1 Dec 2026 14:34:03 +0000 Subject: [PATCH 1/2] iOS: retain the sign-in sheet's callback URL in CN1OidcBrowser.m CN1OidcBrowser.m was written for ARC, but the port compiles with CLANG_ENABLE_OBJC_ARC = NO. ASWebAuthenticationSession's completion handler stored [callbackURL absoluteString] -- autoreleased into the main thread's pool -- in a __block variable and signalled the waiting thread, which then called fromNSString on it after the pool had drained. That use-after-free raised EXC_BAD_ACCESS, which ParparVM's signal handler surfaces as a NullPointerException with no message, so SystemBrowser.authenticate and OidcClient.authorize failed on device after the person approved, with OidcException transport_error "Native sign-in sheet failed: null". Under manual reference counting: - the callback string is copied and the error retained on the main thread, and both are released by the waiting thread once it has read them; - the session and its presentation context, held at +1 from -alloc, are autoreleased when the flow clears them (inside the session's own completion handler, so the session outlives the handler's return) instead of being leaked. Found in a TestFlight build of an app that signs in with OidcClient: every sign-in failed with that transport_error until the generated project was patched the same way. --- Ports/iOSPort/nativeSources/CN1OidcBrowser.m | 47 ++++++++++++++------ 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/Ports/iOSPort/nativeSources/CN1OidcBrowser.m b/Ports/iOSPort/nativeSources/CN1OidcBrowser.m index dc0f06c8ea5..0b31c26646e 100644 --- a/Ports/iOSPort/nativeSources/CN1OidcBrowser.m +++ b/Ports/iOSPort/nativeSources/CN1OidcBrowser.m @@ -94,11 +94,22 @@ - (ASPresentationAnchor)presentationAnchorForWebAuthenticationSession:(ASWebAuth #endif // !TARGET_OS_WATCH // Single static slot to keep the session strongly referenced for the -// duration of a flow (ARC would otherwise deallocate it the moment the -// dispatch_async block returned). +// duration of a flow. The port compiles without ARC (CLANG_ENABLE_OBJC_ARC = +// NO), so these hold the +1 reference from -alloc, and cn1OidcClearSession() +// gives it up. static id g_cn1OidcCurrentSession = nil; static id g_cn1OidcCurrentContext = nil; +// Drops the flow's session and context. Autoreleased rather than released: +// this runs inside the session's own completion handler, and the session must +// outlive the handler's return. +static void cn1OidcClearSession(void) { + [g_cn1OidcCurrentSession autorelease]; + [g_cn1OidcCurrentContext autorelease]; + g_cn1OidcCurrentSession = nil; + g_cn1OidcCurrentContext = nil; +} + JAVA_BOOLEAN com_codename1_impl_ios_IOSNative_oidcSystemBrowserSupported___R_boolean( CN1_THREAD_STATE_MULTI_ARG JAVA_OBJECT me) { if (@available(iOS 12.0, watchOS 6.2, *)) { @@ -133,13 +144,17 @@ JAVA_OBJECT com_codename1_impl_ios_IOSNative_oidcStartAuthorization___java_lang_ [[ASWebAuthenticationSession alloc] initWithURL:url callbackURLScheme:redirectScheme completionHandler:^(NSURL * _Nullable callbackURL, NSError * _Nullable error) { + // Retained: absoluteString is autoreleased into the main + // thread's pool, which drains before the waiting thread reads + // it. Without the copy that read was a use-after-free, which + // ParparVM's signal handler surfaced as a NullPointerException + // (OidcException transport_error, "null"). if (callbackURL) { - result = [callbackURL absoluteString]; + result = [[callbackURL absoluteString] copy]; } else if (error) { - failure = error; + failure = [error retain]; } - g_cn1OidcCurrentSession = nil; - g_cn1OidcCurrentContext = nil; + cn1OidcClearSession(); dispatch_semaphore_signal(sem); }]; @@ -154,11 +169,10 @@ JAVA_OBJECT com_codename1_impl_ios_IOSNative_oidcStartAuthorization___java_lang_ #endif g_cn1OidcCurrentSession = session; if (![session start]) { - failure = [NSError errorWithDomain:@"com.codename1.io.oidc" - code:-1 - userInfo:@{NSLocalizedDescriptionKey: @"ASWebAuthenticationSession refused to start"}]; - g_cn1OidcCurrentSession = nil; - g_cn1OidcCurrentContext = nil; + failure = [[NSError errorWithDomain:@"com.codename1.io.oidc" + code:-1 + userInfo:@{NSLocalizedDescriptionKey: @"ASWebAuthenticationSession refused to start"}] retain]; + cn1OidcClearSession(); dispatch_semaphore_signal(sem); } }); @@ -167,10 +181,15 @@ JAVA_OBJECT com_codename1_impl_ios_IOSNative_oidcStartAuthorization___java_lang_ // sheet is ever held open indefinitely. dispatch_semaphore_wait(sem, dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3600 * NSEC_PER_SEC))); - if (failure != nil || result == nil) { - return JAVA_NULL; + // Both were retained on the main thread (above); release them here, once the + // flow is over. dispatch_semaphore_signal/wait order the writes before this. + JAVA_OBJECT answer = JAVA_NULL; + if (failure == nil && result != nil) { + answer = fromNSString(CN1_THREAD_GET_STATE_PASS_ARG result); } - return fromNSString(CN1_THREAD_GET_STATE_PASS_ARG result); + [result release]; + [failure release]; + return answer; } #else From ad72cfd95686fbad30ef25b1421988f673cd5554 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 1 Dec 2026 15:50:23 +0000 Subject: [PATCH 2/2] iOS: keep CN1OidcBrowser.m valid under ARC as well The iOS native-source lint compiles with -fobjc-arc, where explicit retain/release/autorelease are errors. Guard them with __has_feature(objc_arc), so the file is correct both in the product build (ARC off) and under ARC. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SZMcbmatyXu26VSMeHqWHJ --- Ports/iOSPort/nativeSources/CN1OidcBrowser.m | 33 ++++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/Ports/iOSPort/nativeSources/CN1OidcBrowser.m b/Ports/iOSPort/nativeSources/CN1OidcBrowser.m index 0b31c26646e..31e46a0f6c2 100644 --- a/Ports/iOSPort/nativeSources/CN1OidcBrowser.m +++ b/Ports/iOSPort/nativeSources/CN1OidcBrowser.m @@ -95,17 +95,20 @@ - (ASPresentationAnchor)presentationAnchorForWebAuthenticationSession:(ASWebAuth // Single static slot to keep the session strongly referenced for the // duration of a flow. The port compiles without ARC (CLANG_ENABLE_OBJC_ARC = -// NO), so these hold the +1 reference from -alloc, and cn1OidcClearSession() -// gives it up. +// NO), so there these hold the +1 reference from -alloc, and +// cn1OidcClearSession() gives it up. Under ARC (the iOS native-source lint +// compiles with -fobjc-arc) the slots are strong and assigning nil is enough. static id g_cn1OidcCurrentSession = nil; static id g_cn1OidcCurrentContext = nil; -// Drops the flow's session and context. Autoreleased rather than released: -// this runs inside the session's own completion handler, and the session must -// outlive the handler's return. +// Drops the flow's session and context. Without ARC they are autoreleased +// rather than released: this runs inside the session's own completion +// handler, and the session must outlive the handler's return. static void cn1OidcClearSession(void) { +#if !__has_feature(objc_arc) [g_cn1OidcCurrentSession autorelease]; [g_cn1OidcCurrentContext autorelease]; +#endif g_cn1OidcCurrentSession = nil; g_cn1OidcCurrentContext = nil; } @@ -152,7 +155,11 @@ JAVA_OBJECT com_codename1_impl_ios_IOSNative_oidcStartAuthorization___java_lang_ if (callbackURL) { result = [[callbackURL absoluteString] copy]; } else if (error) { +#if __has_feature(objc_arc) + failure = error; +#else failure = [error retain]; +#endif } cn1OidcClearSession(); dispatch_semaphore_signal(sem); @@ -169,9 +176,12 @@ JAVA_OBJECT com_codename1_impl_ios_IOSNative_oidcStartAuthorization___java_lang_ #endif g_cn1OidcCurrentSession = session; if (![session start]) { - failure = [[NSError errorWithDomain:@"com.codename1.io.oidc" - code:-1 - userInfo:@{NSLocalizedDescriptionKey: @"ASWebAuthenticationSession refused to start"}] retain]; + failure = [NSError errorWithDomain:@"com.codename1.io.oidc" + code:-1 + userInfo:@{NSLocalizedDescriptionKey: @"ASWebAuthenticationSession refused to start"}]; +#if !__has_feature(objc_arc) + [failure retain]; +#endif cn1OidcClearSession(); dispatch_semaphore_signal(sem); } @@ -181,14 +191,17 @@ JAVA_OBJECT com_codename1_impl_ios_IOSNative_oidcStartAuthorization___java_lang_ // sheet is ever held open indefinitely. dispatch_semaphore_wait(sem, dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3600 * NSEC_PER_SEC))); - // Both were retained on the main thread (above); release them here, once the - // flow is over. dispatch_semaphore_signal/wait order the writes before this. + // Without ARC both were retained on the main thread (above); release them + // here, once the flow is over. dispatch_semaphore_signal/wait order the + // writes before this. JAVA_OBJECT answer = JAVA_NULL; if (failure == nil && result != nil) { answer = fromNSString(CN1_THREAD_GET_STATE_PASS_ARG result); } +#if !__has_feature(objc_arc) [result release]; [failure release]; +#endif return answer; }