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