Skip to content

Commit a272202

Browse files
authored
Merge pull request #36 from fullstorydev/clint/WEB-14177
Support scrubbing gzip encoded bundles
2 parents 5a34026 + a52dacd commit a272202

File tree

9 files changed

+349
-44
lines changed

9 files changed

+349
-44
lines changed

catcher/catcher.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"errors"
77
"fmt"
88
"io"
9-
"io/ioutil"
109
"log"
1110
"net"
1211
"net/http"
@@ -82,7 +81,7 @@ func (service *Service) LastRequestBody() ([]byte, error) {
8281
}
8382

8483
defer request.Body.Close()
85-
body, err := ioutil.ReadAll(request.Body)
84+
body, err := io.ReadAll(request.Body)
8685
if err != nil {
8786
return nil, err
8887
}

relay/main/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@ package main
22

33
import (
44
"flag"
5-
"io/ioutil"
5+
"io"
66
"log"
77
"os"
88
"time"
99

1010
"github.com/fullstorydev/relay-core/relay"
1111
"github.com/fullstorydev/relay-core/relay/config"
1212
"github.com/fullstorydev/relay-core/relay/environment"
13-
"github.com/fullstorydev/relay-core/relay/traffic/plugin-loader"
13+
plugin_loader "github.com/fullstorydev/relay-core/relay/traffic/plugin-loader"
1414
)
1515

1616
var logger = log.New(os.Stdout, "[relay] ", 0)
1717

