Skip to content

Throws a specific exception when a certificate is needed but not provided#24544

Open
rolfbjarne wants to merge 5 commits intomainfrom
dev/rolf/dotMorten-detect_missing_cert
Open

Throws a specific exception when a certificate is needed but not provided#24544
rolfbjarne wants to merge 5 commits intomainfrom
dev/rolf/dotMorten-detect_missing_cert

Conversation

@rolfbjarne
Copy link
Copy Markdown
Member

@rolfbjarne rolfbjarne commented Jan 22, 2026

Fixes #21688

This allows the user to detect the specific exception when a certificate is needed and react to it. Exceptions thrown are very similar to what SocketsHttpHandler and other handlers throw, while also following the pattern of other exceptions thrown by NSUrlSessionHandler.

This is a re-creation of #24532 from @dotMorten (due to our CI not being able to build PRs from forks).

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds specific exception handling for scenarios where a server requests a client certificate but none is provided by the application. The implementation throws a structured exception chain (HttpRequestExceptionWebExceptionAuthenticationException) to help developers detect and handle missing certificate scenarios programmatically.

Changes:

  • Modified NSUrlSessionHandler to throw a specific exception when a client certificate is requested but not available
  • Added an AppContext switch to allow disabling the new behavior for backward compatibility
  • Added two test methods to validate optional and required certificate scenarios

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
src/Foundation/NSUrlSessionHandler.cs Adds exception handling logic when client certificate is missing, with AppContext switch for backward compatibility
tests/monotouch-test/System.Net.Http/MessageHandlers.cs Adds two test methods: one for optional certificates (should succeed) and one for required certificates (should throw specific exception)

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

