Skip to content

feat: 支持在界面配置跨域来源及通配符 - #44

Open
xuzeyu91 wants to merge 4 commits into
239573049:mainfrom
xuzeyu91:master
Open

xuzeyu91 wants to merge 4 commits into
239573049:mainfrom
xuzeyu91:master

Conversation

@xuzeyu91

Copy link
Copy Markdown

新增跨域来源管理界面,支持精确域名和通配符,配置保存后立即生效。

Motivation and Context

此前跨域来源主要依赖配置文件或环境变量,维护不便,且无法灵活配置子域名。

How Has This Been Tested?

  • 本地全部 273 项测试通过,覆盖匹配边界、配置持久化、接口鉴权、CORS 和 WebSocket。
  • 前后端构建通过。
  • 修改文件的前端 lint 通过;全量 lint 仍有项目已有问题。
  • 使用本地测试宿主验证,未在生产环境验证。

Breaking Changes

现有有效配置无需修改。无效的来源配置会被拒绝;界面配置优先于环境变量和配置文件。

Types of changes

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

  • 裸通配符支持 HTTP/HTTPS 和任意端口,不包含根域。
  • 无 Origin 的 WebSocket 请求仍可通过来源检查;Origin 校验不替代身份认证。
  • 代理路由的 HTTP CORS 仍由上游处理。

xuzeyu and others added 4 commits November 13, 2026 09:44
- FastGateway.sln 缺少测试项目的 ProjectConfigurationPlatforms 映射,
  dotnet build / dotnet test FastGateway.sln 会完全忽略测试项目并静默返回 0,
  AGENTS.md 推荐的回归命令因此是假通过
- FrameOptionsTests 增加 3 个用例:配置 JSON round-trip、
  旧版 gateway.config 缺字段回退 SAMEORIGIN、集群 MessagePack 同步 round-trip
- .NET 11 回归:dotnet build FastGateway.sln 0 错误,
  dotnet test FastGateway.sln 246/246 通过
Copilot AI balanced review requested due to automatic review settings November 21, 2026 09:51

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Concurrent configuration reloads can overwrite newer policies with stale snapshots, leaving revoked origins allowed even after successful saves.

3 open issues
What changed in this PR

Adds dashboard-managed origin rules for gateway CORS and WebSockets, with exact-domain and wildcard matching, persistent overrides, and immediate application to new requests.

Changes:

  • Adds origin-settings APIs and a dashboard editor with save and reset actions.
  • Introduces shared origin validation and live policy updates.
  • Documents configuration behavior and expands origin-policy and compatibility tests.
File Description
web/​src/​services/​SettingService.ts Adds typed origin-settings API calls.
web/​src/​pages/​system-setting/​page.tsx Displays the origin-settings card.
web/​src/​pages/​system-setting/​features/​OriginSettingsCard.tsx Adds origin editing, saving, and reset controls.
tests/​FastGateway.Tests/​OriginPolicyTests.cs Tests matching, persistence, authorization, CORS, and WebSockets.
tests/​FastGateway.Tests/​FrameOptionsTests.cs Adds serialization and legacy-configuration coverage.
src/​FastGateway/​Services/​SettingService.cs Exposes origin-settings endpoints.
src/​FastGateway/​Services/​ConfigurationService.cs Adds rollback-safe managed-setting persistence.
src/​FastGateway/​Risk/​RiskSigningKey.cs Marks origin settings as managed.
src/​FastGateway/​Program.cs Initializes the shared origin policy.
src/​FastGateway/​Infrastructure/​AppJsonContext.cs Registers origin DTOs for source-generated JSON serialization.
src/​FastGateway/​Gateway/​OriginPolicyStore.cs Implements matching, policy storage, and WebSocket checks.
src/​FastGateway/​Gateway/​Gateway.cs Integrates dynamic origin checks into gateway middleware.
README.md Documents origin configuration in English.
README-zh-cn.md Documents origin configuration in Chinese.
FastGateway.sln Adds test-project build configuration mappings.
CHANGELOG.md Records test infrastructure and compatibility coverage updates.
.gitignore Ignores the local CORS HTTP request file.

Review effort: Balanced

if (setting != null && setting.Value == null)
throw new ValidationException("跨域来源配置不能为空值");
var origins = setting == null ? _fallback : setting.Value.Length == 0 ? [] : setting.Value.Split('\n');
Volatile.Write(ref _current, new OriginPolicy(origins));
var wildcard = bareWildcard || value.StartsWith("http://*.", StringComparison.OrdinalIgnoreCase) ||
value.StartsWith("https://*.", StringComparison.OrdinalIgnoreCase);
var input = bareWildcard ? "https://placeholder." + value[2..] :
wildcard ? value.Replace("*.", "placeholder.", StringComparison.Ordinal) : value;
Comment on lines +36 to +51
? uri.Host.Length > rule.Host.Length + 1 &&
uri.Host.EndsWith("." + rule.Host, StringComparison.OrdinalIgnoreCase)
: string.Equals(uri.Host, rule.Host, StringComparison.OrdinalIgnoreCase)));
}

private static bool TryParseRule(string value, out Rule? rule)
{
rule = null;
var bareWildcard = value.StartsWith("*.", StringComparison.Ordinal);
if (bareWildcard && value.Contains(':')) return false;
var wildcard = bareWildcard || value.StartsWith("http://*.", StringComparison.OrdinalIgnoreCase) ||
value.StartsWith("https://*.", StringComparison.OrdinalIgnoreCase);
var input = bareWildcard ? "https://placeholder." + value[2..] :
wildcard ? value.Replace("*.", "placeholder.", StringComparison.Ordinal) : value;
if (!TryParseUri(input, out var uri)) return false;
var host = uri.Host;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants