@openstax/ts-utils 1.39.0 → 1.39.1
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.
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import { createHash } from 'crypto';
|
|
3
3
|
import { createVerifier, httpbis } from 'http-message-signatures';
|
|
4
4
|
import { SigningKeyNotFoundError } from 'jwks-rsa';
|
|
5
|
+
import { ByteSequence, parseDictionary, parseItem, Token } from 'structured-headers';
|
|
5
6
|
import { assertString } from '../../assertions';
|
|
6
7
|
import { resolveConfigValue } from '../../config';
|
|
7
8
|
import { InvalidRequestError } from '../../errors';
|
|
@@ -28,9 +29,25 @@ export const createHttpMessageVerifier = ({ configSpace, fetcher }) => (configPr
|
|
|
28
29
|
// but we do not bother matching the Signature-Agent names with the Signature names
|
|
29
30
|
// as that would be awkward with the packages we use
|
|
30
31
|
const signatureAgentString = (_a = headers['signature-agent']) !== null && _a !== void 0 ? _a : '';
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
let rawValues;
|
|
33
|
+
// Try parsing as RFC 8941 Item first (e.g., "https://url" or bare token)
|
|
34
|
+
try {
|
|
35
|
+
rawValues = [parseItem(signatureAgentString)];
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
// If item parsing fails, try parsing as a Dictionary (e.g., sig="https://url")
|
|
39
|
+
rawValues = Array.from(parseDictionary(signatureAgentString).values());
|
|
40
|
+
}
|
|
41
|
+
// Convert values to strings (handle Token, string, and reject others) and deduplicate
|
|
42
|
+
const signatureAgents = [...new Set(rawValues.map(([bareItem]) => {
|
|
43
|
+
if (bareItem instanceof Token) {
|
|
44
|
+
return bareItem.toString();
|
|
45
|
+
}
|
|
46
|
+
return assertString(bareItem, new InvalidRequestError('Signature-Agent values must be strings or tokens'));
|
|
47
|
+
}))];
|
|
48
|
+
if (signatureAgents.length === 0) {
|
|
49
|
+
throw new InvalidRequestError('Signature-Agent header is required');
|
|
50
|
+
}
|
|
34
51
|
const keys = (await Promise.all(signatureAgents.map(async (signatureAgent) => {
|
|
35
52
|
if (!await signatureAgentVerifier(signatureAgent)) {
|
|
36
53
|
throw new InvalidRequestError('Signature-Agent verification failed');
|
|
@@ -75,14 +92,37 @@ export const createHttpMessageVerifier = ({ configSpace, fetcher }) => (configPr
|
|
|
75
92
|
// For example, if a GET request uses configOverride to make content-digest not required
|
|
76
93
|
if (!headers['content-digest'])
|
|
77
94
|
return true;
|
|
78
|
-
|
|
79
|
-
|
|
95
|
+
let contentDigest;
|
|
96
|
+
try {
|
|
97
|
+
contentDigest = parseDictionary(headers['content-digest']);
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
80
100
|
throw new InvalidRequestError('Unsupported Content-Digest header format');
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
101
|
+
}
|
|
102
|
+
if (contentDigest.size === 0) {
|
|
103
|
+
throw new InvalidRequestError('Content-Digest header is required');
|
|
104
|
+
}
|
|
105
|
+
// Map Content-Digest algorithm names to Node's crypto algorithm names
|
|
106
|
+
const algorithmMap = {
|
|
107
|
+
'sha-256': 'sha256',
|
|
108
|
+
'sha-512': 'sha512',
|
|
109
|
+
};
|
|
110
|
+
for (const [algorithm, value] of contentDigest.entries()) {
|
|
111
|
+
const [bareItem] = value;
|
|
112
|
+
// Convert ByteSequence to string, or assert it's already a string
|
|
113
|
+
const hashValue = bareItem instanceof ByteSequence
|
|
114
|
+
? bareItem.toBase64()
|
|
115
|
+
: assertString(bareItem, new InvalidRequestError('Content-Digest values must be strings'));
|
|
116
|
+
const cryptoAlgorithm = algorithmMap[algorithm];
|
|
117
|
+
if (!cryptoAlgorithm) {
|
|
118
|
+
throw new InvalidRequestError(`Unsupported Content-Digest algorithm: ${algorithm}`);
|
|
119
|
+
}
|
|
120
|
+
// Calculate the hash
|
|
121
|
+
const calculatedHash = createHash(cryptoAlgorithm).update(assertString(body, new InvalidRequestError('Request body is required when Content-Digest is present'))).digest('base64');
|
|
122
|
+
// Compare with the provided hash
|
|
123
|
+
if (calculatedHash !== hashValue) {
|
|
124
|
+
throw new InvalidRequestError(`Calculated Content-Digest value did not match header for algorithm ${algorithm}`);
|
|
125
|
+
}
|
|
86
126
|
}
|
|
87
127
|
return true;
|
|
88
128
|
},
|