Skip to content

Secure cookies #1167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/java/com/gitblit/GitBlitServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,8 @@ public void log(String message) {
HashSessionManager sessionManager = new HashSessionManager();
sessionManager.setHttpOnly(true);
// Use secure cookies if only serving https
sessionManager.setSecureRequestOnly(params.port <= 0 && params.securePort > 0);
sessionManager.setSecureRequestOnly( (params.port <= 0 && params.securePort > 0) ||
(params.port > 0 && params.securePort > 0 && settings.getBoolean(Keys.server.redirectToHttpsPort, true)) );
rootContext.getSessionHandler().setSessionManager(sessionManager);

// Ensure there is a defined User Service
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/gitblit/manager/AuthenticationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,11 @@ public void setCookie(HttpServletRequest request, HttpServletResponse response,
userCookie = new Cookie(Constants.NAME, cookie);
// expire the cookie in 7 days
userCookie.setMaxAge((int) TimeUnit.DAYS.toSeconds(7));

// Set cookies HttpOnly so they are not accessible to JavaScript engines
userCookie.setHttpOnly(true);
// Set secure cookie if only HTTPS is used
userCookie.setSecure(httpsOnly());
}
}
String path = "/";
Expand All @@ -622,6 +627,15 @@ public void setCookie(HttpServletRequest request, HttpServletResponse response,
}
}


private boolean httpsOnly() {
int port = settings.getInteger(Keys.server.httpPort, 0);
int tlsPort = settings.getInteger(Keys.server.httpsPort, 0);
return (port <= 0 && tlsPort > 0) ||
(port > 0 && tlsPort > 0 && settings.getBoolean(Keys.server.redirectToHttpsPort, true) );
}


/**
* Logout a user.
*
Expand Down