1818
func readConfigFile(path string) (rawConfigFileBytes []byte, err error) {
1919
if path == "-" {
20-
rawConfigFileBytes, err = ioutil.ReadAll(os.Stdin)
20+
rawConfigFileBytes, err = io.ReadAll(os.Stdin)
2121
return
2222
}
2323

relay/plugins/traffic/content-blocker-plugin/content-blocker-plugin.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ package content_blocker_plugin
2525
import (
2626
"bytes"
2727
"fmt"
28-
"io/ioutil"
28+
"io"
2929
"log"
3030
"net/http"
3131
"os"
@@ -78,7 +78,7 @@ func (f contentBlockerPluginFactory) New(configSection *config.Section) (traffic
7878
}
7979

8080
if regexp, err := regexp.Compile(pattern); err != nil {
81-
return fmt.Errorf(`Could not compile regular expression "%v": %v`, pattern, err)
81+
return fmt.Errorf(`could not compile regular expression "%v": %v`, pattern, err)
8282
} else {
8383
logger.Printf("Added rule: %s %s content matching \"%s\"", mode, contentKind, regexp)
8484
blockers = append(blockers, &contentBlocker{
@@ -94,7 +94,7 @@ func (f contentBlockerPluginFactory) New(configSection *config.Section) (traffic
9494
case "header":
9595
plugin.headerBlockers = append(plugin.headerBlockers, blockers...)
9696
default:
97-
return fmt.Errorf(`Unexpected content kind %s`, contentKind)
97+
return fmt.Errorf(`unexpected content kind %s`, contentKind)
9898
}
9999

100100
return nil
@@ -222,28 +222,26 @@ func (plug contentBlockerPlugin) blockBodyContent(response http.ResponseWriter,
222222
return false
223223
}
224224

225-
processedBody, err := ioutil.ReadAll(request.Body)
225+
processedBody, err := io.ReadAll(request.Body)
226226
if err != nil {
227227
http.Error(response, fmt.Sprintf("Error reading request body: %s", err), 500)
228228
request.Body = http.NoBody
229229
return true
230230
}
231-
initialLength := len(processedBody)
232231

233232
for _, blocker := range plug.bodyBlockers {
234233
processedBody = blocker.Block(processedBody)
235234
}
236235

237236
// If the length of the body has changed, we should update the
238237
// Content-Length header too.
239-
finalLength := len(processedBody)
240-
if finalLength != initialLength {
241-
contentLength := int64(finalLength)
238+
contentLength := int64(len(processedBody))
239+
if contentLength != request.ContentLength {
242240
request.ContentLength = contentLength
243241
request.Header.Set("Content-Length", strconv.FormatInt(contentLength, 10))
244242
}
245243

246-
request.Body = ioutil.NopCloser(bytes.NewBuffer(processedBody))
244+
request.Body = io.NopCloser(bytes.NewBuffer(processedBody))
247245
return false
248246
}
249247

@@ -283,7 +281,7 @@ func (b *contentBlocker) Block(content []byte) []byte {
283281
case excludeMode:
284282
return b.regexp.ReplaceAllLiteral(content, []byte{})
285283
default:
286-
panic(fmt.Errorf("Invalid content blocking mode: %v", b.mode))
284+
panic(fmt.Errorf("invalid content blocking mode: %v", b.mode))
287285
}
288286
}
289287

relay/plugins/traffic/content-blocker-plugin/content-blocker-plugin_test.go

Lines changed: 60 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package content_blocker_plugin_test
22

33
import (
44
"bytes"
5+
"fmt"
56
"net/http"
67
"strconv"
78
"testing"
89

910
"github.com/fullstorydev/relay-core/catcher"
1011
"github.com/fullstorydev/relay-core/relay"
11-
"github.com/fullstorydev/relay-core/relay/plugins/traffic/content-blocker-plugin"
12+
content_blocker_plugin "github.com/fullstorydev/relay-core/relay/plugins/traffic/content-blocker-plugin"
1213
"github.com/fullstorydev/relay-core/relay/test"
1314
"github.com/fullstorydev/relay-core/relay/traffic"
1415
"github.com/fullstorydev/relay-core/relay/version"
@@ -133,7 +134,8 @@ func TestContentBlocking(t *testing.T) {
133134
}
134135

135136
for _, testCase := range testCases {
136-
runContentBlockerTest(t, testCase)
137+
runContentBlockerTest(t, testCase, traffic.Identity)
138+
runContentBlockerTest(t, testCase, traffic.Gzip)
137139
}
138140
}
139141

@@ -185,7 +187,18 @@ type contentBlockerTestCase struct {
185187
expectedHeaders map[string]string
186188
}
187189

188-
func runContentBlockerTest(t *testing.T, testCase contentBlockerTestCase) {
190+
func runContentBlockerTest(t *testing.T, testCase contentBlockerTestCase, encoding traffic.Encoding) {
191+
var encodingStr string
192+
switch encoding {
193+
case traffic.Gzip:
194+
encodingStr = "gzip"
195+
case traffic.Identity:
196+
encodingStr = ""
197+
}
198+
199+
// Add encoding to the test description
200+
desc := fmt.Sprintf("%s (encoding: %v)", testCase.desc, encodingStr)
201+
189202
plugins := []traffic.PluginFactory{
190203
content_blocker_plugin.Factory,
191204
}
@@ -203,36 +216,46 @@ func runContentBlockerTest(t *testing.T, testCase contentBlockerTestCase) {
203216
expectedHeaders[content_blocker_plugin.PluginVersionHeaderName] = version.RelayRelease
204217

205218
test.WithCatcherAndRelay(t, testCase.config, plugins, func(catcherService *catcher.Service, relayService *relay.Service) {
219+
b, err := traffic.EncodeData([]byte(testCase.originalBody), encoding)
220+
if err != nil {
221+
t.Errorf("Test '%v': Error encoding data: %v", desc, err)
222+
return
223+
}
224+
206225
request, err := http.NewRequest(
207226
"POST",
208227
relayService.HttpUrl(),
209-
bytes.NewBufferString(testCase.originalBody),
228+
bytes.NewBuffer(b),
210229
)
211230
if err != nil {
212-
t.Errorf("Test '%v': Error creating request: %v", testCase.desc, err)
231+
t.Errorf("Test '%v': Error creating request: %v", desc, err)
213232
return
214233
}
215234

235+
if encoding == traffic.Gzip {
236+
request.Header.Set("Content-Encoding", "gzip")
237+
}
238+
216239
request.Header.Set("Content-Type", "application/json")
217240
for header, headerValue := range originalHeaders {
218241
request.Header.Set(header, headerValue)
219242
}
220243

221244
response, err := http.DefaultClient.Do(request)
222245
if err != nil {
223-
t.Errorf("Test '%v': Error POSTing: %v", testCase.desc, err)
246+
t.Errorf("Test '%v': Error POSTing: %v", desc, err)
224247
return
225248
}
226249
defer response.Body.Close()
227250

228251
if response.StatusCode != 200 {
229-
t.Errorf("Test '%v': Expected 200 response: %v", testCase.desc, response)
252+
t.Errorf("Test '%v': Expected 200 response: %v", desc, response)
230253
return
231254
}
232255

233256
lastRequest, err := catcherService.LastRequest()
234257
if err != nil {
235-
t.Errorf("Test '%v': Error reading last request from catcher: %v", testCase.desc, err)
258+
t.Errorf("Test '%v': Error reading last request from catcher: %v", desc, err)
236259
return
237260
}
238261

@@ -241,43 +264,58 @@ func runContentBlockerTest(t *testing.T, testCase contentBlockerTestCase) {
241264
if expectedHeaderValue != actualHeaderValue {
242265
t.Errorf(
243266
"Test '%v': Expected header '%v' with value '%v' but got: %v",
244-
testCase.desc,
267+
desc,
245268
expectedHeader,
246269
expectedHeaderValue,
247270
actualHeaderValue,
248271
)
249272
}
250273
}
251274

275+
if lastRequest.Header.Get("Content-Encoding") != encodingStr {
276+
t.Errorf(
277+
"Test '%v': Expected Content-Encoding '%v' but got: %v",
278+
desc,
279+
encodingStr,
280+
lastRequest.Header.Get("Content-Encoding"),
281+
)
282+
}
283+
252284
lastRequestBody, err := catcherService.LastRequestBody()
253285
if err != nil {
254-
t.Errorf("Test '%v': Error reading last request body from catcher: %v", testCase.desc, err)
286+
t.Errorf("Test '%v': Error reading last request body from catcher: %v", desc, err)
255287
return
256288
}
257289

258-
lastRequestBodyStr := string(lastRequestBody)
259-
if testCase.expectedBody != lastRequestBodyStr {
260-
t.Errorf(
261-
"Test '%v': Expected body '%v' but got: %v",
262-
testCase.desc,
263-
testCase.expectedBody,
264-
lastRequestBodyStr,
265-
)
266-
}
267-
268290
contentLength, err := strconv.Atoi(lastRequest.Header.Get("Content-Length"))
269291
if err != nil {
270-
t.Errorf("Test '%v': Error parsing Content-Length: %v", testCase.desc, err)
292+
t.Errorf("Test '%v': Error parsing Content-Length: %v", desc, err)
271293
return
272294
}
273295

274296
if contentLength != len(lastRequestBody) {
275297
t.Errorf(
276298
"Test '%v': Content-Length is %v but actual body length is %v",
277-
testCase.desc,
299+
desc,
278300
contentLength,
279301
len(lastRequestBody),
280302
)
281303
}
304+
305+
decodedRequestBody, err := traffic.DecodeData(lastRequestBody, encoding)
306+
if err != nil {
307+
t.Errorf("Test '%v': Error decoding data: %v", desc, err)
308+
return
309+
}
310+
311+
lastRequestBodyStr := string(decodedRequestBody)
312+
if testCase.expectedBody != lastRequestBodyStr {
313+
t.Errorf(
314+
"Test '%v': Expected body '%v' but got: %v",
315+
desc,
316+
testCase.expectedBody,
317+
lastRequestBodyStr,
318+
)
319+
}
282320
})
283321
}

relay/plugins/traffic/paths-plugin/paths-plugin_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ func runPathsPluginTest(t *testing.T, testCase pathsPluginTestCase) {
279279
lastRequest, err = altCatcherService.LastRequest()
280280
}
281281
if err != nil {
282-
t.Errorf("Error reading last request from catcher: %v", err)
282+
t.Errorf("Text '%v': Error reading last request from catcher: %v", testCase.desc, err)
283283
return
284284
}
285285

0 commit comments

Comments
 (0)