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

Functions are not executed when CloudFront returns an error from S3 #8

Open
monken opened this issue May 29, 2021 · 5 comments
Open

Comments

@monken
Copy link

monken commented May 29, 2021

If CloudFront serves files from S3, the CloudFront function is not executed if the file doesn't exist on S3 (404). This is especially harmful in the provided example where security headers such as CSP are set. It basically eliminates all protections provided by the security headers. Any XSS vulnerability that exists on the page can easily be exploited by sending the user to a page that doesn't exist and therefore doesn't include any of the security headers.

This is a very common setup for hosting single page applications on S3 in combination with CloudFront and I assume that most developers are unaware of this behavior. There should be a huge disclaimer on this specific example that the CSP headers are only added to requests to files that exist on the S3 bucket. Any error response (even if it was rewritten to return a 200 status code) will not invoke the function and therefore not include any custom headers.

@monken monken changed the title Functions are not executed when CloudFront returns a configured error page Functions are not executed when CloudFront returns an error from S3 May 29, 2021
@vogd
Copy link

vogd commented Jun 22, 2021

This is expected behavior. It is documented now
https://docs.aws.amazon.com/ja_jp/AmazonCloudFront/latest/DeveloperGuide/edge-functions-restrictions.html#edge-function-restrictions-all

As a workaround you can configure redirect rules for error pages on S3 static website endpoint side [1]
This will provide response from origin with 3xx instead of 4xx in that particular example.
In order it to work you should use http webstite endpoint as your origin
DOC-EXAMPLE-BUCKET.s3-website-us-east-1.amazonaws.com
Not S3 API endpoint DOC-EXAMPLE-BUCKET.s3.amazonaws.com [2]

It is possible to cover multiple error scenarios
[
{
"Condition": {
"HttpErrorCodeReturnedEquals": "403"
},
"Redirect": {
"HostName": "yourdomain.net",
"HttpRedirectCode": "302",
"Protocol": "http",
"ReplaceKeyWith": "redirectstaticpage.html"
}
},
{
"Condition": {
"HttpErrorCodeReturnedEquals": "404"
},
"Redirect": {
"HostName": "yourdomain.net",
"HttpRedirectCode": "302",
"Protocol": "http",
"ReplaceKeyWith": "redirectstatic2.html"
}
}
]

@thomasturrell
Copy link
Contributor

Consider using CloudFront Response Headers Policies instead of CloudFront Functions.

@ravick12
Copy link

ravick12 commented Jan 7, 2022

+1 for having the ability to execute cloud functions on 4xx & 5xx status code. Will help with setting appropriate security headers for error pages as well. The workaround is fine, but may be this on cloud functions can be made configurable.

@bmcclellan-cu
Copy link

+1 for having the ability to execute cloud functions on 4xx & 5xx status code. This allows for OAI access control, not having to setup S3 to serve static websites, and allows us to fully control the results with CF functions.

At the very least a very big warning/disclaimer that "viewer return" functions do not execute on 403/404 S3 errors.

@doctornkz-intelas
Copy link

+1 for having the ability to execute cloud functions on 4xx & 5xx status code. The simplest strategy S3+Cloudront doesn't work for SPA and now it looks so fragile and over-complicated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants