How to block public access to buckets and objects granted through ACL?
** Which Category is your question related to? **
Hosting
** What AWS Services are you utilizing? **
Amplify, CloudFront, S3.
** Provide additional details e.g. code snippets **
As described in this issue: Amplify hosting with CloudFront and S3 still allows direct access to the S3 bucket with HTTP #1599, when creating an app using
-
amplify add hosting- 1a - choose the PROD option for CloudFront and S3 (not the DEV option)
-
amplify push -
amplify publish
Amplify Hosting sets up the CloudFront and S3 bucket. The resulting web is accessible using the CloudFront url, but also with the S3 url. The desired behavior is that the web will only be accessible through the CloudFront url, and direct requests to S3 will get Access Denied.
Objects are uploaded with ACL Public Read and this allows direct access, so the solution is manually set "block public access to buckets and objects granted through new and any ACL" to off inside the 'Block public access' section in the 'Permissions' tab for the S3 bucket. This way it's possible to use a bucket policy as recommended by the AWS documentation.
However, this solution is not really convenient for development. Is it possible to set ACL in S3AndCloudFront/template.json ?
Thank you
I have to add an update to my question. After blocking public access from new and any ACL, while amplify push will run without any issue, it is not possible to run amplify publish anymore. It fails with this error:
chrs@Maxwell:~/web$ amplify publish -c
Current Environment: dev
| Category | Resource name | Operation | Provider plugin |
| -------- | -------------------- | --------- | ----------------- |
| Function | querySearch | No Change | awscloudformation |
| Function | querySearchCreateLog | No Change | awscloudformation |
| Api | web | No Change | awscloudformation |
| Hosting | S3AndCloudFront | No Change | awscloudformation |
No changes detected
> [email protected] build /Users/chrs/web
> node scripts/build.js
dotenv-flow: "HOST" is already defined in `process.env` and will not be overwritten
Creating an optimized production build...
Compiled with warnings.
File sizes after gzip:
556.83 KB build/static/js/2.d3638d87.chunk.js
58.21 KB build/static/css/2.e8d046a2.chunk.css
5.66 KB build/static/js/main.91725953.chunk.js
763 B build/static/js/runtime~main.09088342.js
370 B build/static/css/main.78301fc5.chunk.css
The project was built assuming it is hosted at the server root.
You can control this with the homepage field in your package.json.
For example, add this to build it for GitHub Pages:
"homepage" : "http://myname.github.io/myapp",
The build folder is ready to be deployed.
You may serve it with a static server:
npm install -g serve
serve -s build
Find out more about deployment here:
https://bit.ly/CRA-deploy
frontend build command exited with code 0
✖ Error has occured during file upload.
2019-08-22T20:15:06.846Z - error: uncaughtException: Access Denied
date=Thu Aug 22 2019 16:15:06 GMT-0400 (Eastern Daylight Time),
pid=9864, uid=501, gid=20, cwd=/Users/chrs/web,
execPath=/usr/local/Cellar/node/12.8.1/bin/node, version=v12.8.1,
argv=[/usr/local/Cellar/node/12.8.1/bin/node, /usr/local/bin/amplify, publish, -c],
rss=171339776, heapTotal=131620864, heapUsed=89733528, external=20871376,
loadavg=[7.36865234375, 6.3056640625, 5.462890625], uptime=115287,
trace=[column=35, file=/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/aws-sdk/lib/services/s3.js, function=Request.extractError, line=585, method=extractError, native=false, column=20, file=/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/aws-sdk/lib/sequential_executor.js, function=Request.callListeners, line=106, method=callListeners, native=false, column=10, file=/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/aws-sdk/lib/sequential_executor.js, function=Request.emit, line=78, method=emit, native=false,
column=14, file=/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/aws-sdk/lib/request.js, function=Request.emit, line=683, method=emit, native=false, column=10,
file=/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/aws-sdk/lib/request.js, function=Request.transition, line=22, method=transition, native=false, column=12, file=/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/aws-sdk/lib/state_machine.js, function=AcceptorStateMachine.runTo, line=14, method=runTo, native=false, column=10, file=/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/aws-sdk/lib/state_machine.js, function=null, line=26, method=null, native=false, column=9, file=/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/aws-sdk/lib/request.js, function=null, line=38, method=null, native=false, column=12, file=/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/aws-sdk/lib/request.js, function=null, line=685, method=null, native=false, column=18, file=/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/aws-sdk/lib/sequential_executor.js, function=Request.callListeners, line=116, method=callListeners, native=false], stack=[AccessDenied: Access Denied,
at Request.extractError (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/aws-sdk/lib/services/s3.js:585:35),
at Request.callListeners (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/aws-sdk/lib/sequential_executor.js:106:20),
at Request.emit (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/aws-sdk/lib/sequential_executor.js:78:10),
at Request.emit (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/aws-sdk/lib/request.js:683:14),
at Request.transition (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/aws-sdk/lib/request.js:22:10),
at AcceptorStateMachine.runTo (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/aws-sdk/lib/state_machine.js:14:12),
at /usr/local/lib/node_modules/@aws-amplify/cli/node_modules/aws-sdk/lib/state_machine.js:26:10,
at Request.<anonymous> (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/aws-sdk/lib/request.js:38:9),
at Request.<anonymous> (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/aws-sdk/lib/request.js:685:12),
at Request.callListeners (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/aws-sdk/lib/sequential_executor.js:116:18)]
(Sorry for the ilegible trace).
My amplify/backend/hosting/S3AndCloudFront/template.json is
"Statement": [
{
"Sid": "APIReadForGetBucketObjects",
"Effect": "Allow",
"Principal": {
"CanonicalUser": {
"Fn::GetAtt": [
"OriginAccessIdentity",
"S3CanonicalUserId"
]
}
},
"Action": "s3:*",
"Resource": {
"Fn::Join": [
"",
[
"arn:aws:s3:::",
{
"Ref": "S3Bucket"
},
"/*"
]
]
}
}
]
I tested uploading a file to the bucket directly with the aws s3 cli, and this was successful.
#2369 addressed this.
This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.
Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.