Files
streamrip-go/internal/config/config.go
T
JorenandClaude Opus 5 c030d1be09 fix: resource leaks, cancellation, races and path traversal
Six correctness bugs that the existing tests could not see. The suite passed
and `go test -race` was clean throughout, because nothing exercised concurrent
token refresh, shared-folder artwork, or hostile metadata.

main(): extract run() int
  main() was a single 1196-line function that registered 20
  `defer mainApp.Close()` handlers and then called os.Exit 150 times, so none
  of them ever ran. Close() is what closes the SQLite handle, flushes the
  progress bars and removes the artwork temp dirs, so every failed rip leaked
  temp directories. main() is now a wrapper around run() int and the command
  bodies return exit codes.

Ctrl-C now stops a rip
  ctx was threaded everywhere but never checked: zero ctx.Err()/ctx.Done()
  in internal/app and internal/download. Cancellation left Rip() iterating
  the whole remaining queue, and the retry ladder retried on a dead context,
  so one Ctrl-C cost three doomed attempts per remaining track. Adds checks to
  the dispatch loops, the worker-spawn loops and the retry ladder, and routes
  the MarkFailed/MarkDownloaded bookkeeping through a detached context so the
  records survive cancellation. Convert and TagFLAC now take a ctx and use
  exec.CommandContext, so a stalled ffmpeg/ffprobe no longer outlives Ctrl-C.

Config race
  Provider token refresh mutated the shared *config.Config and called
  SaveFile() from inside download workers with no lock, racing the struct
  fields and issuing concurrent os.WriteFile against one path. Adds a mutex to
  config.Config plus UpdateTokens(), and routes all five refresh sites through
  it. Each provider client gets an authMu guarding loggedIn and its
  login/refresh path; beatport needed it most, since ensureToken is reached
  from apiRequest.

Artwork cover race
  artwork.Prepare was check-then-write on a path shared by every worker in a
  playlist rip. Two workers both saw a missing cover.jpg, both created it, and
  the loser's cleanup deleted the winner's finished file. Adds a per-folder
  lock and downloads via a unique temp file + rename. The regression test
  fails against the old code with 15 of 16 workers getting no cover.

Path traversal from remote metadata
  naming.CleanName stripped <>:"/\|?* and control chars but not "." , so a
  title of ".." survived into filepath.Join and escaped the download root.
  SoundCloud and Tidal playlist names are user-generated. Now rejects "." and
  "..", trims trailing dots, handles Windows device names, adds DEL to the
  invalid set and caps components at 255 bytes without splitting a rune.
  app.go gains safeJoin as a second line of defence.

Credentials no longer world-readable
  The config holds plaintext provider passwords, Deezer ARL cookies and OAuth
  tokens and was written 0o644. Now 0o600, with 0o700 directories, and an
  existing 0o644 file is tightened on write.

Also: deletes the 9 duplicated *-rip-smoke dev commands (~300 lines fully
subsumed by `rip id`) and the unreachable second dev-help arm; registers
image/png so PNG covers are downscaled instead of being silently saved as
cover.jpg containing PNG bytes; adds the missing .gitignore, which was keeping
a 21MB build artifact one `git add -A` away from the repo.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-10-03 16:52:58 +02:00

429 lines
13 KiB
Go

