Skip to content

Commit

Permalink
Improve [trusted-]set-cookie scriptlets
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Mar 23, 2024
1 parent b78f0c4 commit 11a4856
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion assets/resources/scriptlets.js
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,16 @@ function setCookieFn(
path = '',
options = {},
) {
// https://datatracker.ietf.org/doc/html/rfc2616#section-2.2
// https://github.com/uBlockOrigin/uBlock-issues/issues/2777
if ( trusted === false && /[^!#$%&'*+\-.0-9A-Z[\]^_`a-z|~]/.test(name) ) {
name = encodeURIComponent(name);
}
// https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.1
if ( /[^!#-+\--:<-[\]-~]/.test(value) ) {
value = encodeURIComponent(value);
}

const cookieBefore = getCookieFn(name);
if ( cookieBefore !== undefined && options.dontOverwrite ) { return; }
if ( cookieBefore === value && options.reload ) { return; }
Expand Down Expand Up @@ -3713,7 +3723,6 @@ function setCookie(
if ( name === '' ) { return; }
const safe = safeSelf();
const logPrefix = safe.makeLogPrefix('set-cookie', name, value, path);
name = encodeURIComponent(name);

const validValues = [
'accept', 'reject',
Expand Down

0 comments on commit 11a4856

Please sign in to comment.