Skip to content

Commit a72cc6f

Browse files
committed
fix(anonymizer): truncate output files on open to prevent stale JSON
When running the anonymizer tool multiple times pointing to the same output paths, the files were opened with O_CREATE|O_WRONLY but without O_TRUNC. This means any previous content is kept in place and new data is written on top. If the new output happens to be shorter than the old one, the leftover bytes at the end produce malformed JSON that cannot be parsed. Added os.O_TRUNC to all three os.OpenFile calls across writer.go and extractor.go so the file is always zeroed out before writing begins. Also tightened the file creation mode from os.ModePerm (0777) to 0o600 so output files containing potentially sensitive trace data are not world-readable, and included the file path in each error message to make failures easier to diagnose. Fixes: #8231 Signed-off-by: Sakthi Harish <sakthi.harish@edgeverve.com>
1 parent e75b845 commit a72cc6f

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

cmd/anonymizer/app/uiconv/extractor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ type extractor struct {
2424

2525
// newExtractor creates extractor.
2626
func newExtractor(uiFile string, traceID string, reader *spanReader, logger *zap.Logger) (*extractor, error) {
27-
f, err := os.OpenFile(uiFile, os.O_CREATE|os.O_WRONLY, os.ModePerm)
27+
f, err := os.OpenFile(uiFile, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o600)
2828
if err != nil {
29-
return nil, fmt.Errorf("cannot create output file: %w", err)
29+
return nil, fmt.Errorf("cannot create output file %q: %w", uiFile, err)
3030
}
3131
logger.Sugar().Infof("Writing spans to UI file %s", uiFile)
3232

cmd/anonymizer/app/writer/writer.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ func New(config Config, logger *zap.Logger) (*Writer, error) {
4848
}
4949
logger.Sugar().Infof("Current working dir is %s", wd)
5050

51-
cf, err := os.OpenFile(config.CapturedFile, os.O_CREATE|os.O_WRONLY, os.ModePerm)
51+
cf, err := os.OpenFile(config.CapturedFile, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o600)
5252
if err != nil {
53-
return nil, fmt.Errorf("cannot create output file: %w", err)
53+
return nil, fmt.Errorf("cannot create output file %q: %w", config.CapturedFile, err)
5454
}
5555
logger.Sugar().Infof("Writing captured spans to file %s", config.CapturedFile)
5656

57-
af, err := os.OpenFile(config.AnonymizedFile, os.O_CREATE|os.O_WRONLY, os.ModePerm)
57+
af, err := os.OpenFile(config.AnonymizedFile, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o600)
5858
if err != nil {
59-
return nil, fmt.Errorf("cannot create output file: %w", err)
59+
return nil, fmt.Errorf("cannot create output file %q: %w", config.AnonymizedFile, err)
6060
}
6161
logger.Sugar().Infof("Writing anonymized spans to file %s", config.AnonymizedFile)
6262

0 commit comments

Comments
 (0)