HTTP Public Key Pinning (HPKP) middleware

3.0.0 · deprecated · verified Wed Apr 22

The `hpkp` package provides HTTP Public Key Pinning (HPKP) middleware for Express and Connect applications. It facilitates adding the `Public-Key-Pins` or `Public-Key-Pins-Report-Only` headers to web responses, which allows sites to declare cryptographic identities for web servers. However, HPKP as a security standard has been widely deprecated by browser vendors, including Chrome, due to significant risks of misuse and the potential for self-inflicted denial-of-service by rendering a website permanently inaccessible to legitimate users. The package is currently at version 3.0.0 and is explicitly in maintenance mode, indicating it will not receive new feature development but will be maintained for critical bug fixes. Developers are strongly advised against implementing HPKP in new projects and should consider alternatives like Certificate Transparency and Expect-CT headers.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to integrate the `hpkp` middleware into an Express.js application, setting a Public Key Pinning header with example SHA256 hashes and optional configurations. It highlights the use of `maxAge`, `sha256s`, and conditional header setting via `setIf`.

const express = require("express");
const hpkp = require("hpkp");

const app = express();

const ninetyDaysInSeconds = 7776000; // 90 days
app.use(
  hpkp({
    maxAge: ninetyDaysInSeconds,
    sha256s: [
      // These should be your actual SPKI hashes for primary and backup keys
      "AbCdEf123=", 
      "ZyXwVu456=" 
    ],
    includeSubDomains: true, // optional
    reportUri: "https://example.com/hpkp-report", // optional
    reportOnly: false, // optional, set to true for testing

    // Set the header based on a condition. This is optional.
    setIf(req, res) {
      return req.secure; // Only set HPKP for HTTPS requests
    },
  }),
);

app.get('/', (req, res) => {
  res.send('HPKP header should be set if using HTTPS');
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
  console.log('Remember HPKP is deprecated and risky. Use with extreme caution.');
});

view raw JSON →