jso-protector 0.4.14 → 0.4.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/vm-status.js ADDED
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+
3
+ function truth(value) {
4
+ return value === true || value === 1 || (typeof value === "string" && value.trim().toLowerCase() === "true");
5
+ }
6
+
7
+ function count(value) {
8
+ const n = typeof value === "number" || (typeof value === "string" && /^\d+$/.test(value)) ? Number(value) : 0;
9
+ return Number.isSafeInteger(n) && n >= 0 ? n : 0;
10
+ }
11
+
12
+ function get(report, pascal, camel) {
13
+ return report[pascal] === undefined ? report[camel] : report[pascal];
14
+ }
15
+
16
+ function summarizeVmProtection(result, options = {}, expectedReports) {
17
+ const expected = expectedReports === undefined ? (truth(options.UseVMProtection) ? 1 : 0) : count(expectedReports);
18
+ const reports = Array.isArray(result.Reports) && result.Reports.length ? result.Reports : [result.Report];
19
+ const selected = reports.filter(report => report && typeof report === "object").filter(report => {
20
+ const enabled = get(report, "EnabledOptions", "enabledOptions");
21
+ return truth(get(report, "VMProtectionRequested", "vmProtectionRequested")) ||
22
+ truth(get(report, "VMProtectionApplied", "vmProtectionApplied")) ||
23
+ (Array.isArray(enabled) && enabled.includes("UseVMProtection"));
24
+ });
25
+ if (!selected.length && !expected) return null;
26
+ let confirmedReports = 0, virtualizedCount = 0, warningCount = 0;
27
+ for (const report of selected) {
28
+ const n = count(get(report, "VMProtectionVirtualizedCount", "vmProtectionVirtualizedCount") ??
29
+ get(report, "VirtualizedFunctionCount", "virtualizedFunctionCount"));
30
+ if (truth(get(report, "VMProtectionApplied", "vmProtectionApplied")) && n > 0) {
31
+ confirmedReports++;
32
+ virtualizedCount += n;
33
+ }
34
+ const dedicated = get(report, "VMProtectionWarnings", "vmProtectionWarnings");
35
+ const general = get(report, "Warnings", "warnings");
36
+ const warnings = (Array.isArray(dedicated) ? dedicated : []).concat(
37
+ Array.isArray(general) ? general.filter(w => typeof w === "string" && /\bVM\b|@virtualize/i.test(w)) : []);
38
+ warningCount += new Set(warnings.filter(w => typeof w === "string" && w.trim())).size;
39
+ }
40
+ const unconfirmedReports = Math.max(selected.length, expected) - confirmedReports;
41
+ return {
42
+ requested: true,
43
+ status: unconfirmedReports ? (confirmedReports ? "partial" : "unconfirmed") :
44
+ (warningCount ? "applied-with-warnings" : "applied"),
45
+ virtualizedCount, confirmedReports, unconfirmedReports, warningCount
46
+ };
47
+ }
48
+
49
+ function formatVmProtection(status) {
50
+ if (!status) return "";
51
+ if (status.status === "unconfirmed") {
52
+ return "VM protection: not confirmed by the report. Use --report <path> to inspect VMProtectionApplied, VMProtectionVirtualizedCount and VMProtectionWarnings; rerun to refresh an older report.";
53
+ }
54
+ let text = `VM protection: ${status.status === "partial" ? "partially confirmed" : "confirmed"} for ${status.virtualizedCount} function(s).`;
55
+ if (status.unconfirmedReports) text += ` ${status.unconfirmedReports} report(s) lack complete VM evidence.`;
56
+ if (status.warningCount) text += ` Review ${status.warningCount} VM warning(s) in the report for skipped functions.`;
57
+ return text;
58
+ }
59
+
60
+ module.exports = { summarizeVmProtection, formatVmProtection };
package/webpack.js CHANGED
@@ -13,7 +13,14 @@ class JsoProtectorWebpackPlugin {
13
13
  const processAssetsHook = compiler.hooks && compiler.hooks.thisCompilation;
14
14
  if (processAssetsHook && compiler.webpack && compiler.webpack.sources && compiler.webpack.Compilation) {
15
15
  const RawSource = compiler.webpack.sources.RawSource;
16
- const stage = compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE;
16
+ const stages = compiler.webpack.Compilation;
17
+ // Minifiers can decode escaped literals and strip the protection header.
18
+ // Protect after minimization/map generation, before content hashes settle.
19
+ const stage = typeof stages.PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE === "number"
20
+ ? stages.PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE
21
+ : (typeof stages.PROCESS_ASSETS_STAGE_DEV_TOOLING === "number"
22
+ ? stages.PROCESS_ASSETS_STAGE_DEV_TOOLING
23
+ : stages.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE) + 1;
17
24
 
18
25
  processAssetsHook.tap(pluginName, (compilation) => {
19
26
  compilation.hooks.processAssets.tapPromise({ name: pluginName, stage }, async (assets) => {