-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathtrusted-set-cookie.js
156 lines (143 loc) · 4.74 KB
/
trusted-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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import {
hit,
logMessage,
nativeIsNaN,
isCookieSetWithValue,
serializeCookie,
isValidCookiePath,
parseKeywordValue,
getTrustedCookieOffsetMs,
getCookiePath,
} from '../helpers';
/* eslint-disable max-len */
/**
* @trustedScriptlet trusted-set-cookie
*
* @description
* Sets a cookie with arbitrary name and value,
* and with optional ability to offset cookie attribute 'expires', set path
* and set domain.
*
* ### Syntax
*
* ```text
* example.org#%#//scriptlet('trusted-set-cookie', name, value[, offsetExpiresSec[, path[, domain]]])
* ```
*
* - `name` — required, cookie name to be set
* - `value` — required, cookie value. Possible values:
* - arbitrary value
* - empty string for no value
* - `$now$` keyword for setting current time in ms, e.g 1667915146503
* - `$currentDate$` keyword for setting current time as string, e.g 'Tue Nov 08 2022 13:53:19 GMT+0300'
* - `$currentISODate$` keyword for setting current date in the date time string format,
* e.g '2022-11-08T13:53:19.650Z'
* - `offsetExpiresSec` — optional, offset from current time in seconds, after which cookie should expire;
* defaults to no offset. Possible values:
* - positive integer in seconds
* - `1year` keyword for setting expiration date to one year
* - `1day` keyword for setting expiration date to one day
* - `path` — optional, argument for setting 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 cookie names and values.
* > As a result, if a cookie's name or value includes `;`,
* > the scriptlet will not set the cookie since this may cause the cookie to break.
*
* ### Examples
*
* 1. Set cookie
*
* ```adblock
* example.org#%#//scriptlet('trusted-set-cookie', 'cmpconsent', 'accept')
* example.org#%#//scriptlet('trusted-set-cookie', 'cmpconsent', '1-accept_1')
* ```
*
* 1. Set cookie with `new Date().getTime()` value
*
* ```adblock
* example.org#%#//scriptlet('trusted-set-cookie', 'cmpconsent', '$now$')
* ```
*
* 1. Set cookie which will expire in 3 days
*
* ```adblock
* example.org#%#//scriptlet('trusted-set-cookie', 'cmpconsent', 'accept', '259200')
* ```
*
* 1. Set cookie which will expire in one year
*
* ```adblock
* example.org#%#//scriptlet('trusted-set-cookie', 'cmpconsent', 'accept', '1year')
* ```
*
* 1. Set cookie with no path
*
* ```adblock
* example.org#%#//scriptlet('trusted-set-cookie', 'cmpconsent', 'decline', '', 'none')
*
* 1. Set cookie with domain
*
* ```adblock
* example.org#%#//scriptlet('trusted-set-cookie', 'cmpconsent', 'decline', '', 'none', 'example.org')
* ```
*
* @added v1.7.3.
*/
/* eslint-enable max-len */
export function trustedSetCookie(source, name, value, offsetExpiresSec = '', path = '/', domain = '') {
if (typeof name === 'undefined') {
logMessage(source, 'Cookie name should be specified');
return;
}
if (typeof value === 'undefined') {
logMessage(source, 'Cookie value should be specified');
return;
}
const parsedValue = parseKeywordValue(value);
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;
}
let cookieToSet = serializeCookie(name, parsedValue, path, domain, false);
if (!cookieToSet) {
logMessage(source, 'Invalid cookie name or value');
return;
}
// TODO: Move this concat to serializeCookie
if (offsetExpiresSec) {
const parsedOffsetMs = getTrustedCookieOffsetMs(offsetExpiresSec);
if (!parsedOffsetMs) {
logMessage(source, `Invalid offsetExpiresSec value: ${offsetExpiresSec}`);
return;
}
const expires = Date.now() + parsedOffsetMs;
cookieToSet += `; expires=${new Date(expires).toUTCString()}`;
}
document.cookie = cookieToSet;
hit(source);
}
export const trustedSetCookieNames = [
'trusted-set-cookie',
// trusted scriptlets support no aliases
];
// eslint-disable-next-line prefer-destructuring
trustedSetCookie.primaryName = trustedSetCookieNames[0];
trustedSetCookie.injections = [
hit,
logMessage,
nativeIsNaN,
isCookieSetWithValue,
serializeCookie,
isValidCookiePath,
getTrustedCookieOffsetMs,
parseKeywordValue,
getCookiePath,
];