-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AG-27139, AG-27357 Add new trusted scriptlet — trusted-prune-inbound-…
…object. #372, #378 Squashed commit of the following: commit 5308378 Author: Slava Leleka <v.leleka@adguard.com> Date: Mon Nov 13 14:56:26 2023 +0300 Add indentation commit 386fb36 Author: Slava Leleka <v.leleka@adguard.com> Date: Mon Nov 13 14:56:19 2023 +0300 Add indentation commit 3d348db Author: Adam Wróblewski <adam@adguard.com> Date: Mon Nov 13 11:52:54 2023 +0100 Add logMessage Log message if functionName is not a function commit a826233 Merge: 8e37fe8 9abcbcb Author: Adam Wróblewski <adam@adguard.com> Date: Mon Nov 13 11:51:02 2023 +0100 Merge branch 'master' into feature/AG-27139 commit 8e37fe8 Author: Adam Wróblewski <adam@adguard.com> Date: Mon Nov 13 10:53:29 2023 +0100 Add getPrunePath helper Change inboundObject to functionName commit b850874 Author: Stanislav Atroschenko <s.atroschenko@adguard.com> Date: Mon Nov 13 10:23:16 2023 +0300 Update description commit 9f5e76e Author: Adam Wróblewski <adam@adguard.com> Date: Thu Nov 9 11:29:01 2023 +0100 Fix description commit d7084d8 Author: Adam Wróblewski <adam@adguard.com> Date: Wed Nov 8 18:30:25 2023 +0100 Add the correct test description Add another example to the description commit 36e3b24 Author: Slava Leleka <v.leleka@adguard.com> Date: Wed Nov 8 20:15:03 2023 +0300 Fix variable name commit 5c90a46 Author: Slava Leleka <v.leleka@adguard.com> Date: Wed Nov 8 20:14:56 2023 +0300 Fix variable name commit 7e46411 Author: Slava Leleka <v.leleka@adguard.com> Date: Wed Nov 8 20:14:12 2023 +0300 Update description commit 3d7f442 Author: Slava Leleka <v.leleka@adguard.com> Date: Wed Nov 8 20:13:42 2023 +0300 Add @added to description commit 8c63534 Author: Adam Wróblewski <adam@adguard.com> Date: Wed Nov 8 11:27:58 2023 +0100 Remove empty lines Add trustedScriptlet to description commit 5389294 Author: Adam Wróblewski <adam@adguard.com> Date: Tue Nov 7 18:34:56 2023 +0100 Add trusted-prune-inbound-object scriptlet
- Loading branch information
Showing
9 changed files
with
353 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import { | ||
hit, | ||
matchStackTrace, | ||
getPropertyInChain, | ||
getWildcardPropertyInChain, | ||
logMessage, | ||
isPruningNeeded, | ||
jsonPruner, | ||
getPrunePath, | ||
// following helpers are needed for helpers above | ||
toRegExp, | ||
getNativeRegexpTest, | ||
shouldAbortInlineOrInjectedScript, | ||
isEmptyObject, | ||
} from '../helpers/index'; | ||
|
||
/* eslint-disable max-len */ | ||
/** | ||
* @trustedScriptlet trusted-prune-inbound-object | ||
* | ||
* @description | ||
* Removes listed properties from the result of calling specific function (if payload contains `Object`) | ||
* and returns to the caller. | ||
* | ||
* Related UBO scriptlet: | ||
* https://github.com/gorhill/uBlock/commit/1c9da227d7 | ||
* | ||
* ### Syntax | ||
* | ||
* ```text | ||
* example.org#%#//scriptlet('trusted-prune-inbound-object', functionName[, propsToRemove [, obligatoryProps [, stack]]]) | ||
* ``` | ||
* | ||
* - `functionName` — required, the name of the function to trap, it must have an object as an argument | ||
* - `propsToRemove` — optional, string of space-separated properties to remove | ||
* - `obligatoryProps` — optional, string of space-separated properties | ||
* which must be all present for the pruning to occur | ||
* - `stack` — optional, string or regular expression that must match the current function call stack trace; | ||
* if regular expression is invalid it will be skipped | ||
* | ||
* > Note please that you can use wildcard `*` for chain property name, | ||
* > e.g. `ad.*.src` instead of `ad.0.src ad.1.src ad.2.src`. | ||
* | ||
* ### Examples | ||
* | ||
* 1. Removes property `example` from the payload of the Object.getOwnPropertyNames call | ||
* | ||
* ```adblock | ||
* example.org#%#//scriptlet('trusted-prune-inbound-object', 'Object.getOwnPropertyNames', 'example') | ||
* ``` | ||
* | ||
* For instance, the following call will return `['one']` | ||
* | ||
* ```html | ||
* Object.getOwnPropertyNames({ one: 1, example: true }) | ||
* ``` | ||
* | ||
* 2. Removes property `ads` from the payload of the Object.keys call | ||
* | ||
* ```adblock | ||
* example.org#%#//scriptlet('trusted-prune-inbound-object', 'Object.keys', 'ads') | ||
* ``` | ||
* | ||
* For instance, the following call will return `['one', 'two']` | ||
* | ||
* ```html | ||
* Object.keys({ one: 1, two: 2, ads: true }) | ||
* ``` | ||
* | ||
* 3. Removes property `foo.bar` from the payload of the JSON.stringify call | ||
* | ||
* ```adblock | ||
* example.org#%#//scriptlet('trusted-prune-inbound-object', 'JSON.stringify', 'foo.bar') | ||
* ``` | ||
* | ||
* For instance, the following call will return `'{"foo":{"a":2},"b":3}'` | ||
* | ||
* ```html | ||
* JSON.stringify({ foo: { bar: 1, a: 2 }, b: 3 }) | ||
* ``` | ||
* | ||
* 4. Removes property `foo.bar` from the payload of the JSON.stringify call if its error stack trace contains `test.js` | ||
* | ||
* ```adblock | ||
* example.org#%#//scriptlet('trusted-prune-inbound-object', 'JSON.stringify', 'foo.bar', '', 'test.js') | ||
* ``` | ||
* | ||
* 5. Call with only first and third argument will log the current hostname and matched payload at the console | ||
* | ||
* ```adblock | ||
* example.org#%#//scriptlet('trusted-prune-inbound-object', 'JSON.stringify', '', 'bar', '') | ||
* ``` | ||
* | ||
* @added unknown. | ||
*/ | ||
/* eslint-enable max-len */ | ||
export function trustedPruneInboundObject(source, functionName, propsToRemove, requiredInitialProps, stack = '') { | ||
if (!functionName) { | ||
return; | ||
} | ||
|
||
const nativeObjects = { | ||
nativeStringify: window.JSON.stringify, | ||
}; | ||
|
||
const { base, prop } = getPropertyInChain(window, functionName); | ||
if (!base || !prop || typeof base[prop] !== 'function') { | ||
const message = `${functionName} is not a function`; | ||
logMessage(source, message); | ||
return; | ||
} | ||
|
||
const prunePaths = getPrunePath(propsToRemove); | ||
const requiredPaths = getPrunePath(requiredInitialProps); | ||
|
||
const objectWrapper = (target, thisArg, args) => { | ||
let data = args[0]; | ||
if (typeof data === 'object') { | ||
data = jsonPruner(source, data, prunePaths, requiredPaths, stack, nativeObjects); | ||
args[0] = data; | ||
} | ||
return Reflect.apply(target, thisArg, args); | ||
}; | ||
|
||
const objectHandler = { | ||
apply: objectWrapper, | ||
}; | ||
|
||
base[prop] = new Proxy(base[prop], objectHandler); | ||
} | ||
|
||
trustedPruneInboundObject.names = [ | ||
'trusted-prune-inbound-object', | ||
// trusted scriptlets support no aliases | ||
]; | ||
|
||
trustedPruneInboundObject.injections = [ | ||
hit, | ||
matchStackTrace, | ||
getPropertyInChain, | ||
getWildcardPropertyInChain, | ||
logMessage, | ||
isPruningNeeded, | ||
jsonPruner, | ||
getPrunePath, | ||
// following helpers are needed for helpers above | ||
toRegExp, | ||
getNativeRegexpTest, | ||
shouldAbortInlineOrInjectedScript, | ||
isEmptyObject, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.