Expect-CT Header Middleware
The `expect-ct` package provides Express middleware for setting the deprecated Expect-CT HTTP response header. This header was designed to enforce Certificate Transparency (CT) requirements by instructing browsers to expect valid Signed Certificate Timestamps (SCTs) for a website's TLS certificates. However, the Expect-CT header itself has been deprecated by major browsers (e.g., Chrome removed support in version 107 in October 2022) as Certificate Transparency is now a baseline requirement enforced by default across all publicly trusted certificates. As such, this middleware, currently at version 1.0.0 (though `1.0.1` is on npm, published 3 years ago), offers minimal practical security benefit for modern web applications. The Helmet.js project, which originally included this functionality, removed `expect-ct` from its default middlewares in Helmet v5 due to its obsolescence.
Common errors
-
TypeError: expectCt is not a function
cause Incorrect import: Attempting to destructure a default export, or mixing CommonJS `require` syntax with ESM `import` for a default export.fixFor CommonJS: `const expectCt = require('expect-ct');`. For ESM: `import expectCt from 'expect-ct';` (without curly braces). -
ERR_CERTIFICATE_TRANSPARENCY_REQUIRED
cause While Expect-CT is deprecated, if it were still enforced by a browser (e.g., an older Chrome version), this error indicates that your site's SSL/TLS certificate is not satisfying Certificate Transparency requirements.fixContact your Certificate Authority (CA) to ensure your certificates include Signed Certificate Timestamps (SCTs) and are properly logged in CT logs. This is a fundamental requirement for modern certificates, irrespective of the Expect-CT header.
Warnings
- deprecated The Expect-CT HTTP header itself is deprecated and largely obsolete. Most major browsers (like Chrome since version 107 in October 2022) have removed support or no longer process this header, as Certificate Transparency is now a default, built-in security measure.
- breaking The `expect-ct` middleware is no longer included by default in Helmet.js version 5 and later. If you upgraded Helmet and rely on Expect-CT, you will need to install and configure this standalone package explicitly, although it is not recommended due to header deprecation.
- gotcha The Expect-CT header only functions over HTTPS connections. Browsers will ignore the header if sent over plain HTTP.
- gotcha Only Chromium-based browsers (e.g., Google Chrome, Microsoft Edge) ever implemented support for the Expect-CT header. Other browsers like Firefox and Safari never adopted it.
Install
-
npm install expect-ct -
yarn add expect-ct -
pnpm add expect-ct
Imports
- expectCt
import { expectCt } from 'expect-ct';import expectCt from 'expect-ct';
- expectCt
const expectCt = require('expect-ct');
Quickstart
import express from 'express';
import expectCt from 'expect-ct';
const app = express();
const port = process.env.PORT ?? 3000;
// Sets Expect-CT: max-age=123
app.use(expectCt({ maxAge: 123 }));
// Optionally, enforce and report
app.use(
expectCt({
enforce: true,
maxAge: 30,
reportUri: 'https://example.com/report' // Replace with your actual reporting endpoint
})
);
app.get('/', (req, res) => {
res.send('Hello, Expect-CT!');
});
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
console.warn('The Expect-CT header is largely deprecated and may not provide significant security benefits in modern browsers.');
});