Course contentsShow
← Web Development
Lesson 2939 of 2,98252. Frontend SecurityFree lesson

The crossorigin Attribute Requirement

Understanding why and how to use crossorigin='anonymous' with SRI for CORS-enabled resources.

The crossorigin Attribute Requirement

You'll learn why the crossorigin attribute is mandatory when using SRI with external resources from different origins.

Why CORS and SRI Go Together

When you add Subresource Integrity (SRI) hashes to external resources like CDN-hosted scripts or stylesheets, the browser needs to read the file's content to verify the hash. However, web security policies (CORS) normally prevent browsers from inspecting resources from different origins—even if your page loads them successfully.

Without the crossorigin attribute, the browser fetches the resource opaquely, meaning it can use it but cannot read its contents. Since SRI verification requires reading the entire file to compute its hash, the verification simply fails.

How It Works

Think of it like a sealed package: you can accept delivery and use the package, but you can't verify what's inside unless the sender gives you permission to open it. The crossorigin="anonymous" attribute tells the browser to request that permission from the CDN server.

<!-- ❌ This will fail SRI verification -->
<script src="https://cdn.example.com/lib.js"
        integrity="sha384-abc123..."></script>

<!-- ✅ This works correctly -->
<script src="https://cdn.example.com/lib.js"
        integrity="sha384-abc123..."
        crossorigin="anonymous"></script>

The anonymous value requests CORS access without sending credentials (cookies), which is appropriate for public CDN resources.

The Technical Requirement

The CDN server must respond with appropriate CORS headers (Access-Control-Allow-Origin) for this to work. Most major CDNs already support CORS for this exact reason.

Key Takeaway: Always include crossorigin="anonymous" when using SRI with external resources—without it, integrity verification silently fails and your security protection becomes ineffective.