Skip to main content

hyperlane_plugin_webhook/
struct.rs

1use super::*;
2
3/// Streaming FIPS 180-4 SHA-256 hasher with zero heap allocations.
4#[derive(Clone)]
5pub struct Sha256State {
6    /// Eight 32-bit working hash state words.
7    pub(crate) state: [u32; 8],
8    /// Partial 64-byte input block accumulator.
9    pub(crate) buf: [u8; 64],
10    /// Number of valid bytes currently buffered in `buf`.
11    pub(crate) buf_len: usize,
12    /// Total message length absorbed in bytes.
13    pub(crate) total_bytes: u64,
14}
15
16/// Precomputed HMAC-SHA256 key holding ipad and opad midstates for zero-allocation signing/verification.
17#[derive(Clone)]
18pub struct HmacSha256Key {
19    /// SHA-256 state after compressing the 64-byte `key ^ 0x36` inner pad block.
20    pub(crate) ipad_state: [u32; 8],
21    /// SHA-256 state after compressing the 64-byte `key ^ 0x5c` outer pad block.
22    pub(crate) opad_state: [u32; 8],
23}
24
25/// Named secret key participating in per-route webhook secret rotation.
26#[derive(Clone, Data, Eq, PartialEq)]
27pub struct WebhookSecret {
28    /// Logical key identifier surfaced in [`WebhookEvent`] upon successful verification.
29    pub(crate) id: String,
30    /// Raw HMAC-SHA256 signing secret bytes.
31    pub(crate) secret: Vec<u8>,
32}
33
34/// Verification rules for one inbound webhook route or prefix.
35#[derive(Clone, Data, Debug, Eq, PartialEq)]
36pub struct WebhookEndpoint {
37    /// Route path or segment-bounded prefix (for example `"/webhooks/stripe"`).
38    pub(crate) path_prefix: String,
39    /// Request header carrying the webhook signature (stored lowercased).
40    pub(crate) signature_header: String,
41    /// Signature framing and encoding scheme.
42    pub(crate) scheme: WebhookScheme,
43    /// Active signing secrets checked in constant time for zero-downtime key rotation.
44    pub(crate) secrets: Vec<WebhookSecret>,
45    /// Optional separate Unix-seconds timestamp header for `Hex`/`Base64` schemes.
46    #[get(type(clone))]
47    pub(crate) timestamp_header: Option<String>,
48    /// Optional separator when signing `<timestamp><sep><body>` with a separate `timestamp_header`.
49    #[get(type(clone))]
50    pub(crate) timestamp_payload_separator: Option<String>,
51    /// Optional delivery nonce header (for example `X-GitHub-Delivery`) keyed into the replay cache.
52    #[get(type(clone))]
53    pub(crate) id_header: Option<String>,
54    /// Maximum allowed absolute clock skew in seconds (`0` disables skew checks when no timestamp is required).
55    #[get(type(copy))]
56    pub(crate) tolerance_secs: u64,
57    /// Replay-nonce retention TTL in seconds (`0` disables replay caching for this endpoint).
58    #[get(type(copy))]
59    pub(crate) replay_ttl_secs: u64,
60    /// Maximum inbound body length in bytes accepted for verification.
61    #[get(type(copy))]
62    pub(crate) max_body_bytes: usize,
63}
64
65/// Top-level configuration for [`WebhookVerifier`] and [`WebhookPlugin`].
66#[derive(Clone, Data, Debug, Eq, PartialEq)]
67pub struct WebhookConfig {
68    /// Configured webhook endpoints (matched longest-prefix first).
69    pub(crate) endpoints: Vec<WebhookEndpoint>,
70    /// Maximum tracked replay nonces across all 64 shards.
71    #[get(type(copy))]
72    pub(crate) replay_max_entries: usize,
73}
74
75/// Verified webhook delivery metadata injected into Hyperlane [`Context`] under [`ATTR_WEBHOOK_EVENT`].
76#[derive(Clone, Data, Debug, Eq, PartialEq)]
77pub struct WebhookEvent {
78    /// Matched configured endpoint prefix.
79    pub(crate) endpoint_prefix: String,
80    /// Identifier of the active [`WebhookSecret`] that authenticated the payload.
81    pub(crate) secret_id: String,
82    /// Verified Unix epoch timestamp in seconds, when supplied by the scheme or timestamp header.
83    #[get(type(copy))]
84    pub(crate) timestamp: Option<u64>,
85    /// Extracted delivery identifier header value, when present.
86    #[get(type(clone))]
87    pub(crate) delivery_id: Option<String>,
88    /// Verified raw request body length in bytes.
89    #[get(type(copy))]
90    pub(crate) body_len: usize,
91    /// Always `true` when produced by a passing verification.
92    #[get(type(copy))]
93    pub(crate) verified: bool,
94}
95
96/// Precompiled named secret with precomputed HMAC-SHA256 midstates.
97#[derive(Clone)]
98pub(crate) struct CompiledSecret {
99    /// Logical key identifier.
100    pub(crate) id: String,
101    /// Precomputed zero-allocation HMAC-SHA256 key.
102    pub(crate) key: HmacSha256Key,
103}
104
105/// Precompiled endpoint entry ready for hot-path verification.
106#[derive(Clone)]
107pub(crate) struct CompiledEndpoint {
108    /// Normalized path prefix.
109    pub(crate) prefix: String,
110    /// Lowercased signature header name.
111    pub(crate) signature_header: String,
112    /// Signature framing scheme.
113    pub(crate) scheme: WebhookScheme,
114    /// Precompiled active signing secrets.
115    pub(crate) secrets: Vec<CompiledSecret>,
116    /// Optional lowercased separate timestamp header name.
117    pub(crate) timestamp_header: Option<String>,
118    /// Optional separator when signing `<timestamp><sep><body>`.
119    pub(crate) timestamp_payload_separator: Option<String>,
120    /// Optional lowercased delivery ID header name.
121    pub(crate) id_header: Option<String>,
122    /// Timestamp skew window in seconds.
123    pub(crate) tolerance_secs: u64,
124    /// Replay-nonce retention TTL in seconds.
125    pub(crate) replay_ttl_secs: u64,
126    /// Maximum accepted payload length in bytes.
127    pub(crate) max_body_bytes: usize,
128}
129
130/// Shared interior state of the 64-way sharded bounded [`ReplayNonceCache`].
131pub(crate) struct ReplayNonceCacheInner {
132    /// Maximum tracked nonces across all 64 shards.
133    pub(crate) max_entries: usize,
134    /// Lock-free active entry count.
135    pub(crate) count: AtomicUsize,
136    /// 64-way lock-striped digest tables.
137    pub(crate) shards: ReplayShardArray,
138}
139
140/// High-concurrency 64-way sharded bounded TTL replay-nonce cache.
141#[derive(Clone)]
142pub struct ReplayNonceCache {
143    /// Shared sharded cache state.
144    pub(crate) inner: Arc<ReplayNonceCacheInner>,
145}
146
147/// Compiled inbound webhook verification engine backing [`WebhookPlugin`].
148#[derive(Clone)]
149pub struct WebhookVerifier {
150    /// Precompiled endpoints sorted longest-prefix first.
151    pub(crate) endpoints: Arc<Vec<CompiledEndpoint>>,
152    /// 64-shard bounded replay-nonce cache.
153    pub(crate) replay_cache: ReplayNonceCache,
154    /// Prebuilt `401 Unauthorized` wire bytes.
155    pub(crate) prebuilt_unauthorized_wire: ResponseData,
156    /// Prebuilt `409 Conflict` wire bytes for detected replays.
157    pub(crate) prebuilt_replay_wire: ResponseData,
158    /// Prebuilt `413 Payload Too Large` wire bytes.
159    pub(crate) prebuilt_too_large_wire: ResponseData,
160}
161
162/// Process-wide [`WebhookPlugin`] singleton holding a [`WebhookVerifier`].
163#[derive(Clone)]
164pub struct WebhookPlugin {
165    /// Compiled webhook verifier.
166    pub(crate) verifier: WebhookVerifier,
167}
168
169/// Opt-in request middleware verifying inbound webhook signatures and injecting [`WebhookEvent`].
170#[derive(Clone, Copy, Debug, Default)]
171#[hyperlane::request_middleware(-58)]
172pub struct WebhookHook;