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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
/** @type {lazy} */
const lazy = {};
ChromeUtils.defineLazyGetter(lazy, "console", () => {
return console.createInstance({
prefix: "CaptchaDetectionChild",
maxLogLevelPref: "captchadetection.loglevel",
});
});
/**
* Abstract class for handling captchas.
*/
class CaptchaHandler {
static type = "abstract";
/**
* @param {CaptchaDetectionChild} actor - The window actor.
* @param {Event} _event - The initial event that created the actor.
* @param {boolean} skipConstructedNotif - Whether to skip the constructed notification. Used for captchas with multiple frames.
*/
constructor(actor, _event, skipConstructedNotif = false) {
/** @type {CaptchaDetectionChild} */
this.actor = actor;
if (!skipConstructedNotif) {
this.notifyConstructed();
}
}
notifyConstructed() {
lazy.console.debug(`CaptchaHandler constructed: ${this.constructor.type}`);
this.actor.sendAsyncMessage("CaptchaHandler:Constructed", {
type: this.constructor.type,
});
}
static matches(_document) {
throw new Error("abstract method");
}
updateState(state) {
this.actor.sendAsyncMessage("CaptchaState:Update", state);
}
onActorDestroy() {
lazy.console.debug("CaptchaHandler destroyed");
}
/**
* @param {Event} event - The event to handle.
*/
handleEvent(event) {
lazy.console.debug("CaptchaHandler got event:", event);
}
/**
* @param {ReceiveMessageArgument} message - The message to handle.
*/
receiveMessage(message) {
lazy.console.debug("CaptchaHandler got message:", message);
}
}
/**
* Handles Google Recaptcha v2 captchas.
*
* ReCaptcha v2 places two iframes in the page. One for the
* challenge and one for the checkmark. This handler listens
* for the checkmark being clicked or the challenge being
* shown. When either of these events happen, the handler
* sends a message to the parent actor. The handler then
* disconnects the mutation observer to avoid further
* processing.
*/
class GoogleRecaptchaV2Handler extends CaptchaHandler {
#enabled;
#mutationObserver;
static type = "g-recaptcha-v2";
constructor(actor, event) {
super(
actor,
event,
actor.document.location.pathname.endsWith("/bframe") ||
(Cu.isInAutomation &&
actor.document.location.pathname.endsWith(
"g_recaptcha_v2_checkbox.html"
))
);
this.#enabled = true;
this.#mutationObserver = new this.actor.contentWindow.MutationObserver(
this.#mutationHandler.bind(this)
);
this.#mutationObserver.observe(this.actor.document, {
childList: true,
subtree: true,
attributes: true,
attributeFilter: ["style"],
});
}
static matches(document) {
if (Cu.isInAutomation) {
return (
document
.getElementById("captchaType")
?.getAttribute("data-captcha-type") === GoogleRecaptchaV2Handler.type
);
}
return (
[
"https://www.google.com/recaptcha/api2/",
"https://www.google.com/recaptcha/enterprise/",
].some(match => document.location.href.startsWith(match)) &&
!document.location.search.includes("size=invisible")
);
}
#mutationHandler(_mutations, observer) {
if (!this.#enabled) {
return;
}
const token = this.actor.document.getElementById("recaptcha-token");
const initialized = token && token.value !== "";
if (!initialized) {
return;
}
const checkmark = this.actor.document.getElementById("recaptcha-anchor");
if (checkmark && checkmark.ariaChecked === "true") {
this.updateState({
type: GoogleRecaptchaV2Handler.type,
changes: "GotCheckmark",
});
this.#enabled = false;
observer.disconnect();
return;
}
const images = this.actor.document.getElementById("rc-imageselect");
if (images) {
this.updateState({
type: GoogleRecaptchaV2Handler.type,
changes: "ImagesShown",
});
this.#enabled = false;
observer.disconnect();
}
}
onActorDestroy() {
super.onActorDestroy();
this.#mutationObserver.disconnect();
}
}
/**
* Handles Cloudflare Turnstile captchas.
*
* Cloudflare Turnstile captchas have a success and fail div
* that are displayed when the captcha is completed. This
* handler listens for the success or fail div to be displayed
* and sends a message to the parent actor when either of
* these events happen. The handler then disconnects the
* mutation observer to avoid further processing.
* We use two mutation observers to detect shadowroot
* creation and then observe the shadowroot for the success
* or fail div.
*/
class CFTurnstileHandler extends CaptchaHandler {
#observingShadowRoot;
#mutationObserver;
static type = "cf-turnstile";
constructor(actor, event) {
super(actor, event);
this.#observingShadowRoot = false;
if (this.actor.document.body?.openOrClosedShadowRoot) {
this.#observeShadowRoot(this.actor.document.body.openOrClosedShadowRoot);
return;
}
this.#mutationObserver = new this.actor.contentWindow.MutationObserver(
this.#mutationHandler.bind(this)
);
this.#mutationObserver.observe(this.actor.document.documentElement, {
attributes: true,
});
}
static matchesRegex = new RegExp(
"https://challenges.cloudflare.com/cdn-cgi/challenge-platform/.+?/turnstile/if/ov2/av0/rcv/"
);
static matches(document) {
if (Cu.isInAutomation) {
return (
document
.getElementById("captchaType")
?.getAttribute("data-captcha-type") === CFTurnstileHandler.type
);
}
return CFTurnstileHandler.matchesRegex.test(document.location.href);
}
#mutationHandler(_mutations, observer) {
lazy.console.debug(_mutations);
if (this.#observingShadowRoot) {
return;
}
const shadowRoot = this.actor.document.body?.openOrClosedShadowRoot;
if (!shadowRoot) {
return;
}
observer.disconnect();
lazy.console.debug("Found shadowRoot", shadowRoot);
this.#observeShadowRoot(shadowRoot);
}
#observeShadowRoot(shadowRoot) {
if (this.#observingShadowRoot) {
return;
}
this.#observingShadowRoot = true;
this.#mutationObserver = new this.actor.contentWindow.MutationObserver(
(_mutations, observer) => {
const fail = shadowRoot.getElementById("fail");
const success = shadowRoot.getElementById("success");
if (!fail || !success) {
return;
}
if (fail.style.display !== "none") {
lazy.console.debug("Captcha failed");
this.updateState({
type: CFTurnstileHandler.type,
result: "Failed",
});
observer.disconnect();
return;
}
if (success.style.display !== "none") {
lazy.console.debug("Captcha succeeded");
this.updateState({
type: CFTurnstileHandler.type,
result: "Succeeded",
});
observer.disconnect();
}
}
).observe(shadowRoot, {
childList: true,
subtree: true,
attributes: true,
attributeFilter: ["style"],
});
}
onActorDestroy() {
super.onActorDestroy();
this.#mutationObserver.disconnect();
}
}
/**
* Handles Datadome captchas.
*
* Datadome works by placing an iframe that postMessages to
* the parent window. This actor attaches to the iframe
* that posts messages to the parent window. Therefore, we
* ask the parent actor to initialize a new actor in the
* parent window. That actor will then listen for messages
* from the iframe and send a message to the parent actor
* when the captcha is completed.
*/
class DatadomeHandler extends CaptchaHandler {
static type = "datadome";
constructor(actor, event) {
super(actor, event);
event.stopImmediatePropagation();
this.actor
.sendQuery("CaptchaDetection:Init", { type: DatadomeHandler.type })
.then(() => {
// re-dispatch the event
event.target.dispatchEvent(event);
});
}
static matches(document) {
if (Cu.isInAutomation) {
return (
document
.getElementById("captchaType")
?.getAttribute("data-captcha-type") === DatadomeHandler.type
);
}
return document.location.href.startsWith(
"https://geo.captcha-delivery.com/captcha/"
);
}
}
/**
* Handles hCaptcha captchas.
*
* hCaptcha works by placing two iframes in the page. One for
* the challenge and one for the checkbox. This handler listens
* for the challenge being shown or the checkbox being checked.
* When either of these events happen, the handler sends a
* message to the parent actor. The handler then disconnects
* the mutation observer to avoid further processing.
*/
class HCaptchaHandler extends CaptchaHandler {
#shown;
#checked;
#mutationObserver;
static type = "hCaptcha";
constructor(actor, event) {
let params = null;
try {
params = new URLSearchParams(actor.document.location.hash.slice(1));
} catch {
// invalid URL
super(actor, event, true);
return;
}
const frameType = params.get("frame");
super(actor, event, frameType === "challenge");
if (frameType === "challenge") {
this.#initChallengeHandler();
} else if (frameType === "checkbox") {
this.#initCheckboxHandler();
}
}
static matches(document) {
if (Cu.isInAutomation) {
return (
document
.getElementById("captchaType")
?.getAttribute("data-captcha-type") === HCaptchaHandler.type
);
}
return (
document.location.href.startsWith(
"https://newassets.hcaptcha.com/captcha/v1/"
) &&
document.location.pathname.endsWith("/static/hcaptcha.html") &&
!document.location.hash.includes("size=invisible") &&
!document.location.hash.includes("frame=checkbox-invisible")
);
}
#initChallengeHandler() {
this.#shown = false;
this.#mutationObserver = new this.actor.contentWindow.MutationObserver(
this.#challengeMutationHandler.bind(this)
);
this.#mutationObserver.observe(this.actor.document.body, {
attributes: true,
attributeFilter: ["aria-hidden"],
});
}
#challengeMutationHandler(_mutations, observer) {
if (this.#shown) {
return;
}
this.#shown = this.actor.document.body.ariaHidden !== "true";
if (!this.#shown) {
return;
}
this.updateState({
type: HCaptchaHandler.type,
changes: "shown",
});
observer.disconnect();
}
#initCheckboxHandler() {
this.#checked = false;
this.#mutationObserver = new this.actor.contentWindow.MutationObserver(
this.#checkboxMutationHandler.bind(this)
);
this.#mutationObserver.observe(this.actor.document, {
subtree: true,
attributes: true,
attributeFilter: ["aria-checked"],
});
}
#checkboxMutationHandler(_mutations, observer) {
if (this.#checked) {
return;
}
const checkbox = this.actor.document.getElementById("checkbox");
if (checkbox?.ariaChecked === "true") {
this.#checked = true;
this.updateState({
type: HCaptchaHandler.type,
changes: "passed",
});
observer.disconnect();
}
}
onActorDestroy() {
super.onActorDestroy();
this.#mutationObserver.disconnect();
}
}
/**
* Handles Arkose Labs captchas.
*
* We listen for network requests to the
* captcha API. When the response is received, we check for
* the presence of the expected keys and send a message to
* the parent actor with the state of the captcha.
*/
class ArkoseLabsHandler extends CaptchaHandler {
static type = "arkoseLabs";
constructor(actor) {
super(actor);
this.actor.sendQuery("CaptchaDetection:Init", {
type: ArkoseLabsHandler.type,
});
}
static matches(document) {
if (Cu.isInAutomation) {
return (
document
.getElementById("captchaType")
?.getAttribute("data-captcha-type") === ArkoseLabsHandler.type
);
}
return document.location.href.startsWith(
"https://client-api.arkoselabs.com/fc/assets/ec-game-core/game-core/"
);
}
}
/**
* This actor runs in the captcha's frame. It provides information
* about the captcha's state to the parent actor.
*/
export class CaptchaDetectionChild extends JSWindowActorChild {
actorCreated() {
lazy.console.debug("actorCreated");
}
/**
* @constant
* @type {Array<CaptchaHandler>}
*/
static #handlers = [
GoogleRecaptchaV2Handler,
CFTurnstileHandler,
DatadomeHandler,
HCaptchaHandler,
ArkoseLabsHandler,
];
/**
* @param {Event} event - The event that created the actor.
*/
#initCaptchaHandler(event) {
for (const handler of CaptchaDetectionChild.#handlers) {
if (handler.matches(this.document)) {
this.handler = new handler(this, event);
return;
}
}
}
actorDestroy() {
lazy.console.debug("actorDestroy()");
this.handler?.onActorDestroy();
}
/**
* @param {Event} event - The event to handle.
*/
handleEvent(event) {
if (
!this.handler &&
(event.type === "DOMContentLoaded" || event.type === "pageshow")
) {
this.#initCaptchaHandler(event);
return;
}
if (event.type === "pagehide") {
this.sendAsyncMessage("Page:Hide");
}
this.handler?.handleEvent(event);
}
/**
* @param {ReceiveMessageArgument} message - The message to handle.
*/
receiveMessage(message) {
if (this.handler) {
this.handler.receiveMessage(message);
}
}
}
/**
* @typedef lazy
* @type {object}
* @property {ConsoleInstance} console - console instance.
*/
|