Skip to content

Formerly KrillinAI, VideoTranslation and Dubbing Tool for 120+ Languages. Open-source AI workspace for creators, powered by Codex. Video downloader, video translation, create videos, images, audios, and edits with Agents in one place. For your Social Media like YouTube, TikTok, Instagram, etc.

fix: harden subtitle style validation

wulien0175eb70ade9
diff --git a/internal/subtitle_style/style.go b/internal/subtitle_style/style.go
index 97211dad..025b3329 100644
--- a/internal/subtitle_style/style.go
+++ b/internal/subtitle_style/style.go
@@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
+ "io"
"os"
"strconv"
"strings"
@@ -77,6 +78,13 @@ func Decode(data []byte, source string) (*StyleSet, error) {
if err := decoder.Decode(&set); err != nil {
return nil, fmt.Errorf("%s: %w", source, err)
}
+ var extra json.RawMessage
+ if err := decoder.Decode(&extra); err != io.EOF {
+ if err != nil {
+ return nil, fmt.Errorf("%s: trailing JSON: %w", source, err)
+ }
+ return nil, fmt.Errorf("%s: trailing JSON after style object", source)
+ }
if err := Validate(&set); err != nil {
return nil, fmt.Errorf("%s: %w", source, err)
}
@@ -85,7 +93,7 @@ func Decode(data []byte, source string) (*StyleSet, error) {
func Merge(base, override *StyleSet) (*StyleSet, error) {
if base == nil {
- base = &StyleSet{}
+ base = DefaultStyleSet()
}
merged := cloneStyleSet(base)
if override != nil {
@@ -231,10 +239,10 @@ func assStyleLine(style Style, fallbackName string) string {
}
name := valueOr(style.Name, fallbackName)
fontName := valueOr(style.FontName, "Arial")
- primary := normalizeOrDefault(style.PrimaryColor, "&H0000BFFF")
- secondary := normalizeOrDefault(style.SecondaryColor, "&H000000FF")
- outlineColor := normalizeOrDefault(style.OutlineColor, "&H00000000")
- backColor := normalizeOrDefault(style.BackColor, "&H64000000")
+ primary := normalizeColorOrEmptyDefault("primary_color", style.PrimaryColor, "&H0000BFFF")
+ secondary := normalizeColorOrEmptyDefault("secondary_color", style.SecondaryColor, "&H000000FF")
+ outlineColor := normalizeColorOrEmptyDefault("outline_color", style.OutlineColor, "&H00000000")
+ backColor := normalizeColorOrEmptyDefault("back_color", style.BackColor, "&H64000000")
return fmt.Sprintf("Style: %s,%s,%d,%s,%s,%s,%s,%d,%d,%d,%d,%d,%d,%s,%s,%d,%s,%s,%d,%d,%d,%d,%d",
name,
fontName,
@@ -367,6 +375,24 @@ func validateStyle(path string, style Style) error {
if err := validateRawASSStyle(path+".raw_ass_style", style.RawASSStyle); err != nil {
return err
}
+ if err := validateASSFieldString(path+".name", style.Name); err != nil {
+ return err
+ }
+ if err := validateASSFieldString(path+".font_name", style.FontName); err != nil {
+ return err
+ }
+ if err := validateColor(path+".primary_color", style.PrimaryColor); err != nil {
+ return err
+ }
+ if err := validateColor(path+".secondary_color", style.SecondaryColor); err != nil {
+ return err
+ }
+ if err := validateColor(path+".outline_color", style.OutlineColor); err != nil {
+ return err
+ }
+ if err := validateColor(path+".back_color", style.BackColor); err != nil {
+ return err
+ }
if err := validateInt(path+".font_size", style.FontSize, 1, 200); err != nil {
return err
}
@@ -408,7 +434,27 @@ func validateRawASSStyle(path, raw string) error {
return fmt.Errorf("%s must start with \"Style: \"", path)
}
if len(strings.Split(raw, ",")) != 23 {
- return fmt.Errorf("%s must contain 23 comma-separated fields", path)
+ return fmt.Errorf("%s must be a complete simple Style line with 23 comma-separated fields; escaped commas are not supported", path)
+ }
+ return nil
+}
+
+func validateColor(path, value string) error {
+ if value == "" {
+ return nil
+ }
+ if _, err := NormalizeASSColor(value); err != nil {
+ return fmt.Errorf("%s: %w", path, err)
+ }
+ return nil
+}
+
+func validateASSFieldString(path, value string) error {
+ if value == "" {
+ return nil
+ }
+ if strings.ContainsAny(value, ",\n\r") {
+ return fmt.Errorf("%s must not contain comma, newline, or carriage return", path)
}
return nil
}
@@ -450,10 +496,13 @@ func cloneStyle(style Style) Style {
return mergeStyle(Style{}, style)
}
-func normalizeOrDefault(input, fallback string) string {
+func normalizeColorOrEmptyDefault(path, input, fallback string) string {
+ if input == "" {
+ return fallback
+ }
normalized, err := NormalizeASSColor(input)
if err != nil {
- return fallback
+ panic(fmt.Sprintf("%s: invalid style color reached ASS generation: %v", path, err))
}
return normalized
}
diff --git a/internal/subtitle_style/style_test.go b/internal/subtitle_style/style_test.go
index f013bfdd..4b888689 100644
--- a/internal/subtitle_style/style_test.go
+++ b/internal/subtitle_style/style_test.go
@@ -76,6 +76,64 @@ func TestMergeKeepsDefaultsForMissingFields(t *testing.T) {
}
}
+func TestValidateRejectsInvalidColorWithFieldPath(t *testing.T) {
+ style := DefaultStyleSet()
+ style.Horizontal.Major.PrimaryColor = "not-a-color"
+
+ err := Validate(style)
+ if err == nil {
+ t.Fatal("Validate() error = nil, want invalid color error")
+ }
+ if !strings.Contains(err.Error(), "horizontal.major.primary_color") {
+ t.Fatalf("error = %v, want field path containing primary_color", err)
+ }
+}
+
+func TestDecodeRejectsTrailingJSON(t *testing.T) {
+ _, err := Decode([]byte(`{"horizontal":{"major":{"primary_color":"#FFFFFF"}}} {"vertical":{}}`), "style.json")
+ if err == nil {
+ t.Fatal("Decode() error = nil, want trailing JSON error")
+ }
+ if !strings.Contains(err.Error(), "trailing") {
+ t.Fatalf("error = %v, want trailing JSON error", err)
+ }
+}
+
+func TestValidateRejectsUnsafeFontName(t *testing.T) {
+ style := DefaultStyleSet()
+ style.Horizontal.Major.FontName = "Arial,Injected"
+
+ err := Validate(style)
+ if err == nil {
+ t.Fatal("Validate() error = nil, want invalid font_name error")
+ }
+ if !strings.Contains(err.Error(), "horizontal.major.font_name") {
+ t.Fatalf("error = %v, want field path containing font_name", err)
+ }
+}
+
+func TestMergeNilBaseUsesDefaults(t *testing.T) {
+ override := &StyleSet{
+ Horizontal: ScreenStyle{
+ Major: Style{PrimaryColor: "#FFFFFF"},
+ },
+ }
+
+ got, err := Merge(nil, override)
+ if err != nil {
+ t.Fatalf("Merge(nil, override) error = %v", err)
+ }
+ if got.Horizontal.Major.PrimaryColor != "#FFFFFF" {
+ t.Fatalf("primary color = %q, want override", got.Horizontal.Major.PrimaryColor)
+ }
+ if got.Vertical.Minor.MarginV == nil || *got.Vertical.Minor.MarginV != 101 {
+ t.Fatalf("vertical minor margin not inherited from defaults: %#v", got.Vertical.Minor.MarginV)
+ }
+ if got.Vertical.Major.Outline == nil || *got.Vertical.Major.Outline != 2.2 {
+ t.Fatalf("vertical major outline not inherited from defaults: %#v", got.Vertical.Major.Outline)
+ }
+}
+
func TestRawStyleAndDialogueTags(t *testing.T) {
style := DefaultStyleSet()
style.Horizontal.Major.RawASSStyle = "Style: Major,Arial,30,&H00FFFFFF,&H000000FF,&H00000000,&H64000000,-1,0,0,0,100,100,0,0,1,4,2,2,20,20,40,1"