@@ -24,20 +24,21 @@ const moduleVersion = "sigsci-module-golang " + version
24
24
// data collection and sends it to the Signal Sciences Agent for
25
25
// inspection.
26
26
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 )
41
42
}
42
43
43
44
// ModuleConfigOption is a functional config option for configuring the module
@@ -166,6 +167,21 @@ func MaxContentLength(size int64) ModuleConfigOption {
166
167
}
167
168
}
168
169
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
+
169
185
// Timeout is a function argument that sets the maximum time to wait until
170
186
// receiving a reply from the inspector. Once this timeout is reached, the
171
187
// module will fail open.
@@ -496,9 +512,14 @@ func shouldReadBody(req *http.Request, m *Module) bool {
496
512
return false
497
513
}
498
514
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
+ }
502
523
}
503
524
504
525
// only read certain types of content
@@ -526,6 +547,10 @@ func inspectableContentType(s string) bool {
526
547
strings .HasPrefix (s , "application/xml" ) ||
527
548
strings .Contains (s , "+xml" ):
528
549
return true
550
+
551
+ // gRPC (protobuf)
552
+ case strings .HasPrefix (s , "application/grpc" ):
553
+ return true
529
554
}
530
555
531
556
return false
0 commit comments