refactor: use url::DomainIs() to check cookie domains (#43262)

* test: add tests to exercise pre-exsiting cookie domain matching behavior

* refactor: use url::DomainIs() to match cookie domains

* docs: fix typo
This commit is contained in:
Charles Kerr 2024-08-09 18:35:18 -05:00 committed by GitHub
parent c4dfff9844
commit c35739d60d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 56 additions and 20 deletions

View file

@ -1,4 +1,5 @@
import { expect } from 'chai';
import * as crypto from 'node:crypto';
import * as http from 'node:http';
import * as https from 'node:https';
import * as path from 'node:path';
@ -126,6 +127,54 @@ describe('session module', () => {
expect(cs.some(c => c.name === name && c.value === value)).to.equal(true);
});
it('does not match on empty domain filter strings', async () => {
const { cookies } = session.defaultSession;
const name = crypto.randomBytes(20).toString('hex');
const value = '1';
const url = 'https://microsoft.com/';
await cookies.set({ url, name, value });
const cs = await cookies.get({ domain: '' });
expect(cs.some(c => c.name === name && c.value === value)).to.equal(false);
cookies.remove(url, name);
});
it('gets domain-equal cookies', async () => {
const { cookies } = session.defaultSession;
const name = crypto.randomBytes(20).toString('hex');
const value = '1';
const url = 'https://microsoft.com/';
await cookies.set({ url, name, value });
const cs = await cookies.get({ domain: 'microsoft.com' });
expect(cs.some(c => c.name === name && c.value === value)).to.equal(true);
cookies.remove(url, name);
});
it('gets domain-inclusive cookies', async () => {
const { cookies } = session.defaultSession;
const name = crypto.randomBytes(20).toString('hex');
const value = '1';
const url = 'https://subdomain.microsoft.com/';
await cookies.set({ url, name, value });
const cs = await cookies.get({ domain: 'microsoft.com' });
expect(cs.some(c => c.name === name && c.value === value)).to.equal(true);
cookies.remove(url, name);
});
it('omits domain-exclusive cookies', async () => {
const { cookies } = session.defaultSession;
const name = crypto.randomBytes(20).toString('hex');
const value = '1';
const url = 'https://microsoft.com';
await cookies.set({ url, name, value });
const cs = await cookies.get({ domain: 'subdomain.microsoft.com' });
expect(cs.some(c => c.name === name && c.value === value)).to.equal(false);
cookies.remove(url, name);
});
it('rejects when setting a cookie with missing required fields', async () => {
const { cookies } = session.defaultSession;
const name = '1';