CVE-2023-23630 (published 2023-02-01T01:15Z)

Eta is an embedded JS templating engine that works inside Node, Deno, and the browser. XSS attack - anyone using the Express API is impacted. The problem has been resolved. Users should upgrade to version 2.0.0. As a workaround, don't pass user supplied things directly to `res.render`. \n

CVSS v3 base score: 6.1, MEDIUM (CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:C/C:L/I:L/A:N)

Weakness information:
cwe_id: CWE-79
cwe_name: Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting')
description: The product does not neutralize or incorrectly neutralizes user-controllable input before it is placed in output that is used as a web page that is served to other users.
extended_description: 
            Cross-site scripting (XSS) vulnerabilities occur when:
            
               Untrusted data enters a web application, typically from a web request.
               The web application dynamically generates a web page that contains this untrusted data.
               During page generation, the application does not prevent the data from containing content that is executable by a web browser, such as JavaScript, HTML tags, HTML attributes, mouse events, Flash, ActiveX, etc.
               A victim visits the generated web page through a web browser, which contains malicious script that was injected using the untrusted data.
               Since the script comes from a web page that was sent by the web server, the victim's web browser executes the malicious script in the context of the web server's domain.
               This effectively violates the intention of the web browser's same-origin policy, which states that scripts in one domain should not be able to access resources or run code in a different domain.
            
            There are three main kinds of XSS:
            
               
                  Type 1: Reflected XSS (or Non-Persistent) - 
         			The server reads data directly from the HTTP request and reflects it back in the HTTP response. Reflected XSS exploits occur when an attacker causes a victim to supply dangerous content to a vulnerable web application, which is then reflected back to the victim and executed by the web browser. The most common mechanism for delivering malicious content is to include it as a parameter in a URL that is posted publicly or e-mailed directly to the victim. URLs constructed in this manner constitute the core of many phishing schemes, whereby an attacker convinces a victim to visit a URL that refers to a vulnerable site. After the site reflects the attacker's content back to the victim, the content is executed by the victim's browser.
               
                  Type 2: Stored XSS (or Persistent) - 
               The application stores dangerous data in a database, message forum, visitor log, or other trusted data store. At a later time, the dangerous data is subsequently read back into the application and included in dynamic content. From an attacker's perspective, the optimal place to inject malicious content is in an area that is displayed to either many users or particularly interesting users. Interesting users typically have elevated privileges in the application or interact with sensitive data that is valuable to the attacker. If one of these users executes malicious content, the attacker may be able to perform privileged operations on behalf of the user or gain access to sensitive data belonging to the user. For example, the attacker might inject XSS into a log message, which might not be handled properly when an administrator views the logs.
            
               
                  Type 0: DOM-Based XSS - 
               In DOM-based XSS, the client performs the injection of XSS into the page; in the other types, the server performs the injection. DOM-based XSS generally involves server-controlled, trusted script that is sent to the client, such as Javascript that performs sanity checks on a form before the user submits it. If the server-supplied script processes user-supplied data and then injects it back into the web page (such as with dynamic HTML), then DOM-based XSS is possible.
            
            
            Once the malicious script is injected, the attacker can perform a variety of malicious activities. The attacker could transfer private information, such as cookies that may include session information, from the victim's machine to the attacker. The attacker could send malicious requests to a web site on behalf of the victim, which could be especially dangerous to the site if the victim has administrator privileges to manage that site. Phishing attacks could be used to emulate trusted web sites and trick the victim into entering a password, allowing the attacker to compromise the victim's account on that web site. Finally, the script could exploit a vulnerability in the web browser itself possibly taking over the victim's machine, sometimes referred to as "drive-by hacking."
            In many cases, the attack can be launched without the victim even being aware of it. Even with careful users, attackers frequently use a variety of methods to encode the malicious portion of the attack, such as URL encoding or Unicode, so the request looks less suspicious.
         
         

Fix information:
repo_url: https://github.com/eta-dev/eta
commit_details: repo_url: https://github.com/eta-dev/eta
author: Ben Gubler
author_date: 2023-01-27 20:29:45-07:00
msg: Don't use data object for Eta configuration (#214)
repository: repo_url: https://github.com/eta-dev/eta
repo_name: eta-dev/eta
description: Embedded JS template engine for Node, Deno, and the browser. Lighweight, fast, and pluggable. Written in TypeScript
repo_language: TypeScript
owner: eta-dev
file_changes: filename: file-handlers.ts
change_type: ModificationType.MODIFY
diff: @@ -4,7 +4,6 @@ import EtaErr from "./err.ts";
 import compile from "./compile.ts";
 import { getConfig } from "./config.ts";
 import { getPath, readFile } from "./file-utils.ts";
-import { copyProps } from "./utils.ts";
 import { promiseImpl } from "./polyfills.ts";
 
 /* TYPES */
@@ -19,10 +18,6 @@ import type { TemplateFunction } from "./compile.ts";
 export type CallbackFn = (err: Error | null, str?: string) => void;
 
 interface DataObj {
-  /** Express.js settings may be stored here */
-  settings?: {
-    [key: string]: any; // eslint-disable-line @typescript-eslint/no-explicit-any
-  };
   [key: string]: any; // eslint-disable-line @typescript-eslint/no-explicit-any
 }
 
