Skip to content

Headers (User-Agent) containing non UTF-8 are not well handled (502 http error) #509

@bburnichon

Description

@bburnichon

In one of my projects, created ages ago (prior to going async) I could handle the issue by myself:

use lambda_http::{
  lambda_runtime::Context,
  Body, Request, Response,
};

// snipped

    // Call handler function based on path
    fn run(&self, req: Request, ctx: Context) -> Result<Response<Body>, Error> {
        let user_agent = req
            .headers()
            .get("User-Agent")
            .map_or("(no user agent)", |ua| {
                ua.to_str().unwrap_or("(non utf-8 user-agent)")
            });

I could then handle bad headers by simply ignoring them or return a 400 status code.

I should and will upgrade my code to version 0.6.0 but I can check that this is failing still in lambda_runtime even before it is made available for my code.

test.json

{
  "requestContext": {
      "elb": {
          "targetGroupArn": "arn:aws:elasticloadbalancing:region:123456789012:targetgroup/my-target-group/6d0ecf831eec9f09"
      }
  },
  "httpMethod": "OPTIONS",
  "path": "",
  "queryStringParameters": {},
  "headers": {
      "accept": "application/json",
      "content-type": "application/json",
      "host": "my-app-287742195.eu-west-3.elb.amazonaws.com:443",
      "user-agent": "Beno\xEEt's User-Agent",
      "x-amzn-trace-id": "Root=1-5bdb40ca-556d8b0c50dc66f0511bf520",
      "x-forwarded-for": "72.21.198.66",
      "x-forwarded-port": "443",
      "x-forwarded-proto": "https",
      "origin": "example.com"
  },
  "isBase64Encoded": false,
  "body": ""
}

Testing the above payload gives the error:

cargo lambda start &
cargo lambda invoke --data-file test.json
Error: Error("invalid escape", line: 14, column: 27)
[Finished running. Exit status: 1]

I could check the same behavior on a real lambda instance to which I sent an actual request and not a crafted payload as above.

curl https://example.com/myaws-fsafs/test --header "User-Agent: Beno`echo -ne '\xEE'`t's User-Agent"

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions