Skip to content

Encode LDAP filter escapes as octets - #2363

Closed
blaze0089 wants to merge 1 commit into
fortra:masterfrom
blaze0089:fix/ldap-escaped-bytes
Closed

blaze0089 wants to merge 1 commit into
fortra:masterfrom
blaze0089:fix/ldap-escaped-bytes

Conversation

@blaze0089

Copy link
Copy Markdown

Problem

LDAPConnection._processLdapString() turns every \XX escape into chr(0xXX). Since AssertionValue has encoding = 'utf-8', any escaped octet above 0x7f is then sent as two UTF-8 bytes:

c._parseFilter(r"(objectGUID=\a1\00\ff)")  # assertionValue -> c2 a1 00 c3 bf
c._parseFilter(r"(cn=Jos\c3\a9)")          # assertionValue -> 4a 6f 73 c3 83 c2 a9

RFC 4515 defines \XX as one octet, so filters on binary attributes such as objectGUID and objectSid, and filters that escape UTF-8 characters, do not match. Escapes below 0x80 (\28, \29, \2a, \00) are unaffected.

Fix

Encode the unescaped text as UTF-8 first, then replace each \XX with its raw byte. Unescaped non-ASCII values keep working, and escaped UTF-8 sequences now produce the same octets as their literal form:

c._parseFilter(r"(objectGUID=\a1\00\ff)")  # -> a1 00 ff
c._parseFilter(r"(cn=Jos\c3\a9)")          # -> 4a 6f 73 c3 a9

The encoded filter now contains 04 03 a1 00 ff for the first case and 04 05 4a 6f 73 c3 a9 for the second.

Tests

New tests/misc/test_ldap_filter.py covers binary escapes, escaped and unescaped UTF-8, reserved characters, substring assertions, and extensible matches. 8 passed locally.

Fixes #2357.

RFC 4515 defines \XX as a single octet, but _processLdapString turned it into the code point chr(0xXX). Since AssertionValue encodes str as UTF-8, any escaped byte above 0x7f was sent as two bytes, so filters on binary attributes such as objectGUID and objectSid returned no entries.

Encode unescaped text as UTF-8 first and then replace each escape with its raw byte. Unescaped non-ASCII values keep working, and escaped UTF-8 sequences now produce the same octets as their literal form.

Fixes fortra#2357.
@anadrianmanrique

Copy link
Copy Markdown
Collaborator

this one looks duplicate of #2358

@blaze0089

Copy link
Copy Markdown
Author

You're right, #2358 covers this exactly, including the octet-based fix and the same test coverage. I missed it when I opened this. Closing mine as a duplicate so there's one place to review.

@blaze0089 blaze0089 closed this Nov 11, 2026
@anadrianmanrique anadrianmanrique added duplicate This issue or pull request already exists invalid This issue or pull request is no longer relevant labels Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

duplicate This issue or pull request already exists invalid This issue or pull request is no longer relevant

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LDAP search filters with escaped bytes of 0x80 or higher send the wrong value

2 participants