-
Notifications
You must be signed in to change notification settings - Fork 20
skip cert when local #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |||||||||||||||||||||||
| #include <esp_tls.h> | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| static const char* TAG = "remote"; | ||||||||||||||||||||||||
| bool is_local_address(const char* url); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| struct remote_state { | ||||||||||||||||||||||||
| void* buf; | ||||||||||||||||||||||||
|
|
@@ -155,9 +156,15 @@ int remote_get(const char* url, uint8_t** buf, size_t* len, int* b_int, int32_t* | |||||||||||||||||||||||
| .event_handler = _httpCallback, | ||||||||||||||||||||||||
| .user_data = &state, | ||||||||||||||||||||||||
| .timeout_ms = 10e3, | ||||||||||||||||||||||||
| .crt_bundle_attach = esp_crt_bundle_attach, | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if (is_local_address(url)) { | ||||||||||||||||||||||||
| ESP_LOGI(TAG, "local address, skipping cert validation"); | ||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||
| config.crt_bundle_attach = esp_crt_bundle_attach; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
Comment on lines
+162
to
+166
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While the conditional check for local addresses is a step in the right direction, simply skipping certificate verification is still insecure. Explore alternative, secure methods for identifying local connections. For example, you could check for a specific local IP address range or use a more robust method to verify the identity of the server. What are the implications of this approach in a production environment?
Suggested change
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| esp_http_client_handle_t http = esp_http_client_init(&config); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Do the request | ||||||||||||||||||||||||
|
|
@@ -191,3 +198,57 @@ int remote_get(const char* url, uint8_t** buf, size_t* len, int* b_int, int32_t* | |||||||||||||||||||||||
| ESP_LOGI(TAG,"fetched new webp"); | ||||||||||||||||||||||||
| return 0; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| bool is_local_address(const char* url) { | ||||||||||||||||||||||||
| // Skip protocol prefix if present | ||||||||||||||||||||||||
| const char* http_prefix = "http://"; | ||||||||||||||||||||||||
| const char* https_prefix = "https://"; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if (strncmp(url, http_prefix, strlen(http_prefix)) == 0) { | ||||||||||||||||||||||||
| url += strlen(http_prefix); | ||||||||||||||||||||||||
| } else if (strncmp(url, https_prefix, strlen(https_prefix)) == 0) { | ||||||||||||||||||||||||
| url += strlen(https_prefix); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Make a copy of the host part (without the port) | ||||||||||||||||||||||||
| char host[256]; | ||||||||||||||||||||||||
| strncpy(host, url, sizeof(host) - 1); | ||||||||||||||||||||||||
| host[sizeof(host) - 1] = '\0'; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Remove port number if present | ||||||||||||||||||||||||
| char* port = strchr(host, ':'); | ||||||||||||||||||||||||
| if (port != NULL) { | ||||||||||||||||||||||||
| *port = '\0'; // Terminate string at the colon | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Check for .local TLD | ||||||||||||||||||||||||
| size_t len = strlen(host); | ||||||||||||||||||||||||
| if (len >= 6 && strcmp(host + len - 6, ".local") == 0) { | ||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Check if starts with common local IP prefixes | ||||||||||||||||||||||||
| if (strncmp(host, "10.", 3) == 0) { | ||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TLS certificates for local IP addresses? This doesn't make a lot of sense.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, any ip should just return true i guess.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Huh? You want
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand that, but that flag is still only meant for testing and should not be set by default.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so what's the solution for someone who wants to use https on a local address ?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Either use a valid certificate from a default CA (e.g. using the DNS-01 challenge), provide a custom CA certificate to the firmware, or use HTTP (without the S). I personally use the first option, for example. |
||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if (strncmp(host, "192.168.", 8) == 0) { | ||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if (strncmp(host, "172.", 4) == 0) { | ||||||||||||||||||||||||
| // Check for 172.16.0.0 to 172.31.255.255 range | ||||||||||||||||||||||||
| char* end; | ||||||||||||||||||||||||
| long second_octet = strtol(host + 4, &end, 10); | ||||||||||||||||||||||||
| if (*end == '.' && second_octet >= 16 && second_octet <= 31) { | ||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if (strncmp(host, "127.", 4) == 0) { | ||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
Comment on lines
+203
to
+261
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The bool is_local_address(const char* url) {
// Skip protocol prefix if present
const char* http_prefix = "http://";
const char* https_prefix = "https://";
if (strncmp(url, http_prefix, strlen(http_prefix)) == 0) {
url += strlen(http_prefix);
} else if (strncmp(url, https_prefix, strlen(https_prefix)) == 0) {
url += strlen(https_prefix);
}
// Make a copy of the host part (without the port)
char host[256];
if (strncpy(host, url, sizeof(host) - 1) == NULL) { //Error check
ESP_LOGE(TAG, "strncpy failed");
return false;
}
host[sizeof(host) - 1] = '\0';
// Remove port number if present
char* port = strchr(host, ':');
if (port != NULL) {
*port = '\0'; // Terminate string at the colon
}
// Check for .local TLD
size_t len = strlen(host);
if (len >= 6 && strcmp(host + len - 6, ".local") == 0) {
return true;
}
// Check if starts with common local IP prefixes
char *end;
long ip_num = strtol(host, &end, 10);
if (*end != '.' || errno == ERANGE) { //Error check
ESP_LOGE(TAG, "strtol failed");
return false;
}
// ... (rest of the IP address checks with error handling)
return false;
} |
||||||||||||||||||||||||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setting
CONFIG_ESP_TLS_INSECURE=yandCONFIG_ESP_TLS_SKIP_SERVER_CERT_VERIFY=yglobally is a major security risk. This should not be done globally. Consider using environment variables or build flags to control this setting for specific builds or configurations. How will this be controlled in production?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
of course it's gonna say that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested it with just CONFIG_ESP_TLS_SKIP_SERVER_CERT_VERIFY=y and it still fails to fetch https over LAN.