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

fix: throw error on invalid URLs when setting cookie #18911

Closed
wants to merge 2 commits into from
Closed
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
22 changes: 11 additions & 11 deletions atom/browser/api/atom_api_cookies.cc
Expand Up @@ -195,13 +195,13 @@ void FlushCookieStoreOnIOThread(
void SetCookieOnIO(scoped_refptr<net::URLRequestContextGetter> getter,
std::unique_ptr<base::DictionaryValue> details,
const Cookies::SetCallback& callback) {
std::string url, name, value, domain, path;
std::string url_string, name, value, domain, path;
bool secure = false;
bool http_only = false;
double creation_date;
double expiration_date;
double last_access_date;
details->GetString("url", &url);
details->GetString("url", &url_string);
details->GetString("name", &name);
details->GetString("value", &value);
details->GetString("domain", &domain);
Expand Down Expand Up @@ -229,22 +229,22 @@ void SetCookieOnIO(scoped_refptr<net::URLRequestContextGetter> getter,
? base::Time::UnixEpoch()
: base::Time::FromDoubleT(last_access_date);
}

std::unique_ptr<net::CanonicalCookie> canonical_cookie(
net::CanonicalCookie::CreateSanitizedCookie(
GURL(url), name, value, domain, path, creation_time, expiration_time,
last_access_time, secure, http_only,
net::CookieSameSite::DEFAULT_MODE, net::COOKIE_PRIORITY_DEFAULT));
auto completion_callback = base::BindOnce(OnSetCookie, callback);
if (!canonical_cookie || !canonical_cookie->IsCanonical()) {
GURL url(url_string);
if (!url.is_valid()) {
std::move(completion_callback).Run(false);
return;
}
if (url.empty()) {
if (name.empty()) {
std::move(completion_callback).Run(false);
return;
}
if (name.empty()) {
std::unique_ptr<net::CanonicalCookie> canonical_cookie(
net::CanonicalCookie::CreateSanitizedCookie(
url, name, value, domain, path, creation_time, expiration_time,
last_access_time, secure, http_only,
net::CookieSameSite::DEFAULT_MODE, net::COOKIE_PRIORITY_DEFAULT));
if (!canonical_cookie || !canonical_cookie->IsCanonical()) {
std::move(completion_callback).Run(false);
return;
}
Expand Down
12 changes: 12 additions & 0 deletions spec/api-session-spec.js
Expand Up @@ -103,6 +103,18 @@ describe('session module', () => {
})
})

it('yields an error when setting a cookie with an invalid URL', (done) => {
session.defaultSession.cookies.set({
url: 'asdf',
name: '1',
value: '1'
}, (error) => {
assert(error, 'Should have an error')
assert.strictEqual(error.message, 'Setting cookie failed')
done()
})
})

it('should over-write the existent cookie', (done) => {
session.defaultSession.cookies.set({
url,
Expand Down