chore: avoid hardcoding file paths via MIMI_SPIFFS_BASE
Use MIMI_SPIFFS_BASE to centralize file path definitions, making the base path configurable instead of hardcoded. Signed-off-by: Bin Meng <bmeng.cn@gmail.com>
This commit is contained in:
@@ -15,12 +15,17 @@ static const char *TAG = "tool_files";
|
||||
#define MAX_FILE_SIZE (32 * 1024)
|
||||
|
||||
/**
|
||||
* Validate that a path starts with /spiffs/ and contains no ".." traversal.
|
||||
* Validate that a path starts with MIMI_SPIFFS_BASE and contains no ".." traversal.
|
||||
*/
|
||||
static bool validate_path(const char *path)
|
||||
{
|
||||
if (!path) return false;
|
||||
if (strncmp(path, "/spiffs/", 8) != 0) return false;
|
||||
size_t base_len = strlen(MIMI_SPIFFS_BASE);
|
||||
if (strncmp(path, MIMI_SPIFFS_BASE, base_len) != 0) return false;
|
||||
/* Require a path separator after the base (unless base ends with '/') */
|
||||
if (base_len > 0 && MIMI_SPIFFS_BASE[base_len - 1] != '/') {
|
||||
if (path[base_len] != '/') return false;
|
||||
}
|
||||
if (strstr(path, "..") != NULL) return false;
|
||||
return true;
|
||||
}
|
||||
@@ -37,7 +42,7 @@ esp_err_t tool_read_file_execute(const char *input_json, char *output, size_t ou
|
||||
|
||||
const char *path = cJSON_GetStringValue(cJSON_GetObjectItem(root, "path"));
|
||||
if (!validate_path(path)) {
|
||||
snprintf(output, output_size, "Error: path must start with /spiffs/ and must not contain '..'");
|
||||
snprintf(output, output_size, "Error: path must start with %s/ and must not contain '..'", MIMI_SPIFFS_BASE);
|
||||
cJSON_Delete(root);
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
@@ -75,7 +80,7 @@ esp_err_t tool_write_file_execute(const char *input_json, char *output, size_t o
|
||||
const char *content = cJSON_GetStringValue(cJSON_GetObjectItem(root, "content"));
|
||||
|
||||
if (!validate_path(path)) {
|
||||
snprintf(output, output_size, "Error: path must start with /spiffs/ and must not contain '..'");
|
||||
snprintf(output, output_size, "Error: path must start with %s/ and must not contain '..'", MIMI_SPIFFS_BASE);
|
||||
cJSON_Delete(root);
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
@@ -123,7 +128,7 @@ esp_err_t tool_edit_file_execute(const char *input_json, char *output, size_t ou
|
||||
const char *new_str = cJSON_GetStringValue(cJSON_GetObjectItem(root, "new_string"));
|
||||
|
||||
if (!validate_path(path)) {
|
||||
snprintf(output, output_size, "Error: path must start with /spiffs/ and must not contain '..'");
|
||||
snprintf(output, output_size, "Error: path must start with %s/ and must not contain '..'", MIMI_SPIFFS_BASE);
|
||||
cJSON_Delete(root);
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
@@ -226,7 +231,7 @@ esp_err_t tool_list_dir_execute(const char *input_json, char *output, size_t out
|
||||
|
||||
DIR *dir = opendir(MIMI_SPIFFS_BASE);
|
||||
if (!dir) {
|
||||
snprintf(output, output_size, "Error: cannot open /spiffs directory");
|
||||
snprintf(output, output_size, "Error: cannot open %s directory", MIMI_SPIFFS_BASE);
|
||||
cJSON_Delete(root);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
@@ -5,24 +5,24 @@
|
||||
|
||||
/**
|
||||
* Read a file from SPIFFS.
|
||||
* Input JSON: {"path": "/spiffs/..."}
|
||||
* Input JSON: {"path": "<MIMI_SPIFFS_BASE>/..."}
|
||||
*/
|
||||
esp_err_t tool_read_file_execute(const char *input_json, char *output, size_t output_size);
|
||||
|
||||
/**
|
||||
* Write/overwrite a file on SPIFFS.
|
||||
* Input JSON: {"path": "/spiffs/...", "content": "..."}
|
||||
* Input JSON: {"path": "<MIMI_SPIFFS_BASE>/...", "content": "..."}
|
||||
*/
|
||||
esp_err_t tool_write_file_execute(const char *input_json, char *output, size_t output_size);
|
||||
|
||||
/**
|
||||
* Find-and-replace edit a file on SPIFFS.
|
||||
* Input JSON: {"path": "/spiffs/...", "old_string": "...", "new_string": "..."}
|
||||
* Input JSON: {"path": "<MIMI_SPIFFS_BASE>/...", "old_string": "...", "new_string": "..."}
|
||||
*/
|
||||
esp_err_t tool_edit_file_execute(const char *input_json, char *output, size_t output_size);
|
||||
|
||||
/**
|
||||
* List files on SPIFFS, optionally filtered by path prefix.
|
||||
* Input JSON: {"prefix": "/spiffs/..."} (prefix is optional)
|
||||
* Input JSON: {"prefix": "<MIMI_SPIFFS_BASE>/..."} (prefix is optional)
|
||||
*/
|
||||
esp_err_t tool_list_dir_execute(const char *input_json, char *output, size_t output_size);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "tool_registry.h"
|
||||
#include "mimi_config.h"
|
||||
#include "tools/tool_web_search.h"
|
||||
#include "tools/tool_get_time.h"
|
||||
#include "tools/tool_files.h"
|
||||
@@ -83,10 +84,10 @@ esp_err_t tool_registry_init(void)
|
||||
/* Register read_file */
|
||||
mimi_tool_t rf = {
|
||||
.name = "read_file",
|
||||
.description = "Read a file from SPIFFS storage. Path must start with /spiffs/.",
|
||||
.description = "Read a file from SPIFFS storage. Path must start with " MIMI_SPIFFS_BASE "/.",
|
||||
.input_schema_json =
|
||||
"{\"type\":\"object\","
|
||||
"\"properties\":{\"path\":{\"type\":\"string\",\"description\":\"Absolute path starting with /spiffs/\"}},"
|
||||
"\"properties\":{\"path\":{\"type\":\"string\",\"description\":\"Absolute path starting with " MIMI_SPIFFS_BASE "/\"}},"
|
||||
"\"required\":[\"path\"]}",
|
||||
.execute = tool_read_file_execute,
|
||||
};
|
||||
@@ -95,10 +96,10 @@ esp_err_t tool_registry_init(void)
|
||||
/* Register write_file */
|
||||
mimi_tool_t wf = {
|
||||
.name = "write_file",
|
||||
.description = "Write or overwrite a file on SPIFFS storage. Path must start with /spiffs/.",
|
||||
.description = "Write or overwrite a file on SPIFFS storage. Path must start with " MIMI_SPIFFS_BASE "/.",
|
||||
.input_schema_json =
|
||||
"{\"type\":\"object\","
|
||||
"\"properties\":{\"path\":{\"type\":\"string\",\"description\":\"Absolute path starting with /spiffs/\"},"
|
||||
"\"properties\":{\"path\":{\"type\":\"string\",\"description\":\"Absolute path starting with " MIMI_SPIFFS_BASE "/\"},"
|
||||
"\"content\":{\"type\":\"string\",\"description\":\"File content to write\"}},"
|
||||
"\"required\":[\"path\",\"content\"]}",
|
||||
.execute = tool_write_file_execute,
|
||||
@@ -111,7 +112,7 @@ esp_err_t tool_registry_init(void)
|
||||
.description = "Find and replace text in a file on SPIFFS. Replaces first occurrence of old_string with new_string.",
|
||||
.input_schema_json =
|
||||
"{\"type\":\"object\","
|
||||
"\"properties\":{\"path\":{\"type\":\"string\",\"description\":\"Absolute path starting with /spiffs/\"},"
|
||||
"\"properties\":{\"path\":{\"type\":\"string\",\"description\":\"Absolute path starting with " MIMI_SPIFFS_BASE "/\"},"
|
||||
"\"old_string\":{\"type\":\"string\",\"description\":\"Text to find\"},"
|
||||
"\"new_string\":{\"type\":\"string\",\"description\":\"Replacement text\"}},"
|
||||
"\"required\":[\"path\",\"old_string\",\"new_string\"]}",
|
||||
@@ -125,7 +126,7 @@ esp_err_t tool_registry_init(void)
|
||||
.description = "List files on SPIFFS storage, optionally filtered by path prefix.",
|
||||
.input_schema_json =
|
||||
"{\"type\":\"object\","
|
||||
"\"properties\":{\"prefix\":{\"type\":\"string\",\"description\":\"Optional path prefix filter, e.g. /spiffs/memory/\"}},"
|
||||
"\"properties\":{\"prefix\":{\"type\":\"string\",\"description\":\"Optional path prefix filter, e.g. " MIMI_SPIFFS_BASE "/memory/\"}},"
|
||||
"\"required\":[]}",
|
||||
.execute = tool_list_dir_execute,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user