Skip to content

Commit 51f9273

Browse files
committed
fix: escape spreadsheet formulas in the TSV export (CSV injection)
Text columns of the export (titles, excerpts, reasons, user names) are user-influenced; a value starting with =, +, -, @, | or % would be evaluated as a formula by Excel / LibreOffice. Such cells now get a leading apostrophe; the numeric reputation delta is left as is.
1 parent 255d3a3 commit 51f9273

2 files changed

Lines changed: 38 additions & 11 deletions

File tree

‎internal/service/activity_log/activity_log_admin_service.go‎

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -229,17 +229,15 @@ func (s *ActivityLogAdminService) Export(ctx context.Context, req *schema.Activi
229229
var writeErr error
230230
err = s.repo.Iterate(ctx, q, ExportLimit, func(rows []*entity.ActivityLog) bool {
231231
for _, it := range s.decorate(ctx, rows) {
232+
// every column except the numeric delta is user-influenced text → escape for spreadsheets
232233
cols := []string{
233234
time.Unix(it.CreatedAt, 0).Format("2006-01-02 15:04:05"),
234-
userCol(it.User, true), userCol(it.User, false),
235-
it.Action, labels[it.Action],
236-
it.ObjectType, it.ObjectID, it.Title,
237-
userCol(it.Target, true), userCol(it.Target, false),
235+
tsvText(userCol(it.User, true)), tsvText(userCol(it.User, false)),
236+
tsvText(it.Action), tsvText(labels[it.Action]),
237+
tsvText(it.ObjectType), tsvText(it.ObjectID), tsvText(it.Title),
238+
tsvText(userCol(it.Target, true)), tsvText(userCol(it.Target, false)),
238239
fmt.Sprintf("%d", it.RankDelta),
239-
detailText(it.Detail), it.IP,
240-
}
241-
for i := range cols {
242-
cols[i] = tsvClean(cols[i])
240+
tsvText(detailText(it.Detail)), tsvText(it.IP),
243241
}
244242
if _, writeErr = io.WriteString(w, strings.Join(cols, "\t")+"\r\n"); writeErr != nil {
245243
return false
@@ -287,9 +285,19 @@ func detailText(d map[string]any) string {
287285
return strings.Join(parts, "; ")
288286
}
289287

290-
func tsvClean(s string) string {
291-
r := strings.NewReplacer("\t", " ", "\r", " ", "\n", " ")
292-
return r.Replace(s)
288+
// tsvText makes a text cell safe for a TSV opened in a spreadsheet: no tabs / line breaks, and a
289+
// leading apostrophe when the value would otherwise be interpreted as a formula (=, +, -, @, or a
290+
// tab / CR that some spreadsheets strip before parsing) — CSV/TSV formula injection.
291+
func tsvText(s string) string {
292+
s = strings.NewReplacer("\t", " ", "\r", " ", "\n", " ").Replace(s)
293+
if s == "" {
294+
return s
295+
}
296+
switch s[0] {
297+
case '=', '+', '-', '@', '|', '%':
298+
return "'" + s
299+
}
300+
return s
293301
}
294302

295303
func isNumeric(s string) bool {

‎internal/service/activity_log/activity_log_service_test.go‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,22 @@ func TestLogPageView_Dedup(t *testing.T) {
138138
t.Fatalf("unexpected entry %+v", e)
139139
}
140140
}
141+
142+
func TestTsvText_FormulaInjection(t *testing.T) {
143+
cases := map[string]string{
144+
"=1+1": "'=1+1",
145+
"+SUM(A1)": "'+SUM(A1)",
146+
"-2": "'-2",
147+
"@cmd": "'@cmd",
148+
"plain title": "plain title",
149+
"tab\there\nline": "tab here line",
150+
"": "",
151+
"'already": "'already",
152+
"Zamykanie miesiąca": "Zamykanie miesiąca",
153+
}
154+
for in, want := range cases {
155+
if got := tsvText(in); got != want {
156+
t.Errorf("tsvText(%q) = %q, want %q", in, got, want)
157+
}
158+
}
159+
}

0 commit comments

Comments
 (0)