Skip to content

Commit

Permalink
fix(core): subdomain cookies not being removed
Browse files Browse the repository at this point in the history
  • Loading branch information
blakebyrnes committed Nov 23, 2022
1 parent 5a23f89 commit fad14db
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
fail-fast: false
matrix:
os: [macos-latest, windows-latest, ubuntu-latest]
node-version: [14, 16]
node-version: [14, 16, 18]
include:
- node-version: 16.x
os: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion client/lib/CookieStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default class CookieStorage {

public async key(index: number): Promise<string> {
const cookies = await this.getItems();
return Object.keys(cookies)[index];
return cookies[index]?.name;
}

public async clear(): Promise<void> {
Expand Down
16 changes: 15 additions & 1 deletion core/lib/FrameEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,20 @@ b) Use the UserProfile feature to set cookies for 1 or more domains before they'
}

public async removeCookie(name: string): Promise<boolean> {
const cookies = await this.getCookies();
for (const cookie of cookies) {
if (name === cookie.name) {
await this.session.browserContext.addCookies([
{
name,
value: '',
expires: 0,
domain: cookie.domain,
},
]);
return true;
}
}
await this.session.browserContext.addCookies([
{
name,
Expand Down Expand Up @@ -486,7 +500,7 @@ b) Use the UserProfile feature to set cookies for 1 or more domains before they'
return false;
}

public async onShadowDomPushed(payload:string): Promise<void> {
public async onShadowDomPushed(payload: string): Promise<void> {
await this.frame.evaluate(`window.checkForShadowRoot(${payload})`, true);
}

Expand Down
2 changes: 1 addition & 1 deletion docs/main/AdvancedClient/CookieStorage.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Gets the cookie with the given `name` equal to `keyName`.

### cookieStorage.key *(index)*

An integer representing the number of the key you want to get the name of. This is a zero-based index. NOTE: key is equivalent to the `name` of a cookie.
Gets the `name` of the cookie with the given `index`. This is a zero-based index. NOTE: key is equivalent to the `name` of a cookie.

#### **Arguments**:

Expand Down
57 changes: 48 additions & 9 deletions end-to-end/test/basic.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Helpers, Hero } from '@ulixee/hero-testing';
import Resource from '@ulixee/hero/lib/Resource';
import { Session } from '@ulixee/hero-core';

let koaServer;
beforeAll(async () => {
Expand Down Expand Up @@ -79,10 +80,7 @@ describe('basic Full Client tests', () => {
});

const hero = new Hero({
blockedResourceUrls: [
'42.css?x=foo',
'/baz/',
],
blockedResourceUrls: ['42.css?x=foo', '/baz/'],
});
Helpers.needsClosing.push(hero);

Expand Down Expand Up @@ -116,10 +114,7 @@ describe('basic Full Client tests', () => {
});

const hero = new Hero({
blockedResourceUrls: [
/.*\?x=foo/,
/\/baz\//,
],
blockedResourceUrls: [/.*\?x=foo/, /\/baz\//],
});
Helpers.needsClosing.push(hero);

Expand Down Expand Up @@ -192,7 +187,6 @@ describe('basic Full Client tests', () => {
it('can get and set cookies', async () => {
const hero = new Hero();
Helpers.needsClosing.push(hero);

koaServer.get('/cookies', ctx => {
ctx.cookies.set('Cookie1', 'This is a test', {
httpOnly: true,
Expand Down Expand Up @@ -230,6 +224,51 @@ describe('basic Full Client tests', () => {
const documentCookies = await hero.getJsValue('document.cookie');
expect(documentCookies).toBe('');
}
// test deleting a subdomain cookie
await cookieStorage.removeItem('Cookie1');
expect(await cookieStorage.length).toBe(0);
});

it('can get and set subdomain cookies', async () => {
const hero = new Hero();
Helpers.needsClosing.push(hero);

const session = Session.get(await hero.sessionId);
session.agent.mitmRequestSession.interceptorHandlers.push({
urls: ['https://ulixee.org'],
handlerFn(url, type, request, response) {
response.setHeader('Set-Cookie', [
'CookieMain=main; httpOnly',
'CookieSub=sub; domain=.ulixee.org',
]);
response.end(`<html lang='en'>
<body>
<h1>Page Title</h1>
</body>
</html>`);
return true;
},
});

await hero.goto(`https://ulixee.org`);
const cookieStorage = hero.activeTab.cookieStorage;
{
expect(await cookieStorage.length).toBe(2);
const cookie = await cookieStorage.getItem('CookieMain');
expect(cookie.expires).toBe(undefined);
expect(cookie.httpOnly).toBe(true);
const cookieSub = await cookieStorage.getItem('CookieSub');
expect(cookieSub.expires).toBe(undefined);
expect(cookieSub.domain).toBe('.ulixee.org');
// httponly not in doc
const documentCookies = await hero.getJsValue('document.cookie');
expect(documentCookies).toBe('CookieSub=sub');
}
// test deleting a subdomain cookie
await cookieStorage.removeItem('CookieSub');
expect(await cookieStorage.length).toBe(1);
await cookieStorage.removeItem('CookieMain');
expect(await cookieStorage.length).toBe(0);
});

it('should send a friendly message if trying to set cookies before a url is loaded', async () => {
Expand Down

0 comments on commit fad14db

Please sign in to comment.