Skip to content

Commit 729c20c

Browse files
olavmrkjohnweldon
authored andcommitted
Fix LDAP diagnostics message used as format string (#208)
* Add GetLDAPError() tests This patch adds a couple of tests for the `GetLDAPError()` function. * Fix LDAP diagnostics message used as format string The GetLDAPError()-function passes the `diagnosticMessage` error field as the first parameter to `fmt.Errorf()`. If this message happens to contain a `%`-character, Go will try to interpret it. This doesn't directly lead to an error, but results in error messages containing format string error codes. E.g.: The error message "Detailed error message %" will result in the error "Detailed error message %!(NOVERB)". This patch fixes this by inserting a format string as the first argument to `fmt.Errorf()`.
1 parent 813a903 commit 729c20c

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

error.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ func GetLDAPError(packet *ber.Packet) error {
207207
return nil
208208
}
209209
return &Error{ResultCode: resultCode, MatchedDN: response.Children[1].Value.(string),
210-
Err: fmt.Errorf(response.Children[2].Value.(string))}
210+
Err: fmt.Errorf("%s", response.Children[2].Value.(string))}
211211
}
212212
}
213213

error_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,45 @@ func TestConnReadErr(t *testing.T) {
5858
}
5959
}
6060

61+
// TestGetLDAPError tests parsing of result with a error response.
62+
func TestGetLDAPError(t *testing.T) {
63+
diagnosticMessage := "Detailed error message"
64+
bindResponse := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationBindResponse, nil, "Bind Response")
65+
bindResponse.AppendChild(ber.Encode(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, int64(LDAPResultInvalidCredentials), "resultCode"))
66+
bindResponse.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, "dc=example,dc=org", "matchedDN"))
67+
bindResponse.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, diagnosticMessage, "diagnosticMessage"))
68+
packet := ber.NewSequence("LDAPMessage")
69+
packet.AppendChild(ber.Encode(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, int64(0), "messageID"))
70+
packet.AppendChild(bindResponse)
71+
err := GetLDAPError(packet)
72+
if err == nil {
73+
t.Errorf("Did not get error response")
74+
}
75+
76+
ldapError := err.(*Error)
77+
if ldapError.ResultCode != LDAPResultInvalidCredentials {
78+
t.Errorf("Got incorrect error code in LDAP error; got %v, expected %v", ldapError.ResultCode, LDAPResultInvalidCredentials)
79+
}
80+
if ldapError.Err.Error() != diagnosticMessage {
81+
t.Errorf("Got incorrect error message in LDAP error; got %v, expected %v", ldapError.Err.Error(), diagnosticMessage)
82+
}
83+
}
84+
85+
// TestGetLDAPErrorSuccess tests parsing of a result with no error (resultCode == 0).
86+
func TestGetLDAPErrorSuccess(t *testing.T) {
87+
bindResponse := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationBindResponse, nil, "Bind Response")
88+
bindResponse.AppendChild(ber.Encode(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, int64(0), "resultCode"))
89+
bindResponse.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, "", "matchedDN"))
90+
bindResponse.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, "", "diagnosticMessage"))
91+
packet := ber.NewSequence("LDAPMessage")
92+
packet.AppendChild(ber.Encode(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, int64(0), "messageID"))
93+
packet.AppendChild(bindResponse)
94+
err := GetLDAPError(packet)
95+
if err != nil {
96+
t.Errorf("Successful responses should not produce an error, but got: %v", err)
97+
}
98+
}
99+
61100
// signalErrConn is a helpful type used with TestConnReadErr. It implements the
62101
// net.Conn interface to be used as a connection for the test. Most methods are
63102
// no-ops but the Read() method blocks until it receives a signal which it

0 commit comments

Comments
 (0)