Skip to content

Commit

Permalink
pr9856 option 2: check only if jsessionid is not null
Browse files Browse the repository at this point in the history
  • Loading branch information
weizhouapache committed Oct 28, 2024
1 parent 3930d00 commit e127a71
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions utils/src/main/java/com/cloud/utils/HttpUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ public static boolean validateSessionKey(final HttpSession session, final Map<St
return false;
}
final String jsessionidFromCookie = HttpUtils.findCookie(cookies, "JSESSIONID");
if (jsessionidFromCookie == null
|| !(jsessionidFromCookie.equals(session.getId()) || jsessionidFromCookie.startsWith(session.getId() + '.'))) {
if (jsessionidFromCookie != null
&& !(jsessionidFromCookie.equals(session.getId()) || jsessionidFromCookie.startsWith(session.getId() + '.'))) {
s_logger.error("JSESSIONID from cookie is invalid.");
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions utils/src/test/java/com/cloud/utils/HttpUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void validateSessionKeyTest() {
params = null;
cookies = new Cookie[]{new Cookie(sessionKeyString, sessionKeyValue)};
assertFalse(HttpUtils.validateSessionKey(session, params, cookies, "randomString", HttpUtils.ApiSessionKeyCheckOption.CookieOrParameter));
assertFalse(HttpUtils.validateSessionKey(session, params, cookies, sessionKeyString, HttpUtils.ApiSessionKeyCheckOption.CookieOrParameter));
assertTrue(HttpUtils.validateSessionKey(session, params, cookies, sessionKeyString, HttpUtils.ApiSessionKeyCheckOption.CookieOrParameter));

// param null, cookies not null test (JSESSIONID is not null and matches)
cookies = new Cookie[2];
Expand All @@ -95,7 +95,7 @@ public void validateSessionKeyTest() {
cookies = null;
assertFalse(HttpUtils.validateSessionKey(session, params, cookies, sessionKeyString, HttpUtils.ApiSessionKeyCheckOption.CookieOrParameter));
params.put(sessionKeyString, new String[]{sessionKeyValue});
assertFalse(HttpUtils.validateSessionKey(session, params, cookies, sessionKeyString, HttpUtils.ApiSessionKeyCheckOption.CookieOrParameter));
assertTrue(HttpUtils.validateSessionKey(session, params, cookies, sessionKeyString, HttpUtils.ApiSessionKeyCheckOption.CookieOrParameter));

// both param and cookies not null test (JSESSIONID is null)
params = new HashMap<String, Object[]>();
Expand All @@ -104,7 +104,7 @@ public void validateSessionKeyTest() {
params.put(sessionKeyString, new String[]{"incorrectValue"});
assertFalse(HttpUtils.validateSessionKey(session, params, cookies, sessionKeyString, HttpUtils.ApiSessionKeyCheckOption.CookieOrParameter));
params.put(sessionKeyString, new String[]{sessionKeyValue});
assertFalse(HttpUtils.validateSessionKey(session, params, cookies, sessionKeyString, HttpUtils.ApiSessionKeyCheckOption.CookieOrParameter));
assertTrue(HttpUtils.validateSessionKey(session, params, cookies, sessionKeyString, HttpUtils.ApiSessionKeyCheckOption.CookieOrParameter));

// both param and cookies not null test (JSESSIONID is not null but mismatches)
params = new HashMap<String, Object[]>();
Expand Down

0 comments on commit e127a71

Please sign in to comment.