DEV Community

Kasper Sanguesa-Franz
Kasper Sanguesa-Franz

Posted on

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

Top comments (0)