Skip to content

Commit

Permalink
x-pack/filebeat/input/http_endpoint: add support for base64-encoded H…
Browse files Browse the repository at this point in the history
…MAC headers
  • Loading branch information
efd6 committed May 22, 2024
1 parent a6aa347 commit bd10ab4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ https://github.com/elastic/beats/compare/v8.8.1\...main[Check the HEAD diff]
- Improve reindexing support in security module pipelines. {issue}38224[38224] {pull}[]
- Improve reindexing support in security module pipelines. {issue}38224[38224] {pull}39588[39588]
- Make HTTP Endpoint input GA. {issue}38979[38979] {pull}39410[39410]
- Add support for base64-encoded HMAC headers to HTTP Endpoint. {pull}39655[39655]

*Auditbeat*

Expand Down
22 changes: 21 additions & 1 deletion x-pack/filebeat/input/http_endpoint/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"crypto/hmac"
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
Expand Down Expand Up @@ -69,7 +70,7 @@ func (v *apiValidator) validateRequest(r *http.Request) (status int, err error)
if v.hmacPrefix != "" {
hmacHeaderValue = strings.TrimPrefix(hmacHeaderValue, v.hmacPrefix)
}
signature, err := hex.DecodeString(hmacHeaderValue)
signature, err := decodeHeaderValue(hmacHeaderValue)
if err != nil {
return http.StatusUnauthorized, fmt.Errorf("invalid HMAC signature hex: %w", err)
}
Expand Down Expand Up @@ -104,3 +105,22 @@ func (v *apiValidator) validateRequest(r *http.Request) (status int, err error)

return http.StatusAccepted, nil
}

// decoders is the priority-ordered set of decoders to use for HMAC header values.
var decoders = [...]func(string) ([]byte, error){
hex.DecodeString,
base64.RawStdEncoding.DecodeString,
base64.StdEncoding.DecodeString,
}

func decodeHeaderValue(s string) ([]byte, error) {
var errs []error

Check failure on line 117 in x-pack/filebeat/input/http_endpoint/validate.go

View workflow job for this annotation

GitHub Actions / lint (windows)

Consider pre-allocating `errs` (prealloc)
for _, d := range &decoders {
b, err := d(s)
if err == nil {
return b, nil
}
errs = append(errs, err)
}
return nil, errors.Join(errs...)
}

0 comments on commit bd10ab4

Please sign in to comment.