💻 [CI Build #df9f7f9] Tests on macOS arm64 - Mac Sequoia (15) passed 💻

All tests on macOS arm64 - Mac Sequoia (15) passed.

Pipeline on Agent
Hash: df9f7f93197d59ced9f3ff1e790dfa90bc858910 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

💻 [CI Build #df9f7f9] Tests on macOS M1 - Mac Monterey (12) passed 💻

All tests on macOS M1 - Mac Monterey (12) passed.

Pipeline on Agent
Hash: df9f7f93197d59ced9f3ff1e790dfa90bc858910 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

💻 [CI Build #df9f7f9] Tests on macOS X64 - Mac Sonoma (14) passed 💻

All tests on macOS X64 - Mac Sonoma (14) passed.

Pipeline on Agent
Hash: df9f7f93197d59ced9f3ff1e790dfa90bc858910 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

💻 [CI Build #df9f7f9] Tests on macOS M1 - Mac Ventura (13) passed 💻

All tests on macOS M1 - Mac Ventura (13) passed.

Pipeline on Agent
Hash: df9f7f93197d59ced9f3ff1e790dfa90bc858910 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

💻 [CI Build #df9f7f9] Tests on macOS arm64 - Mac Tahoe (26) passed 💻

All tests on macOS arm64 - Mac Tahoe (26) passed.

Pipeline on Agent
Hash: df9f7f93197d59ced9f3ff1e790dfa90bc858910 [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

Morten Nielsen and others added 4 commits March 27, 2026 12:22
Verifies that when the Foundation.NSUrlSessionHandler.NoMissingCertificateHandling
switch is enabled, the specific SecureChannelFailure exception is not thrown.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@rolfbjarne rolfbjarne force-pushed the dev/rolf/dotMorten-detect_missing_cert branch from df9f7f9 to 4e047fd Compare March 27, 2026 14:38
@rolfbjarne rolfbjarne requested a review from Copilot March 27, 2026 14:52
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

WebExceptionStatus.SecureChannelFailure, null));
}
// We will still continue with a null credential, since some services uses optional client certificates and this will still let it succeed
completionHandler (NSUrlSessionAuthChallengeDisposition.PerformDefaultHandling, null!);
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This branch uses PerformDefaultHandling but passes a null credential. Elsewhere in this method, PerformDefaultHandling uses challenge.ProposedCredential; using that here too avoids accidentally dropping a non-null ProposedCredential and matches the existing pattern.

Suggested change
completionHandler (NSUrlSessionAuthChallengeDisposition.PerformDefaultHandling, null!);
completionHandler (NSUrlSessionAuthChallengeDisposition.PerformDefaultHandling, challenge.ProposedCredential);

Copilot uses AI. Check for mistakes.
Comment on lines +821 to +826
listener.SetStateChangedHandler ((state, error) => {
if (state == NWListenerState.Ready)
readyEvent.Set ();
if (state == NWListenerState.Failed)
readyEvent.Set ();
});
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CreateNWTlsListener unblocks the wait on both Ready and Failed, but it doesn’t check which state occurred. If the listener fails to start, this method will still return a listener and subsequent requests can fail in confusing ways/time out. Track the final state/error and throw when the state is Failed (and consider disposing the ManualResetEventSlim).

Copilot uses AI. Check for mistakes.
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

- Use challenge.ProposedCredential instead of null for PerformDefaultHandling.
- Fix grammar: 'services uses' -> 'services use'.
- Track listener error state and throw on Failed instead of silently
  returning a broken listener.
- Dispose ManualResetEventSlim.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ [CI Build #cf677dc] Build passed (Build packages) ✅

Pipeline on Agent
Hash: cf677dce29b4dc6188723072cba6204f75363233 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ [CI Build #cf677dc] Build passed (Build macOS tests) ✅

Pipeline on Agent
Hash: cf677dce29b4dc6188723072cba6204f75363233 [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

🔥 [CI Build #cf677dc] Test results 🔥

Test results

❌ Tests failed on VSTS: test results

1 tests crashed, 6 tests failed, 138 tests passed.

Failures

❌ monotouch tests (macOS)

🔥 Failed catastrophically on VSTS: test results - monotouch_macos (no summary found).

Html Report (VSDrops) Download

❌ monotouch tests (tvOS)

1 tests failed, 10 tests passed.

Failed tests

  • monotouch-test/tvOS - simulator/Release (interpreter): Failed

Html Report (VSDrops) Download

❌ Tests on macOS Monterey (12) tests

1 tests failed, 4 tests passed.

Failed tests

  • monotouch-test: Failed (exit code 2)
    • TestNSUrlSessionHandlerDetectMissingClientCertificate : Interop+AppleCrypto+AppleCommonCryptoCryptographicException : The contents of this item cannot be retrieved.
    • TestNSUrlSessionHandlerDetectMissingClientCertificateOptOut : Interop+AppleCrypto+AppleCommonCryptoCryptographicException : The contents of this item cannot be retrieved.
    • TestNSUrlSessionHandlerOptionalClientCertificate : Interop+AppleCrypto+AppleCommonCryptoCryptographicException : The contents of this item cannot be retrieved.
    • ... and 3 more failures

Html Report (VSDrops) Download

❌ Tests on macOS Ventura (13) tests

1 tests failed, 4 tests passed.

Failed tests

  • monotouch-test: Failed (exit code 2)
    • TestNSUrlSessionHandlerDetectMissingClientCertificate : Interop+AppleCrypto+AppleCommonCryptoCryptographicException : The contents of this item cannot be retrieved.
    • TestNSUrlSessionHandlerDetectMissingClientCertificateOptOut : Interop+AppleCrypto+AppleCommonCryptoCryptographicException : The contents of this item cannot be retrieved.
    • TestNSUrlSessionHandlerOptionalClientCertificate : Interop+AppleCrypto+AppleCommonCryptoCryptographicException : The contents of this item cannot be retrieved.
    • ... and 3 more failures

Html Report (VSDrops) Download

❌ Tests on macOS Sonoma (14) tests

1 tests failed, 4 tests passed.

Failed tests

  • monotouch-test: Failed (exit code 2)
    • TestNSUrlSessionHandlerDetectMissingClientCertificate : Interop+AppleCrypto+AppleCommonCryptoCryptographicException : The contents of this item cannot be retrieved.
    • TestNSUrlSessionHandlerDetectMissingClientCertificateOptOut : Interop+AppleCrypto+AppleCommonCryptoCryptographicException : The contents of this item cannot be retrieved.
    • TestNSUrlSessionHandlerOptionalClientCertificate : Interop+AppleCrypto+AppleCommonCryptoCryptographicException : The contents of this item cannot be retrieved.

Html Report (VSDrops) Download

❌ Tests on macOS Sequoia (15) tests

1 tests failed, 4 tests passed.

Failed tests

  • monotouch-test: Failed (exit code 2)
    • TestNSUrlSessionHandlerDetectMissingClientCertificate : Interop+AppleCrypto+AppleCommonCryptoCryptographicException : The contents of this item cannot be retrieved.
    • TestNSUrlSessionHandlerDetectMissingClientCertificateOptOut : Interop+AppleCrypto+AppleCommonCryptoCryptographicException : The contents of this item cannot be retrieved.
    • TestNSUrlSessionHandlerOptionalClientCertificate : Interop+AppleCrypto+AppleCommonCryptoCryptographicException : The contents of this item cannot be retrieved.
    • ... and 3 more failures

Html Report (VSDrops) Download

❌ Tests on macOS Tahoe (26) tests

1 tests failed, 4 tests passed.

Failed tests

  • monotouch-test: Failed (exit code 2)
    • TestNSUrlSessionHandlerDetectMissingClientCertificate : Interop+AppleCrypto+AppleCommonCryptoCryptographicException : The contents of this item cannot be retrieved.
    • TestNSUrlSessionHandlerDetectMissingClientCertificateOptOut : Interop+AppleCrypto+AppleCommonCryptoCryptographicException : The contents of this item cannot be retrieved.
    • TestNSUrlSessionHandlerOptionalClientCertificate : Interop+AppleCrypto+AppleCommonCryptoCryptographicException : The contents of this item cannot be retrieved.
    • ... and 3 more failures

Html Report (VSDrops) Download

Successes

✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (iOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (MacCatalyst): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (macOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (Multiple platforms): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (tvOS): All 1 tests passed. Html Report (VSDrops) Download
✅ framework: All 2 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 4 tests passed. Html Report (VSDrops) Download
✅ generator: All 5 tests passed. Html Report (VSDrops) Download
✅ interdependent-binding-projects: All 4 tests passed. Html Report (VSDrops) Download
✅ introspection: All 6 tests passed. Html Report (VSDrops) Download
✅ linker: All 44 tests passed. Html Report (VSDrops) Download
✅ monotouch (iOS): All 11 tests passed. Html Report (VSDrops) Download
✅ monotouch (MacCatalyst): All 15 tests passed. Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
✅ sharpie: All 1 tests passed. Html Report (VSDrops) Download
✅ windows: All 3 tests passed. Html Report (VSDrops) Download
✅ xcframework: All 4 tests passed. Html Report (VSDrops) Download
✅ xtro: All 1 tests passed. Html Report (VSDrops) Download

macOS tests

Linux Build Verification

Linux build succeeded

Pipeline on Agent
Hash: cf677dce29b4dc6188723072cba6204f75363233 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

🔥 [PR Build #cf677dc] Build failed (Detect API changes) 🔥

Build failed for the job 'Detect API changes' (with job status 'Canceled')

Pipeline on Agent
Hash: cf677dce29b4dc6188723072cba6204f75363233 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

🔥 Unable to find the contents for the comment: D:\a\1\s\change-detection\results\gh-comment.md does not exist :fire

Pipeline on Agent
Hash: cf677dce29b4dc6188723072cba6204f75363233 [PR build]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community Community contribution ❤

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Detecting certificate challenge in NSUrlSessionHandler

4 participants