v0.7.10 - Fixed api errors in accepting : in subscriptions

This commit is contained in:
Your Name
2025-10-12 10:31:03 -04:00
parent b27a56a296
commit 34bb1c34a2
27 changed files with 2293 additions and 403 deletions

View File

@@ -222,6 +222,17 @@ static int refresh_unified_cache_from_table(void) {
return -1;
}
// Log config table row count at start of refresh_unified_cache_from_table
sqlite3_stmt* count_stmt;
const char* count_sql = "SELECT COUNT(*) FROM config";
if (sqlite3_prepare_v2(g_db, count_sql, -1, &count_stmt, NULL) == SQLITE_OK) {
if (sqlite3_step(count_stmt) == SQLITE_ROW) {
int row_count = sqlite3_column_int(count_stmt, 0);
printf("[DEBUG] refresh_unified_cache_from_table: Config table row count at start: %d\n", row_count);
}
sqlite3_finalize(count_stmt);
}
// Lock the cache for update (don't memset entire cache to avoid wiping relay_info)
pthread_mutex_lock(&g_unified_cache.cache_lock);
@@ -358,6 +369,15 @@ static int refresh_unified_cache_from_table(void) {
pthread_mutex_unlock(&g_unified_cache.cache_lock);
// Log config table row count at end of refresh_unified_cache_from_table
if (sqlite3_prepare_v2(g_db, count_sql, -1, &count_stmt, NULL) == SQLITE_OK) {
if (sqlite3_step(count_stmt) == SQLITE_ROW) {
int row_count = sqlite3_column_int(count_stmt, 0);
printf("[DEBUG] refresh_unified_cache_from_table: Config table row count at end: %d\n", row_count);
}
sqlite3_finalize(count_stmt);
}
return 0;
}
@@ -414,9 +434,11 @@ char** find_existing_db_files(void) {
return NULL;
}
// Count .db files
// Count .db files (excluding auxiliary files like .db-wal, .db-shm, .db-journal)
while ((entry = readdir(dir)) != NULL) {
if (strstr(entry->d_name, ".db") != NULL) {
size_t len = strlen(entry->d_name);
// Only count files that end with exactly ".db" (not .db-wal, .db-shm, etc.)
if (len >= 3 && strcmp(entry->d_name + len - 3, ".db") == 0) {
count++;
}
}
@@ -434,10 +456,12 @@ char** find_existing_db_files(void) {
return NULL;
}
// Store filenames
// Store filenames (only files ending with exactly ".db")
int i = 0;
while ((entry = readdir(dir)) != NULL && i < count) {
if (strstr(entry->d_name, ".db") != NULL) {
size_t len = strlen(entry->d_name);
// Only accept files that end with exactly ".db"
if (len >= 3 && strcmp(entry->d_name + len - 3, ".db") == 0) {
files[i] = malloc(strlen(entry->d_name) + 1);
if (files[i]) {
strcpy(files[i], entry->d_name);
@@ -1216,6 +1240,12 @@ int first_time_startup_sequence(const cli_options_t* cli_options) {
g_unified_cache.admin_pubkey[sizeof(g_unified_cache.admin_pubkey) - 1] = '\0';
strncpy(g_unified_cache.relay_pubkey, relay_pubkey, sizeof(g_unified_cache.relay_pubkey) - 1);
g_unified_cache.relay_pubkey[sizeof(g_unified_cache.relay_pubkey) - 1] = '\0';
// Mark cache as valid to prevent premature refresh from empty database
int cache_timeout = get_cache_timeout();
g_unified_cache.cache_expires = time(NULL) + cache_timeout;
g_unified_cache.cache_valid = 1;
pthread_mutex_unlock(&g_unified_cache.cache_lock);
// 4. Create database with relay pubkey name
@@ -1272,10 +1302,29 @@ int startup_existing_relay(const char* relay_pubkey) {
printf(" Relay pubkey: %s\n", relay_pubkey);
// Log config table row count at start of startup_existing_relay
if (g_db) {
sqlite3_stmt* count_stmt;
const char* count_sql = "SELECT COUNT(*) FROM config";
if (sqlite3_prepare_v2(g_db, count_sql, -1, &count_stmt, NULL) == SQLITE_OK) {
if (sqlite3_step(count_stmt) == SQLITE_ROW) {
int row_count = sqlite3_column_int(count_stmt, 0);
printf("[DEBUG] startup_existing_relay: Config table row count at start: %d\n", row_count);
}
sqlite3_finalize(count_stmt);
}
}
// Store relay pubkey in unified cache
pthread_mutex_lock(&g_unified_cache.cache_lock);
strncpy(g_unified_cache.relay_pubkey, relay_pubkey, sizeof(g_unified_cache.relay_pubkey) - 1);
g_unified_cache.relay_pubkey[sizeof(g_unified_cache.relay_pubkey) - 1] = '\0';
// Mark cache as valid to prevent premature refresh from database before it's initialized
int cache_timeout = get_cache_timeout();
g_unified_cache.cache_expires = time(NULL) + cache_timeout;
g_unified_cache.cache_valid = 1;
pthread_mutex_unlock(&g_unified_cache.cache_lock);
// Set database path
@@ -1289,14 +1338,21 @@ int startup_existing_relay(const char* relay_pubkey) {
g_database_path[sizeof(g_database_path) - 1] = '\0';
free(db_name);
// Ensure default configuration values are populated (for any missing keys)
if (populate_default_config_values() != 0) {
log_warning("Failed to populate default config values for existing relay - continuing");
}
// NOTE: admin_pubkey will be loaded from config table after database initialization
// in main.c by calling add_pubkeys_to_config_table() which handles migration
// Configuration will be migrated from events to table after database initialization
// Load configuration event from database (after database is initialized)
// This will be done in apply_configuration_from_database()
// Log config table row count at end of startup_existing_relay
if (g_db) {
sqlite3_stmt* count_stmt;
const char* count_sql = "SELECT COUNT(*) FROM config";
if (sqlite3_prepare_v2(g_db, count_sql, -1, &count_stmt, NULL) == SQLITE_OK) {
if (sqlite3_step(count_stmt) == SQLITE_ROW) {
int row_count = sqlite3_column_int(count_stmt, 0);
printf("[DEBUG] startup_existing_relay: Config table row count at end: %d\n", row_count);
}
sqlite3_finalize(count_stmt);
}
}
return 0;
}
@@ -2011,7 +2067,9 @@ int handle_configuration_event(cJSON* event, char* error_message, size_t error_s
// Get value from config table
const char* get_config_value_from_table(const char* key) {
if (!g_db || !key) return NULL;
if (!g_db || !key) {
return NULL;
}
const char* sql = "SELECT value FROM config WHERE key = ?";
@@ -2025,7 +2083,9 @@ const char* get_config_value_from_table(const char* key) {
const char* result = NULL;
if (sqlite3_step(stmt) == SQLITE_ROW) {
int step_rc = sqlite3_step(stmt);
if (step_rc == SQLITE_ROW) {
const char* value = (char*)sqlite3_column_text(stmt, 0);
if (value) {
// Return a dynamically allocated string to prevent buffer reuse
@@ -2072,6 +2132,13 @@ int update_config_in_table(const char* key, const char* value) {
return -1;
}
// Additional validation: reject empty strings to prevent accidental deletion of config values
if (strlen(value) == 0) {
log_warning("Attempted to update config with empty value - rejecting to prevent data loss");
printf(" Rejected empty value for key: %s\n", key);
return -1;
}
const char* sql = "UPDATE config SET value = ?, updated_at = strftime('%s', 'now') WHERE key = ?";
sqlite3_stmt* stmt;
@@ -2089,15 +2156,58 @@ int update_config_in_table(const char* key, const char* value) {
return (rc == SQLITE_DONE) ? 0 : -1;
}
// Populate default config values
// Populate default config values (only inserts missing keys, doesn't replace existing)
int populate_default_config_values(void) {
log_info("Populating default configuration values in table...");
if (!g_db) {
log_error("Database not available for populating default config values");
return -1;
}
// Add all default configuration values to the table
// Log config table row count at start of populate_default_config_values
sqlite3_stmt* count_stmt;
const char* count_sql = "SELECT COUNT(*) FROM config";
if (sqlite3_prepare_v2(g_db, count_sql, -1, &count_stmt, NULL) == SQLITE_OK) {
if (sqlite3_step(count_stmt) == SQLITE_ROW) {
int row_count = sqlite3_column_int(count_stmt, 0);
printf("[DEBUG] populate_default_config_values: Config table row count at start: %d\n", row_count);
}
sqlite3_finalize(count_stmt);
}
log_info("Populating missing default configuration values in table...");
int keys_added = 0;
int keys_skipped = 0;
// Add all default configuration values to the table (only if they don't exist)
for (size_t i = 0; i < DEFAULT_CONFIG_COUNT; i++) {
const char* key = DEFAULT_CONFIG_VALUES[i].key;
const char* value = DEFAULT_CONFIG_VALUES[i].value;
// Check if key already exists in config table
const char* check_sql = "SELECT COUNT(*) FROM config WHERE key = ?";
sqlite3_stmt* check_stmt;
int check_rc = sqlite3_prepare_v2(g_db, check_sql, -1, &check_stmt, NULL);
if (check_rc != SQLITE_OK) {
log_error("Failed to prepare config existence check");
continue;
}
sqlite3_bind_text(check_stmt, 1, key, -1, SQLITE_STATIC);
int key_exists = 0;
if (sqlite3_step(check_stmt) == SQLITE_ROW) {
key_exists = sqlite3_column_int(check_stmt, 0) > 0;
}
sqlite3_finalize(check_stmt);
// Skip if key already exists (preserve existing configuration)
if (key_exists) {
keys_skipped++;
continue;
}
// Determine data type
const char* data_type = "string";
if (strcmp(key, "relay_port") == 0 ||
@@ -2150,14 +2260,61 @@ int populate_default_config_values(void) {
requires_restart = 1;
}
if (set_config_value_in_table(key, value, data_type, NULL, category, requires_restart) != 0) {
// Only insert if key doesn't exist (INSERT will fail if key exists due to UNIQUE constraint)
const char* insert_sql = "INSERT INTO config (key, value, data_type, description, category, requires_restart) "
"VALUES (?, ?, ?, ?, ?, ?)";
sqlite3_stmt* insert_stmt;
int insert_rc = sqlite3_prepare_v2(g_db, insert_sql, -1, &insert_stmt, NULL);
if (insert_rc != SQLITE_OK) {
char error_msg[256];
snprintf(error_msg, sizeof(error_msg), "Failed to set default config: %s = %s", key, value);
snprintf(error_msg, sizeof(error_msg), "Failed to prepare insert for: %s", key);
log_error(error_msg);
continue;
}
sqlite3_bind_text(insert_stmt, 1, key, -1, SQLITE_STATIC);
sqlite3_bind_text(insert_stmt, 2, value, -1, SQLITE_STATIC);
sqlite3_bind_text(insert_stmt, 3, data_type, -1, SQLITE_STATIC);
sqlite3_bind_text(insert_stmt, 4, "", -1, SQLITE_STATIC);
sqlite3_bind_text(insert_stmt, 5, category, -1, SQLITE_STATIC);
sqlite3_bind_int(insert_stmt, 6, requires_restart);
int step_rc = sqlite3_step(insert_stmt);
sqlite3_finalize(insert_stmt);
if (step_rc == SQLITE_DONE) {
keys_added++;
} else {
// Silently skip if key already exists (UNIQUE constraint violation)
if (step_rc != SQLITE_CONSTRAINT) {
char error_msg[256];
snprintf(error_msg, sizeof(error_msg), "Failed to insert default config: %s = %s (error: %s)",
key, value, sqlite3_errmsg(g_db));
log_error(error_msg);
}
}
}
log_success("Default configuration values populated with restart requirements");
if (keys_added > 0) {
char success_msg[256];
snprintf(success_msg, sizeof(success_msg),
"Added %d missing default configuration values (%d existing keys preserved)",
keys_added, keys_skipped);
log_success(success_msg);
} else if (keys_skipped > 0) {
log_info("All default configuration keys already exist - no changes needed");
}
// Log config table row count at end of populate_default_config_values
if (sqlite3_prepare_v2(g_db, count_sql, -1, &count_stmt, NULL) == SQLITE_OK) {
if (sqlite3_step(count_stmt) == SQLITE_ROW) {
int row_count = sqlite3_column_int(count_stmt, 0);
printf("[DEBUG] populate_default_config_values: Config table row count at end: %d\n", row_count);
}
sqlite3_finalize(count_stmt);
}
return 0;
}
@@ -2167,15 +2324,79 @@ int add_pubkeys_to_config_table(void) {
log_error("Database not available for pubkey storage");
return -1;
}
// Log config table row count at start of add_pubkeys_to_config_table
sqlite3_stmt* count_stmt;
const char* count_sql = "SELECT COUNT(*) FROM config";
if (sqlite3_prepare_v2(g_db, count_sql, -1, &count_stmt, NULL) == SQLITE_OK) {
if (sqlite3_step(count_stmt) == SQLITE_ROW) {
int row_count = sqlite3_column_int(count_stmt, 0);
printf("[DEBUG] add_pubkeys_to_config_table: Config table row count at start: %d\n", row_count);
}
sqlite3_finalize(count_stmt);
}
log_info("Adding dynamically generated pubkeys to config table...");
// Get the pubkeys directly from unified cache (not through cached accessors to avoid circular dependency)
pthread_mutex_lock(&g_unified_cache.cache_lock);
const char* admin_pubkey = g_unified_cache.admin_pubkey[0] ? g_unified_cache.admin_pubkey : NULL;
const char* relay_pubkey = g_unified_cache.relay_pubkey[0] ? g_unified_cache.relay_pubkey : NULL;
const char* admin_pubkey_cache = g_unified_cache.admin_pubkey[0] ? g_unified_cache.admin_pubkey : NULL;
const char* relay_pubkey_cache = g_unified_cache.relay_pubkey[0] ? g_unified_cache.relay_pubkey : NULL;
pthread_mutex_unlock(&g_unified_cache.cache_lock);
// For existing relays, admin_pubkey might already be in the database but not in cache yet
// Try to load it from the database config table first
const char* admin_pubkey_from_db = NULL;
if (!admin_pubkey_cache || strlen(admin_pubkey_cache) != 64) {
admin_pubkey_from_db = get_config_value_from_table("admin_pubkey");
if (admin_pubkey_from_db && strlen(admin_pubkey_from_db) == 64) {
// Update cache with the value from config table
pthread_mutex_lock(&g_unified_cache.cache_lock);
strncpy(g_unified_cache.admin_pubkey, admin_pubkey_from_db, sizeof(g_unified_cache.admin_pubkey) - 1);
g_unified_cache.admin_pubkey[sizeof(g_unified_cache.admin_pubkey) - 1] = '\0';
pthread_mutex_unlock(&g_unified_cache.cache_lock);
log_success("Loaded admin_pubkey from config table into cache");
// Free the allocated string and return success - pubkey already in table
free((char*)admin_pubkey_from_db);
return 0;
}
if (admin_pubkey_from_db) {
free((char*)admin_pubkey_from_db);
}
// If not in config table, try loading from old event-based config (migration scenario)
const char* sql = "SELECT pubkey FROM events WHERE kind = 33334 ORDER BY created_at DESC LIMIT 1";
sqlite3_stmt* stmt;
int rc = sqlite3_prepare_v2(g_db, sql, -1, &stmt, NULL);
if (rc == SQLITE_OK) {
if (sqlite3_step(stmt) == SQLITE_ROW) {
const char* event_pubkey = (const char*)sqlite3_column_text(stmt, 0);
if (event_pubkey && strlen(event_pubkey) == 64) {
// Store in config table for future use
if (set_config_value_in_table("admin_pubkey", event_pubkey, "string",
"Administrator public key", "authentication", 0) == 0) {
// Update cache
pthread_mutex_lock(&g_unified_cache.cache_lock);
strncpy(g_unified_cache.admin_pubkey, event_pubkey, sizeof(g_unified_cache.admin_pubkey) - 1);
g_unified_cache.admin_pubkey[sizeof(g_unified_cache.admin_pubkey) - 1] = '\0';
pthread_mutex_unlock(&g_unified_cache.cache_lock);
sqlite3_finalize(stmt);
log_success("Migrated admin_pubkey from old config event to config table");
return 0;
}
}
}
sqlite3_finalize(stmt);
}
}
// Use cache value for storage (either from first-time startup or just loaded from DB)
const char* admin_pubkey = admin_pubkey_cache;
const char* relay_pubkey = relay_pubkey_cache;
if (!admin_pubkey || strlen(admin_pubkey) != 64) {
log_error("Admin pubkey not available or invalid for config table storage");
return -1;
@@ -2188,22 +2409,31 @@ int add_pubkeys_to_config_table(void) {
// Store admin pubkey in config table
if (set_config_value_in_table("admin_pubkey", admin_pubkey, "string",
"Administrator public key", "authentication", 0) != 0) {
"Administrator public key", "authentication", 0) != 0) {
log_error("Failed to store admin_pubkey in config table");
return -1;
}
// Store relay pubkey in config table
if (set_config_value_in_table("relay_pubkey", relay_pubkey, "string",
"Relay public key", "relay", 0) != 0) {
"Relay public key", "relay", 0) != 0) {
log_error("Failed to store relay_pubkey in config table");
return -1;
}
log_success("Dynamically generated pubkeys added to config table");
printf(" Admin pubkey: %s\n", admin_pubkey);
printf(" Relay pubkey: %s\n", relay_pubkey);
printf(" Admin pubkey: %s\n", admin_pubkey ? admin_pubkey : "NULL");
printf(" Relay pubkey: %s\n", relay_pubkey ? relay_pubkey : "NULL");
// Log config table row count at end of add_pubkeys_to_config_table
if (sqlite3_prepare_v2(g_db, count_sql, -1, &count_stmt, NULL) == SQLITE_OK) {
if (sqlite3_step(count_stmt) == SQLITE_ROW) {
int row_count = sqlite3_column_int(count_stmt, 0);
printf("[DEBUG] add_pubkeys_to_config_table: Config table row count at end: %d\n", row_count);
}
sqlite3_finalize(count_stmt);
}
return 0;
}
@@ -2229,18 +2459,6 @@ int process_admin_event_in_config(cJSON* event, char* error_message, size_t erro
cJSON* pubkey_obj = cJSON_GetObjectItem(event, "pubkey");
const char* event_pubkey = pubkey_obj ? cJSON_GetStringValue(pubkey_obj) : NULL;
// DEFENSE-IN-DEPTH: Use comprehensive admin authorization validation
if (!is_authorized_admin_event(event)) {
// Log the unauthorized attempt for security monitoring
char log_msg[256];
snprintf(log_msg, sizeof(log_msg),
"Unauthorized admin event attempt in config processing - pubkey: %.16s...",
event_pubkey ? event_pubkey : "null");
log_warning(log_msg);
snprintf(error_message, error_size, "auth-required: not authorized admin");
return -1;
}
// Route to appropriate handler based on kind
switch (kind) {
@@ -2628,9 +2846,16 @@ char* encrypt_admin_response_content(const cJSON* response_data, const char* rec
}
// Perform NIP-44 encryption (relay as sender, admin as recipient)
char encrypted_content[8192]; // Buffer for encrypted content
int encrypt_result = nostr_nip44_encrypt(relay_privkey_bytes, recipient_pubkey_bytes,
response_json, encrypted_content, sizeof(encrypted_content));
// Buffer needs to accommodate: version(1) + nonce(32) + ciphertext(plaintext_size) + mac(32) + base64 overhead(~33%)
// For 5KB plaintext: (1+32+5000+32)*1.34 ≈ 6800 bytes, use 16KB to be safe
char encrypted_content[16384]; // Buffer for encrypted content (16KB)
int encrypt_result = nostr_nip44_encrypt(
relay_privkey_bytes, // sender private key
recipient_pubkey_bytes, // recipient public key
response_json, // plaintext to encrypt
encrypted_content, // output buffer
sizeof(encrypted_content) // output buffer size
);
// Clean up sensitive data immediately after use
memset(relay_privkey_bytes, 0, 32);
@@ -4553,10 +4778,11 @@ int process_startup_config_event_with_fallback(const cJSON* event) {
// If that fails, populate defaults and try again
log_warning("Startup config processing failed - ensuring defaults are populated");
if (populate_default_config_values() != 0) {
log_error("Failed to populate default config values");
return -1;
}
// COMMENTED OUT: Don't modify existing database config on restart
// if (populate_default_config_values() != 0) {
// log_error("Failed to populate default config values");
// return -1;
// }
// Retry processing
if (process_startup_config_event(event) == 0) {