Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
O
Openfire
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Openfire
Commits
4971bfe9
Commit
4971bfe9
authored
Oct 22, 2014
by
Tom Evans
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #88 from tevans/master
OF-830: Properly handle LDAP search filters
parents
b4bc7650
fd090d88
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
111 additions
and
18 deletions
+111
-18
LdapAuthorizationMapping.java
.../jivesoftware/openfire/ldap/LdapAuthorizationMapping.java
+2
-1
LdapGroupProvider.java
...ava/org/jivesoftware/openfire/ldap/LdapGroupProvider.java
+5
-7
LdapManager.java
src/java/org/jivesoftware/openfire/ldap/LdapManager.java
+55
-2
LdapUserProvider.java
...java/org/jivesoftware/openfire/ldap/LdapUserProvider.java
+4
-6
LDAPTest.java
src/test/java/org/jivesoftware/util/LDAPTest.java
+45
-2
No files found.
src/java/org/jivesoftware/openfire/ldap/LdapAuthorizationMapping.java
View file @
4971bfe9
...
...
@@ -107,7 +107,8 @@ public class LdapAuthorizationMapping implements AuthorizationMapping {
}
constraints
.
setReturningAttributes
(
new
String
[]
{
usernameField
});
NamingEnumeration
answer
=
ctx
.
search
(
""
,
princSearchFilter
,
new
String
[]
{
principal
},
NamingEnumeration
answer
=
ctx
.
search
(
""
,
princSearchFilter
,
new
String
[]
{
LdapManager
.
sanitizeSearchFilter
(
principal
)},
constraints
);
Log
.
debug
(
"LdapAuthorizationMapping: ... search finished"
);
if
(
answer
==
null
||
!
answer
.
hasMoreElements
())
{
...
...
src/java/org/jivesoftware/openfire/ldap/LdapGroupProvider.java
View file @
4971bfe9
...
...
@@ -165,7 +165,7 @@ public class LdapGroupProvider extends AbstractGroupProvider {
StringBuilder
filter
=
new
StringBuilder
();
filter
.
append
(
"(&"
);
filter
.
append
(
MessageFormat
.
format
(
manager
.
getGroupSearchFilter
(),
"*"
));
filter
.
append
(
"("
).
append
(
key
).
append
(
"="
).
append
(
value
);
filter
.
append
(
"("
).
append
(
key
).
append
(
"="
).
append
(
LdapManager
.
sanitizeSearchFilter
(
value
)
);
filter
.
append
(
"))"
);
if
(
Log
.
isDebugEnabled
())
{
Log
.
debug
(
"Trying to find group names using query: "
+
filter
.
toString
());
...
...
@@ -188,13 +188,11 @@ public class LdapGroupProvider extends AbstractGroupProvider {
if
(
query
==
null
||
""
.
equals
(
query
))
{
return
Collections
.
emptyList
();
}
// Make the query be a wildcard search by default. So, if the user searches for
// "Test", make the search be "Test*" instead.
if
(!
query
.
endsWith
(
"*"
))
{
query
=
query
+
"*"
;
}
StringBuilder
filter
=
new
StringBuilder
();
filter
.
append
(
"("
).
append
(
manager
.
getGroupNameField
()).
append
(
"="
).
append
(
query
).
append
(
")"
);
// Make the query be a wildcard search by default. So, if the user searches for
// "Test", make the sanitized search be "Test*" instead.
filter
.
append
(
"("
).
append
(
manager
.
getGroupNameField
()).
append
(
"="
)
.
append
(
LdapManager
.
sanitizeSearchFilter
(
query
)).
append
(
"*)"
);
// Perform the LDAP query
return
manager
.
retrieveList
(
manager
.
getGroupNameField
(),
...
...
src/java/org/jivesoftware/openfire/ldap/LdapManager.java
View file @
4971bfe9
...
...
@@ -20,6 +20,7 @@
package
org
.
jivesoftware
.
openfire
.
ldap
;
import
java.io.UnsupportedEncodingException
;
import
java.net.URLEncoder
;
import
java.text.MessageFormat
;
import
java.util.ArrayList
;
...
...
@@ -971,7 +972,8 @@ public class LdapManager {
}
constraints
.
setReturningAttributes
(
new
String
[]
{
usernameField
});
NamingEnumeration
<
SearchResult
>
answer
=
ctx
.
search
(
""
,
getSearchFilter
(),
new
String
[]
{
username
},
NamingEnumeration
<
SearchResult
>
answer
=
ctx
.
search
(
""
,
getSearchFilter
(),
new
String
[]
{
sanitizeSearchFilter
(
username
)},
constraints
);
if
(
debug
)
{
...
...
@@ -1115,7 +1117,7 @@ public class LdapManager {
}
constraints
.
setReturningAttributes
(
new
String
[]
{
groupNameField
});
String
filter
=
MessageFormat
.
format
(
getGroupSearchFilter
(),
groupname
);
String
filter
=
MessageFormat
.
format
(
getGroupSearchFilter
(),
sanitizeSearchFilter
(
JID
.
unescapeNode
(
groupname
))
);
NamingEnumeration
<
SearchResult
>
answer
=
ctx
.
search
(
""
,
filter
,
constraints
);
if
(
debug
)
{
...
...
@@ -2182,6 +2184,57 @@ public class LdapManager {
}
return
count
;
}
/**
* Escapes any special chars (RFC 4515) from a string representing
* a search filter assertion value.
*
* @param input The input string.
*
* @return A assertion value string ready for insertion into a
* search filter string.
*/
public
static
String
sanitizeSearchFilter
(
final
String
value
)
{
StringBuilder
result
=
new
StringBuilder
();
for
(
int
i
=
0
;
i
<
value
.
length
();
i
++)
{
char
c
=
value
.
charAt
(
i
);
switch
(
c
)
{
case
'!'
:
result
.
append
(
"\\21"
);
break
;
case
'&'
:
result
.
append
(
"\\26"
);
break
;
case
'('
:
result
.
append
(
"\\28"
);
break
;
case
')'
:
result
.
append
(
"\\29"
);
break
;
case
'*'
:
result
.
append
(
"\\2a"
);
break
;
case
':'
:
result
.
append
(
"\\3a"
);
break
;
case
'\\'
:
result
.
append
(
"\\5c"
);
break
;
case
'|'
:
result
.
append
(
"\\7c"
);
break
;
case
'~'
:
result
.
append
(
"\\7e"
);
break
;
case
'\u0000'
:
result
.
append
(
"\\00"
);
break
;
default
:
if
(
c
<=
0x7f
)
{
// regular 1-byte UTF-8 char
result
.
append
(
String
.
valueOf
(
c
));
}
else
if
(
c
>=
0x080
)
{
// higher-order 2, 3 and 4-byte UTF-8 chars
try
{
byte
[]
utf8bytes
=
String
.
valueOf
(
c
).
getBytes
(
"UTF8"
);
for
(
byte
b:
utf8bytes
)
{
result
.
append
(
String
.
format
(
"\\%02x"
,
b
));
}
}
catch
(
UnsupportedEncodingException
e
)
{
// ignore
}
}
}
}
return
result
.
toString
();
}
/**
* Encloses DN values with "
...
...
src/java/org/jivesoftware/openfire/ldap/LdapUserProvider.java
View file @
4971bfe9
...
...
@@ -255,11 +255,6 @@ public class LdapUserProvider implements UserProvider {
if
(!
searchFields
.
keySet
().
containsAll
(
fields
))
{
throw
new
IllegalArgumentException
(
"Search fields "
+
fields
+
" are not valid."
);
}
// Make the query be a wildcard search by default. So, if the user searches for
// "John", make the search be "John*" instead.
if
(!
query
.
endsWith
(
"*"
))
{
query
=
query
+
"*"
;
}
StringBuilder
filter
=
new
StringBuilder
();
//Add the global search filter so only those users the directory administrator wants to include
//are returned from the directory
...
...
@@ -271,7 +266,10 @@ public class LdapUserProvider implements UserProvider {
}
for
(
String
field:
fields
)
{
String
attribute
=
searchFields
.
get
(
field
);
filter
.
append
(
"("
).
append
(
attribute
).
append
(
"="
).
append
(
query
).
append
(
")"
);
// Make the query be a wildcard search by default. So, if the user searches for
// "John", make the sanitized search be "John*" instead.
filter
.
append
(
"("
).
append
(
attribute
).
append
(
"="
)
.
append
(
LdapManager
.
sanitizeSearchFilter
(
query
)).
append
(
"*)"
);
}
if
(
fields
.
size
()
>
1
)
{
filter
.
append
(
")"
);
...
...
src/test/java/org/jivesoftware/util/LDAPTest.java
View file @
4971bfe9
...
...
@@ -9,11 +9,13 @@
*/
package
org
.
jivesoftware
.
util
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
javax.naming.ldap.Rdn
;
import
org.jivesoftware.openfire.ldap.LdapManager
;
import
org.junit.Test
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
/**
* @author Daniel Henninger
*/
...
...
@@ -36,4 +38,45 @@ public class LDAPTest {
converted
=
LdapManager
.
getEnclosedDN
(
before
);
assertTrue
(
"Conversion result "
+
before
+
" to "
+
converted
,
converted
.
equals
(
after
));
}
@Test
public
void
testRdnEscapeValue
()
{
String
before
=
"Jive Software, Inc"
;
String
after
=
"Jive Software\\, Inc"
;
String
converted
=
Rdn
.
escapeValue
(
before
);
assertTrue
(
"Conversion result "
+
before
+
" to "
+
converted
,
converted
.
equals
(
after
));
before
=
"Test.User; (+1)"
;
after
=
"Test.User\\; (\\+1)"
;
converted
=
Rdn
.
escapeValue
(
before
);
assertTrue
(
"Conversion result "
+
before
+
" to "
+
converted
,
converted
.
equals
(
after
));
before
=
"Wildcard *"
;
after
=
"Wildcard *"
;
converted
=
Rdn
.
escapeValue
(
before
);
assertTrue
(
"Conversion result "
+
before
+
" to "
+
converted
,
converted
.
equals
(
after
));
before
=
"Group/Section"
;
after
=
"Group/Section"
;
converted
=
Rdn
.
escapeValue
(
before
);
assertTrue
(
"Conversion result "
+
before
+
" to "
+
converted
,
converted
.
equals
(
after
));
}
@Test
public
void
testSanitizeSearchFilter
()
{
String
before
=
"Test.User; (+1)"
;
String
after
=
"Test.User; \\28+1\\29"
;
String
converted
=
LdapManager
.
sanitizeSearchFilter
(
before
);
assertTrue
(
"Conversion result "
+
before
+
" to "
+
converted
,
converted
.
equals
(
after
));
before
=
"Wildcard *"
;
after
=
"Wildcard \\2a"
;
converted
=
LdapManager
.
sanitizeSearchFilter
(
before
);
assertTrue
(
"Conversion result "
+
before
+
" to "
+
converted
,
converted
.
equals
(
after
));
before
=
"~ Group|Section & Teams!"
;
after
=
"\\7e Group\\7cSection \\26 Teams\\21"
;
converted
=
LdapManager
.
sanitizeSearchFilter
(
before
);
assertTrue
(
"Conversion result "
+
before
+
" to "
+
converted
,
converted
.
equals
(
after
));
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment