package config import ( "fmt" "regexp" "gitgud.io/mike/mpv-manager/pkg/constants" ) const hwaMethodAlternation = `auto|auto-safe|nvdec|nvdec-copy|vaapi|vaapi-copy|vulkan|vulkan-copy|d3d11va|d3d11va-copy|videotoolbox|videotoolbox-copy|drm|drm-copy|no` // validMPVConfigValues is shared by every interactive configuration surface. // Keeping this allowlist below the Web and TUI packages prevents either UI // from accepting text that can escape an mpv.conf value onto another line. var validMPVConfigValues = map[string]*regexp.Regexp{ constants.ConfigKeyHardwareDecoder: regexp.MustCompile(`^(` + hwaMethodAlternation + `)(,(` + hwaMethodAlternation + `)){0,3}$`), constants.ConfigKeyVideoOutput: regexp.MustCompile(`^[a-zA-Z0-9_-]+$`), constants.ConfigKeyScaleFilter: regexp.MustCompile(`^[a-zA-Z0-9_-]+$`), constants.ConfigKeyDitherAlgorithm: regexp.MustCompile(`^(no|ordered|error-diffusion|floyd-steinberg)$`), constants.ConfigKeySavePositionOnQuit: regexp.MustCompile(`^(yes|no)$`), constants.ConfigKeyBorder: regexp.MustCompile(`^(yes|no)$`), constants.ConfigKeyProfile: regexp.MustCompile(`^(high-quality|fast|low-latency|sw-fast)$`), constants.ConfigKeyScreenshotFormat: regexp.MustCompile(`^(png|jpg|jpeg|webp|jxl|avif)$`), constants.ConfigKeyScreenshotTagColorspace: regexp.MustCompile(`^(yes|no)$`), constants.ConfigKeyScreenshotHighBitDepth: regexp.MustCompile(`^(yes|no)$`), constants.ConfigKeyScreenshotTemplate: regexp.MustCompile(`^[a-zA-Z0-9_\-\.%:, ()]+$`), constants.ConfigKeyScreenshotDirectory: regexp.MustCompile(`^[\w/\\\-.~:, ]+$`), constants.ConfigKeyScreenshotJpegQuality: regexp.MustCompile(`^(100|[1-9]?\d)$`), constants.ConfigKeyScreenshotPngCompression: regexp.MustCompile(`^[0-9]$`), constants.ConfigKeyScreenshotWebpLossless: regexp.MustCompile(`^(yes|no)$`), constants.ConfigKeyScreenshotWebpQuality: regexp.MustCompile(`^(100|[1-9]?\d)$`), constants.ConfigKeyScreenshotJxlDistance: regexp.MustCompile(`^(15\.0|([0-9]|1[0-4])(\.\d)?|[0-9]|1[0-5])$`), constants.ConfigKeyScreenshotAvifEncoder: regexp.MustCompile(`^[a-zA-Z0-9\-]+$`), constants.ConfigKeyScreenshotAvifPixfmt: regexp.MustCompile(`^[a-zA-Z0-9_\-]*$`), constants.ConfigKeyScreenshotAvifOpts: regexp.MustCompile(`^[a-zA-Z0-9_=,\-\.]+$`), } // ValidateMPVConfigValue validates a key/value accepted by the interactive // config editors. Values not represented in the allowlist fail closed. func ValidateMPVConfigValue(key, value string) error { pattern, ok := validMPVConfigValues[key] if !ok { return fmt.Errorf("invalid config key: %s", key) } if !pattern.MatchString(value) { return fmt.Errorf("invalid value %q for key %q", value, key) } return nil }