Skip to content

Commit 3356b7c

Browse files
authored
fix: mitigate storing password in the memory (#2867)
Signed-off-by: Pavel Jareš <pavel.jares@broadcom.com>
1 parent 9159bc1 commit 3356b7c

File tree

3 files changed

+29
-18
lines changed

3 files changed

+29
-18
lines changed

apiml-security-common/src/main/java/org/zowe/apiml/security/common/content/AbstractSecureContentFilter.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,19 @@ protected void doFilterInternal(@NonNull HttpServletRequest request, @NonNull Ht
7272
Optional<AbstractAuthenticationToken> authenticationToken = extractContent(request);
7373

7474
if (authenticationToken.isPresent()) {
75+
Authentication authentication = null;
7576
try {
76-
Authentication authentication = authenticationManager.authenticate(authenticationToken.get());
77+
authentication = authenticationManager.authenticate(authenticationToken.get());
7778
SecurityContextHolder.getContext().setAuthentication(authentication);
7879
filterChain.doFilter(request, response);
7980
} catch (AuthenticationException authenticationException) {
8081
failureHandler.onAuthenticationFailure(request, response, authenticationException);
8182
} catch (RuntimeException e) {
8283
resourceAccessExceptionHandler.handleException(request, response, e);
8384
} finally {
84-
Authentication authentication = authenticationToken.get();
85+
// TODO: remove once fixed directly in Spring - org.springframework.security.core.CredentialsContainer#eraseCredentials
8586
if (authentication != null) {
86-
Object credentials = authenticationToken.get().getCredentials();
87+
Object credentials = authentication.getCredentials();
8788
if (credentials instanceof char[]) {
8889
Arrays.fill((char[]) credentials, (char) 0);
8990
}

security-service-client-spring/src/main/java/org/zowe/apiml/security/client/login/GatewayLoginProvider.java

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.zowe.apiml.security.common.login.LoginRequest;
2121
import org.zowe.apiml.security.common.token.TokenAuthentication;
2222

23+
import java.util.Arrays;
2324
import java.util.Optional;
2425

2526
import static org.zowe.apiml.security.SecurityUtils.readPassword;
@@ -41,26 +42,34 @@ public class GatewayLoginProvider implements AuthenticationProvider {
4142
@Override
4243
public Authentication authenticate(Authentication authentication) {
4344
String username = authentication.getPrincipal().toString();
44-
char[] password;
45+
char[] password = null;
4546
char[] newPassword = null;
46-
if (authentication.getCredentials() instanceof LoginRequest) {
47-
LoginRequest credentials = (LoginRequest) authentication.getCredentials();
48-
password = credentials.getPassword();
49-
newPassword = LoginRequest.getNewPassword(authentication);
50-
} else {
51-
password = readPassword(authentication.getCredentials());
52-
}
47+
boolean cleanup = false;
48+
try {
49+
if (authentication.getCredentials() instanceof LoginRequest) {
50+
LoginRequest credentials = (LoginRequest) authentication.getCredentials();
51+
password = credentials.getPassword();
52+
newPassword = LoginRequest.getNewPassword(authentication);
53+
} else {
54+
password = readPassword(authentication.getCredentials());
55+
cleanup = !(authentication.getCredentials() instanceof char[]);
56+
}
5357

54-
Optional<String> token = gatewaySecurityService.login(username, password, newPassword);
58+
Optional<String> token = gatewaySecurityService.login(username, password, newPassword);
5559

56-
if (!token.isPresent()) {
57-
throw new BadCredentialsException("Invalid Credentials");
58-
}
60+
if (!token.isPresent()) {
61+
throw new BadCredentialsException("Invalid Credentials");
62+
}
5963

60-
TokenAuthentication tokenAuthentication = new TokenAuthentication(username, token.get());
61-
tokenAuthentication.setAuthenticated(true);
64+
TokenAuthentication tokenAuthentication = new TokenAuthentication(username, token.get());
65+
tokenAuthentication.setAuthenticated(true);
6266

63-
return tokenAuthentication;
67+
return tokenAuthentication;
68+
} finally {
69+
if (cleanup) {
70+
Arrays.fill(password, (char) 0);
71+
}
72+
}
6473
}
6574

6675
@Override

security-service-client-spring/src/main/java/org/zowe/apiml/security/client/service/GatewaySecurityService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public Optional<String> login(String username, char[] password, char[] newPasswo
8888
} catch (IOException e) {
8989
responseHandler.handleException(e);
9090
} finally {
91+
// TODO: remove once fixed directly in Spring - org.springframework.security.core.CredentialsContainer#eraseCredentials
9192
loginRequest.evictSensitiveData();
9293
}
9394
return Optional.empty();

0 commit comments

Comments
 (0)