diff --git a/libbeat/processors/cache/file_store.go b/libbeat/processors/cache/file_store.go index 866175283af5..b5d7180bc7de 100644 --- a/libbeat/processors/cache/file_store.go +++ b/libbeat/processors/cache/file_store.go @@ -26,6 +26,7 @@ import ( "io/fs" "os" "path/filepath" + "strings" "sync" "time" @@ -224,7 +225,7 @@ func (c *fileStore) writeState(final bool) { } return } - f, err := os.CreateTemp(filepath.Dir(c.path), "") + f, err := os.CreateTemp(filepath.Dir(c.path), cleanFilename(c.id)+"-*.tmp") if err != nil { c.log.Errorw("failed to open file to write state", "error", err) return @@ -270,3 +271,24 @@ func (c *fileStore) writeState(final bool) { } } } + +// cleanFilename replaces illegal printable characters (and space or dot) in +// filenames, with underscore. +func cleanFilename(s string) string { + return pathCleaner.Replace(s) +} + +var pathCleaner = strings.NewReplacer( + "/", "_", + "<", "_", + ">", "_", + ":", "_", + `"`, "_", + "/", "_", + `\`, "_", + "|", "_", + "?", "_", + "*", "_", + ".", "_", + " ", "_", +)