Debian Codegraph @

server/server_template_test.go

raw
1 package server
2
3 import (
4 "strings"
5 "testing"
6
7 "github.com/stretchr/testify/require"
8 )
9
10 func TestServer_MessageTemplate_TooLarge(t *testing.T) {
11 forEachBackend(t, func(t *testing.T, databaseURL string) {
12 t.Parallel()
13 s := newTestServer(t, newTestConfig(t, databaseURL))
14 response := request(t, s, "PUT", "/mytopic", `{"foo":"bar"}`, map[string]string{
15 "X-Message": "{{.foo}}" + strings.Repeat("x", 33*1024),
16 "X-Template": "1",
17 })
18 require.Equal(t, 400, response.Code)
19 require.Equal(t, 40056, toHTTPError(t, response.Body.String()).Code)
20 })
21 }
22
23 func TestServer_MessageTemplate_PrintfWidthTooLarge(t *testing.T) {
24 forEachBackend(t, func(t *testing.T, databaseURL string) {
25 t.Parallel()
26 s := newTestServer(t, newTestConfig(t, databaseURL))
27 // A handful of 1MB-wide verbs would allocate several MB inside a single fmt call, where
28 // the executor's context is never checked; the printf guard must reject the call before
29 // fmt runs, not after the limit writer sees the output
30 response := request(t, s, "PUT", "/mytopic", `{"n":1}`, map[string]string{
31 "X-Message": `{{printf "%1000000d%1000000d%1000000d" .n .n .n}}`,
32 "X-Template": "1",
33 })
34 require.Equal(t, 400, response.Code)
35 require.Equal(t, 40045, toHTTPError(t, response.Body.String()).Code)
36 require.Contains(t, response.Body.String(), "printf width or precision too large")
37 })
38 }
39
40 func TestServer_MessageTemplate_PrintfWidthTooLarge_DynamicFormat(t *testing.T) {
41 forEachBackend(t, func(t *testing.T, databaseURL string) {
42 t.Parallel()
43 s := newTestServer(t, newTestConfig(t, databaseURL))
44 // The format string is assembled at execution time, so the guard must inspect the actual
45 // argument, not the template source
46 response := request(t, s, "PUT", "/mytopic", `{"n":1}`, map[string]string{
47 "X-Message": `{{$f := print "%" "999999" "d" "%" "999999" "d"}}{{printf $f .n .n}}`,
48 "X-Template": "1",
49 })
50 require.Equal(t, 400, response.Code)
51 require.Contains(t, response.Body.String(), "printf width or precision too large")
52 })
53 }
54
55 func TestServer_MessageTemplate_PrintfStarWidthTooLarge(t *testing.T) {
56 forEachBackend(t, func(t *testing.T, databaseURL string) {
57 t.Parallel()
58 s := newTestServer(t, newTestConfig(t, databaseURL))
59 // Star widths take the width from an argument; sprig's math functions (int64 results)
60 // make large integer arguments reachable from a template
61 response := request(t, s, "PUT", "/mytopic", `{"n":1}`, map[string]string{
62 "X-Message": `{{printf "%*d" (mul 1000 2000) 1}}`,
63 "X-Template": "1",
64 })
65 require.Equal(t, 400, response.Code)
66 require.Contains(t, response.Body.String(), "printf width or precision too large")
67 })
68 }
69
70 func TestServer_MessageTemplate_PrintfSmallWidthStillWorks(t *testing.T) {
71 forEachBackend(t, func(t *testing.T, databaseURL string) {
72 t.Parallel()
73 s := newTestServer(t, newTestConfig(t, databaseURL))
74 response := request(t, s, "PUT", "/mytopic", `{"n":7}`, map[string]string{
75 "X-Message": `{{printf "%05d" 7}}`,
76 "X-Template": "1",
77 })
78 require.Equal(t, 200, response.Code)
79 require.Equal(t, "00007", toMessage(t, response.Body.String()).Message)
80 })
81 }
82
83 func Test_templatePrintf(t *testing.T) {
84 tests := []struct {
85 format string
86 args []any
87 want string // Empty means the call must be rejected
88 }{
89 {"%d", []any{5}, "5"},
90 {"%05d", []any{5}, "00005"},
91 {"%-8.3f|", []any{1.5}, "1.500 |"},
92 {"%1000d", []any{1}, ""}, // Rejected: four digits
93 {"%.1000s", []any{"x"}, ""},
94 {"%*d", []any{500, 1}, ""}, // Rejected: star width
95 {"%.*s", []any{400, "x"}, ""}, // Rejected: star precision
96 {"%[1]1000000d", []any{1}, ""}, // Rejected: explicit arg index does not hide the width
97 {"%[2]*[1]d", []any{6, 12}, ""}, // Rejected: star width behind an arg index
98 {"100%% of 2024 values", nil, "100% of 2024 values"}, // Literal digits are not a width
99 }
100 for _, test := range tests {
101 out, err := templatePrintf(test.format, test.args...)
102 if test.want == "" {
103 require.Error(t, err, "format %q must be rejected", test.format)
104 require.Contains(t, err.Error(), "too large")
105 } else {
106 require.Nil(t, err, "format %q", test.format)
107 require.Equal(t, test.want, out)
108 }
109 }
110
111 // The largest allowed width still produces bounded output
112 out, err := templatePrintf("%999d", 1)
113 require.Nil(t, err)
114 require.Len(t, out, 999)
115 }
116
117 func TestServer_MessageTemplate_DisallowedCallInChain(t *testing.T) {
118 forEachBackend(t, func(t *testing.T, databaseURL string) {
119 t.Parallel()
120 s := newTestServer(t, newTestConfig(t, databaseURL))
121 // {{call}} behind a field access parses into a ChainNode. JSON data cannot produce a
122 // function value, so this cannot be exploited today, but the ban must catch every
123 // syntactic form rather than relying on the call failing at runtime.
124 response := request(t, s, "PUT", "/mytopic", `{"fn":1}`, map[string]string{
125 "X-Message": `{{(call .fn).x}}`,
126 "X-Template": "1",
127 })
128 require.Equal(t, 400, response.Code)
129 require.Equal(t, 40044, toHTTPError(t, response.Body.String()).Code)
130 })
131 }