AUTH-2977 log file protection

This commit is contained in:
Dalton
2020-08-14 16:51:00 -05:00
committed by Dalton Cherry
parent 5499c77e62
commit 70114c2145
2 changed files with 58 additions and 1 deletions

View File

@@ -3,6 +3,7 @@ package logger
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
@@ -106,7 +107,7 @@ func New(opts ...Option) (Service, error) {
l := NewOutputWriter(SharedWriteManager)
if config.logFileDirectory != "" {
l.Add(NewFileRollingWriter(config.logFileDirectory,
l.Add(NewFileRollingWriter(SanitizeLogPath(config.logFileDirectory),
"cloudflared",
int64(config.maxFileSize),
config.maxFileCount),
@@ -139,3 +140,13 @@ func ParseLevelString(lvl string) ([]Level, error) {
}
return []Level{}, fmt.Errorf("not a valid log level: %q", lvl)
}
// SanitizeLogPath checks that the logger log path
func SanitizeLogPath(path string) string {
newPath := strings.TrimSpace(path)
// make sure it has a log file extension and is not a directory
if filepath.Ext(newPath) != ".log" && !(isDirectory(newPath) || strings.HasSuffix(newPath, "/")) {
newPath = newPath + ".log"
}
return newPath
}