@geekmidas/events 10.0.0-alpha.155 → 10.0.0-alpha.157
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/README.md +19 -0
- package/dist/sns/index.cjs +15 -191
- package/dist/sns/index.cjs.map +1 -1
- package/dist/sns/index.d.cts +5 -115
- package/dist/sns/index.d.cts.map +1 -1
- package/dist/sns/index.d.mts +5 -115
- package/dist/sns/index.d.mts.map +1 -1
- package/dist/sns/index.mjs +1 -177
- package/dist/sns/index.mjs.map +1 -1
- package/dist/sns/message.cjs +194 -0
- package/dist/sns/message.cjs.map +1 -0
- package/dist/sns/message.d.cts +127 -0
- package/dist/sns/message.d.cts.map +1 -0
- package/dist/sns/message.d.mts +127 -0
- package/dist/sns/message.d.mts.map +1 -0
- package/dist/sns/message.mjs +182 -0
- package/dist/sns/message.mjs.map +1 -0
- package/package.json +13 -3
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let node_crypto = require("node:crypto");
|
|
3
|
+
//#region src/sns/message.ts
|
|
4
|
+
/** The fields signed, in the order SNS signs them, per message type. */
|
|
5
|
+
const SIGNED_FIELDS = {
|
|
6
|
+
Notification: [
|
|
7
|
+
"Message",
|
|
8
|
+
"MessageId",
|
|
9
|
+
"Subject",
|
|
10
|
+
"Timestamp",
|
|
11
|
+
"TopicArn",
|
|
12
|
+
"Type"
|
|
13
|
+
],
|
|
14
|
+
SubscriptionConfirmation: [
|
|
15
|
+
"Message",
|
|
16
|
+
"MessageId",
|
|
17
|
+
"SubscribeURL",
|
|
18
|
+
"Timestamp",
|
|
19
|
+
"Token",
|
|
20
|
+
"TopicArn",
|
|
21
|
+
"Type"
|
|
22
|
+
],
|
|
23
|
+
UnsubscribeConfirmation: [
|
|
24
|
+
"Message",
|
|
25
|
+
"MessageId",
|
|
26
|
+
"SubscribeURL",
|
|
27
|
+
"Timestamp",
|
|
28
|
+
"Token",
|
|
29
|
+
"TopicArn",
|
|
30
|
+
"Type"
|
|
31
|
+
]
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Where SNS serves its signing certificates. Anywhere else is a certificate
|
|
35
|
+
* the sender chose, which would let the sender sign its own forgeries.
|
|
36
|
+
*/
|
|
37
|
+
const SNS_CERTIFICATE_HOST = /^sns\.[a-z0-9-]+\.amazonaws\.com(\.cn)?$/;
|
|
38
|
+
const certificates = /* @__PURE__ */ new Map();
|
|
39
|
+
async function defaultFetchCertificate(url) {
|
|
40
|
+
const response = await fetch(url);
|
|
41
|
+
if (!response.ok) throw new SnsCertificateUnavailable(url, response.status);
|
|
42
|
+
return response.text();
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* A URL, or undefined for a value that is not one at all — what an emulator
|
|
46
|
+
* sends — so it is untrusted rather than a crash. (`URL.parse` is Node 22.1+.)
|
|
47
|
+
*/
|
|
48
|
+
function parseUrl(value) {
|
|
49
|
+
try {
|
|
50
|
+
return new URL(String(value));
|
|
51
|
+
} catch {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/** The string SNS signed for this message. */
|
|
56
|
+
function snsStringToSign(message) {
|
|
57
|
+
const fields = SIGNED_FIELDS[message.Type];
|
|
58
|
+
if (!fields) throw new SnsMessageTypeUnknown(String(message.Type));
|
|
59
|
+
return fields.filter((field) => message[field] !== void 0).map((field) => `${field}\n${message[field]}\n`).join("");
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Check that SNS sent this message: the certificate is SNS's own, and the
|
|
63
|
+
* signature over the message's signed fields verifies against it.
|
|
64
|
+
*
|
|
65
|
+
* Throws rather than returning false, so a caller cannot forget to look.
|
|
66
|
+
*/
|
|
67
|
+
async function verifySnsMessage(message, options = {}) {
|
|
68
|
+
const url = parseUrl(message.SigningCertURL);
|
|
69
|
+
if (url?.protocol !== "https:" || !SNS_CERTIFICATE_HOST.test(url.hostname)) throw new SnsCertificateUntrusted(message.SigningCertURL);
|
|
70
|
+
const algorithm = message.SignatureVersion === "1" ? "RSA-SHA1" : message.SignatureVersion === "2" ? "RSA-SHA256" : void 0;
|
|
71
|
+
if (!algorithm) throw new SnsSignatureVersionUnknown(message.SignatureVersion);
|
|
72
|
+
const fetchCertificate = options.fetchCertificate ?? defaultFetchCertificate;
|
|
73
|
+
let certificate = certificates.get(message.SigningCertURL);
|
|
74
|
+
if (!certificate || options.fetchCertificate) {
|
|
75
|
+
certificate = fetchCertificate(message.SigningCertURL);
|
|
76
|
+
if (!options.fetchCertificate) {
|
|
77
|
+
certificates.set(message.SigningCertURL, certificate);
|
|
78
|
+
certificate.catch(() => certificates.delete(message.SigningCertURL));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const verifier = (0, node_crypto.createVerify)(algorithm);
|
|
82
|
+
verifier.update(snsStringToSign(message), "utf8");
|
|
83
|
+
if (!verifier.verify(await certificate, message.Signature, "base64")) throw new SnsSignatureInvalid(message.MessageId, message.TopicArn);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Confirm a subscription by visiting the URL SNS sent. Until this happens SNS
|
|
87
|
+
* delivers nothing to the endpoint.
|
|
88
|
+
*/
|
|
89
|
+
async function confirmSnsSubscription(message, request = fetch) {
|
|
90
|
+
if (message.Type !== "SubscriptionConfirmation" || !message.SubscribeURL) throw new SnsNotAConfirmation(message.Type);
|
|
91
|
+
const response = await request(message.SubscribeURL);
|
|
92
|
+
if (!response.ok) throw new SnsConfirmationFailed(message.TopicArn, response.status);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* A pushed notification as the Lambda event SNS would have invoked a function
|
|
96
|
+
* with, so it can be handed to the same adaptor.
|
|
97
|
+
*/
|
|
98
|
+
function toSnsEvent(message, subscriptionArn = "") {
|
|
99
|
+
return { Records: [{
|
|
100
|
+
EventSource: "aws:sns",
|
|
101
|
+
EventVersion: "1.0",
|
|
102
|
+
EventSubscriptionArn: subscriptionArn,
|
|
103
|
+
Sns: {
|
|
104
|
+
Type: message.Type,
|
|
105
|
+
MessageId: message.MessageId,
|
|
106
|
+
TopicArn: message.TopicArn,
|
|
107
|
+
Subject: message.Subject ?? null,
|
|
108
|
+
Message: message.Message,
|
|
109
|
+
Timestamp: message.Timestamp,
|
|
110
|
+
SignatureVersion: message.SignatureVersion,
|
|
111
|
+
Signature: message.Signature,
|
|
112
|
+
SigningCertUrl: message.SigningCertURL,
|
|
113
|
+
UnsubscribeUrl: message.UnsubscribeURL ?? "",
|
|
114
|
+
MessageAttributes: message.MessageAttributes ?? {}
|
|
115
|
+
}
|
|
116
|
+
}] };
|
|
117
|
+
}
|
|
118
|
+
var SnsCertificateUntrusted = class extends Error {
|
|
119
|
+
url;
|
|
120
|
+
constructor(url) {
|
|
121
|
+
super(`The SNS message names a signing certificate at '${url}', which is not an https URL on sns.<region>.amazonaws.com. Only SNS can send to this endpoint; reject the request.`);
|
|
122
|
+
this.url = url;
|
|
123
|
+
this.name = "SnsCertificateUntrusted";
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
var SnsCertificateUnavailable = class extends Error {
|
|
127
|
+
url;
|
|
128
|
+
status;
|
|
129
|
+
constructor(url, status) {
|
|
130
|
+
super(`Fetching the SNS signing certificate at '${url}' answered ${status}. The message cannot be verified, so it is not handled; SNS will retry.`);
|
|
131
|
+
this.url = url;
|
|
132
|
+
this.status = status;
|
|
133
|
+
this.name = "SnsCertificateUnavailable";
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
var SnsSignatureVersionUnknown = class extends Error {
|
|
137
|
+
version;
|
|
138
|
+
constructor(version) {
|
|
139
|
+
super(`SNS signature version '${version}' is not one this endpoint verifies (it knows '1' and '2'). Reject the request.`);
|
|
140
|
+
this.version = version;
|
|
141
|
+
this.name = "SnsSignatureVersionUnknown";
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
var SnsSignatureInvalid = class extends Error {
|
|
145
|
+
messageId;
|
|
146
|
+
topicArn;
|
|
147
|
+
constructor(messageId, topicArn) {
|
|
148
|
+
super(`SNS message '${messageId}' from '${topicArn}' does not verify against its signing certificate. It was not sent by SNS, or was altered on the way; reject it.`);
|
|
149
|
+
this.messageId = messageId;
|
|
150
|
+
this.topicArn = topicArn;
|
|
151
|
+
this.name = "SnsSignatureInvalid";
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
var SnsMessageTypeUnknown = class extends Error {
|
|
155
|
+
type;
|
|
156
|
+
constructor(type) {
|
|
157
|
+
super(`'${type}' is not an SNS message type (Notification, SubscriptionConfirmation, UnsubscribeConfirmation). Reject it.`);
|
|
158
|
+
this.type = type;
|
|
159
|
+
this.name = "SnsMessageTypeUnknown";
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
var SnsNotAConfirmation = class extends Error {
|
|
163
|
+
type;
|
|
164
|
+
constructor(type) {
|
|
165
|
+
super(`Only a SubscriptionConfirmation carries a URL to confirm; this is a '${type}'.`);
|
|
166
|
+
this.type = type;
|
|
167
|
+
this.name = "SnsNotAConfirmation";
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
var SnsConfirmationFailed = class extends Error {
|
|
171
|
+
topicArn;
|
|
172
|
+
status;
|
|
173
|
+
constructor(topicArn, status) {
|
|
174
|
+
super(`Confirming the subscription to '${topicArn}' answered ${status}. SNS will deliver nothing until it is confirmed; it resends the confirmation, or subscribe the endpoint again.`);
|
|
175
|
+
this.topicArn = topicArn;
|
|
176
|
+
this.status = status;
|
|
177
|
+
this.name = "SnsConfirmationFailed";
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
//#endregion
|
|
181
|
+
exports.SnsCertificateUnavailable = SnsCertificateUnavailable;
|
|
182
|
+
exports.SnsCertificateUntrusted = SnsCertificateUntrusted;
|
|
183
|
+
exports.SnsConfirmationFailed = SnsConfirmationFailed;
|
|
184
|
+
exports.SnsMessageTypeUnknown = SnsMessageTypeUnknown;
|
|
185
|
+
exports.SnsNotAConfirmation = SnsNotAConfirmation;
|
|
186
|
+
exports.SnsSignatureInvalid = SnsSignatureInvalid;
|
|
187
|
+
exports.SnsSignatureVersionUnknown = SnsSignatureVersionUnknown;
|
|
188
|
+
exports.confirmSnsSubscription = confirmSnsSubscription;
|
|
189
|
+
exports.parseUrl = parseUrl;
|
|
190
|
+
exports.snsStringToSign = snsStringToSign;
|
|
191
|
+
exports.toSnsEvent = toSnsEvent;
|
|
192
|
+
exports.verifySnsMessage = verifySnsMessage;
|
|
193
|
+
|
|
194
|
+
//# sourceMappingURL=message.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message.cjs","names":["createVerify"],"sources":["../../src/sns/message.ts"],"sourcesContent":["import { createVerify } from 'node:crypto';\n\n/**\n * SNS delivering to an HTTP(S) endpoint, rather than to a queue that is polled.\n *\n * SNS POSTs each message to the subscribed URL as JSON. That is the whole\n * transport: no queue in between, no poller, and the endpoint is handed the\n * same envelope a Lambda subscription receives inside `Records[].Sns` — so the\n * handler that runs deployed can be the one that runs here.\n *\n * The endpoint is public, so a message is trusted only once its signature\n * checks out against a certificate SNS itself serves. Anything else that POSTs\n * there is somebody's guess at the shape.\n *\n * Nothing here calls the SNS API, so nothing here needs the AWS SDK: a server\n * that only receives pushes loads this as `@geekmidas/events/sns/message`.\n */\n\n/** A message SNS POSTs to a subscribed HTTP endpoint. */\nexport interface SnsHttpMessage {\n\tType: 'Notification' | 'SubscriptionConfirmation' | 'UnsubscribeConfirmation';\n\tMessageId: string;\n\tTopicArn: string;\n\tMessage: string;\n\tTimestamp: string;\n\tSignatureVersion: string;\n\tSignature: string;\n\tSigningCertURL: string;\n\tSubject?: string;\n\t/** Confirmation messages only. */\n\tToken?: string;\n\t/** Confirmation messages only: GET it to confirm. */\n\tSubscribeURL?: string;\n\t/** Notifications only. */\n\tUnsubscribeURL?: string;\n\tMessageAttributes?: Record<string, { Type: string; Value: string }>;\n}\n\n/** The fields signed, in the order SNS signs them, per message type. */\nconst SIGNED_FIELDS: Record<SnsHttpMessage['Type'], (keyof SnsHttpMessage)[]> =\n\t{\n\t\tNotification: [\n\t\t\t'Message',\n\t\t\t'MessageId',\n\t\t\t'Subject',\n\t\t\t'Timestamp',\n\t\t\t'TopicArn',\n\t\t\t'Type',\n\t\t],\n\t\tSubscriptionConfirmation: [\n\t\t\t'Message',\n\t\t\t'MessageId',\n\t\t\t'SubscribeURL',\n\t\t\t'Timestamp',\n\t\t\t'Token',\n\t\t\t'TopicArn',\n\t\t\t'Type',\n\t\t],\n\t\tUnsubscribeConfirmation: [\n\t\t\t'Message',\n\t\t\t'MessageId',\n\t\t\t'SubscribeURL',\n\t\t\t'Timestamp',\n\t\t\t'Token',\n\t\t\t'TopicArn',\n\t\t\t'Type',\n\t\t],\n\t};\n\n/**\n * Where SNS serves its signing certificates. Anywhere else is a certificate\n * the sender chose, which would let the sender sign its own forgeries.\n */\nconst SNS_CERTIFICATE_HOST = /^sns\\.[a-z0-9-]+\\.amazonaws\\.com(\\.cn)?$/;\n\nexport interface VerifySnsMessageOptions {\n\t/**\n\t * Fetch the PEM at a certificate URL. `fetch` by default; replaceable so a\n\t * test signs with its own key instead of SNS's.\n\t */\n\tfetchCertificate?: (url: string) => Promise<string>;\n}\n\nconst certificates = new Map<string, Promise<string>>();\n\nasync function defaultFetchCertificate(url: string): Promise<string> {\n\tconst response = await fetch(url);\n\tif (!response.ok) throw new SnsCertificateUnavailable(url, response.status);\n\treturn response.text();\n}\n\n/**\n * A URL, or undefined for a value that is not one at all — what an emulator\n * sends — so it is untrusted rather than a crash. (`URL.parse` is Node 22.1+.)\n */\nexport function parseUrl(value: unknown): URL | undefined {\n\ttry {\n\t\treturn new URL(String(value));\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\n/** The string SNS signed for this message. */\nexport function snsStringToSign(message: SnsHttpMessage): string {\n\tconst fields = SIGNED_FIELDS[message.Type];\n\tif (!fields) throw new SnsMessageTypeUnknown(String(message.Type));\n\n\treturn fields\n\t\t.filter((field) => message[field] !== undefined)\n\t\t.map((field) => `${field}\\n${message[field]}\\n`)\n\t\t.join('');\n}\n\n/**\n * Check that SNS sent this message: the certificate is SNS's own, and the\n * signature over the message's signed fields verifies against it.\n *\n * Throws rather than returning false, so a caller cannot forget to look.\n */\nexport async function verifySnsMessage(\n\tmessage: SnsHttpMessage,\n\toptions: VerifySnsMessageOptions = {},\n): Promise<void> {\n\tconst url = parseUrl(message.SigningCertURL);\n\tif (url?.protocol !== 'https:' || !SNS_CERTIFICATE_HOST.test(url.hostname)) {\n\t\tthrow new SnsCertificateUntrusted(message.SigningCertURL);\n\t}\n\n\tconst algorithm =\n\t\tmessage.SignatureVersion === '1'\n\t\t\t? 'RSA-SHA1'\n\t\t\t: message.SignatureVersion === '2'\n\t\t\t\t? 'RSA-SHA256'\n\t\t\t\t: undefined;\n\tif (!algorithm) {\n\t\tthrow new SnsSignatureVersionUnknown(message.SignatureVersion);\n\t}\n\n\tconst fetchCertificate = options.fetchCertificate ?? defaultFetchCertificate;\n\tlet certificate = certificates.get(message.SigningCertURL);\n\tif (!certificate || options.fetchCertificate) {\n\t\tcertificate = fetchCertificate(message.SigningCertURL);\n\t\t// Only SNS's real fetch is cached: a test's key belongs to that test.\n\t\tif (!options.fetchCertificate) {\n\t\t\tcertificates.set(message.SigningCertURL, certificate);\n\t\t\tcertificate.catch(() => certificates.delete(message.SigningCertURL));\n\t\t}\n\t}\n\n\tconst verifier = createVerify(algorithm);\n\tverifier.update(snsStringToSign(message), 'utf8');\n\tif (!verifier.verify(await certificate, message.Signature, 'base64')) {\n\t\tthrow new SnsSignatureInvalid(message.MessageId, message.TopicArn);\n\t}\n}\n\n/**\n * Confirm a subscription by visiting the URL SNS sent. Until this happens SNS\n * delivers nothing to the endpoint.\n */\nexport async function confirmSnsSubscription(\n\tmessage: SnsHttpMessage,\n\trequest: (url: string) => Promise<{ ok: boolean; status: number }> = fetch,\n): Promise<void> {\n\tif (message.Type !== 'SubscriptionConfirmation' || !message.SubscribeURL) {\n\t\tthrow new SnsNotAConfirmation(message.Type);\n\t}\n\n\tconst response = await request(message.SubscribeURL);\n\tif (!response.ok) {\n\t\tthrow new SnsConfirmationFailed(message.TopicArn, response.status);\n\t}\n}\n\n/**\n * A pushed notification as the Lambda event SNS would have invoked a function\n * with, so it can be handed to the same adaptor.\n */\nexport function toSnsEvent(message: SnsHttpMessage, subscriptionArn = '') {\n\treturn {\n\t\tRecords: [\n\t\t\t{\n\t\t\t\tEventSource: 'aws:sns',\n\t\t\t\tEventVersion: '1.0',\n\t\t\t\tEventSubscriptionArn: subscriptionArn,\n\t\t\t\tSns: {\n\t\t\t\t\tType: message.Type,\n\t\t\t\t\tMessageId: message.MessageId,\n\t\t\t\t\tTopicArn: message.TopicArn,\n\t\t\t\t\tSubject: message.Subject ?? null,\n\t\t\t\t\tMessage: message.Message,\n\t\t\t\t\tTimestamp: message.Timestamp,\n\t\t\t\t\tSignatureVersion: message.SignatureVersion,\n\t\t\t\t\tSignature: message.Signature,\n\t\t\t\t\t// Lambda spells these two differently from the HTTP envelope.\n\t\t\t\t\tSigningCertUrl: message.SigningCertURL,\n\t\t\t\t\tUnsubscribeUrl: message.UnsubscribeURL ?? '',\n\t\t\t\t\tMessageAttributes: message.MessageAttributes ?? {},\n\t\t\t\t},\n\t\t\t},\n\t\t],\n\t};\n}\n\nexport class SnsCertificateUntrusted extends Error {\n\tconstructor(readonly url: string) {\n\t\tsuper(\n\t\t\t`The SNS message names a signing certificate at '${url}', which is not ` +\n\t\t\t\t'an https URL on sns.<region>.amazonaws.com. Only SNS can send to ' +\n\t\t\t\t'this endpoint; reject the request.',\n\t\t);\n\t\tthis.name = 'SnsCertificateUntrusted';\n\t}\n}\n\nexport class SnsCertificateUnavailable extends Error {\n\tconstructor(\n\t\treadonly url: string,\n\t\treadonly status: number,\n\t) {\n\t\tsuper(\n\t\t\t`Fetching the SNS signing certificate at '${url}' answered ${status}. ` +\n\t\t\t\t'The message cannot be verified, so it is not handled; SNS will retry.',\n\t\t);\n\t\tthis.name = 'SnsCertificateUnavailable';\n\t}\n}\n\nexport class SnsSignatureVersionUnknown extends Error {\n\tconstructor(readonly version: string) {\n\t\tsuper(\n\t\t\t`SNS signature version '${version}' is not one this endpoint verifies ` +\n\t\t\t\t\"(it knows '1' and '2'). Reject the request.\",\n\t\t);\n\t\tthis.name = 'SnsSignatureVersionUnknown';\n\t}\n}\n\nexport class SnsSignatureInvalid extends Error {\n\tconstructor(\n\t\treadonly messageId: string,\n\t\treadonly topicArn: string,\n\t) {\n\t\tsuper(\n\t\t\t`SNS message '${messageId}' from '${topicArn}' does not verify against ` +\n\t\t\t\t'its signing certificate. It was not sent by SNS, or was altered ' +\n\t\t\t\t'on the way; reject it.',\n\t\t);\n\t\tthis.name = 'SnsSignatureInvalid';\n\t}\n}\n\nexport class SnsMessageTypeUnknown extends Error {\n\tconstructor(readonly type: string) {\n\t\tsuper(\n\t\t\t`'${type}' is not an SNS message type (Notification, ` +\n\t\t\t\t'SubscriptionConfirmation, UnsubscribeConfirmation). Reject it.',\n\t\t);\n\t\tthis.name = 'SnsMessageTypeUnknown';\n\t}\n}\n\nexport class SnsNotAConfirmation extends Error {\n\tconstructor(readonly type: string) {\n\t\tsuper(\n\t\t\t`Only a SubscriptionConfirmation carries a URL to confirm; this is a ` +\n\t\t\t\t`'${type}'.`,\n\t\t);\n\t\tthis.name = 'SnsNotAConfirmation';\n\t}\n}\n\nexport class SnsConfirmationFailed extends Error {\n\tconstructor(\n\t\treadonly topicArn: string,\n\t\treadonly status: number,\n\t) {\n\t\tsuper(\n\t\t\t`Confirming the subscription to '${topicArn}' answered ${status}. ` +\n\t\t\t\t'SNS will deliver nothing until it is confirmed; it resends the ' +\n\t\t\t\t'confirmation, or subscribe the endpoint again.',\n\t\t);\n\t\tthis.name = 'SnsConfirmationFailed';\n\t}\n}\n"],"mappings":"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"}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
//#region src/sns/message.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* SNS delivering to an HTTP(S) endpoint, rather than to a queue that is polled.
|
|
4
|
+
*
|
|
5
|
+
* SNS POSTs each message to the subscribed URL as JSON. That is the whole
|
|
6
|
+
* transport: no queue in between, no poller, and the endpoint is handed the
|
|
7
|
+
* same envelope a Lambda subscription receives inside `Records[].Sns` — so the
|
|
8
|
+
* handler that runs deployed can be the one that runs here.
|
|
9
|
+
*
|
|
10
|
+
* The endpoint is public, so a message is trusted only once its signature
|
|
11
|
+
* checks out against a certificate SNS itself serves. Anything else that POSTs
|
|
12
|
+
* there is somebody's guess at the shape.
|
|
13
|
+
*
|
|
14
|
+
* Nothing here calls the SNS API, so nothing here needs the AWS SDK: a server
|
|
15
|
+
* that only receives pushes loads this as `@geekmidas/events/sns/message`.
|
|
16
|
+
*/
|
|
17
|
+
/** A message SNS POSTs to a subscribed HTTP endpoint. */
|
|
18
|
+
export interface SnsHttpMessage {
|
|
19
|
+
Type: 'Notification' | 'SubscriptionConfirmation' | 'UnsubscribeConfirmation';
|
|
20
|
+
MessageId: string;
|
|
21
|
+
TopicArn: string;
|
|
22
|
+
Message: string;
|
|
23
|
+
Timestamp: string;
|
|
24
|
+
SignatureVersion: string;
|
|
25
|
+
Signature: string;
|
|
26
|
+
SigningCertURL: string;
|
|
27
|
+
Subject?: string;
|
|
28
|
+
/** Confirmation messages only. */
|
|
29
|
+
Token?: string;
|
|
30
|
+
/** Confirmation messages only: GET it to confirm. */
|
|
31
|
+
SubscribeURL?: string;
|
|
32
|
+
/** Notifications only. */
|
|
33
|
+
UnsubscribeURL?: string;
|
|
34
|
+
MessageAttributes?: Record<string, {
|
|
35
|
+
Type: string;
|
|
36
|
+
Value: string;
|
|
37
|
+
}>;
|
|
38
|
+
}
|
|
39
|
+
export interface VerifySnsMessageOptions {
|
|
40
|
+
/**
|
|
41
|
+
* Fetch the PEM at a certificate URL. `fetch` by default; replaceable so a
|
|
42
|
+
* test signs with its own key instead of SNS's.
|
|
43
|
+
*/
|
|
44
|
+
fetchCertificate?: (url: string) => Promise<string>;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* A URL, or undefined for a value that is not one at all — what an emulator
|
|
48
|
+
* sends — so it is untrusted rather than a crash. (`URL.parse` is Node 22.1+.)
|
|
49
|
+
*/
|
|
50
|
+
export declare function parseUrl(value: unknown): URL | undefined;
|
|
51
|
+
/** The string SNS signed for this message. */
|
|
52
|
+
export declare function snsStringToSign(message: SnsHttpMessage): string;
|
|
53
|
+
/**
|
|
54
|
+
* Check that SNS sent this message: the certificate is SNS's own, and the
|
|
55
|
+
* signature over the message's signed fields verifies against it.
|
|
56
|
+
*
|
|
57
|
+
* Throws rather than returning false, so a caller cannot forget to look.
|
|
58
|
+
*/
|
|
59
|
+
export declare function verifySnsMessage(message: SnsHttpMessage, options?: VerifySnsMessageOptions): Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Confirm a subscription by visiting the URL SNS sent. Until this happens SNS
|
|
62
|
+
* delivers nothing to the endpoint.
|
|
63
|
+
*/
|
|
64
|
+
export declare function confirmSnsSubscription(message: SnsHttpMessage, request?: (url: string) => Promise<{
|
|
65
|
+
ok: boolean;
|
|
66
|
+
status: number;
|
|
67
|
+
}>): Promise<void>;
|
|
68
|
+
/**
|
|
69
|
+
* A pushed notification as the Lambda event SNS would have invoked a function
|
|
70
|
+
* with, so it can be handed to the same adaptor.
|
|
71
|
+
*/
|
|
72
|
+
export declare function toSnsEvent(message: SnsHttpMessage, subscriptionArn?: string): {
|
|
73
|
+
Records: {
|
|
74
|
+
EventSource: string;
|
|
75
|
+
EventVersion: string;
|
|
76
|
+
EventSubscriptionArn: string;
|
|
77
|
+
Sns: {
|
|
78
|
+
Type: "Notification" | "SubscriptionConfirmation" | "UnsubscribeConfirmation";
|
|
79
|
+
MessageId: string;
|
|
80
|
+
TopicArn: string;
|
|
81
|
+
Subject: string | null;
|
|
82
|
+
Message: string;
|
|
83
|
+
Timestamp: string;
|
|
84
|
+
SignatureVersion: string;
|
|
85
|
+
Signature: string;
|
|
86
|
+
SigningCertUrl: string;
|
|
87
|
+
UnsubscribeUrl: string;
|
|
88
|
+
MessageAttributes: Record<string, {
|
|
89
|
+
Type: string;
|
|
90
|
+
Value: string;
|
|
91
|
+
}>;
|
|
92
|
+
};
|
|
93
|
+
}[];
|
|
94
|
+
};
|
|
95
|
+
export declare class SnsCertificateUntrusted extends Error {
|
|
96
|
+
readonly url: string;
|
|
97
|
+
constructor(url: string);
|
|
98
|
+
}
|
|
99
|
+
export declare class SnsCertificateUnavailable extends Error {
|
|
100
|
+
readonly url: string;
|
|
101
|
+
readonly status: number;
|
|
102
|
+
constructor(url: string, status: number);
|
|
103
|
+
}
|
|
104
|
+
export declare class SnsSignatureVersionUnknown extends Error {
|
|
105
|
+
readonly version: string;
|
|
106
|
+
constructor(version: string);
|
|
107
|
+
}
|
|
108
|
+
export declare class SnsSignatureInvalid extends Error {
|
|
109
|
+
readonly messageId: string;
|
|
110
|
+
readonly topicArn: string;
|
|
111
|
+
constructor(messageId: string, topicArn: string);
|
|
112
|
+
}
|
|
113
|
+
export declare class SnsMessageTypeUnknown extends Error {
|
|
114
|
+
readonly type: string;
|
|
115
|
+
constructor(type: string);
|
|
116
|
+
}
|
|
117
|
+
export declare class SnsNotAConfirmation extends Error {
|
|
118
|
+
readonly type: string;
|
|
119
|
+
constructor(type: string);
|
|
120
|
+
}
|
|
121
|
+
export declare class SnsConfirmationFailed extends Error {
|
|
122
|
+
readonly topicArn: string;
|
|
123
|
+
readonly status: number;
|
|
124
|
+
constructor(topicArn: string, status: number);
|
|
125
|
+
}
|
|
126
|
+
//#endregion
|
|
127
|
+
//# sourceMappingURL=message.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message.d.cts","names":[],"sources":["../../src/sns/message.ts"],"mappings":";;;;;;;;;;;;;;;;;iBAmBiB;EAChB;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;EAEA;;EAEA;;EAEA;EACA,oBAAoB;IAAiB;IAAc;;;iBAwCnC;;;;;EAKhB,oBAAoB,gBAAgB;;;;;;wBAerB,SAAS,iBAAiB;;wBAS1B,gBAAgB,SAAS;;;;;;;wBAgBnB,iBACrB,SAAS,gBACT,UAAS,0BACP;;;;;wBAsCmB,uBACrB,SAAS,gBACT,WAAU,gBAAgB;EAAU;EAAa;KAC/C;;;;;wBAea,WAAW,SAAS,gBAAgB;;;;;;MAQ/C;MACA;MACA;MACA;MACA;MACA;MACA;MACA;MAEA;MACA;MACA,mBAAiB;QAnKsB;QAAe;;;;;qBA0K9C,gCAAgC;WACvB;EAArB,YAAqB;;qBAUT,kCAAkC;WAEpC;WACA;EAFV,YACU,aACA;;qBAUE,mCAAmC;WAC1B;EAArB,YAAqB;;qBAST,4BAA4B;WAE9B;WACA;EAFV,YACU,mBACA;;qBAWE,8BAA8B;WACrB;EAArB,YAAqB;;qBAST,4BAA4B;WACnB;EAArB,YAAqB;;qBAST,8BAA8B;WAEhC;WACA;EAFV,YACU,kBACA"}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
//#region src/sns/message.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* SNS delivering to an HTTP(S) endpoint, rather than to a queue that is polled.
|
|
4
|
+
*
|
|
5
|
+
* SNS POSTs each message to the subscribed URL as JSON. That is the whole
|
|
6
|
+
* transport: no queue in between, no poller, and the endpoint is handed the
|
|
7
|
+
* same envelope a Lambda subscription receives inside `Records[].Sns` — so the
|
|
8
|
+
* handler that runs deployed can be the one that runs here.
|
|
9
|
+
*
|
|
10
|
+
* The endpoint is public, so a message is trusted only once its signature
|
|
11
|
+
* checks out against a certificate SNS itself serves. Anything else that POSTs
|
|
12
|
+
* there is somebody's guess at the shape.
|
|
13
|
+
*
|
|
14
|
+
* Nothing here calls the SNS API, so nothing here needs the AWS SDK: a server
|
|
15
|
+
* that only receives pushes loads this as `@geekmidas/events/sns/message`.
|
|
16
|
+
*/
|
|
17
|
+
/** A message SNS POSTs to a subscribed HTTP endpoint. */
|
|
18
|
+
export interface SnsHttpMessage {
|
|
19
|
+
Type: 'Notification' | 'SubscriptionConfirmation' | 'UnsubscribeConfirmation';
|
|
20
|
+
MessageId: string;
|
|
21
|
+
TopicArn: string;
|
|
22
|
+
Message: string;
|
|
23
|
+
Timestamp: string;
|
|
24
|
+
SignatureVersion: string;
|
|
25
|
+
Signature: string;
|
|
26
|
+
SigningCertURL: string;
|
|
27
|
+
Subject?: string;
|
|
28
|
+
/** Confirmation messages only. */
|
|
29
|
+
Token?: string;
|
|
30
|
+
/** Confirmation messages only: GET it to confirm. */
|
|
31
|
+
SubscribeURL?: string;
|
|
32
|
+
/** Notifications only. */
|
|
33
|
+
UnsubscribeURL?: string;
|
|
34
|
+
MessageAttributes?: Record<string, {
|
|
35
|
+
Type: string;
|
|
36
|
+
Value: string;
|
|
37
|
+
}>;
|
|
38
|
+
}
|
|
39
|
+
export interface VerifySnsMessageOptions {
|
|
40
|
+
/**
|
|
41
|
+
* Fetch the PEM at a certificate URL. `fetch` by default; replaceable so a
|
|
42
|
+
* test signs with its own key instead of SNS's.
|
|
43
|
+
*/
|
|
44
|
+
fetchCertificate?: (url: string) => Promise<string>;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* A URL, or undefined for a value that is not one at all — what an emulator
|
|
48
|
+
* sends — so it is untrusted rather than a crash. (`URL.parse` is Node 22.1+.)
|
|
49
|
+
*/
|
|
50
|
+
export declare function parseUrl(value: unknown): URL | undefined;
|
|
51
|
+
/** The string SNS signed for this message. */
|
|
52
|
+
export declare function snsStringToSign(message: SnsHttpMessage): string;
|
|
53
|
+
/**
|
|
54
|
+
* Check that SNS sent this message: the certificate is SNS's own, and the
|
|
55
|
+
* signature over the message's signed fields verifies against it.
|
|
56
|
+
*
|
|
57
|
+
* Throws rather than returning false, so a caller cannot forget to look.
|
|
58
|
+
*/
|
|
59
|
+
export declare function verifySnsMessage(message: SnsHttpMessage, options?: VerifySnsMessageOptions): Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Confirm a subscription by visiting the URL SNS sent. Until this happens SNS
|
|
62
|
+
* delivers nothing to the endpoint.
|
|
63
|
+
*/
|
|
64
|
+
export declare function confirmSnsSubscription(message: SnsHttpMessage, request?: (url: string) => Promise<{
|
|
65
|
+
ok: boolean;
|
|
66
|
+
status: number;
|
|
67
|
+
}>): Promise<void>;
|
|
68
|
+
/**
|
|
69
|
+
* A pushed notification as the Lambda event SNS would have invoked a function
|
|
70
|
+
* with, so it can be handed to the same adaptor.
|
|
71
|
+
*/
|
|
72
|
+
export declare function toSnsEvent(message: SnsHttpMessage, subscriptionArn?: string): {
|
|
73
|
+
Records: {
|
|
74
|
+
EventSource: string;
|
|
75
|
+
EventVersion: string;
|
|
76
|
+
EventSubscriptionArn: string;
|
|
77
|
+
Sns: {
|
|
78
|
+
Type: "Notification" | "SubscriptionConfirmation" | "UnsubscribeConfirmation";
|
|
79
|
+
MessageId: string;
|
|
80
|
+
TopicArn: string;
|
|
81
|
+
Subject: string | null;
|
|
82
|
+
Message: string;
|
|
83
|
+
Timestamp: string;
|
|
84
|
+
SignatureVersion: string;
|
|
85
|
+
Signature: string;
|
|
86
|
+
SigningCertUrl: string;
|
|
87
|
+
UnsubscribeUrl: string;
|
|
88
|
+
MessageAttributes: Record<string, {
|
|
89
|
+
Type: string;
|
|
90
|
+
Value: string;
|
|
91
|
+
}>;
|
|
92
|
+
};
|
|
93
|
+
}[];
|
|
94
|
+
};
|
|
95
|
+
export declare class SnsCertificateUntrusted extends Error {
|
|
96
|
+
readonly url: string;
|
|
97
|
+
constructor(url: string);
|
|
98
|
+
}
|
|
99
|
+
export declare class SnsCertificateUnavailable extends Error {
|
|
100
|
+
readonly url: string;
|
|
101
|
+
readonly status: number;
|
|
102
|
+
constructor(url: string, status: number);
|
|
103
|
+
}
|
|
104
|
+
export declare class SnsSignatureVersionUnknown extends Error {
|
|
105
|
+
readonly version: string;
|
|
106
|
+
constructor(version: string);
|
|
107
|
+
}
|
|
108
|
+
export declare class SnsSignatureInvalid extends Error {
|
|
109
|
+
readonly messageId: string;
|
|
110
|
+
readonly topicArn: string;
|
|
111
|
+
constructor(messageId: string, topicArn: string);
|
|
112
|
+
}
|
|
113
|
+
export declare class SnsMessageTypeUnknown extends Error {
|
|
114
|
+
readonly type: string;
|
|
115
|
+
constructor(type: string);
|
|
116
|
+
}
|
|
117
|
+
export declare class SnsNotAConfirmation extends Error {
|
|
118
|
+
readonly type: string;
|
|
119
|
+
constructor(type: string);
|
|
120
|
+
}
|
|
121
|
+
export declare class SnsConfirmationFailed extends Error {
|
|
122
|
+
readonly topicArn: string;
|
|
123
|
+
readonly status: number;
|
|
124
|
+
constructor(topicArn: string, status: number);
|
|
125
|
+
}
|
|
126
|
+
//#endregion
|
|
127
|
+
//# sourceMappingURL=message.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message.d.mts","names":[],"sources":["../../src/sns/message.ts"],"mappings":";;;;;;;;;;;;;;;;;iBAmBiB;EAChB;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;EAEA;;EAEA;;EAEA;EACA,oBAAoB;IAAiB;IAAc;;;iBAwCnC;;;;;EAKhB,oBAAoB,gBAAgB;;;;;;wBAerB,SAAS,iBAAiB;;wBAS1B,gBAAgB,SAAS;;;;;;;wBAgBnB,iBACrB,SAAS,gBACT,UAAS,0BACP;;;;;wBAsCmB,uBACrB,SAAS,gBACT,WAAU,gBAAgB;EAAU;EAAa;KAC/C;;;;;wBAea,WAAW,SAAS,gBAAgB;;;;;;MAQ/C;MACA;MACA;MACA;MACA;MACA;MACA;MACA;MAEA;MACA;MACA,mBAAiB;QAnKsB;QAAe;;;;;qBA0K9C,gCAAgC;WACvB;EAArB,YAAqB;;qBAUT,kCAAkC;WAEpC;WACA;EAFV,YACU,aACA;;qBAUE,mCAAmC;WAC1B;EAArB,YAAqB;;qBAST,4BAA4B;WAE9B;WACA;EAFV,YACU,mBACA;;qBAWE,8BAA8B;WACrB;EAArB,YAAqB;;qBAST,4BAA4B;WACnB;EAArB,YAAqB;;qBAST,8BAA8B;WAEhC;WACA;EAFV,YACU,kBACA"}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { createVerify } from "node:crypto";
|
|
2
|
+
//#region src/sns/message.ts
|
|
3
|
+
/** The fields signed, in the order SNS signs them, per message type. */
|
|
4
|
+
const SIGNED_FIELDS = {
|
|
5
|
+
Notification: [
|
|
6
|
+
"Message",
|
|
7
|
+
"MessageId",
|
|
8
|
+
"Subject",
|
|
9
|
+
"Timestamp",
|
|
10
|
+
"TopicArn",
|
|
11
|
+
"Type"
|
|
12
|
+
],
|
|
13
|
+
SubscriptionConfirmation: [
|
|
14
|
+
"Message",
|
|
15
|
+
"MessageId",
|
|
16
|
+
"SubscribeURL",
|
|
17
|
+
"Timestamp",
|
|
18
|
+
"Token",
|
|
19
|
+
"TopicArn",
|
|
20
|
+
"Type"
|
|
21
|
+
],
|
|
22
|
+
UnsubscribeConfirmation: [
|
|
23
|
+
"Message",
|
|
24
|
+
"MessageId",
|
|
25
|
+
"SubscribeURL",
|
|
26
|
+
"Timestamp",
|
|
27
|
+
"Token",
|
|
28
|
+
"TopicArn",
|
|
29
|
+
"Type"
|
|
30
|
+
]
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Where SNS serves its signing certificates. Anywhere else is a certificate
|
|
34
|
+
* the sender chose, which would let the sender sign its own forgeries.
|
|
35
|
+
*/
|
|
36
|
+
const SNS_CERTIFICATE_HOST = /^sns\.[a-z0-9-]+\.amazonaws\.com(\.cn)?$/;
|
|
37
|
+
const certificates = /* @__PURE__ */ new Map();
|
|
38
|
+
async function defaultFetchCertificate(url) {
|
|
39
|
+
const response = await fetch(url);
|
|
40
|
+
if (!response.ok) throw new SnsCertificateUnavailable(url, response.status);
|
|
41
|
+
return response.text();
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* A URL, or undefined for a value that is not one at all — what an emulator
|
|
45
|
+
* sends — so it is untrusted rather than a crash. (`URL.parse` is Node 22.1+.)
|
|
46
|
+
*/
|
|
47
|
+
function parseUrl(value) {
|
|
48
|
+
try {
|
|
49
|
+
return new URL(String(value));
|
|
50
|
+
} catch {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/** The string SNS signed for this message. */
|
|
55
|
+
function snsStringToSign(message) {
|
|
56
|
+
const fields = SIGNED_FIELDS[message.Type];
|
|
57
|
+
if (!fields) throw new SnsMessageTypeUnknown(String(message.Type));
|
|
58
|
+
return fields.filter((field) => message[field] !== void 0).map((field) => `${field}\n${message[field]}\n`).join("");
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Check that SNS sent this message: the certificate is SNS's own, and the
|
|
62
|
+
* signature over the message's signed fields verifies against it.
|
|
63
|
+
*
|
|
64
|
+
* Throws rather than returning false, so a caller cannot forget to look.
|
|
65
|
+
*/
|
|
66
|
+
async function verifySnsMessage(message, options = {}) {
|
|
67
|
+
const url = parseUrl(message.SigningCertURL);
|
|
68
|
+
if (url?.protocol !== "https:" || !SNS_CERTIFICATE_HOST.test(url.hostname)) throw new SnsCertificateUntrusted(message.SigningCertURL);
|
|
69
|
+
const algorithm = message.SignatureVersion === "1" ? "RSA-SHA1" : message.SignatureVersion === "2" ? "RSA-SHA256" : void 0;
|
|
70
|
+
if (!algorithm) throw new SnsSignatureVersionUnknown(message.SignatureVersion);
|
|
71
|
+
const fetchCertificate = options.fetchCertificate ?? defaultFetchCertificate;
|
|
72
|
+
let certificate = certificates.get(message.SigningCertURL);
|
|
73
|
+
if (!certificate || options.fetchCertificate) {
|
|
74
|
+
certificate = fetchCertificate(message.SigningCertURL);
|
|
75
|
+
if (!options.fetchCertificate) {
|
|
76
|
+
certificates.set(message.SigningCertURL, certificate);
|
|
77
|
+
certificate.catch(() => certificates.delete(message.SigningCertURL));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const verifier = createVerify(algorithm);
|
|
81
|
+
verifier.update(snsStringToSign(message), "utf8");
|
|
82
|
+
if (!verifier.verify(await certificate, message.Signature, "base64")) throw new SnsSignatureInvalid(message.MessageId, message.TopicArn);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Confirm a subscription by visiting the URL SNS sent. Until this happens SNS
|
|
86
|
+
* delivers nothing to the endpoint.
|
|
87
|
+
*/
|
|
88
|
+
async function confirmSnsSubscription(message, request = fetch) {
|
|
89
|
+
if (message.Type !== "SubscriptionConfirmation" || !message.SubscribeURL) throw new SnsNotAConfirmation(message.Type);
|
|
90
|
+
const response = await request(message.SubscribeURL);
|
|
91
|
+
if (!response.ok) throw new SnsConfirmationFailed(message.TopicArn, response.status);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* A pushed notification as the Lambda event SNS would have invoked a function
|
|
95
|
+
* with, so it can be handed to the same adaptor.
|
|
96
|
+
*/
|
|
97
|
+
function toSnsEvent(message, subscriptionArn = "") {
|
|
98
|
+
return { Records: [{
|
|
99
|
+
EventSource: "aws:sns",
|
|
100
|
+
EventVersion: "1.0",
|
|
101
|
+
EventSubscriptionArn: subscriptionArn,
|
|
102
|
+
Sns: {
|
|
103
|
+
Type: message.Type,
|
|
104
|
+
MessageId: message.MessageId,
|
|
105
|
+
TopicArn: message.TopicArn,
|
|
106
|
+
Subject: message.Subject ?? null,
|
|
107
|
+
Message: message.Message,
|
|
108
|
+
Timestamp: message.Timestamp,
|
|
109
|
+
SignatureVersion: message.SignatureVersion,
|
|
110
|
+
Signature: message.Signature,
|
|
111
|
+
SigningCertUrl: message.SigningCertURL,
|
|
112
|
+
UnsubscribeUrl: message.UnsubscribeURL ?? "",
|
|
113
|
+
MessageAttributes: message.MessageAttributes ?? {}
|
|
114
|
+
}
|
|
115
|
+
}] };
|
|
116
|
+
}
|
|
117
|
+
var SnsCertificateUntrusted = class extends Error {
|
|
118
|
+
url;
|
|
119
|
+
constructor(url) {
|
|
120
|
+
super(`The SNS message names a signing certificate at '${url}', which is not an https URL on sns.<region>.amazonaws.com. Only SNS can send to this endpoint; reject the request.`);
|
|
121
|
+
this.url = url;
|
|
122
|
+
this.name = "SnsCertificateUntrusted";
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
var SnsCertificateUnavailable = class extends Error {
|
|
126
|
+
url;
|
|
127
|
+
status;
|
|
128
|
+
constructor(url, status) {
|
|
129
|
+
super(`Fetching the SNS signing certificate at '${url}' answered ${status}. The message cannot be verified, so it is not handled; SNS will retry.`);
|
|
130
|
+
this.url = url;
|
|
131
|
+
this.status = status;
|
|
132
|
+
this.name = "SnsCertificateUnavailable";
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
var SnsSignatureVersionUnknown = class extends Error {
|
|
136
|
+
version;
|
|
137
|
+
constructor(version) {
|
|
138
|
+
super(`SNS signature version '${version}' is not one this endpoint verifies (it knows '1' and '2'). Reject the request.`);
|
|
139
|
+
this.version = version;
|
|
140
|
+
this.name = "SnsSignatureVersionUnknown";
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
var SnsSignatureInvalid = class extends Error {
|
|
144
|
+
messageId;
|
|
145
|
+
topicArn;
|
|
146
|
+
constructor(messageId, topicArn) {
|
|
147
|
+
super(`SNS message '${messageId}' from '${topicArn}' does not verify against its signing certificate. It was not sent by SNS, or was altered on the way; reject it.`);
|
|
148
|
+
this.messageId = messageId;
|
|
149
|
+
this.topicArn = topicArn;
|
|
150
|
+
this.name = "SnsSignatureInvalid";
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
var SnsMessageTypeUnknown = class extends Error {
|
|
154
|
+
type;
|
|
155
|
+
constructor(type) {
|
|
156
|
+
super(`'${type}' is not an SNS message type (Notification, SubscriptionConfirmation, UnsubscribeConfirmation). Reject it.`);
|
|
157
|
+
this.type = type;
|
|
158
|
+
this.name = "SnsMessageTypeUnknown";
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
var SnsNotAConfirmation = class extends Error {
|
|
162
|
+
type;
|
|
163
|
+
constructor(type) {
|
|
164
|
+
super(`Only a SubscriptionConfirmation carries a URL to confirm; this is a '${type}'.`);
|
|
165
|
+
this.type = type;
|
|
166
|
+
this.name = "SnsNotAConfirmation";
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
var SnsConfirmationFailed = class extends Error {
|
|
170
|
+
topicArn;
|
|
171
|
+
status;
|
|
172
|
+
constructor(topicArn, status) {
|
|
173
|
+
super(`Confirming the subscription to '${topicArn}' answered ${status}. SNS will deliver nothing until it is confirmed; it resends the confirmation, or subscribe the endpoint again.`);
|
|
174
|
+
this.topicArn = topicArn;
|
|
175
|
+
this.status = status;
|
|
176
|
+
this.name = "SnsConfirmationFailed";
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
//#endregion
|
|
180
|
+
export { SnsCertificateUnavailable, SnsCertificateUntrusted, SnsConfirmationFailed, SnsMessageTypeUnknown, SnsNotAConfirmation, SnsSignatureInvalid, SnsSignatureVersionUnknown, confirmSnsSubscription, parseUrl, snsStringToSign, toSnsEvent, verifySnsMessage };
|
|
181
|
+
|
|
182
|
+
//# sourceMappingURL=message.mjs.map
|