Skip to content

Commit

Permalink
Ignore invalid Set-Cookie values (#6948)
Browse files Browse the repository at this point in the history
* add invalid header char repro

* ignore tough-cookie/automation failures

see #6890 (comment)

* restore snapshot

* Update packages/server/lib/request.coffee

* Update packages/server/lib/request.coffee

Co-Authored-By: Jennifer Shehane <jennifer@cypress.io>

Co-authored-by: Jennifer Shehane <jennifer@cypress.io>
  • Loading branch information
flotwig and jennifer-shehane authored Apr 8, 2020
1 parent cb6f0fd commit 0046cc5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
12 changes: 7 additions & 5 deletions packages/server/__snapshots__/2_cookies_spec.coffee.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ exports['e2e cookies with baseurl'] = `
✓ can set and clear cookie
in a cy.visit
✓ can successfully send cookies as a Cookie header
✓ ignores invalid set-cookie headers that contain control chars
with Domain = superdomain
✓ is set properly with no redirects
✓ is set properly with redirects
Expand All @@ -48,6 +49,7 @@ exports['e2e cookies with baseurl'] = `
✓ can set cookies on lots of redirects, ending with same domain
in a cy.request
✓ can successfully send cookies as a Cookie header
✓ ignores invalid set-cookie headers that contain control chars
with Domain = superdomain
✓ is set properly with no redirects
✓ is set properly with redirects
Expand All @@ -63,14 +65,14 @@ exports['e2e cookies with baseurl'] = `
✓ can set cookies on lots of redirects, ending with same domain
30 passing
32 passing
(Results)
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Tests: 30
│ Passing: 30
│ Tests: 32
│ Passing: 32
│ Failing: 0 │
│ Pending: 0 │
│ Skipped: 0 │
Expand All @@ -94,9 +96,9 @@ exports['e2e cookies with baseurl'] = `
Spec Tests Passing Failing Pending Skipped
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
│ ✔ cookies_spec_baseurl.coffee XX:XX 30 30 - - - │
│ ✔ cookies_spec_baseurl.coffee XX:XX 32 32 - - - │
└────────────────────────────────────────────────────────────────────────────────────────────────┘
✔ All specs passed! XX:XX 30 30 - - -
✔ All specs passed! XX:XX 32 32 - - -
`
Expand Down
18 changes: 14 additions & 4 deletions packages/server/lib/request.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,12 @@ module.exports = (options = {}) ->

debug('parsing cookie %o', { cyCookie, toughCookie: cookie })

if not cookie
## ignore invalid cookies (same as browser behavior)
## https://github.com/cypress-io/cypress/issues/6890
debug('tough-cookie failed to parse, ignoring')
return

cookie.name = cookie.key

if not cookie.domain
Expand All @@ -504,14 +510,18 @@ module.exports = (options = {}) ->
if isFinite(expiry)
cookie.expiry = expiry / 1000

cookie.sameSite = convertSameSiteToughToExtension(cookie.sameSite, cyCookie)

cookie = _.pick(cookie, SERIALIZABLE_COOKIE_PROPS)

if expiry <= 0
return automationFn('clear:cookie', cookie)
automationCmd = 'set:cookie'

cookie.sameSite = convertSameSiteToughToExtension(cookie.sameSite, cyCookie)
if expiry <= 0
automationCmd = 'clear:cookie'

automationFn('set:cookie', cookie)
automationFn(automationCmd, cookie)
.catch (err) ->
debug('automation threw an error during cookie change %o', { automationCmd, cyCookie, cookie, err })

sendStream: (headers, automationFn, options = {}) ->
_.defaults options, {
Expand Down
11 changes: 11 additions & 0 deletions packages/server/test/e2e/2_cookies_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ onServer = (app) ->
res.setHeader("Set-Cookie", header)
res.type('html').end()

app.get "/invalidControlCharCookie", (req, res) ->
## `http` lib throws an error if we use .setHeader to set this
res.connection.end("""
HTTP/1.1 200 OK
Content-Type: text/html
Set-Cookie: ___utmvaFvuoaRv=TkE\u0001sCvZ; path=/; Max-Age=900
Set-Cookie: _valid=true; path=/; Max-Age=900
foo
""")

haveRoot = !process.env.USE_HIGH_PORTS && process.geteuid() == 0

if not haveRoot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,18 @@ describe "cookies", ->
c: 's:PtCc3lNiuqN0AtR9ffgKUnUsDzR5n_4B.qzFDJDvqx8PZNvmOkmcexDs7fRJLOel56Z8Ii6PL+Fo'
})

## https://github.com/cypress-io/cypress/issues/6890
it "ignores invalid set-cookie headers that contain control chars", ->
cy[cmd]("/invalidControlCharCookie")

cy.request("/requestCookies")
.then (res) ->
return res.body
.then (cookies) ->
expect(cookies).to.deep.eq({
_valid: 'true'
})

context "with Domain = superdomain", ->
requestCookiesUrl = "#{Cypress.config('baseUrl')}/requestCookies"
setDomainCookieUrl = "#{Cypress.config('baseUrl')}/setDomainCookie?domain=#{setCookieDomain}"
Expand Down

3 comments on commit 0046cc5

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 0046cc5 Apr 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

You can install this pre-release platform-specific build using instructions at https://on.cypress.io/installing-cypress#Install-pre-release-version.

You will need to use custom CYPRESS_INSTALL_BINARY url and install Cypress using an url instead of the version.

export CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/4.3.1/linux-x64/circle-develop-0046cc55129a37604ac3e47e8a84b1ed5a4d9181-298776/cypress.zip
npm install https://cdn.cypress.io/beta/npm/4.3.1/circle-develop-0046cc55129a37604ac3e47e8a84b1ed5a4d9181-298765/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 0046cc5 Apr 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AppVeyor has built the win32 ia32 version of the Test Runner.

You can install this pre-release platform-specific build using instructions at https://on.cypress.io/installing-cypress#Install-pre-release-version.

You will need to use custom CYPRESS_INSTALL_BINARY url and install Cypress using an url instead of the version.

Instructions are included below, depending on the shell you are using.

In Command Prompt (cmd.exe):

set CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/4.3.1/win32-ia32/appveyor-develop-0046cc55129a37604ac3e47e8a84b1ed5a4d9181-32043384/cypress.zip
npm install https://cdn.cypress.io/beta/npm/4.3.1/appveyor-develop-0046cc55129a37604ac3e47e8a84b1ed5a4d9181-32043384/cypress.tgz

In PowerShell:

$env:CYPRESS_INSTALL_BINARY = https://cdn.cypress.io/beta/binary/4.3.1/win32-ia32/appveyor-develop-0046cc55129a37604ac3e47e8a84b1ed5a4d9181-32043384/cypress.zip
npm install https://cdn.cypress.io/beta/npm/4.3.1/appveyor-develop-0046cc55129a37604ac3e47e8a84b1ed5a4d9181-32043384/cypress.tgz

In Git Bash:

export CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/4.3.1/win32-ia32/appveyor-develop-0046cc55129a37604ac3e47e8a84b1ed5a4d9181-32043384/cypress.zip
npm install https://cdn.cypress.io/beta/npm/4.3.1/appveyor-develop-0046cc55129a37604ac3e47e8a84b1ed5a4d9181-32043384/cypress.tgz

Using cross-env:

If the above commands do not work for you, you can also try using cross-env:

npm i -g cross-env
cross-env CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/4.3.1/win32-ia32/appveyor-develop-0046cc55129a37604ac3e47e8a84b1ed5a4d9181-32043384/cypress.zip npm install https://cdn.cypress.io/beta/npm/4.3.1/appveyor-develop-0046cc55129a37604ac3e47e8a84b1ed5a4d9181-32043384/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 0046cc5 Apr 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AppVeyor has built the win32 x64 version of the Test Runner.

You can install this pre-release platform-specific build using instructions at https://on.cypress.io/installing-cypress#Install-pre-release-version.

You will need to use custom CYPRESS_INSTALL_BINARY url and install Cypress using an url instead of the version.

Instructions are included below, depending on the shell you are using.

In Command Prompt (cmd.exe):

set CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/4.3.1/win32-x64/appveyor-develop-0046cc55129a37604ac3e47e8a84b1ed5a4d9181-32043384/cypress.zip
npm install https://cdn.cypress.io/beta/npm/4.3.1/appveyor-develop-0046cc55129a37604ac3e47e8a84b1ed5a4d9181-32043384/cypress.tgz

In PowerShell:

$env:CYPRESS_INSTALL_BINARY = https://cdn.cypress.io/beta/binary/4.3.1/win32-x64/appveyor-develop-0046cc55129a37604ac3e47e8a84b1ed5a4d9181-32043384/cypress.zip
npm install https://cdn.cypress.io/beta/npm/4.3.1/appveyor-develop-0046cc55129a37604ac3e47e8a84b1ed5a4d9181-32043384/cypress.tgz

In Git Bash:

export CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/4.3.1/win32-x64/appveyor-develop-0046cc55129a37604ac3e47e8a84b1ed5a4d9181-32043384/cypress.zip
npm install https://cdn.cypress.io/beta/npm/4.3.1/appveyor-develop-0046cc55129a37604ac3e47e8a84b1ed5a4d9181-32043384/cypress.tgz

Using cross-env:

If the above commands do not work for you, you can also try using cross-env:

npm i -g cross-env
cross-env CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/4.3.1/win32-x64/appveyor-develop-0046cc55129a37604ac3e47e8a84b1ed5a4d9181-32043384/cypress.zip npm install https://cdn.cypress.io/beta/npm/4.3.1/appveyor-develop-0046cc55129a37604ac3e47e8a84b1ed5a4d9181-32043384/cypress.tgz

Please sign in to comment.