server: fix SSRF for CORS proxy and media URLs (#32115)
This commit is contained in:
1 parent
a1a68eb797
commit
8954f19483
16 files changed
+275
-43
No files matched your search
@@ -9,6 +9,8 @@
|
||||
|
||||
#include "server-common.h"
|
||||
|
||||
#include <cpp-httplib/httplib.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <filesystem>
|
||||
@@ -1215,10 +1217,55 @@ json oaicompat_completion_params_parse(const json & body) {
|
||||
// - file:// for local files (only allowed if media_path is set)
|
||||
// - data: for base64 encoded data with uri scheme (e.g. data:image/png;base64,...)
|
||||
// - raw base64 encoded data
|
||||
static bool ipv4_is_public(const unsigned char * b) {
|
||||
return !(b[0] == 0 || b[0] == 10 || b[0] == 127 || b[0] >= 224 // this network, private, loopback, multicast, reserved
|
||||
|| (b[0] == 100 && (b[1] & 0xc0) == 64) // 100.64.0.0/10 CGNAT
|
||||
|| (b[0] == 169 && b[1] == 254) // link-local, cloud metadata
|
||||
|| (b[0] == 172 && (b[1] & 0xf0) == 16) // 172.16.0.0/12
|
||||
|| (b[0] == 192 && b[1] == 168) // 192.168.0.0/16
|
||||
|| (b[0] == 192 && b[1] == 0 && b[2] == 0) // 192.0.0.0/24
|
||||
|| (b[0] == 198 && (b[1] & 0xfe) == 18)); // 198.18.0.0/15
|
||||
}
|
||||
|
||||
bool server_addr_is_public(const std::string & addr) {
|
||||
unsigned char b[16];
|
||||
if (inet_pton(AF_INET, addr.c_str(), b) == 1) {
|
||||
return ipv4_is_public(b);
|
||||
}
|
||||
if (inet_pton(AF_INET6, addr.c_str(), b) != 1) {
|
||||
return false; // e.g. scoped address "fe80::1%eth0"
|
||||
}
|
||||
static const unsigned char v4_mapped[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff};
|
||||
static const unsigned char nat64[12] = {0, 0x64, 0xff, 0x9b, 0, 0, 0, 0, 0, 0, 0, 0};
|
||||
static const unsigned char zero[12] = {};
|
||||
if (memcmp(b, v4_mapped, 12) == 0 || memcmp(b, nat64, 12) == 0) {
|
||||
return ipv4_is_public(b + 12);
|
||||
}
|
||||
return memcmp(b, zero, 12) != 0 // ::/96: unspecified, loopback, IPv4-compatible
|
||||
&& (b[0] & 0xfe) != 0xfc // fc00::/7 unique local
|
||||
&& !(b[0] == 0xfe && (b[1] & 0x80) == 0x80) // fe80::/10 link-local, fec0::/10 site-local
|
||||
&& b[0] != 0xff; // multicast
|
||||
}
|
||||
|
||||
std::string server_resolve_public_addr(const std::string & host, bool allow_private) {
|
||||
std::vector<std::string> addrs;
|
||||
httplib::hosted_at(host, addrs);
|
||||
if (addrs.empty()) {
|
||||
throw std::invalid_argument("cannot resolve host: " + host);
|
||||
}
|
||||
for (const auto & addr : addrs) {
|
||||
if (!allow_private && !server_addr_is_public(addr)) {
|
||||
throw std::invalid_argument("host is not a public address: " + host);
|
||||
}
|
||||
}
|
||||
return addrs[0];
|
||||
}
|
||||
|
||||
void handle_media(
|
||||
std::vector<raw_buffer> & out_files,
|
||||
const std::string & url,
|
||||
const std::string & media_path) {
|
||||
const std::string & media_path,
|
||||
bool allow_private_urls) {
|
||||
if (!media_path.empty()) {
|
||||
// should already be enforced by arg.cpp, but checking just in case
|
||||
GGML_ASSERT(media_path.back() == DIRECTORY_SEPARATOR);
|
||||
@@ -1230,6 +1277,9 @@ void handle_media(
|
||||
common_remote_params params;
|
||||
params.max_size = 1024 * 1024 * 10; // 10MB
|
||||
params.timeout = 10; // seconds
|
||||
params.resolve_host = [allow_private_urls](const std::string & host) {
|
||||
return server_resolve_public_addr(host, allow_private_urls);
|
||||
};
|
||||
SRV_INF("downloading image from '%s'\n", url.c_str());
|
||||
auto res = common_remote_get_content(url, params);
|
||||
if (200 <= res.first && res.first < 300) {
|
||||
@@ -1296,7 +1346,7 @@ static void oaicompat_content_load_media(json & content, const server_chat_param
|
||||
|
||||
json image_url = json_value(p, "image_url", json::object());
|
||||
std::string url = json_value(image_url, "url", std::string());
|
||||
handle_media(out_files, url, opt.media_path);
|
||||
handle_media(out_files, url, opt.media_path, opt.media_allow_private_urls);
|
||||
|
||||
p["type"] = "media_marker";
|
||||
p["text"] = get_media_marker();
|
||||
@@ -1311,7 +1361,7 @@ static void oaicompat_content_load_media(json & content, const server_chat_param
|
||||
json input_audio = json_value(p, "input_audio", json::object());
|
||||
std::string url = json_value(input_audio, "data",
|
||||
json_value(input_audio, "url", std::string()));
|
||||
handle_media(out_files, url, opt.media_path);
|
||||
handle_media(out_files, url, opt.media_path, opt.media_allow_private_urls);
|
||||
|
||||
p["type"] = "media_marker";
|
||||
p["text"] = get_media_marker();
|
||||
@@ -1326,7 +1376,7 @@ static void oaicompat_content_load_media(json & content, const server_chat_param
|
||||
json input_video = json_value(p, type, json::object());
|
||||
std::string url = json_value(input_video, "data",
|
||||
json_value(input_video, "url", std::string()));
|
||||
handle_media(out_files, url, opt.media_path);
|
||||
handle_media(out_files, url, opt.media_path, opt.media_allow_private_urls);
|
||||
|
||||
p["type"] = "media_marker";
|
||||
p["text"] = get_media_marker();
|
||||
|
||||
Reference in new issue
Block a user