server : validate --tools CLI argument against known tool names (#22538)
Previously, unknown tool names passed via --tools were silently ignored. Now the server validates each tool name at startup and exits with an error if an unrecognized tool is specified, listing the available tools. Assisted-by: llama.cpp:local pi
This commit is contained in:
@@ -10,6 +10,7 @@
|
|||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <climits>
|
#include <climits>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
@@ -744,6 +745,24 @@ void server_tools::setup(const std::vector<std::string> & enabled_tools) {
|
|||||||
std::unordered_set<std::string> enabled_set(enabled_tools.begin(), enabled_tools.end());
|
std::unordered_set<std::string> enabled_set(enabled_tools.begin(), enabled_tools.end());
|
||||||
auto all_tools = build_tools();
|
auto all_tools = build_tools();
|
||||||
|
|
||||||
|
// collect all known tool names for validation
|
||||||
|
std::vector<std::string> known_names;
|
||||||
|
known_names.reserve(all_tools.size());
|
||||||
|
for (const auto & t : all_tools) {
|
||||||
|
known_names.push_back(t->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
// validate that every requested tool is known
|
||||||
|
for (const auto & name : enabled_tools) {
|
||||||
|
if (name == "all") continue;
|
||||||
|
if (std::find(known_names.begin(), known_names.end(), name) == known_names.end()) {
|
||||||
|
throw std::runtime_error(string_format(
|
||||||
|
"unknown tool \"%s\". available tools: %s",
|
||||||
|
name.c_str(),
|
||||||
|
string_join(known_names, ", ").c_str()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
tools.clear();
|
tools.clear();
|
||||||
for (auto & t : all_tools) {
|
for (auto & t : all_tools) {
|
||||||
if (enabled_set.count(t->name) > 0 || enabled_set.count("all") > 0) {
|
if (enabled_set.count(t->name) > 0 || enabled_set.count("all") > 0) {
|
||||||
|
|||||||
@@ -215,7 +215,12 @@ int main(int argc, char ** argv) {
|
|||||||
}
|
}
|
||||||
// EXPERIMENTAL built-in tools
|
// EXPERIMENTAL built-in tools
|
||||||
if (!params.server_tools.empty()) {
|
if (!params.server_tools.empty()) {
|
||||||
tools.setup(params.server_tools);
|
try {
|
||||||
|
tools.setup(params.server_tools);
|
||||||
|
} catch (const std::exception & e) {
|
||||||
|
LOG_ERR("%s: tools setup failed: %s\n", __func__, e.what());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
SRV_WRN("%s", "-----------------\n");
|
SRV_WRN("%s", "-----------------\n");
|
||||||
SRV_WRN("%s", "Built-in tools are enabled, do not expose server to untrusted environments\n");
|
SRV_WRN("%s", "Built-in tools are enabled, do not expose server to untrusted environments\n");
|
||||||
SRV_WRN("%s", "This feature is EXPERIMENTAL and may be changed in the future\n");
|
SRV_WRN("%s", "This feature is EXPERIMENTAL and may be changed in the future\n");
|
||||||
|
|||||||
Reference in New Issue
Block a user