gpt4 book ai didi

web - Go 会清理 Web 请求的 URL 吗?

转载 作者:IT王子 更新时间:2023-10-29 01:25:31 24 4
gpt4 key购买 nike

我正在用 Go 实现一个简单的网络服务器。由于我没有网络开发经验,这对我来说是一个严肃的问题。

假设我正在使用来自 here 的修改后的 loadPage 函数提供网页服务

func loadPage(title string) []byte {
filename := title
body, _ := ioutil.ReadFile(filename)
return body
}

func handler(w http.ResponseWriter, req *http.Request) {
content := loadPage(req.URL.Path[1:])
fmt.Fprintf(w, "%s", content)
}

从技术上讲,这允许我以

的形式编写请求
 http://example.com/../../etc/passwd

代码很乐意为/etc/passwd 文件提供服务,但事实并非如此。这是否意味着在 Go http 包或 http 协议(protocol)本身中有某种针对 ../ 的保护,或者我只是做错了什么,这是一个安全漏洞?

最佳答案

net/http 在其 HTTP 请求多路复用器 ServeMux 中执行此操作:

ServeMux also takes care of sanitizing the URL request path, redirecting any request containing . or .. elements to an equivalent .- and ..-free URL.

相关函数是私有(private)函数cleanPath(p string) stringcalls path.Clean :

1415        np := path.Clean(p)

path.Clean does the appropriate removals :

 97         case path[r] == '.' && path[r+1] == '.' && (r+2 == n || path[r+2] == '/'):
98 // .. element: remove to last /
99 r += 2
100 switch {
101 case out.w > dotdot:
102 // can backtrack
103 out.w--
104 for out.w > dotdot && out.index(out.w) != '/' {
105 out.w--
106 }

如果路径不是根目录,还有另一种情况,但是上面的 cleanPath 确保它是这样,如果还没有路径,则在要清理的路径前加上一个正斜杠。

关于web - Go 会清理 Web 请求的 URL 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23285364/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com