|
Csilk 0.2.1
A lightweight, high-performance C HTTP web framework
|
Stateless CSRF protection middleware implementation. More...
#include <stdint.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include <unistd.h>#include "csilk/core/internal.h"#include "csilk/csilk.h"
Functions | |
| void | csilk_csrf_middleware (csilk_ctx_t *c) |
| Stateless CSRF protection middleware (cookie + header token comparison). | |
| int | csilk_csrf_generate_token (char *buf, size_t buf_size) |
| Generate a cryptographically random CSRF token. | |
Stateless CSRF protection middleware implementation.
| int csilk_csrf_generate_token | ( | char * | buf, |
| size_t | buf_size | ||
| ) |
Generate a cryptographically random CSRF token.
Reads 16 bytes from /dev/urandom and formats them as a 32-character hex string (plus null terminator). If /dev/urandom cannot be opened, falls back to a weak PRNG seeded with time XOR pid.
| buf | Output buffer to receive the null-terminated hex token. |
| buf_size | Size of the output buffer. Must be at least 33 bytes. |
| void csilk_csrf_middleware | ( | csilk_ctx_t * | c | ) |
Stateless CSRF protection middleware (cookie + header token comparison).
Stateless CSRF protection middleware.
On safe HTTP methods (GET, HEAD, OPTIONS), the middleware ensures a CSRF cookie called "csrf_token" is present (generating one if missing) and proceeds to the next handler.
On state-changing methods (POST, PUT, DELETE, etc.), it validates the X-CSRF-Token request header against the csrf_token cookie. If the tokens do not match or the header is absent, a 403 Forbidden response is returned and the pipeline is aborted.
| c | The request context. |