This repository has been archived by the owner on Apr 24, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
addEventListener('fetch', event => { | ||
event.respondWith(main(event)); | ||
}); | ||
|
||
async function main(event) { | ||
const newRequest = stripSessionCookie(event.request); | ||
return fetch(newRequest); | ||
} | ||
|
||
/** | ||
* Strip session cookies from the front-end. | ||
* | ||
* It's important that you disable this script from: | ||
* - /admin/* | ||
* - /review/* | ||
* - /contact/* | ||
* | ||
* Otherwise CSRF won't work. | ||
* | ||
*/ | ||
function stripSessionCookie(request) { | ||
const newHeaders = new Headers(request.headers); | ||
const url = new URL(request.url); | ||
const cookieString = newHeaders.get('Cookie'); | ||
if ( | ||
cookieString !== null | ||
&& (cookieString.includes('csrftoken') || cookieString.includes('sessionid')) | ||
) { | ||
const newValue = stripCookie( | ||
stripCookie(newHeaders.get('Cookie'), 'sessionid'), | ||
'csrftoken' | ||
); | ||
newHeaders.set('Cookie', newValue); | ||
return new Request(request.url, { | ||
headers: newHeaders, | ||
method:request.method, | ||
body: request.body, | ||
redirect: request.redirect, | ||
}); | ||
} | ||
|
||
return request; | ||
} | ||
|
||
/** | ||
* Strip a cookie from the cookie string and return a new cookie string. | ||
*/ | ||
function stripCookie(cookiesString, cookieName) { | ||
return cookiesString.split(';').filter(v => { | ||
return v.split('=')[0].trim() !== cookieName; | ||
}).join(';'); | ||
} |