/* * wh_store.c - named hooks, their secrets, and the delivery log. See wh_store.h. * * The store file is the only place a secret exists on disk, and it is written * at mode 0600 through a temp file plus rename() so a crash mid-write leaves * the previous document intact rather than a truncated one. * * MIT License. Copyright (c) 2026 SCORPIOX INC. */ #define _POSIX_C_SOURCE 200809L #if defined(__APPLE__) && !defined(_DARWIN_C_SOURCE) #define _DARWIN_C_SOURCE 1 #endif #include "wh_store.h" #include #include #include #include #include #include #if defined(_WIN32) # include # include # include # include #else # include # include # include #endif #include "wh_buf.h" #include "yyjson.h" /* ------------------------------------------------------------------- paths */ static void mkdir_p(const char *path) { char tmp[1024]; size_t i, n; n = strlen(path); if (n == 0 || n >= sizeof tmp) return; memcpy(tmp, path, n + 1); for (i = 1; i < n; i++) { if (tmp[i] != '/' && tmp[i] != '\\') continue; tmp[i] = '\0'; #if defined(_WIN32) _mkdir(tmp); #else mkdir(tmp, 0700); #endif tmp[i] = '/'; } #if defined(_WIN32) _mkdir(tmp); #else mkdir(tmp, 0700); #endif } void wh_store_path(char *out, size_t outsz) { const char *env = getenv("WEBHOOK_STORE_PATH"); const char *base = NULL; if (outsz == 0) return; out[0] = '\0'; if (env && *env) { snprintf(out, outsz, "%s", env); return; } #if defined(_WIN32) base = getenv("LOCALAPPDATA"); if (base && *base) { snprintf(out, outsz, "%s\\scorpiox\\connectors\\plugin_webhook\\hooks.json", base); return; } base = getenv("USERPROFILE"); if (base && *base) { snprintf(out, outsz, "%s\\.scorpiox\\connectors\\plugin_webhook\\hooks.json", base); return; } snprintf(out, outsz, "hooks.json"); #else { struct stat st; if (stat("/workspace", &st) == 0 && S_ISDIR(st.st_mode)) snprintf(out, outsz, "/workspace/.scorpiox/connectors/plugin_webhook/hooks.json"); else { base = getenv("HOME"); if (base && *base) snprintf(out, outsz, "%s/.scorpiox/connectors/plugin_webhook/hooks.json", base); else snprintf(out, outsz, "/tmp/.scorpiox/connectors/plugin_webhook/hooks.json"); } } #endif } /* --------------------------------------------------------------- load/save */ static void copy_trunc(char *dst, size_t capsz, const char *src) { size_t n; if (!capsz) return; if (!src) { dst[0] = '\0'; return; } n = strlen(src); if (n >= capsz) n = capsz - 1; memcpy(dst, src, n); dst[n] = '\0'; } static const char *jstr(yyjson_val *o, const char *key) { yyjson_val *v; if (!o) return NULL; v = yyjson_obj_get(o, key); if (!v || !yyjson_is_str(v)) return NULL; return yyjson_get_str(v); } int wh_store_load(wh_store *s, char *err, size_t errsz) { FILE *f; char path[1024]; char *text; long sz; size_t got; yyjson_doc *doc; yyjson_val *root, *hooks, *log; memset(s, 0, sizeof *s); if (err && errsz) err[0] = '\0'; wh_store_path(path, sizeof path); f = fopen(path, "rb"); if (!f) return 0; /* no file yet: empty store */ if (fseek(f, 0, SEEK_END) != 0) { fclose(f); return 0; } sz = ftell(f); if (sz < 0 || sz > (long)(4 * 1024 * 1024)) { fclose(f); if (err) snprintf(err, errsz, "store file is unreadable or too large"); return -1; } rewind(f); text = (char *)malloc((size_t)sz + 1); if (!text) { fclose(f); if (err) snprintf(err, errsz, "out of memory"); return -1; } got = fread(text, 1, (size_t)sz, f); fclose(f); text[got] = '\0'; doc = yyjson_read(text, got, 0); free(text); if (!doc) { /* A corrupt store is reported, never silently replaced: the operator * may still be able to recover the secrets from the file. */ if (err) snprintf(err, errsz, "the hook store exists but is not valid JSON (%s); " "it was left untouched", path); return -1; } root = yyjson_doc_get_root(doc); hooks = yyjson_obj_get(root, "hooks"); if (hooks && yyjson_is_arr(hooks)) { size_t i, n = yyjson_arr_size(hooks); for (i = 0; i < n && s->nhooks < WH_MAX_HOOKS; i++) { yyjson_val *o = yyjson_arr_get(hooks, i); yyjson_val *hdrs; wh_hook *h = &s->hooks[s->nhooks]; size_t k, hn; if (!yyjson_is_obj(o)) continue; copy_trunc(h->name, sizeof h->name, jstr(o, "name")); copy_trunc(h->url, sizeof h->url, jstr(o, "url")); copy_trunc(h->method, sizeof h->method, jstr(o, "method")); copy_trunc(h->desc, sizeof h->desc, jstr(o, "desc")); copy_trunc(h->sig, sizeof h->sig, jstr(o, "sig")); copy_trunc(h->secret, sizeof h->secret, jstr(o, "secret")); if (!h->method[0]) copy_trunc(h->method, sizeof h->method, "POST"); hdrs = yyjson_obj_get(o, "headers"); hn = 0; if (hdrs && yyjson_is_arr(hdrs)) { size_t m = yyjson_arr_size(hdrs); for (k = 0; k < m && hn < WH_MAX_HEADERS; k++) { yyjson_val *e = yyjson_arr_get(hdrs, k); const char *nm = jstr(e, "name"); const char *val = jstr(e, "value"); if (!nm || !*nm) continue; copy_trunc(h->headers[hn].name, sizeof h->headers[hn].name, nm); copy_trunc(h->headers[hn].value, sizeof h->headers[hn].value, val ? val : ""); hn++; } } h->nheaders = (int)hn; if (!h->name[0]) continue; s->nhooks++; } } log = yyjson_obj_get(root, "log"); if (log && yyjson_is_arr(log)) { size_t i, n = yyjson_arr_size(log); for (i = 0; i < n && s->nlog < WH_LOG_MAX; i++) { yyjson_val *o = yyjson_arr_get(log, i); wh_logent *e = &s->log[s->nlog]; if (!yyjson_is_obj(o)) continue; copy_trunc(e->at, sizeof e->at, jstr(o, "at")); copy_trunc(e->name, sizeof e->name, jstr(o, "name")); copy_trunc(e->error, sizeof e->error, jstr(o, "error")); { yyjson_val *v = yyjson_obj_get(o, "status"); e->status = (v && yyjson_is_int(v)) ? (int)yyjson_get_sint(v) : 0; v = yyjson_obj_get(o, "ok"); e->ok = (v && yyjson_is_bool(v)) ? (yyjson_get_bool(v) ? 1 : 0) : 0; v = yyjson_obj_get(o, "bytes"); e->bytes = (v && yyjson_is_int(v)) ? (long)yyjson_get_sint(v) : 0; v = yyjson_obj_get(o, "ms"); e->ms = (v && yyjson_is_int(v)) ? (long)yyjson_get_sint(v) : 0; } if (!e->name[0]) continue; s->nlog++; } } yyjson_doc_free(doc); return 0; } static void set_mode_0600(const char *path) { #if defined(_WIN32) (void)path; /* NTFS ACLs are inherited from the profile dir */ #else chmod(path, 0600); #endif } int wh_store_save(const wh_store *s, char *err, size_t errsz) { char path[1024], tmp[1100], dir[1024]; wh_buf b; FILE *f; int i, first; char *slash; if (err && errsz) err[0] = '\0'; wh_store_path(path, sizeof path); copy_trunc(dir, sizeof dir, path); slash = strrchr(dir, '/'); #if defined(_WIN32) if (!slash) slash = strrchr(dir, '\\'); #endif if (slash) { *slash = '\0'; mkdir_p(dir); } wh_buf_init(&b); wh_buf_str(&b, "{\"version\":1,\"hooks\":["); first = 1; for (i = 0; i < s->nhooks; i++) { const wh_hook *h = &s->hooks[i]; int f2 = 1, k; if (!first) wh_buf_str(&b, ","); first = 0; wh_buf_str(&b, "{"); wh_buf_key(&b, "name", &f2); wh_buf_json_str(&b, h->name); wh_buf_key(&b, "url", &f2); wh_buf_json_str(&b, h->url); wh_buf_key(&b, "method", &f2); wh_buf_json_str(&b, h->method); wh_buf_key(&b, "desc", &f2); wh_buf_json_str(&b, h->desc); wh_buf_key(&b, "sig", &f2); wh_buf_json_str(&b, h->sig); wh_buf_key(&b, "secret", &f2); wh_buf_json_str(&b, h->secret); wh_buf_key(&b, "headers", &f2); wh_buf_str(&b, "["); for (k = 0; k < h->nheaders; k++) { int f3 = 1; if (k) wh_buf_str(&b, ","); wh_buf_str(&b, "{"); wh_buf_key(&b, "name", &f3); wh_buf_json_str(&b, h->headers[k].name); wh_buf_key(&b, "value", &f3); wh_buf_json_str(&b, h->headers[k].value); wh_buf_str(&b, "}"); } wh_buf_str(&b, "]}"); } wh_buf_str(&b, "],\"log\":["); for (i = 0; i < s->nlog; i++) { const wh_logent *e = &s->log[i]; int f2 = 1; if (i) wh_buf_str(&b, ","); wh_buf_str(&b, "{"); wh_buf_key(&b, "at", &f2); wh_buf_json_str(&b, e->at); wh_buf_key(&b, "name", &f2); wh_buf_json_str(&b, e->name); wh_buf_key(&b, "status", &f2); wh_buf_printf(&b, "%d", e->status); wh_buf_key(&b, "ok", &f2); wh_buf_str(&b, e->ok ? "true" : "false"); wh_buf_key(&b, "bytes", &f2); wh_buf_printf(&b, "%ld", e->bytes); wh_buf_key(&b, "ms", &f2); wh_buf_printf(&b, "%ld", e->ms); wh_buf_key(&b, "error", &f2); wh_buf_json_str(&b, e->error); wh_buf_str(&b, "}"); } wh_buf_str(&b, "]}"); if (b.oom) { wh_buf_free(&b); if (err) snprintf(err, errsz, "out of memory"); return -1; } snprintf(tmp, sizeof tmp, "%s.tmp", path); f = fopen(tmp, "wb"); if (!f) { wh_buf_free(&b); if (err) snprintf(err, errsz, "cannot create %s: %s", tmp, strerror(errno)); return -1; } if (fwrite(wh_buf_cstr(&b), 1, b.n, f) != b.n) { fclose(f); remove(tmp); wh_buf_free(&b); if (err) snprintf(err, errsz, "short write to %s", tmp); return -1; } fflush(f); fclose(f); set_mode_0600(tmp); #if defined(_WIN32) remove(path); #endif if (rename(tmp, path) != 0) { remove(tmp); wh_buf_free(&b); if (err) snprintf(err, errsz, "cannot replace %s: %s", path, strerror(errno)); return -1; } set_mode_0600(path); wh_buf_free(&b); return 0; } /* ------------------------------------------------------------------ lookups */ wh_hook *wh_hook_find(wh_store *s, const char *name) { int i; if (!s || !name) return NULL; for (i = 0; i < s->nhooks; i++) if (strcmp(s->hooks[i].name, name) == 0) return &s->hooks[i]; return NULL; } void wh_hook_remove(wh_store *s, int idx) { int i; if (!s || idx < 0 || idx >= s->nhooks) return; for (i = idx; i + 1 < s->nhooks; i++) s->hooks[i] = s->hooks[i + 1]; s->nhooks--; memset(&s->hooks[s->nhooks], 0, sizeof s->hooks[0]); } void wh_store_log_add(wh_store *s, const wh_logent *e) { int i; if (!s || !e) return; for (i = (s->nlog < WH_LOG_MAX ? s->nlog : WH_LOG_MAX - 1); i > 0; i--) s->log[i] = s->log[i - 1]; s->log[0] = *e; if (s->nlog < WH_LOG_MAX) s->nlog++; } /* -------------------------------------------------------------- serializers */ void wh_hook_json(const wh_hook *h, int redacted, char *out, size_t outsz) { wh_buf b; int f = 1, k; if (!out || outsz == 0) return; out[0] = '\0'; wh_buf_init(&b); wh_buf_str(&b, "{"); wh_buf_key(&b, "name", &f); wh_buf_json_str(&b, h->name); wh_buf_key(&b, "method", &f); wh_buf_json_str(&b, h->method); wh_buf_key(&b, "desc", &f); wh_buf_json_str(&b, h->desc); wh_buf_key(&b, "sig", &f); wh_buf_json_str(&b, h->sig); wh_buf_key(&b, "has_secret", &f); wh_buf_str(&b, h->secret[0] ? "true" : "false"); if (redacted) { /* Domain + path only: a URL can carry a token in its query string, and * a list of hooks is exactly the kind of output that ends up in a * prompt. */ wh_url u; char err[128]; char clean[WH_URL_MAX + 32]; clean[0] = '\0'; if (wh_url_parse(h->url, &u, err, sizeof err) == 0) wh_url_redact(&u, clean, sizeof clean); wh_buf_key(&b, "url", &f); wh_buf_json_str(&b, clean); } else { wh_buf_key(&b, "url", &f); wh_buf_json_str(&b, h->url); } wh_buf_key(&b, "headers", &f); wh_buf_str(&b, "["); for (k = 0; k < h->nheaders; k++) { int f2 = 1; if (k) wh_buf_str(&b, ","); wh_buf_str(&b, "{"); wh_buf_key(&b, "name", &f2); wh_buf_json_str(&b, h->headers[k].name); if (redacted) { wh_buf_key(&b, "has_secret", &f2); wh_buf_str(&b, h->headers[k].value[0] ? "true" : "false"); } wh_buf_str(&b, "}"); } wh_buf_str(&b, "]}"); snprintf(out, outsz, "%s", wh_buf_cstr(&b)); wh_buf_free(&b); } void wh_store_json(const wh_store *s, int redacted, char *out, size_t outsz) { wh_buf b; int i; if (!out || outsz == 0) return; out[0] = '\0'; wh_buf_init(&b); wh_buf_str(&b, "{\"ok\":true,\"count\":"); wh_buf_printf(&b, "%d", s->nhooks); wh_buf_str(&b, ",\"hooks\":["); for (i = 0; i < s->nhooks; i++) { char one[WH_URL_MAX * 2 + 4096]; if (i) wh_buf_str(&b, ","); wh_hook_json(&s->hooks[i], redacted, one, sizeof one); wh_buf_str(&b, one); } wh_buf_str(&b, "]}"); snprintf(out, outsz, "%s", wh_buf_cstr(&b)); wh_buf_free(&b); } void wh_store_list_json(const wh_store *s, char *out, size_t outsz) { wh_buf b; int i, f = 0; /* "ok" and "count" were written literally, so the next * key needs its separator */ if (!out || outsz == 0) return; out[0] = '\0'; wh_buf_init(&b); wh_buf_str(&b, "{\"ok\":true,\"count\":"); wh_buf_printf(&b, "%d", s->nhooks); wh_buf_key(&b, "hooks", &f); wh_buf_str(&b, "["); for (i = 0; i < s->nhooks; i++) { char one[WH_URL_MAX * 2 + 4096]; if (i) wh_buf_str(&b, ","); wh_hook_json(&s->hooks[i], 1, one, sizeof one); wh_buf_str(&b, one); } wh_buf_str(&b, "]"); /* Every key after "hooks" must go through wh_buf_key so the object stays * well-formed: a hand-written ",\"log\":[" here once produced * {"count":16"hooks":... - valid-looking, unparseable. */ wh_buf_key(&b, "log", &f); wh_buf_str(&b, "["); for (i = 0; i < s->nlog; i++) { const wh_logent *e = &s->log[i]; int f2 = 1; if (i) wh_buf_str(&b, ","); wh_buf_str(&b, "{"); wh_buf_key(&b, "at", &f2); wh_buf_json_str(&b, e->at); wh_buf_key(&b, "name", &f2); wh_buf_json_str(&b, e->name); wh_buf_key(&b, "status", &f2); wh_buf_printf(&b, "%d", e->status); wh_buf_key(&b, "ok", &f2); wh_buf_str(&b, e->ok ? "true" : "false"); wh_buf_key(&b, "bytes", &f2); wh_buf_printf(&b, "%ld", e->bytes); wh_buf_key(&b, "ms", &f2); wh_buf_printf(&b, "%ld", e->ms); wh_buf_key(&b, "error", &f2); wh_buf_json_str(&b, e->error); wh_buf_str(&b, "}"); } wh_buf_str(&b, "]"); wh_buf_key(&b, "allow_private", &f); wh_buf_str(&b, wh_allow_private() ? "true" : "false"); wh_buf_str(&b, "}"); snprintf(out, outsz, "%s", wh_buf_cstr(&b)); wh_buf_free(&b); } /* ----------------------------------------------------------------- validate */ static int header_name_ok(const char *s) { if (!s || !*s) return 0; for (; *s; s++) { unsigned char c = (unsigned char)*s; if (c <= 0x20 || c >= 0x7F) return 0; if (strchr("()<>@,;:\\\"/[]?={}", c)) return 0; } return 1; } static int header_value_ok(const char *s) { if (!s) return 1; for (; *s; s++) { if (*s == '\r' || *s == '\n' || *s == '\0') return 0; } return 1; } int wh_hook_validate(const wh_hook *h, char *err, size_t errsz) { wh_url u; char uerr[160]; int i; if (err && errsz) err[0] = '\0'; if (!h->name[0]) { if (err) snprintf(err, errsz, "a hook needs a name"); return -1; } for (i = 0; h->name[i]; i++) { unsigned char c = (unsigned char)h->name[i]; if (!(isalnum(c) || c == '-' || c == '_' || c == '.')) { if (err) snprintf(err, errsz, "hook names may contain letters, digits, dash, " "underscore and dot only"); return -1; } } if (wh_url_parse(h->url, &u, uerr, sizeof uerr) != 0) { if (err) snprintf(err, errsz, "url: %s", uerr); return -1; } if (!wh_method_ok(h->method)) { if (err) snprintf(err, errsz, "method must be one of GET, POST, PUT, PATCH, DELETE, HEAD"); return -1; } if (h->sig[0] && strcmp(h->sig, WH_SIG_GITHUB) != 0 && strcmp(h->sig, WH_SIG_GITLAB) != 0 && strcmp(h->sig, WH_SIG_V2) != 0) { if (err) snprintf(err, errsz, "unknown signature scheme"); return -1; } if (h->sig[0] && !h->secret[0]) { if (err) snprintf(err, errsz, "a signature scheme needs a secret"); return -1; } if (h->nheaders < 0 || h->nheaders > WH_MAX_HEADERS) { if (err) snprintf(err, errsz, "too many headers (max %d)", WH_MAX_HEADERS); return -1; } for (i = 0; i < h->nheaders; i++) { if (!header_name_ok(h->headers[i].name)) { if (err) snprintf(err, errsz, "header %d has an invalid name", i + 1); return -1; } if (!header_value_ok(h->headers[i].value)) { if (err) snprintf(err, errsz, "header %s has a newline in its value", h->headers[i].name); return -1; } } return 0; } /* ------------------------------------------------------------- apply a body */ static int apply_obj(wh_hook *h, yyjson_val *root, const wh_hook *existing, char *err, size_t errsz) { const char *v; yyjson_val *hdrs; v = jstr(root, "name"); if (v) copy_trunc(h->name, sizeof h->name, v); v = jstr(root, "url"); if (v) copy_trunc(h->url, sizeof h->url, v); v = jstr(root, "desc"); if (v) copy_trunc(h->desc, sizeof h->desc, v); v = jstr(root, "method"); if (v) { size_t i; char m[8]; copy_trunc(m, sizeof m, v); for (i = 0; m[i]; i++) m[i] = (char)toupper((unsigned char)m[i]); copy_trunc(h->method, sizeof h->method, m); } if (!h->method[0]) copy_trunc(h->method, sizeof h->method, "POST"); v = jstr(root, "sig"); if (v) { if (!*v || strcmp(v, WH_SIG_NONE) == 0) h->sig[0] = '\0'; else copy_trunc(h->sig, sizeof h->sig, v); } /* A secret is never echoed back, so an empty incoming value means "keep the * stored one". Rotating a secret is a save with a new value; clearing it is * an explicit sig=none. */ v = jstr(root, "secret"); if (v && *v) copy_trunc(h->secret, sizeof h->secret, v); else if (!h->sig[0]) h->secret[0] = '\0'; hdrs = yyjson_obj_get(root, "headers"); if (hdrs && yyjson_is_arr(hdrs)) { size_t i, n = yyjson_arr_size(hdrs); int out = 0; for (i = 0; i < n && out < WH_MAX_HEADERS; i++) { yyjson_val *e = yyjson_arr_get(hdrs, i); const char *nm = jstr(e, "name"); const char *val = jstr(e, "value"); int k, kept = 0; if (!nm || !*nm) continue; copy_trunc(h->headers[out].name, sizeof h->headers[out].name, nm); h->headers[out].value[0] = '\0'; if (val) { copy_trunc(h->headers[out].value, sizeof h->headers[out].value, val); } else if (existing) { /* no value supplied: keep the stored value for this name */ for (k = 0; k < existing->nheaders; k++) { if (strcmp(existing->headers[k].name, nm) == 0) { copy_trunc(h->headers[out].value, sizeof h->headers[out].value, existing->headers[k].value); kept = 1; break; } } } (void)kept; out++; } h->nheaders = out; } (void)err; (void)errsz; return 0; } int wh_hook_apply(wh_hook *h, const char *json, const wh_hook *existing, char *err, size_t errsz) { yyjson_doc *doc; yyjson_val *root; if (err && errsz) err[0] = '\0'; if (!json || !*json) { if (err) snprintf(err, errsz, "empty request body"); return -1; } doc = yyjson_read(json, strlen(json), 0); if (!doc) { if (err) snprintf(err, errsz, "request body is not valid JSON"); return -1; } root = yyjson_doc_get_root(doc); if (!yyjson_is_obj(root)) { yyjson_doc_free(doc); if (err) snprintf(err, errsz, "request body must be a JSON object"); return -1; } apply_obj(h, root, existing, err, errsz); yyjson_doc_free(doc); return wh_hook_validate(h, err, errsz); }