CVE-2021-23443 (published 2021-09-21T17:15Z)

This affects the package edge.js before 5.3.2. A type confusion vulnerability can be used to bypass input sanitization when the input to be rendered is an array (instead of a string or a SafeValue), even if {{ }} are used.

Severity: MEDIUM
CVSS v2 base score: 5.8 (AV:N/AC:M/Au:N/C:P/I:P/A: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-843
cwe_name: Access of Resource Using Incompatible Type ('Type Confusion')
description: The product allocates or initializes a resource such as a pointer, object, or variable using one type, but it later accesses that resource using a type that is incompatible with the original type.
extended_description: 
            When the product accesses the resource using an incompatible type, this could trigger logical errors because the resource does not have expected properties. In languages without memory safety, such as C and C++, type confusion can lead to out-of-bounds memory access.
            While this weakness is frequently associated with unions when parsing data with many different embedded object types in C, it can be present in any application that can interpret the same variable or memory location in multiple ways.
            This weakness is not unique to C and C++. For example, errors in PHP applications can be triggered by providing array parameters when scalars are expected, or vice versa. Languages such as Perl, which perform automatic conversion of a variable of one type when it is accessed as if it were another type, can also contain these issues.
         
         

Fix information:
repo_url: https://github.com/edge-js/edge
commit_details: repo_url: https://github.com/edge-js/edge
author: Harminder Virk
author_date: 2021-09-04 18:48:14+05:30
msg: fix: Cross-Site Scripting vulnerability reported by Snyk security team
repository: repo_url: https://github.com/edge-js/edge
repo_name: edge-js/edge
description: Node.js template with a breath of fresh air
repo_language: TypeScript
owner: edge-js
file_changes: filename: index.ts
change_type: ModificationType.MODIFY
diff: @@ -207,7 +207,7 @@ export interface TemplateContract {
   /**
    * Escape input
    */
-  escape<T>(input: T): T
+  escape(input: any): string
 
   /**
    * Rethrow exceptions by pointing back to edge source file and line number

programming_language: TypeScript
filename: index.ts
change_type: ModificationType.MODIFY
diff: @@ -28,12 +28,8 @@ export class SafeValue {
 /**
  * Escapes a given string
  */
-export function escape<T>(input: T): T extends SafeValue ? T['value'] : T {
-  return typeof input === 'string'
-    ? string.escapeHTML(input)
-    : input instanceof SafeValue
-    ? input.value
-    : input
+export function escape(input: any): string {
+  return input instanceof SafeValue ? input.value : string.escapeHTML(String(input))
 }
 
 /**
@@ -197,7 +193,7 @@ export class Template extends Macroable implements TemplateContract {
    * Escapes the value to be HTML safe. Only strings are escaped
    * and rest all values will be returned as it is.
    */
-  public escape<T>(input: T): T extends SafeValue ? T['value'] : T {
+  public escape(input: any): string {
     return escape(input)
   }
 

programming_language: TypeScript
filename: template.spec.ts
change_type: ModificationType.MODIFY
diff: @@ -140,11 +140,11 @@ test.group('Template', (group) => {
     assert.equal(template.escape('<h2> Hello world </h2>'), '&lt;h2&gt; Hello world &lt;/h2&gt;')
   })
 
-  test('do not escape values, which are not string', (assert) => {
+  test('stringify value during escape', (assert) => {
     const processor = new Processor()
     const compiler = new Compiler(loader, tags, processor, { cache: false })
     const template = new Template(compiler, {}, {}, processor)
-    assert.equal(template.escape(22), 22)
+    assert.equal(template.escape(22), '22')
   })
 
   test('do not escape values, which instance of safe value', (assert) => {
@@ -154,6 +154,27 @@ test.group('Template', (group) => {
     assert.equal(template.escape(safeValue('<h2> Hello world </h2>')), '<h2> Hello world </h2>')
   })
 
+  test('stringify array before escape', (assert) => {
+    const processor = new Processor()
+    const compiler = new Compiler(loader, tags, processor, { cache: false })
+    const template = new Template(compiler, {}, {}, processor)
+    assert.equal(template.escape(['<h2> Hello world </h2>']), '&lt;h2&gt; Hello world &lt;/h2&gt;')
+  })
+
+  test('stringify object before escape', (assert) => {
+    const processor = new Processor()
+    const compiler = new Compiler(loader, tags, processor, { cache: false })
+    const template = new Template(compiler, {}, {}, processor)
+    assert.equal(
+      template.escape({
+        toString() {
+          return '<h2> Hello world </h2>'
+        },
+      }),
+      '&lt;h2&gt; Hello world &lt;/h2&gt;'
+    )
+  })
+
   test('add macros to context', (assert) => {
     Template.macro('upper', (username) => {
       return username.toUpperCase()

programming_language: TypeScript