Skip to content

Commit

Permalink
Merge pull request #354 from kagemomiji/issue353-cookie-security
Browse files Browse the repository at this point in the history
#353 Fix code scanning alert about cookies
  • Loading branch information
kagemomiji authored Jan 27, 2024
2 parents 71729f2 + c561462 commit 93b40f9
Showing 1 changed file with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.web.server.Cookie.SameSite;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.event.EventListener;
import org.springframework.http.ResponseCookie;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.ServletRequestUtils;
Expand Down Expand Up @@ -181,15 +183,14 @@ public synchronized Player getPlayer(HttpServletRequest request, HttpServletResp
// Set cookie in response.
if (response != null) {
String cookieName = COOKIE_NAME + "-" + StringUtil.utf8HexEncode(username);
Cookie cookie = new Cookie(cookieName, String.valueOf(player.getId()));
cookie.setMaxAge(COOKIE_EXPIRY);
cookie.setHttpOnly(true);
String path = request.getContextPath();
if (StringUtils.isEmpty(path)) {
path = "/";
}
cookie.setPath(path);
response.addCookie(cookie);
ResponseCookie cookie = ResponseCookie.from(cookieName, String.valueOf(player.getId()))
.maxAge(COOKIE_EXPIRY)
.httpOnly(true)
.path(StringUtils.isEmpty(path) ? "/" : path)
.sameSite(SameSite.STRICT.attributeValue())
.build();
response.addHeader("Set-Cookie", cookie.toString());
}

// Save player in session context.
Expand Down

0 comments on commit 93b40f9

Please sign in to comment.