Skip to main content
Menu
Advisories

Host header not compared with HTTP/2 :authority, so vhost routing can differ from the front proxy

GHSA-M96M-V447-7QM3Published Updated GitHub

Description

@hapi/hapi does not compare the Host header with the HTTP/2 :authority pseudo-header. When the two values are different, hapi routes the request with the Host value. An attacker can then bypass a front proxy that applies host-based access rules to the :authority value. The severity is medium, because the attack needs HTTP/2 from end to end and a front proxy that trusts :authority.

Details

hapi sets request.info.host and request.info.hostname from the Host header. It uses the :authority pseudo-header only when the Host header is missing:

const host = (req.headers.host || req.headers[':authority'] || '').trim();

The router uses request.info.hostname to match vhost routes, and the CORS preflight lookup uses it too. An HTTP/2 client can send a Host header and an :authority pseudo-header with different values. RFC 9113 Section 8.3.1 says that a server should treat this request as malformed. hapi does not compare the two values. Thus, a front proxy that uses the :authority value and hapi can select different virtual hosts for the same request.

Fix

hapi now compares the Host header with the :authority pseudo-header when a request contains both. The comparison ignores letter case, and it treats the default port of the scheme as equal to no port. When the values are different, hapi rejects the request with a 400 Bad Request response. This check occurs before the onRequest extensions and before routing.

Every release up to and including 21.4.10 is affected.

Workarounds

If an upgrade is not possible, do one of these steps:

  • Configure the front proxy to replace the Host header with the :authority value on HTTP/2 requests.
  • Configure the front proxy to remove the Host header from HTTP/2 requests.
  • Add an onRequest extension that rejects a request when the two values are different:
server.ext('onRequest', (request, h) => {

    const { host, ':authority': authority } = request.headers;
    if (host && authority && host.trim().toLowerCase() !== authority.trim().toLowerCase()) {
        throw Boom.badRequest('Host header does not match :authority pseudo-header');
    }

    return h.continue;
});

References

Proof of concept

This script starts hapi with an HTTP/2 listener and two routes for /: one for the admin.example virtual host, and one for all other hosts. The client sends :authority: public.example, which a front proxy would accept, and Host: admin.example.

const Http2 = require('http2');
const Hapi = require('@hapi/hapi');

(async () => {

    const server = Hapi.server({ listener: Http2.createServer(), autoListen: true, port: 0 });
    server.route({ method: 'GET', path: '/', vhost: 'admin.example', handler: () => 'admin area' });
    server.route({ method: 'GET', path: '/', handler: () => 'public site' });
    await server.start();

    const client = Http2.connect(`http://localhost:${server.info.port}`);
    const req = client.request({ ':path': '/', ':authority': 'public.example', host: 'admin.example' });

    let body = '';
    let status;
    req.on('response', (headers) => status = headers[':status']);
    req.on('data', (chunk) => body += chunk);
    req.on('end', async () => {

        console.log(status, body);
        client.close();
        await server.stop();
    });
})();

Output on 21.4.10:

200 admin area

Output on 21.4.11:

400 {"statusCode":400,"error":"Bad Request","message":"Host header does not match :authority pseudo-header"}

Impact

This is a conflict between hapi and a front proxy about the target host of an HTTP/2 request. It affects applications behind a front proxy that applies access rules to the :authority value and sends the Host header without change. An attacker can then access vhost routes, or application logic that uses request.info.hostname, which the front proxy blocks. Applications that receive only HTTP/1.1 requests are not affected.

Affected versions

References

GitHub · hapijs/hapi