Archived
server: bound, gate and sanitize every request entry point
Prepared for exposing the dashboard and the API beyond the local machine. Each piece answers a finding from the review; none changes what a legitimate client sees. Parsing. Six entry points still called json::parse with no bound on the tree: the two WebSocket sessions, the image form's loras field, the two re-parses of template kwargs, the control-vector POST and the lora-admin body. A legal frame or body of brackets alone built a tree whose teardown recurses once per level -- one worker stack, one process. They all go through parse_json_bounded now, the body cap's own value and depth limits. The same reasoning already refused deep bodies on the generation routes; frames and form fields were the ones that never passed a gate. Errors. ApiError::dump threw on invalid UTF-8, and URL-decoded params reach messages without passing a JSON parser: a 404 becomes a 500, or worse a closed connection. The dumps replace bad bytes instead of dying. Logs. Session labels and echoed request text reach the log; an embedded newline forged a line and an escape sequence drove the terminal reading it. log_sanitize filters control bytes at the entry points -- labels, refusal logs, the exception handler -- while the three loggers that print multi-line reports deliberately keep their newlines. The browser. The dashboard page ships a CSP (its script talks only to its own origin) and X-Frame-Options DENY; every response carries X-Content-Type-Options: nosniff. And the loopback-only guards -- control-vector default, runtime LoRA, diagnostics -- now treat a cross-origin browser as the remote client it is: a hostile page reaches them through the victim's own browser, which is exactly loopback. Origin not matching Host refuses; same-origin and non-browser traffic are untouched. The gate. The API key's path exemptions are method-scoped (reads, plus the POST /ping of the health-check convention), so an exemption can never ride a method no route answers. An empty --api-key value is now a startup error rather than a server that looks guarded. SSE. In the vendored httplib 0.46 an exception escaping a content provider aborts the process; the provider ends the response instead. Upstream's 0.54 serve_guarded fixes the cause -- the vendor lift is owed either way, and the catch is harmless belt until it.
This commit is contained in:
1 parent
a28221eb6a
commit
b9c685a9c3
16 files changed
+245
-48
No files matched your search
+22
-10
@@ -163,7 +163,11 @@ std::string ApiError::dump() const {
|
||||
if (n_ctx >= 0) e["n_ctx"] = n_ctx;
|
||||
json o;
|
||||
o["error"] = e;
|
||||
return o.dump();
|
||||
/* A message can carry raw request text (a URL-decoded label, an echoed field name): dump
|
||||
* must survive invalid UTF-8 by replacing it rather than throwing and turning a 404 into
|
||||
* a 500. The JSON parsers refuse invalid UTF-8 in bodies, but URL-decoded params do not
|
||||
* pass through one. */
|
||||
return o.dump(-1, ' ', false, json::error_handler_t::replace);
|
||||
}
|
||||
|
||||
ApiError err_bad(const std::string& param, const std::string& why) {
|
||||
@@ -431,13 +435,16 @@ std::string str_or(const json& o, const char* k, const std::string& def = std::s
|
||||
}
|
||||
|
||||
/* A conversation's label for /sessions: at most 128 bytes, cut at a UTF-8 character boundary
|
||||
* because the label is serialised into JSON. */
|
||||
* because the label is serialised into JSON. Control bytes are dropped at the door: the label
|
||||
* is logged on release and shown on the dashboard, and an embedded ESC sequence would drive
|
||||
* whatever terminal renders it. */
|
||||
std::string session_label_of(const std::string& s) {
|
||||
std::string t = log_sanitize(s);
|
||||
constexpr size_t kMax = 128;
|
||||
if (s.size() <= kMax) return s;
|
||||
if (t.size() <= kMax) return t;
|
||||
size_t cut = kMax;
|
||||
while (cut > 0 && ((unsigned char)s[cut] & 0xC0) == 0x80) --cut;
|
||||
return s.substr(0, cut);
|
||||
while (cut > 0 && ((unsigned char)t[cut] & 0xC0) == 0x80) --cut;
|
||||
return t.substr(0, cut);
|
||||
}
|
||||
|
||||
/* One field under several spellings (OpenAI's, vLLM's, llama.cpp's), read as a number (double)
|
||||
@@ -1053,8 +1060,11 @@ void map_reasoning_effort(const OaiLimits& lim, ChatRenderOptions* opt) {
|
||||
if (lim.reasoning_effort_map.empty()) return;
|
||||
auto it = opt->template_kwargs.find("reasoning_effort");
|
||||
if (it == opt->template_kwargs.end()) return;
|
||||
const json v = json::parse(it->second, nullptr, false);
|
||||
if (!v.is_string()) return;
|
||||
/* The kwarg is client text re-parsed as JSON: bound it like a body, a string of brackets
|
||||
* would overflow this worker's stack when the tree comes down. */
|
||||
bool over = false, deep = false;
|
||||
const json v = parse_json_bounded(it->second, 0, &over, &deep);
|
||||
if (over || deep || !v.is_string()) return;
|
||||
auto m = lim.reasoning_effort_map.find(v.get<std::string>());
|
||||
if (m == lim.reasoning_effort_map.end()) return;
|
||||
if (m->second == "none" || m->second == "minimal") {
|
||||
@@ -1071,9 +1081,11 @@ ApiError template_refusal(const ChatRenderOptions& opt, const OaiLimits& lim, in
|
||||
* variable was in the render. The set is more actionable than the template's traceback. */
|
||||
auto it = opt.template_kwargs.find("reasoning_effort");
|
||||
if (it != opt.template_kwargs.end() && !lim.reasoning_efforts.empty()) {
|
||||
const json v = json::parse(it->second, nullptr, false);
|
||||
bool over = false, deep = false;
|
||||
const json v = parse_json_bounded(it->second, 0, &over, &deep);
|
||||
const std::vector<std::string>& set = lim.reasoning_efforts;
|
||||
if (v.is_string() && std::find(set.begin(), set.end(), v.get<std::string>()) == set.end()) {
|
||||
if (!over && !deep && v.is_string() &&
|
||||
std::find(set.begin(), set.end(), v.get<std::string>()) == set.end()) {
|
||||
std::string list;
|
||||
for (size_t i = 0; i < set.size(); ++i)
|
||||
list += (i ? (i + 1 == set.size() ? " and " : ", ") : "") + ("'" + set[i] + "'");
|
||||
@@ -3782,7 +3794,7 @@ std::string anthropic_error_body(const ApiError& e) {
|
||||
json o;
|
||||
o["type"] = "error";
|
||||
o["error"] = json{ { "type", anthropic_error_type(e.status) }, { "message", e.message } };
|
||||
return o.dump();
|
||||
return o.dump(-1, ' ', false, json::error_handler_t::replace); /* see ApiError::dump */
|
||||
}
|
||||
|
||||
AnthropicStream::AnthropicStream(const OaiRequest& r, int index, const IReplyParser* parser)
|
||||
|
||||
Reference in new issue
Block a user