Skip to content

Commit

Permalink
Error if redirects file is greater than 64 KiB
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Johnson authored and lidel committed Sep 22, 2022
1 parent 0fce9c8 commit c7b04ee
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
14 changes: 13 additions & 1 deletion redirects.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package redirects

import (
"bufio"
"bytes"
"fmt"
"io"
"net/url"
Expand All @@ -13,6 +14,9 @@ import (
"github.com/ucarion/urlpath"
)

// 64 KiB
const maxFileSizeInBytes = 65536

// A Rule represents a single redirection or rewrite rule.
type Rule struct {
// From is the path which is matched to perform the rule.
Expand Down Expand Up @@ -94,8 +98,16 @@ func Must(v []Rule, err error) []Rule {

// Parse the given reader.
func Parse(r io.Reader) (rules []Rule, err error) {
s := bufio.NewScanner(r)
// not too large
b, err := bufio.NewReaderSize(r, maxFileSizeInBytes+1).Peek(maxFileSizeInBytes + 1)
if err != nil && err != io.EOF {
return nil, err
}
if len(b) > maxFileSizeInBytes {
return nil, fmt.Errorf("redirects file size cannot exceed %d bytes", maxFileSizeInBytes)
}

s := bufio.NewScanner(bytes.NewReader(b))
for s.Scan() {
line := strings.TrimSpace(s.Text())

Expand Down
20 changes: 20 additions & 0 deletions redirects_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package redirects

import (
"bytes"
"strings"
"testing"

Expand Down Expand Up @@ -76,4 +77,23 @@ func Test_Parse(t *testing.T) {
assert.Error(t, err)
assert.Contains(t, err.Error(), "status code 42 is not supported")
})

t.Run("with too large file", func(t *testing.T) {
// create a file larger than 64 KiB, using valid rules so the only possible error is the size
line := "/from /to 301"
bytesPerLine := len(line)
totalBytes := 0

var b bytes.Buffer
for totalBytes <= maxFileSizeInBytes {
totalBytes += bytesPerLine
b.WriteString(line + "\n")
}
text := b.String()

_, err := ParseString(text)

assert.Error(t, err)
assert.Contains(t, err.Error(), "redirects file size cannot exceed")
})
}

0 comments on commit c7b04ee

Please sign in to comment.