diff --git a/Cargo.lock b/Cargo.lock index 30849ef..1e6344e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -27,6 +27,15 @@ version = "0.2.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" +[[package]] +name = "android_system_properties" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae221649c9976a6f6c56ae1facf410f3ddb33cc661c4b7b61020a912d4237fbc" +dependencies = [ + "libc", +] + [[package]] name = "anyhow" version = "1.0.104" @@ -45,7 +54,7 @@ dependencies = [ "nom 7.1.3", "num-traits", "rusticata-macros", - "thiserror", + "thiserror 2.0.21", "time", ] @@ -263,6 +272,12 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" +[[package]] +name = "cfg_aliases" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f079e83a288787bcd14a6aea84cee5c87a67c5a3e660c30f557a3d24761b3527" + [[package]] name = "chacha20" version = "0.10.2" @@ -272,7 +287,7 @@ dependencies = [ "cfg-if", "cipher", "cpufeatures 0.3.1", - "rand_core", + "rand_core 0.10.1", ] [[package]] @@ -287,6 +302,20 @@ dependencies = [ "poly1305", ] +[[package]] +name = "chrono" +version = "0.4.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aa79e62e7697b8e29b513a68abacf485adcd1fe8284a4316c5ae868e6633327" +dependencies = [ + "iana-time-zone", + "js-sys", + "num-traits", + "serde", + "wasm-bindgen", + "windows-link", +] + [[package]] name = "cipher" version = "0.5.2" @@ -313,12 +342,48 @@ version = "0.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c9ea0ac24bc397ab3c98583a3c9ba74fa56b09a4449bbe172b9b1ddb016027a" +[[package]] +name = "combine" +version = "4.6.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4356eba6976435498d05a311ee9fc486da3bc81c200d3f0d7dd9703b41913f92" +dependencies = [ + "bytes", + "memchr", +] + [[package]] name = "const-oid" version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a6ef517f0926dd24a1582492c791b6a4818a4d94e789a334894aa15b0d12f55c" +[[package]] +name = "cookie" +version = "0.18.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a373e3602691c3cdea496d2f0ee5935151e6168fe87739483c463db1b2f2f87" +dependencies = [ + "time", + "version_check", +] + +[[package]] +name = "core-foundation" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2a6cd9ae233e7f62ba4e9353e81a88df7fc8a5987b8d445b4d90c879bd156f6" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + [[package]] name = "cpufeatures" version = "0.2.17" @@ -385,7 +450,7 @@ checksum = "ce6e4c961d6cd6c9a86db418387425e8bdeaf05b3c8bc1411e6dca4c252f1453" dependencies = [ "getrandom 0.4.3", "hybrid-array", - "rand_core", + "rand_core 0.10.1", ] [[package]] @@ -408,7 +473,7 @@ dependencies = [ "curve25519-dalek-derive", "digest 0.11.3", "fiat-crypto", - "rand_core", + "rand_core 0.10.1", "rustc_version", "subtle", "zeroize", @@ -513,7 +578,7 @@ checksum = "6ebaa1a2bf1290ab3bfe5a7b771d050ebffab2711c19a81691c683a5144a25de" dependencies = [ "curve25519-dalek", "ed25519", - "rand_core", + "rand_core 0.10.1", "sha2 0.11.1", "signature", "subtle", @@ -687,6 +752,17 @@ version = "0.3.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "53c0fa8157de1303bfffdaa1cc2a673bfffb60102f76b0ef4441659124373fed" +[[package]] +name = "futures-macro" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fb9654ba8355388abeb8dcb4fc62f511300867002afc858860463bdd9fe0c44" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", +] + [[package]] name = "futures-sink" version = "0.3.34" @@ -707,6 +783,7 @@ checksum = "0d50a92467f8ba5dd6e3ee5d4bd04d73ab2e4e1c44474a0674821dfce14b79bc" dependencies = [ "futures-core", "futures-io", + "futures-macro", "futures-sink", "futures-task", "memchr", @@ -731,8 +808,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0" dependencies = [ "cfg-if", + "js-sys", "libc", "wasi", + "wasm-bindgen", ] [[package]] @@ -742,9 +821,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099" dependencies = [ "cfg-if", + "js-sys", "libc", "r-efi", - "rand_core", + "rand_core 0.10.1", + "wasm-bindgen", ] [[package]] @@ -883,6 +964,22 @@ dependencies = [ "pin-project-lite", "smallvec", "tokio", + "want", +] + +[[package]] +name = "hyper-rustls" +version = "0.27.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfa8e654703247911e29c23fbeaa261834bd9bb74efba2f9acddc37bfb127f53" +dependencies = [ + "http", + "hyper", + "hyper-util", + "rustls", + "tokio", + "tokio-rustls", + "tower-service", ] [[package]] @@ -891,13 +988,46 @@ version = "0.1.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ddc03d96684f9226b8a787cdb71488417b53ab5ea8fdb1dac946cb9431cc8bff" dependencies = [ + "base64 0.23.1", "bytes", + "futures-channel", + "futures-util", "http", "http-body", + "httparse", "hyper", + "ipnet", + "libc", + "percent-encoding", "pin-project-lite", + "socket2", "tokio", "tower-service", + "tracing", +] + +[[package]] +name = "iana-time-zone" +version = "0.1.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "log", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", ] [[package]] @@ -1023,6 +1153,12 @@ dependencies = [ "hybrid-array", ] +[[package]] +name = "ipnet" +version = "2.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "791930b43c0d5973160d90a8f3894509f2b273430f5c5c73b668636d0287c5c0" + [[package]] name = "itertools" version = "0.14.0" @@ -1038,6 +1174,55 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" +[[package]] +name = "jni" +version = "0.22.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5efd9a482cf3a427f00d6b35f14332adc7902ce91efb778580e180ff90fa3498" +dependencies = [ + "cfg-if", + "combine", + "jni-macros", + "jni-sys", + "log", + "simd_cesu8", + "thiserror 2.0.21", + "walkdir", + "windows-link", +] + +[[package]] +name = "jni-macros" +version = "0.22.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a00109accc170f0bdb141fed3e393c565b6f5e072365c3bd58f5b062591560a3" +dependencies = [ + "proc-macro2", + "quote", + "rustc_version", + "simd_cesu8", + "syn 2.0.119", +] + +[[package]] +name = "jni-sys" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6377a88cb3910bee9b0fa88d4f42e1d2da8e79915598f65fb0c7ee14c878af2" +dependencies = [ + "jni-sys-macros", +] + +[[package]] +name = "jni-sys-macros" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38c0b942f458fe50cdac086d2f946512305e5631e720728f2a61aabcd47a6264" +dependencies = [ + "quote", + "syn 2.0.119", +] + [[package]] name = "jobserver" version = "0.1.35" @@ -1136,6 +1321,12 @@ version = "0.4.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f9f8bd3e56ce4dfc153cf470fffbfa98c7620958b312ca5c3a4b8d5181fd13c6" +[[package]] +name = "lru-slab" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4050469837a6ff301cd14c1f8f24f88549e6d548f24f64e2148eb0f72cebc51f" + [[package]] name = "matchers" version = "0.2.0" @@ -1258,6 +1449,25 @@ dependencies = [ "autocfg", ] +[[package]] +name = "oauth2" +version = "5.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51e219e79014df21a225b1860a479e2dcd7cbd9130f4defd4bd0e191ea31d67d" +dependencies = [ + "base64 0.22.1", + "chrono", + "getrandom 0.2.17", + "http", + "rand 0.8.8", + "serde", + "serde_json", + "serde_path_to_error", + "sha2 0.10.9", + "thiserror 1.0.69", + "url", +] + [[package]] name = "oid-registry" version = "0.8.1" @@ -1273,6 +1483,12 @@ version = "1.21.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" +[[package]] +name = "openssl-probe" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c87def4c32ab89d880effc9e097653c8da5d6ef28e6b539d313baaacfbafcbe" + [[package]] name = "parking" version = "2.2.1" @@ -1366,6 +1582,15 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4a6394b9e965e73d0a289ee54f589087e2c676aedf60885baf52c76b771e4958" +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + [[package]] name = "prettyplease" version = "0.2.37" @@ -1436,6 +1661,63 @@ dependencies = [ "prost", ] +[[package]] +name = "quinn" +version = "0.11.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4051e23e9185c255a7e33ef59cdbca87a22d359052eecd22fc6b901fb37d9d11" +dependencies = [ + "bytes", + "cfg_aliases", + "pin-project-lite", + "quinn-proto", + "quinn-udp", + "rustc-hash", + "rustls", + "socket2", + "thiserror 2.0.21", + "tokio", + "tracing", + "web-time", +] + +[[package]] +name = "quinn-proto" +version = "0.11.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e750cca55fe4f0439a15d0bb529da9651e79993e8e72c61a899a36d462befbe" +dependencies = [ + "aws-lc-rs", + "bytes", + "getrandom 0.4.3", + "lru-slab", + "rand 0.10.3", + "rand_pcg", + "ring", + "rustc-hash", + "rustls", + "rustls-pki-types", + "slab", + "thiserror 2.0.21", + "tinyvec", + "tracing", + "web-time", +] + +[[package]] +name = "quinn-udp" +version = "0.5.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af66907df18639dcf4db56ca65490cabc4b27a97dbadd96f2926cca73298f016" +dependencies = [ + "cfg_aliases", + "libc", + "once_cell", + "socket2", + "tracing", + "windows-sys 0.61.2", +] + [[package]] name = "quote" version = "1.0.47" @@ -1457,6 +1739,17 @@ version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" +[[package]] +name = "rand" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e058c7de0b26af77780c769414d6257830bb240f3c38477dbc2c16e5f54d6d4c" +dependencies = [ + "libc", + "rand_chacha", + "rand_core 0.6.4", +] + [[package]] name = "rand" version = "0.10.3" @@ -1465,7 +1758,26 @@ checksum = "65c9fb96cbc91e3478eaae79a69fcd3f1ae4ad052e471fe6732fff548984b4af" dependencies = [ "chacha20", "getrandom 0.4.3", - "rand_core", + "rand_core 0.10.1", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core 0.6.4", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom 0.2.17", ] [[package]] @@ -1474,6 +1786,15 @@ version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69" +[[package]] +name = "rand_pcg" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "caa0f4137e1c0a72f4c651489402276c8e8e1cf081f3b0ba156d2cbeef09e86a" +dependencies = [ + "rand_core 0.10.1", +] + [[package]] name = "rcgen" version = "0.14.10" @@ -1526,6 +1847,47 @@ version = "0.8.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4" +[[package]] +name = "reqwest" +version = "0.13.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16a1cfa75cc186dd73d5818e510e042e40927bccc9c236b061cea97e1eb08029" +dependencies = [ + "base64 0.23.1", + "bytes", + "futures-core", + "futures-util", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-rustls", + "hyper-util", + "js-sys", + "log", + "percent-encoding", + "pin-project-lite", + "quinn", + "rustls", + "rustls-pki-types", + "rustls-platform-verifier", + "serde", + "serde_json", + "serde_urlencoded", + "sync_wrapper", + "tokio", + "tokio-rustls", + "tokio-util", + "tower", + "tower-http", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "wasm-streams", + "web-sys", +] + [[package]] name = "ring" version = "0.17.14" @@ -1540,6 +1902,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "rustc-hash" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b1e7f9a428571be2dc5bc0505c13fb6bf936822b894ec87abf8a08a4e51742d" + [[package]] name = "rustc_version" version = "0.4.1" @@ -1587,15 +1955,55 @@ dependencies = [ "zeroize", ] +[[package]] +name = "rustls-native-certs" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a073f5dc7469f984c52ad2752b63b0807745133b6de880b7b64c1ac4c48aec4" +dependencies = [ + "openssl-probe", + "rustls-pki-types", + "schannel", + "security-framework", +] + [[package]] name = "rustls-pki-types" version = "1.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2f4925028c7eb5d1fcdaf196971378ed9d2c1c4efc7dc5d011256f76c99c0a96" dependencies = [ + "web-time", "zeroize", ] +[[package]] +name = "rustls-platform-verifier" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1167586491e2b18b8bfbb293e8180ec17c201c4f076d7cb3070ca964e7598f98" +dependencies = [ + "core-foundation", + "core-foundation-sys", + "jni", + "log", + "once_cell", + "rustls", + "rustls-native-certs", + "rustls-platform-verifier-android", + "rustls-webpki", + "security-framework", + "security-framework-sys", + "webpki-root-certs", + "windows-sys 0.61.2", +] + +[[package]] +name = "rustls-platform-verifier-android" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eec689c0bc40ff2458a5977b6619cb718087084a18e02a131c599b62d05e1a5f" + [[package]] name = "rustls-webpki" version = "0.103.16" @@ -1620,12 +2028,53 @@ version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f" +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "schannel" +version = "0.1.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cb23c424d8ee841bb4d813677ca237e2a610067d594fec130f87d6093495ab5" +dependencies = [ + "windows-sys 0.61.2", +] + [[package]] name = "scopeguard" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" +[[package]] +name = "security-framework" +version = "3.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7f4bc775c73d9a02cde8bf7b2ec4c9d12743edf609006c7facc23998404cd1d" +dependencies = [ + "bitflags", + "core-foundation", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework-sys" +version = "2.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce2691df843ecc5d231c0b14ece2acc3efb62c0a398c7e1d875f3983ce020e3" +dependencies = [ + "core-foundation-sys", + "libc", +] + [[package]] name = "semver" version = "1.0.28" @@ -1771,9 +2220,25 @@ version = "3.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "28d567dcbaf0049cb8ac2608a76cd95ff9e4412e1899d389ee400918ca7537f5" dependencies = [ - "rand_core", + "rand_core 0.10.1", ] +[[package]] +name = "simd_cesu8" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11031e251abf8611c80f460e19dbdeb54a66db918e49c65a7065b46ac7aec520" +dependencies = [ + "rustc_version", + "simdutf8", +] + +[[package]] +name = "simdutf8" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e" + [[package]] name = "slab" version = "0.4.12" @@ -1849,7 +2314,7 @@ dependencies = [ "serde_json", "sha2 0.10.9", "smallvec", - "thiserror", + "thiserror 2.0.21", "tokio", "tokio-stream", "tracing", @@ -1891,7 +2356,7 @@ dependencies = [ "sqlx-postgres", "sqlx-sqlite", "syn 2.0.119", - "thiserror", + "thiserror 2.0.21", "tokio", "url", ] @@ -1918,7 +2383,7 @@ dependencies = [ "sha1", "sha2 0.11.1", "sqlx-core", - "thiserror", + "thiserror 2.0.21", "tracing", ] @@ -1945,14 +2410,14 @@ dependencies = [ "log", "md-5", "memchr", - "rand", + "rand 0.10.3", "serde", "serde_json", "sha2 0.11.1", "smallvec", "sqlx-core", "stringprep", - "thiserror", + "thiserror 2.0.21", "tracing", "whoami", ] @@ -1976,7 +2441,7 @@ dependencies = [ "percent-encoding", "serde", "sqlx-core", - "thiserror", + "thiserror 2.0.21", "tracing", "url", ] @@ -2031,6 +2496,9 @@ name = "sync_wrapper" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" +dependencies = [ + "futures-core", +] [[package]] name = "synstructure" @@ -2067,13 +2535,33 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "thiserror" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" +dependencies = [ + "thiserror-impl 1.0.69", +] + [[package]] name = "thiserror" version = "2.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09e52cb86a36cede5cb101bf8908837b3e4c6e5e59fe7fd85c23fb56200d189e" dependencies = [ - "thiserror-impl", + "thiserror-impl 2.0.21", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", ] [[package]] @@ -2190,6 +2678,19 @@ dependencies = [ "tokio", ] +[[package]] +name = "tokio-util" +version = "0.7.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e464cf451ba96ebfc6f9b6542f17ee8b8956e33f1e40d9690624e59d7a7f8a4b" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", +] + [[package]] name = "toml" version = "1.1.8+spec-1.1.0" @@ -2245,6 +2746,24 @@ dependencies = [ "tracing", ] +[[package]] +name = "tower-http" +version = "0.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cfcf7e2740e6fc6d4d688b4ef00650406bb94adf4731e43c096c3a19fe40840" +dependencies = [ + "bitflags", + "bytes", + "futures-util", + "http", + "http-body", + "pin-project-lite", + "tower", + "tower-layer", + "tower-service", + "url", +] + [[package]] name = "tower-layer" version = "0.3.3" @@ -2319,6 +2838,12 @@ dependencies = [ "tracing-log", ] +[[package]] +name = "try-lock" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" + [[package]] name = "typenum" version = "1.20.1" @@ -2378,6 +2903,7 @@ dependencies = [ "idna", "percent-encoding", "serde", + "serde_derive", ] [[package]] @@ -2427,20 +2953,24 @@ dependencies = [ "ed25519-dalek", "hex", "hkdf", + "hmac", "prost", "prost-build", - "rand", + "rand 0.10.3", "rcgen", + "reqwest", "rustls", "serde", "serde_json", "sha2 0.11.1", "sqlx", + "subtle", "tokio", "tokio-rustls", "toml", "tracing", "tracing-subscriber", + "url", "uuid", "x25519-dalek", ] @@ -2451,13 +2981,18 @@ version = "0.1.0" dependencies = [ "anyhow", "axum", + "cookie", "ed25519-dalek", "hex", "lettre", - "rand", + "oauth2", + "rand 0.10.3", + "reqwest", "serde", "serde_json", + "sha2 0.11.1", "sqlx", + "subtle", "tempfile", "tokio", "tower", @@ -2493,6 +3028,25 @@ dependencies = [ "tracing-subscriber", ] +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + +[[package]] +name = "want" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec4cdd0dd910afe868b7ef477227d8d538b46b3075031afee8a9f2acb0a2ed0b" +dependencies = [ + "try-lock", +] + [[package]] name = "wasi" version = "0.11.1+wasi-snapshot-preview1" @@ -2512,6 +3066,17 @@ dependencies = [ "wasm-bindgen-shared", ] +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3cbab34de2d982e9b48e18d216d04c4a6f641066ff19ffb699980f591ee3610e" +dependencies = [ + "js-sys", + "tokio", + "wasm-bindgen", +] + [[package]] name = "wasm-bindgen-macro" version = "0.2.129" @@ -2544,6 +3109,48 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "wasm-streams" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d1ec4f6517c9e11ae630e200b2b65d193279042e28edd4a2cda233e46670bbb" +dependencies = [ + "futures-util", + "js-sys", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", +] + +[[package]] +name = "web-sys" +version = "0.3.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88261b9deccee56594c11a3460c462c41f58d148598fe70ad77070126a68aba4" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "web-time" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "webpki-root-certs" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e49d0e65abc00c142e8f9428da991da9f1a250c39be97459901eb12b9b49f62a" +dependencies = [ + "rustls-pki-types", +] + [[package]] name = "webpki-roots" version = "1.0.11" @@ -2559,12 +3166,74 @@ version = "2.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "626c4bac6755d76ffc12cb01b2eac751db1996b9e0041de9aa02c8c211ddc82c" +[[package]] +name = "winapi-util" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "windows-core" +version = "0.62.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-link", + "windows-result", + "windows-strings", +] + +[[package]] +name = "windows-implement" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "windows-interface" +version = "0.59.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "windows-link" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" +[[package]] +name = "windows-result" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-strings" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091" +dependencies = [ + "windows-link", +] + [[package]] name = "windows-sys" version = "0.52.0" @@ -2666,7 +3335,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e7e8131a03190127fb2263afc72b322ecadae46b6ff8c6f399ff5d02f5559af6" dependencies = [ "curve25519-dalek", - "rand_core", + "rand_core 0.10.1", "zeroize", ] @@ -2684,7 +3353,7 @@ dependencies = [ "oid-registry", "ring", "rusticata-macros", - "thiserror", + "thiserror 2.0.21", "time", ] @@ -2721,6 +3390,26 @@ dependencies = [ "synstructure 0.14.0", ] +[[package]] +name = "zerocopy" +version = "0.8.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5fe1f8f1b06191a00962174c61aa5005e0bb391a6d80d07e24d115c01a92ed8" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "863ad3ac83293fb4d740aedbfdc9240dd8d1a50c1099acd76ce80ce7c7230c7f" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "zerofrom" version = "0.1.8" diff --git a/Cargo.toml b/Cargo.toml index 41058cd..772844e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,3 +31,8 @@ tracing = "0.1" tracing-subscriber = { version = "0.3", features = ["env-filter"] } anyhow = "1" toml = "1.1" + +# Shared bounded HTTP clients for Hub OAuth and community webhooks. +reqwest = { version = "0.13.5", default-features = false, features = ["rustls", "json", "form", "query", "stream"] } +hmac = "0.13" +subtle = "2.6" diff --git a/deploy/central/central.loki-code.dev.conf b/deploy/central/central.loki-code.dev.conf index f1abf2b..b7f848f 100644 --- a/deploy/central/central.loki-code.dev.conf +++ b/deploy/central/central.loki-code.dev.conf @@ -6,6 +6,7 @@ server { root /var/www/vnox-acme; default_type text/plain; } + location ^~ /v1/auth/ { access_log off; error_log /dev/null; return 400; } location / { return 301 https://central.loki-code.dev$request_uri; } } server { @@ -19,6 +20,19 @@ server { client_max_body_size 1m; add_header X-Content-Type-Options nosniff always; add_header Referrer-Policy no-referrer always; + # OAuth codes/state must never enter access or upstream error logs. + location ^~ /v1/auth/ { + access_log off; + error_log /dev/null; + proxy_pass http://127.0.0.1:37623; + proxy_http_version 1.1; + proxy_set_header Host $host; + proxy_set_header X-Forwarded-Proto https; + proxy_set_header X-Forwarded-For $remote_addr; + proxy_set_header Connection ""; + proxy_connect_timeout 5s; + proxy_read_timeout 40s; + } location / { proxy_pass http://127.0.0.1:37623; proxy_http_version 1.1; diff --git a/deploy/central/hub.env.example b/deploy/central/hub.env.example index df18f40..0056ad0 100644 --- a/deploy/central/hub.env.example +++ b/deploy/central/hub.env.example @@ -9,3 +9,7 @@ SMTP_USER=replace-on-server SMTP_PASSWORD=replace-on-server MAIL_FROM="Vnox " RUST_LOG=vnox_hub=info + +# Optional GitHub App / OAuth App user sign-in. Configure both or neither. +# GITHUB_CLIENT_ID=your-public-client-id +# GITHUB_CLIENT_SECRET=your-secret diff --git a/docs/design/webhook-api.md b/docs/design/webhook-api.md new file mode 100644 index 0000000..dbec503 --- /dev/null +++ b/docs/design/webhook-api.md @@ -0,0 +1,21 @@ +# Community webhook API v1 + +Additive LNEx packets `0x0200..0x0205` manage channel-scoped integrations over an already authenticated encrypted session. `MANAGE_WEBHOOKS` is permission bit11. Management requires a persisted text channel in a guild, actual current membership, and the permission (or guild ownership). The operational HTTP listener never exposes management APIs. + +- `WebhookCreate`: channel_id, name, direction (`incoming`/`outgoing`), provider (`generic`/`github`), endpoint for outgoing. +- `WebhookList`: channel_id; returns descriptors without secrets. +- `WebhookRotate`/`WebhookRevoke`: id; authorization is rechecked against its channel. +- `WebhookResult`: descriptor plus new secret only for create/rotate. +- `WebhookListResult`: descriptors. Descriptors include suspension state. All management requests use the additive `WebhookRequest` message; the packet identifies the operation. + +Only incoming delivery is public HTTP: `POST /v1/webhooks/{webhook_id}`. Generic integrations use `Authorization: Bearer ` and JSON `{ "content": "..." }`; unknown identity/avatar/mention fields are rejected. Optional `Idempotency-Key` deduplicates retries. GitHub adapters verify `X-Hub-Signature-256` over raw bytes and use `X-GitHub-Delivery` for deduplication. GitHub initially supports push, pull_request, issues, release and ping summaries. Delivery body is capped64KiB, content4000characters; mass mention syntax is denied. A webhook sender has a reserved `webhook:` identity, never a user-provided sender ID. Production publishing requires HTTPS proxying; bearer tokens must not appear in URLs or access logs. + +Incoming generic secrets are random256-bit values stored only as SHA256 hashes. GitHub HMAC and outgoing signing keys require reversible storage and are AEAD-sealed using `VNOX_WEBHOOK_SECRET_KEY` (64hexcharacters, protected environment). Those modes are unavailable if the master key is not configured. Key rotation replaces the active secret and invalidates the old token immediately. No DM/Hub/private-account access is granted. + +Outgoing hooks initially subscribe only to `message.created` in their configured channel. The message and outbox row commit together. Payload includes id, occurred_at and channel/message/sender/content fields. Oversized ordinary chat content is bounded with an explicit content_truncated flag; message IDs are bounded to 128 characters with id_truncated. Event/delivery IDs are server-generated. Signatures use `X-Vnox-Signature: sha256=`, `X-Vnox-Timestamp` and `X-Vnox-Delivery`. Consumers validate time and deduplicate the `X-Vnox-Delivery` value. Retries are bounded; delivery is at least once, so duplicate sends remain possible after uncertain failures. + +Only HTTPS public destinations are accepted. Each delivery re-resolves and validates every address, pins the request to that resolution, disables proxies and redirects, and applies request timeouts. Loopback/private/link-local/multicast/documentation/special addresses are rejected. Secrets/target query strings are not logged. Outbox capacity is bounded; a full hook is suspended for new events rather than blocking core chat, and management listing exposes the reason. Rotation explicitly resumes a suspended hook. Completed/terminal delivery rows are deleted, including their payload. The worker supports one active gateway process per database; shared-database multi-worker claiming is not supported. Incoming receipt retention is 24 hours with a hard cap of 4096 receipts per hook; retry identifiers outside this window may create a new message. + +This contract adds server functionality; existing desktop clients do not yet have webhook-management UI. JSON/Protobuf mismatch elsewhere is not repaired by these packets. Community hooks are separate from Hub authentication and are not deployed to central.loki-code.dev. + +Permission and membership changes apply at subsequent authorization checks; an already authorized delivery may finish while a role change is in progress. Credential rotation/revocation is revalidated inside the persistence transaction. Active-hook creation and outbox capacity checks share a database transaction lock, including PostgreSQL advisory locking; PostgreSQL concurrency still requires a live integration fixture. diff --git a/gateway/Cargo.toml b/gateway/Cargo.toml index d71dd42..8636aa8 100644 --- a/gateway/Cargo.toml +++ b/gateway/Cargo.toml @@ -43,5 +43,10 @@ rustls = "0.23" tokio-rustls = "0.26" rcgen = "0.14" +subtle.workspace = true +hmac.workspace = true +reqwest.workspace = true +url = "2" + [build-dependencies] prost-build = "0.14" diff --git a/gateway/src/admin/mod.rs b/gateway/src/admin/mod.rs index 949bc8e..4b82387 100644 --- a/gateway/src/admin/mod.rs +++ b/gateway/src/admin/mod.rs @@ -26,12 +26,13 @@ pub struct AdminState { pub channels_count: std::sync::Arc, } -pub async fn run(bind: String, state: AdminState) { +pub async fn run(bind: String, state: AdminState, gateway: crate::net::state::State) { let app = Router::new() .route("/health", get(health)) .route("/metrics", get(metrics)) .route("/version", get(version)) - .with_state(state); + .with_state(state) + .merge(crate::webhooks::incoming::router(gateway)); let listener = match TcpListener::bind(&bind).await { Ok(l) => l, diff --git a/gateway/src/domain/permissions.rs b/gateway/src/domain/permissions.rs index 93aa673..22fce31 100644 --- a/gateway/src/domain/permissions.rs +++ b/gateway/src/domain/permissions.rs @@ -14,6 +14,7 @@ bitflags! { const VOICE_SPEAK = 1 << 8; const VOICE_MUTE = 1 << 9; const VOICE_DEAFEN = 1 << 10; + const MANAGE_WEBHOOKS = 1 << 11; const ADMINISTRATOR = 1 << 63; } } diff --git a/gateway/src/domain/storage/mod.rs b/gateway/src/domain/storage/mod.rs index 41339ac..64be5b4 100644 --- a/gateway/src/domain/storage/mod.rs +++ b/gateway/src/domain/storage/mod.rs @@ -5,6 +5,7 @@ pub mod guilds; pub mod messages; pub mod presence; pub mod social; +pub mod webhooks; use anyhow::Result; use sqlx::{PgPool, SqlitePool}; @@ -29,6 +30,7 @@ impl Storage { pool: Pool::Sqlite(pool), }; s.migrate().await?; + s.migrate_webhooks().await?; info!("storage (sqlite): {path}"); Ok(s) } @@ -39,6 +41,7 @@ impl Storage { pool: Pool::Postgres(pool), }; s.migrate().await?; + s.migrate_webhooks().await?; info!("storage (postgres): connected"); Ok(s) } diff --git a/gateway/src/domain/storage/webhooks/mod.rs b/gateway/src/domain/storage/webhooks/mod.rs new file mode 100644 index 0000000..4fbf4fb --- /dev/null +++ b/gateway/src/domain/storage/webhooks/mod.rs @@ -0,0 +1,225 @@ +//! Persistent webhook configuration and transactional, bounded delivery queues. +use super::{Pool, Storage}; +use crate::proto::ChatMessagePayload; +use anyhow::Result; + +#[derive(Clone, sqlx::FromRow)] +pub struct Hook { + pub id: String, + pub channel_id: String, + pub guild_id: String, + pub creator_id: String, + pub name: String, + pub direction: String, + pub provider: String, + pub endpoint: String, + pub token_hash: String, + pub sealed_secret: Vec, + pub created_at: i64, + pub revoked_at: Option, + pub suspended_reason: Option, +} +#[derive(sqlx::FromRow)] +pub struct Delivery { + pub id: String, + pub hook_id: String, + pub body: String, + pub attempts: i64, +} + +macro_rules! db { + ($s:expr, $p:ident, $body:expr) => { + match &$s.pool { + Pool::Sqlite($p) => $body, + Pool::Postgres($p) => $body, + } + }; +} +impl Storage { + pub async fn migrate_webhooks(&self) -> Result<()> { + let ddl = "CREATE TABLE IF NOT EXISTS webhooks (id TEXT PRIMARY KEY, channel_id TEXT NOT NULL, guild_id TEXT NOT NULL, creator_id TEXT NOT NULL, name TEXT NOT NULL, direction TEXT NOT NULL, provider TEXT NOT NULL, endpoint TEXT NOT NULL, token_hash TEXT NOT NULL, sealed_secret BLOB NOT NULL, created_at BIGINT NOT NULL, revoked_at BIGINT, suspended_reason TEXT); CREATE INDEX IF NOT EXISTS hooks_channel ON webhooks(channel_id); CREATE TABLE IF NOT EXISTS webhook_receipts (hook_id TEXT NOT NULL, delivery_id TEXT NOT NULL, body_hash TEXT NOT NULL, message_id TEXT NOT NULL, created_at BIGINT NOT NULL, PRIMARY KEY(hook_id,delivery_id)); CREATE TABLE IF NOT EXISTS webhook_outbox (id TEXT PRIMARY KEY, hook_id TEXT NOT NULL, body TEXT NOT NULL, attempts BIGINT NOT NULL DEFAULT 0, next_at BIGINT NOT NULL); CREATE INDEX IF NOT EXISTS outbox_due ON webhook_outbox(next_at);"; + match &self.pool { + Pool::Sqlite(p) => { + sqlx::raw_sql(ddl).execute(p).await?; + } + Pool::Postgres(p) => { + sqlx::raw_sql("CREATE TABLE IF NOT EXISTS webhooks (id TEXT PRIMARY KEY, channel_id TEXT NOT NULL, guild_id TEXT NOT NULL, creator_id TEXT NOT NULL, name TEXT NOT NULL, direction TEXT NOT NULL, provider TEXT NOT NULL, endpoint TEXT NOT NULL, token_hash TEXT NOT NULL, sealed_secret BYTEA NOT NULL, created_at BIGINT NOT NULL, revoked_at BIGINT, suspended_reason TEXT); CREATE INDEX IF NOT EXISTS hooks_channel ON webhooks(channel_id); CREATE TABLE IF NOT EXISTS webhook_receipts (hook_id TEXT NOT NULL, delivery_id TEXT NOT NULL, body_hash TEXT NOT NULL, message_id TEXT NOT NULL, created_at BIGINT NOT NULL, PRIMARY KEY(hook_id,delivery_id)); CREATE TABLE IF NOT EXISTS webhook_outbox (id TEXT PRIMARY KEY, hook_id TEXT NOT NULL, body TEXT NOT NULL, attempts BIGINT NOT NULL DEFAULT 0, next_at BIGINT NOT NULL); CREATE INDEX IF NOT EXISTS outbox_due ON webhook_outbox(next_at);").execute(p).await?; + } + } + Ok(()) + } + pub async fn webhook_scope( + &self, + channel: &str, + user: &str, + ) -> Result> { + Ok(db!(self,p,sqlx::query_as::<_,(String,String,i64)>("SELECT c.guild_id,g.owner_id,(SELECT COUNT(*) FROM guild_members m WHERE m.guild_id=g.id AND m.user_id=$2) FROM channels c JOIN guilds g ON g.id=c.guild_id WHERE c.id=$1 AND c.kind='text'").bind(channel).bind(user).fetch_optional(p).await?)) + } + pub async fn hook(&self, id: &str) -> Result> { + Ok(db!( + self, + p, + sqlx::query_as::<_, Hook>("SELECT * FROM webhooks WHERE id=$1") + .bind(id) + .fetch_optional(p) + .await? + )) + } + pub async fn hooks(&self, channel: &str) -> Result> { + Ok(db!( + self, + p, + sqlx::query_as::<_, Hook>( + "SELECT * FROM webhooks WHERE channel_id=$1 AND revoked_at IS NULL ORDER BY created_at LIMIT 64" + ) + .bind(channel) + .fetch_all(p) + .await? + )) + } + pub async fn insert_hook(&self, h: &Hook) -> Result<()> { + macro_rules! insert { ($p:expr,$lock:literal) => {{ + let mut tx = $p.begin().await?; + sqlx::query($lock).execute(&mut *tx).await?; + sqlx::query("INSERT INTO webhooks(id,channel_id,guild_id,creator_id,name,direction,provider,endpoint,token_hash,sealed_secret,created_at) SELECT $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11 WHERE (SELECT COUNT(*) FROM webhooks WHERE channel_id=$2 AND revoked_at IS NULL)<32") + .bind(&h.id).bind(&h.channel_id).bind(&h.guild_id).bind(&h.creator_id).bind(&h.name).bind(&h.direction).bind(&h.provider).bind(&h.endpoint).bind(&h.token_hash).bind(&h.sealed_secret).bind(h.created_at).execute(&mut *tx).await?.rows_affected().eq(&1).then_some(()).ok_or_else(||anyhow::anyhow!("channel webhook limit reached"))?; + tx.commit().await?; + Ok(()) + }}; } + match &self.pool { + Pool::Sqlite(p) => insert!(p, "UPDATE webhooks SET created_at=created_at WHERE id=''"), + Pool::Postgres(p) => insert!(p, "SELECT pg_advisory_xact_lock(75678021)"), + } + } + + pub async fn rotate_hook(&self, id: &str, hash: &str, secret: &[u8]) -> Result<()> { + db!(self,p,sqlx::query("UPDATE webhooks SET token_hash=$2,sealed_secret=$3,suspended_reason=NULL WHERE id=$1 AND revoked_at IS NULL").bind(id).bind(hash).bind(secret).execute(p).await?.rows_affected()); + Ok(()) + } + pub async fn revoke_hook(&self, id: &str) -> Result<()> { + db!( + self, + p, + sqlx::query( + "UPDATE webhooks SET revoked_at=$2,token_hash='',sealed_secret=$3 WHERE id=$1" + ) + .bind(id) + .bind(crate::webhooks::now_ms()) + .bind(Vec::::new()) + .execute(p) + .await? + .rows_affected() + ); + db!( + self, + p, + sqlx::query("DELETE FROM webhook_outbox WHERE hook_id=$1") + .bind(id) + .execute(p) + .await? + .rows_affected() + ); + Ok(()) + } + /// Incoming receipt, message and outgoing events commit together. Same ID with a different body is rejected. + pub async fn save_webhook_message( + &self, + msg: &ChatMessagePayload, + receipt: Option<(&str, &str, &str, &str)>, + ) -> Result { + let event_id = uuid::Uuid::new_v4().to_string(); + let event=serde_json::json!({"type":"message.created","id":event_id,"occurred_at":crate::webhooks::now_ms(),"message":{"id":msg.message_id.chars().take(128).collect::(),"id_truncated":msg.message_id.chars().count()>128,"channel_id":msg.channel_id,"sender_id":msg.sender_id,"content":msg.content.chars().take(8000).collect::(),"content_truncated":msg.content.chars().count()>8000,"timestamp":msg.timestamp}}).to_string(); + macro_rules! save { ($p:expr,$lock:literal) => {{ + let mut tx = $p.begin().await?; + // Serialize capacity checks and receipt inserts across concurrent publishers. + sqlx::query($lock).execute(&mut *tx).await?; + if let Some((hook, delivery, hash, token_hash)) = receipt { + // Lock and revalidate the credential immediately before persistence, so rotation/revocation cannot race a waiting delivery. + let active=sqlx::query("UPDATE webhooks SET created_at=created_at WHERE id=$1 AND token_hash=$2 AND channel_id=$3 AND revoked_at IS NULL AND direction='incoming'").bind(hook).bind(token_hash).bind(&msg.channel_id).execute(&mut *tx).await?.rows_affected(); + anyhow::ensure!(active == 1, "webhook credential no longer active"); + let found = sqlx::query_as::<_, (String,)>( + "SELECT body_hash FROM webhook_receipts WHERE hook_id=$1 AND delivery_id=$2", + ) + .bind(hook) + .bind(delivery) + .fetch_optional(&mut *tx) + .await?; + if let Some((old,)) = found { + anyhow::ensure!(old == hash, "idempotency conflict"); + return Ok(false); + } + // Keep at most 4096 receipts per hook; expiration bounds disk usage and the replay window. + sqlx::query("DELETE FROM webhook_receipts WHERE hook_id=$1 AND created_at<$2") + .bind(hook) + .bind(crate::webhooks::now_ms() - 86_400_000) + .execute(&mut *tx) + .await?; + let (count,): (i64,) = + sqlx::query_as("SELECT COUNT(*) FROM webhook_receipts WHERE hook_id=$1") + .bind(hook) + .fetch_one(&mut *tx) + .await?; + anyhow::ensure!(count < 4096, "receipt capacity reached"); + sqlx::query("INSERT INTO webhook_receipts(hook_id,delivery_id,body_hash,message_id,created_at) VALUES($1,$2,$3,$4,$5)").bind(hook).bind(delivery).bind(hash).bind(&msg.message_id).bind(crate::webhooks::now_ms()).execute(&mut *tx).await?; + } + let inserted=sqlx::query("INSERT INTO messages(id,channel_id,sender_id,content,timestamp,reply_to) VALUES($1,$2,$3,$4,$5,$6) ON CONFLICT(id) DO NOTHING").bind(&msg.message_id).bind(&msg.channel_id).bind(&msg.sender_id).bind(&msg.content).bind(msg.timestamp).bind(&msg.reply_to).execute(&mut *tx).await?.rows_affected()>0; + if inserted { + let hooks=sqlx::query_as::<_,(String,)>("SELECT id FROM webhooks WHERE channel_id=$1 AND direction='outgoing' AND revoked_at IS NULL AND suspended_reason IS NULL LIMIT 32").bind(&msg.channel_id).fetch_all(&mut *tx).await?; + for (hook,) in hooks { + let (total,): (i64,) = sqlx::query_as("SELECT COUNT(*) FROM webhook_outbox") + .fetch_one(&mut *tx) + .await?; + let (own,): (i64,) = + sqlx::query_as("SELECT COUNT(*) FROM webhook_outbox WHERE hook_id=$1") + .bind(&hook) + .fetch_one(&mut *tx) + .await?; + if total >= 2048 || own >= 128 { + sqlx::query("UPDATE webhooks SET suspended_reason='queue_full' WHERE id=$1") + .bind(&hook) + .execute(&mut *tx) + .await?; + continue; + } + sqlx::query("INSERT INTO webhook_outbox(id,hook_id,body,next_at) VALUES($1,$2,$3,$4) ON CONFLICT(id) DO NOTHING").bind(format!("{}:{}",hook,event_id)).bind(&hook).bind(&event).bind(crate::webhooks::now_ms()).execute(&mut *tx).await?; + } + } + tx.commit().await?; + Ok(inserted) + }}; } + match &self.pool { + Pool::Sqlite(p) => save!(p, "UPDATE webhooks SET created_at=created_at WHERE id=''"), + Pool::Postgres(p) => save!(p, "SELECT pg_advisory_xact_lock(75678021)"), + } + } + pub async fn due_webhooks(&self) -> Result> { + Ok(db!(self,p,sqlx::query_as::<_,Delivery>("SELECT id,hook_id,body,attempts FROM webhook_outbox WHERE next_at<=$1 ORDER BY next_at LIMIT 16").bind(crate::webhooks::now_ms()).fetch_all(p).await?)) + } + pub async fn finish_webhook(&self, d: &Delivery, success: bool) -> Result<()> { + if success || d.attempts >= 7 { + db!( + self, + p, + sqlx::query("DELETE FROM webhook_outbox WHERE id=$1") + .bind(&d.id) + .execute(p) + .await? + .rows_affected() + ); + } else { + let next = + crate::webhooks::now_ms() + 1000 * (1_i64 << ((d.attempts + 1).min(10) as u32)); + db!( + self, + p, + sqlx::query("UPDATE webhook_outbox SET attempts=attempts+1,next_at=$2 WHERE id=$1") + .bind(&d.id) + .bind(next) + .execute(p) + .await? + .rows_affected() + ); + } + Ok(()) + } +} diff --git a/gateway/src/handler/content/chat.rs b/gateway/src/handler/content/chat.rs index 3acb372..f099888 100644 --- a/gateway/src/handler/content/chat.rs +++ b/gateway/src/handler/content/chat.rs @@ -41,7 +41,9 @@ pub async fn handle(session_id: &str, mut msg: ChatMessagePayload, state: &State msg.timestamp = now_ms(); } - state.storage.save_message(&msg).await?; + if !state.storage.save_webhook_message(&msg, None).await? { + return Ok(()); + } state.metrics.inc(&state.metrics.messages_sent); let _ = state.broadcast.send(BroadcastMsg { diff --git a/gateway/src/handler/dispatch.rs b/gateway/src/handler/dispatch.rs index fa211cc..87cc5a6 100644 --- a/gateway/src/handler/dispatch.rs +++ b/gateway/src/handler/dispatch.rs @@ -21,6 +21,12 @@ pub async fn dispatch( addr: std::net::SocketAddr, ) -> Result<()> { match pid { + PacketId::WebhookCreate + | PacketId::WebhookList + | PacketId::WebhookRotate + | PacketId::WebhookRevoke => { + crate::webhooks::management::handle(ctx, session_id, pid, payload).await?; + } PacketId::Ping => { let ping = PingPayload::decode(payload)?; io::send_encrypted( diff --git a/gateway/src/lib.rs b/gateway/src/lib.rs index 6afbdee..a89de09 100644 --- a/gateway/src/lib.rs +++ b/gateway/src/lib.rs @@ -4,6 +4,7 @@ pub mod domain; pub mod handler; pub mod net; pub mod proto; +pub mod webhooks; use anyhow::Result; use std::sync::Arc; @@ -87,6 +88,13 @@ pub async fn run(cfg: Arc, voice_member_tx: Option, voice_member_tx: Option; #[derive(Clone)] pub struct State { + pub webhooks: Arc, pub sessions: SessionStore, pub channels: ChannelStore, pub storage: Arc, @@ -56,6 +57,7 @@ impl State { let burst = config.gateway.message_rate_burst.unwrap_or(10); let rate_limiter = Arc::new(RateLimiter::new(rate_per_sec, burst)); Self { + webhooks: Arc::default(), sessions, channels, storage, diff --git a/gateway/src/proto/packet/id.rs b/gateway/src/proto/packet/id.rs index be7313d..42f646b 100644 --- a/gateway/src/proto/packet/id.rs +++ b/gateway/src/proto/packet/id.rs @@ -1,6 +1,12 @@ #[repr(u16)] #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum PacketId { + WebhookCreate = 0x0200, + WebhookList = 0x0201, + WebhookRotate = 0x0202, + WebhookRevoke = 0x0203, + WebhookResult = 0x0204, + WebhookListResult = 0x0205, Hello = 0x0001, Auth = 0x0002, Session = 0x0003, @@ -74,6 +80,12 @@ pub enum PacketId { impl PacketId { pub fn from_u16(v: u16) -> Option { match v { + 0x0200 => Some(Self::WebhookCreate), + 0x0201 => Some(Self::WebhookList), + 0x0202 => Some(Self::WebhookRotate), + 0x0203 => Some(Self::WebhookRevoke), + 0x0204 => Some(Self::WebhookResult), + 0x0205 => Some(Self::WebhookListResult), 0x0001 => Some(Self::Hello), 0x0002 => Some(Self::Auth), 0x0003 => Some(Self::Session), diff --git a/gateway/src/webhooks/incoming.rs b/gateway/src/webhooks/incoming.rs new file mode 100644 index 0000000..dfd5db9 --- /dev/null +++ b/gateway/src/webhooks/incoming.rs @@ -0,0 +1,178 @@ +//! The only HTTP webhook surface accepts scoped delivery credentials, never management sessions. +use super::{MAX_BODY, management, now_ms, secrets, valid_content}; +use crate::{ + net::state::{BroadcastMsg, State as Gateway}, + proto::{ChatMessagePayload, PacketId, encode_packet, to_payload}, +}; +use axum::{ + Json, Router, + body::Bytes, + extract::{DefaultBodyLimit, Path, State}, + http::{HeaderMap, StatusCode}, + routing::post, +}; +use serde::Deserialize; + +pub fn router(state: Gateway) -> Router { + Router::new() + .route("/v1/webhooks/{id}", post(deliver)) + .layer(DefaultBodyLimit::max(MAX_BODY)) + .with_state(state) +} +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct Payload { + content: String, +} +fn header<'a>(headers: &'a HeaderMap, key: &str) -> Option<&'a str> { + headers.get(key)?.to_str().ok() +} +fn delivery_id(s: &str) -> bool { + (8..=128).contains(&s.len()) + && s.bytes() + .all(|b| b.is_ascii_alphanumeric() || b"-_.:".contains(&b)) +} + +pub async fn accept( + state: &Gateway, + id: &str, + headers: &HeaderMap, + body: &[u8], +) -> Result { + let _slot = state + .webhooks + .deliveries + .try_acquire() + .map_err(|_| StatusCode::TOO_MANY_REQUESTS)?; + if body.len() > MAX_BODY { + return Err(StatusCode::PAYLOAD_TOO_LARGE); + } + let hook = state + .storage + .hook(id) + .await + .map_err(|_| StatusCode::SERVICE_UNAVAILABLE)? + .filter(|h| h.revoked_at.is_none() && h.direction == "incoming") + .ok_or(StatusCode::UNAUTHORIZED)?; + let (content, delivery) = if hook.provider == "github" { + let key = state + .webhooks + .key() + .ok_or(StatusCode::SERVICE_UNAVAILABLE)?; + let secret = secrets::open(key, id, &hook.sealed_secret) + .map_err(|_| StatusCode::SERVICE_UNAVAILABLE)?; + if !secrets::verify( + &secret, + body, + header(headers, "x-hub-signature-256").unwrap_or_default(), + ) { + return Err(StatusCode::UNAUTHORIZED); + } + let delivery = header(headers, "x-github-delivery") + .ok_or(StatusCode::BAD_REQUEST)? + .to_owned(); + let event = header(headers, "x-github-event").ok_or(StatusCode::BAD_REQUEST)?; + if !["push", "pull_request", "issues", "release", "ping"].contains(&event) { + return Err(StatusCode::UNPROCESSABLE_ENTITY); + } + let data: serde_json::Value = + serde_json::from_slice(body).map_err(|_| StatusCode::BAD_REQUEST)?; + // Never copy arbitrary descriptions/URLs/mentions into chat: predictable provider summary only. + let repo = data + .pointer("/repository/full_name") + .and_then(|v| v.as_str()) + .unwrap_or("repository"); + let actor = data + .pointer("/sender/login") + .and_then(|v| v.as_str()) + .unwrap_or("GitHub"); + if [repo, actor].iter().any(|v| { + v.len() > 200 + || !v + .bytes() + .all(|b| b.is_ascii_alphanumeric() || b"-_./".contains(&b)) + }) { + return Err(StatusCode::BAD_REQUEST); + } + (format!("GitHub · {repo}: {event} ({actor})"), delivery) + } else { + let supplied = header(headers, "authorization") + .and_then(|v| v.strip_prefix("Bearer ")) + .filter(|v| v.len() == 64) + .ok_or(StatusCode::UNAUTHORIZED)?; + use subtle::ConstantTimeEq; + let actual = secrets::digest(supplied.as_bytes()); + if !bool::from(actual.as_bytes().ct_eq(hook.token_hash.as_bytes())) { + return Err(StatusCode::UNAUTHORIZED); + } + let delivery = header(headers, "idempotency-key") + .map(str::to_owned) + .unwrap_or_else(|| uuid::Uuid::new_v4().to_string()); + let payload: Payload = serde_json::from_slice(body).map_err(|_| StatusCode::BAD_REQUEST)?; + (payload.content, delivery) + }; + if !delivery_id(&delivery) || !valid_content(&content) { + return Err(StatusCode::BAD_REQUEST); + } + if !state.webhooks.consume(id) { + return Err(StatusCode::TOO_MANY_REQUESTS); + } + // Permission removal, guild/channel deletion or a changed channel scope disables delivery immediately. + if management::authorize(state, &hook.creator_id, &hook.channel_id) + .await + .map_err(|_| StatusCode::SERVICE_UNAVAILABLE)? + .as_deref() + != Some(hook.guild_id.as_str()) + { + return Err(StatusCode::FORBIDDEN); + } + let msg = ChatMessagePayload { + message_id: uuid::Uuid::new_v4().to_string(), + channel_id: hook.channel_id, + sender_id: format!("webhook:{id}"), + content, + timestamp: now_ms(), + edited: false, + reply_to: None, + }; + let added = state + .storage + .save_webhook_message( + &msg, + Some((id, &delivery, &secrets::digest(body), &hook.token_hash)), + ) + .await + .map_err(|e| { + if e.to_string() == "idempotency conflict" { + StatusCode::CONFLICT + } else { + StatusCode::SERVICE_UNAVAILABLE + } + })?; + if added { + state.metrics.inc(&state.metrics.messages_sent); + let _ = state.broadcast.send(BroadcastMsg { + channel_id: Some(msg.channel_id.clone()), + exclude_session: None, + target_session_id: None, + data: encode_packet(PacketId::ChatMessage, 0, &to_payload(&msg)), + }); + } + Ok(added) +} +async fn deliver( + State(state): State, + Path(id): Path, + headers: HeaderMap, + body: Bytes, +) -> Result<(StatusCode, Json), StatusCode> { + let added = accept(&state, &id, &headers, &body).await?; + Ok(( + if added { + StatusCode::CREATED + } else { + StatusCode::OK + }, + Json(serde_json::json!({"accepted":true,"duplicate":!added})), + )) +} diff --git a/gateway/src/webhooks/management.rs b/gateway/src/webhooks/management.rs new file mode 100644 index 0000000..3646b0f --- /dev/null +++ b/gateway/src/webhooks/management.rs @@ -0,0 +1,189 @@ +//! Management is available only inside an authenticated encrypted LNEx session. +use super::{now_ms, policy, secrets}; +use crate::{ + domain::{permissions::Permissions, storage::webhooks::Hook}, + handler::Ctx, + net::{io, state::State}, + proto::*, +}; +use anyhow::Result; +use prost::Message; +use tokio::io::{AsyncRead, AsyncWrite}; + +pub async fn authorize(state: &State, user: &str, channel: &str) -> Result> { + let Some((guild_id, owner, memberships)) = state.storage.webhook_scope(channel, user).await? + else { + return Ok(None); + }; + if owner == user { + return Ok(Some(guild_id)); + } + if memberships == 0 { + return Ok(None); + } + let roles = state.storage.get_user_role_perms(&guild_id, user).await?; + Ok(Permissions::from_role_perms(&roles) + .has(Permissions::MANAGE_WEBHOOKS) + .then_some(guild_id)) +} +fn info(h: &Hook) -> WebhookInfo { + WebhookInfo { + id: h.id.clone(), + channel_id: h.channel_id.clone(), + name: h.name.clone(), + direction: h.direction.clone(), + provider: h.provider.clone(), + endpoint: h.endpoint.clone(), + created_at: h.created_at, + revoked: h.revoked_at.is_some(), + suspended_reason: h.suspended_reason.clone().unwrap_or_default(), + } +} +pub async fn handle( + ctx: &mut Ctx<'_, S>, + session_id: &str, + pid: PacketId, + payload: &[u8], +) -> Result<()> { + let Some(session) = crate::domain::session::get(&ctx.state.sessions, session_id).await else { + return Ok(()); + }; + if !ctx.state.rate_limiter.try_consume(session_id) { + return error(ctx, "rate limited").await; + } + if payload.len() > 8192 { + return error(ctx, "webhook management payload too large").await; + } + let req = WebhookRequest::decode(payload)?; + let existing = if pid == PacketId::WebhookRotate || pid == PacketId::WebhookRevoke { + ctx.state.storage.hook(&req.id).await? + } else { + None + }; + let channel = existing + .as_ref() + .map(|h| h.channel_id.as_str()) + .unwrap_or(&req.channel_id); + let Some(guild) = authorize(ctx.state, &session.user_id, channel).await? else { + return error(ctx, "permission denied").await; + }; + if pid == PacketId::WebhookList { + let hooks = ctx + .state + .storage + .hooks(channel) + .await? + .iter() + .map(info) + .collect(); + return io::send_encrypted( + ctx.stream, + PacketId::WebhookListResult, + ctx.seq, + &to_payload(&WebhookListResult { hooks }), + ctx.crypto, + ) + .await; + } + let mut secret = String::new(); + let hook = if pid == PacketId::WebhookCreate { + if req.name.trim().is_empty() + || req.name.chars().count() > 64 + || req.name.chars().any(char::is_control) + { + return error(ctx, "invalid name").await; + } + if !matches!( + (req.direction.as_str(), req.provider.as_str()), + ("incoming", "generic" | "github") | ("outgoing", "generic") + ) { + return error(ctx, "unsupported webhook type").await; + } + if req.direction == "outgoing" && policy::endpoint(&req.endpoint).is_err() { + return error( + ctx, + "HTTPS public endpoint required; no credentials/query/fragment", + ) + .await; + } + let id = uuid::Uuid::new_v4().to_string(); + secret = secrets::new_secret(); + let sealed = if req.direction == "outgoing" || req.provider == "github" { + let Some(key) = ctx.state.webhooks.key() else { + return error(ctx, "webhook master key not configured").await; + }; + secrets::seal(key, &id, &secret)? + } else { + Vec::new() + }; + let h = Hook { + id, + channel_id: channel.into(), + guild_id: guild, + creator_id: session.user_id, + name: req.name, + direction: req.direction.clone(), + provider: req.provider, + endpoint: if req.direction == "outgoing" { + req.endpoint + } else { + String::new() + }, + token_hash: secrets::digest(secret.as_bytes()), + sealed_secret: sealed, + created_at: now_ms(), + revoked_at: None, + suspended_reason: None, + }; + ctx.state.storage.insert_hook(&h).await?; + h + } else { + let Some(mut h) = existing.filter(|h| h.revoked_at.is_none()) else { + return error(ctx, "webhook not found").await; + }; + if pid == PacketId::WebhookRevoke { + ctx.state.storage.revoke_hook(&h.id).await?; + h.revoked_at = Some(now_ms()); + } else { + secret = secrets::new_secret(); + let sealed = if h.direction == "outgoing" || h.provider == "github" { + let Some(key) = ctx.state.webhooks.key() else { + return error(ctx, "webhook master key not configured").await; + }; + secrets::seal(key, &h.id, &secret)? + } else { + Vec::new() + }; + ctx.state + .storage + .rotate_hook(&h.id, &secrets::digest(secret.as_bytes()), &sealed) + .await?; + h.suspended_reason = None; + } + h + }; + io::send_encrypted( + ctx.stream, + PacketId::WebhookResult, + ctx.seq, + &to_payload(&WebhookResult { + hook: Some(info(&hook)), + secret, + }), + ctx.crypto, + ) + .await +} +async fn error(ctx: &mut Ctx<'_, S>, msg: &str) -> Result<()> { + io::send_encrypted( + ctx.stream, + PacketId::Error, + ctx.seq, + &to_payload(&ErrorPayload { + code: ErrorCode::PermissionDenied as u32, + message: msg.into(), + }), + ctx.crypto, + ) + .await +} diff --git a/gateway/src/webhooks/mod.rs b/gateway/src/webhooks/mod.rs new file mode 100644 index 0000000..4f3e6ee --- /dev/null +++ b/gateway/src/webhooks/mod.rs @@ -0,0 +1,100 @@ +//! Channel-scoped incoming integrations and bounded signed outgoing delivery. +pub mod incoming; +pub mod management; +pub mod outgoing; +pub mod policy; +pub mod secrets; + +use anyhow::{Result, bail}; +use std::{ + collections::HashMap, + sync::{Mutex, OnceLock}, + time::Instant, +}; + +pub const MAX_BODY: usize = 64 * 1024; +pub const MAX_CONTENT: usize = 4000; + +/// Secrets are intentionally not Debug and never enter request URLs or logs. +pub struct Runtime { + deliveries: tokio::sync::Semaphore, + key: OnceLock<[u8; 32]>, + rates: Mutex>, +} + +impl Default for Runtime { + fn default() -> Self { + Self { + deliveries: tokio::sync::Semaphore::new(64), + key: OnceLock::new(), + rates: Mutex::new(HashMap::new()), + } + } +} + +impl Runtime { + pub fn configure_from_env(&self) -> Result<()> { + if let Ok(encoded) = std::env::var("VNOX_WEBHOOK_SECRET_KEY") { + let raw = hex::decode(encoded.trim()) + .ok() + .and_then(|v| v.try_into().ok()); + let Some(key) = raw else { + bail!("VNOX_WEBHOOK_SECRET_KEY must contain exactly 32 hex-encoded bytes") + }; + self.set_key(key)?; + } + Ok(()) + } + + pub fn set_key(&self, key: [u8; 32]) -> Result<()> { + self.key + .set(key) + .map_err(|_| anyhow::anyhow!("webhook master key already configured")) + } + + pub fn key(&self) -> Option<&[u8; 32]> { + self.key.get() + } + + pub fn consume(&self, id: &str) -> bool { + let Ok(mut rates) = self.rates.lock() else { + return false; + }; + if rates.len() >= 4096 { + rates.retain(|_, (_, at)| at.elapsed().as_secs() < 300); + if !rates.contains_key(id) && rates.len() >= 4096 { + return false; + } + } + let (tokens, last) = rates.entry(id.into()).or_insert((10.0, Instant::now())); + *tokens = (*tokens + last.elapsed().as_secs_f64() * 2.0).min(10.0); + *last = Instant::now(); + if *tokens < 1.0 { + return false; + } + *tokens -= 1.0; + true + } +} + +pub(crate) fn now_ms() -> i64 { + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap_or_default() + .as_millis() as i64 +} + +pub(crate) fn valid_content(content: &str) -> bool { + let lowered = content.to_lowercase(); + !content.trim().is_empty() + && content.chars().count() <= MAX_CONTENT + && !lowered.contains("@everyone") + && !lowered.contains("@here") + && !lowered.contains("<@") + && !content + .chars() + .any(|c| c.is_control() && c != '\n' && c != '\t') +} + +#[cfg(test)] +mod tests; diff --git a/gateway/src/webhooks/outgoing.rs b/gateway/src/webhooks/outgoing.rs new file mode 100644 index 0000000..26f6c6b --- /dev/null +++ b/gateway/src/webhooks/outgoing.rs @@ -0,0 +1,81 @@ +//! Single-process durable worker. Every attempt validates and pins all DNS results. +use super::{management, now_ms, policy, secrets}; +use crate::{ + domain::storage::webhooks::{Delivery, Hook}, + net::state::State, +}; +use std::time::Duration; + +pub async fn run(state: State) { + let mut interval = tokio::time::interval(Duration::from_secs(1)); + loop { + interval.tick().await; + let jobs = match state.storage.due_webhooks().await { + Ok(j) => j, + Err(_) => { + tracing::warn!("webhook outbox read failed"); + continue; + } + }; + for job in jobs { + let hook = match state.storage.hook(&job.hook_id).await { + Ok(Some(h)) if h.revoked_at.is_none() => h, + Ok(_) => { + let _ = state.storage.finish_webhook(&job, true).await; + continue; + } + Err(_) => { + tracing::warn!("webhook configuration read failed"); + continue; + } + }; + // Never forward channel contents after the integration creator loses permission. + let authorized = management::authorize(&state, &hook.creator_id, &hook.channel_id) + .await + .ok() + .flatten() + .as_deref() + == Some(hook.guild_id.as_str()); + let success = authorized && attempt(&state, &hook, &job).await.is_ok(); + if state.storage.finish_webhook(&job, success).await.is_err() { + tracing::warn!("webhook outbox update failed") + } + } + } +} +async fn attempt(state: &State, hook: &Hook, job: &Delivery) -> anyhow::Result<()> { + let key = state + .webhooks + .key() + .ok_or_else(|| anyhow::anyhow!("signing unavailable"))?; + let secret = secrets::open(key, &hook.id, &hook.sealed_secret)?; + let url = policy::endpoint(&hook.endpoint)?; + let addresses = policy::resolve(&url).await?; + let host = url + .host_str() + .ok_or_else(|| anyhow::anyhow!("missing host"))?; + let client = reqwest::Client::builder() + .https_only(true) + .no_proxy() + .redirect(reqwest::redirect::Policy::none()) + .timeout(Duration::from_secs(10)) + .resolve_to_addrs(host, &addresses) + .build()?; + let timestamp = (now_ms() / 1000).to_string(); + let signed = format!("{timestamp}.{}", job.body); + let response = client + .post(url) + .header("content-type", "application/json") + .header("x-vnox-delivery", &job.id) + .header("x-vnox-timestamp", timestamp) + .header( + "x-vnox-signature", + format!("sha256={}", secrets::sign(&secret, signed.as_bytes())), + ) + .body(job.body.clone()) + .send() + .await?; + anyhow::ensure!(response.status().is_success(), "delivery not accepted"); + // The response body is deliberately never buffered or logged. + Ok(()) +} diff --git a/gateway/src/webhooks/policy.rs b/gateway/src/webhooks/policy.rs new file mode 100644 index 0000000..8047ef3 --- /dev/null +++ b/gateway/src/webhooks/policy.rs @@ -0,0 +1,121 @@ +use anyhow::{Result, bail}; +use std::net::{IpAddr, SocketAddr}; +use url::Url; + +pub fn endpoint(value: &str) -> Result { + let url = Url::parse(value)?; + if url.scheme() != "https" + || url.host_str().is_none() + || !url.username().is_empty() + || url.password().is_some() + || url.fragment().is_some() + || url.query().is_some() + || value.len() > 2048 + { + bail!("webhook target must be an HTTPS URL without credentials, query or fragment") + } + if let Ok(ip) = url + .host_str() + .unwrap_or_default() + .trim_matches(['[', ']']) + .parse::() + && !public_ip(ip) + { + bail!("webhook target address is not public") + } + Ok(url) +} + +pub fn public_ip(ip: IpAddr) -> bool { + match ip { + IpAddr::V4(ip) => { + let [a, b, c, _] = ip.octets(); + !(ip.is_private() + || ip.is_loopback() + || ip.is_link_local() + || ip.is_broadcast() + || ip.is_documentation() + || ip.is_unspecified() + || ip.is_multicast() + || a == 0 + || a >= 224 + || (a == 100 && (64..=127).contains(&b)) + || (a == 192 && b == 0 && c == 0) + || (a == 192 && b == 88 && c == 99) + || (a == 198 && (b == 18 || b == 19))) + } + IpAddr::V6(ip) => { + if let Some(v4) = ip.to_ipv4_mapped() { + return public_ip(IpAddr::V4(v4)); + } + let s = ip.segments(); + (0x2000..=0x3fff).contains(&s[0]) + && s[0] != 0x2002 + && s[0] != 0x3ffe + && !(s[0] == 0x2001 && (s[1] <= 0x01ff || s[1] == 0x0db8)) + && !(s[0] == 0x3fff && s[1] <= 0x0fff) + } + } +} + +/// Resolve every attempt and pin the same validated addresses into the HTTP client. +/// Mixed public/private answers are rejected rather than trusting resolver ordering. +pub async fn resolve(url: &Url) -> Result> { + let host = url + .host_str() + .ok_or_else(|| anyhow::anyhow!("missing target host"))? + .trim_matches(['[', ']']); + let addresses = tokio::time::timeout( + std::time::Duration::from_secs(3), + tokio::net::lookup_host((host, url.port_or_known_default().unwrap_or(443))), + ) + .await?? + .collect::>(); + if addresses.len() > 32 || addresses.is_empty() || addresses.iter().any(|a| !public_ip(a.ip())) + { + bail!("webhook target DNS contains a non-public address") + } + Ok(addresses) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn denies_internal_special_and_mapped_addresses() { + for value in [ + "127.0.0.1", + "10.1.2.3", + "169.254.169.254", + "100.64.0.1", + "192.0.0.1", + "198.18.0.1", + "224.0.0.1", + "::1", + "::ffff:127.0.0.1", + "fe80::1", + "fc00::1", + "2001:db8::1", + "2002:a00:1::1", + "3fff::1", + ] { + assert!(!public_ip(value.parse().unwrap()), "{value}"); + } + for value in ["8.8.8.8", "1.1.1.1", "2606:4700:4700::1111"] { + assert!(public_ip(value.parse().unwrap())) + } + } + #[test] + fn urls_require_clean_https_origin_and_path() { + for value in [ + "http://example.org", + "https://user:pass@example.org", + "https://example.org/?token=x", + "https://127.0.0.1", + "file:///etc/passwd", + ] { + assert!(endpoint(value).is_err()); + } + assert!(endpoint("https://example.org/events").is_ok()); + } +} diff --git a/gateway/src/webhooks/secrets.rs b/gateway/src/webhooks/secrets.rs new file mode 100644 index 0000000..8dc421b --- /dev/null +++ b/gateway/src/webhooks/secrets.rs @@ -0,0 +1,93 @@ +use anyhow::{Result, bail}; +use chacha20poly1305::{ + ChaCha20Poly1305, KeyInit, Nonce, + aead::{Aead, Payload}, +}; +use hmac::{Hmac, Mac}; +use rand::Rng; +use sha2::{Digest, Sha256}; + +pub fn new_secret() -> String { + hex::encode(rand::random::<[u8; 32]>()) +} +pub fn digest(secret: &[u8]) -> String { + hex::encode(Sha256::digest(secret)) +} + +pub fn seal(master: &[u8; 32], id: &str, secret: &str) -> Result> { + let cipher = ChaCha20Poly1305::new_from_slice(master) + .map_err(|_| anyhow::anyhow!("invalid webhook key"))?; + let mut nonce = [0u8; 12]; + rand::rng().fill_bytes(&mut nonce); + let encrypted = cipher + .encrypt( + &Nonce::from(nonce), + Payload { + msg: secret.as_bytes(), + aad: id.as_bytes(), + }, + ) + .map_err(|_| anyhow::anyhow!("could not protect webhook signing secret"))?; + Ok([nonce.as_slice(), encrypted.as_slice()].concat()) +} + +pub fn open(master: &[u8; 32], id: &str, encoded: &[u8]) -> Result { + if encoded.len() < 28 { + bail!("invalid protected webhook secret") + } + let nonce = <[u8; 12]>::try_from(&encoded[..12])?; + let cipher = ChaCha20Poly1305::new_from_slice(master) + .map_err(|_| anyhow::anyhow!("invalid webhook key"))?; + let plain = cipher + .decrypt( + &Nonce::from(nonce), + Payload { + msg: &encoded[12..], + aad: id.as_bytes(), + }, + ) + .map_err(|_| anyhow::anyhow!("webhook signing secret could not be opened"))?; + String::from_utf8(plain).map_err(|_| anyhow::anyhow!("invalid webhook signing secret")) +} + +pub fn sign(secret: &str, body: &[u8]) -> String { + let mut mac = as hmac::digest::KeyInit>::new_from_slice(secret.as_bytes()) + .expect("HMAC permits any key length"); + mac.update(body); + hex::encode(mac.finalize().into_bytes()) +} + +pub fn verify(secret: &str, body: &[u8], signature: &str) -> bool { + let Some(raw) = signature + .strip_prefix("sha256=") + .and_then(|s| hex::decode(s).ok()) + else { + return false; + }; + let Ok(mut mac) = as hmac::digest::KeyInit>::new_from_slice(secret.as_bytes()) + else { + return false; + }; + mac.update(body); + mac.verify_slice(&raw).is_ok() +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn github_reference_signature_and_tampering() { + let key = "It's a Secret to Everybody"; + let body = b"Hello, World!"; + let signature = "sha256=757107ea0eb2509fc211221cce984b8a37570b6d7586c22c46f4379c8b043e17"; + assert!(verify(key, body, signature)); + assert!(!verify(key, b"changed", signature)); + } + #[test] + fn protected_secret_is_bound_to_hook_identity() { + let blob = seal(&[7; 32], "first", "secret").unwrap(); + assert_eq!(open(&[7; 32], "first", &blob).unwrap(), "secret"); + assert!(open(&[7; 32], "second", &blob).is_err()); + assert!(open(&[8; 32], "first", &blob).is_err()); + } +} diff --git a/gateway/src/webhooks/tests.rs b/gateway/src/webhooks/tests.rs new file mode 100644 index 0000000..7b14a46 --- /dev/null +++ b/gateway/src/webhooks/tests.rs @@ -0,0 +1,436 @@ +use super::*; +use crate::{ + domain::{ + channels, session, + storage::{Storage, webhooks::Hook}, + }, + net::state::State, + proto::ChatMessagePayload, +}; +use axum::http::{HeaderMap, HeaderValue, StatusCode}; +use std::sync::{Arc, atomic::AtomicUsize}; + +async fn fixture() -> (State, Hook, String) { + let storage = Storage::connect_sqlite(":memory:").await.unwrap(); + let guild = storage.create_guild("owner", "test").await.unwrap(); + storage + .create_channel("target", "target", "text", Some(&guild)) + .await + .unwrap(); + let config = toml::from_str(include_str!("../../../dev/config.toml")).unwrap(); + let dir = std::env::temp_dir().join(format!("vnox-hook-test-{}", uuid::Uuid::new_v4())); + let identity = + crate::bootstrap::server_identity::ServerIdentity::load_or_generate(&dir).unwrap(); + std::fs::remove_file(dir.join("server_identity.json")).unwrap(); + std::fs::remove_dir(dir).unwrap(); + let (broadcast, _) = tokio::sync::broadcast::channel(16); + let state = State::new( + session::new_store(), + channels::new_store(), + Arc::new(storage), + Arc::new(config), + Arc::new(identity), + broadcast, + crate::admin::metrics::Metrics::new(), + Arc::new(AtomicUsize::new(0)), + Arc::new(AtomicUsize::new(0)), + None, + ); + state.webhooks.set_key([7; 32]).unwrap(); + let secret = secrets::new_secret(); + let hook = Hook { + id: "incoming".into(), + channel_id: "target".into(), + guild_id: guild, + creator_id: "owner".into(), + name: "CI".into(), + direction: "incoming".into(), + provider: "generic".into(), + endpoint: String::new(), + token_hash: secrets::digest(secret.as_bytes()), + sealed_secret: Vec::new(), + created_at: now_ms(), + revoked_at: None, + suspended_reason: None, + }; + state.storage.insert_hook(&hook).await.unwrap(); + (state, hook, secret) +} +fn headers(secret: &str, id: &str) -> HeaderMap { + let mut h = HeaderMap::new(); + h.insert( + "authorization", + HeaderValue::from_str(&format!("Bearer {secret}")).unwrap(), + ); + h.insert("idempotency-key", HeaderValue::from_str(id).unwrap()); + h +} +#[tokio::test] +async fn incoming_auth_scope_identity_and_idempotency() { + let (s, h, key) = fixture().await; + let body = br#"{"content":"build passed"}"#; + assert_eq!( + incoming::accept(&s, &h.id, &HeaderMap::new(), body).await, + Err(StatusCode::UNAUTHORIZED) + ); + assert_eq!( + incoming::accept(&s, &h.id, &headers(&"0".repeat(64), "delivery1"), body).await, + Err(StatusCode::UNAUTHORIZED) + ); + let auth = headers(&key, "delivery1"); + assert_eq!(incoming::accept(&s, &h.id, &auth, body).await, Ok(true)); + assert_eq!(incoming::accept(&s, &h.id, &auth, body).await, Ok(false)); + assert_eq!( + incoming::accept(&s, &h.id, &auth, br#"{"content":"other"}"#).await, + Err(StatusCode::CONFLICT) + ); + let history = s.storage.get_history("target", 100).await.unwrap(); + assert_eq!(history.len(), 1); + assert_eq!(history[0].sender_id, "webhook:incoming"); + assert!( + s.storage + .get_history("general", 10) + .await + .unwrap() + .is_empty() + ); +} +#[tokio::test] +async fn rejects_impersonation_mentions_large_payloads_and_revoked_keys() { + let (s, h, key) = fixture().await; + let auth = headers(&key, "delivery1"); + for body in [ + br#"{"content":"hello","sender_id":"owner"}"#.as_slice(), + br#"{"content":"@everyone"}"#, + br#"{"content":"<@123>"}"#, + ] { + assert_eq!( + incoming::accept(&s, &h.id, &auth, body).await, + Err(StatusCode::BAD_REQUEST) + ); + } + assert_eq!( + incoming::accept(&s, &h.id, &auth, &vec![b' '; MAX_BODY + 1]).await, + Err(StatusCode::PAYLOAD_TOO_LARGE) + ); + let rotated = secrets::new_secret(); + s.storage + .rotate_hook(&h.id, &secrets::digest(rotated.as_bytes()), &[]) + .await + .unwrap(); + assert_eq!( + incoming::accept(&s, &h.id, &auth, br#"{"content":"hi"}"#).await, + Err(StatusCode::UNAUTHORIZED) + ); + s.storage.revoke_hook(&h.id).await.unwrap(); + assert_eq!( + incoming::accept( + &s, + &h.id, + &headers(&rotated, "delivery2"), + br#"{"content":"hi"}"# + ) + .await, + Err(StatusCode::UNAUTHORIZED) + ); +} +#[tokio::test] +async fn management_requires_membership_even_with_stale_privileged_role() { + let (s, h, _) = fixture().await; + assert!( + management::authorize(&s, "owner", "target") + .await + .unwrap() + .is_some() + ); + assert!( + management::authorize(&s, "stranger", "target") + .await + .unwrap() + .is_none() + ); + assert!( + management::authorize(&s, "owner", "general") + .await + .unwrap() + .is_none() + ); + s.storage + .add_guild_member(&h.guild_id, "member") + .await + .unwrap(); + assert!( + management::authorize(&s, "member", "target") + .await + .unwrap() + .is_none() + ); + if let crate::domain::storage::Pool::Sqlite(p) = &s.storage.pool { + sqlx::query("INSERT INTO member_roles(guild_id,user_id,role_id) SELECT guild_id,$1,id FROM roles WHERE guild_id=$2").bind("member").bind(&h.guild_id).execute(p).await.unwrap(); + } + assert!( + management::authorize(&s, "member", "target") + .await + .unwrap() + .is_some() + ); + s.storage + .remove_guild_member(&h.guild_id, "member") + .await + .unwrap(); + assert!( + management::authorize(&s, "member", "target") + .await + .unwrap() + .is_none() + ); +} +#[tokio::test] +async fn outbox_is_transactional_bounded_and_retries_are_finite() { + let (s, mut h, key) = fixture().await; + h.id = "outgoing".into(); + h.direction = "outgoing".into(); + h.endpoint = "https://example.com/hook".into(); + h.sealed_secret = secrets::seal(&[7; 32], &h.id, &key).unwrap(); + s.storage.insert_hook(&h).await.unwrap(); + let msg = ChatMessagePayload { + message_id: "event1".into(), + channel_id: "target".into(), + sender_id: "owner".into(), + content: "hello".into(), + timestamp: now_ms(), + edited: false, + reply_to: None, + }; + assert!(s.storage.save_webhook_message(&msg, None).await.unwrap()); + assert!(!s.storage.save_webhook_message(&msg, None).await.unwrap()); + let mut jobs = s.storage.due_webhooks().await.unwrap(); + assert_eq!(jobs.len(), 1); + let job = jobs.pop().unwrap(); + assert!(job.body.contains("message.created")); + s.storage.finish_webhook(&job, false).await.unwrap(); + assert!(s.storage.due_webhooks().await.unwrap().is_empty()); + let terminal = crate::domain::storage::webhooks::Delivery { attempts: 7, ..job }; + s.storage.finish_webhook(&terminal, false).await.unwrap(); + if let crate::domain::storage::Pool::Sqlite(p) = &s.storage.pool { + let (n,): (i64,) = sqlx::query_as("SELECT COUNT(*) FROM webhook_outbox") + .fetch_one(p) + .await + .unwrap(); + assert_eq!(n, 0); + } + for i in 0..130 { + let mut m = msg.clone(); + m.message_id = format!("event{i}"); + s.storage.save_webhook_message(&m, None).await.unwrap(); + } + assert_eq!( + s.storage + .hook(&h.id) + .await + .unwrap() + .unwrap() + .suspended_reason + .as_deref(), + Some("queue_full") + ); + if let crate::domain::storage::Pool::Sqlite(p) = &s.storage.pool { + let (n,): (i64,) = sqlx::query_as("SELECT COUNT(*) FROM webhook_outbox") + .fetch_one(p) + .await + .unwrap(); + assert_eq!(n, 128); + } + s.storage.revoke_hook(&h.id).await.unwrap(); + assert!(s.storage.due_webhooks().await.unwrap().is_empty()); +} +#[tokio::test] +async fn github_requires_valid_raw_body_signature_and_deduplicates() { + let (s, mut h, key) = fixture().await; + h.id = "github".into(); + h.provider = "github".into(); + h.sealed_secret = secrets::seal(&[7; 32], &h.id, &key).unwrap(); + s.storage.insert_hook(&h).await.unwrap(); + let body = br#"{"repository":{"full_name":"owner/repo"},"sender":{"login":"alice"}}"#; + let mut headers = HeaderMap::new(); + headers.insert("x-github-delivery", HeaderValue::from_static("delivery1")); + headers.insert("x-github-event", HeaderValue::from_static("push")); + assert_eq!( + incoming::accept(&s, &h.id, &headers, body).await, + Err(StatusCode::UNAUTHORIZED) + ); + headers.insert( + "x-hub-signature-256", + HeaderValue::from_str(&format!("sha256={}", secrets::sign(&key, body))).unwrap(), + ); + assert_eq!(incoming::accept(&s, &h.id, &headers, body).await, Ok(true)); + assert_eq!(incoming::accept(&s, &h.id, &headers, body).await, Ok(false)); + assert_eq!( + incoming::accept(&s, &h.id, &headers, b"changed").await, + Err(StatusCode::UNAUTHORIZED) + ); +} + +#[tokio::test] +async fn http_delivery_route_enforces_auth_and_streamed_body_limit() { + let (state, hook, key) = fixture().await; + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let address = listener.local_addr().unwrap(); + let task = tokio::spawn(async move { + axum::serve(listener, incoming::router(state)) + .await + .unwrap() + }); + let client = reqwest::Client::builder().no_proxy().build().unwrap(); + let target = format!("http://{address}/v1/webhooks/{}", hook.id); + let response = client + .post(&target) + .json(&serde_json::json!({"content":"hello"})) + .send() + .await + .unwrap(); + assert_eq!(response.status(), StatusCode::UNAUTHORIZED); + let response = client + .post(&target) + .bearer_auth(&key) + .json(&serde_json::json!({"content":"hello"})) + .send() + .await + .unwrap(); + assert_eq!(response.status(), StatusCode::CREATED); + let response = client + .post(&target) + .bearer_auth(&key) + .body(vec![b'x'; MAX_BODY + 1]) + .send() + .await + .unwrap(); + assert_eq!(response.status(), StatusCode::PAYLOAD_TOO_LARGE); + task.abort(); + let _ = task.await; +} + +#[test] +fn rate_limit_and_content_boundaries() { + let runtime = Runtime::default(); + for _ in 0..10 { + assert!(runtime.consume("hook")) + } + assert!(!runtime.consume("hook")); + assert!(runtime.consume("another")); + assert!(valid_content(&"я".repeat(MAX_CONTENT))); + assert!(!valid_content(&"я".repeat(MAX_CONTENT + 1))); + assert!(!valid_content("@EVERYONE hi")); + assert!(!valid_content("\0")); + assert!(valid_content("line\nnext\ttab")); +} + +#[tokio::test] +async fn lnex_management_rejects_stranger_and_creates_only_for_owner() { + use crate::proto::{PacketId, SessionCrypto, WebhookRequest, flags, to_payload}; + let (s, _, _) = fixture().await; + let req = WebhookRequest { + id: String::new(), + channel_id: "target".into(), + name: "builds".into(), + direction: "incoming".into(), + provider: "generic".into(), + endpoint: String::new(), + }; + for (user, expected) in [ + ("stranger", PacketId::Error), + ("owner", PacketId::WebhookResult), + ] { + let session = session::create(&s.sessions, user.into(), user.into()).await; + let crypto = SessionCrypto::derive(&[3; 32], &session.session_id); + let (mut stream, mut peer) = tokio::io::duplex(65536); + let mut seq = 0; + let mut ctx = crate::handler::Ctx { + stream: &mut stream, + seq: &mut seq, + crypto: &crypto, + state: &s, + }; + management::handle( + &mut ctx, + &session.session_id, + PacketId::WebhookCreate, + &to_payload(&req), + ) + .await + .unwrap(); + let (header, _) = crate::net::io::read_packet(&mut peer).await.unwrap(); + assert_eq!(header.packet_id, expected as u16); + assert_ne!(header.flags & flags::ENCRYPTED, 0); + } + let hooks = s.storage.hooks("target").await.unwrap(); + assert_eq!(hooks.len(), 2); + assert!(hooks.iter().all(|h| h.creator_id == "owner")); +} + +#[tokio::test] +async fn durable_outbox_and_receipts_survive_database_reopen() { + let (_, mut hook, secret) = fixture().await; + let path = std::env::temp_dir().join(format!("vnox-outbox-{}.sqlite", uuid::Uuid::new_v4())); + let database = Storage::connect_sqlite(path.to_str().unwrap()) + .await + .unwrap(); + database.insert_hook(&hook).await.unwrap(); + hook.id = "outgoing".into(); + hook.direction = "outgoing".into(); + hook.sealed_secret = secrets::seal(&[7; 32], &hook.id, &secret).unwrap(); + database.insert_hook(&hook).await.unwrap(); + let msg = ChatMessagePayload { + message_id: "persistent".into(), + channel_id: "target".into(), + sender_id: "webhook:incoming".into(), + content: "hello".into(), + timestamp: now_ms(), + edited: false, + reply_to: None, + }; + let hash = secrets::digest(b"body"); + let token_hash = secrets::digest(secret.as_bytes()); + database + .save_webhook_message(&msg, Some(("incoming", "delivery1", &hash, &token_hash))) + .await + .unwrap(); + if let crate::domain::storage::Pool::Sqlite(p) = &database.pool { + p.close().await; + } + drop(database); + let reopened = Storage::connect_sqlite(path.to_str().unwrap()) + .await + .unwrap(); + assert_eq!(reopened.due_webhooks().await.unwrap().len(), 1); + assert_eq!(reopened.get_history("target", 10).await.unwrap().len(), 1); + assert!( + !reopened + .save_webhook_message(&msg, Some(("incoming", "delivery1", &hash, &token_hash))) + .await + .unwrap() + ); + if let crate::domain::storage::Pool::Sqlite(p) = &reopened.pool { + p.close().await; + } + drop(reopened); + std::fs::remove_file(path).unwrap(); +} + +#[tokio::test] +async fn management_lists_active_hooks_after_many_revocations() { + let (state, mut hook, _) = fixture().await; + state.storage.revoke_hook(&hook.id).await.unwrap(); + for index in 0..70 { + hook.id = format!("retired-{index}"); + hook.created_at = index; + state.storage.insert_hook(&hook).await.unwrap(); + state.storage.revoke_hook(&hook.id).await.unwrap(); + } + hook.id = "current-integration".into(); + hook.created_at = 100; + state.storage.insert_hook(&hook).await.unwrap(); + let listed = state.storage.hooks("target").await.unwrap(); + assert_eq!(listed.len(), 1); + assert_eq!(listed[0].id, "current-integration"); +} diff --git a/hub/AUTH.md b/hub/AUTH.md new file mode 100644 index 0000000..0dd372d --- /dev/null +++ b/hub/AUTH.md @@ -0,0 +1,26 @@ +# GitHub sign-in + +Configure `GITHUB_CLIENT_ID` and `GITHUB_CLIENT_SECRET` together in the protected service environment. GitHub Apps and OAuth Apps both support this user authorization flow; the GitHub App private signing key is unnecessary at runtime. Register this exact callback: + +`https://central.loki-code.dev/v1/auth/github/callback` + +The configured `PUBLIC_BASE_URL` determines redirects. Request headers and arbitrary return URLs cannot change them. No repository, private email or installation permission is requested. + +## Browser contract + +- Navigate to `GET /v1/auth/github/start` to begin authorization. +- `GET /v1/auth/github/callback` consumes a matching, browser-bound attempt and redirects to `/account`. +- `GET /v1/auth/session` returns `{authenticated:false}` or `{authenticated:true,user,csrf_token}`. Use same-origin credentials and never persist this response in local storage. +- `POST /v1/auth/logout` requires the exact `Origin` and the session's `X-CSRF-Token`. + +All responses are `no-store`. Cookies use `__Host-` names, Secure, HttpOnly, Path=/ and SameSite=Lax. Login attempts expire after ten minutes and use random state and S256 PKCE. Attempts are consumed atomically before exchanging authorization codes. Sessions expire seven days after creation; SQLite stores only token hashes. GitHub access tokens are used once to fetch `/user`, then discarded. + +Accounts bind to GitHub's numeric user ID. Renaming a GitHub account cannot move it to another Vnox account. Email addresses never implicitly link accounts. This web session currently does not replace the desktop client's local cryptographic identity. + +## Operation + +The Hub binds only to loopback. Its reverse proxy must overwrite `X-Forwarded-For`; it supplies the login rate-limit identity. Use the supplied Nginx configuration: authentication paths disable access/error logging to prevent callback codes and state entering logs. Do not enable request-body, query-string, Authorization or Cookie tracing in application logs. + +Back up SQLite with the issuer identity, protect backups as credentials, and apply the account-deletion policy to them. Expired attempts/sessions are pruned on new login attempts; expiry is independently enforced on every consumption/read. Revoking a GitHub authorization does not currently revoke an already created Vnox session; sign out or delete its database session to revoke it. + +`cargo test -p vnox-hub` exercises the complete local browser flow against an injected provider, wrong-browser/replay/expiry rejection and logout CSRF. A real GitHub login requires the account owner to authorize in their browser; automated tests never impersonate consent. diff --git a/hub/Cargo.toml b/hub/Cargo.toml index 8c2862d..c19956f 100644 --- a/hub/Cargo.toml +++ b/hub/Cargo.toml @@ -24,6 +24,12 @@ sqlx = { version = "0.9", default-features = false, features = ["sqlite", "runti lettre = { version = "0.11.23", default-features = false, features = ["builder", "smtp-transport", "tokio1-rustls", "aws-lc-rs", "webpki-roots"] } url = "2" +reqwest.workspace = true +sha2.workspace = true +oauth2 = { version = "5", default-features = false, features = ["timing-resistant-secret-traits"] } +cookie = "0.18" +subtle = "2.6" + [dev-dependencies] tempfile = "3" tower = { version = "0.5", features = ["util"] } diff --git a/hub/migrations/0002_github_auth.sql b/hub/migrations/0002_github_auth.sql new file mode 100644 index 0000000..9b2f118 --- /dev/null +++ b/hub/migrations/0002_github_auth.sql @@ -0,0 +1,24 @@ +ALTER TABLE accounts ADD COLUMN display_name TEXT NOT NULL DEFAULT ''; +ALTER TABLE accounts ADD COLUMN avatar_url TEXT; +CREATE TABLE external_identities ( + provider TEXT NOT NULL, + subject TEXT NOT NULL, + user_id TEXT NOT NULL REFERENCES accounts(user_id) ON DELETE CASCADE, + linked_at INTEGER NOT NULL, + PRIMARY KEY(provider, subject) +); +CREATE TABLE oauth_attempts ( + state_hash TEXT PRIMARY KEY, + browser_hash TEXT NOT NULL, + pkce_verifier TEXT NOT NULL, + expires_at INTEGER NOT NULL +); +CREATE INDEX oauth_attempt_expiry ON oauth_attempts(expires_at); +CREATE TABLE web_sessions ( + token_hash TEXT PRIMARY KEY, + user_id TEXT NOT NULL REFERENCES accounts(user_id) ON DELETE CASCADE, + csrf_token TEXT NOT NULL, + created_at INTEGER NOT NULL, + expires_at INTEGER NOT NULL +); +CREATE INDEX web_session_expiry ON web_sessions(expires_at); diff --git a/hub/src/auth/mod.rs b/hub/src/auth/mod.rs new file mode 100644 index 0000000..186c113 --- /dev/null +++ b/hub/src/auth/mod.rs @@ -0,0 +1,77 @@ +pub mod provider; +mod routes; +pub mod store; + +use sqlx::SqlitePool; +use std::{ + collections::HashMap, + sync::{Arc, Mutex}, +}; +use url::Url; + +use provider::{GitHubProvider, IdentityProvider}; +pub use routes::router; + +/// Secrets are never Debug/Serialize and never sent to browser or community nodes. +pub struct GitHubConfig { + pub client_id: String, + pub client_secret: String, +} + +pub struct AuthService { + pub pool: SqlitePool, + pub origin: Url, + pub provider: Option>, + limits: Mutex>, +} + +impl AuthService { + pub fn new( + pool: SqlitePool, + origin: Url, + config: Option<&GitHubConfig>, + ) -> anyhow::Result { + let provider = config + .map(|config| { + GitHubProvider::new(config, &origin) + .map(|provider| Arc::new(provider) as Arc) + }) + .transpose()?; + Ok(Self::with_provider(pool, origin, provider)) + } + pub fn with_provider( + pool: SqlitePool, + origin: Url, + provider: Option>, + ) -> Self { + Self { + pool, + origin, + provider, + limits: Mutex::new(HashMap::new()), + } + } + pub fn configured(&self) -> bool { + self.provider.is_some() + } + pub fn allow(&self, ip: &str) -> bool { + let Ok(mut limits) = self.limits.lock() else { + return false; + }; + let now = store::now(); + limits.retain(|_, (start, _)| now - *start < 60); + if limits.len() > 4096 { + return false; + } + if limits.get(ip).is_some_and(|(_, count)| *count >= 20) { + return false; + } + let global = limits.entry("global".into()).or_insert((now, 0)); + if global.1 >= 300 { + return false; + } + global.1 += 1; + limits.entry(ip.to_owned()).or_insert((now, 0)).1 += 1; + true + } +} diff --git a/hub/src/auth/provider.rs b/hub/src/auth/provider.rs new file mode 100644 index 0000000..8c9c40e --- /dev/null +++ b/hub/src/auth/provider.rs @@ -0,0 +1,155 @@ +use std::{future::Future, pin::Pin, time::Duration}; + +use oauth2::{ + AuthType, AuthUrl, AuthorizationCode, ClientId, ClientSecret, CsrfToken, EndpointNotSet, + EndpointSet, PkceCodeChallenge, PkceCodeVerifier, RedirectUrl, TokenResponse, TokenUrl, + basic::BasicClient, +}; +use serde::Deserialize; +use url::Url; + +use super::GitHubConfig; + +#[derive(Clone, Deserialize)] +pub struct GitHubProfile { + pub id: u64, + pub login: String, + pub name: Option, + pub avatar_url: Option, +} + +#[derive(Debug)] +pub struct ProviderError; +impl std::fmt::Display for ProviderError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str("GitHub authentication failed") + } +} +impl std::error::Error for ProviderError {} + +pub trait IdentityProvider: Send + Sync { + fn authorize(&self, state: CsrfToken, challenge: PkceCodeChallenge) -> Url; + fn identify<'a>( + &'a self, + code: &'a str, + verifier: &'a str, + ) -> Pin> + Send + 'a>>; +} + +type Client = BasicClient; +pub struct GitHubProvider { + oauth: Client, + http: reqwest::Client, +} + +impl GitHubProvider { + pub fn new(config: &GitHubConfig, origin: &Url) -> anyhow::Result { + let callback = origin.join("/v1/auth/github/callback")?; + Ok(Self { + oauth: BasicClient::new(ClientId::new(config.client_id.clone())) + .set_client_secret(ClientSecret::new(config.client_secret.clone())) + .set_auth_type(AuthType::RequestBody) + .set_auth_uri(AuthUrl::new( + "https://github.com/login/oauth/authorize".into(), + )?) + .set_token_uri(TokenUrl::new( + "https://github.com/login/oauth/access_token".into(), + )?) + .set_redirect_uri(RedirectUrl::new(callback.to_string())?), + http: reqwest::Client::builder() + .https_only(true) + .redirect(reqwest::redirect::Policy::none()) + .no_proxy() + .connect_timeout(Duration::from_secs(5)) + .timeout(Duration::from_secs(15)) + .user_agent("Vnox-Central/0.1") + .build()?, + }) + } +} + +impl IdentityProvider for GitHubProvider { + fn authorize(&self, state: CsrfToken, challenge: PkceCodeChallenge) -> Url { + // Public identity only: no repository/private-email/offline access scopes. + self.oauth + .authorize_url(|| state) + .set_pkce_challenge(challenge) + .url() + .0 + } + + fn identify<'a>( + &'a self, + code: &'a str, + verifier: &'a str, + ) -> Pin> + Send + 'a>> { + Box::pin(async move { + let http = self.http.clone(); + let transport = move |request: oauth2::HttpRequest| { + let http = http.clone(); + async move { + let (parts, body) = request.into_parts(); + let response = http + .request(parts.method, parts.uri.to_string()) + .headers(parts.headers) + .body(body) + .send() + .await + .map_err(|_| ProviderError)?; + let mut builder = oauth2::http::Response::builder().status(response.status()); + for (name, value) in response.headers() { + builder = builder.header(name, value); + } + let body = bounded_body(response, 32 * 1024).await?; + builder.body(body).map_err(|_| ProviderError) + } + }; + let token = self + .oauth + .exchange_code(AuthorizationCode::new(code.into())) + .set_pkce_verifier(PkceCodeVerifier::new(verifier.into())) + .request_async(&transport) + .await + .map_err(|_| ProviderError)?; + let response = self + .http + .get("https://api.github.com/user") + .header(reqwest::header::ACCEPT, "application/vnd.github+json") + .bearer_auth(token.access_token().secret()) + .send() + .await + .map_err(|_| ProviderError)?; + if !response.status().is_success() { + return Err(ProviderError); + } + let profile: GitHubProfile = + serde_json::from_slice(&bounded_body(response, 64 * 1024).await?) + .map_err(|_| ProviderError)?; + if profile.id == 0 || profile.login.is_empty() || profile.login.len() > 100 { + return Err(ProviderError); + } + Ok(profile) + // Provider tokens are dropped: no permanent storage or forwarding to clients/nodes. + }) + } +} + +async fn bounded_body( + mut response: reqwest::Response, + maximum: usize, +) -> Result, ProviderError> { + if response + .content_length() + .is_some_and(|length| length > maximum as u64) + { + return Err(ProviderError); + } + let mut body = Vec::new(); + while let Some(chunk) = response.chunk().await.map_err(|_| ProviderError)? { + if body.len().saturating_add(chunk.len()) > maximum { + return Err(ProviderError); + } + body.extend_from_slice(&chunk); + } + Ok(body) +} diff --git a/hub/src/auth/routes.rs b/hub/src/auth/routes.rs new file mode 100644 index 0000000..85201b9 --- /dev/null +++ b/hub/src/auth/routes.rs @@ -0,0 +1,186 @@ +use super::{AuthService, store}; +use crate::web::HubState; +use axum::{ + Json, Router, + extract::{Query, State}, + http::{HeaderMap, StatusCode, header}, + response::{IntoResponse, Redirect, Response}, + routing::{get, post}, +}; +use cookie::{Cookie, SameSite}; +use oauth2::{CsrfToken, PkceCodeChallenge}; +use serde::Deserialize; +use serde_json::json; +use std::sync::Arc; +use subtle::ConstantTimeEq; +const BROWSER: &str = "__Host-vnox_oauth"; +const SESSION: &str = "__Host-vnox_session"; +pub fn router() -> Router> { + Router::new() + .route("/v1/auth/github/start", get(start)) + .route("/v1/auth/github/callback", get(callback)) + .route("/v1/auth/session", get(session)) + .route("/v1/auth/logout", post(logout)) +} +fn failure(status: StatusCode, code: &'static str) -> Response { + (status, Json(json!({"error":code}))).into_response() +} +fn cookie(headers: &HeaderMap, name: &str) -> Option { + let mut found = None; + let mut size = 0; + for value in headers.get_all(header::COOKIE) { + size += value.len(); + if size > 8192 { + return None; + } + for part in value.to_str().ok()?.split(';') { + let parsed = Cookie::parse(part.trim().to_owned()).ok()?; + if parsed.name() == name { + if found.is_some() + || parsed.value().len() != 64 + || !parsed.value().bytes().all(|c| c.is_ascii_hexdigit()) + { + return None; + } + found = Some(parsed.value().to_owned()); + } + } + } + found +} +fn set_cookie(response: &mut Response, name: &'static str, value: String, ttl: i64) { + let value = Cookie::build((name, value)) + .secure(true) + .http_only(true) + .same_site(SameSite::Lax) + .path("/") + .max_age(cookie::time::Duration::seconds(ttl)) + .build() + .to_string(); + response + .headers_mut() + .append(header::SET_COOKIE, value.parse().expect("generated cookie")); +} +fn allowed(auth: &AuthService, headers: &HeaderMap) -> bool { + // The loopback-only service trusts only the reverse proxy, which overwrites this header. + let ip = headers + .get("x-forwarded-for") + .and_then(|v| v.to_str().ok()) + .and_then(|v| v.parse::().ok()) + .map(|v| v.to_string()) + .unwrap_or_else(|| "local".into()); + auth.allow(&ip) +} +async fn start(State(state): State>, headers: HeaderMap) -> Response { + let auth = &state.auth; + let Some(provider) = &auth.provider else { + return failure(StatusCode::SERVICE_UNAVAILABLE, "github_not_configured"); + }; + if !allowed(auth, &headers) { + return failure(StatusCode::TOO_MANY_REQUESTS, "try_later"); + } + let csrf = CsrfToken::new_random(); + let browser = store::token(); + let (challenge, verifier) = PkceCodeChallenge::new_random_sha256(); + if store::create_attempt(&auth.pool, csrf.secret(), &browser, verifier.secret()) + .await + .is_err() + { + return failure(StatusCode::SERVICE_UNAVAILABLE, "try_later"); + } + let mut response = Redirect::to(provider.authorize(csrf, challenge).as_str()).into_response(); + set_cookie(&mut response, BROWSER, browser, store::LOGIN_TTL); + response +} +#[derive(Deserialize)] +struct Callback { + code: Option, + state: Option, +} +async fn callback( + State(state): State>, + headers: HeaderMap, + Query(query): Query, +) -> Response { + let auth = &state.auth; + let Some(provider) = &auth.provider else { + return failure(StatusCode::SERVICE_UNAVAILABLE, "github_not_configured"); + }; + if !allowed(auth, &headers) { + return failure(StatusCode::TOO_MANY_REQUESTS, "try_later"); + } + let (Some(code), Some(csrf), Some(browser)) = + (query.code, query.state, cookie(&headers, BROWSER)) + else { + return failure(StatusCode::BAD_REQUEST, "invalid_login"); + }; + if code.is_empty() || code.len() > 1024 || csrf.len() > 256 { + return failure(StatusCode::BAD_REQUEST, "invalid_login"); + } + let Ok(Some(verifier)) = store::consume_attempt(&auth.pool, &csrf, &browser).await else { + return failure(StatusCode::BAD_REQUEST, "invalid_login"); + }; + let Ok(profile) = provider.identify(&code, &verifier).await else { + return failure(StatusCode::BAD_GATEWAY, "github_login_failed"); + }; + let Ok(user) = store::upsert_identity(&auth.pool, &profile).await else { + return failure(StatusCode::SERVICE_UNAVAILABLE, "try_later"); + }; + let Ok((token, _)) = store::create_session(&auth.pool, &user.user_id).await else { + return failure(StatusCode::SERVICE_UNAVAILABLE, "try_later"); + }; + if let Some(old) = cookie(&headers, SESSION) { + let _ = sqlx::query("DELETE FROM web_sessions WHERE token_hash=?") + .bind(store::hash(&old)) + .execute(&auth.pool) + .await; + } + let mut response = + Redirect::to(auth.origin.join("account").expect("fixed path").as_str()).into_response(); + set_cookie(&mut response, SESSION, token, store::SESSION_TTL); + set_cookie(&mut response, BROWSER, String::new(), 0); + response +} +async fn session(State(state): State>, headers: HeaderMap) -> Response { + if let Some(token) = cookie(&headers, SESSION) { + match store::get_session(&state.auth.pool, &token).await { + Ok(Some((user, csrf))) => { + return Json(json!({"authenticated":true,"user":user,"csrf_token":csrf})) + .into_response(); + } + Err(_) => return failure(StatusCode::SERVICE_UNAVAILABLE, "try_later"), + _ => {} + } + } + Json(json!({"authenticated":false})).into_response() +} +async fn logout(State(state): State>, headers: HeaderMap) -> Response { + let origin = state.auth.origin.origin().ascii_serialization(); + if headers.get(header::ORIGIN).and_then(|v| v.to_str().ok()) != Some(origin.as_str()) { + return failure(StatusCode::FORBIDDEN, "invalid_origin"); + } + let Some(token) = cookie(&headers, SESSION) else { + return failure(StatusCode::UNAUTHORIZED, "not_authenticated"); + }; + let Ok(Some((_, csrf))) = store::get_session(&state.auth.pool, &token).await else { + return failure(StatusCode::UNAUTHORIZED, "not_authenticated"); + }; + let supplied = headers + .get("x-csrf-token") + .map(|v| v.as_bytes()) + .unwrap_or_default(); + if !bool::from(csrf.as_bytes().ct_eq(supplied)) { + return failure(StatusCode::FORBIDDEN, "invalid_csrf"); + } + if sqlx::query("DELETE FROM web_sessions WHERE token_hash=?") + .bind(store::hash(&token)) + .execute(&state.auth.pool) + .await + .is_err() + { + return failure(StatusCode::SERVICE_UNAVAILABLE, "try_later"); + } + let mut response = StatusCode::NO_CONTENT.into_response(); + set_cookie(&mut response, SESSION, String::new(), 0); + response +} diff --git a/hub/src/auth/store.rs b/hub/src/auth/store.rs new file mode 100644 index 0000000..3f9cb03 --- /dev/null +++ b/hub/src/auth/store.rs @@ -0,0 +1,156 @@ +use serde::Serialize; +use sha2::{Digest, Sha256}; +use sqlx::SqlitePool; +use uuid::Uuid; + +use super::provider::GitHubProfile; + +pub const LOGIN_TTL: i64 = 600; +pub const SESSION_TTL: i64 = 7 * 24 * 60 * 60; + +#[derive(Debug, Clone, Serialize, sqlx::FromRow)] +pub struct User { + pub user_id: String, + pub handle: String, + pub display_name: String, + pub avatar_url: Option, +} + +pub fn hash(value: &str) -> String { + hex::encode(Sha256::digest(value.as_bytes())) +} +pub fn token() -> String { + use rand::Rng; + let mut bytes = [0; 32]; + rand::rng().fill_bytes(&mut bytes); + hex::encode(bytes) +} +pub fn now() -> i64 { + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap_or_default() + .as_secs() as i64 +} + +pub async fn cleanup(pool: &SqlitePool) -> anyhow::Result<()> { + sqlx::query("DELETE FROM oauth_attempts WHERE expires_at<=?") + .bind(now()) + .execute(pool) + .await?; + sqlx::query("DELETE FROM web_sessions WHERE expires_at<=?") + .bind(now()) + .execute(pool) + .await?; + Ok(()) +} + +pub async fn create_attempt( + pool: &SqlitePool, + state: &str, + browser: &str, + verifier: &str, +) -> anyhow::Result<()> { + cleanup(pool).await?; + let count: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM oauth_attempts") + .fetch_one(pool) + .await?; + anyhow::ensure!(count < 4096, "login capacity reached"); + sqlx::query("INSERT INTO oauth_attempts(state_hash,browser_hash,pkce_verifier,expires_at) VALUES(?,?,?,?)") + .bind(hash(state)).bind(hash(browser)).bind(verifier).bind(now()+LOGIN_TTL).execute(pool).await?; + Ok(()) +} + +pub async fn consume_attempt( + pool: &SqlitePool, + state: &str, + browser: &str, +) -> anyhow::Result> { + Ok(sqlx::query_scalar("DELETE FROM oauth_attempts WHERE state_hash=? AND browser_hash=? AND expires_at>? RETURNING pkce_verifier") + .bind(hash(state)).bind(hash(browser)).bind(now()).fetch_optional(pool).await?) +} + +pub async fn upsert_identity(pool: &SqlitePool, profile: &GitHubProfile) -> anyhow::Result { + let subject = profile.id.to_string(); + if let Some(user) = lookup_identity(pool, &subject).await? { + return Ok(user); + } + let id = Uuid::new_v4(); + // Generated stable handle avoids claiming a privileged/reused handle from another provider. + let handle = format!("u_{}", id.simple()); + let name = profile + .name + .as_deref() + .unwrap_or(&profile.login) + .chars() + .take(100) + .collect::(); + let avatar = profile.avatar_url.as_deref().filter(|value| { + url::Url::parse(value).is_ok_and(|url| { + url.scheme() == "https" + && url.host_str() == Some("avatars.githubusercontent.com") + && url.username().is_empty() + && url.password().is_none() + }) + }); + let mut transaction = pool.begin().await?; + sqlx::query( + "INSERT INTO accounts(user_id,handle,created_at,display_name,avatar_url) VALUES(?,?,?,?,?)", + ) + .bind(id.to_string()) + .bind(handle) + .bind(now()) + .bind(name) + .bind(avatar) + .execute(&mut *transaction) + .await?; + let inserted = sqlx::query("INSERT INTO external_identities(provider,subject,user_id,linked_at) VALUES('github.com',?,?,?) ON CONFLICT(provider,subject) DO NOTHING") + .bind(&subject).bind(id.to_string()).bind(now()).execute(&mut *transaction).await?; + if inserted.rows_affected() == 0 { + transaction.rollback().await?; + } else { + transaction.commit().await?; + } + lookup_identity(pool, &subject) + .await? + .ok_or_else(|| anyhow::anyhow!("identity transaction failed")) +} + +async fn lookup_identity(pool: &SqlitePool, subject: &str) -> anyhow::Result> { + Ok(sqlx::query_as("SELECT a.user_id,a.handle,a.display_name,a.avatar_url FROM accounts a JOIN external_identities e ON e.user_id=a.user_id WHERE e.provider='github.com' AND e.subject=?") + .bind(subject).fetch_optional(pool).await?) +} + +pub async fn create_session(pool: &SqlitePool, user_id: &str) -> anyhow::Result<(String, String)> { + let session = token(); + let csrf = token(); + sqlx::query("INSERT INTO web_sessions(token_hash,user_id,csrf_token,created_at,expires_at) VALUES(?,?,?,?,?)") + .bind(hash(&session)).bind(user_id).bind(&csrf).bind(now()).bind(now()+SESSION_TTL).execute(pool).await?; + Ok((session, csrf)) +} + +pub async fn get_session( + pool: &SqlitePool, + session: &str, +) -> anyhow::Result> { + #[derive(sqlx::FromRow)] + struct Row { + user_id: String, + handle: String, + display_name: String, + avatar_url: Option, + csrf_token: String, + } + let row: Option = sqlx::query_as("SELECT a.user_id,a.handle,a.display_name,a.avatar_url,s.csrf_token FROM web_sessions s JOIN accounts a ON a.user_id=s.user_id WHERE s.token_hash=? AND s.expires_at>?") + .bind(hash(session)).bind(now()).fetch_optional(pool).await?; + Ok(row.map(|r| { + ( + User { + user_id: r.user_id, + handle: r.handle, + display_name: r.display_name, + avatar_url: r.avatar_url, + }, + r.csrf_token, + ) + })) +} diff --git a/hub/src/config.rs b/hub/src/config.rs index e239a2e..80d77eb 100644 --- a/hub/src/config.rs +++ b/hub/src/config.rs @@ -9,6 +9,7 @@ pub struct Config { pub data_dir: PathBuf, pub origin: Url, pub smtp: Option, + pub github: Option, } /// SMTP credentials are only read from the service environment, never command arguments. @@ -72,7 +73,23 @@ impl Config { from: get("MAIL_FROM").unwrap_or_else(|| "Vnox ".into()), }) }; + let github = match (get("GITHUB_CLIENT_ID"), get("GITHUB_CLIENT_SECRET")) { + (None, None) => None, + (Some(id), Some(secret)) + if !id.is_empty() + && !secret.is_empty() + && !id.chars().any(char::is_whitespace) + && !secret.chars().any(char::is_whitespace) => + { + Some(crate::auth::GitHubConfig { + client_id: id, + client_secret: secret, + }) + } + _ => bail!("GITHUB_CLIENT_ID and GITHUB_CLIENT_SECRET must be configured together"), + }; Ok(Self { + github, bind, origin, smtp, diff --git a/hub/src/lib.rs b/hub/src/lib.rs index af74370..52374d9 100644 --- a/hub/src/lib.rs +++ b/hub/src/lib.rs @@ -1,3 +1,4 @@ +pub mod auth; pub mod config; pub mod database; pub mod identity; diff --git a/hub/src/main.rs b/hub/src/main.rs index d990fa1..567e385 100644 --- a/hub/src/main.rs +++ b/hub/src/main.rs @@ -47,6 +47,11 @@ async fn main() -> Result<()> { mail::Mailer::new(smtp)?; } let state = Arc::new(web::HubState { + auth: Arc::new(vnox_hub::auth::AuthService::new( + database.clone(), + config.origin.clone(), + config.github.as_ref(), + )?), database, issuer, smtp_configured: config.smtp.is_some(), diff --git a/hub/src/web.rs b/hub/src/web.rs index 453fbf0..04f16ed 100644 --- a/hub/src/web.rs +++ b/hub/src/web.rs @@ -16,6 +16,7 @@ use crate::identity::Issuer; pub struct HubState { pub database: SqlitePool, + pub auth: Arc, pub issuer: Issuer, pub smtp_configured: bool, pub started: Instant, @@ -23,6 +24,7 @@ pub struct HubState { pub fn router(state: Arc) -> Router { Router::new() + .merge(crate::auth::router()) .route("/", get(index)) .route("/health", get(health)) .route("/ready", get(ready)) @@ -74,7 +76,7 @@ async fn info(State(state): State>) -> Json { "smtp": {"configured": state.smtp_configured, "public_send_endpoint": false}, "capabilities": { "email_auth": "not_enabled", - "github_auth": "not_enabled", + "github_auth": if state.auth.configured() { "enabled" } else { "not_enabled" }, "server_registry": "not_enabled", "friends": "not_enabled", "direct_messages": "not_enabled", diff --git a/hub/tests/auth.rs b/hub/tests/auth.rs new file mode 100644 index 0000000..f91b2fd --- /dev/null +++ b/hub/tests/auth.rs @@ -0,0 +1,334 @@ +use axum::{ + body::{Body, to_bytes}, + http::{Request, StatusCode}, +}; +use oauth2::{CsrfToken, PkceCodeChallenge}; +use std::sync::Arc; +use tower::ServiceExt; +use vnox_hub::{ + auth::{ + self, + provider::{GitHubProfile, IdentityProvider, ProviderError}, + store, + }, + database, + identity::Issuer, + web::{self, HubState}, +}; +struct Fake; +impl IdentityProvider for Fake { + fn authorize(&self, state: CsrfToken, _: PkceCodeChallenge) -> url::Url { + let mut u = url::Url::parse("https://github.com/login/oauth/authorize").unwrap(); + u.query_pairs_mut().append_pair("state", state.secret()); + u + } + fn identify<'a>( + &'a self, + code: &'a str, + verifier: &'a str, + ) -> std::pin::Pin< + Box> + Send + 'a>, + > { + Box::pin(async move { + assert_eq!(code, "code"); + assert!(!verifier.is_empty()); + Ok(GitHubProfile { + id: 123, + login: "person".into(), + name: Some("Person".into()), + avatar_url: None, + }) + }) + } +} +async fn setup(enabled: bool) -> (tempfile::TempDir, Arc) { + let dir = tempfile::tempdir().unwrap(); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + std::fs::set_permissions(dir.path(), std::fs::Permissions::from_mode(0o700)).unwrap(); + } + let issuer = Issuer::load_or_create(dir.path()).unwrap(); + let pool = database::open(dir.path(), &issuer).await.unwrap(); + let auth = auth::AuthService::with_provider( + pool.clone(), + url::Url::parse("https://central.loki-code.dev").unwrap(), + if enabled { Some(Arc::new(Fake)) } else { None }, + ); + ( + dir, + Arc::new(HubState { + database: pool, + auth: Arc::new(auth), + issuer, + smtp_configured: false, + started: std::time::Instant::now(), + }), + ) +} +#[tokio::test] +async fn login_browser_binding_replay_session_and_csrf() { + let (_dir, state) = setup(true).await; + let app = web::router(state.clone()); + let response = app + .clone() + .oneshot( + Request::builder() + .uri("/v1/auth/github/start") + .body(Body::empty()) + .unwrap(), + ) + .await + .unwrap(); + assert_eq!(response.status(), StatusCode::SEE_OTHER); + let browser = response.headers()["set-cookie"] + .to_str() + .unwrap() + .split(';') + .next() + .unwrap() + .to_owned(); + assert!( + response.headers()["set-cookie"] + .to_str() + .unwrap() + .contains("HttpOnly") + ); + let redirect = url::Url::parse(response.headers()["location"].to_str().unwrap()).unwrap(); + let csrf = redirect + .query_pairs() + .find(|(k, _)| k == "state") + .unwrap() + .1 + .to_string(); + let callback = format!("/v1/auth/github/callback?code=code&state={csrf}"); + let rejected = app + .clone() + .oneshot( + Request::builder() + .uri(&callback) + .body(Body::empty()) + .unwrap(), + ) + .await + .unwrap(); + assert_eq!(rejected.status(), StatusCode::BAD_REQUEST); + let response = app + .clone() + .oneshot( + Request::builder() + .uri(&callback) + .header("cookie", &browser) + .body(Body::empty()) + .unwrap(), + ) + .await + .unwrap(); + assert_eq!(response.status(), StatusCode::SEE_OTHER); + assert_eq!( + response.headers()["location"], + "https://central.loki-code.dev/account" + ); + let session = response + .headers() + .get_all("set-cookie") + .iter() + .find(|v| v.to_str().unwrap().starts_with("__Host-vnox_session=")) + .unwrap() + .to_str() + .unwrap() + .split(';') + .next() + .unwrap() + .to_owned(); + let replay = app + .clone() + .oneshot( + Request::builder() + .uri(&callback) + .header("cookie", browser) + .body(Body::empty()) + .unwrap(), + ) + .await + .unwrap(); + assert_eq!(replay.status(), StatusCode::BAD_REQUEST); + let response = app + .clone() + .oneshot( + Request::builder() + .uri("/v1/auth/session") + .header("cookie", &session) + .body(Body::empty()) + .unwrap(), + ) + .await + .unwrap(); + let json: serde_json::Value = + serde_json::from_slice(&to_bytes(response.into_body(), 8192).await.unwrap()).unwrap(); + assert_eq!(json["authenticated"], true); + let rejected = app + .clone() + .oneshot( + Request::builder() + .method("POST") + .uri("/v1/auth/logout") + .header("cookie", &session) + .header("origin", "https://central.loki-code.dev") + .body(Body::empty()) + .unwrap(), + ) + .await + .unwrap(); + assert_eq!(rejected.status(), StatusCode::FORBIDDEN); + let ok = app + .clone() + .oneshot( + Request::builder() + .method("POST") + .uri("/v1/auth/logout") + .header("cookie", &session) + .header("origin", "https://central.loki-code.dev") + .header("x-csrf-token", json["csrf_token"].as_str().unwrap()) + .body(Body::empty()) + .unwrap(), + ) + .await + .unwrap(); + assert_eq!(ok.status(), StatusCode::NO_CONTENT); + assert_eq!( + sqlx::query_scalar::<_, i64>("SELECT COUNT(*) FROM web_sessions") + .fetch_one(&state.database) + .await + .unwrap(), + 0 + ); +} +#[tokio::test] +async fn expired_attempt_and_identity_stability() { + let (_dir, state) = setup(false).await; + store::create_attempt(&state.database, "state", "browser", "verifier") + .await + .unwrap(); + assert!( + store::consume_attempt(&state.database, "state", "wrong") + .await + .unwrap() + .is_none() + ); + sqlx::query("UPDATE oauth_attempts SET expires_at=0") + .execute(&state.database) + .await + .unwrap(); + assert!( + store::consume_attempt(&state.database, "state", "browser") + .await + .unwrap() + .is_none() + ); + let mut profile = GitHubProfile { + id: 987, + login: "old".into(), + name: None, + avatar_url: None, + }; + let a = store::upsert_identity(&state.database, &profile) + .await + .unwrap(); + profile.login = "changed".into(); + let b = store::upsert_identity(&state.database, &profile) + .await + .unwrap(); + assert_eq!(a.user_id, b.user_id); + let response = web::router(state) + .oneshot( + Request::builder() + .uri("/v1/auth/github/start") + .body(Body::empty()) + .unwrap(), + ) + .await + .unwrap(); + assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE); +} + +#[tokio::test] +async fn blocked_login_ip_cannot_spend_other_visitors_global_budget() { + let (_dir, state) = setup(false).await; + for _ in 0..20 { + assert!(state.auth.allow("192.0.2.1")); + } + for _ in 0..400 { + assert!(!state.auth.allow("192.0.2.1")); + } + assert!(state.auth.allow("192.0.2.2")); +} + +#[test] +fn production_provider_uses_fixed_callback_and_s256_without_repository_scopes() { + let origin = url::Url::parse("https://central.loki-code.dev").unwrap(); + let provider = auth::provider::GitHubProvider::new( + &auth::GitHubConfig { + client_id: "public-fixture-id".into(), + client_secret: "fixture-secret-not-real".into(), + }, + &origin, + ) + .unwrap(); + let (challenge, _) = PkceCodeChallenge::new_random_sha256(); + let target = provider.authorize(CsrfToken::new("fixture-state".into()), challenge); + assert_eq!(target.host_str(), Some("github.com")); + let pairs = target + .query_pairs() + .collect::>(); + assert_eq!( + pairs["redirect_uri"], + "https://central.loki-code.dev/v1/auth/github/callback" + ); + assert_eq!(pairs["code_challenge_method"], "S256"); + assert!(!pairs["code_challenge"].is_empty()); + assert_eq!(pairs["state"], "fixture-state"); + assert!(!pairs.contains_key("scope")); + assert!(!target.as_str().contains("fixture-secret-not-real")); +} + +#[tokio::test] +async fn persisted_session_is_hashed_and_expiry_is_enforced_without_cleanup() { + let (_dir, state) = setup(false).await; + let user = store::upsert_identity( + &state.database, + &GitHubProfile { + id: 456, + login: "fixture".into(), + name: None, + avatar_url: None, + }, + ) + .await + .unwrap(); + let (raw, _) = store::create_session(&state.database, &user.user_id) + .await + .unwrap(); + let persisted: String = sqlx::query_scalar("SELECT token_hash FROM web_sessions") + .fetch_one(&state.database) + .await + .unwrap(); + assert_eq!(persisted, store::hash(&raw)); + assert_ne!(persisted, raw); + assert!( + store::get_session(&state.database, &raw) + .await + .unwrap() + .is_some() + ); + sqlx::query("UPDATE web_sessions SET expires_at=0") + .execute(&state.database) + .await + .unwrap(); + assert!( + store::get_session(&state.database, &raw) + .await + .unwrap() + .is_none() + ); +} diff --git a/hub/tests/foundation.rs b/hub/tests/foundation.rs index 4d6c204..44f0556 100644 --- a/hub/tests/foundation.rs +++ b/hub/tests/foundation.rs @@ -96,6 +96,14 @@ async fn state() -> (TempDir, Arc) { ( directory, Arc::new(HubState { + auth: Arc::new( + vnox_hub::auth::AuthService::new( + pool.clone(), + url::Url::parse("https://central.loki-code.dev").unwrap(), + None, + ) + .unwrap(), + ), database: pool, issuer, smtp_configured: true, diff --git a/protocol/lnex.proto b/protocol/lnex.proto index 0bc4419..a74bd2e 100644 --- a/protocol/lnex.proto +++ b/protocol/lnex.proto @@ -393,3 +393,26 @@ message E2eeDmHistory { repeated E2eeDmMessage messages = 2; optional int64 limit = 3; } + +// Channel-scoped webhook management: authenticated LNEx only. Secrets returned once. +message WebhookRequest { + string id = 1; + string channel_id = 2; + string name = 3; + string direction = 4; // incoming | outgoing + string provider = 5; // generic | github (incoming only) + string endpoint = 6; // outgoing HTTPS URL +} +message WebhookInfo { + string id = 1; + string channel_id = 2; + string name = 3; + string direction = 4; + string provider = 5; + string endpoint = 6; + int64 created_at = 7; + bool revoked = 8; + string suspended_reason = 9; +} +message WebhookResult { WebhookInfo hook = 1; string secret = 2; } +message WebhookListResult { repeated WebhookInfo hooks = 1; }