| 2026-11-11 | chenhaozhe | ![]() |
| 2026-11-11 | chenhaozhe | ![]() |
| 2026-11-11 | chenhaozhe | ![]() |
| APIGetway.csproj.user | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
| Filter/FileExtensionFilter.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
| Filter/FolderFilter.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
| Program.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
| Properties/PublishProfiles/FolderProfile1.pubxml.user | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
| appsettings.json | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
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> 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; Filter/FolderFilter.cs
New file @@ -0,0 +1,149 @@ using 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)、Gateway:FolderFilter:BlockedFolders(数组,覆盖默认值)。 private const string Section = "Gateway:FolderFilter"; private const string ExtensionsKey = "BlockedFolders"; // 默认拦截的文件夹名(忽略大小写),不允许访问 bin、obj、vlog、dll 等编译输出目录,也不允许访问上级目录。 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>(); } } } 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(需在静态文件与反向代理之前) // 拦截对 bin/obj 等敏感文件夹的访问,直接返回 404(需在静态文件与反向代理之前) app.UseFolderFilter(); // 拦截对 DLL/LOG 等敏感后缀的访问,直接返回 404(需在静态文件与反向代理之前) app.UseFileExtensionFilter(); app.UseUpstreamErrorPageFilter(); app.UseDefaultFiles(); Properties/PublishProfiles/FolderProfile1.pubxml.user
@@ -3,7 +3,7 @@ <Project> <PropertyGroup> <_PublishTargetUrl>F:\网站发布\APIGateway</_PublishTargetUrl> <History>True|2026-11-10T08:34:30.1187375Z||;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> 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" } } }