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:
@@ -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 == "" {
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
@@ -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:
|
||||
|
||||
49
pkg/safecsv/cell.go
Normal file
49
pkg/safecsv/cell.go
Normal file
@@ -0,0 +1,49 @@
|
||||
// Copyright (c) 2026 Probo Inc <hello@probo.com>.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
package safecsv
|
||||
|
||||
// SanitizeRecord returns a copy of record with spreadsheet-safe cell values.
|
||||
func SanitizeRecord(record []string) []string {
|
||||
if len(record) == 0 {
|
||||
return record
|
||||
}
|
||||
|
||||
out := make([]string, len(record))
|
||||
for i, field := range record {
|
||||
out[i] = SanitizeCell(field)
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
// SanitizeCell prefixes values that spreadsheet tools may interpret as formulas.
|
||||
func SanitizeCell(value string) string {
|
||||
if value == "" {
|
||||
return value
|
||||
}
|
||||
|
||||
switch value[0] {
|
||||
case '=', '+', '-', '@', '\t', '\r':
|
||||
return "'" + value
|
||||
default:
|
||||
return value
|
||||
}
|
||||
}
|
||||
52
pkg/safecsv/cell_test.go
Normal file
52
pkg/safecsv/cell_test.go
Normal file
@@ -0,0 +1,52 @@
|
||||
// Copyright (c) 2026 Probo Inc <hello@probo.com>.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
package safecsv
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestSanitizeCell(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert.Equal(t, "plain", SanitizeCell("plain"))
|
||||
assert.Equal(t, "'=1+1", SanitizeCell("=1+1"))
|
||||
assert.Equal(t, "'+cmd", SanitizeCell("+cmd"))
|
||||
assert.Equal(t, "'-2", SanitizeCell("-2"))
|
||||
assert.Equal(t, "'@sum", SanitizeCell("@sum"))
|
||||
}
|
||||
|
||||
func TestWriterWrite(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var buf bytes.Buffer
|
||||
|
||||
w := NewWriter(&buf)
|
||||
require.NoError(t, w.WriteRow("ok", "=evil"))
|
||||
w.Flush()
|
||||
require.NoError(t, w.Error())
|
||||
|
||||
assert.Equal(t, "ok,'=evil\n", buf.String())
|
||||
}
|
||||
50
pkg/safecsv/writer.go
Normal file
50
pkg/safecsv/writer.go
Normal file
@@ -0,0 +1,50 @@
|
||||
// Copyright (c) 2026 Probo Inc <hello@probo.com>.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
package safecsv
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"io"
|
||||
)
|
||||
|
||||
type Writer struct {
|
||||
inner *csv.Writer
|
||||
}
|
||||
|
||||
func NewWriter(w io.Writer) *Writer {
|
||||
return &Writer{inner: csv.NewWriter(w)}
|
||||
}
|
||||
|
||||
func (w *Writer) Write(record []string) error {
|
||||
return w.inner.Write(SanitizeRecord(record))
|
||||
}
|
||||
|
||||
func (w *Writer) WriteRow(fields ...string) error {
|
||||
return w.Write(fields)
|
||||
}
|
||||
|
||||
func (w *Writer) Flush() {
|
||||
w.inner.Flush()
|
||||
}
|
||||
|
||||
func (w *Writer) Error() error {
|
||||
return w.inner.Error()
|
||||
}
|
||||
Reference in New Issue
Block a user