S3 - Access-Control-Allow-Origin Header
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Cross-Origin Resource Sharing (CORS) is a security feature implemented by web browsers that restricts web pages from making requests to a different domain than where the page originated. This can prevent potential security risks, such as Cross-Site Request Forgery (CSRF). However, sometimes it's necessary to allow cross-origin requests for legitimate reasons, particularly for web applications that integrate resources from multiple domains. Amazon S3 is a web service offered by AWS that allows users to store and retrieve data. As part of managing how resources are served, S3 allows users to configure CORS settings, with the Access-Control-Allow-Origin header playing a critical role in this setup.
Access-Control-Allow-Origin in CORS
The Access-Control-Allow-Origin HTTP header determines whether a response can be shared with requesting code from a given origin. An origin in this context is defined as a combination of a protocol, domain, and port. When a web page makes an HTTP request to a server, the server sends a response and may include this header to specify which origin(s) are permitted to access its resources via the browser. Let’s delve deeper into how S3 uses this header.
Configuring CORS in Amazon S3
To enable CORS in Amazon S3, you must specify a CORS configuration using the AWS Management Console, the AWS SDKs, or the AWS CLI. This configuration is an XML document where each rule specifies allowed origins and the operations (HTTP methods) that can be performed.
Example S3 CORS Configuration
Below is an example of a typical CORS configuration for an S3 bucket:
CORS Configuration Details
- AllowedOrigin: Specifies the origin a browser can access the bucket from. Use
*to allow all origins. - AllowedMethod: Lists the HTTP methods (GET, PUT, DELETE, etc.) that the origin can use to access the resource.
- AllowedHeader: Identifies the headers that the origin can include in the preflight request.
- MaxAgeSeconds: Determines how long the results of the preflight request can be cached.
- ExposeHeader: Lists the headers that are exposed to the client.
S3 and the Access-Control-Allow-Origin Header
When S3 responds to a cross-origin request, it includes the Access-Control-Allow-Origin header based on the CORS configuration. This header could specify a particular domain or * to denote any domain, subject to security considerations.
Technical Example
Suppose http://example.com wants to fetch a resource from an S3 bucket. With the configuration above, the following headers might be seen in the response:
Security Considerations
- Wildcard Origin: Although using
*is convenient, it represents a significant security risk if sensitive data is involved since it allows any website to interact with your resources. - Specific Origins: It's best practice to enumerate explicit origins that are trusted, minimizing potential security exposure.
Summary Table
| Aspect | Details |
| AllowedOrigin | Specifies which domain(s) can access resources. Use * cautiously. |
| AllowedMethod | Lists HTTP methods permitted from the origin (e.g., GET, POST). |
| AllowedHeader | Enumerates headers allowed in requests from the origin. |
| MaxAgeSeconds | Cache duration for preflight requests. |
| ExposeHeader | Headers available to the client making the request. |
| Security Best Practice | Prefer specific origins over the wildcard * to limit access. |
Conclusion
Configuring Access-Control-Allow-Origin in S3's CORS settings provides the flexibility needed for modern web applications to function across domains while maintaining security. By carefully crafting CORS policies, web developers can allow necessary cross-origin interactions without opening up vulnerabilities. Proper knowledge and handling of the Access-Control-Allow-Origin header ensure that sensitive resources are protected from unauthorized access, maintaining a robust security posture in web applications.
Related reading
- S3 - What Exactly Is A Prefix? And what Ratelimits apply?
- S3 Bucket action doesn't apply to any resources
- S3 Bucket Lambda Event Unable to validate the following destination configurations
- S3 Bucket Policy to make a specific sub folder public and everything else private?
- S3 bucket policy vs access control list
- S3, Signed-URLs and Caching
- S3 Connection timeout when using boto3
- S3 limit to objects in a bucket

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.