@xkwdstore/agent 2.1.5 → 2.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +163 -130
- package/README.md +281 -280
- package/package.json +43 -43
- package/scripts/install-machine-agent.ps1 +385 -385
- package/src/brokerConnectionSupervisor.js +319 -319
- package/src/brokerHealth.js +113 -113
- package/src/brokerWorkerBridge.js +377 -377
- package/src/browserConnectivity.js +1461 -1461
- package/src/browserIsolation.js +26 -1
- package/src/browserLaunch.js +136 -136
- package/src/cli.js +1290 -1290
- package/src/cookieInjection.js +255 -255
- package/src/index.js +7 -0
- package/src/proxyCapability.js +481 -481
- package/src/sessionAuth.js +439 -439
- package/src/sessionHandler.js +3 -0
- package/src/worker.js +707 -703
package/src/cookieInjection.js
CHANGED
|
@@ -1,255 +1,255 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Device-side cookie restoration (defense in depth).
|
|
5
|
-
*
|
|
6
|
-
* The backend normalizes stored cookies once at the command chokepoint
|
|
7
|
-
* (backend/security/cookiePayload.js). This module is the SECOND gate on the
|
|
8
|
-
* device that owns the profile. It exists because a cookie payload can also
|
|
9
|
-
* arrive from a legacy agent path, a test fixture, or a future caller — and a
|
|
10
|
-
* bad payload must never silently widen a cookie's scope or extend its life.
|
|
11
|
-
*
|
|
12
|
-
* RULES (identical to the backend contract):
|
|
13
|
-
* - only authorized X/Twitter origins (x.com, twitter.com and subdomains),
|
|
14
|
-
* - never invent a name/value; entries without a usable scope are dropped,
|
|
15
|
-
* - `expires` supports seconds and millisecond timestamps; milliseconds are
|
|
16
|
-
* converted to the SAME instant in seconds (never extended),
|
|
17
|
-
* - already-expired persistent cookies are dropped and counted,
|
|
18
|
-
* - a host-only cookie stays host-only (rendered as a `url` cookie) rather
|
|
19
|
-
* than being silently promoted to a `.x.com` domain cookie,
|
|
20
|
-
* - optional attributes (secure/httpOnly/sameSite) are carried only when the
|
|
21
|
-
* payload supplied them; unknown sameSite is omitted,
|
|
22
|
-
* - no cookie value, header, or raw exception is ever logged or returned.
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
const { isAuthorizedXUrl } = require('./sessionAuth');
|
|
26
|
-
|
|
27
|
-
const MAX_COOKIES = 500;
|
|
28
|
-
// Stay well below the 256 KiB worker-bridge frame bound.
|
|
29
|
-
const MAX_TOTAL_COOKIE_BYTES = 180_000;
|
|
30
|
-
const MAX_COOKIE_VALUE_BYTES = 4096;
|
|
31
|
-
const MAX_COOKIE_EXPIRES_SECONDS = 253402300799;
|
|
32
|
-
|
|
33
|
-
const REASONS = Object.freeze({
|
|
34
|
-
ABSENT: 'absent',
|
|
35
|
-
EMPTY: 'empty',
|
|
36
|
-
MALFORMED: 'malformed',
|
|
37
|
-
EXPIRED: 'expired',
|
|
38
|
-
BAD_SCOPE: 'bad_scope',
|
|
39
|
-
UNAUTHORIZED_ORIGIN: 'unauthorized_origin',
|
|
40
|
-
BAD_EXPIRES: 'bad_expires',
|
|
41
|
-
BAD_NAME: 'bad_name',
|
|
42
|
-
BAD_VALUE: 'bad_value',
|
|
43
|
-
BAD_SIZE: 'bad_size',
|
|
44
|
-
PAYLOAD_TOO_LARGE: 'payload_too_large',
|
|
45
|
-
ACCEPTED: 'accepted',
|
|
46
|
-
REJECTED: 'rejected',
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
function _parseToArray(raw) {
|
|
50
|
-
if (Array.isArray(raw)) return { arr: raw, source: 'array' };
|
|
51
|
-
if (typeof raw === 'string') {
|
|
52
|
-
const text = raw.trim();
|
|
53
|
-
if (!text) return { arr: [], source: 'empty_string' };
|
|
54
|
-
try {
|
|
55
|
-
return { arr: JSON.parse(text), source: 'json_string' };
|
|
56
|
-
} catch {
|
|
57
|
-
try {
|
|
58
|
-
return { arr: JSON.parse(Buffer.from(text, 'base64').toString('utf8')), source: 'base64_json' };
|
|
59
|
-
} catch {
|
|
60
|
-
return { arr: null, source: 'unparsable_string' };
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
if (raw && typeof raw === 'object') {
|
|
65
|
-
if (Array.isArray(raw.cookies)) return { arr: raw.cookies, source: 'object.cookies' };
|
|
66
|
-
return { arr: null, source: 'object' };
|
|
67
|
-
}
|
|
68
|
-
return { arr: null, source: 'absent' };
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function _authorizedDomain(domain) {
|
|
72
|
-
if (typeof domain !== 'string' || !domain.trim()) return null;
|
|
73
|
-
const host = domain.trim().toLowerCase().replace(/^\./, '');
|
|
74
|
-
if (host === 'x.com' || host.endsWith('.x.com') || host === 'twitter.com' || host.endsWith('.twitter.com')) {
|
|
75
|
-
return host;
|
|
76
|
-
}
|
|
77
|
-
return null;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function _canonSameSite(value) {
|
|
81
|
-
if (value == null) return null;
|
|
82
|
-
switch (String(value).trim().toLowerCase()) {
|
|
83
|
-
case 'strict': return 'Strict';
|
|
84
|
-
case 'lax': return 'Lax';
|
|
85
|
-
case 'none': return 'None';
|
|
86
|
-
default: return null;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Resolve an expiry to Playwright's seconds contract. A millisecond timestamp is
|
|
92
|
-
* converted to the SAME wall-clock instant. Never extends an expiry.
|
|
93
|
-
* @returns {{present:boolean, usable?:boolean, expired?:boolean, seconds?:number}}
|
|
94
|
-
*/
|
|
95
|
-
function _resolveExpires(value, nowMs) {
|
|
96
|
-
if (value == null || value === '') return { present: false };
|
|
97
|
-
const n = Number(value);
|
|
98
|
-
if (!Number.isFinite(n)) return { present: true, usable: false };
|
|
99
|
-
if (n === -1) return { present: true, usable: true, seconds: -1 };
|
|
100
|
-
if (n <= 0 || n > MAX_COOKIE_EXPIRES_SECONDS * 1000) return { present: true, usable: false };
|
|
101
|
-
const seconds = n >= 1e12 ? Math.floor(n / 1000) : n;
|
|
102
|
-
if (seconds > MAX_COOKIE_EXPIRES_SECONDS) return { present: true, usable: false };
|
|
103
|
-
// A persistent cookie whose instant is already in the past cannot restore a
|
|
104
|
-
// session. Dropping it (rather than writing an expired cookie) is the honest
|
|
105
|
-
// outcome and avoids a misleading "restored" count.
|
|
106
|
-
if (seconds * 1000 <= nowMs) return { present: true, usable: true, expired: true, seconds };
|
|
107
|
-
return { present: true, usable: true, seconds };
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
function _buildHostOnlyUrl(host, pathValue) {
|
|
111
|
-
const p = typeof pathValue === 'string' && pathValue.startsWith('/') ? pathValue : '/';
|
|
112
|
-
return `https://${host}${p}`;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Normalize a cookie payload on the device.
|
|
117
|
-
* @returns {{cookies:object[], state:string, source:string, accepted:number, rejected:number, reasons:object}}
|
|
118
|
-
*/
|
|
119
|
-
function prepareCookies(raw, opts = {}) {
|
|
120
|
-
const nowMs = Number.isFinite(opts.now) ? opts.now : Date.now();
|
|
121
|
-
const empty = (state, source) => ({
|
|
122
|
-
cookies: [], state, source: source || 'absent', accepted: 0, rejected: 0, reasons: {},
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
if (raw == null || raw === '') return empty(REASONS.ABSENT, 'absent');
|
|
126
|
-
|
|
127
|
-
const { arr, source } = _parseToArray(raw);
|
|
128
|
-
if (!Array.isArray(arr)) return empty(REASONS.MALFORMED, source);
|
|
129
|
-
if (arr.length === 0) return empty(REASONS.EMPTY, source);
|
|
130
|
-
|
|
131
|
-
const cookies = [];
|
|
132
|
-
const reasons = {};
|
|
133
|
-
const bump = (r) => { reasons[r] = (reasons[r] || 0) + 1; };
|
|
134
|
-
let rejected = 0;
|
|
135
|
-
let totalBytes = 0;
|
|
136
|
-
|
|
137
|
-
for (const c of arr) {
|
|
138
|
-
if (cookies.length >= MAX_COOKIES) { bump(REASONS.BAD_SCOPE); rejected += 1; continue; }
|
|
139
|
-
if (!c || typeof c !== 'object' || Array.isArray(c)) { bump(REASONS.MALFORMED); rejected += 1; continue; }
|
|
140
|
-
if (typeof c.name !== 'string' || !c.name) { bump(REASONS.BAD_NAME); rejected += 1; continue; }
|
|
141
|
-
if (c.value == null) { bump(REASONS.BAD_VALUE); rejected += 1; continue; }
|
|
142
|
-
if (String(c.value).length > MAX_COOKIE_VALUE_BYTES) { bump(REASONS.BAD_SIZE); rejected += 1; continue; }
|
|
143
|
-
|
|
144
|
-
const exp = _resolveExpires(c.expires, nowMs);
|
|
145
|
-
// Match the backend contract: an unusable expiry is omitted (session-cookie
|
|
146
|
-
// fallback), a provably expired persistent cookie is dropped.
|
|
147
|
-
if (exp.present && !exp.usable) bump(REASONS.BAD_EXPIRES);
|
|
148
|
-
if (exp.present && exp.expired) { bump(REASONS.EXPIRED); rejected += 1; continue; }
|
|
149
|
-
|
|
150
|
-
const out = { name: c.name, value: String(c.value) };
|
|
151
|
-
|
|
152
|
-
const domainHost = _authorizedDomain(c.domain);
|
|
153
|
-
const urlAuthorized = c.url ? isAuthorizedXUrl(c.url) : false;
|
|
154
|
-
|
|
155
|
-
if (domainHost) {
|
|
156
|
-
if (c.hostOnly === true) {
|
|
157
|
-
// Preserve host-only semantics: a url-scoped cookie is host-only and
|
|
158
|
-
// carries the path, instead of promoting it to a domain cookie.
|
|
159
|
-
out.url = _buildHostOnlyUrl(domainHost, c.path);
|
|
160
|
-
} else {
|
|
161
|
-
out.domain = c.domain;
|
|
162
|
-
out.path = (typeof c.path === 'string' && c.path) ? c.path : '/';
|
|
163
|
-
}
|
|
164
|
-
} else if (urlAuthorized) {
|
|
165
|
-
out.url = c.url;
|
|
166
|
-
} else {
|
|
167
|
-
bump(c.domain || c.url ? REASONS.UNAUTHORIZED_ORIGIN : REASONS.BAD_SCOPE);
|
|
168
|
-
rejected += 1;
|
|
169
|
-
continue;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
if (exp.present && exp.usable && exp.seconds != null) out.expires = exp.seconds;
|
|
173
|
-
if (typeof c.httpOnly === 'boolean') out.httpOnly = c.httpOnly;
|
|
174
|
-
if (typeof c.secure === 'boolean') out.secure = c.secure;
|
|
175
|
-
if (c.sameSite != null) {
|
|
176
|
-
const sameSite = _canonSameSite(c.sameSite);
|
|
177
|
-
if (sameSite) out.sameSite = sameSite;
|
|
178
|
-
else if (String(c.sameSite).trim()) bump('unrecognized_samesite');
|
|
179
|
-
}
|
|
180
|
-
// Playwright does not accept a partitionKey field. Counting it keeps the
|
|
181
|
-
// rejection observable without inventing unsupported semantics.
|
|
182
|
-
if (c.partitionKey != null || c.partitioned === true) bump('partitioning_unsupported');
|
|
183
|
-
|
|
184
|
-
const approxBytes = JSON.stringify(out).length + 2;
|
|
185
|
-
if (totalBytes + approxBytes > MAX_TOTAL_COOKIE_BYTES) {
|
|
186
|
-
bump(REASONS.PAYLOAD_TOO_LARGE);
|
|
187
|
-
rejected += 1;
|
|
188
|
-
continue;
|
|
189
|
-
}
|
|
190
|
-
totalBytes += approxBytes;
|
|
191
|
-
|
|
192
|
-
cookies.push(out);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
let state;
|
|
196
|
-
if (cookies.length === 0) {
|
|
197
|
-
state = Object.keys(reasons).length ? REASONS.REJECTED : REASONS.MALFORMED;
|
|
198
|
-
} else {
|
|
199
|
-
state = REASONS.ACCEPTED;
|
|
200
|
-
}
|
|
201
|
-
return { cookies, state, source, accepted: cookies.length, rejected, reasons };
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* Apply cookies to a live Playwright context. Never throws; returns safe counts.
|
|
206
|
-
* @returns {Promise<{ok:boolean, state:string, applied:number, rejected:number, reason:string|null, reasons:object}>}
|
|
207
|
-
*/
|
|
208
|
-
async function applyCookies(context, raw) {
|
|
209
|
-
const prepared = prepareCookies(raw);
|
|
210
|
-
if (prepared.cookies.length === 0) {
|
|
211
|
-
return {
|
|
212
|
-
ok: false,
|
|
213
|
-
state: prepared.state,
|
|
214
|
-
applied: 0,
|
|
215
|
-
rejected: prepared.rejected,
|
|
216
|
-
reason: prepared.state === REASONS.ABSENT ? 'cookies_absent'
|
|
217
|
-
: prepared.state === REASONS.EMPTY ? 'cookies_empty'
|
|
218
|
-
: prepared.state === REASONS.MALFORMED ? 'cookies_malformed'
|
|
219
|
-
: 'cookies_rejected',
|
|
220
|
-
reasons: prepared.reasons,
|
|
221
|
-
};
|
|
222
|
-
}
|
|
223
|
-
try {
|
|
224
|
-
if (!context || typeof context.addCookies !== 'function') {
|
|
225
|
-
return { ok: false, state: 'rejected', applied: 0, rejected: prepared.rejected + prepared.cookies.length, reason: 'cookie_context_unavailable', reasons: prepared.reasons };
|
|
226
|
-
}
|
|
227
|
-
await context.addCookies(prepared.cookies);
|
|
228
|
-
return {
|
|
229
|
-
ok: true,
|
|
230
|
-
state: REASONS.ACCEPTED,
|
|
231
|
-
applied: prepared.cookies.length,
|
|
232
|
-
rejected: prepared.rejected,
|
|
233
|
-
reason: prepared.rejected ? 'some_cookies_rejected' : null,
|
|
234
|
-
reasons: prepared.reasons,
|
|
235
|
-
};
|
|
236
|
-
} catch (err) {
|
|
237
|
-
// Deployments differ in exactly which attribute Playwright rejects. We keep
|
|
238
|
-
// the reason coarse and NEVER echo the value/exception.
|
|
239
|
-
return { ok: false, state: 'rejected', applied: 0, rejected: prepared.cookies.length + prepared.rejected, reason: 'cookie_injection_failed', reasons: prepared.reasons };
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
/** Presence test used to decide whether a bootstrap restore is even eligible. */
|
|
244
|
-
function hasUsableCookies(raw) {
|
|
245
|
-
return prepareCookies(raw).cookies.length > 0;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
module.exports = {
|
|
249
|
-
MAX_COOKIES,
|
|
250
|
-
MAX_COOKIE_EXPIRES_SECONDS,
|
|
251
|
-
REASONS,
|
|
252
|
-
prepareCookies,
|
|
253
|
-
applyCookies,
|
|
254
|
-
hasUsableCookies,
|
|
255
|
-
};
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Device-side cookie restoration (defense in depth).
|
|
5
|
+
*
|
|
6
|
+
* The backend normalizes stored cookies once at the command chokepoint
|
|
7
|
+
* (backend/security/cookiePayload.js). This module is the SECOND gate on the
|
|
8
|
+
* device that owns the profile. It exists because a cookie payload can also
|
|
9
|
+
* arrive from a legacy agent path, a test fixture, or a future caller — and a
|
|
10
|
+
* bad payload must never silently widen a cookie's scope or extend its life.
|
|
11
|
+
*
|
|
12
|
+
* RULES (identical to the backend contract):
|
|
13
|
+
* - only authorized X/Twitter origins (x.com, twitter.com and subdomains),
|
|
14
|
+
* - never invent a name/value; entries without a usable scope are dropped,
|
|
15
|
+
* - `expires` supports seconds and millisecond timestamps; milliseconds are
|
|
16
|
+
* converted to the SAME instant in seconds (never extended),
|
|
17
|
+
* - already-expired persistent cookies are dropped and counted,
|
|
18
|
+
* - a host-only cookie stays host-only (rendered as a `url` cookie) rather
|
|
19
|
+
* than being silently promoted to a `.x.com` domain cookie,
|
|
20
|
+
* - optional attributes (secure/httpOnly/sameSite) are carried only when the
|
|
21
|
+
* payload supplied them; unknown sameSite is omitted,
|
|
22
|
+
* - no cookie value, header, or raw exception is ever logged or returned.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
const { isAuthorizedXUrl } = require('./sessionAuth');
|
|
26
|
+
|
|
27
|
+
const MAX_COOKIES = 500;
|
|
28
|
+
// Stay well below the 256 KiB worker-bridge frame bound.
|
|
29
|
+
const MAX_TOTAL_COOKIE_BYTES = 180_000;
|
|
30
|
+
const MAX_COOKIE_VALUE_BYTES = 4096;
|
|
31
|
+
const MAX_COOKIE_EXPIRES_SECONDS = 253402300799;
|
|
32
|
+
|
|
33
|
+
const REASONS = Object.freeze({
|
|
34
|
+
ABSENT: 'absent',
|
|
35
|
+
EMPTY: 'empty',
|
|
36
|
+
MALFORMED: 'malformed',
|
|
37
|
+
EXPIRED: 'expired',
|
|
38
|
+
BAD_SCOPE: 'bad_scope',
|
|
39
|
+
UNAUTHORIZED_ORIGIN: 'unauthorized_origin',
|
|
40
|
+
BAD_EXPIRES: 'bad_expires',
|
|
41
|
+
BAD_NAME: 'bad_name',
|
|
42
|
+
BAD_VALUE: 'bad_value',
|
|
43
|
+
BAD_SIZE: 'bad_size',
|
|
44
|
+
PAYLOAD_TOO_LARGE: 'payload_too_large',
|
|
45
|
+
ACCEPTED: 'accepted',
|
|
46
|
+
REJECTED: 'rejected',
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
function _parseToArray(raw) {
|
|
50
|
+
if (Array.isArray(raw)) return { arr: raw, source: 'array' };
|
|
51
|
+
if (typeof raw === 'string') {
|
|
52
|
+
const text = raw.trim();
|
|
53
|
+
if (!text) return { arr: [], source: 'empty_string' };
|
|
54
|
+
try {
|
|
55
|
+
return { arr: JSON.parse(text), source: 'json_string' };
|
|
56
|
+
} catch {
|
|
57
|
+
try {
|
|
58
|
+
return { arr: JSON.parse(Buffer.from(text, 'base64').toString('utf8')), source: 'base64_json' };
|
|
59
|
+
} catch {
|
|
60
|
+
return { arr: null, source: 'unparsable_string' };
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (raw && typeof raw === 'object') {
|
|
65
|
+
if (Array.isArray(raw.cookies)) return { arr: raw.cookies, source: 'object.cookies' };
|
|
66
|
+
return { arr: null, source: 'object' };
|
|
67
|
+
}
|
|
68
|
+
return { arr: null, source: 'absent' };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function _authorizedDomain(domain) {
|
|
72
|
+
if (typeof domain !== 'string' || !domain.trim()) return null;
|
|
73
|
+
const host = domain.trim().toLowerCase().replace(/^\./, '');
|
|
74
|
+
if (host === 'x.com' || host.endsWith('.x.com') || host === 'twitter.com' || host.endsWith('.twitter.com')) {
|
|
75
|
+
return host;
|
|
76
|
+
}
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function _canonSameSite(value) {
|
|
81
|
+
if (value == null) return null;
|
|
82
|
+
switch (String(value).trim().toLowerCase()) {
|
|
83
|
+
case 'strict': return 'Strict';
|
|
84
|
+
case 'lax': return 'Lax';
|
|
85
|
+
case 'none': return 'None';
|
|
86
|
+
default: return null;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Resolve an expiry to Playwright's seconds contract. A millisecond timestamp is
|
|
92
|
+
* converted to the SAME wall-clock instant. Never extends an expiry.
|
|
93
|
+
* @returns {{present:boolean, usable?:boolean, expired?:boolean, seconds?:number}}
|
|
94
|
+
*/
|
|
95
|
+
function _resolveExpires(value, nowMs) {
|
|
96
|
+
if (value == null || value === '') return { present: false };
|
|
97
|
+
const n = Number(value);
|
|
98
|
+
if (!Number.isFinite(n)) return { present: true, usable: false };
|
|
99
|
+
if (n === -1) return { present: true, usable: true, seconds: -1 };
|
|
100
|
+
if (n <= 0 || n > MAX_COOKIE_EXPIRES_SECONDS * 1000) return { present: true, usable: false };
|
|
101
|
+
const seconds = n >= 1e12 ? Math.floor(n / 1000) : n;
|
|
102
|
+
if (seconds > MAX_COOKIE_EXPIRES_SECONDS) return { present: true, usable: false };
|
|
103
|
+
// A persistent cookie whose instant is already in the past cannot restore a
|
|
104
|
+
// session. Dropping it (rather than writing an expired cookie) is the honest
|
|
105
|
+
// outcome and avoids a misleading "restored" count.
|
|
106
|
+
if (seconds * 1000 <= nowMs) return { present: true, usable: true, expired: true, seconds };
|
|
107
|
+
return { present: true, usable: true, seconds };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function _buildHostOnlyUrl(host, pathValue) {
|
|
111
|
+
const p = typeof pathValue === 'string' && pathValue.startsWith('/') ? pathValue : '/';
|
|
112
|
+
return `https://${host}${p}`;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Normalize a cookie payload on the device.
|
|
117
|
+
* @returns {{cookies:object[], state:string, source:string, accepted:number, rejected:number, reasons:object}}
|
|
118
|
+
*/
|
|
119
|
+
function prepareCookies(raw, opts = {}) {
|
|
120
|
+
const nowMs = Number.isFinite(opts.now) ? opts.now : Date.now();
|
|
121
|
+
const empty = (state, source) => ({
|
|
122
|
+
cookies: [], state, source: source || 'absent', accepted: 0, rejected: 0, reasons: {},
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
if (raw == null || raw === '') return empty(REASONS.ABSENT, 'absent');
|
|
126
|
+
|
|
127
|
+
const { arr, source } = _parseToArray(raw);
|
|
128
|
+
if (!Array.isArray(arr)) return empty(REASONS.MALFORMED, source);
|
|
129
|
+
if (arr.length === 0) return empty(REASONS.EMPTY, source);
|
|
130
|
+
|
|
131
|
+
const cookies = [];
|
|
132
|
+
const reasons = {};
|
|
133
|
+
const bump = (r) => { reasons[r] = (reasons[r] || 0) + 1; };
|
|
134
|
+
let rejected = 0;
|
|
135
|
+
let totalBytes = 0;
|
|
136
|
+
|
|
137
|
+
for (const c of arr) {
|
|
138
|
+
if (cookies.length >= MAX_COOKIES) { bump(REASONS.BAD_SCOPE); rejected += 1; continue; }
|
|
139
|
+
if (!c || typeof c !== 'object' || Array.isArray(c)) { bump(REASONS.MALFORMED); rejected += 1; continue; }
|
|
140
|
+
if (typeof c.name !== 'string' || !c.name) { bump(REASONS.BAD_NAME); rejected += 1; continue; }
|
|
141
|
+
if (c.value == null) { bump(REASONS.BAD_VALUE); rejected += 1; continue; }
|
|
142
|
+
if (String(c.value).length > MAX_COOKIE_VALUE_BYTES) { bump(REASONS.BAD_SIZE); rejected += 1; continue; }
|
|
143
|
+
|
|
144
|
+
const exp = _resolveExpires(c.expires, nowMs);
|
|
145
|
+
// Match the backend contract: an unusable expiry is omitted (session-cookie
|
|
146
|
+
// fallback), a provably expired persistent cookie is dropped.
|
|
147
|
+
if (exp.present && !exp.usable) bump(REASONS.BAD_EXPIRES);
|
|
148
|
+
if (exp.present && exp.expired) { bump(REASONS.EXPIRED); rejected += 1; continue; }
|
|
149
|
+
|
|
150
|
+
const out = { name: c.name, value: String(c.value) };
|
|
151
|
+
|
|
152
|
+
const domainHost = _authorizedDomain(c.domain);
|
|
153
|
+
const urlAuthorized = c.url ? isAuthorizedXUrl(c.url) : false;
|
|
154
|
+
|
|
155
|
+
if (domainHost) {
|
|
156
|
+
if (c.hostOnly === true) {
|
|
157
|
+
// Preserve host-only semantics: a url-scoped cookie is host-only and
|
|
158
|
+
// carries the path, instead of promoting it to a domain cookie.
|
|
159
|
+
out.url = _buildHostOnlyUrl(domainHost, c.path);
|
|
160
|
+
} else {
|
|
161
|
+
out.domain = c.domain;
|
|
162
|
+
out.path = (typeof c.path === 'string' && c.path) ? c.path : '/';
|
|
163
|
+
}
|
|
164
|
+
} else if (urlAuthorized) {
|
|
165
|
+
out.url = c.url;
|
|
166
|
+
} else {
|
|
167
|
+
bump(c.domain || c.url ? REASONS.UNAUTHORIZED_ORIGIN : REASONS.BAD_SCOPE);
|
|
168
|
+
rejected += 1;
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (exp.present && exp.usable && exp.seconds != null) out.expires = exp.seconds;
|
|
173
|
+
if (typeof c.httpOnly === 'boolean') out.httpOnly = c.httpOnly;
|
|
174
|
+
if (typeof c.secure === 'boolean') out.secure = c.secure;
|
|
175
|
+
if (c.sameSite != null) {
|
|
176
|
+
const sameSite = _canonSameSite(c.sameSite);
|
|
177
|
+
if (sameSite) out.sameSite = sameSite;
|
|
178
|
+
else if (String(c.sameSite).trim()) bump('unrecognized_samesite');
|
|
179
|
+
}
|
|
180
|
+
// Playwright does not accept a partitionKey field. Counting it keeps the
|
|
181
|
+
// rejection observable without inventing unsupported semantics.
|
|
182
|
+
if (c.partitionKey != null || c.partitioned === true) bump('partitioning_unsupported');
|
|
183
|
+
|
|
184
|
+
const approxBytes = JSON.stringify(out).length + 2;
|
|
185
|
+
if (totalBytes + approxBytes > MAX_TOTAL_COOKIE_BYTES) {
|
|
186
|
+
bump(REASONS.PAYLOAD_TOO_LARGE);
|
|
187
|
+
rejected += 1;
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
totalBytes += approxBytes;
|
|
191
|
+
|
|
192
|
+
cookies.push(out);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
let state;
|
|
196
|
+
if (cookies.length === 0) {
|
|
197
|
+
state = Object.keys(reasons).length ? REASONS.REJECTED : REASONS.MALFORMED;
|
|
198
|
+
} else {
|
|
199
|
+
state = REASONS.ACCEPTED;
|
|
200
|
+
}
|
|
201
|
+
return { cookies, state, source, accepted: cookies.length, rejected, reasons };
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Apply cookies to a live Playwright context. Never throws; returns safe counts.
|
|
206
|
+
* @returns {Promise<{ok:boolean, state:string, applied:number, rejected:number, reason:string|null, reasons:object}>}
|
|
207
|
+
*/
|
|
208
|
+
async function applyCookies(context, raw) {
|
|
209
|
+
const prepared = prepareCookies(raw);
|
|
210
|
+
if (prepared.cookies.length === 0) {
|
|
211
|
+
return {
|
|
212
|
+
ok: false,
|
|
213
|
+
state: prepared.state,
|
|
214
|
+
applied: 0,
|
|
215
|
+
rejected: prepared.rejected,
|
|
216
|
+
reason: prepared.state === REASONS.ABSENT ? 'cookies_absent'
|
|
217
|
+
: prepared.state === REASONS.EMPTY ? 'cookies_empty'
|
|
218
|
+
: prepared.state === REASONS.MALFORMED ? 'cookies_malformed'
|
|
219
|
+
: 'cookies_rejected',
|
|
220
|
+
reasons: prepared.reasons,
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
try {
|
|
224
|
+
if (!context || typeof context.addCookies !== 'function') {
|
|
225
|
+
return { ok: false, state: 'rejected', applied: 0, rejected: prepared.rejected + prepared.cookies.length, reason: 'cookie_context_unavailable', reasons: prepared.reasons };
|
|
226
|
+
}
|
|
227
|
+
await context.addCookies(prepared.cookies);
|
|
228
|
+
return {
|
|
229
|
+
ok: true,
|
|
230
|
+
state: REASONS.ACCEPTED,
|
|
231
|
+
applied: prepared.cookies.length,
|
|
232
|
+
rejected: prepared.rejected,
|
|
233
|
+
reason: prepared.rejected ? 'some_cookies_rejected' : null,
|
|
234
|
+
reasons: prepared.reasons,
|
|
235
|
+
};
|
|
236
|
+
} catch (err) {
|
|
237
|
+
// Deployments differ in exactly which attribute Playwright rejects. We keep
|
|
238
|
+
// the reason coarse and NEVER echo the value/exception.
|
|
239
|
+
return { ok: false, state: 'rejected', applied: 0, rejected: prepared.cookies.length + prepared.rejected, reason: 'cookie_injection_failed', reasons: prepared.reasons };
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** Presence test used to decide whether a bootstrap restore is even eligible. */
|
|
244
|
+
function hasUsableCookies(raw) {
|
|
245
|
+
return prepareCookies(raw).cookies.length > 0;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
module.exports = {
|
|
249
|
+
MAX_COOKIES,
|
|
250
|
+
MAX_COOKIE_EXPIRES_SECONDS,
|
|
251
|
+
REASONS,
|
|
252
|
+
prepareCookies,
|
|
253
|
+
applyCookies,
|
|
254
|
+
hasUsableCookies,
|
|
255
|
+
};
|
package/src/index.js
CHANGED
|
@@ -581,6 +581,13 @@ async function startSession(accountId, options = {}, lease) {
|
|
|
581
581
|
if (proxy) {
|
|
582
582
|
// Chromium handles proxy auth via Playwright's proxy config.
|
|
583
583
|
launchOptions.proxy = { ...proxy };
|
|
584
|
+
// HTTP/2 through an authenticated HTTP CONNECT proxy stalls on Google's
|
|
585
|
+
// properties (tunnel + TLS succeed, the main-frame h2 stream never answers).
|
|
586
|
+
// Pin HTTP/1.1 for the credential-bearing case only; no security control is
|
|
587
|
+
// relaxed (TLS/cert validation, DNS pin, QUIC/WebRTC suppression unchanged).
|
|
588
|
+
if (proxy.username !== undefined && proxy.username !== '') {
|
|
589
|
+
launchOptions.args.push('--disable-http2');
|
|
590
|
+
}
|
|
584
591
|
log(`[session] Proxy configured: ${proxy.server} user=${proxy.username || '(none)'}`);
|
|
585
592
|
} else {
|
|
586
593
|
log(`[session] WARNING: No proxy configured for account ${accountId} — browser will use direct connection (real IP visible!)`);
|