Skip to content

Commit 3aca5ea

Browse files
Add the ability to support inspecting gRPC (protobuf) content.
1 parent 31286f8 commit 3aca5ea

File tree

2 files changed

+43
-17
lines changed

2 files changed

+43
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
* Updated helloworld example to be more configurable allowing it to be used in other example documentation
6+
* Added the ability to support inspecting gRPC (protobuf) content
67

78
## 1.6.2 2019-08-25
89

module.go

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,21 @@ const moduleVersion = "sigsci-module-golang " + version
2424
// data collection and sends it to the Signal Sciences Agent for
2525
// inspection.
2626
type Module struct {
27-
handler http.Handler
28-
rpcNetwork string
29-
rpcAddress string
30-
debug bool
31-
timeout time.Duration
32-
anomalySize int64
33-
anomalyDuration time.Duration
34-
maxContentLength int64
35-
moduleVersion string
36-
serverVersion string
37-
inspector Inspector
38-
inspInit InspectorInitFunc
39-
inspFini InspectorFiniFunc
40-
headerExtractor func(*http.Request) (http.Header, error)
27+
handler http.Handler
28+
rpcNetwork string
29+
rpcAddress string
30+
debug bool
31+
timeout time.Duration
32+
anomalySize int64
33+
anomalyDuration time.Duration
34+
maxContentLength int64
35+
allowUnknownContentLength bool
36+
moduleVersion string
37+
serverVersion string
38+
inspector Inspector
39+
inspInit InspectorInitFunc
40+
inspFini InspectorFiniFunc
41+
headerExtractor func(*http.Request) (http.Header, error)
4142
}
4243

4344
// ModuleConfigOption is a functional config option for configuring the module
@@ -166,6 +167,21 @@ func MaxContentLength(size int64) ModuleConfigOption {
166167
}
167168
}
168169

170+
// AllowUnknownContentLength is a function argument to set the ability
171+
// to read the body when the content length is not specified.
172+
//
173+
// NOTE: This can be dangerous (fill RAM) if set when the max content
174+
// length is not limited by the server itself. This is intended
175+
// for use with gRPC where the max message receive length is limited.
176+
// Do NOT enable this if there is no limit set on the request
177+
// content length!
178+
func AllowUnknownContentLength(allow bool) ModuleConfigOption {
179+
return func(m *Module) error {
180+
m.allowUnknownContentLength = allow
181+
return nil
182+
}
183+
}
184+
169185
// Timeout is a function argument that sets the maximum time to wait until
170186
// receiving a reply from the inspector. Once this timeout is reached, the
171187
// module will fail open.
@@ -496,9 +512,14 @@ func shouldReadBody(req *http.Request, m *Module) bool {
496512
return false
497513
}
498514

499-
// skip reading if post is invalid or too long
500-
if req.ContentLength <= 0 || req.ContentLength > m.maxContentLength {
501-
return false
515+
// A ContentLength of -1 is an unknown length (streamed) and is only
516+
// allowed if explicitly configured. In this case the max content length
517+
// check is bypassed.
518+
if !(m.allowUnknownContentLength && req.ContentLength == -1) {
519+
// skip reading if post is invalid or too long
520+
if req.ContentLength <= 0 || req.ContentLength > m.maxContentLength {
521+
return false
522+
}
502523
}
503524

504525
// only read certain types of content
@@ -526,6 +547,10 @@ func inspectableContentType(s string) bool {
526547
strings.HasPrefix(s, "application/xml") ||
527548
strings.Contains(s, "+xml"):
528549
return true
550+
551+
// gRPC (protobuf)
552+
case strings.HasPrefix(s, "application/grpc"):
553+
return true
529554
}
530555

531556
return false

0 commit comments

Comments
 (0)