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: create a valid URL for REST APIs and ALBs when the host header i… #369

Merged
merged 2 commits into from
Nov 20, 2021
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
70 changes: 46 additions & 24 deletions lambda-http/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,18 +440,21 @@ impl<'a> From<LambdaRequest<'a>> for http::Request<Body> {
let builder = http::Request::builder()
.method(http_method)
.uri({
format!(
"{}://{}{}",
headers
.get("X-Forwarded-Proto")
.and_then(|val| val.to_str().ok())
.unwrap_or("https"),
headers
.get(http::header::HOST)
.and_then(|val| val.to_str().ok())
.unwrap_or_default(),
path
)
let host = headers.get(http::header::HOST).and_then(|val| val.to_str().ok());
match host {
Some(host) => {
format!(
"{}://{}{}",
headers
.get("X-Forwarded-Proto")
.and_then(|val| val.to_str().ok())
.unwrap_or("https"),
host,
path
)
}
None => path.to_string(),
}
})
// multi-valued query string parameters are always a super
// set of singly valued query string parameters,
Expand Down Expand Up @@ -503,18 +506,21 @@ impl<'a> From<LambdaRequest<'a>> for http::Request<Body> {
let builder = http::Request::builder()
.method(http_method)
.uri({
format!(
"{}://{}{}",
headers
.get("X-Forwarded-Proto")
.and_then(|val| val.to_str().ok())
.unwrap_or("https"),
headers
.get(http::header::HOST)
.and_then(|val| val.to_str().ok())
.unwrap_or_default(),
path
)
let host = headers.get(http::header::HOST).and_then(|val| val.to_str().ok());
match host {
Some(host) => {
format!(
"{}://{}{}",
headers
.get("X-Forwarded-Proto")
.and_then(|val| val.to_str().ok())
.unwrap_or("https"),
host,
path
)
}
None => path.to_string(),
}
})
// multi valued query string parameters are always a super
// set of singly valued query string parameters,
Expand Down Expand Up @@ -759,6 +765,22 @@ mod tests {
assert_eq!(req.uri(), "http://127.0.0.1:3000/hello");
}

#[test]
fn deserialize_apigw_no_host() {
// generated from the 'apigateway-aws-proxy' test event template in the Lambda console
let input = include_str!("../tests/data/apigw_no_host.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");
assert_eq!(req.method(), "GET");
assert_eq!(req.uri(), "/test/hello");
}

#[test]
fn deserialize_with_null() {
#[derive(Debug, PartialEq, Deserialize)]
Expand Down
54 changes: 54 additions & 0 deletions lambda-http/tests/data/apigw_no_host.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"path": "/test/hello",
"headers": {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate, lzma, sdch, br",
"Accept-Language": "en-US,en;q=0.8",
"CloudFront-Forwarded-Proto": "https",
"CloudFront-Is-Desktop-Viewer": "true",
"CloudFront-Is-Mobile-Viewer": "false",
"CloudFront-Is-SmartTV-Viewer": "false",
"CloudFront-Is-Tablet-Viewer": "false",
"CloudFront-Viewer-Country": "US",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36 OPR/39.0.2256.48",
"Via": "1.1 fb7cca60f0ecd82ce07790c9c5eef16c.cloudfront.net (CloudFront)",
"X-Amz-Cf-Id": "nBsWBOrSHMgnaROZJK1wGCZ9PcRcSpq_oSXZNQwQ10OTZL4cimZo3g==",
"X-Forwarded-For": "192.168.100.1, 192.168.1.1",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https"
},
"pathParameters": {
"proxy": "hello"
},
"requestContext": {
"accountId": "123456789012",
"resourceId": "us4z18",
"stage": "test",
"requestId": "41b45ea3-70b5-11e6-b7bd-69b5aaebc7d9",
"identity": {
"cognitoIdentityPoolId": "",
"accountId": "",
"cognitoIdentityId": "",
"caller": "",
"apiKey": "",
"sourceIp": "192.168.100.1",
"cognitoAuthenticationType": "",
"cognitoAuthenticationProvider": "",
"userArn": "",
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36 OPR/39.0.2256.48",
"user": ""
},
"resourcePath": "/{proxy+}",
"httpMethod": "GET",
"apiId": "wt6mne2s9k"
},
"resource": "/{proxy+}",
"httpMethod": "GET",
"queryStringParameters": {
"name": "me"
},
"stageVariables": {
"stageVarName": "stageVarValue"
}
}