Skip to content

Commit bbb1394

Browse files
committed
Improve aws-s3 gzip file detection to avoid false negatives
Directly check the byte stream for the gzip magic number and deflate compression type. Avoid using http.DetectContentType because it returns the first match it finds while checking many signatures. Closes #29968
1 parent c570227 commit bbb1394

File tree

2 files changed

+4
-9
lines changed

2 files changed

+4
-9
lines changed

CHANGELOG.next.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
110110
- Undo deletion of endpoint config from cloudtrail fileset in {pull}29415[29415]. {pull}29450[29450]
111111
- Make Cisco ASA and FTD modules conform to the ECS definition for event.outcome and event.type. {issue}29581[29581] {pull}29698[29698]
112112
- ibmmq: Fixed `@timestamp` not being populated with correct values. {pull}29773[29773]
113+
- aws-s3: Improve gzip detection to avoid false negatives. {issue}29968[29968]
113114

114115
*Heartbeat*
115116

x-pack/filebeat/input/awss3/s3_objects.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"fmt"
1616
"io"
1717
"io/ioutil"
18-
"net/http"
1918
"reflect"
2019
"strings"
2120
"time"
@@ -375,18 +374,13 @@ func s3ObjectHash(obj s3EventV2) string {
375374
// stream without consuming it. This makes it convenient for code executed after this function call
376375
// to consume the stream if it wants.
377376
func isStreamGzipped(r *bufio.Reader) (bool, error) {
378-
// Why 512? See https://godoc.org/net/http#DetectContentType
379-
buf, err := r.Peek(512)
377+
buf, err := r.Peek(3)
380378
if err != nil && err != io.EOF {
381379
return false, err
382380
}
383381

384-
switch http.DetectContentType(buf) {
385-
case "application/x-gzip", "application/zip":
386-
return true, nil
387-
default:
388-
return false, nil
389-
}
382+
// gzip magic number (1f 8b) and the compression method (08 for DEFLATE).
383+
return bytes.HasPrefix(buf, []byte{0x1F, 0x8B, 0x08}), nil
390384
}
391385

392386
// s3Metadata returns a map containing the selected S3 object metadata keys.

0 commit comments

Comments
 (0)