Skip to content

Commit c1eab3a

Browse files
committed
Restore project source code
Helmet v7 is going to drop support for the `Expect-CT` header. I still plan to keep the code maintained in the `expect-ct` package, and restoring this source code lets me do that. Conceptually, this is a revert of fcb65b1. However, it removes the build system and many other (development-only) dependencies.
1 parent 16ce5c0 commit c1eab3a

File tree

10 files changed

+1402
-2
lines changed

10 files changed

+1402
-2
lines changed

.eslintrc.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"env": {
3+
"commonjs": true,
4+
"es2021": true,
5+
"node": true
6+
},
7+
"extends": "eslint:recommended",
8+
"parserOptions": {
9+
"ecmaVersion": "latest"
10+
}
11+
}

.github/workflows/nodejs.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Node.js CI
2+
3+
on: [push]
4+
5+
permissions:
6+
contents: read
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
12+
strategy:
13+
matrix:
14+
node-version: [18.x]
15+
16+
steps:
17+
- uses: actions/checkout@v3
18+
with:
19+
persist-credentials: false
20+
- uses: actions/setup-node@v3
21+
with:
22+
node-version: ${{ matrix.node-version }}
23+
- run: npm ci
24+
- run: npm test
25+
env:
26+
CI: true

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/node_modules/

CHANGELOG.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Changelog
2+
3+
## 1.0.0 - 2020-08-02
4+
5+
### Changed
6+
7+
- If `maxAge` is `undefined`, it will be set to `0`
8+
- If `maxAge` is not an integer, it will be rounded down
9+
10+
### Removed
11+
12+
- Dropped support for old Node versions. Node 10+ is now required
13+
14+
## 0.3.0 - 2019-09-01
15+
16+
### Changed
17+
18+
- Dropped support for Node <8
19+
- You must now pass a positive integer for `maxAge` (instead of any positive number)
20+
- You cannot pass `undefined` for `maxAge` (though you can still omit the property)
21+
22+
## 0.2.0 - 2019-05-04
23+
24+
### Added
25+
26+
- TypeScript type definitions. See [helmetjs/helmet#188](https://github.com/helmetjs/helmet/issues/188)
27+
- Additional package metadata (bugs, homepage, etc)
28+
29+
### Changed
30+
31+
- Updated documentation
32+
33+
Changes in versions 0.1.1 and below can be found in [Helmet's changelog](https://github.com/helmetjs/helmet/blob/master/CHANGELOG.md).

README.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
1-
The source code for this module has moved.
1+
# Expect-CT middleware
22

3-
[See the Helmet repository](https://github.com/helmetjs/helmet) for the updated source.
3+
The `Expect-CT` HTTP header tells browsers to expect Certificate Transparency. For more, see [this blog post](https://scotthelme.co.uk/a-new-security-header-expect-ct/) and the [article on MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Expect-CT).
4+
5+
Usage:
6+
7+
```javascript
8+
const expectCt = require("expect-ct");
9+
10+
// Sets Expect-CT: max-age=123
11+
app.use(expectCt({ maxAge: 123 }));
12+
13+
// Sets Expect-CT: enforce, max-age=123
14+
app.use(
15+
expectCt({
16+
enforce: true,
17+
maxAge: 123,
18+
})
19+
);
20+
21+
// Sets Expect-CT: enforce, max-age=30, report-uri="https://example.com/report"
22+
app.use(
23+
expectCt({
24+
enforce: true,
25+
maxAge: 30,
26+
reportUri: "https://example.com/report",
27+
})
28+
);
29+
```

index.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { IncomingMessage, ServerResponse } from "http";
2+
3+
export interface ExpectCtOptions {
4+
maxAge?: number;
5+
enforce?: boolean;
6+
reportUri?: string;
7+
}
8+
9+
declare function expectCt(
10+
options?: Readonly<ExpectCtOptions>
11+
): (_req: IncomingMessage, res: ServerResponse, next: () => void) => void;
12+
13+
export default expectCt;

index.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
function parseMaxAge(value = 0) {
2+
if (value >= 0 && Number.isFinite(value)) {
3+
return Math.floor(value);
4+
} else {
5+
throw new Error(
6+
`Expect-CT: ${JSON.stringify(
7+
value
8+
)} is not a valid value for maxAge. Please choose a positive integer.`
9+
);
10+
}
11+
}
12+
13+
function getHeaderValueFromOptions(options) {
14+
const directives = [`max-age=${parseMaxAge(options.maxAge)}`];
15+
16+
if (options.enforce) {
17+
directives.push("enforce");
18+
}
19+
20+
if (options.reportUri) {
21+
directives.push(`report-uri="${options.reportUri}"`);
22+
}
23+
24+
return directives.join(", ");
25+
}
26+
27+
function expectCt(options = {}) {
28+
const headerValue = getHeaderValueFromOptions(options);
29+
30+
return function expectCtMiddleware(_req, res, next) {
31+
res.setHeader("Expect-CT", headerValue);
32+
next();
33+
};
34+
}
35+
36+
module.exports = expectCt;

0 commit comments

Comments
 (0)