From 08737f11e35d227bd9b788ac29ca81f9381baf9d Mon Sep 17 00:00:00 2001 From: Roman Khavronenko Date: Wed, 16 Aug 2023 14:45:35 +0200 Subject: [PATCH] vmbackup: correctly check if specified `-dst` belongs to specified `-storageDataPath` (#4841) See this issue https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4837 Signed-off-by: hagen1778 --- app/vmbackup/main.go | 15 ++++++++++++++- app/vmbackup/main_test.go | 5 +++++ docs/CHANGELOG.md | 2 ++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/app/vmbackup/main.go b/app/vmbackup/main.go index c4facc8e6d..fc34594b3d 100644 --- a/app/vmbackup/main.go +++ b/app/vmbackup/main.go @@ -205,7 +205,20 @@ func hasFilepathPrefix(path, prefix string) bool { if err != nil { return false } - return strings.HasPrefix(pathAbs, prefixAbs) + if prefixAbs == pathAbs { + return true + } + rel, err := filepath.Rel(prefixAbs, pathAbs) + if err != nil { + // if paths can't be related - they don't match + return false + } + if i := strings.Index(rel, "."); i == 0 { + // if path can be related only with . as first char - they still don't match + return false + } + // if paths are related - it is a match + return true } func newOriginFS() (common.OriginFS, error) { diff --git a/app/vmbackup/main_test.go b/app/vmbackup/main_test.go index f3288fb499..c1a87e51d3 100644 --- a/app/vmbackup/main_test.go +++ b/app/vmbackup/main_test.go @@ -26,4 +26,9 @@ func TestHasFilepathPrefix(t *testing.T) { f("fs://"+pwd+"/foo", pwd+"/foo/bar", false) f("fs://"+pwd+"/foo/bar", pwd+"/foo", true) f("fs://"+pwd+"/foo", pwd+"/bar", false) + f("fs:///data1", "/data", false) + f("fs:///data", "/data1", false) + f("fs:///data", "/data/foo", false) + f("fs:///data/foo", "/data", true) + f("fs:///data/foo/", "/data/", true) } diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 81a1c045df..74b2c388c1 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -15,6 +15,8 @@ The following tip changes can be tested by building VictoriaMetrics components f ## v1.87.x long-time support release (LTS) +* BUGFIX: [vmbackup](https://docs.victoriametrics.com/vmbackup.html): correctly check if specified `-dst` belongs to specified `-storageDataPath`. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4837). + ## [v1.87.7](https://github.com/VictoriaMetrics/VictoriaMetrics/releases/tag/v1.87.7) Released at 2023-08-12