@@ -92,6 +92,7 @@ public class Cookie implements Cloneable, Serializable {
92
92
private boolean secure ; // ;Secure ... e.g. use SSL
93
93
private int version = 0 ; // ;Version=1 ... means RFC 2109++ style
94
94
private boolean isHttpOnly = false ;
95
+ private SameSiteMode sameSiteMode = null ;
95
96
96
97
/**
97
98
* Constructs a cookie with the specified name and value.
@@ -422,4 +423,82 @@ public void setHttpOnly(boolean isHttpOnly) {
422
423
public boolean isHttpOnly () {
423
424
return isHttpOnly ;
424
425
}
426
+
427
+ /**
428
+ * Sets the <i>SameSite</i> flag for this cookie with the provided value.
429
+ *
430
+ * <p>
431
+ * When <code>sameSiteMode</code> is not <code>null</code>, this cookie will have its <code>SameSite</code>
432
+ * attribute set with the provided value.
433
+ * </p>
434
+ *
435
+ * @param sameSiteMode The <i>SameSite</i> attribute's value. May be null if the attribute should not be set.
436
+ *
437
+ * @since Servlet 4.x
438
+ */
439
+ public void setSameSiteMode (SameSiteMode sameSiteMode ) {
440
+ this .sameSiteMode = sameSiteMode ;
441
+ }
442
+
443
+ /**
444
+ * Returns the <i>SameSite</i>> mode associated with this cookie.
445
+ *
446
+ * @return The <i>SameSite</i> mode associated with this cookie. May be null.
447
+ *
448
+ * @since Servlet 4.x
449
+ */
450
+ public SameSiteMode getSameSiteMode () {
451
+ return sameSiteMode ;
452
+ }
453
+
454
+ /**
455
+ * The available values that can be used with the cookie's <i>SameSite</i> attribute.
456
+ */
457
+ public enum SameSiteMode {
458
+
459
+ /**
460
+ * This mode effectively disables the protection provided by SameSite cookies.
461
+ *
462
+ * <p>
463
+ * When SameSiteMode is {@literal None}, applications should have additional measures to protect cookies
464
+ * from CSRF attacks.
465
+ * </p>
466
+ *
467
+ * <p>
468
+ * Browsers may default to {@link SameSiteMode#Lax} or {@link SameSiteMode#Strict} when the flag is not
469
+ * explicitly set by applications in the Set-Cookie header. Setting the {@literal SameSite} flag to
470
+ * {@literal None} ensures that this default behavior is disabled in browsers.
471
+ * </p>
472
+ *
473
+ * <p>
474
+ * Note that this mode is browser dependent, and browsers may require cookies to have the {@literal Secure}
475
+ * flag to be set at the same time.
476
+ * </p>
477
+ */
478
+ None ,
479
+ /**
480
+ * This mode allows sending cookies along for cross-site top level navigation requests.
481
+ *
482
+ * <p>
483
+ * When SameSiteMode is {@literal Lax}, the browser is allowed to send the cookie value along with top level
484
+ * navigation requests when making requests cross-site.
485
+ * </p>
486
+ */
487
+ Lax ,
488
+ /**
489
+ * This mode prevents browsers from sending cookies along with any kind of cross-site requests.
490
+ *
491
+ * <p>
492
+ * When SameSiteMode is {@literal Strict}, the browser is not allowed to send the cookie value along with
493
+ * cross-site requests.
494
+ * </p>
495
+ *
496
+ * <p>
497
+ * This behaviour could prevent applications seeing the end-user's session when a cross-site navigation
498
+ * takes place. To overcome this, applications should consider utilising multiple cookies to keep track of
499
+ * sessions.
500
+ * </p>
501
+ */
502
+ Strict
503
+ }
425
504
}
0 commit comments