文件操作

1.【必须】 路径穿越检查

  • 在进行文件操作时,如果对外部传入的文件名未做限制,可能导致任意文件读取或者任意文件写入,严重可能导致代码执行。
// bad: 任意文件读取
func handler(w http.ResponseWriter, r *http.Request) {
    path := r.URL.Query()["path"][0]

    // 未过滤文件路径,可能导致任意文件读取
    data, _ := ioutil.ReadFile(path)
    w.Write(data)

    // 对外部传入的文件名变量,还需要验证是否存在../等路径穿越的文件名
    data, _ = ioutil.ReadFile(filepath.Join("/home/user/", path))
    w.Write(data)
}

// bad: 任意文件写入
func unzip(f string) {
    r, _ := zip.OpenReader(f)
    for _, f := range r.File {
        p, _ := filepath.Abs(f.Name)
        // 未验证压缩文件名,可能导致../等路径穿越,任意文件路径写入
        ioutil.WriteFile(p, []byte("present"), 0640)
    }
}

// good: 检查压缩的文件名是否包含..路径穿越特征字符,防止任意写入
func unzipGood(f string) bool {
    r, err := zip.OpenReader(f)
    if err != nil {
        fmt.Println("read zip file fail")
        return false
    }
    for _, f := range r.File {
        p, _ := filepath.Abs(f.Name)
        if !strings.Contains(p, "..") {
            ioutil.WriteFile(p, []byte("present"), 0640)
        }
    }
    return true
}

2.【必须】 文件访问权限

  • 根据创建文件的敏感性设置不同级别的访问权限,以防止敏感数据被任意权限用户读取。例如,设置文件权限为:-rw-r-----
ioutil.WriteFile(p, []byte("present"), 0640)
最后编辑: kuteng  文档更新时间: 2021-06-04 17:24   作者:kuteng