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/FolderFilter.cs |  149 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 149 insertions(+), 0 deletions(-)

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>();
+        }
+    }
+}

--
Gitblit v1.9.1