Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix double cookie header for Lambda Function URL requests #586

Merged
merged 3 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion lambda-http/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ fn into_api_gateway_v2_request(ag: ApiGatewayV2httpRequest) -> http::Request<Bod
update_xray_trace_id_header(&mut headers);
if let Some(cookies) = ag.cookies {
if let Ok(header_value) = HeaderValue::from_str(&cookies.join(";")) {
headers.append(http::header::COOKIE, header_value);
headers.insert(http::header::COOKIE, header_value);
}
}

Expand Down Expand Up @@ -545,6 +545,42 @@ mod tests {
);
}

#[test]
fn deserializes_lambda_function_url_request_events() {
// from the docs
// https://docs.aws.amazon.com/lambda/latest/dg/urls-invocation.html#urls-payloads
let input = include_str!("../tests/data/lambda_function_url_request.json");
let result = from_str(input);
assert!(
result.is_ok(),
"event was not parsed as expected {:?} given {}",
result,
input
);
let req = result.expect("failed to parse request");
let cookie_header = req
.headers()
.get_all(http::header::COOKIE)
.iter()
.map(|v| v.to_str().unwrap().to_string())
.reduce(|acc, nxt| [acc, nxt].join(";"));

assert_eq!(req.method(), "GET");
assert_eq!(
req.uri(),
"https://id.lambda-url.eu-west-2.on.aws/my/path?parameter1=value1&parameter1=value2&parameter2=value"
);
assert_eq!(cookie_header, Some("test=hi".to_string()));

// Ensure this is an APIGWv2 request (Lambda Function URL requests confirm to API GW v2 Request format)
let req_context = req.request_context();
assert!(
matches!(req_context, RequestContext::ApiGatewayV2(_)),
"expected ApiGatewayV2 context, got {:?}",
req_context
);
}

#[test]
fn deserializes_alb_request_events() {
// from the docs
Expand Down
40 changes: 40 additions & 0 deletions lambda-http/tests/data/lambda_function_url_request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"version": "2.0",
"routeKey": "$default",
"rawPath": "/my/path",
"rawQueryString": "parameter1=value1&parameter1=value2&parameter2=value",
"cookies": [
"test=hi"
],
"headers": {
"x-amzn-tls-cipher-suite": "ECDHE-RSA-AES128-GCM-SHA256",
"x-amzn-tls-version": "TLSv1.2",
"x-amzn-trace-id": "Root=1-5eb33c07-de25b420912dee103a5db434",
"cookie": "test=hi",
"x-forwarded-proto": "https",
"host": "id.lambda-url.eu-west-2.on.aws",
"x-forwarded-port": "443",
"x-forwarded-for": "65.78.31.245",
"accept": "*/*",
"user-agent": "curl/7.68.0"
},
"requestContext": {
"accountId": "123456789012",
"apiId": "xxx",
"domainName": "id.lambda-url.eu-west-2.on.aws",
"domainPrefix": "id",
"http": {
"method": "GET",
"path": "/my/path",
"protocol": "HTTP/1.1",
"sourceIp": "65.78.31.245",
"userAgent": "curl/7.68.0"
},
"requestId": "MIZRNhJtIAMEMDw=",
"routeKey": "$default",
"stage": "$default",
"time": "11/Jan/2023:11:45:34 +0000",
"timeEpoch": 1673437534837
},
"isBase64Encoded": false
}