mirror of
https://github.com/multipleof4/ntfy.git
synced 2026-10-14 23:47:55 +00:00
Merge branch 'main' into cluster2
This commit is contained in:
@@ -12,6 +12,7 @@ const (
|
||||
loopExecutionLimit = 10_000 // Limit the number of loop executions to prevent execution from taking too long
|
||||
stringLengthLimit = 100_000 // Limit the length of strings to prevent memory issues
|
||||
sliceSizeLimit = 10_000 // Limit the size of slices to prevent memory issues
|
||||
indentSpacesLimit = 100 // Limit indentation width to prevent memory issues; indent allocates spaces*lines bytes
|
||||
)
|
||||
|
||||
// TxtFuncMap produces the function map.
|
||||
|
||||
@@ -116,6 +116,7 @@ func cat(v ...any) string {
|
||||
}
|
||||
|
||||
// indent adds a specified number of spaces at the beginning of each line in a string.
|
||||
// It has a safety limit to prevent excessive memory usage.
|
||||
//
|
||||
// Parameters:
|
||||
// - spaces: The number of spaces to add
|
||||
@@ -123,7 +124,13 @@ func cat(v ...any) string {
|
||||
//
|
||||
// Returns:
|
||||
// - string: The indented string
|
||||
//
|
||||
// Panics:
|
||||
// - If spaces exceeds indentSpacesLimit
|
||||
func indent(spaces int, v string) string {
|
||||
if spaces > indentSpacesLimit {
|
||||
panic(fmt.Sprintf("indent %d exceeds limit of %d", spaces, indentSpacesLimit))
|
||||
}
|
||||
pad := strings.Repeat(" ", spaces)
|
||||
return pad + strings.Replace(v, "\n", "\n"+pad, -1)
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/base32"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -214,6 +215,25 @@ func TestNindent(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestIndentLimit(t *testing.T) {
|
||||
// Indentation beyond a sane width is an amplification attempt: indent allocates
|
||||
// spaces * lines bytes in a single uninterruptible call
|
||||
if err := runt(`{{indent 100 "a"}}`, strings.Repeat(" ", 100)+"a"); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
for _, tpl := range []string{
|
||||
`{{indent 101 "a"}}`,
|
||||
`{{nindent 101 "a"}}`,
|
||||
`{{indent 1000000000 "a"}}`,
|
||||
} {
|
||||
if _, err := runRaw(tpl, nil); err == nil {
|
||||
t.Errorf("expected %s to be rejected", tpl)
|
||||
} else if !strings.Contains(err.Error(), "exceeds limit") {
|
||||
t.Errorf("expected limit error for %s, got: %v", tpl, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestReplace(t *testing.T) {
|
||||
tpl := `{{"I Am Henry VIII" | replace " " "-"}}`
|
||||
if err := runt(tpl, "I-Am-Henry-VIII"); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user