Zero-copy FTP/HTTP Daemon compatible with all POSIX systems
| 1 | /* |
| 2 | MIT License |
| 3 | |
| 4 | Copyright (c) 2026 Seregon |
| 5 | |
| 6 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | of this software and associated documentation files (the "Software"), to deal |
| 8 | in the Software without restriction, including without limitation the rights |
| 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | copies of the Software, and to permit persons to whom the Software is |
| 11 | furnished to do so, subject to the following conditions: |
| 12 | |
| 13 | The above copyright notice and this permission notice shall be included in all |
| 14 | copies or substantial portions of the Software. |
| 15 | |
| 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | /** |
| 26 | * @file http_csrf.c |
| 27 | * @brief CSRF Protection Implementation |
| 28 | */ |
| 29 | |
| 30 | #include "http_csrf.h" |
| 31 | #include "http_config.h" |
| 32 | #include <fcntl.h> |
| 33 | #include <stdio.h> |
| 34 | #include <string.h> |
| 35 | #include <unistd.h> |
| 36 | |
| 37 | #if ENABLE_WEB_UPLOAD |
| 38 | |
| 39 | /* Global CSRF token storage (32 hex chars + null) */ |
| 40 | static char g_csrf_token[HTTP_CSRF_TOKEN_LENGTH + 1]; |
| 41 | static int g_csrf_initialized = 0; |
| 42 | |
| 43 | int http_csrf_init(void) { |
| 44 | g_csrf_token[0] = '\0'; |
| 45 | g_csrf_initialized = 0; |
| 46 | int fd = open("/dev/urandom", O_RDONLY); |
| 47 | if (fd < 0) { |
| 48 | /* |
| 49 | * VULN-03 fix: fail hard instead of falling back to a |
| 50 | * predictable token. Empty token -> all uploads rejected. |
| 51 | * |
| 52 | * BEFORE: snprintf(g_csrf_token, ..., "0123456789abcdef..."); |
| 53 | * AFTER: g_csrf_token[0] = '\0'; (all requests fail) |
| 54 | */ |
| 55 | g_csrf_token[0] = '\0'; |
| 56 | return -1; |
| 57 | } |
| 58 | |
| 59 | unsigned char random_bytes[HTTP_CSRF_TOKEN_LENGTH / 2]; |
| 60 | if (read(fd, random_bytes, sizeof(random_bytes)) != sizeof(random_bytes)) { |
| 61 | close(fd); |
| 62 | /* Partial read -> unusable entropy, fail hard */ |
| 63 | g_csrf_token[0] = '\0'; |
| 64 | return -1; |
| 65 | } |
| 66 | close(fd); |
| 67 | |
| 68 | /* Convert to hex string */ |
| 69 | for (int i = 0; i < HTTP_CSRF_TOKEN_LENGTH / 2; i++) { |
| 70 | size_t offset = (size_t)i * 2U; |
| 71 | size_t remaining = sizeof(g_csrf_token) - offset; |
| 72 | if (remaining < 3U) { |
| 73 | /* Buffer exhausted during conversion, fail hard */ |
| 74 | g_csrf_token[0] = '\0'; |
| 75 | return -1; |
| 76 | } |
| 77 | int n = snprintf(g_csrf_token + offset, remaining, "%02x", random_bytes[i]); |
| 78 | if ((n < 0) || (n >= (int)remaining)) { |
| 79 | g_csrf_token[0] = '\0'; |
| 80 | return -1; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | g_csrf_initialized = 1; |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | const char *http_csrf_get_token(void) { return g_csrf_token; } |
| 89 | |
| 90 | int http_csrf_validate(const http_request_t *req) { |
| 91 | if (req == NULL) { |
| 92 | return -1; |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * Phase 1.2: use explicit initialization flag instead of |
| 97 | * testing token emptiness. More readable and unforgeable. |
| 98 | */ |
| 99 | if (g_csrf_initialized == 0) { |
| 100 | return -1; |
| 101 | } |
| 102 | |
| 103 | /* Look for X-CSRF-Token header */ |
| 104 | const char *token = NULL; |
| 105 | for (size_t i = 0; i < req->num_headers; i++) { |
| 106 | if (strcasecmp(req->headers[i].name, "X-CSRF-Token") == 0) { |
| 107 | token = req->headers[i].value; |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | if (token == NULL) { |
| 113 | return -1; |
| 114 | } |
| 115 | |
| 116 | if (strcmp(token, g_csrf_token) != 0) { |
| 117 | return -1; |
| 118 | } |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | #else |
| 124 | |
| 125 | /* No-op implementations when upload is disabled */ |
| 126 | void http_csrf_init(void) {} |
| 127 | const char *http_csrf_get_token(void) { return ""; } |
| 128 | int http_csrf_validate(const http_request_t *req) { |
| 129 | (void)req; |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | #endif /* ENABLE_WEB_UPLOAD */ |
| 134 |