セキュリティヘッダーを設定する
一般的なセキュリティヘッダーを設定する (X-XSS-Protection, X-Frame-Options, X-Content-Type-Options, Permissions-Policy, Referrer-Policy, Strict-Transport-Security, Content-Security-Policy)。
export default { async fetch(request) { const DEFAULT_SECURITY_HEADERS = { /* Content-Security-Policy ヘッダーでアプリケーションを保護します。 これらのヘッダーを有効にすると、信頼できるドメインとそのすべてのサブドメインからのコンテンツが許可されます。 @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy "Content-Security-Policy": "default-src 'self' example.com *.example.com", */ /* Strict-Transport-Security ヘッダーも設定できます。 これらは自動的には設定されません。なぜなら、あなたのウェブサイトが Chrome の HSTS プリロードリストに追加される可能性があるからです。 適用したい場合のコードは以下の通りです: "Strict-Transport-Security" : "max-age=63072000; includeSubDomains; preload", */ /* Permissions-Policy ヘッダーは、FLoC からのオプトアウトなど、ブラウザ機能の使用を許可または拒否する能力を提供します - 以下のように使用できます: "Permissions-Policy": "interest-cohort=()", */ /* X-XSS-Protection ヘッダーは、XSS 攻撃が検出された場合にページの読み込みを防ぎます。 @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection */ "X-XSS-Protection": "0", /* X-Frame-Options ヘッダーは、クリックジャッキング攻撃を防ぎます。 @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options */ "X-Frame-Options": "DENY", /* X-Content-Type-Options ヘッダーは、MIME スニッフィングを防ぎます。 @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Content-Type-Options */ "X-Content-Type-Options": "nosniff", "Referrer-Policy": "strict-origin-when-cross-origin", "Cross-Origin-Embedder-Policy": 'require-corp; report-to="default";', "Cross-Origin-Opener-Policy": 'same-site; report-to="default";', "Cross-Origin-Resource-Policy": "same-site", }; const BLOCKED_HEADERS = [ "Public-Key-Pins", "X-Powered-By", "X-AspNet-Version", ];
let response = await fetch(request); let newHeaders = new Headers(response.headers);
const tlsVersion = request.cf.tlsVersion; console.log(tlsVersion); // これは HTML レスポンスのヘッダーを設定します: if ( newHeaders.has("Content-Type") && !newHeaders.get("Content-Type").includes("text/html") ) { return new Response(response.body, { status: response.status, statusText: response.statusText, headers: newHeaders, }); }
Object.keys(DEFAULT_SECURITY_HEADERS).map((name) => { newHeaders.set(name, DEFAULT_SECURITY_HEADERS[name]); });
BLOCKED_HEADERS.forEach((name) => { newHeaders.delete(name); });
if (tlsVersion !== "TLSv1.2" && tlsVersion !== "TLSv1.3") { return new Response("TLS バージョン 1.2 以上を使用する必要があります。", { status: 400, }); } else { return new Response(response.body, { status: response.status, statusText: response.statusText, headers: newHeaders, }); } },};export default { async fetch(request): Promise<Response> { const DEFAULT_SECURITY_HEADERS = { /* Content-Security-Policy ヘッダーでアプリケーションを保護します。 これらのヘッダーを有効にすると、信頼できるドメインとそのすべてのサブドメインからのコンテンツが許可されます。 @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy "Content-Security-Policy": "default-src 'self' example.com *.example.com", */ /* Strict-Transport-Security ヘッダーも設定できます。 これらは自動的には設定されません。なぜなら、あなたのウェブサイトが Chrome の HSTS プリロードリストに追加される可能性があるからです。 適用したい場合のコードは以下の通りです: "Strict-Transport-Security" : "max-age=63072000; includeSubDomains; preload", */ /* Permissions-Policy ヘッダーは、FLoC からのオプトアウトなど、ブラウザ機能の使用を許可または拒否する能力を提供します - 以下のように使用できます: "Permissions-Policy": "interest-cohort=()", */ /* X-XSS-Protection ヘッダーは、XSS 攻撃が検出された場合にページの読み込みを防ぎます。 @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection */ "X-XSS-Protection": "0", /* X-Frame-Options ヘッダーは、クリックジャッキング攻撃を防ぎます。 @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options */ "X-Frame-Options": "DENY", /* X-Content-Type-Options ヘッダーは、MIME スニッフィングを防ぎます。 @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Content-Type-Options */ "X-Content-Type-Options": "nosniff", "Referrer-Policy": "strict-origin-when-cross-origin", "Cross-Origin-Embedder-Policy": 'require-corp; report-to="default";', "Cross-Origin-Opener-Policy": 'same-site; report-to="default";', "Cross-Origin-Resource-Policy": "same-site", }; const BLOCKED_HEADERS = [ "Public-Key-Pins", "X-Powered-By", "X-AspNet-Version", ];
let response = await fetch(request); let newHeaders = new Headers(response.headers);
const tlsVersion = request.cf.tlsVersion; console.log(tlsVersion); // これは HTML レスポンスのヘッダーを設定します: if ( newHeaders.has("Content-Type") && !newHeaders.get("Content-Type").includes("text/html") ) { return new Response(response.body, { status: response.status, statusText: response.statusText, headers: newHeaders, }); }
Object.keys(DEFAULT_SECURITY_HEADERS).map((name) => { newHeaders.set(name, DEFAULT_SECURITY_HEADERS[name]); });
BLOCKED_HEADERS.forEach((name) => { newHeaders.delete(name); });
if (tlsVersion !== "TLSv1.2" && tlsVersion !== "TLSv1.3") { return new Response("TLS バージョン 1.2 以上を使用する必要があります。", { status: 400, }); } else { return new Response(response.body, { status: response.status, statusText: response.statusText, headers: newHeaders, }); } },} satisfies ExportedHandler;from js import Response, fetch, Headers
async def on_fetch(request): default_security_headers = { # Content-Security-Policy ヘッダーでアプリケーションを保護します。 #これらのヘッダーを有効にすると、信頼できるドメインとそのすべてのサブドメインからのコンテンツが許可されます。 #@see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy "Content-Security-Policy": "default-src 'self' example.com *.example.com", #Strict-Transport-Security ヘッダーも設定できます。 #これらは自動的には設定されません。なぜなら、あなたのウェブサイトが Chrome の HSTS プリロードリストに追加される可能性があるからです。 #適用したい場合のコードは以下の通りです: "Strict-Transport-Security" : "max-age=63072000; includeSubDomains; preload", #Permissions-Policy ヘッダーは、FLoC からのオプトアウトなど、ブラウザ機能の使用を許可または拒否する能力を提供します - 以下のように使用できます: "Permissions-Policy": "interest-cohort=()", #X-XSS-Protection ヘッダーは、XSS 攻撃が検出された場合にページの読み込みを防ぎます。 #@see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection "X-XSS-Protection": "0", #X-Frame-Options ヘッダーは、クリックジャッキング攻撃を防ぎます。 #@see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options "X-Frame-Options": "DENY", #X-Content-Type-Options ヘッダーは、MIME スニッフィングを防ぎます。 #@see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Content-Type-Options "X-Content-Type-Options": "nosniff", "Referrer-Policy": "strict-origin-when-cross-origin", "Cross-Origin-Embedder-Policy": 'require-corp; report-to="default";', "Cross-Origin-Opener-Policy": 'same-site; report-to="default";', "Cross-Origin-Resource-Policy": "same-site", } blocked_headers = ["Public-Key-Pins", "X-Powered-By" ,"X-AspNet-Version"]
res = await fetch(request) new_headers = Headers.new(res.headers)
# これは HTML レスポンスのヘッダーを設定します if "text/html" in new_headers["Content-Type"]: return Response.new(res.body, status=res.status, statusText=res.statusText, headers=new_headers)
for name in default_security_headers: new_headers.set(name, default_security_headers[name])
for name in blocked_headers: new_headers.delete(name)
tls = request.cf.tlsVersion
if not tls in ("TLSv1.2", "TLSv1.3"): return Response.new("TLS バージョン 1.2 以上を使用する必要があります。", status=400) return Response.new(res.body, status=res.status, statusText=res.statusText, headers=new_headers)