Content-Security-Policy: worker-src directive
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since May 2022.
Chỉ thị worker-src trong HTTP Content-Security-Policy (CSP) chỉ định các nguồn hợp lệ cho các script Worker, SharedWorker, hoặc ServiceWorker.
| Phiên bản CSP | 3 |
|---|---|
| Loại chỉ thị | Fetch directive |
| Dự phòng |
Nếu chỉ thị này vắng mặt thì trình duyệt sẽ tìm kiếm chỉ thị
|
Cú pháp
Content-Security-Policy: worker-src 'none';
Content-Security-Policy: worker-src <source-expression-list>;
Chỉ thị này có thể nhận một trong các giá trị sau:
'none'-
Không có tài nguyên nào thuộc loại này được tải. Dấu nháy đơn là bắt buộc.
<source-expression-list>-
Danh sách các giá trị biểu thức nguồn phân cách bằng khoảng trắng. Tài nguyên thuộc loại này có thể được tải nếu chúng khớp với bất kỳ biểu thức nguồn nào. Đối với chỉ thị này, các giá trị biểu thức nguồn sau đây có thể áp dụng:
Ví dụ
>Trường hợp vi phạm
Với tiêu đề CSP này:
Content-Security-Policy: worker-src https://example.com/
Worker, SharedWorker, ServiceWorker bị chặn và sẽ không tải:
<script>
let blockedWorker = new Worker("data:text/javascript,…");
blockedWorker = new SharedWorker("https://not-example.com/");
navigator.serviceWorker.register("https://not-example.com/sw.js");
</script>
Thông số kỹ thuật
| Specification |
|---|
| Content Security Policy Level 3> # directive-worker-src> |