Skip to content

Commit

Permalink
Detect UTF-8 for vtt/srt, error if encoded otherwise
Browse files Browse the repository at this point in the history
  • Loading branch information
nakkamarra committed Oct 3, 2024
1 parent d27006f commit 4e31607
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions srt.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"strings"
"time"
"unicode/utf8"
)

// Constants
Expand Down Expand Up @@ -42,6 +43,10 @@ func ReadFromSRT(i io.Reader) (o *Subtitles, err error) {
for scanner.Scan() {
// Fetch line
line = strings.TrimSpace(scanner.Text())
if !utf8.ValidString(line) {
err = fmt.Errorf("astisub: bytes are not valid utf-8")
return
}
lineNum++

// Remove BOM header
Expand Down
5 changes: 5 additions & 0 deletions webvtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strconv"
"strings"
"time"
"unicode/utf8"

"golang.org/x/net/html"
)
Expand Down Expand Up @@ -127,6 +128,10 @@ func ReadFromWebVTT(i io.Reader) (o *Subtitles, err error) {
for scanner.Scan() {
lineNum++
line = scanner.Text()
if !utf8.ValidString(line) {
err = fmt.Errorf("astisub: bytes are not valid utf-8")
return
}
line = strings.TrimPrefix(line, string(BytesBOM))
if fs := strings.Fields(line); len(fs) > 0 && fs[0] == "WEBVTT" {
break
Expand Down

0 comments on commit 4e31607

Please sign in to comment.