Skip to content

Add cookie exception when dot exists before domain name #4861

Closed
@alexgmuir

Description

@alexgmuir

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);

Activity

p0deje

p0deje commented on Oct 16, 2017

@p0deje
Member
  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.
added
J-awaiting answerQuestion asked of user; a reply moves it to triage again
on Oct 16, 2017
p0deje

p0deje commented on Oct 16, 2017

@p0deje
Member

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).

removed
J-awaiting answerQuestion asked of user; a reply moves it to triage again
on Oct 16, 2017
alexgmuir

alexgmuir commented on Oct 16, 2017

@alexgmuir
Author
Be5yond

Be5yond commented on Nov 9, 2017

@Be5yond

@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

dbstraffin

dbstraffin commented on Feb 16, 2018

@dbstraffin

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)
locked and limited conversation to collaborators on Aug 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @p0deje@alexgmuir@dbstraffin@Be5yond

        Issue actions

          Add cookie exception when dot exists before domain name · Issue #4861 · SeleniumHQ/selenium