package config
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"github.com/pelletier/go-toml/v2"
)
const CurrentConfigVersion = "2.2.0"
var ErrOutdatedConfig = errors.New("config version mismatch")
// Config holds the on-disk config (File) and the runtime view with CLI
// overrides applied (Session).
//
// mu guards File, Session and the write to Path. Provider clients refresh OAuth
// tokens from inside concurrent download workers, so both the struct fields and
// the config-file write have to be serialized; without this, two workers
// refreshing at once raced each other and issued two concurrent os.WriteFile
// calls against the same path.
type Config struct {
Path string
mu sync.Mutex
File ConfigData
Session ConfigData
}
type ConfigData struct {
Downloads DownloadsConfig `toml:"downloads"`
Qobuz QobuzConfig `toml:"qobuz"`
Tidal TidalConfig `toml:"tidal"`
Deezer DeezerConfig `toml:"deezer"`
Yandex YandexConfig `toml:"yandex"`
Beatport BeatportConfig `toml:"beatport"`
Soundcloud SoundcloudConfig `toml:"soundcloud"`
Youtube YoutubeConfig `toml:"youtube"`
Database DatabaseConfig `toml:"database"`
Conversion ConversionConfig `toml:"conversion"`
QobuzFilters QobuzDiscographyFilterConfig `toml:"qobuz_filters"`
Artwork ArtworkConfig `toml:"artwork"`
Metadata MetadataConfig `toml:"metadata"`
Filepaths FilepathsConfig `toml:"filepaths"`
LastFM LastFMConfig `toml:"lastfm"`
CLI CLIConfig `toml:"cli"`
Misc MiscConfig `toml:"misc"`
}
type DownloadsConfig struct {
Folder string `toml:"folder"`
SourceSubdirectories bool `toml:"source_subdirectories"`
DiscSubdirectories bool `toml:"disc_subdirectories"`
Concurrency bool `toml:"concurrency"`
MaxConnections int `toml:"max_connections"`
RequestsPerMinute int `toml:"requests_per_minute"`
VerifySSL bool `toml:"verify_ssl"`
}
type QobuzConfig struct {
Quality int `toml:"quality"`
DownloadBooklets bool `toml:"download_booklets"`
UseAuthToken bool `toml:"use_auth_token"`
EmailOrUserID string `toml:"email_or_userid"`
PasswordOrToken string `toml:"password_or_token"`
AppID string `toml:"app_id"`
Secrets []string `toml:"secrets"`
}
type TidalConfig struct {
Quality int `toml:"quality"`
DownloadVideos bool `toml:"download_videos"`
PreferAtmos bool `toml:"prefer_atmos"`
UserID string `toml:"user_id"`
CountryCode string `toml:"country_code"`
AccessToken string `toml:"access_token"`
RefreshToken string `toml:"refresh_token"`
TokenExpiry int64 `toml:"token_expiry"`
}
type DeezerConfig struct {
Quality int `toml:"quality"`
LowerQualityIfNotAvailable bool `toml:"lower_quality_if_not_available"`
ARL string `toml:"arl"`
Email string `toml:"email"`
Password string `toml:"password"`
RefreshToken string `toml:"refresh_token"`
}
type YandexConfig struct {
Quality int `toml:"quality"`
AccessToken string `toml:"access_token"`
UserID string `toml:"user_id"`
}
type BeatportConfig struct {
Quality int `toml:"quality"`
VariousArtistsThreshold int `toml:"various_artists_threshold"`
Username string `toml:"username"`
Password string `toml:"password"`
AccessToken string `toml:"access_token"`
RefreshToken string `toml:"refresh_token"`
TokenExpiry int64 `toml:"token_expiry"`
}
type SoundcloudConfig struct {
Quality int `toml:"quality"`
ClientID string `toml:"client_id"`
AppVersion string `toml:"app_version"`
}
type YoutubeConfig struct {
Quality int `toml:"quality"`
DownloadVideos bool `toml:"download_videos"`
VideoDownloadsFolder string `toml:"video_downloads_folder"`
}
type DatabaseConfig struct {
DownloadsEnabled bool `toml:"downloads_enabled"`
DownloadsPath string `toml:"downloads_path"`
FailedDownloadsEnabled bool `toml:"failed_downloads_enabled"`
FailedDownloadsPath string `toml:"failed_downloads_path"`
}
type ConversionConfig struct {
Enabled bool `toml:"enabled"`
Codec string `toml:"codec"`
SamplingRate int `toml:"sampling_rate"`
BitDepth int `toml:"bit_depth"`
LossyBitrate int `toml:"lossy_bitrate"`
}
type QobuzDiscographyFilterConfig struct {
Extras bool `toml:"extras"`
Repeats bool `toml:"repeats"`
NonAlbums bool `toml:"non_albums"`
Features bool `toml:"features"`
NonStudioAlbums bool `toml:"non_studio_albums"`
NonRemaster bool `toml:"non_remaster"`
}
type ArtworkConfig struct {
Embed bool `toml:"embed"`
EmbedSize string `toml:"embed_size"`
EmbedMaxWidth int `toml:"embed_max_width"`
SaveArtwork bool `toml:"save_artwork"`
SavedMaxWidth int `toml:"saved_max_width"`
}
type MetadataConfig struct {
SetPlaylistToAlbum bool `toml:"set_playlist_to_album"`
RenumberPlaylistTracks bool `toml:"renumber_playlist_tracks"`
ArtistSeparator string `toml:"artist_separator"`
Exclude []string `toml:"exclude"`
}
type FilepathsConfig struct {
AddSinglesToFolder bool `toml:"add_singles_to_folder"`
FolderFormat string `toml:"folder_format"`
TrackFormat string `toml:"track_format"`
RestrictCharacters bool `toml:"restrict_characters"`
TruncateTo int `toml:"truncate_to"`
}
type LastFMConfig struct {
Source string `toml:"source"`
FallbackSource string `toml:"fallback_source"`
}
type CLIConfig struct {
TextOutput bool `toml:"text_output"`
ProgressBars bool `toml:"progress_bars"`
MaxSearchResults int `toml:"max_search_results"`
}
type MiscConfig struct {
Version string `toml:"version"`
CheckForUpdates bool `toml:"check_for_updates"`
}
func Load(path string) (*Config, error) {
resolvedPath, err := resolvePath(path)
if err != nil {
return nil, err
}
if _, err = os.Stat(resolvedPath); errors.Is(err, os.ErrNotExist) {
cfg := DefaultConfigData()
if err = saveConfigData(resolvedPath, cfg); err != nil {
return nil, err
}
return &Config{Path: resolvedPath, File: cfg, Session: cloneConfigData(cfg)}, nil
}
if err != nil {
return nil, err
}
raw, err := os.ReadFile(resolvedPath)
if err != nil {
return nil, err
}
data := DefaultConfigData()
if err = toml.Unmarshal(raw, &data); err != nil {
return nil, err
}
applyRuntimeDefaults(&data)
if data.Misc.Version != CurrentConfigVersion {
return nil, fmt.Errorf("%w: need to update from %q to %q", ErrOutdatedConfig, data.Misc.Version, CurrentConfigVersion)
}
return &Config{Path: resolvedPath, File: data, Session: cloneConfigData(data)}, nil
}
func UpgradeOutdated(path string) (string, error) {
resolvedPath, err := resolvePath(path)
if err != nil {
return "", err
}
raw, err := os.ReadFile(resolvedPath)
if err != nil {
return "", err
}
data := DefaultConfigData()
if err = toml.Unmarshal(raw, &data); err != nil {
return "", err
}
applyRuntimeDefaults(&data)
data.Misc.Version = CurrentConfigVersion
if err = saveConfigData(resolvedPath, data); err != nil {
return "", err
}
return resolvedPath, nil
}
func (c *Config) SaveFile() error {
c.mu.Lock()
defer c.mu.Unlock()
return c.saveFileLocked()
}
func (c *Config) saveFileLocked() error {
return saveConfigData(c.Path, c.File)
}
// UpdateTokens applies fn to both the on-disk and session copies of the config
// under the config lock, then persists the result. Provider token refresh must
// go through this rather than mutating c.File/c.Session directly, because it
// runs concurrently from download workers.
func (c *Config) UpdateTokens(fn func(file, session *ConfigData)) error {
if fn == nil {
return nil
}
c.mu.Lock()
defer c.mu.Unlock()
fn(&c.File, &c.Session)
// An empty Path means there is no config file backing this Config (tests,
// and callers that loaded defaults only); the in-memory update still has to
// happen, but there is nothing to write.
if strings.TrimSpace(c.Path) == "" {
return nil
}
return c.saveFileLocked()
}
func DefaultConfigData() ConfigData {
home, _ := os.UserHomeDir()
appDir := defaultAppDir()
downloadsFolder := filepath.Join(home, "StreamripDownloads")
data := ConfigData{
Downloads: DownloadsConfig{
Folder: downloadsFolder,
SourceSubdirectories: false,
DiscSubdirectories: true,
Concurrency: true,
MaxConnections: 6,
RequestsPerMinute: 60,
VerifySSL: true,
},
Qobuz: QobuzConfig{
Quality: 3,
DownloadBooklets: true,
UseAuthToken: false,
},
Tidal: TidalConfig{
Quality: 3,
DownloadVideos: true,
PreferAtmos: false,
},
Deezer: DeezerConfig{
Quality: 2,
LowerQualityIfNotAvailable: true,
},
Yandex: YandexConfig{
Quality: 2,
},
Beatport: BeatportConfig{
Quality: 3,
VariousArtistsThreshold: 3,
},
Soundcloud: SoundcloudConfig{
Quality: 0,
},
Youtube: YoutubeConfig{
Quality: 0,
DownloadVideos: false,
VideoDownloadsFolder: filepath.Join(downloadsFolder, "YouTubeVideos"),
},
Database: DatabaseConfig{
DownloadsEnabled: true,
DownloadsPath: filepath.Join(appDir, "downloads.db"),
FailedDownloadsEnabled: true,
FailedDownloadsPath: filepath.Join(appDir, "failed_downloads.db"),
},
Conversion: ConversionConfig{
Enabled: false,
Codec: "ALAC",
SamplingRate: 48000,
BitDepth: 24,
LossyBitrate: 320,
},
QobuzFilters: QobuzDiscographyFilterConfig{},
Artwork: ArtworkConfig{
Embed: true,
EmbedSize: "large",
EmbedMaxWidth: -1,
SaveArtwork: true,
SavedMaxWidth: -1,
},
Metadata: MetadataConfig{
SetPlaylistToAlbum: true,
RenumberPlaylistTracks: true,
ArtistSeparator: "; ",
Exclude: []string{},
},
Filepaths: FilepathsConfig{
AddSinglesToFolder: false,
FolderFormat: "{albumartist} - {title} ({year}) [{container}] [{bit_depth}B-{sampling_rate}kHz]",
TrackFormat: "{tracknumber:02}. {artist} - {title}{explicit}",
RestrictCharacters: false,
TruncateTo: 120,
},
LastFM: LastFMConfig{
Source: "qobuz",
},
CLI: CLIConfig{
TextOutput: true,
ProgressBars: true,
MaxSearchResults: 100,
},
Misc: MiscConfig{
Version: CurrentConfigVersion,
CheckForUpdates: true,
},
}
return data
}
func resolvePath(path string) (string, error) {
// 0o700: this directory holds the credential-bearing config and the
// download databases.
if path != "" {
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
return "", err
}
return path, nil
}
appDir := defaultAppDir()
if err := os.MkdirAll(appDir, 0o700); err != nil {
return "", err
}
return filepath.Join(appDir, "config.toml"), nil
}
func defaultAppDir() string {
base, err := os.UserConfigDir()
if err != nil {
return "."
}
return filepath.Join(base, "streamrip")
}
func saveConfigData(path string, data ConfigData) error {
// 0o700/0o600: this file holds plaintext provider passwords, Deezer ARL
// cookies and OAuth access/refresh tokens, so it must not be world-readable.
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
return err
}
b, err := toml.Marshal(data)
if err != nil {
return err
}
if err := os.WriteFile(path, b, 0o600); err != nil {
return err
}
// Tighten an existing file that a previous version created as 0o644.
return os.Chmod(path, 0o600)
}
func applyRuntimeDefaults(data *ConfigData) {
home, _ := os.UserHomeDir()
appDir := defaultAppDir()
if data.Downloads.Folder == "" {
data.Downloads.Folder = filepath.Join(home, "StreamripDownloads")
}
if data.Database.DownloadsPath == "" {
data.Database.DownloadsPath = filepath.Join(appDir, "downloads.db")
}
if data.Database.FailedDownloadsPath == "" {
data.Database.FailedDownloadsPath = filepath.Join(appDir, "failed_downloads.db")
}
if data.Youtube.VideoDownloadsFolder == "" {
data.Youtube.VideoDownloadsFolder = filepath.Join(data.Downloads.Folder, "YouTubeVideos")
}
}
func cloneConfigData(in ConfigData) ConfigData {
out := in
out.Qobuz.Secrets = append([]string(nil), in.Qobuz.Secrets...)
out.Metadata.Exclude = append([]string(nil), in.Metadata.Exclude...)
return out
}