scorpiox-bot-plugin-webhook
←wh_store.hc
108 lines · 4 KBHEADBlameDownload
1/*
2 * wh_store.h - the named-hook store: definitions, secrets, delivery log.
3 *
4 * One JSON document on disk, written atomically at mode 0600, in
5 *
6 * /workspace/.scorpiox/connectors/plugin_webhook/hooks.json (when /workspace exists)
7 * ~/.scorpiox/connectors/plugin_webhook/hooks.json (elsewhere)
8 *
9 * or wherever WEBHOOK_STORE_PATH points (the offline suite uses that).
10 *
11 * Secrets never leave this module in a readable form. Every serializer that
12 * faces the browser or an MCP client emits, for each header, only its NAME and
13 * `has_secret: true`; for the HMAC it emits the scheme and `has_secret: true`.
14 * A hook definition read back from disk is the only place a value exists, and
15 * it is used for exactly one thing: composing the outbound request.
16 *
17 * MIT License. Copyright (c) 2026 SCORPIOX INC.
18 */
19#ifndef SX_WH_STORE_H
20#define SX_WH_STORE_H
21
22#include <stddef.h>
23
24#include "wh_http.h"
25
26#define WH_MAX_HOOKS 64
27#define WH_MAX_HEADERS 16
28#define WH_LOG_MAX 50
29#define WH_NAME_MAX 64
30#define WH_DESC_MAX 200
31#define WH_SECRET_MAX 256
32#define WH_HDRVAL_MAX 1024
33
34/* HMAC schemes. Body-only signatures are not offered: they never expire, so a
35 * captured request can be replayed forever. */
36#define WH_SIG_NONE ""
37#define WH_SIG_GITHUB "github" /* X-Hub-Signature-256: sha256=<hex(body)> */
38#define WH_SIG_GITLAB "gitlab" /* X-Gitlab-Token: <secret> (shared token) */
39#define WH_SIG_V2 "v2" /* X-Webhook-Signature: t=<ts>,v1=<hex(ts|.|body)> */
40
41typedef struct {
42 char name[WH_NAME_MAX];
43 char url[WH_URL_MAX];
44 char method[8];
45 char desc[WH_DESC_MAX];
46 wh_header headers[WH_MAX_HEADERS]; /* values are secrets */
47 int nheaders;
48 char sig[16]; /* WH_SIG_* */
49 char secret[WH_SECRET_MAX]; /* a secret: never serialized out */
50} wh_hook;
51
52typedef struct {
53 char at[24]; /* ISO-8601 UTC */
54 char name[WH_NAME_MAX];
55 int status;
56 int ok;
57 long bytes;
58 long ms;
59 char error[192];
60} wh_logent;
61
62typedef struct {
63 wh_hook hooks[WH_MAX_HOOKS];
64 int nhooks;
65 wh_logent log[WH_LOG_MAX];
66 int nlog;
67} wh_store;
68
69/* Path of the store file (created parents not included). Always writes. */
70void wh_store_path(char *out, size_t outsz);
71
72/* Load the document. A missing file is an empty store (returns 0). A file that
73 * exists but cannot be read costs a diagnostic in `err` and an empty store, so
74 * the UI can say why the list is empty instead of pretending there are none. */
75int wh_store_load(wh_store *s, char *err, size_t errsz);
76
77/* Write atomically (tmp + rename) at mode 0600. Returns 0 on success. */
78int wh_store_save(const wh_store *s, char *err, size_t errsz);
79
80/* Find a hook by name (exact). NULL when absent. */
81wh_hook *wh_hook_find(wh_store *s, const char *name);
82
83/* Remove hook at index, shifting the rest. */
84void wh_hook_remove(wh_store *s, int idx);
85
86/* Append to the delivery log, newest first, dropping the oldest beyond
87 * WH_LOG_MAX. */
88void wh_store_log_add(wh_store *s, const wh_logent *e);
89
90/* ---- serializers -------------------------------------------------------
91 * `redacted` emits names + has_secret only (browser / MCP). Never pass a
92 * secret-bearing document to a caller that writes it to a response. */
93void wh_hook_json(const wh_hook *h, int redacted, char *out, size_t outsz);
94void wh_store_json(const wh_store *s, int redacted, char *out, size_t outsz);
95/* "hooks" array + "log" array + counts, the shape every read route returns. */
96void wh_store_list_json(const wh_store *s, char *out, size_t outsz);
97
98/* Apply a JSON object (as sent by the UI or an MCP call) onto `h`, keeping the
99 * stored secret for any header whose value is absent, and keeping the stored
100 * HMAC secret when the incoming secret is empty. Returns 0 on success or -1
101 * with a human-readable reason. */
102int wh_hook_apply(wh_hook *h, const char *json, const wh_hook *existing,
103 char *err, size_t errsz);
104
105/* Validate a hook (URL shape, method, name, header names). Returns 0 or -1. */
106int wh_hook_validate(const wh_hook *h, char *err, size_t errsz);
107
108#endif /* SX_WH_STORE_H */