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

Fixes client DOM Cookie Poisoning #3982

Closed
wants to merge 13 commits into from
42 changes: 24 additions & 18 deletions client/src/lifecycleManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,36 +143,42 @@ class LifecycleManager extends LuigiClientBase {
if (this.currentContext?.internal?.thirdPartyCookieCheck?.disabled || this.disableTpcCheck) {
return;
}

const winParent = window.parent;
const targetOrigin = winParent.origin !== 'null' ? winParent.origin : '*';
const luigiCookieValue = 'luigiCookie=true';
const sanitizeString = input =>
input ? input.replace(/[^a-z0-9%._=;]/gim, '') : '';
const getLuigiCookie = cookies =>
cookies
.split(';')
.map(cookie => sanitizeString(cookie))
.find(cookie => cookie === luigiCookieValue);
let cookies = sanitizeString(document.cookie);
let tpc = 'enabled';
let cookies = document.cookie;
let luigiCookie;
let luigiCookieKey;

if (cookies) {
luigiCookie = cookies
.split(';')
.map(cookie => cookie.trim())
.find(cookie => cookie == 'luigiCookie=true');
luigiCookie = getLuigiCookie(cookies);
}
if (luigiCookie === 'luigiCookie=true') {
luigiCookieKey = luigiCookie.split('=')[0];
if (luigiCookie === luigiCookieValue) {
luigiCookieKey = luigiCookieValue.split('=')[0];
document.cookie = luigiCookieKey + '=; Max-Age=-99999999; SameSite=None; Secure';
}
document.cookie = 'luigiCookie=true; SameSite=None; Secure';
cookies = document.cookie;

document.cookie = luigiCookieValue + '; SameSite=None; Secure';
cookies = sanitizeString(document.cookie);

if (cookies) {
luigiCookie = cookies
.split(';')
.map(cookie => cookie.trim())
.find(cookie => cookie == 'luigiCookie=true');
luigiCookie = getLuigiCookie(cookies);
}
if (luigiCookie === 'luigiCookie=true') {
window.parent.postMessage({ msg: 'luigi.third-party-cookie', tpc }, '*');
document.cookie = luigiCookieKey + '=; Max-Age=-99999999; SameSite=None; Secure';
} else {
if (luigiCookie !== luigiCookieValue) {
tpc = 'disabled';
window.parent.postMessage({ msg: 'luigi.third-party-cookie', tpc }, '*');
console.warn('Third party cookies are not supported!');
}

winParent.postMessage({ msg: 'luigi.third-party-cookie', tpc }, targetOrigin);
}

/**
Expand Down
45 changes: 26 additions & 19 deletions core/third-party-cookies/init.html
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
<body>
<script>
let cookies = document.cookie;
<script type="text/javascript">
const winParent = window.parent;
const targetOrigin = winParent.origin !== 'null' ? winParent.origin : '*';
const luigiCookieValue = 'luigiCookie=true';
const sanitizeString = input =>
input ? input.replace(/[^a-z0-9%._=;]/gim, '') : '';
const getLuigiCookie = cookies =>
cookies
.split(';')
.map(cookie => sanitizeString(cookie))
.find(cookie => cookie === luigiCookieValue);
let cookies = sanitizeString(document.cookie);
let tpc = 'enabled';
let luigiCookie;
let luigiCookieKey;

if (cookies) {
luigiCookie = cookies
.split(';')
.map(cookie => cookie.trim())
.find(cookie => cookie == 'luigiCookie=true');
luigiCookie = getLuigiCookie(cookies);
}
if (luigiCookie === 'luigiCookie=true') {
luigiCookieKey = luigiCookie.split('=')[0];
if (luigiCookie === luigiCookieValue) {
luigiCookieKey = luigiCookieValue.split('=')[0];
document.cookie = luigiCookieKey + '=; Max-Age=-99999999; SameSite=None; Secure';
}
document.cookie = 'luigiCookie=true; SameSite=None; Secure';
cookies = document.cookie;

document.cookie = luigiCookieValue + '; SameSite=None; Secure';
cookies = sanitizeString(document.cookie);

if (cookies) {
luigiCookie = cookies
.split(';')
.map(cookie => cookie.trim())
.find(cookie => cookie == 'luigiCookie=true');
luigiCookie = getLuigiCookie(cookies);
}
if (luigiCookie === 'luigiCookie=true') {
window.parent.postMessage('luigi.tpcEnabled', '*');
document.cookie = luigiCookieKey + '=; Max-Age=-99999999; SameSite=None; Secure';
} else {
window.parent.postMessage('luigi.tpcDisabled', '*');
if (luigiCookie !== luigiCookieValue) {
tpc = 'disabled';
console.warn('Third party cookies are not supported!');
}

winParent.postMessage({ msg: 'luigi.third-party-cookie', tpc }, targetOrigin);
</script>
</body>