/* * wh_store.h - the named-hook store: definitions, secrets, delivery log. * * One JSON document on disk, written atomically at mode 0600, in * * /workspace/.scorpiox/connectors/plugin_webhook/hooks.json (when /workspace exists) * ~/.scorpiox/connectors/plugin_webhook/hooks.json (elsewhere) * * or wherever WEBHOOK_STORE_PATH points (the offline suite uses that). * * Secrets never leave this module in a readable form. Every serializer that * faces the browser or an MCP client emits, for each header, only its NAME and * `has_secret: true`; for the HMAC it emits the scheme and `has_secret: true`. * A hook definition read back from disk is the only place a value exists, and * it is used for exactly one thing: composing the outbound request. * * MIT License. Copyright (c) 2026 SCORPIOX INC. */ #ifndef SX_WH_STORE_H #define SX_WH_STORE_H #include #include "wh_http.h" #define WH_MAX_HOOKS 64 #define WH_MAX_HEADERS 16 #define WH_LOG_MAX 50 #define WH_NAME_MAX 64 #define WH_DESC_MAX 200 #define WH_SECRET_MAX 256 #define WH_HDRVAL_MAX 1024 /* HMAC schemes. Body-only signatures are not offered: they never expire, so a * captured request can be replayed forever. */ #define WH_SIG_NONE "" #define WH_SIG_GITHUB "github" /* X-Hub-Signature-256: sha256= */ #define WH_SIG_GITLAB "gitlab" /* X-Gitlab-Token: (shared token) */ #define WH_SIG_V2 "v2" /* X-Webhook-Signature: t=,v1= */ typedef struct { char name[WH_NAME_MAX]; char url[WH_URL_MAX]; char method[8]; char desc[WH_DESC_MAX]; wh_header headers[WH_MAX_HEADERS]; /* values are secrets */ int nheaders; char sig[16]; /* WH_SIG_* */ char secret[WH_SECRET_MAX]; /* a secret: never serialized out */ } wh_hook; typedef struct { char at[24]; /* ISO-8601 UTC */ char name[WH_NAME_MAX]; int status; int ok; long bytes; long ms; char error[192]; } wh_logent; typedef struct { wh_hook hooks[WH_MAX_HOOKS]; int nhooks; wh_logent log[WH_LOG_MAX]; int nlog; } wh_store; /* Path of the store file (created parents not included). Always writes. */ void wh_store_path(char *out, size_t outsz); /* Load the document. A missing file is an empty store (returns 0). A file that * exists but cannot be read costs a diagnostic in `err` and an empty store, so * the UI can say why the list is empty instead of pretending there are none. */ int wh_store_load(wh_store *s, char *err, size_t errsz); /* Write atomically (tmp + rename) at mode 0600. Returns 0 on success. */ int wh_store_save(const wh_store *s, char *err, size_t errsz); /* Find a hook by name (exact). NULL when absent. */ wh_hook *wh_hook_find(wh_store *s, const char *name); /* Remove hook at index, shifting the rest. */ void wh_hook_remove(wh_store *s, int idx); /* Append to the delivery log, newest first, dropping the oldest beyond * WH_LOG_MAX. */ void wh_store_log_add(wh_store *s, const wh_logent *e); /* ---- serializers ------------------------------------------------------- * `redacted` emits names + has_secret only (browser / MCP). Never pass a * secret-bearing document to a caller that writes it to a response. */ void wh_hook_json(const wh_hook *h, int redacted, char *out, size_t outsz); void wh_store_json(const wh_store *s, int redacted, char *out, size_t outsz); /* "hooks" array + "log" array + counts, the shape every read route returns. */ void wh_store_list_json(const wh_store *s, char *out, size_t outsz); /* Apply a JSON object (as sent by the UI or an MCP call) onto `h`, keeping the * stored secret for any header whose value is absent, and keeping the stored * HMAC secret when the incoming secret is empty. Returns 0 on success or -1 * with a human-readable reason. */ int wh_hook_apply(wh_hook *h, const char *json, const wh_hook *existing, char *err, size_t errsz); /* Validate a hook (URL shape, method, name, header names). Returns 0 or -1. */ int wh_hook_validate(const wh_hook *h, char *err, size_t errsz); #endif /* SX_WH_STORE_H */