Skip to content

Commit a8f2517

Browse files
committed
Improve getSourceInfo() null-safety and error handling
1 parent 183b868 commit a8f2517

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

src/main/java/hudson/plugins/active_directory/ActiveDirectoryUnixAuthenticationProvider.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -812,30 +812,30 @@ private void parseMembers(String userDN, Set<GrantedAuthority> groups, NamingEnu
812812
/**
813813
* Safely extracts the source IP address from the current HTTP request.
814814
* Returns an empty string if the request context is not available.
815-
*
815+
*
816816
* @return A formatted string with source IP (e.g., " from 192.168.1.100")
817817
* or an empty string if request information is unavailable
818818
*/
819819
private String getSourceInfo() {
820820
try {
821-
ServletRequestAttributes attributes =
822-
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
823-
824-
if (attributes != null) {
825-
HttpServletRequest request = attributes.getRequest();
826-
String remoteAddr = request.getRemoteAddr();
827-
828-
if (remoteAddr != null) {
829-
return " from " + remoteAddr;
830-
}
821+
// Use fail-fast lookup; throws if no request is bound
822+
ServletRequestAttributes attributes =
823+
(ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
824+
825+
HttpServletRequest request = attributes != null ? attributes.getRequest() : null;
826+
String remoteAddr = request != null ? request.getRemoteAddr() : null;
827+
828+
if (remoteAddr != null && !remoteAddr.isBlank()) {
829+
return " from " + remoteAddr;
831830
}
832-
} catch (Exception e) {
831+
} catch (IllegalStateException e) {
833832
// If we can't get request info, just continue without it
834833
LOGGER.log(Level.FINE, "Could not retrieve request source information", e);
835834
}
836835
return "";
837836
}
838837

838+
839839
/*package*/ static String toDC(String domainName) {
840840
StringBuilder buf = new StringBuilder();
841841
for (String token : domainName.split("\\.")) {

0 commit comments

Comments
 (0)