/* * wh_http.h - one outbound HTTP request with SSRF policy and verified TLS. * * The connector sends a user-defined request (method, URL, headers, body) to a * user-defined endpoint. Two things make that safe enough to expose as a tool: * * 1. SSRF policy. Every address the hostname resolves to is checked against a * deny list of loopback / RFC1918 / link-local / ULA / CGNAT / multicast / * documentation / reserved ranges BEFORE connecting, and the connection is * then made to that exact validated numeric address. A DNS answer that * changes between the check and the connect therefore cannot move the * request onto a private network. WEBHOOK_ALLOW_PRIVATE=1 lifts the policy * for an operator who genuinely means to call their own LAN (the offline * suite uses it to reach its recording receiver on 127.0.0.1). * * 2. Verified TLS. POSIX wraps the validated socket in mbedTLS (TLS 1.2+, * VERIFY_PEER, hostname check, system or WEBHOOK_TLS_CA_FILE anchors) with * no insecure bypass. _WIN32 uses WinHTTP with the system root store; it * connects by NAME rather than by the validated numeric address because * WinHTTP derives both SNI and the certificate hostname it verifies from * the name given to WinHttpConnect(), so an IP connection would refuse * every ordinary name-based certificate. The all-answers SSRF check still * runs first, and verified TLS refuses a public name whose peer cannot * present a certificate for it. * * Redirects are NOT followed: a hook's URL is part of its definition, so a * 30x is reported as a status instead of silently becoming a request to * somewhere the operator never configured. * * MIT License. Copyright (c) 2026 SCORPIOX INC. */ #ifndef SX_WH_HTTP_H #define SX_WH_HTTP_H #include #define WH_URL_MAX 2048 #define WH_PREVIEW_CAP 4096 /* bytes of the response body we keep */ #define WH_HEAD_CAP (64u * 1024u) #define WH_BODY_CAP (1u * 1024u * 1024u) /* outbound body limit */ #define WH_CONNECT_MS 10000 #define WH_READ_MS 15000 typedef struct { char scheme[8]; /* "http" | "https" */ char host[256]; int port; char path[WH_URL_MAX]; /* path + query; "/" when absent */ int tls; } wh_url; typedef struct { char name[128]; char value[1024]; } wh_header; typedef struct { int ok; /* 1 = an HTTP response was received */ char error[512]; int status; char content_type[128]; char body[WH_PREVIEW_CAP + 1]; size_t body_len; size_t body_total; /* may exceed body_len when truncated */ int truncated; } wh_response; /* Parse http(s)://host[:port][/path][?query]. Refuses userinfo, IPv6 * literals, control characters and an empty host. 0 ok, -1 with err. */ int wh_url_parse(const char *url, wh_url *out, char *err, size_t errsz); /* The URL with any query string and credentials removed, so a redacted * summary can say where a hook points without leaking a token that a user * pasted into the query string. Always writes something. */ void wh_url_redact(const wh_url *u, char *out, size_t outsz); /* 1 when `ip_text` (IPv4 or IPv6 in text form) is loopback / private / * link-local / CGNAT / multicast / documentation / reserved. 0 when public. * An unparseable string returns 1 (deny), so a bug cannot open a hole. */ int wh_ip_is_denied(const char *ip_text); /* 1 when the environment lifts the SSRF policy (WEBHOOK_ALLOW_PRIVATE=1). */ int wh_allow_private(void); /* Perform one request. `hdrs` may be NULL. `body` may be NULL with blen 0. * Returns 0 when a response arrived (r->ok = 1, r->status set even for 4xx/5xx) * or -1 with r->error set when the request could not be completed. */ int wh_http_request(const char *method, const wh_url *u, const wh_header *hdrs, int nhdrs, const char *body, size_t blen, long timeout_ms, wh_response *r); /* Method validation: GET POST PUT PATCH DELETE HEAD, upper-cased. */ int wh_method_ok(const char *method); #endif /* SX_WH_HTTP_H */