selenium icon indicating copy to clipboard operation
selenium copied to clipboard

Add cookie exception when dot exists before domain name

Open alexgmuir opened this issue 9 years ago • 5 comments

Meta -

OS: Ubuntu 17.04 Selenium Version: 3.5.1 Browser: FireFox Browser Version: 55.0.3

Expected Behavior -

  1. Loaded the webpage http://www.cbc.ca
  2. Add a cookie with the domain .www.cbc.ca
  3. Cookie is added

Actual Behavior -

  1. Loaded the webpage http://www.cbc.ca
  2. Add a cookie with the domain .www.cbc.ca
  3. Exception is generated due to the dot (.) in front of the www.cbc.ca
amplitude_id_statswww.cbc.ca=eyJkZXZpY2VJZCI6IjMzZjA1NzU4LTc3ODMtNDBmOS05NDZkLWIyOWNhYzA0YTMyZVIiLCJ1c2VySWQiOm51bGwsIm9wdE91dCI6ZmFsc2UsInNlc3Npb25JZCI6MTUwNzgzNzAxMTU5OCwibGFzdEV2ZW50VGltZSI6MTUwNzgzNzAxMTYwMCwiZXZlbnRJZCI6MSwiaWRlbnRpZnlJZCI6MCwic2VxdWVuY2VOdW1iZXIiOjF9; path=/; **domain=.www.cbc.ca**
You may only set cookies for the current domain

Build info: version: '3.5.1', revision: '9c21bb67ef', time: '2017-08-17T15:26:08.955Z'
System info: host: 'ubuntu', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.10.0-35-generic', java.version: '1.8.0_144'

Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{moz:profile=/tmp/rust_mozprofile.Hp4phd3YgXqn, rotatable=false, timeouts={implicit=0.0, pageLoad=300000.0, script=30000.0}, pageLoadStrategy=normal, platform=ANY, specificationLevel=0.0, moz:accessibilityChecks=false, acceptInsecureCerts=false, browserVersion=55.0.3, platformVersion=4.10.0-35-generic, moz:processID=9650.0, browserName=firefox, javascriptEnabled=true, platformName=linux}]
Session ID: c720ed38-7226-458f-8a77-aed7336a1c37

org.openqa.selenium.InvalidCookieDomainException: You may only set cookies for the current domain
Build info: version: '3.5.1', revision: '9c21bb67ef', time: '2017-08-17T15:26:08.955Z'
System info: host: 'ubuntu', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.10.0-35-generic', java.version: '1.8.0_144'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{moz:profile=/tmp/rust_mozprofile.Hp4phd3YgXqn, rotatable=false, timeouts={implicit=0.0, pageLoad=300000.0, script=30000.0}, pageLoadStrategy=normal, platform=ANY, specificationLevel=0.0, moz:accessibilityChecks=false, acceptInsecureCerts=false, browserVersion=55.0.3, platformVersion=4.10.0-35-generic, moz:processID=9650.0, browserName=firefox, javascriptEnabled=true, platformName=linux}]
Session ID: c720ed38-7226-458f-8a77-aed7336a1c37
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:185)
	at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:120)
	at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
	at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
	at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
	at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:641)
	at org.openqa.selenium.remote.RemoteWebDriver$RemoteWebDriverOptions.addCookie(RemoteWebDriver.java:768)

Steps to reproduce -

    driver.get(cbcURL);
    driver.manage().addCookie(cbcCookie);

alexgmuir avatar Oct 12 '17 20:10 alexgmuir

  1. What driver/browser do you use?
  2. Does adding cookie to .cbc.ca work?
  3. WebDriver specification part about cookies is missing so I'm not sure how exactly it should behave when used like .www.cbc.ua.

p0deje avatar Oct 16 '17 04:10 p0deje

Actually, WebDriver spec defines the add cookie logic:

If ... cookie domain is not equal to the current browsing context’s active document’s domain, ..., return error with error code invalid argument.

So the behavior is defined. If you think it's incorrect one, please raise an issue for specification. Once the specification is changed, drivers will adopt the new behavior.

P.S. I also checked that you can add cookie with .cbc.ca domain and I believe that's what you actually want to achieve (i.e. adding cookies available to subdomains).

p0deje avatar Oct 16 '17 05:10 p0deje

Alex,

To be clear I was simply storing all cbc cookies on the first visit and adding all the cookies cbc.ca gave me back in the second session and I get that exception. If you think that should be the expected behavior, I respect your decision.

Regards Alex tech.jahtoe.com bafila.jahtoe.com

On Mon, Oct 16, 2017 at 5:07 AM, Alex Rodionov [email protected] wrote:

Closed #4861 https://github.com/SeleniumHQ/selenium/issues/4861.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/SeleniumHQ/selenium/issues/4861#event-1294114570, or mute the thread https://github.com/notifications/unsubscribe-auth/AGMa37rDUOK9MdcqW4C_5oKJsg_wmap3ks5ssuSbgaJpZM4P3jo3 .

alexgmuir avatar Oct 16 '17 11:10 alexgmuir

@p0deje, hello, I got exactly the same problem. here is some log for what happened in my case. any request access shop.mysite.com without access_token would be redirect to login.mysite.com. after I hit my login button. I paused the script. PS: I am using the pytest so selenium is a webdriver instance. (Pdb) selenium.get_cookies() [{'name': 'access_token', 'value': '23a3ec47-9feb-4898-90f0-3a540d6041c9', 'secure': False, 'httpOnly': False, 'path': '/', 'expiry': 1512812800, 'domain': '.mysite.com'}] (Pdb) cookie = selenium.get_cookies()[0] (Pdb) selenium.delete_all_cookies() (Pdb) selenium.add_cookie(cookie) (Pdb) selenium.get_cookies() [{'name': 'access_token', 'value': '23a3ec47-9feb-4898-90f0-3a540d6041c9', 'secure': False, 'httpOnly': False, 'path': '/', 'expiry': 1512812800, 'domain': 'shop.mysite.com'}]

any help would be appreciated.

OS: Ubuntu 17.04 Selenium Version: 3.7.0

Be5yond avatar Nov 09 '17 10:11 Be5yond

I have ths same issue (I can read a cookie but not set that same cookie via Selenium). It's easy to workaround using javascript.

Here is an example using python:

parts = ["{}={}".format(cookie['name'], cookie['value'])]
for k, v in cookie.items():
    if k in ['name', 'value']:
        continue
    parts.append("{}={}".format(k, v))

script = 'document.cookie = "{}";'.format('; '.join(parts))
driver.execute_script(script)

dbstraffin avatar Feb 16 '18 17:02 dbstraffin