

# CloudFront Functions のビューワーレスポンスイベントに HTTP セキュリティヘッダーを追加する
<a name="example_cloudfront_functions_add_security_headers_section"></a>

次のコード例は、CloudFront Functions のビューワーレスポンスイベントに HTTP セキュリティヘッダーを追加する方法を示しています。

------
#### [ JavaScript ]

**CloudFront Functions の JavaScript ランタイム 2.0**  
 GitHub には、その他のリソースもあります。用例一覧と設定および実行方法については、[CloudFront Functions の例](https://github.com/aws-samples/amazon-cloudfront-functions/tree/main/add-security-headers)リポジトリをご覧ください。

```
async function handler(event) {
    var response = event.response;
    var headers = response.headers;

    // Set HTTP security headers
    // Since JavaScript doesn't allow for hyphens in variable names, we use the dict["key"] notation 
    headers['strict-transport-security'] = { value: 'max-age=63072000; includeSubdomains; preload'}; 
    headers['content-security-policy'] = { value: "default-src 'none'; img-src 'self'; script-src 'self'; style-src 'self'; object-src 'none'; frame-ancestors 'none'"}; 
    headers['x-content-type-options'] = { value: 'nosniff'}; 
    headers['x-frame-options'] = {value: 'DENY'}; 
    headers['x-xss-protection'] = {value: '1; mode=block'};
    headers['referrer-policy'] = {value: 'same-origin'};
    
    // Return the response to viewers 
    return response;
}
```

------

AWS SDK デベロッパーガイドとコード例の詳細なリストについては、[AWS SDK での CloudFront の使用](sdk-general-information-section.md) を参照してください。このトピックには、使用開始方法に関する情報と、以前の SDK バージョンの詳細も含まれています。