Skip to content

Commit 1c9da22

Browse files
committed
Add trusted-prune-inbound-object scriptlet
As per discussion with filter list maintainers. To perform object pruning for any given call which has an object as argument (hence "inbound"). Since `json-prune-stringify` scriptlet is a specific form of pruning inbound objects, it has been removed. The arguments for `trusted-prune-inbound-object` in order are: - The name of the property to trap. Must be a function, and must exist when the scriptlet tries to install the trap. - The position of the object to prune in the argument list when the trapped function is called. The position is 1-based and must be an integer greater than 0. - The properties to prune (as with `json-prune`) - The properties which must all be present for pruning to occur (as with `json-prune`) - Varargs: - `, dontOverwrite, 1`: do not modify the target inbound object Examples: Remove `title` and `name` properties before passing the object to `JSON.stringify` call: example.org##+js(trusted-prune-inbound-object, JSON.stringify, 1, title name) Remove `status` property before passing the object to `Object.keys` call but do not modify caller's instance of the object: example.org##+js(trusted-prune-inbound-object, Object.keys, 1, status, , dontOverwrite, 1)
1 parent 287f771 commit 1c9da22

File tree

1 file changed

+56
-36
lines changed

1 file changed

+56
-36
lines changed

assets/resources/scriptlets.js

Lines changed: 56 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,42 +1429,6 @@ function jsonPrune(
14291429
});
14301430
}
14311431

1432-
/******************************************************************************/
1433-
1434-
builtinScriptlets.push({
1435-
name: 'json-prune-stringify.js',
1436-
fn: jsonPruneStringify,
1437-
dependencies: [
1438-
'object-prune.fn',
1439-
'safe-self.fn',
1440-
],
1441-
});
1442-
function jsonPruneStringify(
1443-
rawPrunePaths = '',
1444-
rawNeedlePaths = '',
1445-
stackNeedle = ''
1446-
) {
1447-
const safe = safeSelf();
1448-
const stackNeedleDetails = safe.initPattern(stackNeedle, { canNegate: true });
1449-
const extraArgs = safe.getExtraArgs(Array.from(arguments), 3);
1450-
JSON.stringify = new Proxy(JSON.stringify, {
1451-
apply: function(target, thisArg, args) {
1452-
const objBefore = args[0];
1453-
if ( objBefore instanceof Object ) {
1454-
const objAfter = objectPruneFn(
1455-
objBefore,
1456-
rawPrunePaths,
1457-
rawNeedlePaths,
1458-
stackNeedleDetails,
1459-
extraArgs
1460-
);
1461-
args[0] = objAfter || objBefore;
1462-
}
1463-
return Reflect.apply(target, thisArg, args);
1464-
},
1465-
});
1466-
}
1467-
14681432
/*******************************************************************************
14691433
*
14701434
* json-prune-fetch-response.js
@@ -4071,3 +4035,59 @@ function trustedClickElement(
40714035
}
40724036

40734037
/******************************************************************************/
4038+
4039+
builtinScriptlets.push({
4040+
name: 'trusted-prune-inbound-object.js',
4041+
requiresTrust: true,
4042+
fn: trustedPruneInboundObject,
4043+
dependencies: [
4044+
'object-prune.fn',
4045+
'safe-self.fn',
4046+
],
4047+
});
4048+
function trustedPruneInboundObject(
4049+
entryPoint = '',
4050+
argPos = '',
4051+
rawPrunePaths = '',
4052+
rawNeedlePaths = ''
4053+
) {
4054+
if ( entryPoint === '' ) { return; }
4055+
let context = globalThis;
4056+
let prop = entryPoint;
4057+
for (;;) {
4058+
const pos = prop.indexOf('.');
4059+
if ( pos === -1 ) { break; }
4060+
context = context[prop.slice(0, pos)];
4061+
if ( context instanceof Object === false ) { return; }
4062+
prop = prop.slice(pos+1);
4063+
}
4064+
if ( typeof context[prop] !== 'function' ) { return; }
4065+
const argIndex = parseInt(argPos);
4066+
if ( isNaN(argIndex) ) { return; }
4067+
if ( argIndex < 1 ) { return; }
4068+
const safe = safeSelf();
4069+
const extraArgs = safe.getExtraArgs(Array.from(arguments), 4);
4070+
context[prop] = new Proxy(context[prop], {
4071+
apply: function(target, thisArg, args) {
4072+
const targetArg = argIndex <= args.length
4073+
? args[argIndex-1]
4074+
: undefined;
4075+
if ( targetArg instanceof Object ) {
4076+
const objBefore = extraArgs.dontOverwrite
4077+
? safe.JSON_parse(safe.JSON_stringify(targetArg))
4078+
: targetArg;
4079+
const objAfter = objectPruneFn(
4080+
objBefore,
4081+
rawPrunePaths,
4082+
rawNeedlePaths,
4083+
{ matchAll: true },
4084+
extraArgs
4085+
);
4086+
args[argIndex-1] = objAfter || objBefore;
4087+
}
4088+
return Reflect.apply(target, thisArg, args);
4089+
},
4090+
});
4091+
}
4092+
4093+
/******************************************************************************/

0 commit comments

Comments
 (0)