Skip to content

Commit 6e466cf

Browse files
committed
Add [trusted-]edit-inbound-object scriptlets
As discussed with filter list maintainers. @Scriptlet edit-inbound-object.js @description Prune properties from an object passed as argument to a specific method. Properties can only be removed. @param propChain Property chain of the method to trap. @param argPos 0-based position of the argument. Use negative integer for position relative to the end. @param jsonq A uBO-flavored JSONPath query. @Scriptlet trusted-edit-inbound-object.js @description Edit properties of an object passed as argument to a specific method. Properties can be assigned new values. @param propChain Property chain of the method to trap. @param argPos 0-based position of the argument. Use negative integer for position relative to the end. @param jsonq A uBO-flavored JSONPath query.
1 parent 754f617 commit 6e466cf

File tree

1 file changed

+133
-2
lines changed

1 file changed

+133
-2
lines changed

src/js/resources/json-edit.js

Lines changed: 133 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ registerScriptlet(editOutboundObjectFn, {
7676
* Prune properties from an object returned by a specific method.
7777
* Properties can only be removed.
7878
*
79+
* @param propChain
80+
* Property chain of the method to trap.
81+
*
7982
* @param jsonq
8083
* A uBO-flavored JSONPath query.
8184
*
@@ -96,10 +99,13 @@ registerScriptlet(editOutboundObject, {
9699
* @scriptlet trusted-edit-outbound-object.js
97100
*
98101
* @description
99-
* Edit properties from an object returned by a specific method.
102+
* Edit properties of an object returned by a specific method.
100103
* Properties can be assigned new values.
101104
*
102-
* @param jsonq
105+
* @param propChain
106+
* Property chain of the method to trap.
107+
*
108+
* @param jsonq
103109
* A uBO-flavored JSONPath query.
104110
*
105111
* */
@@ -166,6 +172,131 @@ registerScriptlet(trustedJsonEdit, {
166172
/******************************************************************************/
167173
/******************************************************************************/
168174

175+
function editInboundObjectFn(
176+
trusted = false,
177+
propChain = '',
178+
argPosRaw = '',
179+
jsonq = '',
180+
) {
181+
if ( propChain === '' ) { return; }
182+
const safe = safeSelf();
183+
const logPrefix = safe.makeLogPrefix(
184+
`${trusted ? 'trusted-' : ''}edit-inbound-object`,
185+
propChain,
186+
jsonq
187+
);
188+
const jsonp = JSONPath.create(jsonq);
189+
if ( jsonp.valid === false || jsonp.value !== undefined && trusted !== true ) {
190+
return safe.uboLog(logPrefix, 'Bad JSONPath query');
191+
}
192+
const argPos = parseInt(argPosRaw, 10);
193+
if ( isNaN(argPos) ) { return; }
194+
const getArgPos = args => {
195+
if ( argPos >= 0 ) {
196+
if ( args.length <= argPos ) { return; }
197+
return argPos;
198+
}
199+
if ( args.length < -argPos ) { return; }
200+
return args.length + argPos;
201+
};
202+
const editObj = obj => {
203+
let clone;
204+
try {
205+
clone = safe.JSON_parse(safe.JSON_stringify(obj));
206+
} catch {
207+
}
208+
if ( typeof clone !== 'object' || clone === null ) { return; }
209+
if ( jsonp.apply(clone) === 0 ) { return; }
210+
safe.uboLog(logPrefix, 'Edited');
211+
if ( safe.logLevel > 1 ) {
212+
safe.uboLog(logPrefix, `After edit:\n${safe.JSON_stringify(clone, null, 2)}`);
213+
}
214+
return clone;
215+
};
216+
proxyApplyFn(propChain, function(context) {
217+
const i = getArgPos(context.args);
218+
if ( i !== undefined ) {
219+
const obj = editObj(context.args[i]);
220+
if ( obj ) {
221+
context.args[i] = obj;
222+
}
223+
}
224+
return context.reflect();
225+
});
226+
}
227+
registerScriptlet(editInboundObjectFn, {
228+
name: 'edit-inbound-object.fn',
229+
dependencies: [
230+
JSONPath,
231+
proxyApplyFn,
232+
safeSelf,
233+
],
234+
});
235+
236+
/******************************************************************************/
237+
/**
238+
* @scriptlet edit-inbound-object.js
239+
*
240+
* @description
241+
* Prune properties from an object passed as argument to a specific method.
242+
* Properties can only be removed.
243+
*
244+
* @param propChain
245+
* Property chain of the method to trap.
246+
*
247+
* @param argPos
248+
* 0-based position of the argument. Use negative integer for position relative
249+
* to the end.
250+
*
251+
* @param jsonq
252+
* A uBO-flavored JSONPath query.
253+
*
254+
* */
255+
256+
function editInboundObject(propChain = '', argPos = '', jsonq = '') {
257+
editInboundObjectFn(false, propChain, argPos, jsonq);
258+
}
259+
registerScriptlet(editInboundObject, {
260+
name: 'edit-inbound-object.js',
261+
dependencies: [
262+
editInboundObjectFn,
263+
],
264+
});
265+
266+
/******************************************************************************/
267+
/**
268+
* @scriptlet trusted-edit-inbound-object.js
269+
*
270+
* @description
271+
* Edit properties of an object passed as argument to a specific method.
272+
* Properties can be assigned new values.
273+
*
274+
* @param propChain
275+
* Property chain of the method to trap.
276+
*
277+
* @param argPos
278+
* 0-based position of the argument. Use negative integer for position relative
279+
* to the end.
280+
*
281+
* @param jsonq
282+
* A uBO-flavored JSONPath query.
283+
*
284+
* */
285+
286+
function trustedEditInboundObject(propChain = '', argPos = '', jsonq = '') {
287+
editInboundObjectFn(true, propChain, argPos, jsonq);
288+
}
289+
registerScriptlet(trustedEditInboundObject, {
290+
name: 'trusted-edit-inbound-object.js',
291+
requiresTrust: true,
292+
dependencies: [
293+
editInboundObjectFn,
294+
],
295+
});
296+
297+
/******************************************************************************/
298+
/******************************************************************************/
299+
169300
function jsonEditXhrResponseFn(trusted, jsonq = '') {
170301
const safe = safeSelf();
171302
const logPrefix = safe.makeLogPrefix(

0 commit comments

Comments
 (0)