@@ -173,8 +168,7 @@ function includeFile(
  *
  * This can take two different function signatures:
  *
- * - `renderFile(filename, dataAndConfig, [cb])`
- *   - Eta will merge `dataAndConfig` into `eta.config`
+ * - `renderFile(filename, data, [cb])`
  * - `renderFile(filename, data, [config], [cb])`
  *
  * Note that renderFile does not immediately return the rendered result. If you pass in a callback function, it will be called with `(err, res)`. Otherwise, `renderFile` will return a `Promise` that resolves to the render result.
@@ -189,7 +183,6 @@ function includeFile(
  *
  * let rendered = await eta.renderFile("./template.eta", data, {cache: true})
  *
- * let rendered = await eta.renderFile("./template", {...data, cache: true})
  * ```
  */
 
@@ -224,14 +217,13 @@ function renderFile(
   /*
   Here we have some function overloading.
   Essentially, the first 2 arguments to renderFile should always be the filename and data
-  However, with Express, configuration options will be passed along with the data.
-  Thus, Express will call renderFile with (filename, dataAndOptions, cb)
-  And we want to also make (filename, data, options, cb) available
+  Express will call renderFile with (filename, data, cb)
+  We also want to make (filename, data, options, cb) available
   */
 
   let renderConfig: EtaConfigWithFilename;
   let callback: CallbackFn | undefined;
-  data = data || {}; // If data is undefined, we don't want accessing data.settings to error
+  data = data || {};
 
   // First, assign our callback function to `callback`
   // We can leave it undefined if neither parameter is a function;
@@ -250,26 +242,8 @@ function renderFile(
       (config as PartialConfig) || {},
     ) as EtaConfigWithFilename;
   } else {
-    // Otherwise, get the config from the data object
-    // And then grab some config options from data.settings
-    // Which is where Express sometimes stores them
-    renderConfig = getConfig(data as PartialConfig) as EtaConfigWithFilename;
-    if (data.settings) {
-      // Pull a few things from known locations
-      if (data.settings.views) {
-        renderConfig.views = data.settings.views;
-      }
-      if (data.settings["view cache"]) {
-        renderConfig.cache = true;
-      }
-      // Undocumented after Express 2, but still usable, esp. for
-      // items that are unsafe to be passed along with data, like `root`
-      const viewOpts = data.settings["view options"];
-
-      if (viewOpts) {
-        copyProps(renderConfig, viewOpts);
-      }
-    }
+    // Otherwise, get the default config
+    renderConfig = getConfig({}) as EtaConfigWithFilename;
   }
 
   // Set the filename option on the template
@@ -286,8 +260,7 @@ function renderFile(
  *
  * This can take two different function signatures:
  *
- * - `renderFile(filename, dataAndConfig, [cb])`
- *   - Eta will merge `dataAndConfig` into `eta.config`
+ * - `renderFile(filename, data, [cb])`
  * - `renderFile(filename, data, [config], [cb])`
  *
  * Note that renderFile does not immediately return the rendered result. If you pass in a callback function, it will be called with `(err, res)`. Otherwise, `renderFile` will return a `Promise` that resolves to the render result.
@@ -302,7 +275,6 @@ function renderFile(
  *
  * let rendered = await eta.renderFile("./template.eta", data, {cache: true})
  *
- * let rendered = await eta.renderFile("./template", {...data, cache: true})
  * ```
  */
 

programming_language: TypeScript
filename: file-handlers.ts
change_type: ModificationType.MODIFY
diff: @@ -4,7 +4,6 @@ import EtaErr from "./err.js";
 import compile from "./compile.js";
 import { getConfig } from "./config.js";
 import { getPath, readFile } from "./file-utils.js";
-import { copyProps } from "./utils.js";
 import { promiseImpl } from "./polyfills.js";
 
 /* TYPES */
@@ -15,10 +14,6 @@ import type { TemplateFunction } from "./compile.js";
 export type CallbackFn = (err: Error | null, str?: string) => void;
 
 interface DataObj {
-  /** Express.js settings may be stored here */
-  settings?: {
-    [key: string]: any; // eslint-disable-line @typescript-eslint/no-explicit-any
-  };
   [key: string]: any; // eslint-disable-line @typescript-eslint/no-explicit-any
 }
 
@@ -150,8 +145,7 @@ function includeFile(path: string, options: EtaConfig): [TemplateFunction, EtaCo
  *
  * This can take two different function signatures:
  *
- * - `renderFile(filename, dataAndConfig, [cb])`
- *   - Eta will merge `dataAndConfig` into `eta.config`
+ * - `renderFile(filename, data, [cb])`
  * - `renderFile(filename, data, [config], [cb])`
  *
  * Note that renderFile does not immediately return the rendered result. If you pass in a callback function, it will be called with `(err, res)`. Otherwise, `renderFile` will return a `Promise` that resolves to the render result.
@@ -166,7 +160,6 @@ function includeFile(path: string, options: EtaConfig): [TemplateFunction, EtaCo
  *
  * let rendered = await eta.renderFile("./template.eta", data, {cache: true})
  *
- * let rendered = await eta.renderFile("./template", {...data, cache: true})
  * ```
  */
 
@@ -192,14 +185,13 @@ function renderFile(
   /*
   Here we have some function overloading.
   Essentially, the first 2 arguments to renderFile should always be the filename and data
-  However, with Express, configuration options will be passed along with the data.
-  Thus, Express will call renderFile with (filename, dataAndOptions, cb)
-  And we want to also make (filename, data, options, cb) available
+  Express will call renderFile with (filename, data, cb)
+  We also want to make (filename, data, options, cb) available
   */
 
   let renderConfig: EtaConfigWithFilename;
   let callback: CallbackFn | undefined;
-  data = data || {}; // If data is undefined, we don't want accessing data.settings to error
+  data = data || {};
 
   // First, assign our callback function to `callback`
   // We can leave it undefined if neither parameter is a function;
@@ -216,26 +208,8 @@ function renderFile(
   if (typeof config === "object") {
     renderConfig = getConfig((config as PartialConfig) || {}) as EtaConfigWithFilename;
   } else {
-    // Otherwise, get the config from the data object
-    // And then grab some config options from data.settings
-    // Which is where Express sometimes stores them
-    renderConfig = getConfig(data as PartialConfig) as EtaConfigWithFilename;
-    if (data.settings) {
-      // Pull a few things from known locations
-      if (data.settings.views) {
-        renderConfig.views = data.settings.views;
-      }
-      if (data.settings["view cache"]) {
-        renderConfig.cache = true;
-      }
-      // Undocumented after Express 2, but still usable, esp. for
-      // items that are unsafe to be passed along with data, like `root`
-      const viewOpts = data.settings["view options"];
-
-      if (viewOpts) {
-        copyProps(renderConfig, viewOpts);
-      }
-    }
+    // Otherwise, get the default config
+    renderConfig = getConfig({}) as EtaConfigWithFilename;
   }
 
   // Set the filename option on the template
@@ -252,8 +226,7 @@ function renderFile(
  *
  * This can take two different function signatures:
  *
- * - `renderFile(filename, dataAndConfig, [cb])`
- *   - Eta will merge `dataAndConfig` into `eta.config`
+ * - `renderFile(filename, data, [cb])`
  * - `renderFile(filename, data, [config], [cb])`
  *
  * Note that renderFile does not immediately return the rendered result. If you pass in a callback function, it will be called with `(err, res)`. Otherwise, `renderFile` will return a `Promise` that resolves to the render result.
@@ -268,7 +241,6 @@ function renderFile(
  *
  * let rendered = await eta.renderFile("./template.eta", data, {cache: true})
  *
- * let rendered = await eta.renderFile("./template", {...data, cache: true})
  * ```
  */
 

programming_language: TypeScript
filename: file-handlers.spec.ts
change_type: ModificationType.MODIFY
diff: @@ -67,28 +67,10 @@ describe("Simple renderFile tests", () => {
     templates.define(fakeFilePath, compile("This template does not exist"));
 
     // renderFile should just look straight in the cache for the template
-    renderFile(fakeFilePath, { cache: true }, function (_err: Error | null, res?: string) {
+    renderFile(fakeFilePath, {}, { cache: true }, function (_err: Error | null, res?: string) {
       expect(res).toEqual("This template does not exist");
     });
   });
-
-  it("parses a simple template w/ settings from Express", async () => {
-    renderFile(
-      filePath,
-      {
-        name: "<p>Ben</p>",
-        cache: true,
-        settings: {
-          views: [path.join(__dirname, "templates"), path.join(__dirname, "othertemplates")],
-          "view cache": true,
-          "view options": { autoEscape: false },
-        },
-      },
-      function (_err: Error | null, res?: string) {
-        expect(res).toEqual("Hi <p>Ben</p>");
-      }
-    );
-  });
 });
 
 describe("File location tests", () => {
@@ -105,9 +87,9 @@ describe("File location tests", () => {
 
 describe("renderFile error tests", () => {
   it("render file with callback works on error", (done) => {
-    function cb(err: Error, _res?: string) {
+    function cb(err: Error | null, _res?: string) {
       expect(err).toBeTruthy();
-      expect(err.message).toMatch(
+      expect(err?.message).toMatch(
         buildRegEx(`
 var tR='',__l,__lP,include=E.include.bind(E),includeFile=E.includeFile.bind(E)
 function layout(p,d){__l=p;__lP=d}
@@ -120,7 +102,7 @@ if(cb){cb(null,tR)} return tR
       done();
     }
 
-    renderFile(errFilePath, { name: "Ada Lovelace", async: true }, cb);
+    renderFile(errFilePath, { name: "Ada Lovelace" }, { async: true }, cb);
   });
 
   test("throws with bad inner JS syntax using Promises", async () => {

programming_language: JavaScript