From d4cc07f8a60972ef2f193928dc188de7d315d761 Mon Sep 17 00:00:00 2001 From: Elizaveta Egorova Date: Mon, 7 Oct 2024 17:36:54 +0300 Subject: [PATCH 1/2] =?UTF-8?q?AG-31636=20Improve=20'prevent-xhr'=20?= =?UTF-8?q?=E2=80=94=20modify=20response.=20#415?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Squashed commit of the following: commit 92b79820d763a1d9cb180a0354018fdd00313e82 Author: jellizaveta Date: Mon Oct 7 16:15:38 2024 +0300 remove duplicate in compatibility-table commit 33f2e9d6b2ddad099f772c890a0fceee9bee01d9 Author: jellizaveta Date: Mon Oct 7 16:08:36 2024 +0300 update compatibility-table commit 019c9ab2e1eba44719d00c4cdeb62c8b96e3810a Author: jellizaveta Date: Mon Oct 7 16:02:53 2024 +0300 update changelog commit 600895afbf88c1f30bbee18eb1a89f2fe7b5c904 Author: jellizaveta Date: Mon Oct 7 15:56:03 2024 +0300 Revert "Update changelog" This reverts commit 94e7d3005c7c4f9618ecbae5a031c8b2253c59c9. commit 0790360f718aa0d1941cc5dbc13a18009240b434 Author: jellizaveta Date: Mon Oct 7 15:48:27 2024 +0300 Revert "revert wiki .md files, update compatibility-table.json" This reverts commit 1e5458dfd3229f95fedadecab70b81f07fb76109. commit 1e5458dfd3229f95fedadecab70b81f07fb76109 Author: jellizaveta Date: Mon Oct 7 15:39:10 2024 +0300 revert wiki .md files, update compatibility-table.json commit 94e7d3005c7c4f9618ecbae5a031c8b2253c59c9 Author: jellizaveta Date: Mon Oct 7 15:18:40 2024 +0300 Update changelog commit 4bf90714d84bc72d9bea8289e463086727e9bd4f Author: jellizaveta Date: Mon Oct 7 15:13:31 2024 +0300 update compatibility-table.json commit 5a6f20e1be994f5b539409d35b4fbcda800a9ec5 Author: jellizaveta Date: Mon Oct 7 15:03:33 2024 +0300 update tests commit 3cc23c111edce49dc05f5a56147d7498237159a6 Merge: 076ea890 756c9184 Author: jellizaveta Date: Mon Oct 7 14:55:13 2024 +0300 resolve conflicts commit 076ea890afc4df84954b790ead0b8e051405d104 Author: jellizaveta Date: Mon Oct 7 14:46:43 2024 +0300 AG-31636 Improve — modify response --- CHANGELOG.md | 5 ++++ scripts/compatibility-table.json | 13 ++++++-- src/scriptlets/prevent-xhr.js | 7 +++-- tests/scriptlets/prevent-xhr.test.js | 44 ++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e0c9ebf3..81b4ce30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,8 +16,13 @@ The format is based on [Keep a Changelog], and this project adheres to [Semantic - `prevent-canvas` scriptlet [#451] +### Changed + +- improve `prevent-xhr` — modify response [#415] + [Unreleased]: https://github.com/AdguardTeam/Scriptlets/compare/v1.12.1...HEAD [#451]: https://github.com/AdguardTeam/Scriptlets/issues/451 +[#415]: https://github.com/AdguardTeam/Scriptlets/issues/415 ## [v1.12.1] - 2024-09-20 diff --git a/scripts/compatibility-table.json b/scripts/compatibility-table.json index d264c072..b88b8d14 100644 --- a/scripts/compatibility-table.json +++ b/scripts/compatibility-table.json @@ -165,7 +165,7 @@ }, { "adg": "prevent-xhr", - "ubo": "no-xhr-if.js (prevent-xhr.js)" + "ubo": "prevent-xhr.js (no-xhr-if.js)" }, { "adg": "prevent-popads-net" @@ -384,6 +384,15 @@ }, { "ubo": "trusted-suppress-native-method.js" + }, + { + "ubo": "trusted-prevent-xhr.js" + }, + { + "ubo": "trusted-prevent-dom-bypass.js" + }, + { + "ubo": "trusted-override-element-method.js" } ], "redirects": [ @@ -587,4 +596,4 @@ "ubo": "noop-0.5s.mp3" } ] -} +} \ No newline at end of file diff --git a/src/scriptlets/prevent-xhr.js b/src/scriptlets/prevent-xhr.js index 83ebc6ee..b8a835f2 100644 --- a/src/scriptlets/prevent-xhr.js +++ b/src/scriptlets/prevent-xhr.js @@ -46,9 +46,9 @@ import { * - `value` is string or regular expression for matching the value of the option * passed to `XMLHttpRequest.open()` call * - `randomize` — defaults to `false` for empty responseText, - * optional argument to randomize responseText of matched XMLHttpRequest's response; possible values: - * - `true` to randomize responseText, random alphanumeric string of 10 symbols - * - colon-separated pair `name:value` string value to customize responseText data where + * optional argument to randomize responseText and response of matched XMLHttpRequest's response; possible values: + * - `true` to randomize responseText and response, random alphanumeric string of 10 symbols + * - colon-separated pair `name:value` string value to customize responseText and response data where * - `name` — only `length` supported for now * - `value` — range on numbers, for example `100-300`, limited to 500000 characters * @@ -169,6 +169,7 @@ export function preventXHR(source, propsToMatch, customResponseText) { if (customResponseText) { const randomText = generateRandomResponse(customResponseText); if (randomText) { + modifiedResponse = randomText; modifiedResponseText = randomText; } else { logMessage(source, `Invalid randomize parameter: '${customResponseText}'`); diff --git a/tests/scriptlets/prevent-xhr.test.js b/tests/scriptlets/prevent-xhr.test.js index dad85d44..e7d69f0a 100644 --- a/tests/scriptlets/prevent-xhr.test.js +++ b/tests/scriptlets/prevent-xhr.test.js @@ -147,6 +147,50 @@ if (isSupported) { assert.strictEqual(xhr.getAllResponseHeaders(), expectedAllHeaders, 'getAllResponseHeaders() is mocked'); }); + test('Args, method matched, randomize response (length:25000-30000)', async (assert) => { + const METHOD = 'GET'; + const URL = `${FETCH_OBJECTS_PATH}/test01.json`; + const MATCH_DATA = ['method:GET', 'length:25000-30000']; + + runScriptlet(name, MATCH_DATA); + + const done = assert.async(); + + const xhr = new XMLHttpRequest(); + xhr.open(METHOD, URL); + xhr.onload = () => { + assert.strictEqual(xhr.readyState, 4, 'Response done'); + assert.strictEqual(typeof xhr.response, 'string', 'Response mocked'); + assert.ok( + xhr.response.length > 20000, + `Response randomized, response length: ${xhr.response.length}`, + ); + assert.strictEqual(window.hit, 'FIRED', 'hit function fired'); + done(); + }; + xhr.send(); + }); + + test('Empty arg, prevent all, randomize response', async (assert) => { + const METHOD = 'GET'; + const URL = `${FETCH_OBJECTS_PATH}/test01.json`; + const MATCH_DATA = ['', 'true']; + + runScriptlet(name, MATCH_DATA); + + const done = assert.async(); + + const xhr = new XMLHttpRequest(); + xhr.open(METHOD, URL); + xhr.onload = () => { + assert.strictEqual(typeof xhr.response, 'string', 'Response mocked'); + assert.ok(xhr.response.length > 0, 'Response randomized'); + assert.strictEqual(window.hit, 'FIRED', 'hit function fired'); + done(); + }; + xhr.send(); + }); + test('Empty arg, prevent all, randomize response text', async (assert) => { const METHOD = 'GET'; const URL = `${FETCH_OBJECTS_PATH}/test01.json`; From 90eaf3d10c4f920e310a12ed99f03cf3f6c25902 Mon Sep 17 00:00:00 2001 From: Atlassian Bamboo Date: Mon, 7 Oct 2024 17:37:01 +0300 Subject: [PATCH 2/2] skipci: Automatic increment build number --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1f5cc1a1..3527e567 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@adguard/scriptlets", - "version": "1.12.3", + "version": "1.12.4", "description": "AdGuard's JavaScript library of Scriptlets and Redirect resources", "scripts": { "build": "babel-node -x .js,.ts scripts/build.js",