Skip to content

Fix NegotiateAuthentication to surface TargetUnknown status for unknown SPNs - #126623

Merged
rzikm merged 3 commits into
mainfrom
copilot/fix-negotiate-authentication-target-unknown
Jun 9, 2026
Merged

rzikm merged 3 commits into
mainfrom
copilot/fix-negotiate-authentication-target-unknown

Conversation

Copilot AI commented Apr 8, 2026 •

Copy link
Copy Markdown
Contributor

NegotiateAuthentication returned GenericFailure instead of TargetUnknown when the target SPN does not exist in the KDC — both on Windows (SSPI) and Unix (GSSAPI/Kerberos).

Description

Windows (NegotiateAuthenticationPal.Windows.cs): SecurityStatusPalErrorCode.TargetUnknown was missing from the SecurityStatusPalErrorCode → NegotiateAuthenticationStatusCode switch, causing it to fall through to default (GenericFailure).

Unix (NegotiateAuthenticationPal.Unix.cs): GSS_S_FAILURE was unhandled in GetErrorCode. When the major status is GSS_S_FAILURE and the minor status is KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN (0x96C73A07 — the com_err-based Kerberos error, identical in MIT and Heimdal Kerberos), the method now returns TargetUnknown.

// Before: returned GenericFailure when target SPN doesn't exist
// After:
var client = new NegotiateAuthentication(new NegotiateAuthenticationClientOptions
{
    TargetName = "HTTP/nonexistent-server.example.com"
});
client.GetOutgoingBlob(null, out var statusCode);
// statusCode == NegotiateAuthenticationStatusCode.TargetUnknown  ✓

Test (NegotiateAuthenticationKerberosTest.cs): Added Client_UnknownSPN_ReturnsTargetUnknown using KerberosExecutor. The test spins up a fake in-process KDC with a registered service and user, then attempts Kerberos authentication (Package = "Kerberos") to an unregistered SPN (HTTP/nonexistent.linux.contoso.com). This exercises the GSS_S_FAILURE/KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN code path and asserts that GetOutgoingBlob returns NegotiateAuthenticationStatusCode.TargetUnknown.

Copilot AI requested review from Copilot and removed request for Copilot April 8, 2026 02:43
…nknown SPN

- Windows: Add SecurityStatusPalErrorCode.TargetUnknown mapping to NegotiateAuthenticationStatusCode.TargetUnknown
- Unix: Handle GSS_S_FAILURE with KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN minor status to return TargetUnknown

Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/cf7ddabe-e0b2-43dc-b16d-ea550f2e080a

Co-authored-by: wfurt <[email protected]>
Copilot AI requested review from Copilot and removed request for Copilot April 8, 2026 03:10
Copilot AI changed the title [WIP] Fix NegotiateAuthentication to notify TargetUnknown correctly Fix NegotiateAuthentication to surface TargetUnknown status for unknown SPNs Apr 8, 2026
Copilot AI requested a review from wfurt April 8, 2026 03:11
Copilot finished work on behalf of wfurt April 8, 2026 03:11
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/ncl, @bartonjs, @vcsjones
See info in area-owners.md if you want to be subscribed.

@rzikm

rzikm commented Apr 8, 2026

Copy link
Copy Markdown
Member

Can we add enterprise tests for this?

@rzikm

rzikm commented Jun 9, 2026

Copy link
Copy Markdown
Member

@copilot can you add some tests for this change? Using either FakeNtlmServer or KerberosExecutor

Copilot AI requested review from Copilot and removed request for Copilot June 9, 2026 07:48

Copilot AI commented Jun 9, 2026

Copy link
Copy Markdown
Contributor Author

@copilot can you add some tests for this change? Using either FakeNtlmServer or KerberosExecutor

Added Client_UnknownSPN_ReturnsTargetUnknown to NegotiateAuthenticationKerberosTest.cs using KerberosExecutor. The test sets up a KDC with one registered service (for the keytab) and a user, then attempts Kerberos authentication to an unregistered SPN (HTTP/nonexistent.linux.contoso.com). It asserts that GetOutgoingBlob returns TargetUnknown — exercising the GSS_S_FAILURE/KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN code path in NegotiateAuthenticationPal.Unix.cs.

