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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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,38 @@
{
"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": "nonexistentKey",
"action": "delete"
}
}
]
}
]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!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>
document.cookie = 'keyToBeDeleted="valueToBeDeleted"'
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 nonexistentKey = hasCookie('nonexistentKey')
const otherKey = hasCookie('otherKey')

return [
{ name: 'specified cookie entry deleted', result: specifiedKey, 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 @@
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 @@
});
}

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

Check failure on line 594 in injected/src/features/web-compat.js

View workflow job for this annotation

GitHub Actions / unit (ubuntu-latest)

Property 'modifyCookies' does not exist on type 'Partial<FullWebCompatOptions>'.
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