Skip to content

Commit 90a8d1a

Browse files
committed
Set secure user cookies and only for HTTP.
Mark the user authentication cookie to be only used for HTTP, making it inaccessible for JavaScript engines. If only HTTPS is used and no HTTP (i.e. also if HTTP is redirected to HTTPS) then mark the user cookie to be sent only over secure connections.
1 parent d10fe0d commit 90a8d1a

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/main/java/com/gitblit/manager/AuthenticationManager.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,11 @@ public void setCookie(HttpServletRequest request, HttpServletResponse response,
608608
userCookie = new Cookie(Constants.NAME, cookie);
609609
// expire the cookie in 7 days
610610
userCookie.setMaxAge((int) TimeUnit.DAYS.toSeconds(7));
611+
612+
// Set cookies HttpOnly so they are not accessible to JavaScript engines
613+
userCookie.setHttpOnly(true);
614+
// Set secure cookie if only HTTPS is used
615+
userCookie.setSecure(httpsOnly());
611616
}
612617
}
613618
String path = "/";
@@ -622,6 +627,15 @@ public void setCookie(HttpServletRequest request, HttpServletResponse response,
622627
}
623628
}
624629

630+
631+
private boolean httpsOnly() {
632+
int port = settings.getInteger(Keys.server.httpPort, 0);
633+
int tlsPort = settings.getInteger(Keys.server.httpsPort, 0);
634+
return (port <= 0 && tlsPort > 0) ||
635+
(port > 0 && tlsPort > 0 && settings.getBoolean(Keys.server.redirectToHttpsPort, true) );
636+
}
637+
638+
625639
/**
626640
* Logout a user.
627641
*

0 commit comments

Comments
 (0)