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

Support modifying cookies #1384

Merged
merged 4 commits into from
Jan 14, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a test that confirms path is working as expected?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added test to check both states with path. I'd rather not add a second sub path test to validate some of the other cases (I don't think we need to validate the visibility of the cookie for example, because the behavior of cookie is path not set when deleting won't delete).

The same is true for domain but that's a bit more problematic as we intend to run these in a few places.

];
});

// 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
Loading