CVE-2021-41130 (C++, published 2021-10-07T19:15Z)
Description: value: Extensible Service Proxy, a.k.a. ESP is a proxy which enables API management capabilities for JSON/REST or gRPC API services. ESPv1 can be configured to authenticate a JWT token. Its verified JWT claim is passed to the application by HTTP header "X-Endpoint-API-UserInfo", the application can use it to do authorization. But if there are two "X-Endpoint-API-UserInfo" headers from the client, ESPv1 only replaces the first one, the 2nd one will be passed to the application. An attacker can send two "X-Endpoint-API-UserInfo" headers, the second one with a fake JWT claim. Application may use the fake JWT claim to do the authorization. This impacts following ESPv1 usages: 1) Users have configured ESPv1 to do JWT authentication with Google ID Token as described in the referenced google endpoint document. 2) Users backend application is using the info in the "X-Endpoint-API-UserInfo" header to do the authorization. It has been fixed by v1.58.0. You need to patch it in the following ways: * If your docker image is using tag ":1", needs to re-start the container to pick up the new version. The tag ":1" will automatically point to the latest version. * If your docker image tag pings to a specific minor version, e.g. ":1.57". You need to update it to ":1.58" and re-start the container. There are no workaround for this issue.
Severity: MEDIUM (CVSS v3 base score: 5.4)
Weakness: CWE-290 \u2014 Authentication Bypass by Spoofing
This attack-focused weakness is caused by incorrectly implemented authentication schemes that are subject to spoofing attacks.
Repository: https://github.com/cloudendpoints/esp
Files changed:
include/api_manager/request.h
src/api_manager/check_auth.cc
src/api_manager/check_auth_test.cc
src/api_manager/check_service_control.cc
src/api_manager/context/client_ip_extraction_test.cc
src/api_manager/context/request_context.cc
src/api_manager/mock_request.h
src/api_manager/request_handler.cc
src/nginx/request.cc
src/nginx/request.h
src/nginx/t/ApiManager.pm
src/nginx/t/auth_remove_user_info.t
Commit message: Remove X-Endpoint-API-UserInfo for all header occurrences. (#845)



Vulnerable code:
                                           const std::string &value) = 0;
                                          base64_json_buf);
              AddHeaderToBackend(kEndpointApiUserInfo, user_info))
  EXPECT_CALL(*raw_request_,
              AddHeaderToBackend(kEndpointApiUserInfo, kUserInfo_kSub_kIss))
  EXPECT_CALL(*raw_request_,
              AddHeaderToBackend(kEndpointApiUserInfo, kUserInfo_kSub2_kIss2))
  EXPECT_CALL(*raw_request_,
              AddHeaderToBackend(kEndpointApiUserInfo, kUserInfo_kSub_kIss2))
          context->request()->AddHeaderToBackend(kConsumerProjecId,
                                                 info.consumer_project_id);
  MOCK_METHOD2(AddHeaderToBackend,
               utils::Status(const std::string &, const std::string &));
#include "google/api/backend.pb.h"
#include "src/api_manager/auth/lib/json_util.h"
#include "src/api_manager/utils/str_util.h"
  request_->AddHeaderToBackend(kDefaultApiKeyHeaderName, api_key_);
      trace_context_header);
            kXForwardedAuthorizationHeader, origin_auth_header);
      Status status = request()->AddHeaderToBackend(kAuthorizationHeader,
                                                    kBearerPrefix + token);
  MOCK_METHOD2(AddHeaderToBackend,
               utils::Status(const std::string &, const std::string &));
        google::api_manager::auth::kEndpointApiUserInfo, "");
                                                const std::string &value) {
  ngx_table_elt_t *h = nullptr;
      h = &h_in;
      break;
  if (h == nullptr) {
    h = reinterpret_cast<ngx_table_elt_t *>(
  if (ngx_str_copy_from_std(r_->pool, key, &h->key) != NGX_OK ||
      ngx_str_copy_from_std(r_->pool, value, &h->value) != NGX_OK) {
    return utils::Status(Code::INTERNAL, "Out of memory");
  ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r_->connection->log, 0,
                 "updates header to backend: \"%V: %V\"", &h->key, &h->value);
                                           const std::string &value);
my $t = Test::Nginx->new()->has(qw/http proxy/)->plan(14);

Fixed code:
                                           const std::string &value,
                                           bool changeAllOccurrence) = 0;
                                          base64_json_buf, false);
              AddHeaderToBackend(kEndpointApiUserInfo, user_info, false))
  EXPECT_CALL(*raw_request_, AddHeaderToBackend(kEndpointApiUserInfo,
                                                kUserInfo_kSub_kIss, false))
  EXPECT_CALL(*raw_request_, AddHeaderToBackend(kEndpointApiUserInfo,
                                                kUserInfo_kSub2_kIss2, false))
  EXPECT_CALL(*raw_request_, AddHeaderToBackend(kEndpointApiUserInfo,
                                                kUserInfo_kSub_kIss2, false))

          context->request()->AddHeaderToBackend(
              kConsumerProjecId, info.consumer_project_id, false);
  MOCK_METHOD3(AddHeaderToBackend,
               utils::Status(const std::string &, const std::string &, bool));

#include "google/api/backend.pb.h"
#include "src/api_manager/auth/lib/json_util.h"
#include "src/api_manager/utils/str_util.h"

  request_->AddHeaderToBackend(kDefaultApiKeyHeaderName, api_key_, false);
      trace_context_header, false);
            kXForwardedAuthorizationHeader, origin_auth_header, false);
      Status status = request()->AddHeaderToBackend(
          kAuthorizationHeader, kBearerPrefix + token, false);
  MOCK_METHOD3(AddHeaderToBackend,
               utils::Status(const std::string &, const std::string &, bool));
        google::api_manager::auth::kEndpointApiUserInfo, "", true);
                                                const std::string &value,
                                                bool changeAllOccurrence) {
  std::vector<ngx_table_elt_t *> headers;
      headers.push_back(&h_in);
      if (!changeAllOccurrence) {
        break;
      }
  if (headers.empty()) {
    ngx_table_elt_t *h = reinterpret_cast<ngx_table_elt_t *>(
    headers.push_back(h);
  for (size_t i = 0; i < headers.size(); ++i) {
    ngx_table_elt_t *it = headers[i];
    if (ngx_str_copy_from_std(r_->pool, key, &it->key) != NGX_OK ||
        ngx_str_copy_from_std(r_->pool, value, &it->value) != NGX_OK) {
      return utils::Status(Code::INTERNAL, "Out of memory");
    }

  ngx_log_debug3(
      NGX_LOG_DEBUG_HTTP, r_->connection->log, 0,
      "updates header to backend, changeAllOccurrence: '%t', \"%V: %V\"",
      changeAllOccurrence, &h->key, &h->value);
                                           const std::string &value,
                                           bool changeAllOccurrence);
      'header_lines' => \@header_lines,
my $t = Test::Nginx->new()->has(qw/http proxy/)->plan(15);
x-endpoint-api-userinfo: Multiple userinfo should all be removed
my $arr = $r->{header_lines};
my @userinfo_list = ();

foreach (@$arr) {
  push(@userinfo_list, $_) if index($_, 'X-Endpoint-API-UserInfo') != -1;
}
is(join(",", @userinfo_list), 'X-Endpoint-API-UserInfo: ,X-Endpoint-API-UserInfo: ',
   "Multiple userinfo should all be removed");