| 1 | /** |
| 2 | * Browser cookie extraction for Twitter authentication. |
| 3 | * Delegates to @steipete/sweet-cookie for Safari/Chrome/Firefox reads. |
| 4 | */ |
| 5 | const TWITTER_COOKIE_NAMES = ['auth_token', 'ct0']; |
| 6 | const TWITTER_URL = 'https://x.com/'; |
| 7 | const TWITTER_ORIGINS = ['https://x.com/', 'https://twitter.com/']; |
| 8 | const DEFAULT_COOKIE_TIMEOUT_MS = 30_000; |
| 9 | export const COOKIE_VALUE_REGEX = /^[!#$%&'()*+\-./0-9:<=>?@A-Z[\]^_`a-z{|}~]+$/; |
| 10 | const HEADER_VALUE_REGEX = /^[\x20-\x7e]+$/; |
| 11 | const INVALID_COOKIE_ERROR = 'Invalid auth_token/ct0 format'; |
| 12 | const SECRET_ENV_KEYS = ['AUTH_TOKEN', 'CT0', 'TWITTER_AUTH_TOKEN', 'TWITTER_CT0']; |
| 13 | export function isValidCookieValue(value) { |
| 14 | return typeof value === 'string' && COOKIE_VALUE_REGEX.test(value); |
| 15 | } |
| 16 | export function isValidHeaderValue(value) { |
| 17 | return typeof value === 'string' && HEADER_VALUE_REGEX.test(value); |
| 18 | } |
| 19 | function normalizeCookieValue(value) { |
| 20 | if (typeof value !== 'string') { |
| 21 | return null; |
| 22 | } |
| 23 | if (/[\r\n\0]/.test(value)) { |
| 24 | throw new Error(INVALID_COOKIE_ERROR); |
| 25 | } |
| 26 | const trimmed = value.replace(/^[ \t]+|[ \t]+$/g, ''); |
| 27 | if (trimmed.length === 0) { |
| 28 | return null; |
| 29 | } |
| 30 | if (!COOKIE_VALUE_REGEX.test(trimmed)) { |
| 31 | throw new Error(INVALID_COOKIE_ERROR); |
| 32 | } |
| 33 | return trimmed; |
| 34 | } |
| 35 | export function redactSecrets(text, extraSecrets = []) { |
| 36 | if (text === null || text === undefined) { |
| 37 | return ''; |
| 38 | } |
| 39 | let out = String(text); |
| 40 | const candidates = new Set(); |
| 41 | const addCandidate = (raw) => { |
| 42 | if (typeof raw !== 'string') { |
| 43 | return; |
| 44 | } |
| 45 | for (const variant of [raw, raw.trim()]) { |
| 46 | if (variant.length >= 6) { |
| 47 | candidates.add(variant); |
| 48 | const escaped = JSON.stringify(variant).slice(1, -1); |
| 49 | if (escaped.length >= 6) { |
| 50 | candidates.add(escaped); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | }; |
| 55 | for (const secret of extraSecrets) { |
| 56 | addCandidate(secret); |
| 57 | } |
| 58 | for (const key of SECRET_ENV_KEYS) { |
| 59 | addCandidate(process.env[key]); |
| 60 | } |
| 61 | // Mask longest known secrets (e.g. a full custom cookieHeader with extra |
| 62 | // cookies) before individual auth_token=/ct0= substitutions alter the text. |
| 63 | const ordered = [...candidates].sort((a, b) => b.length - a.length); |
| 64 | for (const secret of ordered) { |
| 65 | out = out.split(secret).join('<redacted>'); |
| 66 | } |
| 67 | out = out.replace( |
| 68 | /(Headers\.\w+:\s*)(["'])[\s\S]*?\2(\s+is an invalid header value)/gi, |
| 69 | '$1"<redacted>"$3', |
| 70 | ); |
| 71 | out = out.replace(/\b(auth_token|ct0)=[^;\s"']*/gi, '$1=<redacted>'); |
| 72 | return out; |
| 73 | } |
| 74 | async function loadSweetCookie() { |
| 75 | return import('@steipete/sweet-cookie'); |
| 76 | } |
| 77 | function normalizeValue(value) { |
| 78 | if (typeof value !== 'string') { |
| 79 | return null; |
| 80 | } |
| 81 | const trimmed = value.trim(); |
| 82 | return trimmed.length > 0 ? trimmed : null; |
| 83 | } |
| 84 | const TRUTHY_ENV_VALUES = new Set(['1', 'true', 'yes', 'on']); |
| 85 | function envFlagEnabled(name) { |
| 86 | const value = normalizeValue(process.env[name]); |
| 87 | if (!value) { |
| 88 | return false; |
| 89 | } |
| 90 | return TRUTHY_ENV_VALUES.has(value.toLowerCase()); |
| 91 | } |
| 92 | function browserCookiesDeniedByEnv() { |
| 93 | if (envFlagEnabled('BIRD_DISABLE_BROWSER_COOKIES') || |
| 94 | envFlagEnabled('LAST30DAYS_DISABLE_BROWSER_COOKIES')) { |
| 95 | return true; |
| 96 | } |
| 97 | const fromBrowser = process.env.FROM_BROWSER; |
| 98 | if (typeof fromBrowser === 'string' && fromBrowser.trim().toLowerCase() === 'off') { |
| 99 | return true; |
| 100 | } |
| 101 | const browserConsent = process.env.BROWSER_CONSENT; |
| 102 | if (browserConsent !== undefined) { |
| 103 | const normalized = String(browserConsent).trim().toLowerCase(); |
| 104 | if (!TRUTHY_ENV_VALUES.has(normalized)) { |
| 105 | return true; |
| 106 | } |
| 107 | } |
| 108 | return false; |
| 109 | } |
| 110 | function cookieHeader(authToken, ct0) { |
| 111 | if (!isValidCookieValue(authToken) || !isValidCookieValue(ct0)) { |
| 112 | throw new Error(INVALID_COOKIE_ERROR); |
| 113 | } |
| 114 | return `auth_token=${authToken}; ct0=${ct0}`; |
| 115 | } |
| 116 | function buildEmpty() { |
| 117 | return { authToken: null, ct0: null, cookieHeader: null, source: null }; |
| 118 | } |
| 119 | function readEnvCookie(cookies, keys, field) { |
| 120 | if (cookies[field]) { |
| 121 | return; |
| 122 | } |
| 123 | for (const key of keys) { |
| 124 | const value = normalizeCookieValue(process.env[key]); |
| 125 | if (!value) { |
| 126 | continue; |
| 127 | } |
| 128 | cookies[field] = value; |
| 129 | if (!cookies.source) { |
| 130 | cookies.source = `env ${key}`; |
| 131 | } |
| 132 | break; |
| 133 | } |
| 134 | } |
| 135 | function resolveSources(cookieSource) { |
| 136 | if (Array.isArray(cookieSource)) { |
| 137 | return cookieSource; |
| 138 | } |
| 139 | if (cookieSource) { |
| 140 | return [cookieSource]; |
| 141 | } |
| 142 | return ['safari', 'chrome', 'firefox']; |
| 143 | } |
| 144 | function labelForSource(source, profile) { |
| 145 | if (source === 'safari') { |
| 146 | return 'Safari'; |
| 147 | } |
| 148 | if (source === 'chrome') { |
| 149 | return profile ? `Chrome profile "${profile}"` : 'Chrome default profile'; |
| 150 | } |
| 151 | return profile ? `Firefox profile "${profile}"` : 'Firefox default profile'; |
| 152 | } |
| 153 | function pickCookieValue(cookies, name) { |
| 154 | const matches = cookies.filter((c) => c?.name === name && typeof c.value === 'string'); |
| 155 | if (matches.length === 0) { |
| 156 | return null; |
| 157 | } |
| 158 | const preferred = matches.find((c) => (c.domain ?? '').endsWith('x.com')); |
| 159 | if (preferred?.value) { |
| 160 | return preferred.value; |
| 161 | } |
| 162 | const twitter = matches.find((c) => (c.domain ?? '').endsWith('twitter.com')); |
| 163 | if (twitter?.value) { |
| 164 | return twitter.value; |
| 165 | } |
| 166 | return matches[0]?.value ?? null; |
| 167 | } |
| 168 | async function readTwitterCookiesFromBrowser(options) { |
| 169 | const warnings = []; |
| 170 | const out = buildEmpty(); |
| 171 | const { getCookies } = options; |
| 172 | const { cookies, warnings: providerWarnings } = await getCookies({ |
| 173 | url: TWITTER_URL, |
| 174 | origins: TWITTER_ORIGINS, |
| 175 | names: [...TWITTER_COOKIE_NAMES], |
| 176 | browsers: [options.source], |
| 177 | mode: 'merge', |
| 178 | chromeProfile: options.chromeProfile, |
| 179 | firefoxProfile: options.firefoxProfile, |
| 180 | timeoutMs: options.cookieTimeoutMs, |
| 181 | }); |
| 182 | warnings.push(...providerWarnings); |
| 183 | let authToken = null; |
| 184 | let ct0 = null; |
| 185 | try { |
| 186 | authToken = normalizeCookieValue(pickCookieValue(cookies, 'auth_token')); |
| 187 | ct0 = normalizeCookieValue(pickCookieValue(cookies, 'ct0')); |
| 188 | } |
| 189 | catch { |
| 190 | warnings.push(INVALID_COOKIE_ERROR); |
| 191 | return { cookies: out, warnings }; |
| 192 | } |
| 193 | if (authToken) { |
| 194 | out.authToken = authToken; |
| 195 | } |
| 196 | if (ct0) { |
| 197 | out.ct0 = ct0; |
| 198 | } |
| 199 | if (out.authToken && out.ct0) { |
| 200 | out.cookieHeader = cookieHeader(out.authToken, out.ct0); |
| 201 | out.source = labelForSource(options.source, options.source === 'chrome' ? options.chromeProfile : options.firefoxProfile); |
| 202 | return { cookies: out, warnings }; |
| 203 | } |
| 204 | if (options.source === 'safari') { |
| 205 | warnings.push('No Twitter cookies found in Safari. Make sure you are logged into x.com in Safari.'); |
| 206 | } |
| 207 | else if (options.source === 'chrome') { |
| 208 | warnings.push('No Twitter cookies found in Chrome. Make sure you are logged into x.com in Chrome.'); |
| 209 | } |
| 210 | else { |
| 211 | warnings.push('No Twitter cookies found in Firefox. Make sure you are logged into x.com in Firefox and the profile exists.'); |
| 212 | } |
| 213 | return { cookies: out, warnings }; |
| 214 | } |
| 215 | async function extractCookiesFromBrowser(options) { |
| 216 | const { getCookies } = await loadSweetCookie(); |
| 217 | return readTwitterCookiesFromBrowser({ |
| 218 | getCookies, |
| 219 | ...options, |
| 220 | }); |
| 221 | } |
| 222 | export async function extractCookiesFromSafari() { |
| 223 | return extractCookiesFromBrowser({ source: 'safari' }); |
| 224 | } |
| 225 | export async function extractCookiesFromChrome(profile) { |
| 226 | return extractCookiesFromBrowser({ source: 'chrome', chromeProfile: profile }); |
| 227 | } |
| 228 | export async function extractCookiesFromFirefox(profile) { |
| 229 | return extractCookiesFromBrowser({ source: 'firefox', firefoxProfile: profile }); |
| 230 | } |
| 231 | /** |
| 232 | * Resolve Twitter credentials from multiple sources. |
| 233 | * Priority: CLI args > environment variables > browsers (ordered). |
| 234 | */ |
| 235 | export async function resolveCredentials(options) { |
| 236 | const warnings = []; |
| 237 | const cookies = buildEmpty(); |
| 238 | const disableBrowserCookies = browserCookiesDeniedByEnv(); |
| 239 | const cookieTimeoutMs = typeof options.cookieTimeoutMs === 'number' && |
| 240 | Number.isFinite(options.cookieTimeoutMs) && |
| 241 | options.cookieTimeoutMs > 0 |
| 242 | ? options.cookieTimeoutMs |
| 243 | : process.platform === 'darwin' |
| 244 | ? DEFAULT_COOKIE_TIMEOUT_MS |
| 245 | : undefined; |
| 246 | if (options.authToken !== undefined && options.authToken !== null) { |
| 247 | const normalizedAuthToken = normalizeCookieValue(options.authToken); |
| 248 | if (normalizedAuthToken) { |
| 249 | cookies.authToken = normalizedAuthToken; |
| 250 | cookies.source = 'CLI argument'; |
| 251 | } |
| 252 | } |
| 253 | if (options.ct0 !== undefined && options.ct0 !== null) { |
| 254 | const normalizedCt0 = normalizeCookieValue(options.ct0); |
| 255 | if (normalizedCt0) { |
| 256 | cookies.ct0 = normalizedCt0; |
| 257 | if (!cookies.source) { |
| 258 | cookies.source = 'CLI argument'; |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | readEnvCookie(cookies, ['AUTH_TOKEN', 'TWITTER_AUTH_TOKEN'], 'authToken'); |
| 263 | readEnvCookie(cookies, ['CT0', 'TWITTER_CT0'], 'ct0'); |
| 264 | if (cookies.authToken && cookies.ct0) { |
| 265 | cookies.cookieHeader = cookieHeader(cookies.authToken, cookies.ct0); |
| 266 | return { cookies, warnings }; |
| 267 | } |
| 268 | if (disableBrowserCookies) { |
| 269 | if (!cookies.authToken) { |
| 270 | warnings.push('Missing auth_token - provide via --auth-token, AUTH_TOKEN env var, or disable BIRD_DISABLE_BROWSER_COOKIES to allow browser cookie lookup'); |
| 271 | } |
| 272 | if (!cookies.ct0) { |
| 273 | warnings.push('Missing ct0 - provide via --ct0, CT0 env var, or disable BIRD_DISABLE_BROWSER_COOKIES to allow browser cookie lookup'); |
| 274 | } |
| 275 | return { cookies, warnings }; |
| 276 | } |
| 277 | const sourcesToTry = resolveSources(options.cookieSource); |
| 278 | let getCookies; |
| 279 | try { |
| 280 | ({ getCookies } = await loadSweetCookie()); |
| 281 | } |
| 282 | catch (error) { |
| 283 | if (error?.code !== 'ERR_MODULE_NOT_FOUND' || |
| 284 | !String(error.message ?? '').includes('@steipete/sweet-cookie')) { |
| 285 | throw error; |
| 286 | } |
| 287 | warnings.push('Browser cookie lookup unavailable because vendored dependency @steipete/sweet-cookie is not installed.'); |
| 288 | if (!cookies.authToken) { |
| 289 | warnings.push('Missing auth_token - provide via --auth-token, AUTH_TOKEN env var, or login to x.com in Safari/Chrome/Firefox'); |
| 290 | } |
| 291 | if (!cookies.ct0) { |
| 292 | warnings.push('Missing ct0 - provide via --ct0, CT0 env var, or login to x.com in Safari/Chrome/Firefox'); |
| 293 | } |
| 294 | return { cookies, warnings }; |
| 295 | } |
| 296 | for (const source of sourcesToTry) { |
| 297 | const res = await readTwitterCookiesFromBrowser({ |
| 298 | getCookies, |
| 299 | source, |
| 300 | chromeProfile: options.chromeProfile, |
| 301 | firefoxProfile: options.firefoxProfile, |
| 302 | cookieTimeoutMs, |
| 303 | }); |
| 304 | warnings.push(...res.warnings); |
| 305 | if (res.cookies.authToken && res.cookies.ct0) { |
| 306 | return { cookies: res.cookies, warnings }; |
| 307 | } |
| 308 | } |
| 309 | if (!cookies.authToken) { |
| 310 | warnings.push('Missing auth_token - provide via --auth-token, AUTH_TOKEN env var, or login to x.com in Safari/Chrome/Firefox'); |
| 311 | } |
| 312 | if (!cookies.ct0) { |
| 313 | warnings.push('Missing ct0 - provide via --ct0, CT0 env var, or login to x.com in Safari/Chrome/Firefox'); |
| 314 | } |
| 315 | if (cookies.authToken && cookies.ct0) { |
| 316 | cookies.cookieHeader = cookieHeader(cookies.authToken, cookies.ct0); |
| 317 | } |
| 318 | return { cookies, warnings }; |
| 319 | } |
| 320 | //# sourceMappingURL=cookies.js.map |
| 321 |