History

fix(postgresstore): normalize config line endings for DB/disk writes

hkfires committed 1 year ago

Commit f335aee1 parent 61f6a611 file · +13 −2Browse files

Unified · Split

internal/store/postgresstore.go+13−2
@@ -425,7 +425,8 @@ func (s *PostgresStore) syncConfigFromDatabase(ctx context.Context, exampleConfi · 424 unmodified lines
425425 if err = os.MkdirAll(filepath.Dir(s.configPath), 0o700); err != nil {
426426 return fmt.Errorf("postgres store: prepare config directory: %w", err)
427427 }
428− if err = os.WriteFile(s.configPath, []byte(content), 0o600); err != nil {
428+ normalized := normalizeLineEndings(content)
429+ if err = os.WriteFile(s.configPath, []byte(normalized), 0o600); err != nil {
429430 return fmt.Errorf("postgres store: write config to spool: %w", err)
430431 }
431432 }
@@ -528,7 +529,8 @@ func (s *PostgresStore) persistConfig(ctx context.Context, data []byte) error { · 96 unmodified lines
528529 ON CONFLICT (id)
529530 DO UPDATE SET content = EXCLUDED.content, updated_at = NOW()
530531 `, s.fullTableName(s.cfg.ConfigTable))
531− if _, err := s.db.ExecContext(ctx, query, defaultConfigKey, string(data)); err != nil {
532+ normalized := normalizeLineEndings(string(data))
533+ if _, err := s.db.ExecContext(ctx, query, defaultConfigKey, normalized); err != nil {
532534 return fmt.Errorf("postgres store: upsert config: %w", err)
533535 }
534536 return nil
@@ -652,3 +654,12 @@ func labelFor(metadata map[string]any) string { · 117 unmodified lines
652654 func normalizeAuthID(id string) string {
653655 return filepath.ToSlash(filepath.Clean(id))
654656 }
657+
658+func normalizeLineEndings(s string) string {
659+ if s == "" {
660+ return s
661+ }
662+ s = strings.ReplaceAll(s, "\r\n", "\n")
663+ s = strings.ReplaceAll(s, "\r", "\n")
664+ return s
665+}