Skip to content

Commit 5a41718

Browse files
committed
Sanitize user agent, init logging from serverSettings.DataDir
1 parent 4764dba commit 5a41718

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

internal/configuration/Configuration.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func Load() {
9393
}
9494
helper.CreateDir(serverSettings.DataDir)
9595
filesystem.Init(serverSettings.DataDir)
96-
logging.Init(parsedEnvironment.DataDir)
96+
logging.Init(serverSettings.DataDir)
9797
}
9898

9999
// ConnectDatabase loads the database that is defined in the configuration

internal/logging/Logging.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net"
77
"net/http"
88
"os"
9+
"regexp"
910
"strings"
1011
"sync"
1112
"time"
@@ -278,12 +279,18 @@ func LogValidLogin(username string) {
278279
// LogDownload adds a log entry when a download was requested. Non-Blocking
279280
func LogDownload(file models.File, r *http.Request, saveIp bool) {
280281
if saveIp {
281-
createLogEntry(categoryDownload, fmt.Sprintf("%s, IP %s, ID %s, Useragent %s", file.Name, GetIpAddress(r), file.Id, r.UserAgent()), false)
282+
createLogEntry(categoryDownload, fmt.Sprintf("%s, IP %s, ID %s, Useragent %s", file.Name, GetIpAddress(r), file.Id, sanitiseUserAgent(r)), false)
282283
} else {
283-
createLogEntry(categoryDownload, fmt.Sprintf("%s, ID %s, Useragent %s", file.Name, file.Id, r.UserAgent()), false)
284+
createLogEntry(categoryDownload, fmt.Sprintf("%s, ID %s, Useragent %s", file.Name, file.Id, sanitiseUserAgent(r)), false)
284285
}
285286
}
286287

288+
var regexUserAgent = regexp.MustCompile(`[^A-Za-z0-9/. ;:+(|)_\-,]`)
289+
290+
func sanitiseUserAgent(r *http.Request) string {
291+
return regexUserAgent.ReplaceAllString(r.UserAgent(), "")
292+
}
293+
287294
// LogUpload adds a log entry when an upload was created. Non-Blocking
288295
func LogUpload(file models.File, user models.User, fr models.FileRequest) {
289296
if fr.Id != "" {

0 commit comments

Comments
 (0)