DEV Community

Kasper Sanguesa-Franz
Kasper Sanguesa-Franz

Posted on

1 1

API Gateway + HTTP Host = no X_forwarded_for header

Background

When deploying our REST API to API Gateway, we found an issue where there was no HTTP_X_FORWARDED_FOR header; this was a security concern for our security team as there was no way for us to get the customers IP address.

We are utilising the x-amazon-apigateway-integration OpenAPI extension to add additional data to the API documentation provided to end-users of our API.

Example

Below is an example of a very simple Open API 3.0 endpoint where we use the stage variable URL to access our internal backend.

/products:
    summary: get all products
    get:
      summary: Retrieve all products
      responses:
        '200':
          description: No issues
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/products'
      x-amazon-apigateway-integration:
        type: http
        httpMethod: GET
        uri: https://${stageVariables.url}/products
        requestParameters:
          default:
            statusCode: 200
Enter fullscreen mode Exit fullscreen mode

Solution

When deploying the REST API, we need to pass the x-forwarded-for header to the http endpoint like below.

/products:
    summary: get all products
    get:
      summary: Retrieve all products
      responses:
        '200':
          description: No issues
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/products'
      x-amazon-apigateway-integration:
        type: http
        httpMethod: GET
        uri: https://${stageVariables.url}/products
        requestParameters:
          integration.request.header.x-forwarded-for: method.request.header.x-forwarded-for
          default:
            statusCode: 200
      parameters:
        - name: x-forwarded-for
          in: header
          required: true
          schema:
            type: string
Enter fullscreen mode Exit fullscreen mode

Next steps

Our x-amazon-API gateway-integration documentation kept growing, so we updated our automated deployment script to add most of these generic items - which helps us keep the documentation clean and without any specific amazon documentation.
I have added an example of our current product endpoint specification.

/products:
    summary: get all products
    get:
      summary: Retrieve all products
      responses:
        '200':
          description: No issues
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/products'
Enter fullscreen mode Exit fullscreen mode

API Trace View

How I Cut 22.3 Seconds Off an API Call with Sentry πŸ‘€

Struggling with slow API calls? Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more β†’

Top comments (0)

Sentry image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay