feat: add GitHub sign-in and channel webhooks
This commit is contained in:
parent
45e89f9826
commit
ff9c8f330e
36 changed files with 3240 additions and 24 deletions
727
Cargo.lock
generated
727
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -9,3 +9,7 @@ SMTP_USER=replace-on-server
|
|||
SMTP_PASSWORD=replace-on-server
|
||||
MAIL_FROM="Vnox <noreply@loki-code.dev>"
|
||||
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
|
||||
|
|
|
|||
21
docs/design/webhook-api.md
Normal file
21
docs/design/webhook-api.md
Normal file
|
|
@ -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 <secret>` 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:<id>` 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=<HMAC(timestamp+'.'+raw_body)>`, `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.
|
||||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -26,12 +26,13 @@ pub struct AdminState {
|
|||
pub channels_count: std::sync::Arc<std::sync::atomic::AtomicUsize>,
|
||||
}
|
||||
|
||||
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,
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
225
gateway/src/domain/storage/webhooks/mod.rs
Normal file
225
gateway/src/domain/storage/webhooks/mod.rs
Normal file
|
|
@ -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<u8>,
|
||||
pub created_at: i64,
|
||||
pub revoked_at: Option<i64>,
|
||||
pub suspended_reason: Option<String>,
|
||||
}
|
||||
#[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<Option<(String, String, i64)>> {
|
||||
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<Option<Hook>> {
|
||||
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<Vec<Hook>> {
|
||||
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::<u8>::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<bool> {
|
||||
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::<String>(),"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::<String>(),"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<Vec<Delivery>> {
|
||||
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(())
|
||||
}
|
||||
}
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -21,6 +21,12 @@ pub async fn dispatch<S: AsyncRead + AsyncWrite + Unpin>(
|
|||
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(
|
||||
|
|
|
|||
|
|
@ -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<config::Config>, voice_member_tx: Option<VoiceMemberTx
|
|||
voice_member_tx,
|
||||
);
|
||||
|
||||
state
|
||||
.storage
|
||||
.load_channels_to_cache(&state.channels)
|
||||
.await?;
|
||||
state.webhooks.configure_from_env()?;
|
||||
tokio::spawn(webhooks::outgoing::run(state.clone()));
|
||||
|
||||
if let Ok(rows) = state.storage.load_all_presences().await {
|
||||
let mut presences = state.presences.write().await;
|
||||
for row in rows {
|
||||
|
|
@ -118,7 +126,7 @@ pub async fn run(cfg: Arc<config::Config>, voice_member_tx: Option<VoiceMemberTx
|
|||
sessions_count,
|
||||
channels_count,
|
||||
};
|
||||
tokio::spawn(admin::run(admin_bind, admin_state));
|
||||
tokio::spawn(admin::run(admin_bind, admin_state, state.clone()));
|
||||
|
||||
let listener = TcpListener::bind(&cfg.gateway.bind).await?;
|
||||
info!("listening on {}", cfg.gateway.bind);
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ pub type VoiceMemberTx = tokio::sync::broadcast::Sender<String>;
|
|||
|
||||
#[derive(Clone)]
|
||||
pub struct State {
|
||||
pub webhooks: Arc<crate::webhooks::Runtime>,
|
||||
pub sessions: SessionStore,
|
||||
pub channels: ChannelStore,
|
||||
pub storage: Arc<Storage>,
|
||||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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<Self> {
|
||||
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),
|
||||
|
|
|
|||
178
gateway/src/webhooks/incoming.rs
Normal file
178
gateway/src/webhooks/incoming.rs
Normal file
|
|
@ -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<bool, StatusCode> {
|
||||
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<Gateway>,
|
||||
Path(id): Path<String>,
|
||||
headers: HeaderMap,
|
||||
body: Bytes,
|
||||
) -> Result<(StatusCode, Json<serde_json::Value>), 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})),
|
||||
))
|
||||
}
|
||||
189
gateway/src/webhooks/management.rs
Normal file
189
gateway/src/webhooks/management.rs
Normal file
|
|
@ -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<Option<String>> {
|
||||
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<S: AsyncRead + AsyncWrite + Unpin>(
|
||||
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<S: AsyncRead + AsyncWrite + Unpin>(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
|
||||
}
|
||||
100
gateway/src/webhooks/mod.rs
Normal file
100
gateway/src/webhooks/mod.rs
Normal file
|
|
@ -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<HashMap<String, (f64, Instant)>>,
|
||||
}
|
||||
|
||||
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;
|
||||
81
gateway/src/webhooks/outgoing.rs
Normal file
81
gateway/src/webhooks/outgoing.rs
Normal file
|
|
@ -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(())
|
||||
}
|
||||
121
gateway/src/webhooks/policy.rs
Normal file
121
gateway/src/webhooks/policy.rs
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
use anyhow::{Result, bail};
|
||||
use std::net::{IpAddr, SocketAddr};
|
||||
use url::Url;
|
||||
|
||||
pub fn endpoint(value: &str) -> Result<Url> {
|
||||
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::<IpAddr>()
|
||||
&& !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<Vec<SocketAddr>> {
|
||||
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::<Vec<_>>();
|
||||
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());
|
||||
}
|
||||
}
|
||||
93
gateway/src/webhooks/secrets.rs
Normal file
93
gateway/src/webhooks/secrets.rs
Normal file
|
|
@ -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<Vec<u8>> {
|
||||
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<String> {
|
||||
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 = <Hmac<Sha256> 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) = <Hmac<Sha256> 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());
|
||||
}
|
||||
}
|
||||
436
gateway/src/webhooks/tests.rs
Normal file
436
gateway/src/webhooks/tests.rs
Normal file
|
|
@ -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");
|
||||
}
|
||||
26
hub/AUTH.md
Normal file
26
hub/AUTH.md
Normal file
|
|
@ -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.
|
||||
|
|
@ -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"] }
|
||||
|
|
|
|||
24
hub/migrations/0002_github_auth.sql
Normal file
24
hub/migrations/0002_github_auth.sql
Normal file
|
|
@ -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);
|
||||
77
hub/src/auth/mod.rs
Normal file
77
hub/src/auth/mod.rs
Normal file
|
|
@ -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<Arc<dyn IdentityProvider>>,
|
||||
limits: Mutex<HashMap<String, (i64, u32)>>,
|
||||
}
|
||||
|
||||
impl AuthService {
|
||||
pub fn new(
|
||||
pool: SqlitePool,
|
||||
origin: Url,
|
||||
config: Option<&GitHubConfig>,
|
||||
) -> anyhow::Result<Self> {
|
||||
let provider = config
|
||||
.map(|config| {
|
||||
GitHubProvider::new(config, &origin)
|
||||
.map(|provider| Arc::new(provider) as Arc<dyn IdentityProvider>)
|
||||
})
|
||||
.transpose()?;
|
||||
Ok(Self::with_provider(pool, origin, provider))
|
||||
}
|
||||
pub fn with_provider(
|
||||
pool: SqlitePool,
|
||||
origin: Url,
|
||||
provider: Option<Arc<dyn IdentityProvider>>,
|
||||
) -> 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
|
||||
}
|
||||
}
|
||||
155
hub/src/auth/provider.rs
Normal file
155
hub/src/auth/provider.rs
Normal file
|
|
@ -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<String>,
|
||||
pub avatar_url: Option<String>,
|
||||
}
|
||||
|
||||
#[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<Box<dyn Future<Output = Result<GitHubProfile, ProviderError>> + Send + 'a>>;
|
||||
}
|
||||
|
||||
type Client = BasicClient<EndpointSet, EndpointNotSet, EndpointNotSet, EndpointNotSet, EndpointSet>;
|
||||
pub struct GitHubProvider {
|
||||
oauth: Client,
|
||||
http: reqwest::Client,
|
||||
}
|
||||
|
||||
impl GitHubProvider {
|
||||
pub fn new(config: &GitHubConfig, origin: &Url) -> anyhow::Result<Self> {
|
||||
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<Box<dyn Future<Output = Result<GitHubProfile, ProviderError>> + 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<Vec<u8>, 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)
|
||||
}
|
||||
186
hub/src/auth/routes.rs
Normal file
186
hub/src/auth/routes.rs
Normal file
|
|
@ -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<Arc<HubState>> {
|
||||
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<String> {
|
||||
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::<std::net::IpAddr>().ok())
|
||||
.map(|v| v.to_string())
|
||||
.unwrap_or_else(|| "local".into());
|
||||
auth.allow(&ip)
|
||||
}
|
||||
async fn start(State(state): State<Arc<HubState>>, 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<String>,
|
||||
state: Option<String>,
|
||||
}
|
||||
async fn callback(
|
||||
State(state): State<Arc<HubState>>,
|
||||
headers: HeaderMap,
|
||||
Query(query): Query<Callback>,
|
||||
) -> 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<Arc<HubState>>, 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<Arc<HubState>>, 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
|
||||
}
|
||||
156
hub/src/auth/store.rs
Normal file
156
hub/src/auth/store.rs
Normal file
|
|
@ -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<String>,
|
||||
}
|
||||
|
||||
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<Option<String>> {
|
||||
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<User> {
|
||||
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::<String>();
|
||||
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<Option<User>> {
|
||||
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<Option<(User, String)>> {
|
||||
#[derive(sqlx::FromRow)]
|
||||
struct Row {
|
||||
user_id: String,
|
||||
handle: String,
|
||||
display_name: String,
|
||||
avatar_url: Option<String>,
|
||||
csrf_token: String,
|
||||
}
|
||||
let row: Option<Row> = 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,
|
||||
)
|
||||
}))
|
||||
}
|
||||
|
|
@ -9,6 +9,7 @@ pub struct Config {
|
|||
pub data_dir: PathBuf,
|
||||
pub origin: Url,
|
||||
pub smtp: Option<SmtpConfig>,
|
||||
pub github: Option<crate::auth::GitHubConfig>,
|
||||
}
|
||||
|
||||
/// 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 <noreply@loki-code.dev>".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,
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
pub mod auth;
|
||||
pub mod config;
|
||||
pub mod database;
|
||||
pub mod identity;
|
||||
|
|
|
|||
|
|
@ -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(),
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ use crate::identity::Issuer;
|
|||
|
||||
pub struct HubState {
|
||||
pub database: SqlitePool,
|
||||
pub auth: Arc<crate::auth::AuthService>,
|
||||
pub issuer: Issuer,
|
||||
pub smtp_configured: bool,
|
||||
pub started: Instant,
|
||||
|
|
@ -23,6 +24,7 @@ pub struct HubState {
|
|||
|
||||
pub fn router(state: Arc<HubState>) -> 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<Arc<HubState>>) -> Json<Value> {
|
|||
"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",
|
||||
|
|
|
|||
334
hub/tests/auth.rs
Normal file
334
hub/tests/auth.rs
Normal file
|
|
@ -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<dyn std::future::Future<Output = Result<GitHubProfile, ProviderError>> + 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<HubState>) {
|
||||
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::<std::collections::HashMap<_, _>>();
|
||||
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()
|
||||
);
|
||||
}
|
||||
|
|
@ -96,6 +96,14 @@ async fn state() -> (TempDir, Arc<HubState>) {
|
|||
(
|
||||
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,
|
||||
|
|
|
|||
|
|
@ -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; }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue