Skip to content

Commit

Permalink
AG-24527 Fix 'json-prune' — obligatoryProps and stack trace. #345 #348
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit 38f0da2
Author: Slava Leleka <v.leleka@adguard.com>
Date:   Thu Aug 10 13:22:40 2023 +0300

    Update test description

commit 4c9356b
Author: Slava Leleka <v.leleka@adguard.com>
Date:   Thu Aug 10 13:22:08 2023 +0300

    Update test description

commit 15b3d7c
Merge: 0e3b35c 1b81a02
Author: Adam Wróblewski <adam@adguard.com>
Date:   Thu Aug 10 11:08:30 2023 +0200

    Merge branch 'master' into fix/AG-24527

commit 0e3b35c
Merge: 31d149d 975ad11
Author: Adam Wróblewski <adam@adguard.com>
Date:   Fri Aug 4 14:49:41 2023 +0200

    Merge branch 'master' into fix/AG-24527

commit 31d149d
Author: Adam Wróblewski <adam@adguard.com>
Date:   Thu Aug 3 16:24:50 2023 +0200

    Fix issue with stack and obligatoryProps in json-prune
  • Loading branch information
AdamWr committed Aug 10, 2023
1 parent 1b81a02 commit 5f4949d
Show file tree
Hide file tree
Showing 5 changed files with 359 additions and 14 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
<!-- TODO: add @added tag to the files with specific version -->
<!-- during new scriptlets or redirects releasing -->

## [Unreleased]

### Fixed

- issue with `stack` in `json-prune` scriptlet
[#348](https://github.com/AdguardTeam/Scriptlets/issues/348)
- issue with `obligatoryProps` in `json-prune` scriptlet
[#345](https://github.com/AdguardTeam/Scriptlets/issues/345)

## [v1.9.62] - 2023-08-04

Expand Down Expand Up @@ -227,6 +235,7 @@ prevent inline `onerror` and match `link` tag [#276](https://github.com/AdguardT
- `metrika-yandex-tag` [#254](https://github.com/AdguardTeam/Scriptlets/issues/254)
- `googlesyndication-adsbygoogle` [#252](https://github.com/AdguardTeam/Scriptlets/issues/252)

[Unreleased]: https://github.com/AdguardTeam/Scriptlets/compare/v1.9.62...HEAD
[v1.9.62]: https://github.com/AdguardTeam/Scriptlets/compare/v1.9.61...v1.9.62
[v1.9.61]: https://github.com/AdguardTeam/Scriptlets/compare/v1.9.58...v1.9.61
[v1.9.58]: https://github.com/AdguardTeam/Scriptlets/compare/v1.9.57...v1.9.58
Expand Down
11 changes: 11 additions & 0 deletions src/helpers/get-wildcard-property-in-chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ export function getWildcardPropertyInChain(
});
}

// If base is an Array check elements in array
// https://github.com/AdguardTeam/Scriptlets/issues/345
if (Array.isArray(base)) {
base.forEach((key) => {
const nextBase = key;
if (nextBase !== undefined) {
getWildcardPropertyInChain(nextBase, chain, lookThrough, output);
}
});
}

const nextBase = base[prop];
chain = chain.slice(pos + 1);
if (nextBase !== undefined) {
Expand Down
29 changes: 26 additions & 3 deletions src/helpers/prune-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { hit } from './hit';
import { getWildcardPropertyInChain } from './get-wildcard-property-in-chain';
import { logMessage } from './log-message';
import { toRegExp } from './string-utils';
import { matchStackTrace } from './match-stack';

/**
* Checks if prunning is required
Expand All @@ -18,6 +19,7 @@ export function isPruningNeeded(
root: ChainBase,
prunePaths: string[],
requiredPaths: string[],
stack: string,
): boolean | undefined {
if (!root) {
return false;
Expand All @@ -31,7 +33,11 @@ export function isPruningNeeded(
const matchRegex = toRegExp(requiredPaths.join(''));
const shouldLog = matchRegex.test(rootString);
if (shouldLog) {
logMessage(source, `${window.location.hostname}\n${JSON.stringify(root, null, 2)}`, true);
logMessage(
source,
`${window.location.hostname}\n${JSON.stringify(root, null, 2)}\nStack trace:\n${new Error().stack}`,
true,
);
if (root && typeof root === 'object') {
logMessage(source, root, true, false);
}
Expand All @@ -40,6 +46,11 @@ export function isPruningNeeded(
}
}

if (stack && !matchStackTrace(stack, new Error().stack || '')) {
shouldProcess = false;
return shouldProcess;
}

const wildcardSymbols = ['.*.', '*.', '.*', '.[].', '[].', '.[]'];

for (let i = 0; i < requiredPaths.length; i += 1) {
Expand All @@ -50,6 +61,13 @@ export function isPruningNeeded(
// if the path has wildcard, getPropertyInChain should 'look through' chain props
const details = getWildcardPropertyInChain(root, requiredPath, hasWildcard);

// Do not prune if details is an empty Array
// https://github.com/AdguardTeam/Scriptlets/issues/345
if (!details.length) {
shouldProcess = false;
return shouldProcess;
}

// start value of 'shouldProcess' due to checking below
shouldProcess = !hasWildcard;

Expand Down Expand Up @@ -85,17 +103,22 @@ export const jsonPruner = (
root: ChainBase,
prunePaths: string[],
requiredPaths: string[],
stack: string,
): ArbitraryObject => {
if (prunePaths.length === 0 && requiredPaths.length === 0) {
logMessage(source, `${window.location.hostname}\n${JSON.stringify(root, null, 2)}`, true);
logMessage(
source,
`${window.location.hostname}\n${JSON.stringify(root, null, 2)}\nStack trace:\n${new Error().stack}`,
true,
);
if (root && typeof root === 'object') {
logMessage(source, root, true, false);
}
return root;
}

try {
if (isPruningNeeded(source, root, prunePaths, requiredPaths) === false) {
if (isPruningNeeded(source, root, prunePaths, requiredPaths, stack) === false) {
return root;
}

Expand Down
10 changes: 3 additions & 7 deletions src/scriptlets/json-prune.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,7 @@ import {
* @added v1.1.0.
*/
/* eslint-enable max-len */
export function jsonPrune(source, propsToRemove, requiredInitialProps, stack) {
if (!!stack && !matchStackTrace(stack, new Error().stack)) {
return;
}

export function jsonPrune(source, propsToRemove, requiredInitialProps, stack = '') {
const prunePaths = propsToRemove !== undefined && propsToRemove !== ''
? propsToRemove.split(/ +/)
: [];
Expand All @@ -115,7 +111,7 @@ export function jsonPrune(source, propsToRemove, requiredInitialProps, stack) {
// dealing with stringified json in args, which should be parsed.
// so we call nativeJSONParse as JSON.parse which is bound to JSON object
const root = nativeJSONParse.apply(JSON, args);
return jsonPruner(source, root, prunePaths, requiredPaths);
return jsonPruner(source, root, prunePaths, requiredPaths, stack);
};

// JSON.parse mocking
Expand All @@ -127,7 +123,7 @@ export function jsonPrune(source, propsToRemove, requiredInitialProps, stack) {
const responseJsonWrapper = function () {
const promise = nativeResponseJson.apply(this);
return promise.then((obj) => {
return jsonPruner(source, obj, prunePaths, requiredPaths);
return jsonPruner(source, obj, prunePaths, requiredPaths, stack);
});
};

Expand Down
Loading

0 comments on commit 5f4949d

Please sign in to comment.