-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathset-cookie.js
122 lines (114 loc) · 3.44 KB
/
set-cookie.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import {
hit,
logMessage,
nativeIsNaN,
isCookieSetWithValue,
getLimitedCookieValue,
serializeCookie,
isValidCookiePath,
getCookiePath,
} from '../helpers';
/* eslint-disable max-len */
/**
* @scriptlet set-cookie
*
* @description
* Sets a cookie with the specified name, value, path, and domain.
*
* Related UBO scriptlet:
* https://github.com/gorhill/uBlock/wiki/Resources-Library#set-cookiejs-
*
* ### Syntax
*
* ```text
* example.org#%#//scriptlet('set-cookie', name, value[, path[, domain]])
* ```
*
* - `name` — required, cookie name to be set
* - `value` — required, cookie value; possible values:
* - positive decimal integer `<= 32767`
* - one of the predefined constants in any case variation:
* - `true` / `t`
* - `false` / `f`
* - `yes` / `y`
* - `no` / `n`
* - `ok`
* - `on` / `off`
* - `accept`/ `accepted` / `notaccepted`
* - `reject` / `rejected`
* - `allow` / `allowed`
* - `disallow` / `deny`
* - `enable` / `enabled`
* - `disable` / `disabled`
* - `necessary` / `required`
* - `hide` / `hidden`
* - `essential` / `nonessential`
* - `checked` / `unchecked`
* - `forbidden` / `forever`
* - `path` — optional, cookie path, defaults to `/`; possible values:
* - `/` — root path
* - `none` — to set no path at all
* - `domain` — optional, cookie domain, if not set origin will be set as domain,
* if the domain does not match the origin, the cookie will not be set
*
* > Note that the scriptlet does not encode a cookie name,
* > e.g. name 'a:b' will be set as 'a:b' and not as 'a%3Ab'.
* >
* > Also if a cookie name includes `;`, the cookie will not be set since this may cause the cookie to break.
*
* ### Examples
*
* ```adblock
* example.org#%#//scriptlet('set-cookie', 'CookieConsent', '1')
*
* example.org#%#//scriptlet('set-cookie', 'gdpr-settings-cookie', 'true')
*
* example.org#%#//scriptlet('set-cookie', 'cookie_consent', 'ok', 'none')
*
* example.org#%#//scriptlet('set-cookie-reload', 'test', '1', 'none', 'example.org')
* ```
*
* @added v1.2.3.
*/
/* eslint-enable max-len */
export function setCookie(source, name, value, path = '/', domain = '') {
const validValue = getLimitedCookieValue(value);
if (validValue === null) {
logMessage(source, `Invalid cookie value: '${validValue}'`);
return;
}
if (!isValidCookiePath(path)) {
logMessage(source, `Invalid cookie path: '${path}'`);
return;
}
if (!document.location.origin.includes(domain)) {
logMessage(source, `Cookie domain not matched by origin: '${domain}'`);
return;
}
const cookieToSet = serializeCookie(name, validValue, path, domain);
if (!cookieToSet) {
logMessage(source, 'Invalid cookie name or value');
return;
}
hit(source);
document.cookie = cookieToSet;
}
export const setCookieNames = [
'set-cookie',
// aliases are needed for matching the related scriptlet converted into our syntax
'set-cookie.js',
'ubo-set-cookie.js',
'ubo-set-cookie',
];
// eslint-disable-next-line prefer-destructuring
setCookie.primaryName = setCookieNames[0];
setCookie.injections = [
hit,
logMessage,
nativeIsNaN,
isCookieSetWithValue,
getLimitedCookieValue,
serializeCookie,
isValidCookiePath,
getCookiePath,
];