Skip to content

Commit 8627888

Browse files
authored
Redact launcher friend names from FactoryGame.log (#234)
1 parent 6f1404d commit 8627888

File tree

1 file changed

+40
-16
lines changed

1 file changed

+40
-16
lines changed

backend/app/debug_info.go

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"log/slog"
1010
"os"
1111
"path/filepath"
12+
"regexp"
1213
"runtime"
1314
"strings"
1415
"time"
@@ -47,7 +48,9 @@ func addDefaultFactoryGameLog(writer *zip.Writer) error {
4748
if err != nil {
4849
return fmt.Errorf("failed to get user cache dir: %w", err)
4950
}
50-
err = utils.AddFileToZip(writer, filepath.Join(cacheDir, "FactoryGame", "Saved", "Logs", "FactoryGame.log"), "FactoryGame.log")
51+
defaultLogPath := filepath.Join(cacheDir, "FactoryGame", "Saved", "Logs", "FactoryGame.log")
52+
// Default log will always be on the local disk, if it exists
53+
bytes, err := os.ReadFile(defaultLogPath)
5154
if err != nil {
5255
if errors.Is(err, os.ErrNotExist) {
5356
if runtime.GOOS != "windows" {
@@ -57,59 +60,80 @@ func addDefaultFactoryGameLog(writer *zip.Writer) error {
5760
}
5861
return fmt.Errorf("log does not exist")
5962
}
60-
return fmt.Errorf("failed to add FactoryGame.log to zip: %w", err)
63+
return fmt.Errorf("failed to read default FactoryGame.log: %w", err)
64+
}
65+
err = addLogFromBytes(writer, bytes, "FactoryGame.log")
66+
if err != nil {
67+
return fmt.Errorf("failed to add default FactoryGame.log to zip: %w", err)
68+
}
69+
return nil
70+
}
71+
72+
func redactLogBytes(bytes []byte) []byte {
73+
// Prevent leaking of Steam/Epic friend nicknames in submitted logs
74+
re := regexp.MustCompile(`(Added friend with nickname ').*(' on online context)`)
75+
return re.ReplaceAll(bytes, []byte("${1}REDACTED${2}"))
76+
}
77+
78+
func addLogFromBytes(writer *zip.Writer, bytes []byte, zipFileName string) error {
79+
redactedBytes := redactLogBytes(bytes)
80+
81+
logFile, err := writer.Create(zipFileName)
82+
if err != nil {
83+
return fmt.Errorf("failed to create log file in zip: %w", err)
84+
}
85+
86+
_, err = logFile.Write(redactedBytes)
87+
if err != nil {
88+
return fmt.Errorf("failed to write log file to zip: %w", err)
6189
}
6290
return nil
6391
}
6492

6593
func addInstallFactoryGameLog(writer *zip.Writer, install *common.Installation) error {
6694
logPath := filepath.Join(install.SavedPath, "Logs", "FactoryGame.log")
95+
// Install-specific logs could be on remote disks
6796
d, err := ficsitcli.FicsitCLI.GetInstallation(install.Path).GetDisk()
6897
if err != nil {
6998
return fmt.Errorf("failed to get disk for installation: %w", err)
7099
}
100+
71101
logExists, err := d.Exists(logPath)
72102
if err != nil {
73103
return fmt.Errorf("failed to check if log exists: %w", err)
74104
}
75105
if !logExists {
76106
return fmt.Errorf("log does not exist")
77107
}
108+
78109
bytes, err := d.Read(logPath)
79110
if err != nil {
80111
return fmt.Errorf("failed to read log file: %w", err)
81112
}
82-
logFile, err := writer.Create(getLogNameForInstall(install))
83-
if err != nil {
84-
return fmt.Errorf("failed to create log file in zip: %w", err)
85-
}
86-
_, err = logFile.Write(bytes)
87-
if err != nil {
88-
return fmt.Errorf("failed to write log file to zip: %w", err)
89-
}
90-
return nil
113+
return addLogFromBytes(writer, bytes, getLogNameForInstall(install))
91114
}
92115

93116
func addFactoryGameLogs(writer *zip.Writer) {
94117
err := addDefaultFactoryGameLog(writer)
95118
if err != nil {
96119
slog.Warn("failed to add default FactoryGame.log to debuginfo zip", slog.Any("error", err))
97120
}
98-
for _, meta := range ficsitcli.FicsitCLI.GetInstallationsMetadata() {
99-
if meta.Info == nil {
121+
for _, installMeta := range ficsitcli.FicsitCLI.GetInstallationsMetadata() {
122+
if installMeta.Info == nil {
100123
continue
101124
}
102125

103-
err := addInstallFactoryGameLog(writer, meta.Info)
126+
err := addInstallFactoryGameLog(writer, installMeta.Info)
104127
if err != nil {
105-
slog.Warn("failed to add FactoryGame.log to debuginfo zip", slog.String("path", meta.Info.Path), slog.Any("error", err))
128+
slog.Warn("failed to add FactoryGame.log to debuginfo zip", slog.String("path", installMeta.Info.Path), slog.Any("error", err))
106129
}
107130
}
108131
}
109132

110133
func getLogNameForInstall(install *common.Installation) string {
111134
hash := sha256.Sum256([]byte(install.Path))
112-
return fmt.Sprintf("FactoryGame_%s.log", hex.EncodeToString(hash[:])[:8])
135+
first8 := hex.EncodeToString(hash[:])[:8]
136+
return fmt.Sprintf("FactoryGame_%s_%s_%s_%s.log", first8, install.Location, install.Branch, install.Type)
113137
}
114138

115139
func addMetadata(writer *zip.Writer) error {

0 commit comments

Comments
 (0)