Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,52 @@
});

test.describe('solveCaptchaInfo', () => {
test('solves the captcha for the correct action data', async ({ createConfiguredDbp }) => {
test('solves the captcha for the correct action data', async ({ page, createConfiguredDbp }) => {

Check failure on line 133 in injected/integration-test/broker-protection-tests/broker-protection-captcha.spec.js

View workflow job for this annotation

GitHub Actions / snapshots

'page' is declared but its value is never read.

Check failure on line 133 in injected/integration-test/broker-protection-tests/broker-protection-captcha.spec.js

View workflow job for this annotation

GitHub Actions / unit (ubuntu-latest)

'page' is declared but its value is never read.
const dbp = await createConfiguredDbp(BROKER_PROTECTION_CONFIGS.default);
await dbp.navigatesTo(imageCaptchaTargetPage);
await dbp.receivesInlineAction(createSolveImageCaptchaAction({ selector: imageCaptchaResponseSelector }));
dbp.getSuccessResponse();

await dbp.isCaptchaTokenFilled(imageCaptchaResponseSelector);
});

test('retry fails with permanently invalid element type', async ({ page, createConfiguredDbp }) => {
const dbp = await createConfiguredDbp(BROKER_PROTECTION_CONFIGS.default);
await dbp.navigatesTo('image-captcha.html');

// Replace input with a div element which cannot accept token injection
await page.evaluate(() => {
const element = document.getElementById('svgCaptchaInputId');
if (element) {
const div = document.createElement('div');
div.id = 'svgCaptchaInputId';
div.textContent = 'This is a div, not an input';
element.parentNode?.replaceChild(div, element);
}
});

await dbp.simulateSubscriptionMessage('onActionReceived', {
state: {
action: {
actionType: 'solveCaptcha',
id: 'retry-invalid-element',
selector: '#svgCaptchaInputId',
retry: {
environment: 'web',
maxAttempts: 3,
interval: { ms: 200 },
},
},
data: {
token: 'RETRY_FAIL_TOKEN',
},
},
});

// Should fail after all retry attempts because div is invalid for token injection
const response = await dbp.collector.waitForMessage('actionCompleted');
dbp.isErrorMessage(response);
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,7 @@ test.describe('Broker Protection communications', () => {
const response = await dbp.collector.waitForMessage('actionCompleted');
dbp.isSuccessMessage(response);
});

test('ensuring retry doesnt apply everywhere', async ({ page }, workerInfo) => {
const dbp = BrokerProtectionPage.create(page, workerInfo.project.use);
await dbp.enabled();
Expand Down
1 change: 1 addition & 0 deletions injected/src/features/broker-protection.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export default class BrokerProtection extends ContentFeature {
};
}
}

return retryConfig;
}
}
1 change: 1 addition & 0 deletions injected/src/features/broker-protection/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* @property {string} [captchaType]
* @property {string} [injectCaptchaHandler]
* @property {string} [dataSource]
* @property {{environment: string, maxAttempts: number, interval: {ms: number}}} [retry]
*/

/**
Expand Down
19 changes: 16 additions & 3 deletions injected/src/features/broker-protection/utils/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/**
* Gets the owner document of an element or uses document as fallback
*
* @param {Node|Element} element
* @return {Document} The owner document
*/
function getOwnerDocument(element) {
return element.ownerDocument || document;
}

/**
* Get a single element.
*
Expand Down Expand Up @@ -75,7 +85,8 @@ export function getElementMatches(element, selector) {
* @return {boolean}
*/
function matchesXPath(element, selector) {
const xpathResult = document.evaluate(selector, element, null, XPathResult.BOOLEAN_TYPE, null);
const ownerDoc = getOwnerDocument(element);
const xpathResult = ownerDoc.evaluate(selector, element, null, XPathResult.BOOLEAN_TYPE, null);

return xpathResult.booleanValue;
}
Expand Down Expand Up @@ -131,7 +142,8 @@ function safeQuerySelector(element, selector) {
*/
function safeQuerySelectorXPath(element, selector) {
try {
const match = document.evaluate(selector, element, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
const ownerDoc = getOwnerDocument(element);
const match = ownerDoc.evaluate(selector, element, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
const single = match?.singleNodeValue;
if (single) {
return /** @type {HTMLElement} */ (single);
Expand All @@ -150,8 +162,9 @@ function safeQuerySelectorXPath(element, selector) {
*/
function safeQuerySelectorAllXpath(element, selector) {
try {
const ownerDoc = getOwnerDocument(element);
// gets all elements matching the xpath query
const xpathResult = document.evaluate(selector, element, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
const xpathResult = ownerDoc.evaluate(selector, element, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
if (xpathResult) {
/** @type {HTMLElement[]} */
const matchedNodes = [];
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes are outside the scope of this task. However, they aim to enhance the PIR debugger experience, as some actions currently trigger errors.

Expand Down
Loading