Skip to content

Cookiestore API protection #766

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
57 changes: 51 additions & 6 deletions src/features/cookie.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { postDebugMessage, getStackTraceOrigins, getStack, isBeingFramed, isThirdPartyFrame, getTabHostname, matchHostname } from '../utils.js'
import { postDebugMessage, getStackTraceOrigins, getStack, isBeingFramed, isThirdPartyFrame, getTabHostname, matchHostname, DDGProxy, DDGReflect } from '../utils.js'
import { Cookie } from '../cookie.js'
import ContentFeature from '../content-feature.js'
import { isTrackerOrigin } from '../trackers.js'
Expand Down Expand Up @@ -93,6 +93,13 @@ function isNonTrackingCookie () {
return cookiePolicy.isFrame && !cookiePolicy.isTracker && cookiePolicy.isThirdPartyFrame
}

function getPolicy () {
const { policy, trackerPolicy } = cookiePolicy
const stack = getStack()
const scriptOrigins = getStackTraceOrigins(stack)
return isFirstPartyTrackerScript(scriptOrigins) ? trackerPolicy : policy
}

export default class CookieFeature extends ContentFeature {
load () {
if (this.documentOriginIsTracker) {
Expand Down Expand Up @@ -191,11 +198,8 @@ export default class CookieFeature extends ContentFeature {
try {
// wait for config before doing same-site tests
loadPolicyThen(() => {
const { shouldBlock, policy, trackerPolicy } = cookiePolicy
const stack = getStack()
const scriptOrigins = getStackTraceOrigins(stack)
const chosenPolicy = isFirstPartyTrackerScript(scriptOrigins) ? trackerPolicy : policy
if (!shouldBlock) {
const chosenPolicy = getPolicy()
if (!cookiePolicy.shouldBlock) {
debugHelper('ignore', 'disabled', setCookieContext)
return
}
Expand Down Expand Up @@ -230,6 +234,47 @@ export default class CookieFeature extends ContentFeature {
set: setCookiePolicy,
get: getCookiePolicy
})

if (globalThis.CookieStore) {
// @ts-expect-error - error 'CookieStore' is not defined no-undef
// eslint-disable-next-line no-undef
const proxy = new DDGProxy(this, CookieStore.prototype, 'set', {
async apply (target, thisArg, args) {
let setCookieContext = null
if (args.length === 0 || !args[0].expires) {
return DDGReflect.apply(target, thisArg, args)
}
await loadPolicy
const chosenPolicy = getPolicy()

if (cookiePolicy.debug) {
const stack = getStack()
const cookie = args[0]
setCookieContext = {
stack,
value: `${cookie.name}=${cookie.value}`
}
}
console.log('xxx', cookiePolicy, setCookieContext)
if (!cookiePolicy.shouldBlock) {
debugHelper('ignore', 'disabled (cookieStore)', setCookieContext)
return DDGReflect.apply(target, thisArg, args)
}
if (shouldBlockTrackingCookie() || shouldBlockNonTrackingCookie()) {
debugHelper('block', '3p frame (cookieStore)', setCookieContext)
return undefined
}
if (args[0].expires > Date.now() + (chosenPolicy.threshold * 1000)) {
debugHelper('restrict', 'expiry (cookieStore)', setCookieContext)
args[0].expires = Date.now() + (chosenPolicy.maxAge * 1000)
} else {
debugHelper('ignore', 'expiry (cookieStore)', setCookieContext)
}
return DDGReflect.apply(target, thisArg, args)
}
})
proxy.overload()
}
}

init (args) {
Expand Down