Skip to content

Core - OnSelectClientCertificate own the copied certificate vector - #5281

Open
jrhodnik wants to merge 1 commit into
cefsharp:masterfrom
jrhodnik:upstream-pr/defer-client-certificate
Open

Core - OnSelectClientCertificate own the copied certificate vector#5281
jrhodnik wants to merge 1 commit into
cefsharp:masterfrom
jrhodnik:upstream-pr/defer-client-certificate

Conversation

@jrhodnik

@jrhodnik jrhodnik commented Aug 19, 2026

Copy link
Copy Markdown

Fixes: #2948

Summary:

  • ISelectClientCertificateCallback.Select still throws when called after OnSelectClientCertificate has returned, so the deferred form documented by CEF ("Return true and call CefSelectClientCertificateCallback::Select either in this method or at a later time") cannot be used
  • Winforms - ISelectClientCertificateCallback.Select exception #2948 was closed on the basis that there had been no reports since a51cdd37, with an invitation to reopen if it still reproduced. It does, on v144.0.270, and CefCertificateCallbackWrapper.h is unchanged on master
  • The 2021 diagnosis on that issue"It sounds like the vector is being freed. We can possibly copy the vector to resolve this (assuming the CefRefPtr instances are valid)" — was correct. a51cdd3 makes the copy, but CefCertificateCallbackWrapper stores it as const X509CertificateList& and the copy is a local in ClientAdapter::OnSelectClientCertificate, so it is destroyed when the handler returns, exactly as the original was
  • Answering inside the handler works, which is likely why no further reports followed. @thracx's workaround on that issue, removing InvokeOnUiThreadIfRequired so the certificate is selected on the handler's own thread, is the same observation from the other direction

Changes: [specify the structures changed]

  • I have modified CefCertificateCallbackWrapper
    • Owns a heap copy of the certificate list instead of holding const CefRequestHandler::X509CertificateList&
    • Allocated in the constructor initializer and deleted in the finalizer, following the ownership pattern already used by BrowserSettings, CefSettingsBase, RequestContextSettings and WindowInfo
    • Select iterates via the pointer
  • Copying the vector copies the reference-counted CefX509Certificate pointers, which is what keeps the certificates alive. A ref class cannot hold a std::vector (or a smart pointer) by value, which is why the field was a reference to begin with
  • No signature change, and no behaviour change for callers that answer inside the handler

How Has This Been Tested?
Built the NETCore packages for x86 and x64 from the v144.0.270 tag with this change, on VS2022 / Windows 10, and loaded servers that require a client certificate and servers that merely request one.

Deferred path, the failing case: held the callback, returned true from OnSelectClientCertificate, then called Select afterwards from another thread.

  • Before: the process died inside the thumbprint comparison loop.
  • After: the chosen certificate is presented on the still-suspended request and the server accepts it.

Also re-checked the two paths that already worked, both unchanged: calling Select inside the handler, and calling Select(nullptr) to continue without a certificate.

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Updated documentation

Checklist:

  • Tested the code(if applicable)
  • Commented my code
  • Changed the documentation(if applicable)
  • New files have a license disclaimer
  • The formatting is consistent with the project (project supports .editorconfig)

One point worth a maintainer's opinion: with deferral working, Select becomes reachable from a thread other than the CEF UI thread. The header's "or at a later time" wording reads as intended, and it works in practice, but if Select is meant to be UI-thread-only then that is worth documenting.

Summary by CodeRabbit

  • Bug Fixes
    • Improved certificate selection reliability by preserving the available certificate list for the duration of the selection process.
    • Prevented potential issues caused by accessing certificate data after its original source was released.

CefCertificateCallbackWrapper held the offered certificate list as
`const X509CertificateList&`, bound to a stack local built in
ClientAdapter::OnSelectClientCertificate. Once that handler returned the
list was destroyed, so calling Select() at any later point walked freed
memory and threw inside the thumbprint-matching loop, taking the host
process down with it.

CEF explicitly permits answering later. cef_request_handler.h says to
return true and call Select "either in this method or at a later time",
so a wrapper that outlives the handler has to own the list it selects
from. It now holds a heap-allocated copy, freed in the finalizer. A ref
class cannot contain a std::vector by value, hence the pointer. Copying
the vector copies the reference-counted CefX509Certificate pointers, and
those references are what keep the certificates alive.

This is the remaining half of cefsharp#2948. The comment above the caller reads
"Create a copy of the vector in an attempt to fix cefsharp#2948", and the copy is
indeed made - but it is then bound by reference, so it dies at the same
instant the original would have.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 0dafed63-04eb-455f-a288-ce197401b5a8

📥 Commits

Reviewing files that changed from the base of the PR and between 16bc6e0 and adcfd63.

📒 Files selected for processing (1)
  • CefSharp.Core.Runtime/Internals/CefCertificateCallbackWrapper.h

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

CefCertificateCallbackWrapper now owns a heap-allocated copy of the certificate list. It releases the copy during destruction and uses the owned list during certificate selection.

Changes

Certificate callback lifetime

Layer / File(s) Summary
Own and iterate the certificate list
CefSharp.Core.Runtime/Internals/CefCertificateCallbackWrapper.h
The wrapper copies the certificate list, deletes and nulls the allocation during finalization, and dereferences the owned list during iteration.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to adcfd

This localized fix keeps the copied certificate list alive for deferred selection without changing existing caller behavior. No actionable merge-blocking risk remains after normal checks and review.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the main change: owning the copied certificate vector for OnSelectClientCertificate.
Description check ✅ Passed The description follows the repository template and provides issue context, implementation details, testing results, change types, and checklist status.
Linked Issues check ✅ Passed The change addresses issue #2948 by preserving the certificate list for deferred Select calls and maintaining synchronous and default-selection behavior.
Out of Scope Changes check ✅ Passed The changes are limited to CefCertificateCallbackWrapper lifetime management and directly support the linked issue objectives.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@AppVeyorBot

Copy link
Copy Markdown

Build CefSharp 151.3.160-CI5592 completed (commit a1db8af689 by @)

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants