#include "anthropic/admin/webhook.h" #include #include #include #include #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(std::tolower(static_cast(c))); return s; } std::string header(const std::map& 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(key.data()), static_cast(key.size())) == 0 && wc_HmacUpdate(&h, reinterpret_cast(msg.data()), static_cast(msg.size())) == 0 && wc_HmacFinal(&h, out) == 0) result.assign(reinterpret_cast(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(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_"); } 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& 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& 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