|
| 1 | +// Unless explicitly stated otherwise all files in this repository are licensed |
| 2 | +// under the Apache License Version 2.0. |
| 3 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | +// Copyright 2025 Datadog, Inc. |
| 5 | + |
| 6 | +package rum |
| 7 | + |
| 8 | +import ( |
| 9 | + "bytes" |
| 10 | + "io" |
| 11 | + "net/http" |
| 12 | + "net/http/httptest" |
| 13 | + "strings" |
| 14 | + "testing" |
| 15 | + |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | +) |
| 18 | + |
| 19 | +func TestInjector(t *testing.T) { |
| 20 | + payload := []byte("Hello, world!") |
| 21 | + h := func(w http.ResponseWriter, r *http.Request) { |
| 22 | + w.Write(payload) |
| 23 | + } |
| 24 | + injector := NewInjector(h) |
| 25 | + server := httptest.NewServer(injector) |
| 26 | + defer server.Close() |
| 27 | + |
| 28 | + resp, err := http.DefaultClient.Get(server.URL) |
| 29 | + assert.NoError(t, err) |
| 30 | + defer resp.Body.Close() |
| 31 | + |
| 32 | + respBody, err := io.ReadAll(resp.Body) |
| 33 | + assert.NoError(t, err) |
| 34 | + assert.Equal(t, "Hello, world!", string(respBody)) |
| 35 | + // TODO: when Content-Length is implemented, uncomment this. |
| 36 | + // assert.Equal(t, int64(len(payload) + len(snippet)), resp.ContentLength) |
| 37 | +} |
| 38 | + |
| 39 | +func TestInjectorMatch(t *testing.T) { |
| 40 | + cases := []struct { |
| 41 | + in []byte |
| 42 | + want state |
| 43 | + }{ |
| 44 | + {[]byte("hello </head> world"), sDone}, |
| 45 | + {[]byte("noise </ head > tail"), sDone}, // spaces after '/' and before '>' |
| 46 | + {[]byte("nope < /head>"), sInit}, // space between '<' and '/' |
| 47 | + {[]byte("nope </ he ad >"), sInit}, // spaces inside "head" |
| 48 | + {[]byte("ok </\tHead\t\t >"), sDone}, // tabs after '/', spaces before '>' |
| 49 | + {[]byte("partial </hea>"), sInit}, // missing 'd' |
| 50 | + {[]byte("wrong </header>"), sInit}, // extra letters before '>' |
| 51 | + {[]byte("caps </HEAD>"), sDone}, // case-insensitive |
| 52 | + {[]byte("attr-like </head foo>"), sInit}, // rejected by our custom rule |
| 53 | + {[]byte("prefix << / h e a d >"), sInit}, // multiple violations |
| 54 | + } |
| 55 | + |
| 56 | + for _, tc := range cases { |
| 57 | + t.Run(string(tc.in), func(t *testing.T) { |
| 58 | + i := &injector{} |
| 59 | + i.match(tc.in) |
| 60 | + got := i.st |
| 61 | + i.Reset() |
| 62 | + if got != tc.want { |
| 63 | + t.Fatalf("match(%q) = %v; want %v", tc.in, got, tc.want) |
| 64 | + } |
| 65 | + }) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func TestInjectorWrite(t *testing.T) { |
| 70 | + cases := []struct { |
| 71 | + category string |
| 72 | + in string // comma separated chunks |
| 73 | + out string |
| 74 | + }{ |
| 75 | + {"basic", "abc</head>def", "abc<snippet></head>def"}, |
| 76 | + {"basic", "abc</he,ad>def", "abc<snippet></head>def"}, |
| 77 | + {"basic", "abc,</head>def", "abc<snippet></head>def"}, |
| 78 | + {"basic", "abc</head>,def", "abc<snippet></head>def"}, |
| 79 | + {"basic", "abc</h,ea,d>def", "abc<snippet></head>def"}, |
| 80 | + {"basic", "abc,</hea,d>def", "abc<snippet></head>def"}, |
| 81 | + {"no-head", "abc", "abc"}, |
| 82 | + {"no-head", "abc</hea", "abc</hea"}, |
| 83 | + {"empty", "", ""}, |
| 84 | + {"empty", ",", ""}, |
| 85 | + {"incomplete", "abc</he</head>def", "abc</he<snippet></head>def"}, |
| 86 | + {"incomplete", "abc</he,</head>def", "abc</he<snippet></head>def"}, |
| 87 | + {"casing", "abc</HeAd>def", "abc<snippet></HeAd>def"}, |
| 88 | + {"casing", "abc</HEAD>def", "abc<snippet></HEAD>def"}, |
| 89 | + {"spaces", "abc </head>def", "abc <snippet></head>def"}, |
| 90 | + {"spaces", "abc </hea,d>def", "abc <snippet></head>def"}, |
| 91 | + {"spaces", "abc</ head>def", "abc<snippet></ head>def"}, |
| 92 | + {"spaces", "abc</h ead>def", "abc</h ead>def"}, |
| 93 | + {"spaces", "abc</he ad>def", "abc</he ad>def"}, |
| 94 | + {"spaces", "abc</hea d>def", "abc</hea d>def"}, |
| 95 | + {"spaces", "abc</head >def", "abc<snippet></head >def"}, |
| 96 | + {"spaces", "abc</head> def", "abc<snippet></head> def"}, |
| 97 | + } |
| 98 | + |
| 99 | + for _, tc := range cases { |
| 100 | + t.Run(tc.category+":"+tc.in, func(t *testing.T) { |
| 101 | + total := 0 |
| 102 | + recorder := httptest.NewRecorder() |
| 103 | + i := &injector{ |
| 104 | + wrapped: recorder, |
| 105 | + } |
| 106 | + chunks := strings.Split(tc.in, ",") |
| 107 | + for _, chunk := range chunks { |
| 108 | + w, err := i.Write([]byte(chunk)) |
| 109 | + assert.NoError(t, err) |
| 110 | + total += w |
| 111 | + } |
| 112 | + sz, err := i.Flush() |
| 113 | + assert.NoError(t, err) |
| 114 | + total += sz |
| 115 | + body := recorder.Body.String() |
| 116 | + assert.Equal(t, tc.out, body) |
| 117 | + assert.Equal(t, len(tc.out), total) |
| 118 | + }) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +type sinkResponseWriter struct { |
| 123 | + out []byte |
| 124 | +} |
| 125 | + |
| 126 | +func (s *sinkResponseWriter) Header() http.Header { |
| 127 | + return http.Header{} |
| 128 | +} |
| 129 | +func (s *sinkResponseWriter) Write(p []byte) (int, error) { |
| 130 | + s.out = append(s.out, p...) |
| 131 | + return len(p), nil |
| 132 | +} |
| 133 | +func (s *sinkResponseWriter) WriteHeader(int) {} |
| 134 | + |
| 135 | +func BenchmarkInjectorWrite(b *testing.B) { |
| 136 | + b.ReportAllocs() |
| 137 | + b.ResetTimer() |
| 138 | + sink := &sinkResponseWriter{} |
| 139 | + ij := &injector{ |
| 140 | + wrapped: sink, |
| 141 | + } |
| 142 | + for i := 0; i < b.N; i++ { |
| 143 | + ij.Write([]byte("abc</head>def")) |
| 144 | + if !bytes.Equal(sink.out, []byte("abc<snippet></head>def")) { |
| 145 | + b.Fatalf("out is not as expected: %q", sink.out) |
| 146 | + } |
| 147 | + sink.out = sink.out[:0] |
| 148 | + ij.Reset() |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +func FuzzInjectorWrite(f *testing.F) { |
| 153 | + cases := []string{ |
| 154 | + "abc</head>def", |
| 155 | + "abc", |
| 156 | + "abc</hea", |
| 157 | + "abc</he</head>def", |
| 158 | + "abc</HeAd>def", |
| 159 | + "abc</HEAD>def", |
| 160 | + "abc </head>def", |
| 161 | + "abc</ head>def", |
| 162 | + "abc</h ead>def", |
| 163 | + "abc</he ad>def", |
| 164 | + "abc</hea d>def", |
| 165 | + "abc</head >def", |
| 166 | + "abc</head> def", |
| 167 | + "", |
| 168 | + } |
| 169 | + for _, tc := range cases { |
| 170 | + f.Add([]byte(tc)) |
| 171 | + } |
| 172 | + f.Fuzz(func(t *testing.T, in []byte) { |
| 173 | + sink := &sinkResponseWriter{} |
| 174 | + ij := &injector{ |
| 175 | + wrapped: sink, |
| 176 | + } |
| 177 | + ij.Write(in) |
| 178 | + }) |
| 179 | +} |
0 commit comments