Fix panic on malformed LDAP responses#586
Merged
cpuschma merged 1 commit intogo-ldap:masterfrom Apr 4, 2026
Merged
Conversation
Six operations (Add, Del, Compare, Modify, ModifyDN, PasswordModify) access packet.Children[1] without bounds checking. A malformed BER response with fewer than 2 children causes a panic (index out of range). Add len(packet.Children) < 2 guard in all affected functions, returning a descriptive error instead of panicking. Fixes #585
cpuschma
approved these changes
Apr 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Six operations (
Add,Del,Compare,Modify,ModifyDN,PasswordModify) accesspacket.Children[1]without bounds checking. A malformed BER response with fewer than 2 children causes a panic (index out of range).Issue #453 previously fixed
GetLDAPErrorfor the same class of bug, but these individual callers were never patched.Fixes #585
Solution
Add
len(packet.Children) < 2guard before accessingpacket.Children[1]in all seven affected call sites across six files:v3/add.go—Add()v3/del.go—Del()v3/compare.go—Compare()v3/modify.go—Modify()andModifyWithResult()v3/moddn.go—ModifyDN()v3/passwdmodify.go—PasswordModify()Each now returns a descriptive error (
"ldap: malformed response: expected at least 2 children, got N") instead of panicking.Testing
The fix is purely defensive — existing tests should pass unchanged. A malformed packet with 0 or 1 children will now return an error instead of crashing.