fix: properly bubble up cookie creation failure message (#37586)
This commit is contained in:
parent
48d0b09ad9
commit
b8f970c1c7
3 changed files with 22 additions and 7 deletions
|
@ -180,7 +180,7 @@ std::string InclusionStatusToString(net::CookieInclusionStatus status) {
|
||||||
return "Failed to parse cookie";
|
return "Failed to parse cookie";
|
||||||
if (status.HasExclusionReason(
|
if (status.HasExclusionReason(
|
||||||
net::CookieInclusionStatus::EXCLUDE_INVALID_DOMAIN))
|
net::CookieInclusionStatus::EXCLUDE_INVALID_DOMAIN))
|
||||||
return "Failed to get cookie domain";
|
return "Failed to set cookie with an invalid domain attribute";
|
||||||
if (status.HasExclusionReason(
|
if (status.HasExclusionReason(
|
||||||
net::CookieInclusionStatus::EXCLUDE_INVALID_PREFIX))
|
net::CookieInclusionStatus::EXCLUDE_INVALID_PREFIX))
|
||||||
return "Failed because the cookie violated prefix rules.";
|
return "Failed because the cookie violated prefix rules.";
|
||||||
|
@ -318,19 +318,24 @@ v8::Local<v8::Promise> Cookies::Set(v8::Isolate* isolate,
|
||||||
return handle;
|
return handle;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
net::CookieInclusionStatus status;
|
||||||
auto canonical_cookie = net::CanonicalCookie::CreateSanitizedCookie(
|
auto canonical_cookie = net::CanonicalCookie::CreateSanitizedCookie(
|
||||||
url, name ? *name : "", value ? *value : "", domain ? *domain : "",
|
url, name ? *name : "", value ? *value : "", domain ? *domain : "",
|
||||||
path ? *path : "", ParseTimeProperty(details.FindDouble("creationDate")),
|
path ? *path : "", ParseTimeProperty(details.FindDouble("creationDate")),
|
||||||
ParseTimeProperty(details.FindDouble("expirationDate")),
|
ParseTimeProperty(details.FindDouble("expirationDate")),
|
||||||
ParseTimeProperty(details.FindDouble("lastAccessDate")), secure,
|
ParseTimeProperty(details.FindDouble("lastAccessDate")), secure,
|
||||||
http_only, same_site, net::COOKIE_PRIORITY_DEFAULT, same_party,
|
http_only, same_site, net::COOKIE_PRIORITY_DEFAULT, same_party,
|
||||||
absl::nullopt);
|
absl::nullopt, &status);
|
||||||
|
|
||||||
if (!canonical_cookie || !canonical_cookie->IsCanonical()) {
|
if (!canonical_cookie || !canonical_cookie->IsCanonical()) {
|
||||||
promise.RejectWithErrorMessage(
|
promise.RejectWithErrorMessage(InclusionStatusToString(
|
||||||
InclusionStatusToString(net::CookieInclusionStatus(
|
!status.IsInclude()
|
||||||
net::CookieInclusionStatus::EXCLUDE_FAILURE_TO_STORE)));
|
? status
|
||||||
|
: net::CookieInclusionStatus(
|
||||||
|
net::CookieInclusionStatus::EXCLUDE_FAILURE_TO_STORE)));
|
||||||
return handle;
|
return handle;
|
||||||
}
|
}
|
||||||
|
|
||||||
net::CookieOptions options;
|
net::CookieOptions options;
|
||||||
if (http_only) {
|
if (http_only) {
|
||||||
options.set_include_httponly();
|
options.set_include_httponly();
|
||||||
|
|
|
@ -903,6 +903,16 @@ describe('net module', () => {
|
||||||
expect(cookies[0].name).to.equal('cookie2');
|
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 () => {
|
it('should be able correctly filter out cookies that are session', async () => {
|
||||||
const sess = session.fromPartition(`cookie-tests-${Math.random()}`);
|
const sess = session.fromPartition(`cookie-tests-${Math.random()}`);
|
||||||
|
|
||||||
|
|
|
@ -128,7 +128,7 @@ describe('session module', () => {
|
||||||
|
|
||||||
await expect(
|
await expect(
|
||||||
cookies.set({ url: '', name, value })
|
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 () => {
|
it('yields an error when setting a cookie with an invalid URL', async () => {
|
||||||
|
@ -138,7 +138,7 @@ describe('session module', () => {
|
||||||
|
|
||||||
await expect(
|
await expect(
|
||||||
cookies.set({ url: 'asdf', name, value })
|
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 () => {
|
it('should overwrite previous cookies', async () => {
|
||||||
|
|
Loading…
Reference in a new issue