Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions examples/server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,27 @@ configured defaults.
| `--max-pending-jobs <count>` | `64` | Maximum async jobs queued or generating |
| `--completed-ttl-seconds <seconds>` | `600` | Retention time for completed async jobs |

### Access

By default any program on the machine, and any web page open in a browser on
it, can call the server. To limit that:

| Option | Default | Purpose |
| --- | --- | --- |
| `--api-key <key>` | none | Key clients must send as `Authorization: Bearer <key>` or `X-Api-Key: <key>`; comma-separated for several. Requests without a valid key get `401`. |
| `--api-key-file <file>` | none | The same, one key per line, so keys don't show in the process list |
| `--cors-origins <origins>` | any | Web origins allowed to read the server's answers in a browser, comma-separated |

With a key, the page at `/` still loads, but the bundled Web UI can't send
the key, so its calls get `401`; it is meant for use without a key.

`--cors-origins` only decides which web pages may read the answers: other
pages get no CORS headers, but their requests still reach the server. To
refuse requests, set a key as well.

An empty list (such as `--api-key ,`) or a key file without keys stops the
server at startup instead of leaving it open.

### Logging

Logging defaults to `info`. Use `--log-level <level>` to select `debug`, `verbose`,
Expand Down
52 changes: 44 additions & 8 deletions examples/server/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <mutex>
Expand Down Expand Up @@ -62,6 +63,20 @@ static void parse_args(int argc,
}
}

// api_key_matches compares in constant time, so the answer's timing
// doesn't tell how much of a key was right.
static bool api_key_matches(const std::vector<std::string>& keys, const std::string& key) {
bool found = false;
for (const auto& k : keys) {
unsigned char diff = k.size() != key.size();
for (size_t i = 0; i < k.size(); i++) {
diff |= static_cast<unsigned char>(k[i] ^ (i < key.size() ? key[i] : 0));
}
found |= diff == 0;
}
return found;
}

