| 1 | /* |
| 2 | * wh_http.h - one outbound HTTP request with SSRF policy and verified TLS. |
| 3 | * |
| 4 | * The connector sends a user-defined request (method, URL, headers, body) to a |
| 5 | * user-defined endpoint. Two things make that safe enough to expose as a tool: |
| 6 | * |
| 7 | * 1. SSRF policy. Every address the hostname resolves to is checked against a |
| 8 | * deny list of loopback / RFC1918 / link-local / ULA / CGNAT / multicast / |
| 9 | * documentation / reserved ranges BEFORE connecting, and the connection is |
| 10 | * then made to that exact validated numeric address. A DNS answer that |
| 11 | * changes between the check and the connect therefore cannot move the |
| 12 | * request onto a private network. WEBHOOK_ALLOW_PRIVATE=1 lifts the policy |
| 13 | * for an operator who genuinely means to call their own LAN (the offline |
| 14 | * suite uses it to reach its recording receiver on 127.0.0.1). |
| 15 | * |
| 16 | * 2. Verified TLS. POSIX wraps the validated socket in mbedTLS (TLS 1.2+, |
| 17 | * VERIFY_PEER, hostname check, system or WEBHOOK_TLS_CA_FILE anchors) with |
| 18 | * no insecure bypass. _WIN32 uses WinHTTP with the system root store; it |
| 19 | * connects by NAME rather than by the validated numeric address because |
| 20 | * WinHTTP derives both SNI and the certificate hostname it verifies from |
| 21 | * the name given to WinHttpConnect(), so an IP connection would refuse |
| 22 | * every ordinary name-based certificate. The all-answers SSRF check still |
| 23 | * runs first, and verified TLS refuses a public name whose peer cannot |
| 24 | * present a certificate for it. |
| 25 | * |
| 26 | * Redirects are NOT followed: a hook's URL is part of its definition, so a |
| 27 | * 30x is reported as a status instead of silently becoming a request to |
| 28 | * somewhere the operator never configured. |
| 29 | * |
| 30 | * MIT License. Copyright (c) 2026 SCORPIOX INC. |
| 31 | */ |
| 32 | #ifndef SX_WH_HTTP_H |
| 33 | #define SX_WH_HTTP_H |
| 34 | |
| 35 | #include <stddef.h> |
| 36 | |
| 37 | #define WH_URL_MAX 2048 |
| 38 | #define WH_PREVIEW_CAP 4096 /* bytes of the response body we keep */ |
| 39 | #define WH_HEAD_CAP (64u * 1024u) |
| 40 | #define WH_BODY_CAP (1u * 1024u * 1024u) /* outbound body limit */ |
| 41 | #define WH_CONNECT_MS 10000 |
| 42 | #define WH_READ_MS 15000 |
| 43 | |
| 44 | typedef struct { |
| 45 | char scheme[8]; /* "http" | "https" */ |
| 46 | char host[256]; |
| 47 | int port; |
| 48 | char path[WH_URL_MAX]; /* path + query; "/" when absent */ |
| 49 | int tls; |
| 50 | } wh_url; |
| 51 | |
| 52 | typedef struct { |
| 53 | char name[128]; |
| 54 | char value[1024]; |
| 55 | } wh_header; |
| 56 | |
| 57 | typedef struct { |
| 58 | int ok; /* 1 = an HTTP response was received */ |
| 59 | char error[512]; |
| 60 | int status; |
| 61 | char content_type[128]; |
| 62 | char body[WH_PREVIEW_CAP + 1]; |
| 63 | size_t body_len; |
| 64 | size_t body_total; /* may exceed body_len when truncated */ |
| 65 | int truncated; |
| 66 | } wh_response; |
| 67 | |
| 68 | /* Parse http(s)://host[:port][/path][?query]. Refuses userinfo, IPv6 |
| 69 | * literals, control characters and an empty host. 0 ok, -1 with err. */ |
| 70 | int wh_url_parse(const char *url, wh_url *out, char *err, size_t errsz); |
| 71 | |
| 72 | /* The URL with any query string and credentials removed, so a redacted |
| 73 | * summary can say where a hook points without leaking a token that a user |
| 74 | * pasted into the query string. Always writes something. */ |
| 75 | void wh_url_redact(const wh_url *u, char *out, size_t outsz); |
| 76 | |
| 77 | /* 1 when `ip_text` (IPv4 or IPv6 in text form) is loopback / private / |
| 78 | * link-local / CGNAT / multicast / documentation / reserved. 0 when public. |
| 79 | * An unparseable string returns 1 (deny), so a bug cannot open a hole. */ |
| 80 | int wh_ip_is_denied(const char *ip_text); |
| 81 | |
| 82 | /* 1 when the environment lifts the SSRF policy (WEBHOOK_ALLOW_PRIVATE=1). */ |
| 83 | int wh_allow_private(void); |
| 84 | |
| 85 | /* Perform one request. `hdrs` may be NULL. `body` may be NULL with blen 0. |
| 86 | * Returns 0 when a response arrived (r->ok = 1, r->status set even for 4xx/5xx) |
| 87 | * or -1 with r->error set when the request could not be completed. */ |
| 88 | int wh_http_request(const char *method, const wh_url *u, |
| 89 | const wh_header *hdrs, int nhdrs, |
| 90 | const char *body, size_t blen, |
| 91 | long timeout_ms, wh_response *r); |
| 92 | |
| 93 | /* Method validation: GET POST PUT PATCH DELETE HEAD, upper-cased. */ |
| 94 | int wh_method_ok(const char *method); |
| 95 | |
| 96 | #endif /* SX_WH_HTTP_H */ |