aws-sam-cli icon indicating copy to clipboard operation
aws-sam-cli copied to clipboard

"authorization" and "cookie" header values are always null

Open akhilkvpv88 opened this issue 7 years ago • 4 comments

I have created a SAM template that includes an API Gateway containing a Lambda Authorizer. In the Authorizer, I have configured 3 header parameters namely - authorization, xXsrfToken, and cookie. When I am calling this API with all the required values, only xXsrfToken value is being received at lambda end and cookie and authorization values are coming null. I am sharing my sam template and AWS Console snapshots below.

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  my-api

  Sample SAM Template for my-api

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 20



Resources:
  TestAuthSAM:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: target/JWTValidateToken-1.0-SNAPSHOT.jar
      Handler: com.test.LambdaFunctionHandler::handleRequest
      Runtime: java8


  MyApi:
      Type: AWS::Serverless::Api
      Properties:
        StageName: v1
        DefinitionBody: 
            swagger: "2.0"
            info:
              version: "2019-04-22T08:30:48Z"
              title: "TestAuth"
            host: "d2xas9f38a.execute-api.us-east-2.amazonaws.com"
            basePath: "/v1"
            schemes:
            - "https"
            paths:
              /testauth:
                post:
                  produces:
                  - "application/json"
                  responses:
                    "200":
                      description: "200 response"
                      schema:
                        $ref: "#/definitions/Empty"
                  security:
                  - HelloWorld: []
                  x-amazon-apigateway-integration:
                    uri: "http://someapi.com/getdata"
                    responses:
                      default:
                        statusCode: "200"
                    passthroughBehavior: "when_no_match"
                    httpMethod: "GET"
                    type: "http"


            definitions:
              Empty:
                type: "object"
                title: "Empty Schema"




        Auth: 
            DefaultAuthorizer: MyLambdaRequestAuthorizer
            Authorizers:
                MyLambdaRequestAuthorizer:
                  FunctionPayloadType: REQUEST
                  FunctionArn: !GetAtt TestAuthSAM.Arn
                  Identity:
                    Headers:
                      - cookie
                      - xXsrfToken
                      - authorization

Outputs:

  TestAuthSAM:
    Description: "Custom Cookie Auth Lambda Function ARN"
    Value: !GetAtt TestAuthSAM.Arn
  TestAuthSAMIamRole:
    Description: "Implicit IAM Role created for Custom Cookie Auth function"
    Value: !GetAtt TestAuthSAM.Arn

When I am calling this API through Postman with all the required values, only xXsrfToken is being received. Cookie and Authorization are coming as null. I tried implementing the same by manually configuring everything and in that case, it is working as expected.

My Request class for Lambda Authorizer is as given below:

public class RequestAuthorizerContext {

private String type;
private String methodArn;
private RequestHeader headers;
private String sourceIp;
private Identity identity;

public RequestAuthorizerContext(){}

public RequestAuthorizerContext(String type, String methodArn, RequestHeader headers) {
    this.type = type;
    this.methodArn = methodArn;
    this.headers = headers;
}

public String getSourceIp() {
    return sourceIp;
}

public void setSourceIp(String sourceIp) {
    this.sourceIp = sourceIp;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public String getMethodArn() {
    return methodArn;
}

public void setMethodArn(String methodArn) {
    this.methodArn = methodArn;
}

public RequestHeader getHeaders() {
    return headers;
}

public void setHeaders(RequestHeader headers) {
    this.headers = headers;
}

public Identity getIdentity() {
    return identity;
}

public void setIdentity(Identity identity) {
    this.identity = identity;
}

public static class RequestHeader {
    @JsonProperty("X-XSRF-TOKEN")
    private String xXsrfToken;
    private String cookie;
    private String authorization;

    public RequestHeader(){}

    public RequestHeader(String xXsrfToken, String cookie, String authorization) {
        this.xXsrfToken = xXsrfToken;
        this.cookie = cookie;
        this.authorization = authorization;
    }

    public String getxXsrfToken() { return xXsrfToken; }

    public void setxXsrfToken(String xXsrfToken) { this.xXsrfToken = xXsrfToken; }

    public String getCookie() { return cookie; }

    public void setCookie(String cookie) { this.cookie = cookie; }

    public String getAuthorization() { return authorization; }

    public void setAuthorization(String authorization) { this.authorization = authorization; }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}

public static class Identity {
    private String sourceIp;

    public String getSourceIp() {
        return sourceIp;
    }

    public void setSourceIp(String sourceIp) {
        this.sourceIp = sourceIp;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}

@Override
public String toString() {
    return ToStringBuilder.reflectionToString(this);
}
}

As per my analysis, the header names like "cookie" and "authorization" are not being supported by aws-sam-cli. If I change these header names, then I am able to receive the values. With these names (cookie and authorization), values received are always null.

akhilkvpv88 avatar Apr 24 '19 05:04 akhilkvpv88

I debugged it further and found that "cookie" is getting converted to "Cookie" and "authorization" is getting converted to "Authorization". However, in my lambda (Written in Java), I have defined variables in lowercase (like cookie and authorization) and their getters/setters accordingly. Why is this transformation happening? Can anyone please help?

akhilkvpv88 avatar Apr 24 '19 06:04 akhilkvpv88

https://github.com/aws/serverless-application-model/blob/abde7719eda13cb671af3dead526763cb4071f46/samtranslator/model/apigatewayv2.py there is no transformation which seems to happen which does anything with header names and their casing. Are you still experiencing this?

sriram-mv avatar Feb 21 '21 23:02 sriram-mv

Closing as there is no response.

CoshUS avatar Mar 17 '21 06:03 CoshUS

If anyone ever come to this issue, not necessarily with aws-sam-cli, make sure you are not using a custom typescript model on your code defining as authorization or cookie, always define with uppercase letter e.g. Authorization or Cookie.

lucasandre22 avatar Jul 20 '26 17:07 lucasandre22