Extract safecsv writer for spreadsheet-safe exports

Wrap encoding/csv with formula-safe cell sanitization and
use it from log export streaming.

Signed-off-by: Cursor Agent <cursoragent@cursor.com>

Co-authored-by: Bryan FRIMIN <bryan@frimin.fr>
This commit is contained in:
Cursor Agent
2026-07-30 06:36:35 +00:00
parent c5b1f210d5
commit fc54faa263
6 changed files with 160 additions and 41 deletions

View File

@@ -22,7 +22,6 @@ package iam
import (
"context"
"encoding/csv"
"fmt"
"strconv"
"strings"
@@ -33,6 +32,7 @@ import (
"go.probo.inc/probo/pkg/gid"
"go.probo.inc/probo/pkg/mail"
"go.probo.inc/probo/pkg/page"
"go.probo.inc/probo/pkg/safecsv"
)
type (
@@ -93,7 +93,7 @@ func (s *LogExportService) streamAuditLogCSV(
organizationID gid.GID,
organizationName string,
filter *coredata.AuditLogEntryFilter,
w *csv.Writer,
w *safecsv.Writer,
) error {
if err := w.Write(auditLogExportCSVHeader); err != nil {
return fmt.Errorf("cannot write audit log CSV header: %w", err)
@@ -148,7 +148,7 @@ func (s *LogExportService) streamSCIMEventCSV(
organizationID gid.GID,
organizationName string,
filter *coredata.SCIMEventFilter,
w *csv.Writer,
w *safecsv.Writer,
) error {
if err := w.Write(scimEventExportCSVHeader); err != nil {
return fmt.Errorf("cannot write SCIM event CSV header: %w", err)
@@ -207,7 +207,7 @@ func auditLogEntryCSVRow(
entry *coredata.AuditLogEntry,
actor auditLogActorExportInfo,
) []string {
return csvExportRow(
return []string{
organizationName,
entry.ID.String(),
entry.CreatedAt.Format(time.RFC3339),
@@ -218,7 +218,7 @@ func auditLogEntryCSVRow(
entry.Action,
entry.ResourceType,
entry.ResourceID.String(),
)
}
}
func scimEventCSVRow(
@@ -233,7 +233,7 @@ func scimEventCSVRow(
email = scimEmailFromUserName(event.UserName)
}
return csvExportRow(
return []string{
organizationName,
event.ID.String(),
event.CreatedAt.Format(time.RFC3339),
@@ -245,7 +245,7 @@ func scimEventCSVRow(
strconv.Itoa(event.StatusCode),
stringPtrValue(event.ErrorMessage),
event.IPAddress.String(),
)
}
}
func loadAuditLogActorExportInfo(
@@ -381,28 +381,6 @@ func uniqueNonEmptyStrings(values []string) []string {
return out
}
func csvExportRow(fields ...string) []string {
row := make([]string, len(fields))
for i, field := range fields {
row[i] = csvSafeCell(field)
}
return row
}
func csvSafeCell(value string) string {
if value == "" {
return value
}
switch value[0] {
case '=', '+', '-', '@', '\t', '\r':
return "'" + value
default:
return value
}
}
func scimEmailFromUserName(userName string) string {
userName = strings.TrimSpace(userName)
if userName == "" {

View File

@@ -33,16 +33,6 @@ func TestUniqueNonEmptyStrings(t *testing.T) {
assert.Equal(t, []string{"a", "b"}, got)
}
func TestCsvSafeCell(t *testing.T) {
t.Parallel()
assert.Equal(t, "plain", csvSafeCell("plain"))
assert.Equal(t, "'=1+1", csvSafeCell("=1+1"))
assert.Equal(t, "'+cmd", csvSafeCell("+cmd"))
assert.Equal(t, "'-2", csvSafeCell("-2"))
assert.Equal(t, "'@sum", csvSafeCell("@sum"))
}
func TestScimEmailFromUserName(t *testing.T) {
t.Parallel()

View File

@@ -22,7 +22,6 @@ package iam
import (
"context"
"encoding/csv"
"fmt"
"io"
"strings"
@@ -35,6 +34,7 @@ import (
"go.probo.inc/probo/pkg/filemanager"
"go.probo.inc/probo/pkg/gid"
"go.probo.inc/probo/pkg/mail"
"go.probo.inc/probo/pkg/safecsv"
)
type LogExportService struct {
@@ -228,7 +228,7 @@ func (s *LogExportService) streamCSV(
return fmt.Errorf("cannot load organization for log export: %w", err)
}
w := csv.NewWriter(pw)
w := safecsv.NewWriter(pw)
switch exportJob.Type {
case coredata.ExportJobTypeAuditLog: