95 lines
3.0 KiB
TypeScript
95 lines
3.0 KiB
TypeScript
|
|
import { createVerify } from "node:crypto";
|
|
|
|
import { config } from "./config";
|
|
|
|
type SnsEnvelope = {
|
|
Type: string;
|
|
MessageId: string;
|
|
TopicArn: string;
|
|
Message: string;
|
|
Timestamp: string;
|
|
Signature: string;
|
|
SignatureVersion?: string;
|
|
SigningCertURL: string;
|
|
Subject?: string;
|
|
SubscribeURL?: string;
|
|
Token?: string;
|
|
};
|
|
|
|
const certCache = new Map<string, string>();
|
|
|
|
function allowedCertUrl(value: string): boolean {
|
|
try {
|
|
const url = new URL(value);
|
|
return (
|
|
url.protocol === "https:" &&
|
|
/^sns\.[a-z0-9-]+\.amazonaws\.com$/.test(url.hostname) &&
|
|
url.pathname.endsWith(".pem")
|
|
);
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function allowedSubscribeUrl(value: string): boolean {
|
|
try {
|
|
const url = new URL(value);
|
|
return url.protocol === "https:" && /^sns\.[a-z0-9-]+\.amazonaws\.com$/.test(url.hostname);
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
async function signingCert(url: string): Promise<string> {
|
|
const cached = certCache.get(url);
|
|
if (cached) return cached;
|
|
const response = await fetch(url);
|
|
if (!response.ok) throw new Error(`SNS-Zertifikat ${response.status}`);
|
|
const pem = await response.text();
|
|
if (!pem.includes("BEGIN CERTIFICATE")) throw new Error("SNS-Zertifikat ungültig");
|
|
certCache.set(url, pem);
|
|
return pem;
|
|
}
|
|
|
|
function canonical(envelope: SnsEnvelope): string {
|
|
if (envelope.Type === "SubscriptionConfirmation" || envelope.Type === "UnsubscribeConfirmation") {
|
|
const parts = [
|
|
"Message", envelope.Message,
|
|
"MessageId", envelope.MessageId,
|
|
"SubscribeURL", envelope.SubscribeURL ?? "",
|
|
"Timestamp", envelope.Timestamp,
|
|
"Token", envelope.Token ?? "",
|
|
"TopicArn", envelope.TopicArn,
|
|
"Type", envelope.Type,
|
|
];
|
|
return `${parts.join("\n")}\n`;
|
|
}
|
|
const parts = ["Message", envelope.Message, "MessageId", envelope.MessageId];
|
|
if (envelope.Subject) parts.push("Subject", envelope.Subject);
|
|
parts.push("Timestamp", envelope.Timestamp, "TopicArn", envelope.TopicArn, "Type", envelope.Type);
|
|
return `${parts.join("\n")}\n`;
|
|
}
|
|
|
|
export async function verifySns(envelope: SnsEnvelope): Promise<void> {
|
|
if (envelope.TopicArn !== config.snsTopicArn) throw new Error("SNS-Topic unbekannt");
|
|
if (!allowedCertUrl(envelope.SigningCertURL)) throw new Error("SNS-Zertifikats-URL ungültig");
|
|
const cert = await signingCert(envelope.SigningCertURL);
|
|
const verifier = createVerify(envelope.SignatureVersion === "2" ? "RSA-SHA256" : "RSA-SHA1");
|
|
verifier.update(canonical(envelope), "utf8");
|
|
verifier.end();
|
|
if (!verifier.verify(cert, envelope.Signature, "base64")) {
|
|
throw new Error("SNS-Signatur ungültig");
|
|
}
|
|
}
|
|
|
|
export async function confirmSubscription(envelope: SnsEnvelope): Promise<void> {
|
|
if (!envelope.SubscribeURL || !allowedSubscribeUrl(envelope.SubscribeURL)) {
|
|
throw new Error("SNS-Subscribe-URL ungültig");
|
|
}
|
|
const response = await fetch(envelope.SubscribeURL);
|
|
if (!response.ok) throw new Error(`SNS-Bestätigung ${response.status}`);
|
|
}
|
|
|
|
export type { SnsEnvelope };
|