From 0fc2bb13a789df338845ea3beefb287e624c1a1a Mon Sep 17 00:00:00 2001 From: groselt Date: Sat, 30 May 2020 17:04:36 +0100 Subject: [PATCH 1/3] Sanitize log filename before saving Remove invalid path characters --- internal/view/log.go | 13 ++++++++++++- internal/view/log_test.go | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/internal/view/log.go b/internal/view/log.go index a06e040c..07f43029 100644 --- a/internal/view/log.go +++ b/internal/view/log.go @@ -7,6 +7,7 @@ import ( "io" "os" "path/filepath" + "regexp" "strings" "time" @@ -29,6 +30,9 @@ const ( flushTimeout = 50 * time.Millisecond ) +// InvalidCharsRX contains invalid filename characters. +var invalidPathCharsRX = regexp.MustCompile(`[:/\\]+`) + // Log represents a generic log viewer. type Log struct { *tview.Flex @@ -284,12 +288,19 @@ func (l *Log) SaveCmd(evt *tcell.EventKey) *tcell.EventKey { return nil } +// SanitizeFilename removes characters not allowed by OS +func SanitizeFilename(name string) string { + processedString := invalidPathCharsRX.ReplaceAllString(name, "-") + + return processedString +} + func ensureDir(dir string) error { return os.MkdirAll(dir, 0744) } func saveData(cluster, name, data string) (string, error) { - dir := filepath.Join(config.K9sDumpDir, cluster) + dir := filepath.Join(config.K9sDumpDir, SanitizeFilename(cluster)) if err := ensureDir(dir); err != nil { return "", err } diff --git a/internal/view/log_test.go b/internal/view/log_test.go index 049636a3..dd47b30e 100644 --- a/internal/view/log_test.go +++ b/internal/view/log_test.go @@ -43,6 +43,23 @@ func TestLogViewSave(t *testing.T) { assert.Equal(t, len(c2), len(c1)+1) } +func TestSanitizedFilename(t *testing.T) { + uu := []struct { + name string + expected string + }{ + {"alpha", "alpha"}, + {"123", "123"}, + {"with/slash", "with-slash"}, + {"with:colon", "with-colon"}, + {":many:invalid\\characters\\", "-many-invalid-characters-"}, + } + + for _, u := range uu { + assert.Equal(t, u.expected, view.SanitizeFilename(u.name)) + } +} + // ---------------------------------------------------------------------------- // Helpers... From 7d2eb83f1578283ec7ccd0638540eab10b489d1e Mon Sep 17 00:00:00 2001 From: groselt Date: Sat, 30 May 2020 18:09:41 +0100 Subject: [PATCH 2/3] Unify sanitizing of filenames --- internal/view/log.go | 2 +- internal/view/table_helper.go | 4 ++-- internal/view/yaml.go | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/view/log.go b/internal/view/log.go index 07f43029..23b08a77 100644 --- a/internal/view/log.go +++ b/internal/view/log.go @@ -306,7 +306,7 @@ func saveData(cluster, name, data string) (string, error) { } now := time.Now().UnixNano() - fName := fmt.Sprintf("%s-%d.log", strings.Replace(name, "/", "-", -1), now) + fName := fmt.Sprintf("%s-%d.log", SanitizeFilename(name), now) path := filepath.Join(dir, fName) mod := os.O_CREATE | os.O_WRONLY diff --git a/internal/view/table_helper.go b/internal/view/table_helper.go index 64d86469..6348966f 100644 --- a/internal/view/table_helper.go +++ b/internal/view/table_helper.go @@ -18,12 +18,12 @@ import ( func computeFilename(cluster, ns, title, path string) (string, error) { now := time.Now().UnixNano() - dir := filepath.Join(config.K9sDumpDir, cluster) + dir := filepath.Join(config.K9sDumpDir, SanitizeFilename(cluster)) if err := ensureDir(dir); err != nil { return "", err } - name := title + "-" + strings.Replace(path, "/", "-", -1) + name := title + "-" + SanitizeFilename(path) if path == "" { name = title } diff --git a/internal/view/yaml.go b/internal/view/yaml.go index 90ff4258..2c00a9c3 100644 --- a/internal/view/yaml.go +++ b/internal/view/yaml.go @@ -61,13 +61,13 @@ func enableRegion(str string) string { } func saveYAML(cluster, name, data string) (string, error) { - dir := filepath.Join(config.K9sDumpDir, cluster) + dir := filepath.Join(config.K9sDumpDir, SanitizeFilename(cluster)) if err := ensureDir(dir); err != nil { return "", err } now := time.Now().UnixNano() - fName := fmt.Sprintf("%s-%d.yml", strings.Replace(name, "/", "-", -1), now) + fName := fmt.Sprintf("%s-%d.yml", SanitizeFilename(name), now) path := filepath.Join(dir, fName) mod := os.O_CREATE | os.O_WRONLY From 6e5afd6d56d05de5b96b693cdf422f6d06adf4dd Mon Sep 17 00:00:00 2001 From: groselt Date: Sat, 30 May 2020 18:17:05 +0100 Subject: [PATCH 3/3] Make sanitizeFilename private --- internal/view/log.go | 7 +++---- internal/view/log_test.go | 17 ----------------- internal/view/table_helper.go | 4 ++-- internal/view/yaml.go | 4 ++-- 4 files changed, 7 insertions(+), 25 deletions(-) diff --git a/internal/view/log.go b/internal/view/log.go index 23b08a77..47385d64 100644 --- a/internal/view/log.go +++ b/internal/view/log.go @@ -288,8 +288,7 @@ func (l *Log) SaveCmd(evt *tcell.EventKey) *tcell.EventKey { return nil } -// SanitizeFilename removes characters not allowed by OS -func SanitizeFilename(name string) string { +func sanitizeFilename(name string) string { processedString := invalidPathCharsRX.ReplaceAllString(name, "-") return processedString @@ -300,13 +299,13 @@ func ensureDir(dir string) error { } func saveData(cluster, name, data string) (string, error) { - dir := filepath.Join(config.K9sDumpDir, SanitizeFilename(cluster)) + dir := filepath.Join(config.K9sDumpDir, sanitizeFilename(cluster)) if err := ensureDir(dir); err != nil { return "", err } now := time.Now().UnixNano() - fName := fmt.Sprintf("%s-%d.log", SanitizeFilename(name), now) + fName := fmt.Sprintf("%s-%d.log", sanitizeFilename(name), now) path := filepath.Join(dir, fName) mod := os.O_CREATE | os.O_WRONLY diff --git a/internal/view/log_test.go b/internal/view/log_test.go index dd47b30e..049636a3 100644 --- a/internal/view/log_test.go +++ b/internal/view/log_test.go @@ -43,23 +43,6 @@ func TestLogViewSave(t *testing.T) { assert.Equal(t, len(c2), len(c1)+1) } -func TestSanitizedFilename(t *testing.T) { - uu := []struct { - name string - expected string - }{ - {"alpha", "alpha"}, - {"123", "123"}, - {"with/slash", "with-slash"}, - {"with:colon", "with-colon"}, - {":many:invalid\\characters\\", "-many-invalid-characters-"}, - } - - for _, u := range uu { - assert.Equal(t, u.expected, view.SanitizeFilename(u.name)) - } -} - // ---------------------------------------------------------------------------- // Helpers... diff --git a/internal/view/table_helper.go b/internal/view/table_helper.go index 6348966f..8e18c48e 100644 --- a/internal/view/table_helper.go +++ b/internal/view/table_helper.go @@ -18,12 +18,12 @@ import ( func computeFilename(cluster, ns, title, path string) (string, error) { now := time.Now().UnixNano() - dir := filepath.Join(config.K9sDumpDir, SanitizeFilename(cluster)) + dir := filepath.Join(config.K9sDumpDir, sanitizeFilename(cluster)) if err := ensureDir(dir); err != nil { return "", err } - name := title + "-" + SanitizeFilename(path) + name := title + "-" + sanitizeFilename(path) if path == "" { name = title } diff --git a/internal/view/yaml.go b/internal/view/yaml.go index 2c00a9c3..6831490d 100644 --- a/internal/view/yaml.go +++ b/internal/view/yaml.go @@ -61,13 +61,13 @@ func enableRegion(str string) string { } func saveYAML(cluster, name, data string) (string, error) { - dir := filepath.Join(config.K9sDumpDir, SanitizeFilename(cluster)) + dir := filepath.Join(config.K9sDumpDir, sanitizeFilename(cluster)) if err := ensureDir(dir); err != nil { return "", err } now := time.Now().UnixNano() - fName := fmt.Sprintf("%s-%d.yml", SanitizeFilename(name), now) + fName := fmt.Sprintf("%s-%d.yml", sanitizeFilename(name), now) path := filepath.Join(dir, fName) mod := os.O_CREATE | os.O_WRONLY