1
#include "source/extensions/http/original_ip_detection/custom_header/custom_header.h"
2

            
3
#include "source/common/network/utility.h"
4

            
5
namespace Envoy {
6
namespace Extensions {
7
namespace Http {
8
namespace OriginalIPDetection {
9
namespace CustomHeader {
10

            
11
CustomHeaderIPDetection::CustomHeaderIPDetection(
12
    const envoy::extensions::http::original_ip_detection::custom_header::v3::CustomHeaderConfig&
13
        config)
14
8
    : header_name_(config.header_name()),
15
8
      allow_trusted_address_checks_(config.allow_extension_to_set_address_as_trusted()) {
16
8
  if (config.has_reject_with_status()) {
17
4
    const auto reject_code = toErrorCode(config.reject_with_status().code());
18
4
    reject_options_ = {reject_code, ""};
19
4
  }
20
8
}
21

            
22
CustomHeaderIPDetection::CustomHeaderIPDetection(
23
    const std::string& header_name,
24
    absl::optional<Envoy::Http::OriginalIPRejectRequestOptions> reject_options)
25
2
    : header_name_(header_name), reject_options_(reject_options) {}
26

            
27
Envoy::Http::OriginalIPDetectionResult
28
14
CustomHeaderIPDetection::detect(Envoy::Http::OriginalIPDetectionParams& params) {
29
  // NOTE: The ``XFF`` header from this extension is intentionally not appended.
30
  // To preserve the behavior prior to #31831, ``skip_xff_append`` is explicitly set to true.
31
14
  constexpr bool skip_xff_append = true;
32

            
33
14
  auto hdr = params.request_headers.get(header_name_);
34
14
  if (hdr.empty()) {
35
5
    return {nullptr, false, reject_options_, skip_xff_append};
36
5
  }
37

            
38
9
  auto header_value = hdr[0]->value().getStringView();
39
9
  auto addr = Network::Utility::parseInternetAddressNoThrow(std::string(header_value));
40
9
  if (addr) {
41
7
    return {addr, allow_trusted_address_checks_, absl::nullopt, skip_xff_append};
42
7
  }
43

            
44
2
  return {nullptr, false, reject_options_, skip_xff_append};
45
9
}
46

            
47
} // namespace CustomHeader
48
} // namespace OriginalIPDetection
49
} // namespace Http
50
} // namespace Extensions
51
} // namespace Envoy