Skip to main content

hyperlane_plugin_webhook/
const.rs

1/// Context attribute key storing the verified [`WebhookEvent`](crate::WebhookEvent) metadata.
2pub const ATTR_WEBHOOK_EVENT: &str = "hyperlane_plugin_webhook_event";
3
4/// Default Stripe webhook signature header name.
5pub const HEADER_STRIPE_SIGNATURE: &str = "stripe-signature";
6
7/// Default GitHub SHA-256 webhook signature header name.
8pub const HEADER_GITHUB_SIGNATURE_256: &str = "x-hub-signature-256";
9
10/// Default GitHub delivery nonce header name.
11pub const HEADER_GITHUB_DELIVERY: &str = "x-github-delivery";
12
13/// Default Shopify HMAC-SHA256 webhook signature header name.
14pub const HEADER_SHOPIFY_HMAC_SHA256: &str = "x-shopify-hmac-sha256";
15
16/// Default timestamp skew tolerance window in seconds (5 minutes).
17pub const DEFAULT_TOLERANCE_SECS: u64 = 300;
18
19/// Default replay-nonce TTL in seconds (10 minutes).
20pub const DEFAULT_REPLAY_TTL_SECS: u64 = 600;
21
22/// Default maximum replay cache entries across all 64 shards.
23pub const DEFAULT_REPLAY_MAX_ENTRIES: usize = 65_536;
24
25/// Default maximum inbound webhook body length in bytes (2 MiB).
26pub const DEFAULT_MAX_BODY_BYTES: usize = 2 * 1024 * 1024;
27
28/// Default Stripe signature scheme version key (`"v1"`).
29pub const DEFAULT_STRIPE_VERSION: &str = "v1";
30
31/// Default GitHub signature prefix (`"sha256="`).
32pub const GITHUB_SHA256_PREFIX: &str = "sha256=";
33
34/// Number of lock-striped shards in [`ReplayNonceCache`] (power of two).
35pub(crate) const REPLAY_SHARD_COUNT: usize = 64;
36
37/// FIPS 180-4 SHA-256 initial hash state words.
38pub(crate) const SHA256_INIT: [u32; 8] = [
39    0x6a09_e667,
40    0xbb67_ae85,
41    0x3c6e_f372,
42    0xa54f_f53a,
43    0x510e_527f,
44    0x9b05_688c,
45    0x1f83_d9ab,
46    0x5be0_cd19,
47];
48
49/// FIPS 180-4 SHA-256 round constants.
50pub(crate) const SHA256_K: [u32; 64] = [
51    0x428a_2f98,
52    0x7137_4491,
53    0xb5c0_fbcf,
54    0xe9b5_dba5,
55    0x3956_c25b,
56    0x59f1_11f1,
57    0x923f_82a4,
58    0xab1c_5ed5,
59    0xd807_aa98,
60    0x1283_5b01,
61    0x2431_85be,
62    0x550c_7dc3,
63    0x72be_5d74,
64    0x80de_b1fe,
65    0x9bdc_06a7,
66    0xc19b_f174,
67    0xe49b_69c1,
68    0xefbe_4786,
69    0x0fc1_9dc6,
70    0x240c_a1cc,
71    0x2de9_2c6f,
72    0x4a74_84aa,
73    0x5cb0_a9dc,
74    0x76f9_88da,
75    0x983e_5152,
76    0xa831_c66d,
77    0xb003_27c8,
78    0xbf59_7fc7,
79    0xc6e0_0bf3,
80    0xd5a7_9147,
81    0x06ca_6351,
82    0x1429_2967,
83    0x27b7_0a85,
84    0x2e1b_2138,
85    0x4d2c_6dfc,
86    0x5338_0d13,
87    0x650a_7354,
88    0x766a_0abb,
89    0x81c2_c92e,
90    0x9272_2c85,
91    0xa2bf_e8a1,
92    0xa81a_664b,
93    0xc24b_8b70,
94    0xc76c_51a3,
95    0xd192_e819,
96    0xd699_0624,
97    0xf40e_3585,
98    0x106a_a070,
99    0x19a4_c116,
100    0x1e37_6c08,
101    0x2748_774c,
102    0x34b0_bcb5,
103    0x391c_0cb3,
104    0x4ed8_aa4a,
105    0x5b9c_ca4f,
106    0x682e_6ff3,
107    0x748f_82ee,
108    0x78a5_636f,
109    0x84c8_7814,
110    0x8cc7_0208,
111    0x90be_fffa,
112    0xa450_6ceb,
113    0xbef9_a3f7,
114    0xc671_78f2,
115];
116
117/// Whether ARMv8 little-endian `sha2` hardware acceleration is available at compile time.
118pub(crate) const USE_AARCH64_SHA2: bool = cfg!(all(
119    target_arch = "aarch64",
120    target_feature = "sha2",
121    target_endian = "little"
122));
123
124/// Lowercase hexadecimal encoding table.
125pub(crate) const HEX_DIGITS: &[u8; 16] = b"0123456789abcdef";
126
127/// Standard RFC 4648 base64 encoding alphabet.
128pub(crate) const BASE64_ALPHABET: &[u8; 64] =
129    b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
130
131/// Default `Content-Type` header emitted on webhook rejection responses.
132pub(crate) const REJECT_CONTENT_TYPE: &str = "text/plain; charset=utf-8";
133
134/// Prebuilt response body emitted on `401 Unauthorized` signature or timestamp verification failure.
135pub(crate) const BODY_UNAUTHORIZED: &[u8] = b"Invalid webhook signature";
136
137/// Prebuilt response body emitted on `409 Conflict` when a replayed webhook delivery is detected.
138pub(crate) const BODY_REPLAY: &[u8] = b"Duplicate webhook delivery";
139
140/// Prebuilt response body emitted on `413 Payload Too Large` when body exceeds configured cap.
141pub(crate) const BODY_TOO_LARGE: &[u8] = b"Webhook payload too large";
142
143/// Debug redaction placeholder for secret bytes in [`WebhookSecret`] and [`HmacSha256Key`].
144pub(crate) const DEBUG_REDACTED: &str = "<redacted>";
145
146/// Debug struct name for [`WebhookSecret`].
147pub(crate) const DEBUG_WEBHOOK_SECRET: &str = "WebhookSecret";
148
149/// Debug struct name for [`HmacSha256Key`].
150pub(crate) const DEBUG_HMAC_KEY: &str = "HmacSha256Key";
151
152/// Domain separator byte between route prefix and nonce in replay cache digests.
153pub(crate) const REPLAY_SEPARATOR: &[u8] = &[0xFF];
154
155/// Default timestamp separator used in Stripe-style signed payloads (`"<t>.<body>"`).
156pub(crate) const STRIPE_TIMESTAMP_SEP: &[u8] = b".";
157
158/// Preformatted [`Context`] external attribute map key for [`ATTR_WEBHOOK_EVENT`].
159pub(crate) const ATTR_WEBHOOK_EVENT_MAP_KEY: &str = "External(\"hyperlane_plugin_webhook_event\")";
160
161/// Maximum Stripe signature candidates stored on the stack without heap allocation.
162pub(crate) const STRIPE_STACK_CANDIDATES: usize = 8;
163
164/// Outer HMAC-SHA256 total message bit length (`(64 + 32) * 8 = 768` bits).
165pub(crate) const HMAC_OUTER_BIT_LEN_BE: [u8; 8] = 768u64.to_be_bytes();
166
167/// Two-character lowercase hexadecimal lookup table for every byte `0x00..=0xFF`.
168pub(crate) const HEX_ENCODE_PAIRS: [[u8; 2]; 256] = {
169    let mut table: [[u8; 2]; 256] = [[0u8; 2]; 256];
170    let mut i: usize = 0;
171    while i < 256 {
172        table[i] = [HEX_DIGITS[i >> 4], HEX_DIGITS[i & 0x0F]];
173        i += 1;
174    }
175    table
176};
177
178/// 256-entry ASCII hex nibble decode table (`0..=15` valid, `0xFF` invalid).
179pub(crate) const HEX_DECODE_LUT: [u8; 256] = {
180    let mut table: [u8; 256] = [0xFFu8; 256];
181    let mut c: usize = b'0' as usize;
182    while c <= b'9' as usize {
183        table[c] = (c - b'0' as usize) as u8;
184        c += 1;
185    }
186    let mut c: usize = b'a' as usize;
187    while c <= b'f' as usize {
188        table[c] = (c - b'a' as usize + 10) as u8;
189        c += 1;
190    }
191    let mut c: usize = b'A' as usize;
192    while c <= b'F' as usize {
193        table[c] = (c - b'A' as usize + 10) as u8;
194        c += 1;
195    }
196    table
197};
198
199/// 256-entry RFC 4648 base64 decode table supporting standard (`+/`) and URL-safe (`-_`) alphabets (`0xFF` invalid).
200pub(crate) const BASE64_DECODE_LUT: [u8; 256] = {
201    let mut table: [u8; 256] = [0xFFu8; 256];
202    let mut c: usize = b'A' as usize;
203    while c <= b'Z' as usize {
204        table[c] = (c - b'A' as usize) as u8;
205        c += 1;
206    }
207    let mut c: usize = b'a' as usize;
208    while c <= b'z' as usize {
209        table[c] = (c - b'a' as usize + 26) as u8;
210        c += 1;
211    }
212    let mut c: usize = b'0' as usize;
213    while c <= b'9' as usize {
214        table[c] = (c - b'0' as usize + 52) as u8;
215        c += 1;
216    }
217    table[b'+' as usize] = 62;
218    table[b'-' as usize] = 62;
219    table[b'/' as usize] = 63;
220    table[b'_' as usize] = 63;
221    table
222};
223
224/// ASCII equals sign padding byte for base64 encoding/decoding.
225pub(crate) const BYTE_EQ: u8 = b'=';
226
227/// ASCII forward slash byte for path prefix segment checks.
228pub(crate) const BYTE_SLASH: u8 = b'/';
229
230/// Stripe timestamp parameter key prefix (`"t="`).
231pub(crate) const STRIPE_TIME_KEY: &str = "t";