跨域配置改为支持域名白名单

This commit is contained in:
xiongziliang
2026-09-29 11:31:43 +08:00
parent ed3925e950
commit de040511d2
4 changed files with 60 additions and 17 deletions

View File

@@ -452,9 +452,12 @@ forbidCacheSuffix=
# Header name to trust for extracting the real client IP from an HTTP proxy request header. See: https://github.com/ZLMediaKit/ZLMediaKit/issues/1388
# Do not expose this key, as it may lead to forged client IPs.
forwarded_ip_header=
# 默认允许所有跨域请求
# Whether to allow all cross-origin requests by default (sets generic CORS headers).
allow_cross_domains=1
# 允许跨域的域名列表,多个用,隔开,设置为*则允许所有域名跨域,置空情况下不允许任何域名跨域
# 示例: http://example.com,https://app.example.com
# Allowed cross-origin domain list, separated by ",". Set to * to allow all origins.
# Leave empty to deny any cross-origin request.
# Example: http://example.com,https://app.example.com
allow_origins=*
# 允许访问http api和http文件索引的ip地址范围白名单,置空情况下不做限制
# IP whitelist ranges allowed to access the HTTP API and file indexes. Leave empty to allow any IP without restrictions.
allow_ip_range=::1,127.0.0.1,172.16.0.0-172.31.255.255,192.168.0.0-192.168.255.255,10.0.0.0-10.255.255.255

View File

