Skip to content

Commit

Permalink
Support modifying cookies (#1384)
Browse files Browse the repository at this point in the history
* Support modifying cookies

* Add test for path
  • Loading branch information
jonathanKingston authored Jan 14, 2025
1 parent d302d8d commit 399ce49
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 206 deletions.
10 changes: 10 additions & 0 deletions injected/integration-test/pages.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,14 @@ test.describe('Test integration pages', () => {
`./integration-test/test-pages/webcompat/config/modify-localstorage.json`,
);
});

test('Properly modifies cookies', async ({ page }, testInfo) => {
// prettier-ignore
await testPage(
page,
testInfo,
'webcompat/pages/modify-cookies.html',
`./integration-test/test-pages/webcompat/config/modify-cookies.json`,
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"unprotectedTemporary": [],
"features": {
"webCompat": {
"exceptions": [],
"state": "enabled",
"settings": {
"modifyCookies": {
"state": "enabled",
"changes": []
},
"domains": [
{
"domain": ["localhost", "privacy-test-pages.site"],
"patchSettings": [
{
"op": "add",
"path": "/modifyCookies/changes/-",
"value": {
"key": "keyToBeDeleted",
"action": "delete"
}
},
{
"op": "add",
"path": "/modifyCookies/changes/-",
"value": {
"key": "pathCookie",
"action": "delete"
}
},
{
"op": "add",
"path": "/modifyCookies/changes/-",
"value": {
"key": "pathCookieWithPath",
"path": "/webcompat/pages/",
"action": "delete"
}
},
{
"op": "add",
"path": "/modifyCookies/changes/-",
"value": {
"key": "nonexistentKey",
"action": "delete"
}
}
]
}
]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Modify Cookies</title>
<link rel="stylesheet" href="../../shared/style.css">
<script>
// Current file path
let path = '/webcompat/pages/'
document.cookie = 'keyToBeDeleted="valueToBeDeleted"'
document.cookie = 'pathCookie="valueToBeDeleted"; path=' + path
document.cookie = 'pathCookieWithPath="valueToBeDeleted"; path=' + path
document.cookie = 'otherKey="valueToRemain"'
</script>
</head>
<body>
<script src="../../shared/utils.js"></script>
<p><a href="../index.html">[WebCompat]</a></p>

<p>This page verifies that cookie modifications work properly given the <a href="../config/modify-cookies.json">config</a>. At this time, only deletion is supported.</p>

<script>
// Copy from: https://stackoverflow.com/a/2144404
function hasCookie(name){
return document.cookie.split(';').some(c => {
return c.trim().startsWith(name + '=');
});
}

// eslint-disable-next-line no-undef
test('Only specified cookies should be removed', async () => {
const specifiedKey = hasCookie('keyToBeDeleted')
const pathKey = hasCookie('pathCookie')
const pathKey2 = hasCookie('pathCookie2')
const pathKey3 = hasCookie('pathCookieWithPath')
const nonexistentKey = hasCookie('nonexistentKey')
const otherKey = hasCookie('otherKey')

return [
{ name: 'specified cookie entry deleted', result: specifiedKey, expected: false },
{ name: 'specified cookie entry with path not deleted', result: pathKey, expected: true },
{ name: `specified cookie entry with correct path (${path}) deleted`, result: pathKey3, expected: false },
{ name: 'specified cookie entry that is not present on page load', result: nonexistentKey, expected: false },
{ name: 'other cookie entry untouched', result: otherKey, expected: true }
];
});

// eslint-disable-next-line no-undef
renderResults();
</script>
</body>
</html>
22 changes: 22 additions & 0 deletions injected/src/features/web-compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ export class WebCompat extends ContentFeature {
if (this.getFeatureSettingEnabled('modifyLocalStorage')) {
this.modifyLocalStorage();
}

if (this.getFeatureSettingEnabled('modifyCookies')) {
this.modifyCookies();
}
}

/** Shim Web Share API in Android WebView */
Expand Down Expand Up @@ -583,6 +587,24 @@ export class WebCompat extends ContentFeature {
});
}

/**
* Support for modifying cookies
*/
modifyCookies() {
/** @type {import('@duckduckgo/privacy-configuration/schema/features/webcompat').WebCompatSettings['modifyCookies']} */
const settings = this.getFeatureSetting('modifyCookies');

if (!settings || !settings.changes) return;

settings.changes.forEach((change) => {
if (change.action === 'delete') {
const pathValue = change.path ? `; path=${change.path}` : '';
const domainValue = change.domain ? `; domain=${change.domain}` : '';
document.cookie = `${change.key}=; expires=Thu, 01 Jan 1970 00:00:00 GMT${pathValue}${domainValue}`;
}
});
}

/**
* Support for proxying `window.webkit.messageHandlers`
*/
Expand Down
121 changes: 0 additions & 121 deletions injected/src/types/duckplayer-settings.d.ts

This file was deleted.

84 changes: 0 additions & 84 deletions injected/src/types/webcompat-settings.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"playwright.config.js",
"injected",
"injected/src/globals.d.ts",
"injected/src/features/duckplayer/duckplayer-settings.ts",
"typedoc.js",
".github/scripts"
],
Expand Down

0 comments on commit 399ce49

Please sign in to comment.