CVE-2022-21122 (published 2022-06-08T09:15Z)

The package metacalc before 0.0.2 are vulnerable to Arbitrary Code Execution when it exposes JavaScript's Math class to the v8 context. As the Math class is exposed to user-land, it can be used to get access to JavaScript's Function constructor.

Severity: HIGH
CVSS v2 base score: 7.5 (AV:N/AC:L/Au:N/C:P/I:P/A:P)
CVSS v3 base score: 9.8, CRITICAL (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H)

Weakness information:
cwe_id: CWE-94
cwe_name: Improper Control of Generation of Code ('Code Injection')
description: The product constructs all or part of a code segment using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the syntax or behavior of the intended code segment.
extended_description: 
            When a product allows a user's input to contain code syntax, it might be possible for an attacker to craft the code in such a way that it will alter the intended control flow of the product. Such an alteration could lead to arbitrary code execution.
            Injection problems encompass a wide variety of issues -- all mitigated in very different ways. For this reason, the most effective way to discuss these weaknesses is to note the distinct features which classify them as injection weaknesses. The most important issue to note is that all injection problems share one thing in common -- i.e., they allow for the injection of control plane data into the user-controlled data plane. This means that the execution of the process may be altered by sending code in through legitimate data channels, using no other mechanism. While buffer overflows, and many other flaws, involve the use of some further issue to gain execution, injection problems need only for the data to be parsed. The most classic instantiations of this category of weakness are SQL injection and format string vulnerabilities.
         
         

Fix information:
repo_url: https://github.com/metarhia/metacalc
commit_details: repo_url: https://github.com/metarhia/metacalc
author: Timur Shemsedinov
author_date: 2022-05-15 23:49:46+03:00
msg: Prevent arbitrary js code execution

PR-URL: https://github.com/metarhia/metacalc/pull/16
repository: repo_url: https://github.com/metarhia/metacalc
repo_name: metarhia/metacalc
description: Spreadsheet calculations for Metarhia 🧮
repo_language: JavaScript
owner: metarhia
file_changes: filename: CHANGELOG.md
change_type: ModificationType.MODIFY
diff: @@ -2,6 +2,8 @@
 
 ## [Unreleased][unreleased]
 
+- Prevent arbitrary js code execution
+
 ## [0.0.1][] - 2022-05-13
 
 - First simple implementation

programming_language: Markdown
filename: sheet.js
change_type: ModificationType.MODIFY
diff: @@ -2,8 +2,20 @@
 
 const metavm = require('metavm');
 
+const wrap = (target) =>
+  new Proxy(target, {
+    get: (target, prop) => {
+      if (prop === 'constructor') return null;
+      const value = target[prop];
+      if (typeof value === 'number') return value;
+      return wrap(value);
+    },
+  });
+
+const math = wrap(Math);
+
 const getValue = (target, prop) => {
-  if (prop === 'Math') return Math;
+  if (prop === 'Math') return math;
   const { expressions, data } = target;
   if (!expressions.has(prop)) return data.get(prop);
   const expression = expressions.get(prop);

programming_language: TypeScript
filename: unit.js
change_type: ModificationType.MODIFY
diff: @@ -48,3 +48,19 @@ metatests.test('JavaScript Math', async (test) => {
   test.strictSame(sheet.values['I1'], Math.sin(Math.sqrt(Math.pow(100, -2))));
   test.end();
 });
+
+metatests.test('Prevent arbitrary js code execution', async (test) => {
+  const sheet = new Sheet();
+  sheet.cells['A1'] =
+    '=Math.constructor.constructor("console.log(\\"Hello, World!\\")")();';
+  try {
+    const res = sheet.values['A1'];
+    test.strictSame(res, undefined);
+  } catch (error) {
+    test.strictSame(
+      error.message,
+      `Cannot read property '${'constructor'}' of null`
+    );
+  }
+  test.end();
+});

programming_language: TypeScript