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

Enable loose cookie parsing in tough-cookie #1811

Merged
merged 1 commit into from Oct 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.1.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