@patrickdk77/script-defuse 0.2.0 → 0.3.1

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/README.md CHANGED
@@ -35,6 +35,8 @@ const result = await disarmPdf(bytes, { scriptPlugins: [formScriptPlugin()] });
35
35
 
36
36
  pdf-defuse removes every script unless a plugin keeps it. This plugin keeps the form scripts and removes the rest. From pdf-defuse 0.2.4 the plugin's findings go into the PDF's report at each script's location.
37
37
 
38
+ The plugin's answer is final, and its findings are what the script scores. Each finding's `weight` is what it adds to that script's score from `inspectScript`: a finding that strips weighs what `scoreWeights` or the table gives it, and one that only describes the script weighs 0. A form script the plugin keeps therefore adds nothing to the PDF's score from pdf-defuse 0.3.0 and office-defuse 0.3.0. Earlier versions scored a kept script at their own weight for it.
39
+
38
40
  Make one plugin per PDF. pdf-defuse hands it the document-level scripts first, and the plugin remembers the globals of the ones it kept, so a field script may call a helper they define. A field script that calls a helper from a document script the plugin removed is removed too.
39
41
 
40
42
  With [file-defuse](https://github.com/patrickdk77/file-defuse) 0.1.4 or later, every PDF and Office file already gets one.
package/dist/analyze.js CHANGED
@@ -196,10 +196,10 @@ function analyzeScript(input) {
196
196
  declare(program, top);
197
197
  // 2. What each binding is given: initializers and assignments. An assignment to a name nothing declares makes a
198
198
  // global, unless it is a property of the document, which an AcroForm script's bare names reach.
199
- const global = (name) => (!xfa && api_1.DOC_ALL.has(name) ? undefined : top.declare(name));
199
+ const declareGlobal = (name) => (!xfa && api_1.DOC_ALL.has(name) ? undefined : top.declare(name));
200
200
  const assign = (target, expr, scope) => {
201
201
  for (const name of patternNames(target)) {
202
- const b = scope.lookup(name) ?? global(name);
202
+ const b = scope.lookup(name) ?? declareGlobal(name);
203
203
  if (!b)
204
204
  continue;
205
205
  if (expr && target.type === 'Identifier')
@@ -231,11 +231,11 @@ function analyzeScript(input) {
231
231
  assign(n.left, n.right, scope);
232
232
  else
233
233
  for (const name of patternNames(n.left))
234
- (scope.lookup(name) ?? global(name))?.kinds.add(n.operator === '+=' ? 'data' : 'number');
234
+ (scope.lookup(name) ?? declareGlobal(name))?.kinds.add(n.operator === '+=' ? 'data' : 'number');
235
235
  }
236
236
  else if ((n.type === 'ForInStatement' || n.type === 'ForOfStatement') && n.left.type !== 'VariableDeclaration' && n.left.type !== 'MemberExpression') {
237
237
  for (const name of patternNames(n.left))
238
- (scope.lookup(name) ?? global(name))?.kinds.add(n.type === 'ForInStatement' ? 'string' : 'unknown');
238
+ (scope.lookup(name) ?? declareGlobal(name))?.kinds.add(n.type === 'ForInStatement' ? 'string' : 'unknown');
239
239
  }
240
240
  else if ((n.type === 'ForInStatement' || n.type === 'ForOfStatement') && n.left.type === 'VariableDeclaration') {
241
241
  for (const d of n.left.declarations)
@@ -243,7 +243,7 @@ function analyzeScript(input) {
243
243
  (scope.lookup(name) ?? scope.declare(name)).kinds.add(n.type === 'ForInStatement' ? 'string' : 'unknown');
244
244
  }
245
245
  else if (n.type === 'UpdateExpression' && n.argument.type === 'Identifier')
246
- (scope.lookup(n.argument.name) ?? global(n.argument.name))?.kinds.add('number');
246
+ (scope.lookup(n.argument.name) ?? declareGlobal(n.argument.name))?.kinds.add('number');
247
247
  for (const c of children(n))
248
248
  collect(c, inner);
249
249
  };
package/dist/plugins.js CHANGED
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.formScriptPlugin = formScriptPlugin;
4
4
  const engine_1 = require("./engine");
5
+ const score_1 = require("./score");
5
6
  /**
6
7
  * A plugin that keeps a script only when inspectScript calls it a form script, and removes every other one.
7
8
  *
@@ -22,8 +23,11 @@ function formScriptPlugin(options = {}) {
22
23
  if (result === 'passed')
23
24
  for (const name of r.definedGlobals)
24
25
  known.add(name);
25
- decisions.push({ script, result, findings: r.findings });
26
- return { result, findings: r.findings };
26
+ // The container scores the script by these weights alone, so they are the ones this script's score counted:
27
+ // a finding that strips, at the caller's weight, and nothing for one that only describes the script.
28
+ const findings = r.findings.map(f => ({ ...f, weight: f.action === 'strip' ? (0, score_1.weightOf)(f, options) : 0 }));
29
+ decisions.push({ script, result, findings });
30
+ return { result, findings };
27
31
  },
28
32
  };
29
33
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@patrickdk77/script-defuse",
3
- "version": "0.2.0",
3
+ "version": "0.3.1",
4
4
  "description": "Inspect document scripts: PDF form scripts in JavaScript and FormCalc, from AcroForm and XFA forms, and the VBA modules of Office documents. Keeps a form script only when every call and property it uses formats, checks or calculates form fields, and a macro only when it calls nothing that reaches outside its document. Reports external access, dynamic code and obfuscation. Includes a script plugin for pdf-defuse and office-defuse.",
5
5
  "license": "MIT",
6
6
  "author": "patrickdk77",