@@ -200,7 +200,7 @@ const string kNotFound = HTTP_FIELD "notFound";
const string kDirMenu = HTTP_FIELD "dirMenu";
const string kForbidCacheSuffix = HTTP_FIELD "forbidCacheSuffix";
const string kForwardedIpHeader = HTTP_FIELD "forwarded_ip_header";
const string kAllowCrossDomains = HTTP_FIELD "allow_cross_domains";
const string kAllowOrigins = HTTP_FIELD "allow_origins";
const string kAllowIPRange = HTTP_FIELD "allow_ip_range";
static onceToken token([]() {
@@ -225,7 +225,9 @@ static onceToken token([]() {
<< endl;
mINI::Instance()[kForbidCacheSuffix] = "";
mINI::Instance()[kForwardedIpHeader] = "";
mINI::Instance()[kAllowCrossDomains] = 1;
// 默认允许所有域名跨域
// Allow all origins by default
mINI::Instance()[kAllowOrigins] = "*";
mINI::Instance()[kAllowIPRange] = "::1,127.0.0.1,172.16.0.0-172.31.255.255,192.168.0.0-192.168.255.255,10.0.0.0-10.255.255.255";
});

View File

@@ -385,9 +385,12 @@ extern const std::string kForbidCacheSuffix;
// 可以把http代理前真实客户端ip放在http头中:https://github.com/ZLMediaKit/ZLMediaKit/issues/1388 [AUTO-TRANSLATED:afcd9556]
// You can put the real client IP address before the HTTP proxy in the HTTP header: https://github.com/ZLMediaKit/ZLMediaKit/issues/1388
extern const std::string kForwardedIpHeader;
// 是否允许所有跨域请求 [AUTO-TRANSLATED:2551c096]
// Whether to allow all cross-domain requests
extern const std::string kAllowCrossDomains;
// 允许跨域的域名列表,多个用","隔开,设置为*则允许所有域名跨域,置空情况下不允许任何域名跨域
// 示例: http://example.com,https://app.example.com
// Allowed cross-origin domain list, separated by ",". Set to * to allow all origins,
// leave empty to deny any cross-origin request.
// Example: http://example.com,https://app.example.com
extern const std::string kAllowOrigins;
// 允许访问http api和http文件索引的ip地址范围白名单,置空情况下不做限制 [AUTO-TRANSLATED:ab939863]
// Whitelist of IP address ranges allowed to access HTTP API and HTTP file index. No restrictions are imposed when empty
extern const std::string kAllowIPRange;

View File

@@ -11,18 +11,43 @@
#include <stdio.h>
#include <sys/stat.h>
#include <algorithm>
#include <set>
#include "Common/config.h"
#include "Common/strCoding.h"
#include "HttpSession.h"
#include "HttpConst.h"
#include "Common/Parser.h"
#include "Util/base64.h"
#include "Util/SHA1.h"
#include "Util/util.h"
using namespace std;
using namespace toolkit;
namespace mediakit {
// 判断该跨域域名是否在白名单内
// Check whether the cross-origin domain is in the whitelist
static bool isOriginAllowed(const string &origin) {
using OriginSet = std::set<std::string, StrCaseCompare>;
GET_CONFIG_FUNC(OriginSet, allow_origins, Http::kAllowOrigins, [](const string &str) {
OriginSet ret;
for (auto &item : split(str, ",")) {
trim(item);
if (!item.empty()) {
ret.emplace(item);
}
}
return ret;
});
// 包含*则允许所有域名跨域
// Allow all origins when * is configured
if (allow_origins.find("*") != allow_origins.end()) {
return true;
}
return allow_origins.find(origin) != allow_origins.end();
}
HttpSession::HttpSession(const Socket::Ptr &pSock) : Session(pSock) {
// 设置默认参数 [AUTO-TRANSLATED:ae5b72e6]
// Set default parameters
@@ -43,15 +68,23 @@ void HttpSession::onHttpRequest_HEAD() {
void HttpSession::onHttpRequest_OPTIONS() {
KeyValue header;
header.emplace("Allow", "GET, POST, PUT, HEAD, OPTIONS, DELETE");
GET_CONFIG(bool, allow_cross_domains, Http::kAllowCrossDomains);
if (allow_cross_domains) {
header.emplace("Access-Control-Allow-Origin", "*");
header.emplace("Access-Control-Allow-Headers", "*");
header.emplace("Access-Control-Allow-Methods", "GET, POST, PUT, HEAD, OPTIONS, DELETE");
}
if (!_origin.empty() && isOriginAllowed(_origin)) {
// 回显origin而不是返回*,避免与Access-Control-Allow-Credentials冲突
// Echo the origin instead of returning *, to avoid conflicting with Access-Control-Allow-Credentials
header.emplace("Access-Control-Allow-Origin", _origin);
header.emplace("Access-Control-Allow-Credentials", "true");
header.emplace("Access-Control-Request-Methods", "GET, POST, PUT, OPTIONS, DELETE");
header.emplace("Access-Control-Request-Headers", "Accept,Accept-Language,Content-Language,Content-Type");
// 带凭证的请求不支持通配符,此处回显客户端声明的请求头
// Credentialed requests do not support wildcards, so echo the headers declared by the client here
auto &req_headers = _parser["Access-Control-Request-Headers"];
header.emplace("Access-Control-Allow-Headers", req_headers.empty() ? "*" : req_headers);
header.emplace("Access-Control-Allow-Methods", "GET, POST, PUT, HEAD, OPTIONS, DELETE");
// 预检结果缓存1天,减少预检请求次数
// Cache the preflight result for 1 day to reduce preflight requests
header.emplace("Access-Control-Max-Age", "86400");
// 跨域响应内容随origin变化,提示缓存服务器按Origin区分缓存
// The cross-origin response varies with origin, tell caches to vary on Origin
header.emplace("Vary", "Origin");
}
sendResponse(200, true, nullptr, header);
}
@@ -768,10 +801,12 @@ void HttpSession::sendResponse(int code,
headerOut.emplace("Server", kServerName);
headerOut.emplace("Connection", bClose ? "close" : "keep-alive");
GET_CONFIG(bool, allow_cross_domains, Http::kAllowCrossDomains);
if (allow_cross_domains && !_origin.empty()) {
if (!_origin.empty() && isOriginAllowed(_origin)) {
headerOut.emplace("Access-Control-Allow-Origin", _origin);
headerOut.emplace("Access-Control-Allow-Credentials", "true");
// 跨域响应内容随origin变化,提示缓存服务器按Origin区分缓存
// The cross-origin response varies with origin, tell caches to vary on Origin
headerOut.emplace("Vary", "Origin");
}
if (!bClose) {