Copilot finished work on behalf of rzikm June 9, 2026 07:49
Copilot AI requested a review from rzikm June 9, 2026 07:49
@rzikm
rzikm marked this pull request as ready for review June 9, 2026 11:18
Copilot AI review requested due to automatic review settings June 9, 2026 11:18

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.

Pull request overview

This PR fixes NegotiateAuthentication status-code mapping so that an unknown target SPN surfaces as NegotiateAuthenticationStatusCode.TargetUnknown (instead of GenericFailure) across both Windows (SSPI) and Unix (GSSAPI/Kerberos).

Changes:

  • Windows: map SecurityStatusPalErrorCode.TargetUnknown to NegotiateAuthenticationStatusCode.TargetUnknown.
  • Unix: treat GSS_S_FAILURE + Kerberos minor status for “server principal unknown” as TargetUnknown.
  • Tests: add a Kerberos functional test that authenticates to an unregistered SPN and asserts TargetUnknown.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/libraries/System.Net.Security/tests/FunctionalTests/NegotiateAuthenticationKerberosTest.cs Adds regression test asserting unknown SPN returns TargetUnknown.
src/libraries/System.Net.Security/src/System/Net/NegotiateAuthenticationPal.Windows.cs Fixes SSPI error-to-status mapping for TargetUnknown.
src/libraries/System.Net.Security/src/System/Net/NegotiateAuthenticationPal.Unix.cs Adds Kerberos-specific mapping from GSS_S_FAILURE minor status to TargetUnknown.

@rzikm
rzikm enabled auto-merge (squash) June 9, 2026 14:22
@rzikm
rzikm merged commit 3654fe4 into main Jun 9, 2026
97 checks passed
@rzikm
rzikm deleted the copilot/fix-negotiate-authentication-target-unknown branch June 9, 2026 14:53
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview6 milestone Jun 17, 2026
eiriktsarpalis pushed a commit that referenced this pull request Jul 15, 2026
…wn SPNs (#126623)

`NegotiateAuthentication` returned `GenericFailure` instead of
`TargetUnknown` when the target SPN does not exist in the KDC — both on
Windows (SSPI) and Unix (GSSAPI/Kerberos).

## Description

**Windows** (`NegotiateAuthenticationPal.Windows.cs`):
`SecurityStatusPalErrorCode.TargetUnknown` was missing from the
`SecurityStatusPalErrorCode` → `NegotiateAuthenticationStatusCode`
switch, causing it to fall through to `default` (`GenericFailure`).

**Unix** (`NegotiateAuthenticationPal.Unix.cs`): `GSS_S_FAILURE` was
unhandled in `GetErrorCode`. When the major status is `GSS_S_FAILURE`
and the minor status is `KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN` (`0x96C73A07`
— the `com_err`-based Kerberos error, identical in MIT and Heimdal
Kerberos), the method now returns `TargetUnknown`.

```csharp
// Before: returned GenericFailure when target SPN doesn't exist
// After:
var client = new NegotiateAuthentication(new NegotiateAuthenticationClientOptions
{
    TargetName = "HTTP/nonexistent-server.example.com"
});
client.GetOutgoingBlob(null, out var statusCode);
// statusCode == NegotiateAuthenticationStatusCode.TargetUnknown  ✓
```

**Test** (`NegotiateAuthenticationKerberosTest.cs`): Added
`Client_UnknownSPN_ReturnsTargetUnknown` using `KerberosExecutor`. The
test spins up a fake in-process KDC with a registered service and user,
then attempts Kerberos authentication (`Package = "Kerberos"`) to an
unregistered SPN (`HTTP/nonexistent.linux.contoso.com`). This exercises
the `GSS_S_FAILURE`/`KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN` code path and
asserts that `GetOutgoingBlob` returns
`NegotiateAuthenticationStatusCode.TargetUnknown`.

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: wfurt <[email protected]>
Co-authored-by: rzikm <[email protected]>
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 18, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NegotiateAuthentication failed authentication doesn't notify of TargetUnknown

4 participants