13static unsigned char ascii_lower(
unsigned char c) {
14 return c >=
'A' &&
c <=
'Z' ? (
unsigned char)(
c + (
'a' -
'A')) :
c;
28static const waf_signature waf_signatures[] = {{
"<script", 7},
42#define WAF_MAX_PATTERNS 64
43#define WAF_MAX_TEXT 4096
44#define WAF_ALPHABET 128
62static void waf_build(
void) {
66 for (
size_t rule = 0;
rule <
sizeof(waf_signatures) /
sizeof(waf_signatures[0]); ++
rule) {
68 for (
size_t i = 0;
i < waf_signatures[
rule].length; ++
i) {
69 unsigned char c = (
unsigned char)waf_signatures[
rule].text[
i];
70 if (waf_goto[s][
c] == 0) {
71 if (waf_states > 255)
return;
72 waf_goto[s][
c] = (
uint8_t)waf_states++;
81 size_t qh = 0,
qt = 0;
83 if (waf_goto[0][
c]) queue[
qt++] = waf_goto[0][
c];
87 if (waf_out[waf_fail[r]]) waf_out[r] =
true;
93 while (
f && waf_goto[
f][
c] == 0)
f = waf_fail[
f];
94 waf_fail[s] = waf_goto[
f][
c];
96 waf_goto[r][
c] = waf_goto[waf_fail[r]][
c];
114 if (!
input)
return true;
117 for (
size_t i = 0;
i < length; ++
i) {
119 if (
c == 0 || (
c < 0x20U &&
c !=
'\t' &&
c !=
'\n' &&
c !=
'\r'))
return false;
126 s = waf_goto[s][ascii_lower(
c)];
127 if (waf_out[s])
return false;
144 for (
size_t i = 0;
i < length; ++
i) {
146 case '&':
extra += 4;
break;
148 case '>':
extra += 3;
break;
149 case '"':
extra += 5;
break;
150 case '\'':
extra += 4;
break;
158 for (
size_t i = 0;
i < length; ++
i) {
184 for (; header; header = header->
next) {
Memory allocation wrappers backed by libttak.
void * cwist_alloc(size_t size)
Allocate zeroed memory tracked by libttak.
cwist_error_t cwist_sstring_assign(cwist_sstring *str, const char *data)
Assign a C string.
HTTP Protocol Definitions and Helpers.
cwist_sstring * path
e.g., "/users/1"
cwist_sstring * query
e.g., "active=true" (raw)
cwist_http_header_node * headers
HTTP Response Object. Supports standard string body or Zero-Copy pointer body.
cwist_http_status_t status_code
char * data
Access directly only when raw handling is necessary.
void(* cwist_handler_func)(cwist_http_request *req, cwist_http_response *res)
Function pointer type for HTTP route handlers.
void(* cwist_middleware_func)(cwist_http_request *req, cwist_http_response *res, cwist_handler_func next)
Middleware type that receives req/res pair and the next stage in the chain.
cwist_middleware_func cwist_mw_waf_lite(void)
Construct the WAF-lite middleware.
char * cwist_sanitize_html(const char *input)
Escape HTML-special characters in a NUL-terminated string.
bool cwist_waf_is_safe(const char *input, size_t length)
Check whether a byte string is free of hostile signatures.
Deterministic request firewall and output sanitization.