Skip to content

Commit

Permalink
Don't allow 0.0.0.0 (skot#665)
Browse files Browse the repository at this point in the history
  • Loading branch information
eandersson authored Jan 27, 2025
1 parent c8671cf commit 1db16a5
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions main/http_server/http_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,11 @@ static esp_err_t ip_in_private_range(uint32_t address) {
return ESP_FAIL;
}

static uint32_t extract_origin_ip_addr(httpd_req_t *req)
static uint32_t extract_origin_ip_addr(char *origin)
{
char origin[128];
char ip_str[16];
uint32_t origin_ip_addr = 0;

// Attempt to get the Origin header.
if (httpd_req_get_hdr_value_str(req, "Origin", origin, sizeof(origin)) != ESP_OK) {
ESP_LOGD(CORS_TAG, "No origin header found.");
return 0;
}
ESP_LOGD(CORS_TAG, "Origin header: %s", origin);

// Find the start of the IP address in the Origin header
const char *prefix = "http://";
char *ip_start = strstr(origin, prefix);
Expand Down Expand Up @@ -148,8 +140,14 @@ static esp_err_t is_network_allowed(httpd_req_t * req)
// Convert to IPv4 string
inet_ntop(AF_INET, &request_ip_addr, ipstr, sizeof(ipstr));

uint32_t origin_ip_addr = extract_origin_ip_addr(req);
if (origin_ip_addr == 0) {
// Attempt to get the Origin header.
char origin[128];
uint32_t origin_ip_addr;
if (httpd_req_get_hdr_value_str(req, "Origin", origin, sizeof(origin)) == ESP_OK) {
ESP_LOGD(CORS_TAG, "Origin header: %s", origin);
origin_ip_addr = extract_origin_ip_addr(origin);
} else {
ESP_LOGD(CORS_TAG, "No origin header found.");
origin_ip_addr = request_ip_addr;
}

Expand Down

0 comments on commit 1db16a5

Please sign in to comment.