Skip to content

Commit

Permalink
Merge pull request #1811 from Sebmaster/malformed-cookies
Browse files Browse the repository at this point in the history
Enable loose cookie parsing in tough-cookie
  • Loading branch information
simov committed Oct 11, 2015
2 parents 655d05c + 488fc47 commit 02c4b3a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/cookies.js
Expand Up @@ -13,13 +13,13 @@ exports.parse = function(str) {
if (typeof str !== 'string') {
throw new Error('The cookie function only accepts STRING as param')
}
return Cookie.parse(str)
return Cookie.parse(str, {loose: true})
}

// Adapt the sometimes-Async api of tough.CookieJar to our requirements
function RequestJar(store) {
var self = this
self._jar = new CookieJar(store)
self._jar = new CookieJar(store, {looseMode: true})
}
RequestJar.prototype.setCookie = function(cookieOrStr, uri, options) {
var self = this
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -32,7 +32,7 @@
"node-uuid": "~1.4.3",
"qs": "~5.2.0",
"tunnel-agent": "~0.4.1",
"tough-cookie": "~2.1.0",
"tough-cookie": "~2.2.0",
"http-signature": "~0.11.0",
"oauth-sign": "~0.8.0",
"hawk": "~3.1.0",
Expand Down
30 changes: 30 additions & 0 deletions tests/test-cookies.js
Expand Up @@ -6,11 +6,14 @@ var http = require('http')


var validUrl = 'http://localhost:6767/valid'
, malformedUrl = 'http://localhost:6767/malformed'
, invalidUrl = 'http://localhost:6767/invalid'

var server = http.createServer(function (req, res) {
if (req.url === '/valid') {
res.setHeader('set-cookie', 'foo=bar')
} else if (req.url === '/malformed') {
res.setHeader('set-cookie', 'foo')
} else if (req.url === '/invalid') {
res.setHeader('set-cookie', 'foo=bar; Domain=foo.com')
}
Expand All @@ -30,6 +33,13 @@ tape('simple cookie creation', function(t) {
t.end()
})

tape('simple malformed cookie creation', function(t) {
var cookie = request.cookie('foo')
t.equals(cookie.key, '')
t.equals(cookie.value, 'foo')
t.end()
})

tape('after server sends a cookie', function(t) {
var jar1 = request.jar()
request({
Expand All @@ -50,6 +60,26 @@ tape('after server sends a cookie', function(t) {
})
})

tape('after server sends a malformed cookie', function(t) {
var jar = request.jar()
request({
method: 'GET',
url: malformedUrl,
jar: jar
},
function (error, response, body) {
t.equal(error, null)
t.equal(jar.getCookieString(malformedUrl), 'foo')
t.equal(body, 'okay')

var cookies = jar.getCookies(malformedUrl)
t.equal(cookies.length, 1)
t.equal(cookies[0].key, '')
t.equal(cookies[0].value, 'foo')
t.end()
})
})

tape('after server sends a cookie for a different domain', function(t) {
var jar2 = request.jar()
request({
Expand Down

0 comments on commit 02c4b3a

Please sign in to comment.