fixed out of bound panic in substring filter

This commit is contained in:
BroodjeAap 2023-01-14 12:49:45 +00:00
parent 9580e32387
commit 4cbe9ffbb0
3 changed files with 48 additions and 10 deletions

View file

@ -379,9 +379,9 @@ func getFilterResultXPath(filter *Filter) {
} }
default: default:
{ {
var b bytes.Buffer var b bytes.Buffer
html.Render(&b, node) html.Render(&b, node)
filter.Results = append(filter.Results, html.UnescapeString(b.String())) filter.Results = append(filter.Results, html.UnescapeString(b.String()))
break break
} }
} }
@ -446,9 +446,9 @@ func getFilterResultCSS(filter *Filter) {
} }
default: default:
{ {
var b bytes.Buffer var b bytes.Buffer
html.Render(&b, node) html.Render(&b, node)
filter.Results = append(filter.Results, html.UnescapeString(b.String())) filter.Results = append(filter.Results, html.UnescapeString(b.String()))
break break
} }
} }
@ -514,6 +514,11 @@ func getFilterResultSubstring(filter *Filter) {
} else if from < 0 { } else if from < 0 {
from = len(asRunes) + from from = len(asRunes) + from
} }
if from < 0 {
filter.log("Out of bounds:", from_to)
continue
}
toStr := from_to[1] toStr := from_to[1]
var hasTo bool = true var hasTo bool = true
if toStr == "" { if toStr == "" {
@ -527,8 +532,15 @@ func getFilterResultSubstring(filter *Filter) {
} else if to < 0 { } else if to < 0 {
to = len(asRunes) + to to = len(asRunes) + to
} }
if to < 0 {
filter.log("Out of bounds:", from_to)
continue
}
if hasFrom && hasTo { if hasFrom && hasTo {
sb.WriteString(string(asRunes[from:to])) _, err := sb.WriteString(string(asRunes[from:to]))
if err != nil {
filter.log("Could not substring: ", err)
}
} else if hasFrom { } else if hasFrom {
sb.WriteString(string(asRunes[from:])) sb.WriteString(string(asRunes[from:]))
} else if hasTo { } else if hasTo {

View file

@ -277,6 +277,34 @@ func TestFilterSubstring(t *testing.T) {
} }
} }
func TestFilterSubstringOutOfBounds(t *testing.T) {
var tests = []struct {
Input string
Query string
}{
{"01234", ":-6"},
{"01234", "-6:"},
}
for _, test := range tests {
testname := fmt.Sprintf("%s %s", test.Input, test.Query)
t.Run(testname, func(t *testing.T) {
filter := Filter{
Parents: []*Filter{
{Results: []string{test.Input}},
},
Var1: test.Query,
}
getFilterResultSubstring(
&filter,
)
if len(filter.Logs) == 0 {
t.Errorf("No log message, expected one for OoB")
}
})
}
}
func TestFilterContains(t *testing.T) { func TestFilterContains(t *testing.T) {
var tests = []struct { var tests = []struct {
Input []string Input []string

View file

@ -13,6 +13,4 @@
- ~~inner~~ - ~~inner~~
- ~~attributes~~ - ~~attributes~~
- ~~node~~ - ~~node~~
- tests for all of it - tests for all of it
- substring out of range?
- test it