[v8.3.x] Security: Fix directory traversal issue (#42838)

* security: fix dir traversal issue

* Improve comments and error message.

* Fix lint

Co-authored-by: Kyle Brandt <kyle@grafana.com>
Co-authored-by: Sofia Papagiannaki <1632407+papagian@users.noreply.github.com>
This commit is contained in:
Dimitris Sotirakis
2021-12-07 19:37:30 +02:00
committed by GitHub
co-authored by Kyle Brandt Sofia Papagiannaki
parent 3e1d120d88
commit 6aafac7e60
2 changed files with 43 additions and 3 deletions
+16 -3
View File
@@ -281,14 +281,27 @@ func (hs *HTTPServer) getPluginAssets(c *models.ReqContext) {
return
}
requestedFile := filepath.Clean(web.Params(c.Req)["*"])
pluginFilePath := filepath.Join(plugin.PluginDir, requestedFile)
// prepend slash for cleaning relative paths
requestedFile := filepath.Clean(filepath.Join("/", web.Params(c.Req)["*"]))
rel, err := filepath.Rel("/", requestedFile)
if err != nil {
// slash is prepended above therefore this is not expected to fail
c.JsonApiErr(500, "Failed to get the relative path", err)
return
}
if !plugin.IncludedInSignature(requestedFile) {
if !plugin.IncludedInSignature(rel) {
hs.log.Warn("Access to requested plugin file will be forbidden in upcoming Grafana versions as the file "+
"is not included in the plugin signature", "file", requestedFile)
}
absPluginDir, err := filepath.Abs(plugin.PluginDir)
if err != nil {
c.JsonApiErr(500, "Failed to get plugin absolute path", nil)
return
}
pluginFilePath := filepath.Join(absPluginDir, rel)
// It's safe to ignore gosec warning G304 since we already clean the requested file path and subsequently
// use this with a prefix of the plugin's directory, which is set during plugin loading
// nolint:gosec