Skip to content

[gRPC] Use typed API to read certificate DNS names - #69237

Merged
javiercn merged 3 commits into
mainfrom
javiercn/typed-san-extraction
Sep 28, 2026
Merged

javiercn merged 3 commits into
mainfrom
javiercn/typed-san-extraction

Conversation

@javiercn

@javiercn javiercn commented Sep 11, 2026 •

Copy link
Copy Markdown
Member

Fixes #69169

Overview

gRPC JSON transcoding currently derives AuthContext SAN identities by parsing the human-readable output of X509Extension.Format(). That text varies by OS and culture, and valid dNSName values containing formatter-like delimiters can be split or truncated. This change replaces the heuristic with structured DER decoding and adds focused certificate coverage; it changes no public API.

Design

None. The affected helper is internal, so no public API or API review is involved.

The implementation uses the framework-provided X509SubjectAlternativeNameExtension rather than maintaining a platform-specific parser. This preserves the existing contract—return only dNSName entries in encoded order—while making extraction independent of localized display text. Malformed SAN DER continues to surface as an explicit decoding failure rather than being treated as an absent DNS identity and falling back to the certificate common name.

Implementation

// src/Grpc/JsonTranscoding/src/Shared/X509CertificateHelpers.cs
// Match the SAN extension by OID, decode its DER payload through the typed API,
// and enumerate only DNS entries in their original encoded order.
if (ext.Oid?.Value == X509SubjectAlternativeNameId)
{
    var subjectAlternativeName = new X509SubjectAlternativeNameExtension(ext.RawData, ext.Critical);
    return subjectAlternativeName.EnumerateDnsNames().ToArray();
}

The removed code called Format(false), inferred each platform's localized identifier/delimiter/separator from a synthetic extension, then split formatted strings. The replacement delegates DER validation and GeneralName filtering to the X509 API designed for SAN data.

Certificate-level tests create real self-signed certificates with DER-encoded SAN extensions and cover these behavior classes:

  • no SAN extension returns no names;
  • mixed email, DNS, URI, and IP entries return only DNS names in encoded order;
  • a DNS value containing Windows-style formatted text (first.example, DNS Name=second.example) is preserved intact;
  • a DNS value containing Linux-style formatted text (first.example, DNS:second.example) is preserved intact;
  • malformed SAN DER throws CryptographicException instead of silently enabling common-name fallback.

Outcome

Before the fix, the two delimiter-sensitive rows failed independently: the Windows-shaped value became two identities, while the Linux-shaped value was truncated. After the fix, all focused certificate tests pass (5/5), the shipping assembly builds with zero warnings and errors, and the complete JsonTranscoding unit suite passes (305/305).

The canonical src\Grpc\build.cmd -test -noRestore path was attempted after generating required JavaScript assets and initializing required submodules, but its framework ref-pack packaging step requires wix.exe, which is unavailable in this environment. Validation therefore built the actual JsonTranscoding assembly and test project against the installed SDK framework with project-reference rebuilding disabled.

@github-actions github-actions Bot added the area-grpc Includes: GRPC wire-up, templates label Sep 11, 2026
@javiercn javiercn changed the title Use typed API to read gRPC certificate DNS names [gRPC] Use typed API to read certificate DNS names Sep 11, 2026
@javiercn
javiercn marked this pull request as ready for review September 14, 2026 10:05
@javiercn
javiercn requested a review from SamMonoRT as a code owner September 14, 2026 10:05
Copilot AI lite review requested due to automatic review settings September 14, 2026 10:05

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The typed SAN implementation and focused tests address the reviewed behavior without unresolved issues.

Pull request overview

Replaces localized SAN text parsing with typed DER decoding for reliable DNS identity extraction.

Changes:

  • Uses X509SubjectAlternativeNameExtension.EnumerateDnsNames().
  • Adds focused certificate tests for filtering, ordering, and delimiter-containing names.
File summaries
File Description
src/Grpc/JsonTranscoding/test/Microsoft.AspNetCore.Grpc.JsonTranscoding.Tests/X509CertificateHelpersTests.cs Adds focused certificate coverage.
src/Grpc/JsonTranscoding/src/Shared/X509CertificateHelpers.cs Implements structured SAN DNS extraction.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@javiercn
javiercn merged commit 6f5bf24 into main Sep 28, 2026
27 checks passed
@javiercn
javiercn deleted the javiercn/typed-san-extraction branch September 28, 2026 13:18
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-grpc Includes: GRPC wire-up, templates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

gRPC AuthContext: SAN dNSName extraction relies on parsing X509Extension.Format() text instead of typed API

4 participants