scorpiox-bot-plugin-fetch
←fetch_http.hc
63 lines · 2 KBHEADBlameDownload
1/*
2 * fetch_http.h - URL parse + SSRF policy + HTTP/HTTPS GET with redirects.
3 *
4 * One GET exchange (no keep-alive; Connection: close), up to 5 redirects.
5 * Every hop (and every resolved address of every hop) is checked against the
6 * private/special-purpose denylist BEFORE connect, and the socket is then
7 * connected to that exact validated address, so DNS cannot answer a different
8 * (private) IP between the check and the connect (DNS-rebinding defence).
9 *
10 * TLS on POSIX: vendored mbedTLS over our own sockets, VERIFY_PEER against a
11 * CA bundle (FETCH_TLS_CA_FILE, else the first system bundle found). There is
12 * deliberately NO insecure bypass. On _WIN32 the whole HTTP leg goes through
13 * WinHTTP (system root store).
14 *
15 * Implementation in fetch_http.c (+ fetch_tls.c for the mbedTLS BIO layer).
16 */
17#ifndef SX_FETCH_HTTP_H
18#define SX_FETCH_HTTP_H
19
20#include <stddef.h>
21
22#include "sxbuf.h"
23
24/* Hard cap on the response body we are willing to read, like upstream's
25 * MAX_RESPONSE_BYTES. A larger declared body is refused without reading it. */
26#define FX_MAX_BODY (5u * 1024u * 1024u)
27#define FX_MAX_HEAD (64u * 1024u)
28#define FX_MAX_REDIRECTS 5
29#define FX_CONNECT_MS 10000
30#define FX_READ_MS 15000
31
32typedef struct {
33 int ok; /* 1 = response delivered */
34 char error[512];
35 int status; /* HTTP status of the final response */
36 char url[1024]; /* final URL after redirects */
37 char first_url[1024]; /* URL we were first asked for */
38 char content_type[128];
39 int hops; /* redirects followed */
40 char *body; /* heap, NUL-terminated */
41 size_t body_len;
42} fx_response;
43
44void fx_response_free(fx_response *r);
45
46/* Perform GET url (scheme http|https only). Returns 0 on success (response
47 * fields filled) or -1 with r->error set. */
48int fx_fetch(const char *url, fx_response *r);
49
50/* SSRF: returns 1 when `ip_text` (IPv4 or IPv6 in text form) is a private /
51 * loopback / link-local / special-purpose address that must be refused,
52 * 0 when public. Mirrors the upstream _NON_PUBLIC_NETWORKS list plus its
53 * documented public exceptions. */
54int fx_ip_is_denied(const char *ip_text);
55
56/* Whether a body with no declared Content-Type opens as an HTML document
57 * (after BOM/whitespace: "<!doctype html" or "<html"). */
58int fx_body_looks_like_html(const char *body, size_t len);
59
60/* Helper shared with fetch_md: heap copy + NUL terminate. */
61char *fx_strndup(const char *s, size_t n);
62
63#endif /* SX_FETCH_HTTP_H */