Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,16 @@ func (l *Logger) initLoggers() error {
var err error
today := time.Now().Format("2006-01-02")

baseDir, err := filepath.Abs(l.config.LogDir)
if err != nil {
return fmt.Errorf("resolve log dir: %w", err)
}

// Open info log file
infoPath := filepath.Join(l.config.LogDir, fmt.Sprintf("%s_%s.log", l.config.AppName, today))
infoPath := filepath.Join(baseDir, fmt.Sprintf("%s_%s.log", l.config.AppName, today))

// FIX: G304: Potential file inclusion via variable
cleanPath := filepath.Clean(infoPath)
if !strings.HasPrefix(cleanPath, l.config.LogDir) {
return fmt.Errorf("invalid log file path: %s", cleanPath)
if rel, err := filepath.Rel(baseDir, infoPath); err != nil || strings.HasPrefix(rel, "..") {
return fmt.Errorf("invalid log file path: %s", infoPath)
}

l.infoFile, err = os.OpenFile(infoPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o600)
Expand All @@ -119,12 +122,9 @@ func (l *Logger) initLoggers() error {
}

// Open error log file
errorPath := filepath.Join(l.config.LogDir, fmt.Sprintf("%s_error_%s.log", l.config.AppName, today))

// FIX: G304: Potential file inclusion via variable
cleanErrorPath := filepath.Clean(errorPath)
if !strings.HasPrefix(cleanErrorPath, l.config.LogDir) {
return fmt.Errorf("invalid log file path: %s", cleanPath)
errorPath := filepath.Join(baseDir, fmt.Sprintf("%s_error_%s.log", l.config.AppName, today))
if rel, err := filepath.Rel(baseDir, errorPath); err != nil || strings.HasPrefix(rel, "..") {
return fmt.Errorf("invalid log_error file path: %s", errorPath)
}

l.errorFile, err = os.OpenFile(errorPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o600)
Expand Down
Loading