slashbin-receiver-kit 0.0.0-stage → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +3 -0
- package/dist/index.js +2 -0
- package/dist/receiver-signature-verifier.d.ts +26 -0
- package/dist/receiver-signature-verifier.js +52 -0
- package/dist/slashbin-signature-middleware.d.ts +8 -0
- package/dist/slashbin-signature-middleware.js +40 -0
- package/package.json +31 -4
- package/README.md +0 -3
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { ReceiverSignatureVerifier } from "./receiver-signature-verifier.js";
|
|
2
|
+
export type { IncomingHeaders, ReceiverSignatureVerifierOptions, VerifyResult, } from "./receiver-signature-verifier.js";
|
|
3
|
+
export { slashbinSignatureMiddleware } from "./slashbin-signature-middleware.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export type IncomingHeaders = Record<string, string | string[] | undefined>;
|
|
2
|
+
export interface ReceiverSignatureVerifierOptions {
|
|
3
|
+
/** 0..n shared secrets; empty strings are dropped. Two entries = rotation overlap. */
|
|
4
|
+
keys: readonly string[];
|
|
5
|
+
}
|
|
6
|
+
export type VerifyResult = {
|
|
7
|
+
ok: true;
|
|
8
|
+
keyIndex: number;
|
|
9
|
+
} | {
|
|
10
|
+
ok: false;
|
|
11
|
+
reason: "missing-key" | "missing-header" | "bad-signature";
|
|
12
|
+
};
|
|
13
|
+
export declare class ReceiverSignatureVerifier {
|
|
14
|
+
readonly keyCount: number;
|
|
15
|
+
private readonly keys;
|
|
16
|
+
constructor(opts: ReceiverSignatureVerifierOptions);
|
|
17
|
+
verify(input: {
|
|
18
|
+
source: string;
|
|
19
|
+
rawBody: Buffer;
|
|
20
|
+
headers: IncomingHeaders;
|
|
21
|
+
}): VerifyResult;
|
|
22
|
+
health(): {
|
|
23
|
+
status: "ok" | "degraded";
|
|
24
|
+
signingKeys: number;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { createHmac, timingSafeEqual } from "node:crypto";
|
|
2
|
+
// The wire contract this verifies is Slashbin's OUTBOUND signature:
|
|
3
|
+
// header x-slashbin-signature
|
|
4
|
+
// value "sha256=" + lowercase hex( HMAC-SHA256(key, raw body bytes) )
|
|
5
|
+
// Verified over the bytes the receiver received, never over a re-serialized body.
|
|
6
|
+
const SIGNATURE_HEADER = "x-slashbin-signature";
|
|
7
|
+
const SIGNATURE_PREFIX = "sha256=";
|
|
8
|
+
const HEX_DIGEST_LENGTH = 64;
|
|
9
|
+
const HEX_DIGEST = /^[0-9a-f]{64}$/;
|
|
10
|
+
export class ReceiverSignatureVerifier {
|
|
11
|
+
keyCount;
|
|
12
|
+
keys;
|
|
13
|
+
// Never throws: zero keys is a degraded state that verify() reports as missing-key.
|
|
14
|
+
constructor(opts) {
|
|
15
|
+
this.keys = opts.keys.filter((key) => key !== "").map((key) => Buffer.from(key, "utf8"));
|
|
16
|
+
this.keyCount = this.keys.length;
|
|
17
|
+
}
|
|
18
|
+
// `source` names the sender for the caller's rejection log; the check itself does not use it.
|
|
19
|
+
verify(input) {
|
|
20
|
+
if (this.keyCount === 0)
|
|
21
|
+
return { ok: false, reason: "missing-key" };
|
|
22
|
+
const header = firstHeaderValue(input.headers[SIGNATURE_HEADER]);
|
|
23
|
+
if (header === undefined || header === "")
|
|
24
|
+
return { ok: false, reason: "missing-header" };
|
|
25
|
+
const received = parseSignature(header);
|
|
26
|
+
if (received === null)
|
|
27
|
+
return { ok: false, reason: "bad-signature" };
|
|
28
|
+
// Every key is compared, with no early return, so timing does not reveal which key matched.
|
|
29
|
+
let matched = -1;
|
|
30
|
+
this.keys.forEach((key, index) => {
|
|
31
|
+
const expected = createHmac("sha256", key).update(input.rawBody).digest();
|
|
32
|
+
if (timingSafeEqual(expected, received) && matched === -1)
|
|
33
|
+
matched = index;
|
|
34
|
+
});
|
|
35
|
+
return matched === -1 ? { ok: false, reason: "bad-signature" } : { ok: true, keyIndex: matched };
|
|
36
|
+
}
|
|
37
|
+
health() {
|
|
38
|
+
return { status: this.keyCount === 0 ? "degraded" : "ok", signingKeys: this.keyCount };
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function firstHeaderValue(value) {
|
|
42
|
+
return Array.isArray(value) ? value[0] : value;
|
|
43
|
+
}
|
|
44
|
+
// Exact "sha256=" prefix and exact-length lowercase hex, so the compare below always has equal lengths.
|
|
45
|
+
function parseSignature(header) {
|
|
46
|
+
if (!header.startsWith(SIGNATURE_PREFIX))
|
|
47
|
+
return null;
|
|
48
|
+
const hex = header.slice(SIGNATURE_PREFIX.length);
|
|
49
|
+
if (hex.length !== HEX_DIGEST_LENGTH || !HEX_DIGEST.test(hex))
|
|
50
|
+
return null;
|
|
51
|
+
return Buffer.from(hex, "hex");
|
|
52
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { NextFunction, Request, Response } from "express";
|
|
2
|
+
import type { ReceiverSignatureVerifier } from "./receiver-signature-verifier.js";
|
|
3
|
+
export declare function slashbinSignatureMiddleware(verifier: ReceiverSignatureVerifier, opts: {
|
|
4
|
+
source: string;
|
|
5
|
+
logger: {
|
|
6
|
+
warn(obj: object, msg: string): void;
|
|
7
|
+
};
|
|
8
|
+
}): (req: Request, res: Response, next: NextFunction) => void;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// express.raw() must be mounted first so req.body is the Buffer Slashbin sent.
|
|
2
|
+
export function slashbinSignatureMiddleware(verifier, opts) {
|
|
3
|
+
return (req, res, next) => {
|
|
4
|
+
const result = verifier.verify({
|
|
5
|
+
source: opts.source,
|
|
6
|
+
rawBody: Buffer.isBuffer(req.body) ? req.body : Buffer.alloc(0),
|
|
7
|
+
headers: firstSignatureOccurrence(req),
|
|
8
|
+
});
|
|
9
|
+
if (result.ok)
|
|
10
|
+
return next();
|
|
11
|
+
// The reason goes to the log only; the caller always sees the same body.
|
|
12
|
+
opts.logger.warn({
|
|
13
|
+
event: "slashbin.signature.rejected",
|
|
14
|
+
source: opts.source,
|
|
15
|
+
reason: result.reason,
|
|
16
|
+
signingKeys: verifier.keyCount,
|
|
17
|
+
}, "slashbin.signature.rejected");
|
|
18
|
+
res.status(401).json({ error: "Invalid signature" });
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
const SIGNATURE_HEADER = "x-slashbin-signature";
|
|
22
|
+
// req.headers joins repeated fields with ", ", so "sig, junk" cannot be told apart from one
|
|
23
|
+
// malformed field "sig,junk". req.rawHeaders keeps each physical field, so the first
|
|
24
|
+
// occurrence is taken whole from there. Anything else about it (a comma suffix, stray
|
|
25
|
+
// whitespace) reaches the verifier unchanged and fails as bad-signature. Without rawHeaders
|
|
26
|
+
// (a hand-built request) the first element of the headers value is used, also unaltered.
|
|
27
|
+
// Later occurrences cannot change the decision. Validation stays with the verifier.
|
|
28
|
+
function firstSignatureOccurrence(req) {
|
|
29
|
+
const raw = req.rawHeaders;
|
|
30
|
+
if (Array.isArray(raw)) {
|
|
31
|
+
for (let i = 0; i + 1 < raw.length; i += 2) {
|
|
32
|
+
if (raw[i]?.toLowerCase() === SIGNATURE_HEADER)
|
|
33
|
+
return { [SIGNATURE_HEADER]: raw[i + 1] };
|
|
34
|
+
}
|
|
35
|
+
return {};
|
|
36
|
+
}
|
|
37
|
+
const name = Object.keys(req.headers).find((key) => key.toLowerCase() === SIGNATURE_HEADER);
|
|
38
|
+
const value = name === undefined ? undefined : req.headers[name];
|
|
39
|
+
return { [SIGNATURE_HEADER]: Array.isArray(value) ? value[0] : value };
|
|
40
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "slashbin-receiver-kit",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "Receiver-side kit for code behind a Slashbin destination: fail-closed, multi-key verification of Slashbin's delivery signature.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"scripts": {
|
|
22
|
+
"check": "tsc --noEmit",
|
|
23
|
+
"build": "tsc",
|
|
24
|
+
"test": "vitest run"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/express": "^5.0.0",
|
|
28
|
+
"@types/node": "^22.0.0",
|
|
29
|
+
"express": "^5.0.0",
|
|
30
|
+
"typescript": "^5.6.0",
|
|
31
|
+
"vitest": "^3.0.0"
|
|
32
|
+
}
|
|
33
|
+
}
|
package/README.md
DELETED