Skip to content

Commit b036714

Browse files
authored
Fix certification warning (#107)
* Refactor code + remove error when 0 functions found * Bump version * Fix npm audit * Rename identifier and refactor logAudit function * Updated changelog and readme
1 parent 786e2ee commit b036714

5 files changed

Lines changed: 104 additions & 140 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ Plugin config description
3535
devMode: "development mode",
3636
packageOutPath: "location to create *.pbiviz file",
3737
generateResources: "it is used --resources flag in pbiviz tools",
38-
generatePbiviz: "it is used by --no-pbiviz flag in pbiviz tools"
38+
generatePbiviz: "it is used by --no-pbiviz flag in pbiviz tools",
39+
certificationAudit: "scan the bundle for forbidden calls (fetch, eval, XMLHttpRequest) and report them as errors",
40+
certificationFix: "remove forbidden calls from the bundle by replacing them with undefined; takes precedence over certificationAudit when both are set"
3941
};
4042
```
4143

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
This page contains information about changes to the **PowerBI-visuals-webpack-plugin**.
44

5+
## 5.0.1
6+
7+
* Fixed misleading output when no dangerous functions are found during certification audit or fix
8+
59
## 5.0.0
610

711
* Updated packages

package-lock.json

Lines changed: 53 additions & 92 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "powerbi-visuals-webpack-plugin",
3-
"version": "5.0.0",
3+
"version": "5.0.1",
44
"description": "PowerBI Custom Visuals webpack plugin",
55
"main": "src/index.js",
66
"keywords": [
@@ -29,13 +29,13 @@
2929
"ajv": "^8.18.0",
3030
"chalk": "^4.1.2",
3131
"compare-versions": "^6.1.1",
32-
"fs-extra": "^11.3.3",
32+
"fs-extra": "^11.3.4",
3333
"jszip": "^3.10.1",
3434
"lodash.clonedeep": "4.5.0",
3535
"webpack-sources": "^3.3.4"
3636
},
3737
"devDependencies": {
38-
"eslint": "^10.0.1",
38+
"eslint": "^10.0.2",
3939
"eslint-config-prettier": "^10.1.8",
4040
"eslint-plugin-n": "^17.18.0",
4141
"eslint-plugin-prettier": "^5.5.5",

src/index.js

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const pluginTemplate = require("../templates/plugin-template");
1818
const jsonTemplate = require("../templates/package-json-template");
1919

2020
const DEBUG = "_DEBUG";
21+
const FORBIDDEN_CALLS = ["fetch", "eval", "XMLHttpRequest"];
2122

2223
const base64Img = (filepath) => {
2324
let imageAsBase64 = fs.readFileSync(filepath, "base64"),
@@ -213,73 +214,69 @@ class PowerBICustomVisualsWebpackPlugin {
213214
sourceType: "module",
214215
plugins: ["jsx"],
215216
});
216-
let certificationAudit = {
217-
foundCalls: {},
218-
total: 0,
219-
};
220-
const callsToCheck = ["fetch", "eval", "XMLHttpRequest"];
217+
const foundCalls = {};
221218

222-
// Helper function to check and replace forbidden calls
223219
const checkAndReplace = (node, name) => {
224-
if (callsToCheck.includes(name)) {
225-
certificationAudit.foundCalls[name] =
226-
(certificationAudit.foundCalls[name] || 0) + 1;
227-
certificationAudit.total++;
228-
if (forceFix) {
229-
node.replaceWithSourceString("undefined");
230-
}
220+
if (!FORBIDDEN_CALLS.includes(name)) return;
221+
foundCalls[name] = (foundCalls[name] || 0) + 1;
222+
if (forceFix) {
223+
node.replaceWithSourceString("undefined");
224+
}
225+
};
226+
227+
const checkForbiddenIdentifier = (path, node) => {
228+
if (node.isIdentifier()) {
229+
checkAndReplace(path, node.node.name);
231230
}
232231
};
233232

234233
traverse(parsedCode, {
235234
CallExpression(path) {
236-
const callee = path.get("callee");
237-
if (callee.isIdentifier()) {
238-
checkAndReplace(path, callee.node.name);
239-
}
235+
checkForbiddenIdentifier(path, path.get("callee"));
240236
},
241-
242237
NewExpression(path) {
243-
const callee = path.get("callee");
244-
if (callee.isIdentifier()) {
245-
checkAndReplace(path, callee.node.name);
246-
}
238+
checkForbiddenIdentifier(path, path.get("callee"));
247239
},
248-
249240
MemberExpression(path) {
250-
const property = path.get("property");
251-
252-
if (property.isIdentifier()) {
253-
checkAndReplace(path, property.node.name);
254-
}
241+
checkForbiddenIdentifier(path, path.get("property"));
255242
},
256243
});
257244

258-
this.logAudit(certificationAudit, forceFix, audit, callsToCheck);
245+
this.logAudit(foundCalls, forceFix, audit);
259246
return generate(parsedCode, { retainLines: true }).code;
260247
}
261248

262-
logAudit(certificationAudit, forceFix, audit, callsToCheck) {
249+
logAudit(foundCalls, forceFix, audit) {
250+
const entries = Object.entries(foundCalls);
251+
const total = entries.reduce((sum, [, count]) => sum + count, 0);
252+
253+
if (total === 0 && !audit && !forceFix) return;
254+
255+
logger.separator();
256+
logger.info("External requests audit:");
257+
logger.info(
258+
"Read more about certification requirements here: https://learn.microsoft.com/en-us/power-bi/developer/visuals/power-bi-custom-visuals-certified#not-allowed",
259+
);
260+
261+
if (total === 0) {
262+
logger.info("No external requests found in the visual.");
263+
} else {
264+
for (const [name, count] of entries) {
265+
logger.warn(`${name} - Found ${count} times`);
266+
}
267+
}
268+
263269
if (forceFix) {
264270
logger.warn(
265-
`${certificationAudit.total} entries of ${callsToCheck.join(", ")} were removed. Test the visual before publishing`,
266-
);
267-
} else if (audit) {
268-
logger.separator();
269-
logger.info("External requests audit:");
270-
Object.keys(certificationAudit.foundCalls).forEach((key) => {
271-
logger.error(
272-
`${key} - Found ${certificationAudit.foundCalls[key]} times`,
273-
);
274-
});
275-
logger.info(
276-
"Read more about certification requirements here: https://learn.microsoft.com/en-us/power-bi/developer/visuals/power-bi-custom-visuals-certified#not-allowed",
271+
`${total} forbidden calls were removed. Test the visual before publishing.`,
277272
);
273+
} else if (total > 0) {
278274
logger.error(
279-
`Found ${certificationAudit.total} external requests in resulted package. Compile the package with --certification-fix flag to remove forbidden requests.`,
275+
`Found ${total} external request(s) in resulted package. Compile the package with --certification-fix flag to remove forbidden requests.`,
280276
);
281-
logger.separator();
282277
}
278+
279+
logger.separator();
283280
}
284281

285282
async _beforeCompile(callback) {

0 commit comments

Comments
 (0)