Tạo Subresource Integrity hash cho scripts và stylesheets
Subresource Integrity (SRI) cho phép browsers xác minh rằng resources từ CDN không bị thay đổi.
Nếu file bị modified (bởi attacker), browser sẽ từ chối load, bảo vệ users khỏi malicious code.
Công cụ tạo SRI (Subresource Integrity) hash cho JavaScript và CSS files từ CDN online miễn phí. Hỗ trợ SHA-256, SHA-384, SHA-512 algorithms. Tự động generate script tag và link tag với integrity attribute. Bảo vệ website khỏi CDN compromise và supply chain attacks. Fetch resource và tính hash trực tiếp trên browser. Copy tags với một click. Hữu ích cho web developers và security engineers.
Subresource Integrity (SRI) là security feature cho phép browsers xác minh rằng resources (JS, CSS) từ CDN không bị thay đổi. Khi bạn load script từ CDN bên thứ ba, có risk: CDN bị hack và file bị inject malicious code, Man-in-the-middle attack thay đổi file, CDN serve wrong version. SRI giải quyết bằng cách: Browser tính hash của file downloaded, so sánh với hash trong integrity attribute, nếu không khớp, browser từ chối execute. Đây là defense-in-depth quan trọng cho supply chain security. Nhiều security standards (CSP, PCI-DSS) recommend hoặc require SRI cho third-party resources.
SRI (Subresource Integrity) là W3C specification cho phép browsers verify integrity của resources fetched từ CDN. Browser tính cryptographic hash của downloaded file và so sánh với hash trong integrity attribute. Nếu không match, browser block resource. Điều này bảo vệ khỏi CDN compromise, MITM attacks, và accidental file corruption.
SHA-384 được recommend bởi W3C và là default của nhiều tools. SHA-256 cũng an toàn và hash ngắn hơn. SHA-512 secure nhất nhưng hash dài hơn. Trong thực tế, cả 3 đều đủ secure cho SRI. SHA-384 là balance tốt. Browsers support cả 3 algorithms.
Khi dùng SRI, browser cần access response body để tính hash. Với cross-origin requests (CDN), cần CORS. crossorigin='anonymous' tells browser to make CORS request without credentials. Nếu thiếu attribute này, SRI sẽ fail cho cross-origin resources. Hầu hết CDNs đã enable CORS.
Tool cần fetch file để tính hash. Nếu CDN không enable CORS, browser block request. Hầu hết major CDNs (cdnjs, unpkg, jsdelivr) đã enable CORS. Nếu gặp lỗi: Kiểm tra URL đúng chưa, thử CDN khác, hoặc dùng command line tool như shasum.
Minimal. Browser cần tính hash của downloaded file, nhưng modern browsers rất nhanh. Hash calculation là O(n) với file size. Với typical JS/CSS files (vài trăm KB), overhead không đáng kể. Security benefit outweighs tiny performance cost.
SRI fail khi: File content thay đổi (CDN update version, file bị tamper). Hash algorithm không match. integrity attribute syntax sai. CORS không được enable. Khi fail, browser log error trong console và không execute script / apply styles. Website có thể break nên cần test kỹ.
Technically không bắt buộc vì same-origin resources đã được trust. Tuy nhiên, SRI vẫn hữu ích để: Detect accidental file changes, Ensure correct version trong CI/CD, Defense-in-depth nếu server bị compromise. Nhiều teams dùng SRI cho tất cả resources.
Khi update library version, phải regenerate SRI hash vì file content thay đổi. Workflow: Update URL với new version, Generate new SRI hash, Update integrity attribute. Nhiều build tools (webpack-subresource-integrity, vite) tự động generate SRI. Hoặc dùng lockfile để pin exact versions.
Chúng tôi không chỉ thiết kế website, mà còn giúp doanh nghiệp xây dựng thương hiệu số mạnh mẽ. Cung cấp dịch vụ thiết kế website trọn gói từ thiết kế đến tối ưu SEO. Hãy liên hệ ngay với Tấn Phát Digital để cùng tạo nên những giải pháp công nghệ đột phá, hiệu quả và bền vững cho doanh nghiệp của bạn tại Hồ Chí Minh.