-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoxic-cookies.js
131 lines (131 loc) · 4.95 KB
/
toxic-cookies.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
'use strict';
/**
* Tool for poisoning browser cookies of currently loaded domain
* @author S0AndS0
* @license AGPL-3.0
*/
class Toxic_Cookies {
/**
* Initialize new instance of `Toxic_Cookies`
* @param {Configure_Toxic_Cookies} - Configuration object to unwrap
* @param {string[]} [Configure_Toxic_Cookies.clean_keys=[]]
* @param {number} [Configure_Toxic_Cookies.max_bite_size=4090]
* @param {string} [Configure_Toxic_Cookies.path='/']
* @param {fuzzy_callback} [Configure_Toxic_Cookies.key_callback=() => { return Math.random(); }]
* @param {fuzzy_callback} [Configure_Toxic_Cookies.value_callback=() => { return Math.random(); }]
* @example
* ```JavaScript
* const toxic_cookies = new Toxic_Cookies({
* clean_keys: ['nan', 'auth'],
* max_bite_size: 4090,
* path: window.location.href,
* key_callback: () => { return Math.random(); },
* value_callback: () => { return Math.random(); },
* });
* ```
*/
/* istanbul ignore next */
constructor({ clean_keys = [], max_bite_size = 4090, path = '/', key_callback = () => { return Math.random(); }, value_callback = () => { return Math.random(); }, }) {
this.clean_keys = clean_keys;
this.max_bite_size = max_bite_size;
this.path = path;
this.key_callback = key_callback;
this.value_callback = value_callback;
}
/**
* @param {number} [days=1]
* @returns {number}
* @example
* ```javascript
* Toxic_Cookies.calculateCookieExpiration(2);
* //> "Sun, 27 Dec 2020 01:13:12 GMT"
* ```
*/
static calculateCookieExpiration(days = 1) {
days = Number(days) | 0;
const date = new Date();
const now = date.getTime();
date.setTime(now + days * 24 * 60 * 60 * 1000);
return date.toUTCString();
}
/**
* @param {boolean} [coerce_values=false] Default `false`
* @returns {coercedCookies}
*/
static objectifyCookies(coerce_values = false) {
return document.cookie.split(';').reduce((accumulator, cookie) => {
/* istanbul ignore next */
if (cookie.length) {
const chunk = cookie.split('=');
const key = chunk[0].trim();
let value = chunk[1] ? chunk[1].trim() : '';
if (coerce_values === true) {
try {
value = JSON.parse(value);
}
catch (e) {
/* istanbul ignore next */
if (!(e instanceof SyntaxError)) {
throw e;
}
if ('undefined' === value) {
value = undefined;
}
else if ('NaN' === value) {
value = NaN;
}
else if ('Infinity' === value) {
value = Infinity;
}
/* istanbul ignore next */
else if ('-Infinity' === value) {
value = -Infinity;
}
}
}
accumulator[key] = value;
}
return accumulator;
}, {});
}
/**
* Overwrites existing cookies, except those listed in `this.clean_keys`, with data from value callback function
*/
poisonExistingCookies() {
const expiration = this.constructor.calculateCookieExpiration();
const cookie_metadata = `expires=${expiration};path=${this.path}`;
const objectified_cookies = this.constructor.objectifyCookies();
Object.entries(objectified_cookies).forEach(([key, value]) => {
if (!this.clean_keys.includes(key)) {
window.document.cookie = `${key}=${this.value_callback()};${cookie_metadata}`;
}
});
}
/**
* Fills remaining space for cookies with data generated by key/value callback functions
* @TODO - Figure out why NodeJS does not play nice with Blob
*/
/* istanbul ignore next */
poisonRemainingCookies() {
const expiration = this.constructor.calculateCookieExpiration();
while (new Blob([window.document.cookie]).size < this.max_bite_size) {
const key = this.key_callback();
const value = this.value_callback();
window.document.cookie = `${key}=${value};expires=${expiration};path=${this.path}`;
}
}
/**
* Runs `this.poisonExistingCookies()` and `this.poisonRemainingCookies()` methods
* @see Toxic_Cookies.poisonRemainingCookies
*/
/* istanbul ignore next */
poisionAllCookies() {
this.poisonExistingCookies();
this.poisonRemainingCookies();
}
}
/* istanbul ignore next */
if (typeof module !== 'undefined') {
module.exports = Toxic_Cookies;
}
//# sourceMappingURL=toxic-cookies.js.map