AWS-Lambda
view release on metacpan or search on metacpan
lib/AWS/Lambda/PSGI.pm view on Meta::CPAN
so C<X_Forwarded_For> and C<X-Forwarded-For> would both be C<HTTP_X_FORWARDED_FOR>,
and a client could overwrite a header that the AWS front end sets,
such as C<X-Forwarded-For> or C<X-Amzn-Oidc-Identity> of Application Load Balancer.
This is a change from the earlier versions, which passed these headers to the app.
Use C<-> instead of C<_> in the names of your own headers.
=head2 Remote Address
L<AWS::Lambda::PSGI> sets C<REMOTE_ADDR> from the Lambda event, not from the C<X-Forwarded-For> header.
=over 4
=item API Gateway REST API
C<REMOTE_ADDR> is C<requestContext.identity.sourceIp>.
=item API Gateway HTTP API and Lambda Function URLs
C<REMOTE_ADDR> is C<requestContext.http.sourceIp>.
=item Application Load Balancer
Its events have no C<sourceIp>, so C<REMOTE_ADDR> is the last address in the C<X-Forwarded-For> header
that the load balancer sets.
Only the header named exactly C<X-Forwarded-For> is used; a header such as C<X_Forwarded_For> is not.
This address is the client's only while the load balancer appends to C<X-Forwarded-For>,
which is the default of the C<routing.http.xff_header_processing.mode> attribute.
In the C<preserve> mode the client can set it to any value,
and in the C<remove> mode C<REMOTE_ADDR> is not set.
=back
This is a change from the earlier versions, which took C<REMOTE_ADDR> from the last address in C<X-Forwarded-For>.
If another proxy, such as Amazon CloudFront, is in front of API Gateway or Lambda Function URLs,
C<sourceIp> is the address of that proxy, not the viewer's.
If you trust that proxy and want the viewer's address,
take it from C<X-Forwarded-For> in your app,
for example with L<Plack::Middleware::ReverseProxy> or L<Plack::Middleware::XForwardedFor> and the proxy's addresses.
=head2 URL Scheme and Port
L<AWS::Lambda::PSGI> sets C<psgi.url_scheme>, C<HTTPS> and C<SERVER_PORT> from the Lambda event.
=over 4
=item API Gateway and Lambda Function URLs
They accept only HTTPS, so C<psgi.url_scheme> is C<https>, C<HTTPS> is C<ON> and C<SERVER_PORT> is C<443>.
=item Application Load Balancer
They come from the headers named exactly C<X-Forwarded-Proto> and C<X-Forwarded-Port> that the load balancer sets.
A header such as C<X_Forwarded_Proto> is not used.
=back
=head2 Allowed Hosts
Application Load Balancer passes the C<Host> header that the client sends to the function as is,
unless a listener rule checks it, and its events have no other host name.
So C<HTTP_HOST>, and the URLs that the app builds from it (for example the links in password reset emails),
can be any value that the client chooses.
Set the host names that the app serves with the C<allowed_hosts> option of C<new> or C<wrap>,
or with the Lambda environment variable C<PERL5_LAMBDA_PSGI_ALLOWED_HOSTS> (comma-separated).
A request whose C<Host> header is not one of them gets C<400 Bad Request>, and the app is not called.
my $func = AWS::Lambda::PSGI->wrap($app, allowed_hosts => ['example.com', '.example.net']);
# or in the SAM template
Environment:
Variables:
PERL5_LAMBDA_PSGI_ALLOWED_HOSTS: example.com,.example.net
The port in the C<Host> header is ignored, and host names are compared case-insensitively.
A name that starts with a dot, such as C<.example.net>, matches C<example.net> and all its subdomains.
The option applies to all event types, so include every host name that clients use,
such as the custom domain names of API Gateway or the Lambda Function URL.
If neither is set, any C<Host> header is accepted, as in the earlier versions.
=head2 Reverse Proxy
Earlier versions of C<wrap> enabled L<Plack::Middleware::ReverseProxy>. It no longer does.
The values that it derived from the C<X-Forwarded-For>, C<X-Forwarded-Proto> and C<X-Forwarded-Port> headers
now come from the Lambda event as described above.
The AWS front ends do not set C<X-Forwarded-Host>, C<X-Forwarded-Server> and C<X-Forwarded-HTTPS>.
Their values come from the client as is,
so they no longer change C<HTTP_HOST>, C<SERVER_PORT> or C<psgi.url_scheme>.
The app still receives all these headers as request headers.
If a proxy that you trust sets them, enable L<Plack::Middleware::ReverseProxy> in your own app.
=head1 LICENSE
The MIT License (MIT)
Copyright (C) ICHINOSE Shogo.
=head1 AUTHOR
ICHINOSE Shogo E<lt>shogo82148@gmail.comE<gt>
=cut
( run in 0.645 second using v1.01-cache-2.11-cpan-f5f21f1076c )