Skip to content

BindToDirectory throws NullReferenceException on Linux when LdapConnection AuthType is Anonymous #61683

@JasonDebug

Description

@JasonDebug

Description

When using AuthType.Anonymous in LdapConnection, System.DirectoryServices.Protocols.LdapPal.BindToDirectory throws a NullReferenceException.

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at System.DirectoryServices.Protocols.LdapPal.BindToDirectory(ConnectionHandle ld, String who, String passwd)
   at System.DirectoryServices.Protocols.LdapConnection.BindHelper(NetworkCredential newCredential, Boolean needSetCredential)
   at System.DirectoryServices.Protocols.LdapConnection.Bind()

This issue does not occur in Windows.

Reproduction Steps

Run the following code in Linux:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="System.DirectoryServices.Protocols" Version="6.0.0" />
  </ItemGroup>

</Project>
LdapDirectoryIdentifier ldapID = new LdapDirectoryIdentifier(_serverName_, 389, false, false);
LdapConnection ldapConnection = new LdapConnection(ldapID);
ldapConnection.AuthType = AuthType.Anonymous;
LdapSessionOptions ldapOptions = ldapConnection.SessionOptions;
ldapOptions.ProtocolVersion = 3;
ldapConnection.Bind();    // This line will throw NullReferenceException

Expected behavior

The code works

Actual behavior

The code throws a NullReferenceException

Regression?

The responsible code was added in this commit:

3291837#diff-1b5dd42e4f4f04e57161fa4e3a946dd3702fdc5ad206ab89ca75be6e46e95b42R110

Suggested change would be to simply check for null:

bv_len = passwd?.Length ?? 0,

Known Workarounds

None

Configuration

Tested with .NET 6.0.100 SDK on WSL2 Ubuntu

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions