5 injection vulnerabilities hackers don't want developers to know about (and how to prevent them)

This page summarizes the projects mentioned and recommended in the original post on /r/node

Our great sponsors
  • SurveyJS - Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • WorkOS - The modern identity platform for B2B SaaS
  • DOMPurify

    DOMPurify - a DOM-only, super-fast, uber-tolerant XSS sanitizer for HTML, MathML and SVG. DOMPurify works with a secure default, but offers a lot of configurability and hooks. Demo:

  • body, input.value property, or body are all different). If you need to insert untrusted input into raw HTML, use a well-tested sanitizer such as DOMPurify.

    Setting a strong Content Security Policy without unsafe-inline or unsafe-eval in the script-src or default-src directives is an effective defense-in-depth) measure to prevent modern browsers from executing attacker code even if the attacker is able to insert </code> elements into the page.</p> <p><strong>3. HTTP API injection</strong></p> <p>RESTful APIs, GraphQL, and other HTTP-based APIs are ubiquitous. When a web application makes an API call to another service, injection vulnerabilities are possible when that request includes untrusted input.</p> <p>Consider a contrived example in which a web app integrates with a payments service that has a REST API endpoint for creating a subscription: <code>POST /subscriptions/{product_id}?price_usd=<price></code> where <code>price_usd</code> is optional, and a pre-configured price is used if omitted. If an attacker controls the value of <code>product_id</code> and passes a value of <code>desired_product_id?price_id=0</code>, the web app would end up making a request to <code>POST /subscriptions/desired_product_id?price_id=0</code>, which would allow the attacker to sign up for a free subscription.</p> <p>In JavaScript, the standard way to sanitize untrusted inputs in URL paths is <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent"><code>encodeURIComponent</code></a>, which replaces problematic characters such as <code>?</code> and <code>/</code> with safe percent-encoded sequences. When inserting untrusted input into URL query parameters, <a href="https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/URLSearchParams"><code>new URLSearchParams(queryParams)</code></a> provides a convenient, safe interface for building a query string from a JavaScript object of key-value pairs.</p> <p><strong>4. Shell injection</strong></p> <p>Backend APIs sometimes need to execute external commands on the machine where they run. Consider an API that performs <a href="https://en.wikipedia.org/wiki/WHOIS">WHOIS</a> lookups for a requested domain by executing the <code>whois</code> command locally.</p> <p>Consider the following <strong>vulnerable</strong> Node.js code:</p> <pre><code>const whois = child_process.execSync(`whois ${whoisRequest.domain}`); </code></pre> <p>If an attacker can pass the domain <code>reddit.com && rm -rf /</code>, the backend will execute the command <code>whois reddit.com && rm -rf /</code>. The <a href="https://nodejs.org/api/child_process.html#child_processexecsynccommand-options"><code>child_process.execSync</code></a> function passes the command string to the shell (<code>/bin/sh</code> by default on Linux), which parses <code>&& rm -rf /</code> as a subsequent command to wipe the filesystem.</p> <p>To avoid this issue, <strong>never pass untrusted input to a shell</strong>. Instead, use an interface such as <a href="https://nodejs.org/api/child_process.html#child_processexecfilesyncfile-args-options"><code>child_process.execFileSync</code></a> that executes a specific binary (which shouldn't be a shell!) and passes arguments <em>as an array</em>:</p> <pre><code>const whois = child_process.execFileSync("whois", [whoisRequest.domain]); </code></pre> <p>Now, even if the user passes a domain <code>reddit.com && rm -rf /</code>, that entire string will be passed as the command-line argument to <code>whois</code>, which will exit with an error but will not cause any harmful side-effects. Perhaps an even better solution would be to use a library to perform WHOIS queries without needing to execute a separate command.</p> <p>Astute readers may point out that validating the domain against a regex would also likely prevent shell injection in this case. However, avoiding the possibility of shell injection by using a safe interface that keeps untrusted input away from a shell's command parser is a more robust solution that avoids shell injection in all cases.</p> <p><strong>5. Path traversal</strong></p> <p>Finally, a path traversal vulnerability arises when an untrusted input is inserted into a filesystem path, which can cause the wrong file to be read or even written. Consider a backend API that reads a file at the path <code>/teams/${team_id}/${report_name}.csv</code>. If an attacker controls the value of <code>report_name</code> but not <code>team_id</code>, they could pass a <code>report_name</code> of <code>../other_team_id/private.</code> This would cause the file <code>/teams/team_id/../other_team_id/private.csv</code> (resolved to <code>/teams/other_team_id/private.csv</code>) to be read, leaking data from a different team.</p> <p>To avoid path traversal vulnerabilities, <strong>never use untrusted input in file or directory names</strong>. It's safest always to control the names of files and directories, including IDs that you generate and control (e.g., UUIDs, KSUIDs, etc.). If the name of a file or directory absolutely <em>must</em> be derived from untrusted input, consider hashing it (e.g., using SHA-256) or at least encoding it into a format that doesn't include dots or slashes (e.g., <a href="https://datatracker.ietf.org/doc/html/rfc4648#section-5">URL-safe base64</a>).</p> <p>​</p> <p>Know of good Node.js libraries for avoiding injection vulnerabilities? Let folks know in the comments!</p> </div><!-- SC_ON -->

  • React

    The library for web and native user interfaces.

  • The most robust protection against XSS is to use a client-side framework like React that lets you intuitively render untrusted inputs on a page without risking XSS. Be sure to avoid using dangerouslySetInnerHTML, which bypasses XSS protections.

  • SurveyJS

    Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App. With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.

    SurveyJS logo
  • sanitizer-api

  • The upcoming Sanitizer API - kinda like a native DOMPurify that provides el.setHTML() and Document.parseHTML()

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts