Improve `[trusted-]set-cookie` scriptlets

Related issues:
- https://github.com/uBlockOrigin/uBlock-issues/issues/3178
- https://github.com/uBlockOrigin/uBlock-issues/issues/2777
This commit is contained in:
Raymond Hill 2024-03-23 11:00:45 -04:00
parent b78f0c4fa4
commit 11a48561e0
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 10 additions and 1 deletions

View File

@ -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; }
@ -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',