Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: Adding new cookie sameSite (c-nodejs) #7901

Merged
merged 6 commits into from
Mar 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions javascript/node/selenium-webdriver/lib/webdriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,7 @@ class Options {
* invalid.
* @throws {TypeError} if `spec` is not a cookie object.
*/
addCookie({name, value, path, domain, secure, httpOnly, expiry}) {
addCookie({name, value, path, domain, secure, httpOnly, expiry, sameSite}) {
// We do not allow '=' or ';' in the name.
if (/[;=]/.test(name)) {
throw new error.InvalidArgumentError(
Expand All @@ -1131,6 +1131,16 @@ class Options {
expiry = Math.floor(date.getTime() / 1000);
}

// if not declared, attribute is set to 'undefined'
// (or)
// if declared (ex: sameSite:"") and string length is zero, attribute is 'None' by default
if(typeof sameSite === "undefined" || sameSite.length===0) {
harsha509 marked this conversation as resolved.
Show resolved Hide resolved
// Do noting. sameSite value will be none by default
} else if(sameSite!=="undefined" && (sameSite!=="Strict" && sameSite!=="Lax")) {
throw new error.InvalidArgumentError(
'Invalid sameSite cookie value "' + sameSite + '". It should be either "Lax" (or) "Strict" ');
harsha509 marked this conversation as resolved.
Show resolved Hide resolved
}

return this.driver_.execute(
new command.Command(command.Name.ADD_COOKIE).
setParameter('cookie', {
Expand All @@ -1140,7 +1150,8 @@ class Options {
'domain': domain,
'secure': !!secure,
'httpOnly': !!httpOnly,
'expiry': expiry
'expiry': expiry,
'sameSite': sameSite
}));
}

Expand Down Expand Up @@ -1398,6 +1409,17 @@ Options.Cookie.prototype.httpOnly;
Options.Cookie.prototype.expiry;


/**
* When the cookie applies to a SameSite policy.
*
* When {@linkplain Options#addCookie() adding a cookie}, this may be specified
* as a {@link string} object which is either 'Lax' or 'Strict'.
*
*
* @type {(!Date|number|undefined)}
*/
Options.Cookie.prototype.sameSite;

/**
* An interface for managing the current window.
*
Expand Down
28 changes: 28 additions & 0 deletions javascript/node/selenium-webdriver/test/cookie_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,26 @@ suite(function(env) {
await driver.sleep(expirationDelay);
await assertHasCookies();
});

it('can add same site cookie property to `Strict`', async function() {
let cookie = createSameSiteCookieSpec('Strict');
let childUrl = fileserver.whereIs('child/childPage.html');
await driver.get(childUrl);
await driver.manage().addCookie(cookie);
await driver.manage().getCookie(cookie.name).then(function(actual) {
assert.equal(actual.sameSite, "Strict");
});
});

it('can add same site cookie property to `Lax`', async function() {
let cookie = createSameSiteCookieSpec('Lax');
let childUrl = fileserver.whereIs('child/childPage.html');
await driver.get(childUrl);
await driver.manage().addCookie(cookie);
await driver.manage().getCookie(cookie.name).then(function(actual) {
harsha509 marked this conversation as resolved.
Show resolved Hide resolved
assert.equal(actual.sameSite, "Lax");
});
});
});

function createCookieSpec(opt_options) {
Expand All @@ -181,6 +201,14 @@ suite(function(env) {
return spec;
}

function createSameSiteCookieSpec(sameSiteVal) {
return {
name: getRandomString(),
value: getRandomString(),
sameSite: sameSiteVal
};
}

function buildCookieMap(cookies) {
var map = {};
cookies.forEach(function(cookie) {
Expand Down