Skip to content
Closed
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
16 changes: 15 additions & 1 deletion server/src/com/cloud/api/ApiServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Cookie;

import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.ApiServerService;
Expand Down Expand Up @@ -166,6 +167,18 @@ void processRequestInContext(final HttpServletRequest req, final HttpServletResp
responseType = (String)responseTypeParam[0];
}

// Implement HttpOnly Session Cookie Support
String[] setAuth=(String[])params.get(ApiConstants.SESSIONKEY);
if ((setAuth!=null&&setAuth.length>0)&&(setAuth[0]==null||setAuth[0].equals("null"))){
Cookie[] requestCookies = req.getCookies();
for(Cookie c : requestCookies){
if (c.getName().equals(ApiConstants.SESSIONKEY)) {
setAuth[0]=c.getValue();
break;
}
}
}

final Object[] commandObj = params.get(ApiConstants.COMMAND);
if (commandObj != null) {
final String command = (String) commandObj[0];
Expand Down Expand Up @@ -195,9 +208,10 @@ void processRequestInContext(final HttpServletRequest req, final HttpServletResp
}
}
}

try {
responseString = apiAuthenticator.authenticate(command, params, session, InetAddress.getByName(remoteAddress), responseType, auditTrailSb, req, resp);
// Add HttpOnly cookie to header on successful auth
resp.addHeader("SET-COOKIE", ApiConstants.SESSIONKEY+"=\"" + session.getAttribute("sessionkey") + "\";HttpOnly;Version=1");
} catch (ServerApiException e) {
httpResponseCode = e.getErrorCode().getHttpCode();
responseString = e.getMessage();
Expand Down
23 changes: 9 additions & 14 deletions ui/scripts/cloudStack.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@
bypassLoginCheck: function(args) { //determine to show or bypass login screen
if (g_loginResponse == null) { //show login screen
/*
* Since we no longer store sessionKey in cookie, opening the
* 2nd browser window (of the same domain) will show login screen (i.e. user has to
* enter credentials again) and will cause the 1st browser window session timeout.
* Changing the cookie name to hostname_cookieName allows
* separate sessions in the same domain to be handled
* separately without breaking session persistence.
*/
var unBoxCookieValue = function (cookieName) {
var cookieValue = $.cookie(cookieName);
Expand All @@ -117,15 +117,8 @@
}
return cookieValue;
};
unBoxCookieValue('sessionkey');
// if sessionkey cookie exists use this to set g_sessionKey
// and destroy sessionkey cookie
if ($.cookie('sessionkey')) {
g_sessionKey = $.cookie('sessionkey');
$.cookie('sessionkey', null);
} else {
g_sessionKey = unBoxCookieValue('JSESSIONID');
}

g_sessionKey = unBoxCookieValue('sessionKey');

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original issue was related to security where session key was stored in cookie; the crux of the fix by Jessica was to use the cookie to set the sessionkey on window (g_sessionKey variable) and destroy it so it may not be read later.

g_role = unBoxCookieValue('role');
g_userid = unBoxCookieValue('userid');
g_domainid = unBoxCookieValue('domainid');
Expand Down Expand Up @@ -328,14 +321,17 @@
g_kvmsnapshotenabled = null;
g_regionsecondaryenabled = null;
g_loginCmdText = null;
g_userfullname = null;
g_userid = null;

$.cookie('JSESSIONID', null);
$.cookie('sessionkey', null);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm no UI/Javascript expert, but should these include hostname as well? If so, same goes for L351+

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed Erik, thanks for pointing that out. I've updated the PR.

On Tue, May 26, 2015 at 11:46 PM, Erik Weber notifications@github.com
wrote:

In ui/scripts/cloudStack.js
#308 (comment):

@@ -329,7 +325,6 @@
g_regionsecondaryenabled = null;
g_loginCmdText = null;

  •                    $.cookie('JSESSIONID', null);
                     $.cookie('sessionkey', null);
    

I'm no UI/Javascript expert, but should these include hostname as well? If
so, same goes for L351+

—
Reply to this email directly or view it on GitHub
https://github.com/apache/cloudstack/pull/308/files#r31083230.

$.cookie('username', null);
$.cookie('account', null);
$.cookie('domainid', null);
$.cookie('role', null);
$.cookie('timezone', null);
$.cookie('userfullname', null);
$.cookie('userid', null);

if (onLogoutCallback()) { //onLogoutCallback() will set g_loginResponse(single-sign-on variable) to null, then bypassLoginCheck() will show login screen.
document.location.reload(); //when onLogoutCallback() returns true, reload the current document.
Expand All @@ -353,7 +349,6 @@
},

samlLoginAction: function(args) {
$.cookie('JSESSIONID', null);
$.cookie('sessionkey', null);
$.cookie('username', null);
$.cookie('account', null);
Expand Down