Skip to main content

agentmux_srv_core/backend/jobs/
webhook_local.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3//
4//! A job on the `webhook_local` source starts a run when a script on this computer POSTs to
5//! `http://127.0.0.1:<port>/agentmux/hook/<job id>` with the job's secret
6//! (docs/specs/SPEC_DRONE_REAL_WORLD_JOBS_2026_11_11.md ยง3.2; the route is `server::hook_handlers`).
7//!
8//! The job's filter holds one thing: `secret_sha256`, the SHA-256 of the secret, as 64 lowercase hex characters
9//! (`printf %s "$SECRET" | shasum -a 256`). The secret itself is never stored: whoever writes the hook's caller
10//! (a git hook, a CI script) keeps it, and a job definition, which agents and the Jobs list can read, holds only the hash.
11//! A secret should be random (`openssl rand -hex 32`); a guessable one makes its hash guessable.
12//!
13//! The run reads the request as `{{trigger.body}}` (the JSON object the script sent) and `{{trigger.received_ms}}`.
14//! The body sits under its own key so a script's own field names can never be read as the event's.
15
16use ring::constant_time::verify_slices_are_equal;
17use serde_json::{Map, Value};
18use sha2::{Digest, Sha256};
19
20use super::definition::{event_source, trigger_kind};
21use crate::backend::storage::cron::CronJob;
22
23/// The header that carries the secret: never the URL, a query or the body, so a cross-site form cannot send it.
24pub const SECRET_HEADER: &str = "x-agentmux-hook-secret";
25/// An optional header naming the call, so a script that retries a call it did not hear back from starts one run.
26pub const ID_HEADER: &str = "x-agentmux-hook-id";
27/// The most a request body may be.
28pub const MAX_BODY: usize = 64 * 1024;
29/// The longest a call id may be.
30pub const MAX_ID_CHARS: usize = 200;
31
32const KEY: &str = "secret_sha256";
33
34/// A `webhook_local` trigger's filter: the secret's hash, and nothing else.
35pub fn validate_filter(filter: &Map<String, Value>) -> Result<(), String> {
36    if let Some(key) = filter.keys().find(|k| k.as_str() != KEY) {
37        return Err(format!("trigger: webhook_local has no filter key {key:?}; it has only {KEY}"));
38    }
39    let hash = filter.get(KEY).and_then(Value::as_str).unwrap_or("");
40    if hash.len() != 64 || !hash.bytes().all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)) {
41        return Err(format!("trigger: a webhook_local filter has {KEY}, the SHA-256 of the secret as 64 lowercase hex characters"));
42    }
43    Ok(())
44}
45
46/// The hash a job keeps for a secret.
47pub fn hash_of(secret: &str) -> String {
48    hex::encode(Sha256::digest(secret.as_bytes()))
49}
50
51/// Whether `job` is an enabled event job on `webhook_local`, and `secret` is its secret. The hashes are compared in
52/// constant time. A job that is not one, or is off, never matches: the caller answers all of those the same way.
53pub fn accepts(job: &CronJob, secret: &str) -> bool {
54    if !job.enabled || job.trigger_kind != trigger_kind::EVENT {
55        return false;
56    }
57    let t = &job.trigger_event;
58    if t.get("source").and_then(Value::as_str) != Some(event_source::WEBHOOK_LOCAL) {
59        return false;
60    }
61    let Some(want) = t.get("filter").and_then(|f| f.get(KEY)).and_then(Value::as_str) else { return false };
62    verify_slices_are_equal(hash_of(secret).as_bytes(), want.as_bytes()).is_ok()
63}