From 4837024d8cd40e31bb4b95cbeec0ae473dfca4e3 Mon Sep 17 00:00:00 2001
From: chenhaozhe <cgz@hz-kingdee.com>
Date: 星期三, 11 十一月 2026 16:26:31 +0800
Subject: [PATCH] 新增 目录过滤器。增加对敏感目录的管控。
---
Filter/FileExtensionFilter.cs | 2
appsettings.json | 15 ++++
Properties/PublishProfiles/FolderProfile1.pubxml.user | 2
Program.cs | 7 +
APIGetway.csproj.user | 2
Filter/FolderFilter.cs | 149 +++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 170 insertions(+), 7 deletions(-)
diff --git a/APIGetway.csproj.user b/APIGetway.csproj.user
index 0fee2b9..8c05e64 100644
--- a/APIGetway.csproj.user
+++ b/APIGetway.csproj.user
@@ -2,7 +2,7 @@
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ActiveDebugProfile>http</ActiveDebugProfile>
- <NameOfLastUsedPublishProfile>F:\ProjFolder\APIGetway\Properties\PublishProfiles\FolderProfile.pubxml</NameOfLastUsedPublishProfile>
+ <NameOfLastUsedPublishProfile>F:\WorkBench\APIGetway\Properties\PublishProfiles\FolderProfile1.pubxml</NameOfLastUsedPublishProfile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
diff --git a/Filter/FileExtensionFilter.cs b/Filter/FileExtensionFilter.cs
index 0e68e94..b770010 100644
--- a/Filter/FileExtensionFilter.cs
+++ b/Filter/FileExtensionFilter.cs
@@ -19,7 +19,7 @@
private const string Section = "Gateway:FileFilter";
private const string ExtensionsKey = "BlockedExtensions";
- private static readonly string[] DefaultExtensions = { ".dll", ".log" };
+ private static readonly string[] DefaultExtensions = { ".dll", ".log", ".config" };
private readonly RequestDelegate _next;
private readonly ILogger<FileExtensionFilter> _logger;
diff --git a/Filter/FolderFilter.cs b/Filter/FolderFilter.cs
new file mode 100644
index 0000000..4dcd505
--- /dev/null
+++ b/Filter/FolderFilter.cs
@@ -0,0 +1,149 @@
+锘縰sing System;
+using System.Collections.Generic;
+using Microsoft.AspNetCore.Builder;
+using Microsoft.AspNetCore.Http;
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.Logging;
+using System.Threading.Tasks;
+
+namespace APIGetway.Filter
+{
+ public class FolderFilter
+ {
+ // 閰嶇疆鑺傦細Gateway:FolderFilter:Enabled锛堥粯璁� true锛夈�丟ateway:FolderFilter:BlockedFolders锛堟暟缁勶紝瑕嗙洊榛樿鍊硷級銆�
+ private const string Section = "Gateway:FolderFilter";
+ private const string ExtensionsKey = "BlockedFolders";
+
+ // 榛樿鎷︽埅鐨勬枃浠跺す鍚嶏紙蹇界暐澶у皬鍐欙級锛屼笉鍏佽璁块棶 bin銆乷bj銆乿log銆乨ll 绛夌紪璇戣緭鍑虹洰褰曪紝涔熶笉鍏佽璁块棶涓婄骇鐩綍銆�
+ private static readonly Dictionary<string, int> DefaultFolders = new Dictionary<string, int>
+ {
+ ["bin"] = 1,
+ ["obj"] = 1,
+ ["vlog"] = 1,
+ ["dll"] = 1,
+ };
+
+
+ private readonly RequestDelegate _next;
+
+ private readonly ILogger<FileExtensionFilter> _logger;
+
+ private readonly bool _enabled;
+
+ private readonly Dictionary<string, int> _folders;
+
+ public FolderFilter(RequestDelegate next, ILogger<FileExtensionFilter> logger, IConfiguration configuration)
+ {
+ _next = next;
+ _logger = logger;
+ _enabled = configuration.GetValue<bool>($"{Section}:Enabled");
+ _folders = ResolveFolders(configuration);
+ }
+
+ public Task InvokeAsync(HttpContext context)
+ {
+ if (_enabled)
+ {
+ var folder = MatchFolders(context.Request.Path);
+ if (folder != null)
+ {
+ _logger.LogWarning("宸叉嫤鎴� {Folder} 鏂囦欢澶硅闂�: {Method} {Path}锛屾潵婧� {RemoteIp}",
+ folder,
+ context.Request.Method,
+ context.Request.Path.Value,
+ context.Connection.RemoteIpAddress);
+ context.Response.StatusCode = StatusCodes.Status404NotFound;
+ return Task.CompletedTask;
+ }
+ }
+ return _next(context);
+ }
+
+ private static Dictionary<string, int> ResolveFolders(IConfiguration configuration)
+ {
+ var folders = new Dictionary<string, int>();
+ foreach (var item in configuration.GetSection(Section).GetSection(ExtensionsKey).GetChildren())
+ {
+ var value = item.Key?.Trim();
+ if (string.IsNullOrEmpty(value))
+ {
+ continue;
+ }
+
+ folders[value] = 1;
+ }
+
+ return folders.Count > 0 ? folders : DefaultFolders;
+ }
+
+
+ public string? MatchFolders(PathString path)
+ {
+ if(!path.HasValue)
+ {
+ return null;
+ }
+
+ //var segments = path.Value.Split('/', StringSplitOptions.RemoveEmptyEntries);
+
+ //if(segments.Length == 0)
+ //{
+ // return null;
+ //}
+ //foreach (var segment in segments)
+ //{
+
+ // if(_folders.TryGetValue(segment.ToLower(), out _))
+ // {
+ // return segment;
+ // }
+ //}
+
+ // 浣跨敤 ReadOnlySpan<char> 鏇夸唬 Split 鏂规
+ ReadOnlySpan<char> pathSpan = path.Value.AsSpan();
+
+ while(!pathSpan.IsEmpty)
+ {
+ int slashIndex = pathSpan.IndexOf('/');
+
+ // 鍙栧綋鍓嶈矾寰勭墖娈碉紱娌℃湁鏂滄潬鏃讹紝鍓╀綑鐨勯儴鍒嗗氨鏄渶鍚庝竴涓墖娈�
+ ReadOnlySpan<char> segment = slashIndex >= 0 ?
+ pathSpan[..slashIndex] : pathSpan;
+
+ // 妫�鏌ュ綋鍓嶇墖娈垫槸鍚﹀湪鎷︽埅鍒楄〃涓紙蹇界暐澶у皬鍐欙級
+ if (!segment.IsEmpty)
+ {
+ // 鐩存帴閫氳繃瀛楁key鍒ゆ柇 褰撳墠璺緞鏄惁鍦ㄥ瓧鍏镐腑锛屽鏋滃湪瀛楀吀涓紝鍒欒涓烘槸鏁忔劅鏂囦欢澶癸紝杩斿洖璇ユ枃浠跺す鍚嶇О銆傛棤闇�鍒ゆ柇瀛楀吀Key鐨勫�硷紝鍥犱负瀛楀吀鐨勫�煎彧鏄崰浣嶇銆�
+ // 榛樿瀛楀吀key 鍙栧皬鍐欒矾寰勩�傚垎鍓茬殑鐗囨鍏ㄩ儴闇�瑕佹寜鐓у皬鍐欏鐞�
+ if(_folders.ContainsKey(segment.ToString().ToLowerInvariant()))
+ {
+ return segment.ToString();
+ }
+ }
+
+ if (slashIndex < 0)
+ {
+ break; // 娌℃湁鏇村鐨勬枩鏉狅紝缁撴潫寰幆
+ }
+
+ pathSpan = pathSpan[(slashIndex + 1)..]; // 绉诲姩鍒颁笅涓�涓墖娈�
+ }
+
+ return null;
+ }
+ }
+
+
+
+
+ public static class FolderFilterExtensions
+ {
+ /// <summary>
+ /// 娉ㄥ唽鏂囦欢璺緞鎷︽埅銆傚繀椤绘斁鍦� UseStaticFiles / MapReverseProxy 涔嬪墠銆�
+ /// </summary>
+ public static IApplicationBuilder UseFolderFilter(this IApplicationBuilder builder)
+ {
+ return builder.UseMiddleware<FolderFilter>();
+ }
+ }
+}
diff --git a/Program.cs b/Program.cs
index 3db8134..88dd7ae 100644
--- a/Program.cs
+++ b/Program.cs
@@ -13,7 +13,7 @@
builder.Services.AddCors(options =>
{
options.AddPolicy("frontend-dev", policy => policy
- .WithOrigins("http://192.168.0.102:32976") // 鎹㈡垚鍓嶇鐪熷疄鍦板潃+绔彛
+ .WithOrigins("http://192.168.0.62:8080", "http://localhost:8080") // 鎹㈡垚鍓嶇鐪熷疄鍦板潃+绔彛
.WithMethods("GET", "POST", "PUT", "DELETE")
.WithHeaders("Content-Type", "X-Username", "Authorization"));
});
@@ -24,9 +24,12 @@
var app = builder.Build();
-// 鎷︽埅瀵� DLL/LOG 绛夋晱鎰熷悗缂�鐨勮闂紝鐩存帴杩斿洖 406锛堥渶鍦ㄩ潤鎬佹枃浠朵笌鍙嶅悜浠g悊涔嬪墠锛�
+// 鎷︽埅瀵� bin/obj 绛夋晱鎰熸枃浠跺す鐨勮闂紝鐩存帴杩斿洖 404锛堥渶鍦ㄩ潤鎬佹枃浠朵笌鍙嶅悜浠g悊涔嬪墠锛�
+app.UseFolderFilter();
+// 鎷︽埅瀵� DLL/LOG 绛夋晱鎰熷悗缂�鐨勮闂紝鐩存帴杩斿洖 404锛堥渶鍦ㄩ潤鎬佹枃浠朵笌鍙嶅悜浠g悊涔嬪墠锛�
app.UseFileExtensionFilter();
+
app.UseUpstreamErrorPageFilter();
app.UseDefaultFiles();
diff --git a/Properties/PublishProfiles/FolderProfile1.pubxml.user b/Properties/PublishProfiles/FolderProfile1.pubxml.user
index 019b465..8d559a7 100644
--- a/Properties/PublishProfiles/FolderProfile1.pubxml.user
+++ b/Properties/PublishProfiles/FolderProfile1.pubxml.user
@@ -3,7 +3,7 @@
<Project>
<PropertyGroup>
<_PublishTargetUrl>F:\缃戠珯鍙戝竷\APIGateway</_PublishTargetUrl>
- <History>True|2026-11-11T01:37:43.3467684Z||;False|2026-11-11T09:37:15.6975640+08:00||;False|2026-11-11T09:36:31.3977175+08:00||;False|2026-11-11T09:35:25.1979137+08:00||;False|2026-11-11T09:35:08.4358206+08:00||;False|2026-11-11T09:33:22.3580839+08:00||;False|2026-11-11T09:33:07.2010007+08:00||;False|2026-11-11T09:32:09.1065679+08:00||;True|2026-11-10T16:34:30.1187375+08:00||;True|2026-11-10T16:28:40.4045409+08:00||;False|2026-11-10T16:28:37.3348136+08:00||;True|2026-11-10T16:27:44.5323533+08:00||;True|2026-11-10T16:26:20.7674970+08:00||;True|2026-11-10T15:47:59.7843377+08:00||;False|2026-11-10T15:46:07.7669474+08:00||;False|2026-11-10T15:46:05.2442797+08:00||;False|2026-11-10T15:46:03.3664927+08:00||;False|2026-11-10T15:45:47.6003342+08:00||;True|2026-11-10T14:26:32.9989135+08:00||;True|2026-11-10T14:07:58.8871076+08:00||;False|2026-11-10T14:07:22.8184692+08:00||;False|2026-11-10T14:06:47.1047518+08:00||;True|2026-11-09T09:50:20.3651906+08:00||;</History>
+ <History>True|2026-11-11T08:23:04.5355376Z||;True|2026-11-11T16:19:21.6400504+08:00||;True|2026-11-11T16:18:41.3730174+08:00||;True|2026-11-11T16:17:53.8726499+08:00||;True|2026-11-11T16:13:55.3887339+08:00||;True|2026-11-11T16:12:27.5807898+08:00||;True|2026-11-11T16:12:21.1721936+08:00||;True|2026-11-11T10:09:22.6463364+08:00||;True|2026-11-11T10:04:06.1802503+08:00||;True|2026-11-11T09:41:13.9651776+08:00||;True|2026-11-11T09:37:43.3467684+08:00||;False|2026-11-11T09:37:15.6975640+08:00||;False|2026-11-11T09:36:31.3977175+08:00||;False|2026-11-11T09:35:25.1979137+08:00||;False|2026-11-11T09:35:08.4358206+08:00||;False|2026-11-11T09:33:22.3580839+08:00||;False|2026-11-11T09:33:07.2010007+08:00||;False|2026-11-11T09:32:09.1065679+08:00||;True|2026-11-10T16:34:30.1187375+08:00||;True|2026-11-10T16:28:40.4045409+08:00||;False|2026-11-10T16:28:37.3348136+08:00||;True|2026-11-10T16:27:44.5323533+08:00||;True|2026-11-10T16:26:20.7674970+08:00||;True|2026-11-10T15:47:59.7843377+08:00||;False|2026-11-10T15:46:07.7669474+08:00||;False|2026-11-10T15:46:05.2442797+08:00||;False|2026-11-10T15:46:03.3664927+08:00||;False|2026-11-10T15:45:47.6003342+08:00||;True|2026-11-10T14:26:32.9989135+08:00||;True|2026-11-10T14:07:58.8871076+08:00||;False|2026-11-10T14:07:22.8184692+08:00||;False|2026-11-10T14:06:47.1047518+08:00||;True|2026-11-09T09:50:20.3651906+08:00||;</History>
<LastFailureDetails />
</PropertyGroup>
</Project>
\ No newline at end of file
diff --git a/appsettings.json b/appsettings.json
index e75c39a..2b780cb 100644
--- a/appsettings.json
+++ b/appsettings.json
@@ -9,7 +9,18 @@
"Gateway": {
"FileFilter": {
"Enabled": true,
- "BlockedExtensions": [ ".dll", ".log" ]
+ "BlockedExtensions": [ ".dll", ".log", ".config" ]
+ },
+ "FolderFilter": {
+ "Enabled": true,
+ // BlockedFolders 涓殑key 鍏ㄩ儴閰嶇疆涓哄皬鍐欏瓧姣�
+ "BlockedFolders": {
+ "bin": 1,
+ "obj": 1,
+ "vlog": 1,
+ "dll": 1,
+ "PageLogs": 1
+ }
},
"ErrorPage": {
"Enabled": true,
@@ -34,7 +45,7 @@
"mes-backend": {
"Destinations": {
"backend": {
- "Address": "http://localhost:81"
+ "Address": "http://127.0.0.1:81"
}
}
}
--
Gitblit v1.9.1