Guide to CORS Configuration

Guide to CORS Configuration

Learn how to configure Cross-Origin Resource Sharing (CORS) with Filebase's S3-compatible API. Enable secure cross-origin access to your buckets.

AuthorFilebase Team
CategoryGuides

What is CORS?

Cross-Origin Resource Sharing (CORS) is a security mechanism that allows a web page from one domain to request resources from another domain. By default, web browsers block these "cross-origin" requests to prevent malicious scripts from accessing sensitive data.

When you store files in a Filebase bucket and want to access them directly from a web application (like a React app or a static site), you need to configure CORS on your bucket to tell the browser: "It's okay to let this website access these files."

Understanding Request Types

Not all cross-origin requests are treated the same. Browsers distinguish between "Simple" requests and those that require a "Preflight" check.

Simple Requests

Basic GET or POST requests that don't require preflight checks. The browser automatically sets CORS headers.

  • Uses standard HTTP methods (GET, POST, HEAD)
  • Contains only standard headers
  • Content-Type limited to application/x-www-form-urlencoded, multipart/form-data, or text/plain

Preflight Requests

Complex requests that require an OPTIONS check before the actual request. This "preflight" asks the server for permission first.

  • Required for PUT, DELETE, or custom headers
  • Browser verifies response headers before sending the actual data
  • Server must respond to the OPTIONS method

CORS Configuration Elements

A CORS configuration is a JSON document that defines a set of rules. Here are the key elements you need to know:

ElementDescription
AllowedOriginsSpecify which domains can access your resources.
Example: ["http://www.example.com"]
AllowedMethodsHTTP methods permitted for cross-origin requests.
Example: ["GET", "PUT"]
AllowedHeadersCustom headers allowed in requests.
Example: ["*"]
ExposeHeadersHeaders that browsers are allowed to access in the response.
Example: ["x-amz-request-id"]
MaxAgeSecondsHow long browsers should cache preflight results.
Example: 3000

Implementation Guide

Follow this step-by-step guide to configure CORS for your Filebase buckets using the AWS CLI.

1. Basic Configuration

Create a file named corspolicy.json. This basic configuration allows cross-origin GET requests from all origins. This is useful for public read access.

corspolicy.json
{
  "CORSRules": [
    {
      "AllowedHeaders": [],
      "AllowedMethods": ["GET"],
      "AllowedOrigins": ["*"],
      "ExposeHeaders": []
    }
  ]
}

2. Advanced Configuration

For more control, you can specify allowed methods, headers, and cache duration. This example allows specific operations from example.com with custom headers.

corspolicy.json
{
  "CORSRules": [
    {
      "AllowedHeaders": ["*"],
      "AllowedMethods": ["PUT", "POST", "DELETE"],
      "AllowedOrigins": ["http://www.example.com"],
      "ExposeHeaders": [
        "x-amz-server-side-encryption",
        "x-amz-request-id",
        "x-amz-id-2"
      ],
      "MaxAgeSeconds": 3000
    }
  ]
}

3. Apply & Verify

Use the AWS CLI to apply the CORS configuration to your bucket. Make sure you have configured AWS CLI with your Filebase credentials first.

terminal
# Configure AWS CLI (if not already done)
aws configure
# Access Key ID: Your-Filebase-Access-Key
# Secret Access Key: Your-Filebase-Secret-Key
# Region: us-east-1
# Output Format: Optional

# Apply CORS configuration
aws --endpoint https://s3.filebase.com s3api put-bucket-cors \
    --bucket your-bucket-name \
    --cors-configuration file://corspolicy.json

# Verify configuration
aws --endpoint https://s3.filebase.com s3api get-bucket-cors \
    --bucket your-bucket-name

Configuration Builder

Generate a CORS configuration file based on your access requirements.

corspolicy.json
{
  "CORSRules": [
    {
      "AllowedHeaders": [
        "*"
      ],
      "AllowedMethods": [
        "GET"
      ],
      "AllowedOrigins": [
        "*"
      ],
      "ExposeHeaders": []
    }
  ]
}

A simple configuration that allows GET requests from any origin. This is suitable for basic testing and public read-only access to your bucket resources.

Best Practices Checklist

Before rolling this out to production, run through this quick security and performance checklist.

Security Considerations

  • Origin Specification: Use exact domain origins in production environments instead of wildcards (*).
  • Method Restriction: Only allow necessary HTTP methods (e.g., only GET if you don't need uploads) to minimize attack surface.
  • Header Control: Explicitly specify allowed headers rather than using wildcards.

Performance Optimization

  • Preflight Caching: Set appropriate MaxAgeSeconds (e.g., 3000) to reduce the number of preflight requests your browser needs to make.
  • Header Minimization: Only expose necessary headers to reduce response size.