Skip to content
This repository was archived by the owner on Jul 31, 2022. It is now read-only.

Commit 1a3a4ea

Browse files
committed
Illargs on session cookie setting
1 parent f0dd231 commit 1a3a4ea

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

src/main/java/com/kttdevelopment/simplehttpserver/HttpSession.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public abstract class HttpSession {
3030
* @return a {@link HttpSession}
3131
*
3232
* @since 02.00.00
33-
* @author KTt Development
33+
* @author Ktt Development
3434
*/
3535
synchronized static HttpSession create(){
3636
return HttpSessionImpl.createHttpSession();

src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchange.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ static SimpleHttpExchange create(final HttpExchange exchange){
320320
*
321321
* @param key name of cookie to set
322322
* @param value value of cookie
323+
* @throws IllegalArgumentException if the cookie name is reserved by the server
323324
*
324325
* @see SimpleHttpCookie
325326
* @see #setCookie(SimpleHttpCookie)
@@ -334,6 +335,7 @@ static SimpleHttpExchange create(final HttpExchange exchange){
334335
* Sets a cookie in the response header.
335336
*
336337
* @param cookie cookie to set
338+
* @throws IllegalArgumentException if the cookie name is reserved by the server
337339
*
338340
* @see SimpleHttpCookie
339341
* @see #setCookie(String, String)

src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchangeImpl.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,10 @@ public synchronized final void setCookie(final String key, final String value){
316316

317317
@Override
318318
public synchronized final void setCookie(final SimpleHttpCookie cookie){
319-
getResponseHeaders().add("Set-Cookie",cookie.toCookieHeaderString());
319+
final String cstring = cookie.toCookieHeaderString();
320+
if(cstring.startsWith("__session-id="))
321+
throw new IllegalArgumentException("The cookie '__session-id' can not be set because it is reserved by the server");
322+
getResponseHeaders().add("Set-Cookie",cstring);
320323
}
321324

322325
//
@@ -325,13 +328,15 @@ public synchronized final void setCookie(final SimpleHttpCookie cookie){
325328
public synchronized final HttpSession getHttpSession(){
326329
final String sessionId;
327330
final HttpSession session;
331+
328332
if((sessionId = cookies.get("__session-id")) == null || !HttpSession.sessions.containsKey(sessionId)){
329333
session = HttpSession.create();
330-
setCookie(
334+
final SimpleHttpCookie cookie =
331335
new SimpleHttpCookie.Builder("__session-id",session.getSessionID())
336+
.setPath("/")
332337
.setHttpOnly(true)
333-
.build()
334-
);
338+
.build();
339+
getResponseHeaders().add("Set-Cookie",cookie.toCookieHeaderString()); // bypass implementation
335340
}else{
336341
session = HttpSession.sessions.get(sessionId);
337342
}

0 commit comments

Comments
 (0)