Skip to content

Commit

Permalink
AG-18869 Improve json-prune — fix issue with logging null. #282
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit b4a5884
Merge: 98cedec 85e6beb
Author: Adam Wróblewski <[email protected]>
Date:   Tue Feb 28 15:14:09 2023 +0100

    Merge branch 'master' into fix/AG-18869

commit 98cedec
Author: Adam Wróblewski <[email protected]>
Date:   Mon Feb 27 18:05:45 2023 +0100

    Remove objectToString from import
    and related helpers
    Update comment
    Update logMessage description

commit 3cda968
Author: Adam Wróblewski <[email protected]>
Date:   Wed Feb 22 13:01:35 2023 +0100

    Fix test

commit 68cf621
Author: Adam Wróblewski <[email protected]>
Date:   Tue Feb 21 16:58:23 2023 +0100

    Fix issue with logging null
    Improve logging
  • Loading branch information
AdamWr committed Feb 28, 2023
1 parent 85e6beb commit d6edd08
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/helpers/log-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* and use 'forced' argument otherwise.
*
* @param {Object} source required, scriptlet properties
* @param {string} message required, message to log
* @param {any} message required, message to log
* @param {boolean} [forced=false] to log message unconditionally
* @param {boolean} [convertMessageToString=true] to convert message to string
*/
Expand Down
5 changes: 5 additions & 0 deletions src/helpers/string-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@ export const parseDelayArg = (delay) => {
* @returns {string} object's string representation
*/
export const objectToString = (obj) => {
// In case if the type of passed obj is different than Object
// https://github.com/AdguardTeam/Scriptlets/issues/282
if (!obj || typeof obj !== 'object') {
return String(obj);
}
return isEmptyObject(obj)
? '{}'
: getObjectEntries(obj)
Expand Down
16 changes: 8 additions & 8 deletions src/scriptlets/json-prune.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ import {
matchStackTrace,
getWildcardPropertyInChain,
logMessage,
objectToString,
// following helpers are needed for helpers above
toRegExp,
isEmptyObject,
getObjectEntries,
getNativeRegexpTest,
shouldAbortInlineOrInjectedScript,
} from '../helpers/index';
Expand Down Expand Up @@ -113,7 +110,10 @@ export function jsonPrune(source, propsToRemove, requiredInitialProps, stack) {
const matchRegex = toRegExp(requiredPaths.join(''));
const shouldLog = matchRegex.test(rootString);
if (shouldLog) {
logMessage(source, `${window.location.hostname} ${objectToString(root)}`, true);
logMessage(source, `${window.location.hostname}\n${JSON.stringify(root, null, 2)}`, true);
if (root && typeof root === 'object') {
logMessage(source, root, true, false);
}
shouldProcess = false;
return shouldProcess;
}
Expand Down Expand Up @@ -161,7 +161,10 @@ export function jsonPrune(source, propsToRemove, requiredInitialProps, stack) {
*/
const jsonPruner = (root) => {
if (prunePaths.length === 0 && requiredPaths.length === 0) {
logMessage(source, `${window.location.hostname} ${objectToString(root)}`, true);
logMessage(source, `${window.location.hostname}\n${JSON.stringify(root, null, 2)}`, true);
if (root && typeof root === 'object') {
logMessage(source, root, true, false);
}
return root;
}

Expand Down Expand Up @@ -232,11 +235,8 @@ jsonPrune.injections = [
matchStackTrace,
getWildcardPropertyInChain,
logMessage,
objectToString,
// following helpers are needed for helpers above
toRegExp,
isEmptyObject,
getObjectEntries,
getNativeRegexpTest,
shouldAbortInlineOrInjectedScript,
];
52 changes: 46 additions & 6 deletions tests/scriptlets/json-prune.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,19 +484,26 @@ test('can NOT remove propsToRemove if nested requiredInitialProps has wildcard b
});

test('does NOT remove propsToRemove if invoked without parameter propsToRemove and return hostname', (assert) => {
console.log = (message) => {
assert.ok(message.includes(window.location.hostname), 'should log hostname in console');
assert.ok(message.includes('a:"1" b:"2"'), 'should log parameters in console');
assert.expect(2);
console.log = (...args) => {
if (args.length === 1) {
assert.ok(args[0].includes(window.location.hostname), 'should log hostname in console');
assert.ok(args[0].includes('"a": 1,\n "b": 2'), 'should log parameters in console');
}
nativeConsole(...args);
};
runScriptlet('json-prune');
JSON.parse('{"a":1, "b":2}');
});

test('logs matched object and hostname if invoked with only second arg', (assert) => {
assert.expect(2);
console.log = (message) => {
assert.ok(message.includes(window.location.hostname), 'should log hostname in console');
assert.ok(message.includes('a:"1"'), 'should log parameters in console');
console.log = (...args) => {
if (args.length === 1) {
assert.ok(args[0].includes(window.location.hostname), 'should log hostname in console');
assert.ok(args[0].includes('"a": 1'), 'should log parameters in console');
}
nativeConsole(...args);
};
runScriptlet('json-prune', '', '"a":1');
JSON.parse('{"a":1}');
Expand Down Expand Up @@ -556,3 +563,36 @@ test('does not remove propsToRemove - invalid regexp pattern for stack match', (
runScriptlet('json-prune', 'x', '', stackArg);
assert.deepEqual(JSON.parse('{"x":1}'), { x: 1 }, 'should NOT remove propsToRemove');
});

test('logs null', (assert) => {
assert.expect(2);
console.log = (message) => {
assert.ok(message.includes(window.location.hostname), 'should log hostname in console');
assert.ok(message.includes('null'), 'should log parameters in console');
nativeConsole(message);
};
runScriptlet('json-prune');
JSON.parse(null);
});

test('logs 0', (assert) => {
assert.expect(2);
console.log = (message) => {
assert.ok(message.includes(window.location.hostname), 'should log hostname in console');
assert.ok(message.endsWith('0'), 'should log parameters in console');
nativeConsole(message);
};
runScriptlet('json-prune');
JSON.parse(0);
});

test('logs 10', (assert) => {
assert.expect(2);
console.log = (message) => {
assert.ok(message.includes(window.location.hostname), 'should log hostname in console');
assert.ok(message.endsWith('10'), 'should log parameters in console');
nativeConsole(message);
};
runScriptlet('json-prune');
JSON.parse(10);
});

0 comments on commit d6edd08

Please sign in to comment.