Skip to content

Harden Negotiate LDAP role and identity validation - #10226

Merged
wtgodbe merged 2 commits into
mainfrom
wtgodbe/port-6b230a0-main
Oct 14, 2026
Merged

wtgodbe merged 2 commits into
mainfrom
wtgodbe/port-6b230a0-main

Conversation

@wtgodbe

@wtgodbe wtgodbe commented Oct 14, 2026

Copy link
Copy Markdown
Member

Restrict Negotiate LDAP role claims to security-enabled groups and validate that LDAP search results match the Kerberos-authenticated principal's SID. This prevents distribution groups from being treated as roles and avoids potential account confusion.

Roman Konecny and others added 2 commits October 13, 2026 15:43
…role claims to security-enabled groups

## Summary

`LdapAdapter` emitted every group in the user's `memberOf` as a role claim, including Active Directory **distribution groups**. The Windows Kerberos token carries only security-group SIDs, so a distribution group never becomes a role on Windows. On Linux the LDAP path granted it.

This breaks the documented contract in Role-based authorization ("Windows Authentication **security groups** as app roles"), which applies to `AddNegotiate()` regardless of platform.

## Fix

`GetNestedGroups` now requests `groupType` alongside `memberof` in the **existing** base-scope read, so there is **no extra LDAP round trip** (verified: `groupType` was already on the wire and being discarded). Groups without `GROUP_TYPE_SECURITY_ENABLED` (`0x80000000`) no longer produce a role claim.

Design notes:

- Bit test is done in C#, **not** via the AD-proprietary matching-rule OID `1.2.840.113556.1.4.803`, which non-AD directories do not implement.
- **Fails open** when `groupType` is absent or unparseable, so non-AD directories and foreign security principals are unaffected.
- Traversal still recurses **through** non-security groups (only the claim is skipped), so a security group nested inside a distribution group still grants its role.
- Suppressed groups are logged at `Debug` with the group DN, so the resulting 403 is diagnosable.

## Breaking change / opt-out

Apps that relied on distribution groups for authorization will start receiving 403. Opt-out:

`Microsoft.AspNetCore.Authentication.Negotiate.AllowNonSecurityEnabledGroupClaims=true`

## Validation

- Unit tests added for the `groupType` bit matrix, covering all security/distribution scopes plus unparseable and missing values (fail-open).
- Reproduced end-to-end against a Samba AD DC rig: a user in a distribution group whose CN matches the role gets **200 before / 403 after**, with the legitimate `Readers` role retained. Removing the membership restores 403, confirming causality.
- Local repo build not run: the internal branch pins an unreleased servicing runtime unavailable on public feeds. Final binaries will be validated end-to-end before release.

## Scope

Does **not** address role-name (CN) collisions across OUs, which is tracked separately. Fix messaging should stay scoped to distribution groups.

----
#### AI description  (iteration 1)
#### PR Classification
Bug fix to restrict LDAP role claims to security-enabled groups only, with an opt-out switch to restore previous behavior.

#### PR Summary
This PR modifies the LDAP authentication adapter to filter out distribution groups (non-security-enabled groups) from role claims by default, addressing a security concern where all group types were previously included. An AppContext switch allows restoring the previous behavior if needed.

- `LdapAdapter.cs`: Added `IsSecurityGroup()` and `ShouldEmitRoleClaim()` methods to evaluate the `groupType` LDAP attribute (checking for 0x80000000 flag) and determine whether to emit role claims based on securi...

Co-authored-by: Copilot <[email protected]>
… Kerberos Principal

Bug fix to prevent potential account confusion by validating that LDAP search results match the Kerberos-authenticated principal's SID.

----
#### AI description  (iteration 1)
#### PR Classification
Security bug fix to prevent potential account confusion by validating that the LDAP-retrieved account matches the Kerberos-authenticated principal's SID.

#### PR Summary
Adds SID (Security Identifier) validation in the LDAP adapter to ensure the LDAP search result corresponds to the same user account as the Kerberos-authenticated principal, preventing potential security issues from name-based mismatches.

- `LdapAdapter.cs`: Implements `VerifyLdapSidMatchesKerberosPrincipal()` to validate LDAP objectSid matches the Kerberos principal's PrimarySid claim before resolving claims
- `LdapAdapter.cs`: Adds `ParseSidBytesToString()` cross-platform helper to convert binary SID bytes to canonical string format (e.g., "S-1-5-21-...")
- `LdapAdapter.cs`: Updates cache key to include both username and SID for unique user identification
- `LdapAdapter.cs`: Introduces `RequirePrimarySidClaim` AppContext switch to optionally enforce PrimarySid claim presence
- `LdapAdapterTests.cs`: Adds comprehensive unit tests covering SID matching scenarios, edge cases, and the SID parsing implementation
<!-- GitOpsUserAgent=GitOps.Apps.Server.pullrequestcopilot -->

Co-authored-by: Copilot <[email protected]>
@wtgodbe
wtgodbe requested review from DeagleGross and rokonec and a balanced review from Copilot October 14, 2026 16:10
@dotnet-policy-service
dotnet-policy-service Bot requested a review from a team October 14, 2026 16:10
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
1 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

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.

🟡 Changes recommended

SID validation remains opt-in for SID-less identities, unverifiable groups fail open, and integration-path coverage is missing.

3 open findings
What changed in this PR

Hardens Negotiate LDAP claim resolution by validating account SIDs and filtering group roles.

Changes:

  • Adds LDAP/Kerberos SID comparison.
  • Filters distribution groups from role claims.
  • Adds SID and group-type unit tests.
File Description
LdapAdapter.cs Implements SID validation, cache-key changes, and group filtering.
LdapAdapterTests.cs Tests SID parsing, matching, and group classification.

🧠 Review effort: Balanced


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

@wtgodbe
wtgodbe merged commit 9a576c9 into main Oct 14, 2026
12 checks passed
@wtgodbe
wtgodbe deleted the wtgodbe/port-6b230a0-main branch October 14, 2026 23:39
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.

3 participants