Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce UTF-8 encoding for input bytes #115

Merged
merged 8 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -43,6 +44,10 @@ func ReadFromSRT(i io.Reader) (o *Subtitles, err error) {
// Fetch line
line = strings.TrimSpace(scanner.Text())
lineNum++
if !utf8.ValidString(line) {
err = fmt.Errorf("astisub: line %d is not valid utf-8", lineNum)
return
}

// Remove BOM header
if lineNum == 1 {
Expand Down
14 changes: 14 additions & 0 deletions srt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package astisub_test
import (
"bytes"
"io/ioutil"
"os"
"testing"

"github.com/asticode/go-astisub"
Expand Down Expand Up @@ -46,3 +47,16 @@ func TestSRTMissingSequence(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, string(c), w.String())
}

func TestNonUTF8SRT(t *testing.T) {
_, err := astisub.OpenFile("./testdata/example-in-non-utf8.srt")
assert.Error(t, err)
}

func BenchmarkOpenSRT(b *testing.B) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove this benchmark?

f, _ := os.Open("./testdata/example-in.srt")
defer f.Close()
for i := 0; i < b.N; i++ {
astisub.ReadFromSRT(f)
}
}
Binary file added testdata/example-in-non-utf8.srt
Binary file not shown.
Binary file added testdata/example-in-non-utf8.vtt
Binary file not shown.
9 changes: 9 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 @@ -128,6 +129,10 @@ func ReadFromWebVTT(i io.Reader) (o *Subtitles, err error) {
lineNum++
line = scanner.Text()
line = strings.TrimPrefix(line, string(BytesBOM))
if !utf8.ValidString(line) {
err = fmt.Errorf("astisub: line %d is not valid utf-8", lineNum)
return
}
if fs := strings.Fields(line); len(fs) > 0 && fs[0] == "WEBVTT" {
break
}
Expand All @@ -144,6 +149,10 @@ func ReadFromWebVTT(i io.Reader) (o *Subtitles, err error) {
// Fetch line
line = strings.TrimSpace(scanner.Text())
lineNum++
if !utf8.ValidString(line) {
err = fmt.Errorf("astisub: line %d is not valid utf-8", lineNum)
return
}

switch {
// Comment
Expand Down
14 changes: 14 additions & 0 deletions webvtt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package astisub_test
import (
"bytes"
"io/ioutil"
"os"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -48,6 +49,11 @@ func TestBroken1WebVTT(t *testing.T) {
assert.Nil(t, err)
}

func TestNonUTF8WebVTT(t *testing.T) {
_, err := astisub.OpenFile("./testdata/example-in-non-utf8.vtt")
assert.Error(t, err)
}

func TestWebVTTWithVoiceName(t *testing.T) {
testData := `WEBVTT

Expand Down Expand Up @@ -227,3 +233,11 @@ func TestWebVTTTags(t *testing.T) {
Text with a <00:06:30.000>timestamp in the middle
`, b.String())
}

func BenchmarkOpenWebVTT(b *testing.B) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove this benchmark?

f, _ := os.Open("./testdata/example-in.vtt")
defer f.Close()
for i := 0; i < b.N; i++ {
astisub.ReadFromWebVTT(f)
}
}
Loading