Course contentsShow
← Node.js
Lesson 1806 of 1,98016. Security Hardening and Threat MitigationFree lesson

Protecting Against HTTP Parameter Pollution

Configure your application to handle duplicate parameters in a predictable and secure manner.

In this lesson, you will learn how to defend your Node.js applications against HTTP Parameter Pollution (HPP) by enforcing predictable handling of duplicate query or body parameters.

The Problem: Unexpected Parameter Resolution

HTTP Parameter Pollution occurs when an attacker sends multiple parameters with the same name in a single request (e.g., ?id=123&id=456). Because different web frameworks and underlying engines handle these duplicates differently—some taking the first value, some the last, and others concatenating them into an array—your application may process data in a way that diverges from your security logic.

If your authentication or authorization middleware expects a single id but your business logic sees an array, an attacker might bypass filters or inject unexpected values into your database queries.

Handling Duplicates Predictably

The primary defense is to adopt a "fail-fast" or "strict-parsing" approach. Instead of allowing the underlying framework to guess, you should explicitly define how duplicates are handled.

In Express, for example, the default behavior often interprets duplicate parameters as an array. If your application logic is not designed to handle an array, it might crash or behave insecurely when it receives id=["123", "456"].

To mitigate this, you should sanitize input early in the middleware chain:

// Example of enforcing strict parameter handling
app.use((req, res, next) => {
  for (const key in req.query) {
    if (Array.isArray(req.query[key])) {
      // Reject requests containing duplicate parameters
      return res.status(400).send('Invalid parameter format');
    }
  }
  next();
});

By iterating through the req.query (or req.body using similar logic), you ensure that if any parameter has been "polluted" with multiple values, the application rejects the request immediately rather than attempting to guess the attacker's intent.

Another common strategy is to use middleware specifically designed for this purpose, like hpp, which automatically sanitizes or flattens parameters, preventing them from overriding your application's expected data structure. By explicitly controlling how the application perceives input, you eliminate the ambiguity that allows HPP attacks to succeed.

Key Takeaway: Never rely on default framework behavior for duplicate parameters; explicitly validate that incoming data conforms to a single-value structure to prevent bypasses and logic errors.