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
425 if err = os.MkdirAll(filepath.Dir(s.configPath), 0o700); err != nil {425 if err = os.MkdirAll(filepath.Dir(s.configPath), 0o700); err != nil {
426 return fmt.Errorf("postgres store: prepare config directory: %w", err)426 return fmt.Errorf("postgres store: prepare config directory: %w", err)
427 }427 }
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 {
429 return fmt.Errorf("postgres store: write config to spool: %w", err)430 return fmt.Errorf("postgres store: write config to spool: %w", err)
430 }431 }
431 }432 }
@@ -528,7 +529,8 @@ func (s *PostgresStore) persistConfig(ctx context.Context, data []byte) error { · 96 unmodified lines
528 ON CONFLICT (id)529 ON CONFLICT (id)
529 DO UPDATE SET content = EXCLUDED.content, updated_at = NOW()530 DO UPDATE SET content = EXCLUDED.content, updated_at = NOW()
530 `, s.fullTableName(s.cfg.ConfigTable))531 `, 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 {
532 return fmt.Errorf("postgres store: upsert config: %w", err)534 return fmt.Errorf("postgres store: upsert config: %w", err)
533 }535 }
534 return nil536 return nil
@@ -652,3 +654,12 @@ func labelFor(metadata map[string]any) string { · 117 unmodified lines
652func normalizeAuthID(id string) string {654func normalizeAuthID(id string) string {
653 return filepath.ToSlash(filepath.Clean(id))655 return filepath.ToSlash(filepath.Clean(id))
654}656}
657
658func 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}