Skip to content

Commit

Permalink
fix JENKINS-63974 NPE when shutting down with CSRF
Browse files Browse the repository at this point in the history
  • Loading branch information
evernat committed Oct 18, 2020
1 parent 5b01c0a commit ced58bc
Showing 1 changed file with 12 additions and 10 deletions.
Expand Up @@ -195,17 +195,19 @@ static String htmlEncodeButNotSpace(String text) {
public static String getCsrfTokenUrlPart() {
if (CSRF_PROTECTION_ENABLED) {
final HttpSession currentSession = SessionListener.getCurrentSession();
String csrfToken = (String) currentSession
.getAttribute(SessionListener.CSRF_TOKEN_SESSION_NAME);
if (csrfToken == null) {
final byte[] bytes = new byte[16];
new SecureRandom().nextBytes(bytes);
csrfToken = new String(Base64Coder.encode(bytes));
// '+' would break in the url parameters
csrfToken = csrfToken.replace('+', '0').replace('/', '1');
currentSession.setAttribute(SessionListener.CSRF_TOKEN_SESSION_NAME, csrfToken);
if (currentSession != null) {
String csrfToken = (String) currentSession
.getAttribute(SessionListener.CSRF_TOKEN_SESSION_NAME);
if (csrfToken == null) {
final byte[] bytes = new byte[16];
new SecureRandom().nextBytes(bytes);
csrfToken = new String(Base64Coder.encode(bytes));
// '+' would break in the url parameters
csrfToken = csrfToken.replace('+', '0').replace('/', '1');
currentSession.setAttribute(SessionListener.CSRF_TOKEN_SESSION_NAME, csrfToken);
}
return "&" + HttpParameter.TOKEN + '=' + csrfToken;
}
return "&" + HttpParameter.TOKEN + '=' + csrfToken;
}
return "";
}
Expand Down

0 comments on commit ced58bc

Please sign in to comment.