v1.2.1 - Handle NDKs pings of kind 99999

This commit is contained in:
Your Name
2026-02-03 13:19:33 -04:00
parent 18a7deec54
commit 086d2af56c
3 changed files with 66 additions and 10 deletions

View File

@@ -975,11 +975,15 @@ static int nostr_relay_callback(struct lws *wsi, enum lws_callback_reasons reaso
// Validate filters before processing
char filter_error[512] = {0};
if (!validate_filter_array(filters, filter_error, sizeof(filter_error))) {
int validation_result = validate_filter_array(filters, filter_error, sizeof(filter_error));
if (validation_result <= 0) {
DEBUG_TRACE("REQ rejected: filter validation failed - %s", filter_error);
send_notice_message(wsi, pss, filter_error);
DEBUG_WARN("REQ rejected: invalid filters");
record_malformed_request(pss);
// Only record as malformed if it's a true error (0), not benign error (-1)
if (validation_result == 0) {
record_malformed_request(pss);
}
cJSON_Delete(filters);
cJSON_Delete(json);
// Note: complete_message points to reassembly_buffer, which is managed separately
@@ -1060,10 +1064,14 @@ static int nostr_relay_callback(struct lws *wsi, enum lws_callback_reasons reaso
// Validate filters before processing
char filter_error[512] = {0};
if (!validate_filter_array(filters, filter_error, sizeof(filter_error))) {
int validation_result = validate_filter_array(filters, filter_error, sizeof(filter_error));
if (validation_result <= 0) {
send_notice_message(wsi, pss, filter_error);
DEBUG_WARN("COUNT rejected: invalid filters");
record_malformed_request(pss);
// Only record as malformed if it's a true error (0), not benign error (-1)
if (validation_result == 0) {
record_malformed_request(pss);
}
cJSON_Delete(filters);
cJSON_Delete(json);
// Note: complete_message points to reassembly_buffer, which is managed separately
@@ -1673,11 +1681,15 @@ static int nostr_relay_callback(struct lws *wsi, enum lws_callback_reasons reaso
// Validate filters before processing
char filter_error[512] = {0};
if (!validate_filter_array(filters, filter_error, sizeof(filter_error))) {
int validation_result = validate_filter_array(filters, filter_error, sizeof(filter_error));
if (validation_result <= 0) {
DEBUG_TRACE("REQ rejected: filter validation failed - %s", filter_error);
send_notice_message(wsi, pss, filter_error);
DEBUG_WARN("REQ rejected: invalid filters");
record_malformed_request(pss);
// Only record as malformed if it's a true error (0), not benign error (-1)
if (validation_result == 0) {
record_malformed_request(pss);
}
cJSON_Delete(filters);
cJSON_Delete(json);
free(message);
@@ -1756,10 +1768,14 @@ static int nostr_relay_callback(struct lws *wsi, enum lws_callback_reasons reaso
// Validate filters before processing
char filter_error[512] = {0};
if (!validate_filter_array(filters, filter_error, sizeof(filter_error))) {
int validation_result = validate_filter_array(filters, filter_error, sizeof(filter_error));
if (validation_result <= 0) {
send_notice_message(wsi, pss, filter_error);
DEBUG_WARN("COUNT rejected: invalid filters");
record_malformed_request(pss);
// Only record as malformed if it's a true error (0), not benign error (-1)
if (validation_result == 0) {
record_malformed_request(pss);
}
cJSON_Delete(filters);
cJSON_Delete(json);
free(message);
@@ -2871,6 +2887,10 @@ int is_valid_hex_string(const char* str, size_t expected_len) {
/**
* Validate a filter array for REQ and COUNT messages
* Returns:
* 1 = valid
* 0 = invalid (malformed, should count toward rate limit)
* -1 = invalid but benign (e.g., kind 99999 from NDK ping, should not count toward rate limit)
*/
int validate_filter_array(cJSON* filters, char* error_message, size_t error_size) {
if (!filters || !cJSON_IsArray(filters)) {
@@ -2884,6 +2904,8 @@ int validate_filter_array(cJSON* filters, char* error_message, size_t error_size
return 0;
}
int has_kind_99999 = 0; // Track if we encounter kind 99999 (NDK ping)
// Validate each filter object
for (int i = 0; i < filter_count; i++) {
cJSON* filter = cJSON_GetArrayItem(filters, i);
@@ -2964,6 +2986,15 @@ int validate_filter_array(cJSON* filters, char* error_message, size_t error_size
return 0;
}
int kind_val = (int)cJSON_GetNumberValue(kind);
// Special case: kind 99999 is used by NDK (Nostr Development Kit) for ping/connectivity checks
// We reject it but don't count it as a malformed request to avoid rate limiting NDK clients
if (kind_val == 99999) {
has_kind_99999 = 1;
snprintf(error_message, error_size, "error: invalid kind value %d (NDK ping)", kind_val);
continue; // Continue checking other kinds
}
if (kind_val < 0 || kind_val > MAX_KIND_VALUE) {
snprintf(error_message, error_size, "error: invalid kind value %d", kind_val);
return 0;
@@ -3041,5 +3072,10 @@ int validate_filter_array(cJSON* filters, char* error_message, size_t error_size
}
}
// If we found kind 99999 (NDK ping), return -1 to indicate benign error
if (has_kind_99999) {
return -1;
}
return 1; // All filters valid
}