logo

Database

Lack of data validation In http-proxy-middleware

Description

http-proxy-middleware router host+path substring matching allows Host-header-driven backend routing bypass

Summary

http-proxy-middleware documents router proxy-table entries as host, path, or host+path selectors, but the host+path implementation uses unanchored substring matching on attacker-controlled request metadata. As a result, a crafted Host header that is only a superstring match for a configured host+path key can still route a request to an unintended backend.

Details

Tested code state:

    validated on tag v4.0.0-beta.5

    corresponding commit: 339f09ede860197807d4fd99ed9020fa5d0bd358

Relevant code locations:

    src/router.ts

    src/http-proxy-middleware.ts

Affected public API:

    createProxyMiddleware({ router: { 'host/path': 'http://target' } })

Code explanation:

When a proxy-table router key contains /, getTargetFromProxyTable() concatenates attacker-controlled req.headers.host and req.url into a single hostAndPath string, then accepts the route if:

hostAndPath.indexOf(key) > -1

That is a substring test, not an exact host match plus intended path match. In the validated PoC, the configured router key is:

localhost:3000/api

but the attacker-controlled host is:

evillocalhost:3000

and the request path is:

/api

The concatenated attacker-controlled string:

evillocalhost:3000/api

still contains the configured router key as a substring, so the middleware selects the alternate backend even though the host is not equal to the configured host.

Exploit path:

    the application enables the documented proxy-table router feature with at least one host+path rule

    an external attacker sends an ordinary HTTP request with a crafted Host header

    HttpProxyMiddleware.prepareProxyRequest() applies router selection before proxying

    getTargetFromProxyTable() accepts the crafted Host + path string through substring matching

    the request is proxied to the wrong backend

PoC

Create these files in the same working directory and run:

bash ./run.sh

File: run.sh

#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_URL="https://github.com/chimurai/http-proxy-middleware.git"
REPO_REF="v4.0.0-beta.5"
WORKDIR="$(mktemp -d "${SCRIPT_DIR}/.tmp-repro.XXXXXX")"
TARGET_REPO_DIR="${WORKDIR}/repo"...

File: Dockerfile

FROM node:22-bullseye

WORKDIR /work

COPY repo/package.json repo/yarn.lock /work/repo/

RUN corepack enable \
  && cd /work/repo \...

File: verify.mjs

import http from 'node:http';
import fs from 'node:fs';
import assert from 'node:assert/strict';

import { createProxyMiddleware } from '/work/repo/dist/index.js';

const ROUTER_KEY = 'localhost:3000/api';
const CRAFTED_HOST = 'evillocalhost:3000';...

This PoC starts:

    one default backend returning DEFAULT

    one alternate backend returning SECRET

    one proxy using:

createProxyMiddleware({
  target: 'http://127.0.0.1:3101',
  router: {
    [ROUTER_KEY]: 'http://127.0.0.1:3102',
  },
});

It then sends:

    a baseline request to /api with Host: safe.example:3000

    a crafted request to /api with Host: evillocalhost:3000

Observed result from the validated PoC:

    baseline request: STEP baseline-route body=DEFAULT

    crafted request: STEP crafted-route body=SECRET

    success marker: RESULT reproduced host_header_injection router substring match bypass

The PoC is considered successful only if:

    the baseline request stays on the default backend

    the crafted request reaches the alternate backend

    the crafted host is not equal to the configured router host

Impact

This is a backend-selection integrity issue in a documented library feature. Applications that use host+path router-table rules for backend segmentation, tenant routing, or separation of public and more sensitive upstreams can have that routing boundary bypassed by an unauthenticated external client using an ordinary crafted Host header.

Mitigation

Update Impact

Minimal update. May introduce new vulnerabilities or breaking changes.

Ecosystem
Component
Affected version
Patched versions