| 1 | /* |
| 2 | * wh_store.c - named hooks, their secrets, and the delivery log. See wh_store.h. |
| 3 | * |
| 4 | * The store file is the only place a secret exists on disk, and it is written |
| 5 | * at mode 0600 through a temp file plus rename() so a crash mid-write leaves |
| 6 | * the previous document intact rather than a truncated one. |
| 7 | * |
| 8 | * MIT License. Copyright (c) 2026 SCORPIOX INC. |
| 9 | */ |
| 10 | #define _POSIX_C_SOURCE 200809L |
| 11 | #if defined(__APPLE__) && !defined(_DARWIN_C_SOURCE) |
| 12 | #define _DARWIN_C_SOURCE 1 |
| 13 | #endif |
| 14 | |
| 15 | #include "wh_store.h" |
| 16 | |
| 17 | #include <ctype.h> |
| 18 | #include <errno.h> |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <time.h> |
| 23 | |
| 24 | #if defined(_WIN32) |
| 25 | # include <direct.h> |
| 26 | # include <io.h> |
| 27 | # include <sys/stat.h> |
| 28 | # include <windows.h> |
| 29 | #else |
| 30 | # include <sys/stat.h> |
| 31 | # include <sys/types.h> |
| 32 | # include <unistd.h> |
| 33 | #endif |
| 34 | |
| 35 | #include "wh_buf.h" |
| 36 | #include "yyjson.h" |
| 37 | |
| 38 | /* ------------------------------------------------------------------- paths */ |
| 39 | |
| 40 | static void mkdir_p(const char *path) |
| 41 | { |
| 42 | char tmp[1024]; |
| 43 | size_t i, n; |
| 44 | |
| 45 | n = strlen(path); |
| 46 | if (n == 0 || n >= sizeof tmp) return; |
| 47 | memcpy(tmp, path, n + 1); |
| 48 | for (i = 1; i < n; i++) { |
| 49 | if (tmp[i] != '/' && tmp[i] != '\\') continue; |
| 50 | tmp[i] = '\0'; |
| 51 | #if defined(_WIN32) |
| 52 | _mkdir(tmp); |
| 53 | #else |
| 54 | mkdir(tmp, 0700); |
| 55 | #endif |
| 56 | tmp[i] = '/'; |
| 57 | } |
| 58 | #if defined(_WIN32) |
| 59 | _mkdir(tmp); |
| 60 | #else |
| 61 | mkdir(tmp, 0700); |
| 62 | #endif |
| 63 | } |
| 64 | |
| 65 | void wh_store_path(char *out, size_t outsz) |
| 66 | { |
| 67 | const char *env = getenv("WEBHOOK_STORE_PATH"); |
| 68 | const char *base = NULL; |
| 69 | |
| 70 | if (outsz == 0) return; |
| 71 | out[0] = '\0'; |
| 72 | if (env && *env) { |
| 73 | snprintf(out, outsz, "%s", env); |
| 74 | return; |
| 75 | } |
| 76 | #if defined(_WIN32) |
| 77 | base = getenv("LOCALAPPDATA"); |
| 78 | if (base && *base) { |
| 79 | snprintf(out, outsz, "%s\\scorpiox\\connectors\\plugin_webhook\\hooks.json", base); |
| 80 | return; |
| 81 | } |
| 82 | base = getenv("USERPROFILE"); |
| 83 | if (base && *base) { |
| 84 | snprintf(out, outsz, "%s\\.scorpiox\\connectors\\plugin_webhook\\hooks.json", base); |
| 85 | return; |
| 86 | } |
| 87 | snprintf(out, outsz, "hooks.json"); |
| 88 | #else |
| 89 | { |
| 90 | struct stat st; |
| 91 | if (stat("/workspace", &st) == 0 && S_ISDIR(st.st_mode)) |
| 92 | snprintf(out, outsz, "/workspace/.scorpiox/connectors/plugin_webhook/hooks.json"); |
| 93 | else { |
| 94 | base = getenv("HOME"); |
| 95 | if (base && *base) |
| 96 | snprintf(out, outsz, "%s/.scorpiox/connectors/plugin_webhook/hooks.json", base); |
| 97 | else |
| 98 | snprintf(out, outsz, "/tmp/.scorpiox/connectors/plugin_webhook/hooks.json"); |
| 99 | } |
| 100 | } |
| 101 | #endif |
| 102 | } |
| 103 | |
| 104 | /* --------------------------------------------------------------- load/save */ |
| 105 | |
| 106 | static void copy_trunc(char *dst, size_t capsz, const char *src) |
| 107 | { |
| 108 | size_t n; |
| 109 | if (!capsz) return; |
| 110 | if (!src) { dst[0] = '\0'; return; } |
| 111 | n = strlen(src); |
| 112 | if (n >= capsz) n = capsz - 1; |
| 113 | memcpy(dst, src, n); |
| 114 | dst[n] = '\0'; |
| 115 | } |
| 116 | |
| 117 | static const char *jstr(yyjson_val *o, const char *key) |
| 118 | { |
| 119 | yyjson_val *v; |
| 120 | if (!o) return NULL; |
| 121 | v = yyjson_obj_get(o, key); |
| 122 | if (!v || !yyjson_is_str(v)) return NULL; |
| 123 | return yyjson_get_str(v); |
| 124 | } |
| 125 | |
| 126 | int wh_store_load(wh_store *s, char *err, size_t errsz) |
| 127 | { |
| 128 | FILE *f; |
| 129 | char path[1024]; |
| 130 | char *text; |
| 131 | long sz; |
| 132 | size_t got; |
| 133 | yyjson_doc *doc; |
| 134 | yyjson_val *root, *hooks, *log; |
| 135 | |
| 136 | memset(s, 0, sizeof *s); |
| 137 | if (err && errsz) err[0] = '\0'; |
| 138 | wh_store_path(path, sizeof path); |
| 139 | |
| 140 | f = fopen(path, "rb"); |
| 141 | if (!f) return 0; /* no file yet: empty store */ |
| 142 | if (fseek(f, 0, SEEK_END) != 0) { fclose(f); return 0; } |
| 143 | sz = ftell(f); |
| 144 | if (sz < 0 || sz > (long)(4 * 1024 * 1024)) { |
| 145 | fclose(f); |
| 146 | if (err) snprintf(err, errsz, "store file is unreadable or too large"); |
| 147 | return -1; |
| 148 | } |
| 149 | rewind(f); |
| 150 | text = (char *)malloc((size_t)sz + 1); |
| 151 | if (!text) { fclose(f); if (err) snprintf(err, errsz, "out of memory"); return -1; } |
| 152 | got = fread(text, 1, (size_t)sz, f); |
| 153 | fclose(f); |
| 154 | text[got] = '\0'; |
| 155 | |
| 156 | doc = yyjson_read(text, got, 0); |
| 157 | free(text); |
| 158 | if (!doc) { |
| 159 | /* A corrupt store is reported, never silently replaced: the operator |
| 160 | * may still be able to recover the secrets from the file. */ |
| 161 | if (err) snprintf(err, errsz, |
| 162 | "the hook store exists but is not valid JSON (%s); " |
| 163 | "it was left untouched", path); |
| 164 | return -1; |
| 165 | } |
| 166 | root = yyjson_doc_get_root(doc); |
| 167 | |
| 168 | hooks = yyjson_obj_get(root, "hooks"); |
| 169 | if (hooks && yyjson_is_arr(hooks)) { |
| 170 | size_t i, n = yyjson_arr_size(hooks); |
| 171 | for (i = 0; i < n && s->nhooks < WH_MAX_HOOKS; i++) { |
| 172 | yyjson_val *o = yyjson_arr_get(hooks, i); |
| 173 | yyjson_val *hdrs; |
| 174 | wh_hook *h = &s->hooks[s->nhooks]; |
| 175 | size_t k, hn; |
| 176 | |
| 177 | if (!yyjson_is_obj(o)) continue; |
| 178 | copy_trunc(h->name, sizeof h->name, jstr(o, "name")); |
| 179 | copy_trunc(h->url, sizeof h->url, jstr(o, "url")); |
| 180 | copy_trunc(h->method, sizeof h->method, jstr(o, "method")); |
| 181 | copy_trunc(h->desc, sizeof h->desc, jstr(o, "desc")); |
| 182 | copy_trunc(h->sig, sizeof h->sig, jstr(o, "sig")); |
| 183 | copy_trunc(h->secret, sizeof h->secret, jstr(o, "secret")); |
| 184 | if (!h->method[0]) copy_trunc(h->method, sizeof h->method, "POST"); |
| 185 | hdrs = yyjson_obj_get(o, "headers"); |
| 186 | hn = 0; |
| 187 | if (hdrs && yyjson_is_arr(hdrs)) { |
| 188 | size_t m = yyjson_arr_size(hdrs); |
| 189 | for (k = 0; k < m && hn < WH_MAX_HEADERS; k++) { |
| 190 | yyjson_val *e = yyjson_arr_get(hdrs, k); |
| 191 | const char *nm = jstr(e, "name"); |
| 192 | const char *val = jstr(e, "value"); |
| 193 | if (!nm || !*nm) continue; |
| 194 | copy_trunc(h->headers[hn].name, sizeof h->headers[hn].name, nm); |
| 195 | copy_trunc(h->headers[hn].value, sizeof h->headers[hn].value, |
| 196 | val ? val : ""); |
| 197 | hn++; |
| 198 | } |
| 199 | } |
| 200 | h->nheaders = (int)hn; |
| 201 | if (!h->name[0]) continue; |
| 202 | s->nhooks++; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | log = yyjson_obj_get(root, "log"); |
| 207 | if (log && yyjson_is_arr(log)) { |
| 208 | size_t i, n = yyjson_arr_size(log); |
| 209 | for (i = 0; i < n && s->nlog < WH_LOG_MAX; i++) { |
| 210 | yyjson_val *o = yyjson_arr_get(log, i); |
| 211 | wh_logent *e = &s->log[s->nlog]; |
| 212 | if (!yyjson_is_obj(o)) continue; |
| 213 | copy_trunc(e->at, sizeof e->at, jstr(o, "at")); |
| 214 | copy_trunc(e->name, sizeof e->name, jstr(o, "name")); |
| 215 | copy_trunc(e->error, sizeof e->error, jstr(o, "error")); |
| 216 | { |
| 217 | yyjson_val *v = yyjson_obj_get(o, "status"); |
| 218 | e->status = (v && yyjson_is_int(v)) ? (int)yyjson_get_sint(v) : 0; |
| 219 | v = yyjson_obj_get(o, "ok"); |
| 220 | e->ok = (v && yyjson_is_bool(v)) ? (yyjson_get_bool(v) ? 1 : 0) : 0; |
| 221 | v = yyjson_obj_get(o, "bytes"); |
| 222 | e->bytes = (v && yyjson_is_int(v)) ? (long)yyjson_get_sint(v) : 0; |
| 223 | v = yyjson_obj_get(o, "ms"); |
| 224 | e->ms = (v && yyjson_is_int(v)) ? (long)yyjson_get_sint(v) : 0; |
| 225 | } |
| 226 | if (!e->name[0]) continue; |
| 227 | s->nlog++; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | yyjson_doc_free(doc); |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | static void set_mode_0600(const char *path) |
| 236 | { |
| 237 | #if defined(_WIN32) |
| 238 | (void)path; /* NTFS ACLs are inherited from the profile dir */ |
| 239 | #else |
| 240 | chmod(path, 0600); |
| 241 | #endif |
| 242 | } |
| 243 | |
| 244 | int wh_store_save(const wh_store *s, char *err, size_t errsz) |
| 245 | { |
| 246 | char path[1024], tmp[1100], dir[1024]; |
| 247 | wh_buf b; |
| 248 | FILE *f; |
| 249 | int i, first; |
| 250 | char *slash; |
| 251 | |
| 252 | if (err && errsz) err[0] = '\0'; |
| 253 | wh_store_path(path, sizeof path); |
| 254 | copy_trunc(dir, sizeof dir, path); |
| 255 | slash = strrchr(dir, '/'); |
| 256 | #if defined(_WIN32) |
| 257 | if (!slash) slash = strrchr(dir, '\\'); |
| 258 | #endif |
| 259 | if (slash) { *slash = '\0'; mkdir_p(dir); } |
| 260 | |
| 261 | wh_buf_init(&b); |
| 262 | wh_buf_str(&b, "{\"version\":1,\"hooks\":["); |
| 263 | first = 1; |
| 264 | for (i = 0; i < s->nhooks; i++) { |
| 265 | const wh_hook *h = &s->hooks[i]; |
| 266 | int f2 = 1, k; |
| 267 | if (!first) wh_buf_str(&b, ","); |
| 268 | first = 0; |
| 269 | wh_buf_str(&b, "{"); |
| 270 | wh_buf_key(&b, "name", &f2); wh_buf_json_str(&b, h->name); |
| 271 | wh_buf_key(&b, "url", &f2); wh_buf_json_str(&b, h->url); |
| 272 | wh_buf_key(&b, "method", &f2); wh_buf_json_str(&b, h->method); |
| 273 | wh_buf_key(&b, "desc", &f2); wh_buf_json_str(&b, h->desc); |
| 274 | wh_buf_key(&b, "sig", &f2); wh_buf_json_str(&b, h->sig); |
| 275 | wh_buf_key(&b, "secret", &f2); wh_buf_json_str(&b, h->secret); |
| 276 | wh_buf_key(&b, "headers", &f2); |
| 277 | wh_buf_str(&b, "["); |
| 278 | for (k = 0; k < h->nheaders; k++) { |
| 279 | int f3 = 1; |
| 280 | if (k) wh_buf_str(&b, ","); |
| 281 | wh_buf_str(&b, "{"); |
| 282 | wh_buf_key(&b, "name", &f3); wh_buf_json_str(&b, h->headers[k].name); |
| 283 | wh_buf_key(&b, "value", &f3); wh_buf_json_str(&b, h->headers[k].value); |
| 284 | wh_buf_str(&b, "}"); |
| 285 | } |
| 286 | wh_buf_str(&b, "]}"); |
| 287 | } |
| 288 | wh_buf_str(&b, "],\"log\":["); |
| 289 | for (i = 0; i < s->nlog; i++) { |
| 290 | const wh_logent *e = &s->log[i]; |
| 291 | int f2 = 1; |
| 292 | if (i) wh_buf_str(&b, ","); |
| 293 | wh_buf_str(&b, "{"); |
| 294 | wh_buf_key(&b, "at", &f2); wh_buf_json_str(&b, e->at); |
| 295 | wh_buf_key(&b, "name", &f2); wh_buf_json_str(&b, e->name); |
| 296 | wh_buf_key(&b, "status", &f2); wh_buf_printf(&b, "%d", e->status); |
| 297 | wh_buf_key(&b, "ok", &f2); wh_buf_str(&b, e->ok ? "true" : "false"); |
| 298 | wh_buf_key(&b, "bytes", &f2); wh_buf_printf(&b, "%ld", e->bytes); |
| 299 | wh_buf_key(&b, "ms", &f2); wh_buf_printf(&b, "%ld", e->ms); |
| 300 | wh_buf_key(&b, "error", &f2); wh_buf_json_str(&b, e->error); |
| 301 | wh_buf_str(&b, "}"); |
| 302 | } |
| 303 | wh_buf_str(&b, "]}"); |
| 304 | |
| 305 | if (b.oom) { |
| 306 | wh_buf_free(&b); |
| 307 | if (err) snprintf(err, errsz, "out of memory"); |
| 308 | return -1; |
| 309 | } |
| 310 | snprintf(tmp, sizeof tmp, "%s.tmp", path); |
| 311 | f = fopen(tmp, "wb"); |
| 312 | if (!f) { |
| 313 | wh_buf_free(&b); |
| 314 | if (err) snprintf(err, errsz, "cannot create %s: %s", tmp, strerror(errno)); |
| 315 | return -1; |
| 316 | } |
| 317 | if (fwrite(wh_buf_cstr(&b), 1, b.n, f) != b.n) { |
| 318 | fclose(f); |
| 319 | remove(tmp); |
| 320 | wh_buf_free(&b); |
| 321 | if (err) snprintf(err, errsz, "short write to %s", tmp); |
| 322 | return -1; |
| 323 | } |
| 324 | fflush(f); |
| 325 | fclose(f); |
| 326 | set_mode_0600(tmp); |
| 327 | #if defined(_WIN32) |
| 328 | remove(path); |
| 329 | #endif |
| 330 | if (rename(tmp, path) != 0) { |
| 331 | remove(tmp); |
| 332 | wh_buf_free(&b); |
| 333 | if (err) snprintf(err, errsz, "cannot replace %s: %s", path, strerror(errno)); |
| 334 | return -1; |
| 335 | } |
| 336 | set_mode_0600(path); |
| 337 | wh_buf_free(&b); |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | /* ------------------------------------------------------------------ lookups */ |
| 342 | |
| 343 | wh_hook *wh_hook_find(wh_store *s, const char *name) |
| 344 | { |
| 345 | int i; |
| 346 | if (!s || !name) return NULL; |
| 347 | for (i = 0; i < s->nhooks; i++) |
| 348 | if (strcmp(s->hooks[i].name, name) == 0) return &s->hooks[i]; |
| 349 | return NULL; |
| 350 | } |
| 351 | |
| 352 | void wh_hook_remove(wh_store *s, int idx) |
| 353 | { |
| 354 | int i; |
| 355 | if (!s || idx < 0 || idx >= s->nhooks) return; |
| 356 | for (i = idx; i + 1 < s->nhooks; i++) s->hooks[i] = s->hooks[i + 1]; |
| 357 | s->nhooks--; |
| 358 | memset(&s->hooks[s->nhooks], 0, sizeof s->hooks[0]); |
| 359 | } |
| 360 | |
| 361 | void wh_store_log_add(wh_store *s, const wh_logent *e) |
| 362 | { |
| 363 | int i; |
| 364 | if (!s || !e) return; |
| 365 | for (i = (s->nlog < WH_LOG_MAX ? s->nlog : WH_LOG_MAX - 1); i > 0; i--) |
| 366 | s->log[i] = s->log[i - 1]; |
| 367 | s->log[0] = *e; |
| 368 | if (s->nlog < WH_LOG_MAX) s->nlog++; |
| 369 | } |
| 370 | |
| 371 | /* -------------------------------------------------------------- serializers */ |
| 372 | |
| 373 | void wh_hook_json(const wh_hook *h, int redacted, char *out, size_t outsz) |
| 374 | { |
| 375 | wh_buf b; |
| 376 | int f = 1, k; |
| 377 | |
| 378 | if (!out || outsz == 0) return; |
| 379 | out[0] = '\0'; |
| 380 | wh_buf_init(&b); |
| 381 | wh_buf_str(&b, "{"); |
| 382 | wh_buf_key(&b, "name", &f); wh_buf_json_str(&b, h->name); |
| 383 | wh_buf_key(&b, "method", &f); wh_buf_json_str(&b, h->method); |
| 384 | wh_buf_key(&b, "desc", &f); wh_buf_json_str(&b, h->desc); |
| 385 | wh_buf_key(&b, "sig", &f); wh_buf_json_str(&b, h->sig); |
| 386 | wh_buf_key(&b, "has_secret", &f); |
| 387 | wh_buf_str(&b, h->secret[0] ? "true" : "false"); |
| 388 | if (redacted) { |
| 389 | /* Domain + path only: a URL can carry a token in its query string, and |
| 390 | * a list of hooks is exactly the kind of output that ends up in a |
| 391 | * prompt. */ |
| 392 | wh_url u; |
| 393 | char err[128]; |
| 394 | char clean[WH_URL_MAX + 32]; |
| 395 | clean[0] = '\0'; |
| 396 | if (wh_url_parse(h->url, &u, err, sizeof err) == 0) |
| 397 | wh_url_redact(&u, clean, sizeof clean); |
| 398 | wh_buf_key(&b, "url", &f); |
| 399 | wh_buf_json_str(&b, clean); |
| 400 | } else { |
| 401 | wh_buf_key(&b, "url", &f); |
| 402 | wh_buf_json_str(&b, h->url); |
| 403 | } |
| 404 | wh_buf_key(&b, "headers", &f); |
| 405 | wh_buf_str(&b, "["); |
| 406 | for (k = 0; k < h->nheaders; k++) { |
| 407 | int f2 = 1; |
| 408 | if (k) wh_buf_str(&b, ","); |
| 409 | wh_buf_str(&b, "{"); |
| 410 | wh_buf_key(&b, "name", &f2); |
| 411 | wh_buf_json_str(&b, h->headers[k].name); |
| 412 | if (redacted) { |
| 413 | wh_buf_key(&b, "has_secret", &f2); |
| 414 | wh_buf_str(&b, h->headers[k].value[0] ? "true" : "false"); |
| 415 | } |
| 416 | wh_buf_str(&b, "}"); |
| 417 | } |
| 418 | wh_buf_str(&b, "]}"); |
| 419 | snprintf(out, outsz, "%s", wh_buf_cstr(&b)); |
| 420 | wh_buf_free(&b); |
| 421 | } |
| 422 | |
| 423 | void wh_store_json(const wh_store *s, int redacted, char *out, size_t outsz) |
| 424 | { |
| 425 | wh_buf b; |
| 426 | int i; |
| 427 | |
| 428 | if (!out || outsz == 0) return; |
| 429 | out[0] = '\0'; |
| 430 | wh_buf_init(&b); |
| 431 | wh_buf_str(&b, "{\"ok\":true,\"count\":"); |
| 432 | wh_buf_printf(&b, "%d", s->nhooks); |
| 433 | wh_buf_str(&b, ",\"hooks\":["); |
| 434 | for (i = 0; i < s->nhooks; i++) { |
| 435 | char one[WH_URL_MAX * 2 + 4096]; |
| 436 | if (i) wh_buf_str(&b, ","); |
| 437 | wh_hook_json(&s->hooks[i], redacted, one, sizeof one); |
| 438 | wh_buf_str(&b, one); |
| 439 | } |
| 440 | wh_buf_str(&b, "]}"); |
| 441 | snprintf(out, outsz, "%s", wh_buf_cstr(&b)); |
| 442 | wh_buf_free(&b); |
| 443 | } |
| 444 | |
| 445 | void wh_store_list_json(const wh_store *s, char *out, size_t outsz) |
| 446 | { |
| 447 | wh_buf b; |
| 448 | int i, f = 0; /* "ok" and "count" were written literally, so the next |
| 449 | * key needs its separator */ |
| 450 | |
| 451 | if (!out || outsz == 0) return; |
| 452 | out[0] = '\0'; |
| 453 | wh_buf_init(&b); |
| 454 | wh_buf_str(&b, "{\"ok\":true,\"count\":"); |
| 455 | wh_buf_printf(&b, "%d", s->nhooks); |
| 456 | wh_buf_key(&b, "hooks", &f); |
| 457 | wh_buf_str(&b, "["); |
| 458 | for (i = 0; i < s->nhooks; i++) { |
| 459 | char one[WH_URL_MAX * 2 + 4096]; |
| 460 | if (i) wh_buf_str(&b, ","); |
| 461 | wh_hook_json(&s->hooks[i], 1, one, sizeof one); |
| 462 | wh_buf_str(&b, one); |
| 463 | } |
| 464 | wh_buf_str(&b, "]"); |
| 465 | /* Every key after "hooks" must go through wh_buf_key so the object stays |
| 466 | * well-formed: a hand-written ",\"log\":[" here once produced |
| 467 | * {"count":16"hooks":... - valid-looking, unparseable. */ |
| 468 | wh_buf_key(&b, "log", &f); |
| 469 | wh_buf_str(&b, "["); |
| 470 | for (i = 0; i < s->nlog; i++) { |
| 471 | const wh_logent *e = &s->log[i]; |
| 472 | int f2 = 1; |
| 473 | if (i) wh_buf_str(&b, ","); |
| 474 | wh_buf_str(&b, "{"); |
| 475 | wh_buf_key(&b, "at", &f2); wh_buf_json_str(&b, e->at); |
| 476 | wh_buf_key(&b, "name", &f2); wh_buf_json_str(&b, e->name); |
| 477 | wh_buf_key(&b, "status", &f2); wh_buf_printf(&b, "%d", e->status); |
| 478 | wh_buf_key(&b, "ok", &f2); wh_buf_str(&b, e->ok ? "true" : "false"); |
| 479 | wh_buf_key(&b, "bytes", &f2); wh_buf_printf(&b, "%ld", e->bytes); |
| 480 | wh_buf_key(&b, "ms", &f2); wh_buf_printf(&b, "%ld", e->ms); |
| 481 | wh_buf_key(&b, "error", &f2); wh_buf_json_str(&b, e->error); |
| 482 | wh_buf_str(&b, "}"); |
| 483 | } |
| 484 | wh_buf_str(&b, "]"); |
| 485 | wh_buf_key(&b, "allow_private", &f); |
| 486 | wh_buf_str(&b, wh_allow_private() ? "true" : "false"); |
| 487 | wh_buf_str(&b, "}"); |
| 488 | snprintf(out, outsz, "%s", wh_buf_cstr(&b)); |
| 489 | wh_buf_free(&b); |
| 490 | } |
| 491 | |
| 492 | /* ----------------------------------------------------------------- validate */ |
| 493 | |
| 494 | static int header_name_ok(const char *s) |
| 495 | { |
| 496 | if (!s || !*s) return 0; |
| 497 | for (; *s; s++) { |
| 498 | unsigned char c = (unsigned char)*s; |
| 499 | if (c <= 0x20 || c >= 0x7F) return 0; |
| 500 | if (strchr("()<>@,;:\\\"/[]?={}", c)) return 0; |
| 501 | } |
| 502 | return 1; |
| 503 | } |
| 504 | |
| 505 | static int header_value_ok(const char *s) |
| 506 | { |
| 507 | if (!s) return 1; |
| 508 | for (; *s; s++) { |
| 509 | if (*s == '\r' || *s == '\n' || *s == '\0') return 0; |
| 510 | } |
| 511 | return 1; |
| 512 | } |
| 513 | |
| 514 | int wh_hook_validate(const wh_hook *h, char *err, size_t errsz) |
| 515 | { |
| 516 | wh_url u; |
| 517 | char uerr[160]; |
| 518 | int i; |
| 519 | |
| 520 | if (err && errsz) err[0] = '\0'; |
| 521 | if (!h->name[0]) { |
| 522 | if (err) snprintf(err, errsz, "a hook needs a name"); |
| 523 | return -1; |
| 524 | } |
| 525 | for (i = 0; h->name[i]; i++) { |
| 526 | unsigned char c = (unsigned char)h->name[i]; |
| 527 | if (!(isalnum(c) || c == '-' || c == '_' || c == '.')) { |
| 528 | if (err) snprintf(err, errsz, |
| 529 | "hook names may contain letters, digits, dash, " |
| 530 | "underscore and dot only"); |
| 531 | return -1; |
| 532 | } |
| 533 | } |
| 534 | if (wh_url_parse(h->url, &u, uerr, sizeof uerr) != 0) { |
| 535 | if (err) snprintf(err, errsz, "url: %s", uerr); |
| 536 | return -1; |
| 537 | } |
| 538 | if (!wh_method_ok(h->method)) { |
| 539 | if (err) snprintf(err, errsz, "method must be one of GET, POST, PUT, PATCH, DELETE, HEAD"); |
| 540 | return -1; |
| 541 | } |
| 542 | if (h->sig[0] && strcmp(h->sig, WH_SIG_GITHUB) != 0 && |
| 543 | strcmp(h->sig, WH_SIG_GITLAB) != 0 && strcmp(h->sig, WH_SIG_V2) != 0) { |
| 544 | if (err) snprintf(err, errsz, "unknown signature scheme"); |
| 545 | return -1; |
| 546 | } |
| 547 | if (h->sig[0] && !h->secret[0]) { |
| 548 | if (err) snprintf(err, errsz, "a signature scheme needs a secret"); |
| 549 | return -1; |
| 550 | } |
| 551 | if (h->nheaders < 0 || h->nheaders > WH_MAX_HEADERS) { |
| 552 | if (err) snprintf(err, errsz, "too many headers (max %d)", WH_MAX_HEADERS); |
| 553 | return -1; |
| 554 | } |
| 555 | for (i = 0; i < h->nheaders; i++) { |
| 556 | if (!header_name_ok(h->headers[i].name)) { |
| 557 | if (err) snprintf(err, errsz, "header %d has an invalid name", i + 1); |
| 558 | return -1; |
| 559 | } |
| 560 | if (!header_value_ok(h->headers[i].value)) { |
| 561 | if (err) snprintf(err, errsz, |
| 562 | "header %s has a newline in its value", |
| 563 | h->headers[i].name); |
| 564 | return -1; |
| 565 | } |
| 566 | } |
| 567 | return 0; |
| 568 | } |
| 569 | |
| 570 | /* ------------------------------------------------------------- apply a body */ |
| 571 | |
| 572 | static int apply_obj(wh_hook *h, yyjson_val *root, const wh_hook *existing, |
| 573 | char *err, size_t errsz) |
| 574 | { |
| 575 | const char *v; |
| 576 | yyjson_val *hdrs; |
| 577 | |
| 578 | v = jstr(root, "name"); |
| 579 | if (v) copy_trunc(h->name, sizeof h->name, v); |
| 580 | v = jstr(root, "url"); |
| 581 | if (v) copy_trunc(h->url, sizeof h->url, v); |
| 582 | v = jstr(root, "desc"); |
| 583 | if (v) copy_trunc(h->desc, sizeof h->desc, v); |
| 584 | v = jstr(root, "method"); |
| 585 | if (v) { |
| 586 | size_t i; |
| 587 | char m[8]; |
| 588 | copy_trunc(m, sizeof m, v); |
| 589 | for (i = 0; m[i]; i++) m[i] = (char)toupper((unsigned char)m[i]); |
| 590 | copy_trunc(h->method, sizeof h->method, m); |
| 591 | } |
| 592 | if (!h->method[0]) copy_trunc(h->method, sizeof h->method, "POST"); |
| 593 | |
| 594 | v = jstr(root, "sig"); |
| 595 | if (v) { |
| 596 | if (!*v || strcmp(v, WH_SIG_NONE) == 0) h->sig[0] = '\0'; |
| 597 | else copy_trunc(h->sig, sizeof h->sig, v); |
| 598 | } |
| 599 | |
| 600 | /* A secret is never echoed back, so an empty incoming value means "keep the |
| 601 | * stored one". Rotating a secret is a save with a new value; clearing it is |
| 602 | * an explicit sig=none. */ |
| 603 | v = jstr(root, "secret"); |
| 604 | if (v && *v) copy_trunc(h->secret, sizeof h->secret, v); |
| 605 | else if (!h->sig[0]) h->secret[0] = '\0'; |
| 606 | |
| 607 | hdrs = yyjson_obj_get(root, "headers"); |
| 608 | if (hdrs && yyjson_is_arr(hdrs)) { |
| 609 | size_t i, n = yyjson_arr_size(hdrs); |
| 610 | int out = 0; |
| 611 | for (i = 0; i < n && out < WH_MAX_HEADERS; i++) { |
| 612 | yyjson_val *e = yyjson_arr_get(hdrs, i); |
| 613 | const char *nm = jstr(e, "name"); |
| 614 | const char *val = jstr(e, "value"); |
| 615 | int k, kept = 0; |
| 616 | if (!nm || !*nm) continue; |
| 617 | copy_trunc(h->headers[out].name, sizeof h->headers[out].name, nm); |
| 618 | h->headers[out].value[0] = '\0'; |
| 619 | if (val) { |
| 620 | copy_trunc(h->headers[out].value, sizeof h->headers[out].value, val); |
| 621 | } else if (existing) { |
| 622 | /* no value supplied: keep the stored value for this name */ |
| 623 | for (k = 0; k < existing->nheaders; k++) { |
| 624 | if (strcmp(existing->headers[k].name, nm) == 0) { |
| 625 | copy_trunc(h->headers[out].value, |
| 626 | sizeof h->headers[out].value, |
| 627 | existing->headers[k].value); |
| 628 | kept = 1; |
| 629 | break; |
| 630 | } |
| 631 | } |
| 632 | } |
| 633 | (void)kept; |
| 634 | out++; |
| 635 | } |
| 636 | h->nheaders = out; |
| 637 | } |
| 638 | (void)err; |
| 639 | (void)errsz; |
| 640 | return 0; |
| 641 | } |
| 642 | |
| 643 | int wh_hook_apply(wh_hook *h, const char *json, const wh_hook *existing, |
| 644 | char *err, size_t errsz) |
| 645 | { |
| 646 | yyjson_doc *doc; |
| 647 | yyjson_val *root; |
| 648 | |
| 649 | if (err && errsz) err[0] = '\0'; |
| 650 | if (!json || !*json) { |
| 651 | if (err) snprintf(err, errsz, "empty request body"); |
| 652 | return -1; |
| 653 | } |
| 654 | doc = yyjson_read(json, strlen(json), 0); |
| 655 | if (!doc) { |
| 656 | if (err) snprintf(err, errsz, "request body is not valid JSON"); |
| 657 | return -1; |
| 658 | } |
| 659 | root = yyjson_doc_get_root(doc); |
| 660 | if (!yyjson_is_obj(root)) { |
| 661 | yyjson_doc_free(doc); |
| 662 | if (err) snprintf(err, errsz, "request body must be a JSON object"); |
| 663 | return -1; |
| 664 | } |
| 665 | apply_obj(h, root, existing, err, errsz); |
| 666 | yyjson_doc_free(doc); |
| 667 | return wh_hook_validate(h, err, errsz); |
| 668 | } |