Files
moloapp/src/anthropic/admin/webhook.cpp
T

103 lines
3.8 KiB
C++

#include "anthropic/admin/webhook.h"
#include <wolfssl/options.h>
#include <wolfssl/wolfcrypt/hmac.h>
#include <cctype>
#include <cstdlib>
#include "anthropic/admin/caps.h"
#include "llm/error.h"
#include "security/crypto/Base64.h"
namespace molo::anthropic::admin {
namespace {
std::string lower(std::string s) {
for (char& c : s) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
return s;
}
std::string header(const std::map<std::string, std::string>& h, const char* name) {
for (const auto& [k, v] : h)
if (lower(k) == name) return v;
return "";
}
std::string hmac_sha256(const std::string& key, const std::string& msg) {
Hmac h;
unsigned char out[WC_SHA256_DIGEST_SIZE];
if (wc_HmacInit(&h, nullptr, INVALID_DEVID) != 0) return "";
std::string result;
if (wc_HmacSetKey(&h, WC_SHA256, reinterpret_cast<const unsigned char*>(key.data()),
static_cast<word32>(key.size())) == 0 &&
wc_HmacUpdate(&h, reinterpret_cast<const unsigned char*>(msg.data()), static_cast<word32>(msg.size())) == 0 &&
wc_HmacFinal(&h, out) == 0)
result.assign(reinterpret_cast<char*>(out), sizeof(out));
wc_HmacFree(&h);
return result;
}
bool equal_const(const std::string& a, const std::string& b) {
if (a.size() != b.size()) return false;
unsigned char d = 0;
for (std::size_t i = 0; i < a.size(); ++i) d |= static_cast<unsigned char>(a[i] ^ b[i]);
return d == 0;
}
} // namespace
WebhookVerifier::WebhookVerifier(const std::string& signing_key) {
std::string k = signing_key;
if (k.rfind("whsec_", 0) == 0) k = k.substr(6);
if (k.empty() || !security::crypto::Base64::decode(k, key_) || key_.empty())
throw llm::ApiError::invalid("anthropic", "webhook signing key must be whsec_<base64>");
}
std::string WebhookVerifier::sign(const std::string& id, long timestamp, const std::string& payload) const {
std::string mac = hmac_sha256(key_, id + "." + std::to_string(timestamp) + "." + payload);
return "v1," + security::crypto::Base64::encode(mac);
}
bool WebhookVerifier::verify(const std::string& payload, const std::map<std::string, std::string>& headers,
long now, std::string* why) const {
auto fail = [&](const char* m) {
if (why) *why = m;
return false;
};
if (payload.size() > Caps::kMaxWebhookPayloadBytes) return fail("payload over the byte cap");
std::string id = header(headers, "webhook-id");
std::string ts = header(headers, "webhook-timestamp");
std::string sigs = header(headers, "webhook-signature");
if (id.empty() || ts.empty() || sigs.empty()) return fail("missing webhook headers");
char* end = nullptr;
long t = std::strtol(ts.c_str(), &end, 10);
if (*end || ts.empty()) return fail("bad webhook-timestamp");
long skew = now > t ? now - t : t - now;
if (skew > Caps::kWebhookToleranceSeconds) return fail("timestamp outside the tolerance");
std::string want = sign(id, t, payload).substr(3);
std::size_t pos = 0;
while (pos < sigs.size()) {
std::size_t sp = sigs.find(' ', pos);
std::string part = sigs.substr(pos, sp == std::string::npos ? std::string::npos : sp - pos);
pos = sp == std::string::npos ? sigs.size() : sp + 1;
if (part.rfind("v1,", 0) == 0 && equal_const(part.substr(3), want)) return true;
}
return fail("no matching signature");
}
clients::body::Json WebhookVerifier::unwrap(const std::string& payload,
const std::map<std::string, std::string>& headers,
long now) const {
std::string why;
if (!verify(payload, headers, now, &why)) throw llm::ApiError("anthropic", 400, "invalid_signature", why);
try {
return clients::body::Json::parse(payload);
} catch (const std::exception& e) {
throw llm::ApiError("anthropic", 400, "invalid_payload", e.what());
}
}
} // namespace molo::anthropic::admin