Repository navigation
fix(net/ghttp): anchor static file reads to the search root - #4985
Conversation
Disk files found by searchStaticFile now record their search root and are opened through os.Root, so paths escaping the root are rejected at open time. This closes two escapes the previous path-prefix check missed: a sibling directory sharing the root string prefix, and a symbolic link inside the root pointing outside. Both are covered by a test that fails before this change. Response.ServeFile and ServeFileDownload are explicit-path APIs and keep opening paths as given.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #4985 +/- ##
==========================================
+ Coverage 85.02% 85.08% +0.05%
==========================================
Files 893 893
Lines 47167 47187 +20
==========================================
+ Hits 40103 40148 +45
+ Misses 7064 7039 -25
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
这次改动整体方向可以继续推进,通过
我暂时没有添加 |
Normalize the search root and file path with filepath.Abs before computing the relative path for os.OpenRoot, so relative SearchPaths and StaticPaths keep serving files instead of returning 403. Log root close errors and assert server start/shutdown errors in the new static security test.
|
三点已处理,最新 head
验证: |
Use gfile.Abs for the root and file paths, dropping the filepath.Abs error branches that cannot fail here, and report a Rel failure as an outside-root path. Add an internal test for a missing root so the OpenRoot error is covered.
|
补一条最新 head
验证: |
|
之前提到的几处已经在当前 head
当前 head 的检查是通过的。这次是把磁盘静态文件限制在对应搜索根里:同前缀的兄弟目录,以及指向根外的符号链接,都会在打开时被拒绝。 |
背景
ghttp 磁盘静态文件的越界校验只依赖
gspath.Search里的路径前缀检查。这个检查挡不住两类情况:/srv/static与目录/srv/static-evil;Stat/Open会跟随链接,而路径串仍停在根内。两者都会把配置根之外的文件当作静态资源返回,属于越权读取:兄弟目录只暴露同前缀的相邻路径,符号链接则可读取进程有权限的任意文件。触发前提是启用了静态文件服务(
SetServerRoot/AddSearchPath/AddStaticPath),且请求路径未被客户端规范化(curl --path-as-is、原始 socket、%2e%2e编码均可)。复现
以
static为静态根,布局如下:SetServerRoot(<root>/static)启动服务后,直接发送未规范化的请求:改动前三个请求分别返回
sibling-secret、symlink-secret、symlink-secret;改动后均返回 403,/inside.txt仍返回inside。自动化版本见Test_Static_ServerRoot_Security_Escape。改动
staticFile增加RootPath,searchStaticFile为StaticPaths、SearchPaths搜索到的磁盘文件记录对应搜索根;serveFile打开磁盘文件时先计算filepath.Rel,用filepath.IsLocal过滤越界结果,再通过os.OpenRoot(RootPath).Open(rel)打开;os.Root在打开时按目录句柄和平台语义校验,覆盖..、绝对路径和逃逸符号链接;Response.ServeFile、ServeFileDownload是显式路径 API,不经过该流程,行为不变。兼容性
os.Root拒绝,即使其目标落在根内——这是os.Root的既定约束,需要按符号链接对外提供根外目录的部署不再适用。gres内存资源不涉及磁盘打开,行为不变。os.Root拒绝逃逸路径。测试
go test ./net/ghttp/ -run 'Test_Static' -count=1Test_Static_ServerRoot_Security_Escape:/../static-evil/secret.txt)不返回根外内容;gofmt干净。改动文件
提交