S3 CORS permissions need review

Configure S3 CORS to allow only the origins and methods required by the web application.

Description

For an app that uses an S3 bucket from another origin, limit allowed_methods to the methods the app needs. S3 checks read and write permissions separately for each request.

Potential impact

An unnecessary write method can allow a web app to upload or delete objects if it also has the permissions or a signed request for that operation.

Remediation

In allowed_origins, enter the app’s HTTPS scheme, host and any nondefault port, without a path. Add the request headers the app needs. Test an allowed browser request and a preflight for an excluded method. Choose broader access only for content intended for other sites.

Examples

The app in the examples needs only GET and POST on mys3bucket. The module shown as community.aws.aws_s3_cors was renamed community.aws.s3_cors in collection 5.0.0.

Before

yaml
- name: Configure the S3 CORS rule
  community.aws.aws_s3_cors:
    name: mys3bucket
    state: present
    rules:
      - allowed_origins:
          - https://www.example.com
        allowed_methods:
          - GET
          - POST
          - PUT
          - DELETE
          - HEAD

The first rule also allows PUT, DELETE and HEAD from the same origin.

After

yaml
- name: Configure the S3 CORS rule
  community.aws.aws_s3_cors:
    name: mys3bucket
    state: present
    rules:
      - allowed_origins:
          - https://www.example.com
        allowed_methods:
          - GET
          - POST

The second keeps GET and POST. If the app uses POST for uploads, enforce S3 write permissions and validate the upload.

References