From c3c044a66efdb8894d8d956a6b7633887e5df6db Mon Sep 17 00:00:00 2001 From: viralsinha Date: Wed, 17 Jun 2020 15:46:05 -0500 Subject: [PATCH 1/4] Returned null in case of empty cookie value --- lib/cookie.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/cookie.js b/lib/cookie.js index a042893e..9e385bf5 100644 --- a/lib/cookie.js +++ b/lib/cookie.js @@ -1081,6 +1081,9 @@ class CookieJar { if (typeof options === "function") { cb = options; options = {}; + + if(typeof cookie === 'string' && cookie.length <= 0 ) + return cb(null); } const host = canonicalDomain(context.hostname); From cb6cdf98b8ad547ff5bc69eaac6e1d51b37e3ee0 Mon Sep 17 00:00:00 2001 From: Viral Sinha Date: Thu, 10 Sep 2020 14:57:28 -0500 Subject: [PATCH 2/4] Update lib/cookie.js Co-authored-by: Andrew Waterman --- lib/cookie.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cookie.js b/lib/cookie.js index 1277b379..93eca295 100644 --- a/lib/cookie.js +++ b/lib/cookie.js @@ -1100,7 +1100,7 @@ class CookieJar { cb = options; options = {}; - if(typeof cookie === 'string' && cookie.length <= 0 ) + if(!validators.isNonEmptyString(cookie)) return cb(null); } validators.validate(validators.isFunction(cb), cb); From 8b753a4fd00377742b3d7916b7198e4fd30879b2 Mon Sep 17 00:00:00 2001 From: Mike de Libero Date: Tue, 24 Nov 2020 10:07:20 -0800 Subject: [PATCH 3/4] Added in a test and fixed a few issues with the if-statement based on testing --- lib/cookie.js | 6 ++++-- test/cookie_jar_test.js | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/cookie.js b/lib/cookie.js index 93eca295..331dadf0 100644 --- a/lib/cookie.js +++ b/lib/cookie.js @@ -1099,10 +1099,12 @@ class CookieJar { if (validators.isFunction(options)) { cb = options; options = {}; + } - if(!validators.isNonEmptyString(cookie)) - return cb(null); + if(!validators.isNonEmptyString(cookie) && !validators.isObject(cookie)) { + return cb(null); } + validators.validate(validators.isFunction(cb), cb); const host = canonicalDomain(context.hostname); diff --git a/test/cookie_jar_test.js b/test/cookie_jar_test.js index 8b8af0a5..2d3eb274 100644 --- a/test/cookie_jar_test.js +++ b/test/cookie_jar_test.js @@ -686,4 +686,21 @@ vows } } }) + .addBatch({ + "Issue #197 - CookieJar().setCookie crashes when empty cookie is passed": { + "with missing parameters": { + topic: function() { + const jar = new tough.CookieJar(); + jar.setCookie( + '', + "https://google.com", + this.callback + ); + }, + "results in a error being returned because of missing parameters": function(err, cookies) { + assert(cookies == undefined); + } + } + } + }) .export(module); From dc95a906dcf4f11096a043b1deab22d12a439fa0 Mon Sep 17 00:00:00 2001 From: Mike de Libero Date: Tue, 24 Nov 2020 12:21:00 -0800 Subject: [PATCH 4/4] Changes based on review and fixing the if-statement so a test does not fail --- lib/cookie.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/cookie.js b/lib/cookie.js index 331dadf0..1f8fa9e0 100644 --- a/lib/cookie.js +++ b/lib/cookie.js @@ -1101,12 +1101,12 @@ class CookieJar { options = {}; } - if(!validators.isNonEmptyString(cookie) && !validators.isObject(cookie)) { + validators.validate(validators.isFunction(cb), cb); + + if(!validators.isNonEmptyString(cookie) && !validators.isObject(cookie) && ( cookie instanceof String && cookie.length == 0)) { return cb(null); } - validators.validate(validators.isFunction(cb), cb); - const host = canonicalDomain(context.hostname); const loose = options.loose || this.enableLooseMode;