Skip to content

aws-lambda passing wrong content-type when payload_passthrough=false #16615

Closed

Description

@caborabo

Description:

When returning the response from a Lambda function to the client Envoy sets the 'content-type' header to 'application/json' instead of the content-type set by the Lambda. It seems the aws-lambda extension is pulling the content-type from the aws lambda api connection (line 351 in aws_lambda_filter.cc)

Repro steps:

Create a Lambda function that responds with content-type = 'text/html' or anything other than 'application/json'. Enable the aws-lambda extension with payload_passthrough = false.

Config:

admin:
  address:
    socket_address: { address: 127.0.0.1, port_value: 9901 }
static_resources:
  listeners:
  - address:
      socket_address: { address: 0.0.0.0, port_value: 443 }
    filter_chains:
    - transport_socket:
        name: envoy.transport_sockets.tls
        typed_config:
          "@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.DownstreamTlsContext
          common_tls_context:
            tls_certificates:
            - certificate_chain:
                filename: "/etc/xx/xx/default.crt"
              private_key:
                filename: "/etc/x/xx/default.key"
      filters:
      - name: envoy.extenstions.filters.network.http_connection_manager.v3
        typed_config:
          "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
          codec_type: AUTO
          stat_prefix: http
          use_remote_address: true
          server_header_transformation: OVERWRITE
          strip_any_host_port: true
          http2_protocol_options:
            max_concurrent_streams: 100
          access_log:
            name: envoy.file_access_log
            typed_config:
              "@type": type.googleapis.com/envoy.extensions.access_loggers.file.v3.FileAccessLog
              path: /var/log/envoy/access.log
          route_config:
            name: local_route
            virtual_hosts:
            - name: local_service
              domains: ["*"]
              routes:
              - match: { prefix: "/" }
                route: { cluster: lambda_egress_gateway }
          http_filters:
          - name: envoy.filters.http.compressor
            typed_config:
              "@type": type.googleapis.com/envoy.extensions.filters.http.compressor.v3.Compressor
              response_direction_config:
                common_config:
                  min_content_length: 100
                  content_type:
                    - text/html
                    - application/json
                disable_on_etag_header: true
              request_direction_config:
                common_config:
                  enabled:
                    default_value: false
                    runtime_key: request_compressor_enabled
              compressor_library:
                name: text_optimized
                typed_config:
                  "@type": type.googleapis.com/envoy.extensions.compression.gzip.compressor.v3.Gzip
                  memory_level: 3
                  window_bits: 10
                  compression_level: best_compression
                  compression_strategy: default_strategy
          - name: envoy.filters.http.aws_lambda
            typed_config:
              "@type": type.googleapis.com/envoy.extensions.filters.http.aws_lambda.v3.Config
              arn: "arn:aws:lambda:us-west-1:062333172176:function:xxxx-myfunction-xxxx"
              payload_passthrough: false 
          - name: envoy.filters.http.router
  clusters:
  - name: lambda_egress_gateway
    connect_timeout: 0.55s
    type: LOGICAL_DNS
    lb_policy: ROUND_ROBIN
    metadata:
      filter_metadata:
        com.amazonaws.lambda:
          egress_gateway: true
    load_assignment:
      cluster_name: lambda_egress_gateway
      endpoints:
      - lb_endpoints:
        - endpoint:
            address:
              socket_address:
                address: lambda.us-west-1.amazonaws.com
                port_value: 443
    transport_socket:
      name: envoy.transport_sockets.tls
      typed_config:
        "@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext
        sni: "*.amazonaws.com"

Logs:

envoy_aws_lambda_content_type_bug.txt

Activity

  1. added
    triage
    on May 21, 2021
  2. rgs1 commented on May 24, 2021

    @rgs1
    Member
  3. added and removed
    triage
    on May 24, 2021
  4. jstewmon commented on May 24, 2021

    @jstewmon
    Contributor

    I think the call to headers.setReferenceContentType in dejsonizeResponse just need to be moved above the iteration of the json response headers, so that the default content type is application/json unless the json response headers field has a content-type key:

    for (auto&& kv : json_resp.headers()) {
    // ignore H2 pseudo-headers (if any)
    if (kv.first[0] == ':') {
    continue;
    }
    headers.setCopy(Http::LowerCaseString(kv.first), kv.second);
    }
    for (auto&& cookie : json_resp.cookies()) {
    headers.addReferenceKey(Http::Headers::get().SetCookie, cookie);
    }
    if (json_resp.status_code() != 0) {
    headers.setStatus(json_resp.status_code());
    }
    headers.setReferenceContentType(Http::Headers::get().ContentTypeValues.Json);

  5. marcomagdy commented on May 24, 2021

    @marcomagdy
    Contributor

    If passthrough is set to false, then the request and the response are assumed to be JSON. For any other content-type set passthrough to true.

  6. jstewmon commented on May 24, 2021

    @jstewmon
    Contributor

    If passthrough is set to false, then the request and the response are assumed to be JSON. For any other content-type set passthrough to true.

    @marcomagdy is there some reason the filter should not use the content-type header from the response? From the filter docs:

    • The headers are used as the HTTP response headers.

    I haven't used this filter myself, but just reviewing the docs and the implementation, it looks like a bug that the content-type of the response is always set to application/json even when the JSON response from the lambda specifies a different content-type.

  7. marcomagdy commented on May 24, 2021

    @marcomagdy
    Contributor

    Yes, I think you're right.
    It's been a long time, so the details elude me sometimes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions