CSP - 内容安全策略 - 注意事项
2025-03-03
4 min read
基础事项
WASM 需要使用 wasm-unsafe-eva
(属于 script-src
的子属性)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSP with SHA-256 Example</title>
<!-- Content Security Policy (CSP) -->
<meta http-equiv="Content-Security-Policy"
content="script-src 'sha256-2XA6OeWgx7rumjOswMWkHzvY7xYWT9JsRykQhkmJXi0';">
</head>
<body>
<h1>CSP with SHA-256 Example</h1>
<p>Check the console for output.</p>
<!-- Inline Script with SHA-256 Hash -->
<script>const inline = 1;</script>
<!-- hash: 'sha256-2XA6OeWgx7rumjOswMWkHzvY7xYWT9JsRykQhkmJXi0=' -->
<!-- 注意事项 -->
<!-- When generating the hash, don't include the <script> tags and note that capitalization and whitespace matter, including leading or trailing whitespace.
引用自 https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src#examples -->
<!-- 注释也会影响生成的 hash -->
</body>
</html>
Nginx
对默认配置添加 CSP 头以及修改根目录
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 8080;
server_name localhost;
# 添加 CSP 头部
add_header Content-Security-Policy "
default-src 'self'
https://www.google.com;
script-src 'self'
'wasm-unsafe-eval' 'unsafe-eval' 'sha256-??????????????' https://www.google-analytics.com blob:;
style-src 'self'
'unsafe-hashes' 'sha256-????????????????????=' https://fonts.googleapis.com;
connect-src 'self'
https://www.google-analytics.com; object-src 'self'; base-uri 'self';img-src 'self' https://www.google-analytics.com data:;media-src 'self' data:;
";
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# root html;
# 根目录设置为 csp 文件夹
root /Users/lov3/Downloads/csp/csp;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
include servers/*;
}