dotnet / dotnet/runtime

LdapConnection incorrectly marshal HostName string for ReferralCallback

Open
#121,439 1 comment 1 reaction 2 assignees Claimed by @steveisok View on GitHub
area-System.DirectoryServices
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

### Description

When an instance of LdapConnection is configured to chase referrals, incorrect server identifier is passed to LdapConnection.SessionOptions.ReferralCallback.

### Reproduction Steps

The following code reproduces the problem

```
const string server = "domain.com"; // Specify you AD domain
const string dn = "DC=child,DC=domain,DC=com"; // Specify DN of an object from another domain but of the same forest

NetworkCredential cred = new("administrator", "Password", "domain.com"); // Specify credentials

using LdapConnection connection = new(server);
connection.Bind(cred);
connection.SessionOptions.ReferralChasing = ReferralChasingOptions.All;
connection.SessionOptions.ReferralCallback = new()
{
QueryForConnection = (LdapConnection primaryConnection,
LdapConnection referralFromConnection,
string newDistinguishedName,
LdapDirectoryIdentifier identifier,
NetworkCredential credential,
long currentUserToken) =>
{
// Here identifier.Servers contains incorrect string with unicode characters
return null;
}
};
SearchRequest request = new(dn, "(objectClass=*)", SearchScope.OneLevel, null);
SearchResponse response = (SearchResponse)connection.SendRequest(request);
```

### Expected behavior

Argument **identifier** should contains the correct name of the referred-to server.

### Actual behavior

Argument **identifier** contains a string like _污桰⹡摡硡_

### Regression?

It DID work in .NET Framewok.

### Known Workarounds

_No response_

### Configuration

.NET version is 9.0
OS - Windows 11,
Architecture - x64

### Other information

It looks like the problem is in the **LdapSessionOptions.ProcessQueryConnection** method. According to the description of **LDAP_REFERRAL_CALLBACK** structure (https://learn.microsoft.com/en-us/windows/win32/api/winldap/ns-winldap-ldap_referral_callback), the type of HostName argument is **PCHAR**, but the string is marhalled using **Marshal.PtrToStringUni** instead of **Marshal.PtrToStringAnsi**

Image

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.