Skip to content

Commit

Permalink
Adding SameSite cookie information to .NET cookie handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jimevans committed Mar 17, 2020
1 parent 13d830b commit c543c22
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
19 changes: 18 additions & 1 deletion dotnet/src/webdriver/Cookie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class Cookie
private string cookieValue;
private string cookiePath;
private string cookieDomain;
private string sameSite;
private DateTime? cookieExpiry;

/// <summary>
Expand Down Expand Up @@ -179,6 +180,16 @@ public virtual bool IsHttpOnly
get { return false; }
}

/// <summary>
/// Gets the SameSite setting for the cookie.
/// </summary>
[JsonProperty("sameSite")]
public virtual string SameSite
{
get { return this.sameSite; }
protected set { this.sameSite = value; }
}

/// <summary>
/// Gets the expiration date of the cookie.
/// </summary>
Expand Down Expand Up @@ -258,7 +269,13 @@ public static Cookie FromDictionary(Dictionary<string, object> rawCookie)
isHttpOnly = bool.Parse(rawCookie["httpOnly"].ToString());
}

return new ReturnedCookie(name, value, domain, path, expires, secure, isHttpOnly);
string sameSite = null;
if (rawCookie.ContainsKey("sameSite") && rawCookie["sameSite"] != null)
{
sameSite = rawCookie["sameSite"].ToString();
}

return new ReturnedCookie(name, value, domain, path, expires, secure, isHttpOnly, sameSite);
}

/// <summary>
Expand Down
23 changes: 22 additions & 1 deletion dotnet/src/webdriver/Internal/ReturnedCookie.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="ReturnedCookie.cs" company="WebDriver Committers">
// <copyright file="ReturnedCookie.cs" company="WebDriver Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
Expand Down Expand Up @@ -44,10 +44,31 @@ public class ReturnedCookie : Cookie
/// or if it contains a semi-colon.</exception>
/// <exception cref="ArgumentNullException">If the value or currentUrl is <see langword="null"/>.</exception>
public ReturnedCookie(string name, string value, string domain, string path, DateTime? expiry, bool isSecure, bool isHttpOnly)
: this(name, value, domain, path, expiry, isSecure, isHttpOnly, null)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ReturnedCookie"/> class with a specific name,
/// value, domain, path and expiration date.
/// </summary>
/// <param name="name">The name of the cookie.</param>
/// <param name="value">The value of the cookie.</param>
/// <param name="domain">The domain of the cookie.</param>
/// <param name="path">The path of the cookie.</param>
/// <param name="expiry">The expiration date of the cookie.</param>
/// <param name="isSecure"><see langword="true"/> if the cookie is secure; otherwise <see langword="false"/></param>
/// <param name="isHttpOnly"><see langword="true"/> if the cookie is an HTTP-only cookie; otherwise <see langword="false"/></param>
/// <param name="sameSite">The SameSite value of cookie.</param>
/// <exception cref="ArgumentException">If the name is <see langword="null"/> or an empty string,
/// or if it contains a semi-colon.</exception>
/// <exception cref="ArgumentNullException">If the value or currentUrl is <see langword="null"/>.</exception>
public ReturnedCookie(string name, string value, string domain, string path, DateTime? expiry, bool isSecure, bool isHttpOnly, string sameSite)
: base(name, value, domain, path, expiry)
{
this.isSecure = isSecure;
this.isHttpOnly = isHttpOnly;
this.SameSite = sameSite;
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions dotnet/test/common/CookieImplementationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ public void CanHandleSecureCookie()
{
driver.Url = EnvironmentManager.Instance.UrlBuilder.WhereIsSecure("animals");

Cookie addedCookie = new ReturnedCookie("fish", "cod", null, "/common/animals", null, true, false);
Cookie addedCookie = new ReturnedCookie("fish", "cod", null, "/common/animals", null, true, false, null);
driver.Manage().Cookies.AddCookie(addedCookie);

driver.Navigate().Refresh();
Expand All @@ -490,7 +490,7 @@ public void ShouldRetainCookieSecure()
{
driver.Url = EnvironmentManager.Instance.UrlBuilder.WhereIsSecure("animals");

ReturnedCookie addedCookie = new ReturnedCookie("fish", "cod", string.Empty, "/common/animals", null, true, false);
ReturnedCookie addedCookie = new ReturnedCookie("fish", "cod", string.Empty, "/common/animals", null, true, false, null);

driver.Manage().Cookies.AddCookie(addedCookie);

Expand Down

0 comments on commit c543c22

Please sign in to comment.