void sd_log_cb(enum sd_log_level_t level, const char* log, void* data) {
SDSvrParams* svr_params = (SDSvrParams*)data;
log_print(level, log, svr_params->log_level, svr_params->color);
Expand Down Expand Up @@ -136,20 +151,41 @@ int main(int argc, const char** argv) {

httplib::Server svr;

svr.set_pre_routing_handler([](const httplib::Request& req, httplib::Response& res) {
std::string origin = req.get_header_value("Origin");
if (origin.empty()) {
origin = "*";
svr.set_pre_routing_handler([&svr_params](const httplib::Request& req, httplib::Response& res) {
// CORS: with --cors-origins only those origins get the headers, so a
// browser keeps other web pages out; without it, any origin (as before).
const auto& allowed = svr_params.cors_origins;
std::string origin = req.get_header_value("Origin");
if (allowed.empty() || std::find(allowed.begin(), allowed.end(), origin) != allowed.end()) {
res.set_header("Access-Control-Allow-Origin", origin.empty() ? "*" : origin);
res.set_header("Access-Control-Allow-Credentials", "true");
res.set_header("Access-Control-Allow-Methods", "*");
// With credentials, browsers take "*" literally and never let
// Authorization through a wildcard: name the headers asked for.
std::string headers = req.get_header_value("Access-Control-Request-Headers");
res.set_header("Access-Control-Allow-Headers", headers.empty() ? "*" : headers);
}
res.set_header("Access-Control-Allow-Origin", origin);
res.set_header("Access-Control-Allow-Credentials", "true");
res.set_header("Access-Control-Allow-Methods", "*");
res.set_header("Access-Control-Allow-Headers", "*");

if (req.method == "OPTIONS") {
res.status = 204;
return httplib::Server::HandlerResponse::Handled;
}

// API key: every request but the web page itself needs one.
if (!svr_params.api_keys.empty() && !(req.method == "GET" && req.path == "/")) {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
std::string key = req.get_header_value("X-Api-Key");
const std::string auth = req.get_header_value("Authorization");
const std::string bearer = "Bearer ";
if (key.empty() && auth.compare(0, bearer.size(), bearer) == 0) {
key = auth.substr(bearer.size());
}
if (!api_key_matches(svr_params.api_keys, key)) {
res.status = 401;
res.set_content(R"({"error":{"code":401,"message":"Invalid API Key","type":"authentication_error"}})",
"application/json");
return httplib::Server::HandlerResponse::Handled;
}
}
return httplib::Server::HandlerResponse::Unhandled;
});

Expand Down
66 changes: 66 additions & 0 deletions examples/server/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <cmath>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <mutex>
#include <regex>
#include <sstream>
Expand Down Expand Up @@ -392,8 +393,51 @@ ArgOptions SDSvrParams::get_options() {
return -1;
};

// split_list adds the comma-separated entries of arg to out; a list
// without any entry (like ",") is an error, so a restriction can't
// silently turn into none.
auto split_list = [](const char* arg, std::vector<std::string>& out) {
std::stringstream ss(arg);
std::string item;
size_t added = 0;
while (std::getline(ss, item, ',')) {
if (!item.empty()) {
out.push_back(item);
added++;
}
}
return added > 0;
};

options.manual_options = {
{"-h", "--help", "show this help message and exit", on_help_arg},
{"", "--api-key", "API key clients must send (Authorization: Bearer KEY or X-Api-Key); comma-separated for several",
[this, split_list](int argc, const char** argv, int index) {
if (++index >= argc || argv[index][0] == '\0')
return -1;
if (!split_list(argv[index], api_keys)) {
LOG_ERROR("error: --api-key needs at least one key");
return -1;
}
return 1;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}},
{"", "--api-key-file", "file with API keys, one per line (keeps them off the command line)",
[this](int argc, const char** argv, int index) {
if (++index >= argc || argv[index][0] == '\0')
return -1;
api_key_file = argv_to_utf8(index, argv);
return 1;
}},
{"", "--cors-origins", "web origins allowed to call the server, comma-separated (default: any)",
[this, split_list](int argc, const char** argv, int index) {
if (++index >= argc || argv[index][0] == '\0')
return -1;
if (!split_list(argv[index], cors_origins)) {
LOG_ERROR("error: --cors-origins needs at least one origin");
return -1;
}
return 1;
}},
{"", "--model-dir", "model root; repeat to search multiple directories (default: models)",
[this](int argc, const char** argv, int index) {
if (++index >= argc || argv[index][0] == '\0')
Expand Down Expand Up @@ -438,6 +482,26 @@ bool SDSvrParams::resolve_and_validate() {
if (!validate()) {
return false;
}
if (!api_key_file.empty()) {
std::ifstream file(fs::u8path(api_key_file));
if (!file) {
LOG_ERROR("error: can't read api_key_file: %s", api_key_file.c_str());
return false;
}
std::string line;
const size_t before = api_keys.size();
while (std::getline(file, line)) {
line.erase(line.find_last_not_of(" \t\r\n") + 1);
line.erase(0, line.find_first_not_of(" \t"));
if (!line.empty()) {
api_keys.push_back(line);
}
}
if (api_keys.size() == before) {
LOG_ERROR("error: api_key_file has no keys: %s", api_key_file.c_str());
return false;
}
}
return true;
}

Expand All @@ -450,6 +514,8 @@ std::string SDSvrParams::to_string() const {
<< " max_pending_jobs: " << max_pending_jobs << ",\n"
<< " completed_ttl_seconds: " << completed_ttl_seconds << ",\n"
<< " serve_html_path: \"" << serve_html_path << "\",\n"
<< " api_keys: " << api_keys.size() << ",\n"
<< " cors_origins: " << cors_origins.size() << ",\n"
<< "}";
return oss.str();
}
Expand Down
5 changes: 5 additions & 0 deletions examples/server/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ struct SDSvrParams {
int completed_ttl_seconds = 600;
std::string serve_html_path;
std::vector<std::string> model_dirs;
// Access: keys clients must send, and the web origins that may call the
// server. Empty: no key needed, any origin (as before).
std::vector<std::string> api_keys;
std::string api_key_file;
std::vector<std::string> cors_origins;
bool normal_exit = false;
sd_log_level_t log_level = SD_LOG_INFO;
bool color = false;
Expand Down