Commit 2f1b2e6
Improve LDAP authentication error handling and resource management (#2949)
* Improve LDAP authentication error handling and resource management
This commit enhances the LDAP authentication implementation with several
important improvements for robustness, security, and maintainability:
## Error Handling Improvements
- Added more specific exception handling in validate(), login(), and
changePassword() methods
- Separated LdapConfigurationException, LdapOperationException, and
generic Exception handling for better error diagnosis
- Improved error logging with more descriptive messages including
context information (e.g., username, operation type)
## Resource Management
- Enhanced ThreadLocal cleanup in DirContextHolder.close() with
try-finally block to ensure contextLocal.remove() is always called
- Prevents potential ThreadLocal memory leaks in application servers
- Added debug logging for context close failures
## Defensive Programming
- Added null/blank checks in critical methods:
* login(username, password) - validates both parameters
* login(username) - validates username parameter
* changePassword() - validates username and password
* getSAMAccountGroupName() - validates bindDn and groupName
- Returns empty results gracefully instead of failing with NPE
- Added early returns for invalid input to avoid unnecessary processing
## Documentation Enhancements
- Expanded JavaDoc for escapeLDAPSearchFilter() with:
* Detailed character escaping reference per RFC 4515
* Security notes emphasizing LDAP injection prevention
* RFC reference link for further reading
- Enhanced changePassword() JavaDoc with:
* HTML-formatted documentation
* Detailed validation steps list
* Parameter constraints clearly documented
- Added null handling in escapeLDAPSearchFilter() for safety
- Optimized StringBuilder initial capacity for better performance
## Minor Code Quality Improvements
- Changed local variables to final where appropriate for immutability
- Improved code organization with better nesting and try-catch structure
- Consistent error message formatting across methods
These changes improve the security, reliability, and maintainability of
the LDAP authentication module without breaking existing functionality.
* Fix JavaDoc to match null handling in escapeLDAPSearchFilter
Updated the JavaDoc documentation for escapeLDAPSearchFilter() to accurately
reflect that null input is treated as an empty string, rather than stating
'must not be null' which conflicted with the implementation.
This change resolves the inconsistency between documentation and
implementation, maintaining defensive programming while being clear about
the method's behavior.
* Add comprehensive tests for LDAP authentication improvements
This commit adds extensive unit tests for the LDAP authentication
improvements made in previous commits, covering:
## LDAP Injection Prevention Tests (11 tests)
- Null and empty string handling
- Normal input (alphanumeric, dots)
- Special character escaping:
* Backslash (\) → \5c
* Asterisk (*) → \2a (prevents wildcard injection)
* Parentheses ((), )) → \28, \29 (prevents filter injection)
* Null byte (\0) → \00
- Complex injection attempt simulation
- All special characters combined
- Unicode character preservation
- Mixed content handling
## Defensive Null/Blank Check Tests (11 tests)
- getSAMAccountGroupName():
* Null bindDn handling
* Blank bindDn handling (empty string and whitespace)
* Null groupName handling
* Blank groupName handling
- changePassword():
* Null username handling
* Blank username handling
* Null password handling
* Blank password handling
* Admin disabled scenario
## Error Handling Tests (3 tests)
- normalizePermissionName():
* Null input handling
* Lowercase enabled behavior
* Lowercase disabled behavior
## Edge Case Tests (3 tests)
- getSearchRoleName() with various invalid inputs
- replaceWithUnderscores() with edge cases
- escapeLDAPSearchFilter() with Unicode characters
Total: 28 new comprehensive unit tests added
Test Coverage:
- Security: LDAP injection prevention
- Robustness: Null/blank input handling
- Correctness: Expected behavior validation
- Edge cases: Boundary condition testing
All tests follow the existing test structure using UnitFessTestCase
and verify the improvements made to error handling, defensive
programming, and security enhancements.
* Fix test_replaceWithUnderscores_withEdgeCases assertion
Corrected the expected value in test_replaceWithUnderscores_withEdgeCases.
The input string "//\\[]:;" contains 8 special characters:
- // (2 slashes) → __
- \\ (2 backslashes) → __
- []:; (4 characters) → ____
Total: 8 underscores, not 10 as previously expected.
This fixes the test failure:
expected:<________[__]> but was:<________[]>
* Fix flaky test_startProcess_replaceExistingProcess test
The test was failing intermittently because it used 'echo' commands
which complete almost instantaneously. By the time the assertion
checked if the process was running, it had already terminated.
Changes:
- Replace 'echo' with 'cat' command which waits for input, making
it a long-running process suitable for testing
- Add polling logic (max 5 seconds) to wait for process to become
running before assertions
- Add descriptive failure messages to help diagnose timeout issues
This makes the test more reliable across different system loads and
timing conditions while still testing the intended behavior of
replacing an existing process with the same session ID.
Note: This fix is independent of LDAP authentication improvements
and addresses a pre-existing test stability issue.
* Change LDAP error logs to warn level per logging policy
Updated logger.error() calls to logger.warn() in LDAP authentication
code to follow the Fess logging policy:
- error: System-stopping errors where service cannot be provided
- warn: Issues where system remains usable (e.g., search still works)
Changes:
- validate(): LdapConfigurationException now logs at warn level
- changePassword(): Both LdapOperationException and general exceptions
now log at warn level
Rationale: LDAP authentication failures do not prevent the search
functionality from working, so these should be warnings rather than
errors.
* Enhance logging for LDAP errors
---------
Co-authored-by: Claude <[email protected]>1 parent bdc87b8 commit 2f1b2e6
File tree
3 files changed
+493
-63
lines changed- src
- main/java/org/codelibs/fess/ldap
- test/java/org/codelibs/fess
- helper
- ldap
3 files changed
+493
-63
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
195 | 195 | | |
196 | 196 | | |
197 | 197 | | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
203 | 206 | | |
204 | | - | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
205 | 211 | | |
206 | | - | |
| 212 | + | |
207 | 213 | | |
208 | 214 | | |
209 | 215 | | |
| |||
217 | 223 | | |
218 | 224 | | |
219 | 225 | | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
220 | 234 | | |
221 | 235 | | |
222 | 236 | | |
223 | 237 | | |
224 | | - | |
225 | | - | |
226 | | - | |
227 | | - | |
228 | | - | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
229 | 249 | | |
230 | | - | |
| 250 | + | |
231 | 251 | | |
232 | | - | |
233 | | - | |
234 | | - | |
235 | | - | |
| 252 | + | |
236 | 253 | | |
237 | | - | |
| 254 | + | |
| 255 | + | |
238 | 256 | | |
239 | | - | |
| 257 | + | |
240 | 258 | | |
241 | 259 | | |
242 | 260 | | |
| |||
248 | 266 | | |
249 | 267 | | |
250 | 268 | | |
251 | | - | |
252 | | - | |
253 | | - | |
254 | | - | |
255 | | - | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
256 | 288 | | |
257 | | - | |
| 289 | + | |
258 | 290 | | |
259 | | - | |
260 | | - | |
261 | | - | |
262 | | - | |
| 291 | + | |
263 | 292 | | |
264 | | - | |
| 293 | + | |
| 294 | + | |
265 | 295 | | |
266 | | - | |
| 296 | + | |
267 | 297 | | |
268 | 298 | | |
269 | 299 | | |
| |||
370 | 400 | | |
371 | 401 | | |
372 | 402 | | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
373 | 411 | | |
374 | 412 | | |
375 | 413 | | |
| |||
390 | 428 | | |
391 | 429 | | |
392 | 430 | | |
| 431 | + | |
| 432 | + | |
393 | 433 | | |
394 | | - | |
| 434 | + | |
395 | 435 | | |
396 | 436 | | |
397 | 437 | | |
| |||
463 | 503 | | |
464 | 504 | | |
465 | 505 | | |
466 | | - | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
467 | 516 | | |
468 | | - | |
469 | | - | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
470 | 523 | | |
471 | | - | |
472 | | - | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
473 | 529 | | |
474 | | - | |
| 530 | + | |
475 | 531 | | |
476 | 532 | | |
477 | 533 | | |
| |||
1458 | 1514 | | |
1459 | 1515 | | |
1460 | 1516 | | |
1461 | | - | |
1462 | | - | |
| 1517 | + | |
| 1518 | + | |
| 1519 | + | |
| 1520 | + | |
| 1521 | + | |
| 1522 | + | |
| 1523 | + | |
| 1524 | + | |
| 1525 | + | |
1463 | 1526 | | |
| 1527 | + | |
1464 | 1528 | | |
1465 | 1529 | | |
1466 | | - | |
| 1530 | + | |
| 1531 | + | |
| 1532 | + | |
1467 | 1533 | | |
1468 | 1534 | | |
1469 | 1535 | | |
1470 | | - | |
1471 | | - | |
1472 | | - | |
1473 | | - | |
1474 | | - | |
| 1536 | + | |
| 1537 | + | |
| 1538 | + | |
1475 | 1539 | | |
1476 | | - | |
1477 | | - | |
1478 | | - | |
1479 | | - | |
1480 | | - | |
| 1540 | + | |
| 1541 | + | |
| 1542 | + | |
| 1543 | + | |
| 1544 | + | |
| 1545 | + | |
| 1546 | + | |
| 1547 | + | |
| 1548 | + | |
| 1549 | + | |
| 1550 | + | |
| 1551 | + | |
| 1552 | + | |
| 1553 | + | |
| 1554 | + | |
| 1555 | + | |
| 1556 | + | |
| 1557 | + | |
| 1558 | + | |
| 1559 | + | |
| 1560 | + | |
| 1561 | + | |
1481 | 1562 | | |
1482 | 1563 | | |
1483 | 1564 | | |
| |||
1671 | 1752 | | |
1672 | 1753 | | |
1673 | 1754 | | |
1674 | | - | |
1675 | | - | |
1676 | | - | |
1677 | | - | |
1678 | | - | |
1679 | | - | |
| 1755 | + | |
| 1756 | + | |
| 1757 | + | |
| 1758 | + | |
| 1759 | + | |
| 1760 | + | |
| 1761 | + | |
| 1762 | + | |
| 1763 | + | |
1680 | 1764 | | |
| 1765 | + | |
| 1766 | + | |
| 1767 | + | |
1681 | 1768 | | |
1682 | 1769 | | |
1683 | 1770 | | |
| |||
Lines changed: 25 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
149 | 149 | | |
150 | 150 | | |
151 | 151 | | |
152 | | - | |
153 | | - | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
154 | 156 | | |
155 | 157 | | |
156 | 158 | | |
| |||
159 | 161 | | |
160 | 162 | | |
161 | 163 | | |
162 | | - | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
163 | 175 | | |
164 | 176 | | |
165 | 177 | | |
166 | 178 | | |
167 | | - | |
168 | 179 | | |
169 | | - | |
170 | | - | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
171 | 190 | | |
172 | 191 | | |
173 | 192 | | |
| |||
0 commit comments