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: properly bubble up cookie creation failure message #37596

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
15 changes: 10 additions & 5 deletions shell/browser/api/electron_api_cookies.cc
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ std::string InclusionStatusToString(net::CookieInclusionStatus status) {
return "Failed to parse cookie";
if (status.HasExclusionReason(
net::CookieInclusionStatus::EXCLUDE_INVALID_DOMAIN))
return "Failed to get cookie domain";
return "Failed to set cookie with an invalid domain attribute";
if (status.HasExclusionReason(
net::CookieInclusionStatus::EXCLUDE_INVALID_PREFIX))
return "Failed because the cookie violated prefix rules.";
Expand Down Expand Up @@ -315,19 +315,24 @@ v8::Local<v8::Promise> Cookies::Set(v8::Isolate* isolate,
return handle;
}

net::CookieInclusionStatus status;
auto canonical_cookie = net::CanonicalCookie::CreateSanitizedCookie(
url, name ? *name : "", value ? *value : "", domain ? *domain : "",
path ? *path : "", ParseTimeProperty(details.FindDouble("creationDate")),
ParseTimeProperty(details.FindDouble("expirationDate")),
ParseTimeProperty(details.FindDouble("lastAccessDate")), secure,
http_only, same_site, net::COOKIE_PRIORITY_DEFAULT, same_party,
absl::nullopt);
absl::nullopt, &status);

if (!canonical_cookie || !canonical_cookie->IsCanonical()) {
promise.RejectWithErrorMessage(
InclusionStatusToString(net::CookieInclusionStatus(
net::CookieInclusionStatus::EXCLUDE_FAILURE_TO_STORE)));
promise.RejectWithErrorMessage(InclusionStatusToString(
!status.IsInclude()
? status
: net::CookieInclusionStatus(
net::CookieInclusionStatus::EXCLUDE_FAILURE_TO_STORE)));
return handle;
}

net::CookieOptions options;
if (http_only) {
options.set_include_httponly();
Expand Down
10 changes: 10 additions & 0 deletions spec/api-net-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,16 @@ describe('net module', () => {
expect(cookies[0].name).to.equal('cookie2');
});

it('throws when an invalid domain is passed', async () => {
const sess = session.fromPartition(`cookie-tests-${Math.random()}`);

await expect(sess.cookies.set({
url: 'https://electronjs.org',
domain: 'wssss.iamabaddomain.fun',
name: 'cookie1'
})).to.eventually.be.rejectedWith(/Failed to set cookie with an invalid domain attribute/);
});

it('should be able correctly filter out cookies that are session', async () => {
const sess = session.fromPartition(`cookie-tests-${Math.random()}`);

Expand Down
4 changes: 2 additions & 2 deletions spec/api-session-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ describe('session module', () => {

await expect(
cookies.set({ url: '', name, value })
).to.eventually.be.rejectedWith('Failed to get cookie domain');
).to.eventually.be.rejectedWith('Failed to set cookie with an invalid domain attribute');
});

it('yields an error when setting a cookie with an invalid URL', async () => {
Expand All @@ -139,7 +139,7 @@ describe('session module', () => {

await expect(
cookies.set({ url: 'asdf', name, value })
).to.eventually.be.rejectedWith('Failed to get cookie domain');
).to.eventually.be.rejectedWith('Failed to set cookie with an invalid domain attribute');
});

it('should overwrite previous cookies', async () => {
Expand Down