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.
TypeScript agent-skillscodex-clideskop-appdubbingimage-generationlocalizationskillsttsvideo-editingvideo-generationvideo-transcriptionvideo-translation
fix: harden subtitle style overrides
wulien4ee2379cdbd6
diff --git a/config/subtitle-style-default.json b/config/subtitle-style-default.jsonindex aba78c0b..fea00f5a 100644--- a/config/subtitle-style-default.json+++ b/config/subtitle-style-default.json@@ -7,7 +7,7 @@"primary_color": "#FFBF00","secondary_color": "#FF0000","outline_color": "#000000",- "back_color": "#00000064",+ "back_color": "&H64000000","bold": true,"italic": false,"underline": false,@@ -31,7 +31,7 @@"primary_color": "#FFBF00","secondary_color": "#FF0000","outline_color": "#000000",- "back_color": "#00000064",+ "back_color": "&H64000000","bold": true,"italic": false,"underline": false,@@ -57,7 +57,7 @@"primary_color": "#FFBF00","secondary_color": "#FF0000","outline_color": "#000000",- "back_color": "#00000064",+ "back_color": "&H64000000","bold": true,"italic": false,"underline": false,@@ -81,7 +81,7 @@"primary_color": "#FFBF00","secondary_color": "#FF0000","outline_color": "#000000",- "back_color": "#00000064",+ "back_color": "&H64000000","bold": true,"italic": false,"underline": false,diff --git a/internal/cli/commands.go b/internal/cli/commands.goindex 0d00f41c..415e4245 100644--- a/internal/cli/commands.go+++ b/internal/cli/commands.go@@ -114,7 +114,7 @@ Flags:--video <file> Optional source video for dubbed output--voice <voice> Provider-specific voice--voice-clone-source <source> Optional voice clone source- --dry-run Validate command without external calls+ --dry-run Validate and write manifest without external calls-h, --help Show this help`case "render-horizontal":@@ -168,7 +168,7 @@ Flags:--task-id <id> Optional task id--prompt <text> Prompt for GPT image cover generation--size <size> Image size, such as 1024x1024 or 1536x1024- --dry-run Validate command without external calls+ --dry-run Validate and write manifest without external calls-h, --help Show this help`case "status":diff --git a/internal/cli/commands_test.go b/internal/cli/commands_test.goindex 9b904ee4..3dab1a85 100644--- a/internal/cli/commands_test.go+++ b/internal/cli/commands_test.go@@ -169,7 +169,7 @@ func TestParseCoverCommandHelp(t *testing.T) {}func TestHelpDryRunTextDoesNotClaimManifestWrites(t *testing.T) {- commands := []string{"subtitle", "tts", "render-horizontal", "render-vertical", "cover"}+ commands := []string{"subtitle", "render-horizontal", "render-vertical"}for _, name := range commands {cmd, err := Parse([]string{name, "--help"})if err != nil {diff --git a/internal/subtitle_style/style.go b/internal/subtitle_style/style.goindex 1cc4cbc2..4a0c9cdb 100644--- a/internal/subtitle_style/style.go+++ b/internal/subtitle_style/style.go@@ -148,7 +148,8 @@ func BuildAssHeader(set *StyleSet, horizontal bool) string {func DialogueTags(s Style) string {var tags strings.Builder- if s.FadeInMS != nil || s.FadeOutMS != nil || s.OverrideTags != "" {+ overrideTags := normalizeOverrideTags(s.OverrideTags)+ if s.FadeInMS != nil || s.FadeOutMS != nil || overrideTags != "" {tags.WriteString("{")if s.FadeInMS != nil || s.FadeOutMS != nil {in := 0@@ -161,7 +162,7 @@ func DialogueTags(s Style) string {}tags.WriteString(fmt.Sprintf(`\fad(%d,%d)`, in, out))}- tags.WriteString(s.OverrideTags)+ tags.WriteString(overrideTags)tags.WriteString("}")}return tags.String()@@ -202,7 +203,11 @@ func NormalizeASSColor(input string) (string, error) {hex = strings.ToUpper(hex)alpha := "00"if len(hex) == 8 {- alpha = hex[6:8]+ alphaValue, err := strconv.ParseUint(hex[6:8], 16, 8)+ if err != nil {+ return "", fmt.Errorf("invalid HTML alpha %q: %w", input, err)+ }+ alpha = fmt.Sprintf("%02X", 255-alphaValue)}red := hex[0:2]green := hex[2:4]@@ -370,14 +375,14 @@ func mergeStyleRest(base, override Style) Style {}func validateScreen(path string, screen ScreenStyle) error {- if err := validateStyle(path+".major", screen.Major); err != nil {+ if err := validateStyle(path+".major", "Major", screen.Major); err != nil {return err}- return validateStyle(path+".minor", screen.Minor)+ return validateStyle(path+".minor", "Minor", screen.Minor)}-func validateStyle(path string, style Style) error {- if err := validateRawASSStyle(path+".raw_ass_style", style.RawASSStyle); err != nil {+func validateStyle(path, expectedName string, style Style) error {+ if err := validateRawASSStyle(path+".raw_ass_style", expectedName, style.RawASSStyle); err != nil {return err}if err := validateASSFieldString(path+".name", style.Name); err != nil {@@ -410,6 +415,12 @@ func validateStyle(path string, style Style) error {if err := validateInt(path+".alignment", style.AlignmentValue, 1, 9); err != nil {return err}+ if err := validateInt(path+".border_style", style.BorderStyle, 1, 3); err != nil {+ return err+ }+ if err := validateInt(path+".encoding", style.Encoding, 0, 255); err != nil {+ return err+ }if err := validateInt(path+".margin_l", style.MarginL, 0, 2000); err != nil {return err}@@ -425,13 +436,22 @@ func validateStyle(path string, style Style) error {if err := validateFloat(path+".shadow", style.Shadow, 0, 20); err != nil {return err}+ if err := validateFloat(path+".spacing", style.Spacing, -100, 100); err != nil {+ return err+ }+ if err := validateFloat(path+".angle", style.Angle, -360, 360); err != nil {+ return err+ }if err := validateInt(path+".fade_in_ms", style.FadeInMS, 0, 10000); err != nil {return err}- return validateInt(path+".fade_out_ms", style.FadeOutMS, 0, 10000)+ if err := validateInt(path+".fade_out_ms", style.FadeOutMS, 0, 10000); err != nil {+ return err+ }+ return validateOverrideTags(path+".override_tags", style.OverrideTags)}-func validateRawASSStyle(path, raw string) error {+func validateRawASSStyle(path, expectedName, raw string) error {if raw == "" {return nil}@@ -441,9 +461,36 @@ func validateRawASSStyle(path, raw string) error {if len(strings.Split(raw, ",")) != 23 {return fmt.Errorf("%s must be a complete simple Style line with 23 comma-separated fields; escaped commas are not supported", path)}+ fields := strings.Split(raw, ",")+ name := strings.TrimPrefix(fields[0], "Style: ")+ if name != expectedName {+ return fmt.Errorf("%s style name must be %q, got %q", path, expectedName, name)+ }return nil}+func validateOverrideTags(path, tags string) error {+ normalized := normalizeOverrideTags(tags)+ if normalized == "" {+ return nil+ }+ if strings.ContainsAny(normalized, "{}\n\r") {+ return fmt.Errorf("%s must not contain braces, newline, or carriage return", path)+ }+ if !strings.HasPrefix(normalized, `\`) {+ return fmt.Errorf("%s must start with an ASS override tag backslash", path)+ }+ return nil+}++func normalizeOverrideTags(tags string) string {+ trimmed := strings.TrimSpace(tags)+ if strings.HasPrefix(trimmed, "{") && strings.HasSuffix(trimmed, "}") {+ trimmed = strings.TrimSpace(strings.TrimSuffix(strings.TrimPrefix(trimmed, "{"), "}"))+ }+ return trimmed+}+func validateColor(path, value string) error {if value == "" {return nildiff --git a/internal/subtitle_style/style_test.go b/internal/subtitle_style/style_test.goindex fb9b41d5..d2194e3b 100644--- a/internal/subtitle_style/style_test.go+++ b/internal/subtitle_style/style_test.go@@ -32,8 +32,8 @@ func TestParseColorConvertsHTMLToASS(t *testing.T) {if err != nil {t.Fatalf("NormalizeASSColor(alpha) error = %v", err)}- if got != "&H80CC6633" {- t.Fatalf("NormalizeASSColor(alpha) = %q, want &H80CC6633", got)+ if got != "&H7FCC6633" {+ t.Fatalf("NormalizeASSColor(alpha) = %q, want &H7FCC6633", got)}}@@ -171,5 +171,62 @@ func TestRawStyleAndDialogueTags(t *testing.T) {}}+func TestValidateRejectsRawStyleWithWrongName(t *testing.T) {+ style := DefaultStyleSet()+ style.Horizontal.Major.RawASSStyle = "Style: MyMajor,Arial,30,&H00FFFFFF,&H000000FF,&H00000000,&H64000000,-1,0,0,0,100,100,0,0,1,4,2,2,20,20,40,1"++ err := Validate(style)+ if err == nil {+ t.Fatal("Validate() error = nil, want raw style name error")+ }+ if !strings.Contains(err.Error(), "horizontal.major.raw_ass_style") || !strings.Contains(err.Error(), "Major") {+ t.Fatalf("error = %v, want raw style name context", err)+ }+}++func TestValidateRejectsUnsafeOverrideTags(t *testing.T) {+ style := DefaultStyleSet()+ style.Horizontal.Major.OverrideTags = `\blur1}+Dialogue: 0,0:00:00.00,0:00:01.00,Major,,0,0,0,,Injected`++ err := Validate(style)+ if err == nil {+ t.Fatal("Validate() error = nil, want unsafe override_tags error")+ }+ if !strings.Contains(err.Error(), "horizontal.major.override_tags") {+ t.Fatalf("error = %v, want override_tags context", err)+ }+}++func TestDialogueTagsNormalizesBraces(t *testing.T) {+ style := DefaultStyleSet()+ style.Horizontal.Major.OverrideTags = `{\blur1}`++ tags := DialogueTags(style.Horizontal.Major)+ if tags != `{\blur1}` {+ t.Fatalf("DialogueTags() = %q, want normalized override tags", tags)+ }+}++func TestValidateRejectsOutOfRangeStyleFields(t *testing.T) {+ style := DefaultStyleSet()+ borderStyle := 4+ encoding := -1+ spacing := 1001.0+ angle := 361.0+ style.Horizontal.Major.BorderStyle = &borderStyle+ style.Horizontal.Minor.Encoding = &encoding+ style.Vertical.Major.Spacing = &spacing+ style.Vertical.Minor.Angle = &angle++ err := Validate(style)+ if err == nil {+ t.Fatal("Validate() error = nil, want out-of-range field error")+ }+ if !strings.Contains(err.Error(), "border_style") {+ t.Fatalf("error = %v, want first invalid field path", err)+ }+}+func intPtr(v int) *int { return &v }func floatPtr(v float64) *float64 { return &v }