0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

CloudFront の Response Headers 要件とは

0
Posted at

CloudFront の Response Headers 要件とは、

CloudFrontがブラウザへ返すHTTPレスポンスヘッダに、
セキュリティ設定を付与すること

です。

特に実務で頻出なのが:

  • HSTS
  • nosniff
  • CSP

です。

これらは:

「ブラウザ側セキュリティ制御」

になります。


全体像

User Browser
    ↑
CloudFront
    ↑
S3 / ALB / API

CloudFrontがレスポンスヘッダを追加。


1. HSTS

正式名:

HTTP Strict Transport Security


何を防ぐ?

HTTP通信へのダウングレード

を防ぎます。


悪意ある攻撃:

https://example.com
 ↓
http://example.com に誘導

HSTSあり

ブラウザが:

「このサイトはHTTPSしか許可しない」

と記憶。


ヘッダ例

Strict-Transport-Security:
max-age=31536000;
includeSubDomains;
preload

意味

項目 意味
max-age 有効期間
includeSubDomains サブドメイン含む
preload ブラウザ内蔵HSTS対象

実務ポイント

かなり重要。

特に:

  • ログインサイト
  • 会員サイト
  • 管理画面

ではほぼ必須級。


2. nosniff

正式名:

X-Content-Type-Options


何を防ぐ?

ブラウザの勝手なMIME推測

を禁止。


問題例

本来:

text/plain

なのに、

ブラウザが:

JavaScriptかも

と誤解釈。

XSS原因になります。


ヘッダ

X-Content-Type-Options: nosniff

効果

Content-Typeを厳密適用

します。


実務

これは:

ほぼ常時ON

レベル。


3. CSP

正式名:

Content Security Policy


超重要

かなり強力。


何を防ぐ?

XSS(クロスサイトスクリプティング)

対策。


考え方

ブラウザへ:

「どこからScript読み込み許可するか」

を宣言。


Content-Security-Policy:
default-src 'self';
script-src 'self' https://apis.example.com;
img-src 'self' data:;

意味

設定 意味
default-src 基本許可元
script-src JS許可元
img-src 画像許可元

例えば防げる

悪意ある:

<script src="evil.com/xss.js">

を拒否。


CSPの難しさ

実務ではかなり難しい。

理由:

  • 外部CDN
  • Google Analytics
  • Stripe
  • YouTube
  • React inline script

などとの調整が必要。


CloudFrontで設定する理由

CloudFrontで統一管理できる。


以前

各アプリでヘッダ設定

していた。


CloudFrontなら

Edgeで一括付与

可能。


AWSでの設定方法

CloudFront の:

Response Headers Policy

を使用。


AWS管理ポリシー例

  • SecurityHeadersPolicy
  • CORS-with-preflight
  • SimpleCORS

など。


CDK例

new cloudfront.Distribution(this, 'Dist', {
  defaultBehavior: {
    responseHeadersPolicy:
      cloudfront.ResponseHeadersPolicy.SECURITY_HEADERS,
  },
});

SECURITY_HEADERS で付くもの

代表例:

  • HSTS
  • X-Frame-Options
  • nosniff
  • Referrer-Policy

など。


CSPは別途設定多い

CSPはサイト依存が強いため、

個別カスタム設定が多い。


実務で重要な理解

これらは:

「サーバ保護」

ではなく、

「ブラウザ制御」

です。

つまり:

ブラウザに安全な動きを強制する

仕組み。


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?