資安議題 — Http Security Header

LSZ
程式愛好者
Published in
7 min readDec 1, 2020

相關網站

安全性的header相關資訊都可在developer.mozilla.org找到。

網站header檢測:https://securityheaders.com/

完整網站安全檢測:https://www.ssllabs.com/ssltest/

前言

當使用者通過瀏覽器發送request到伺服器上,伺服器會回應response給瀏覽器,此時就會帶上一些header,其中有些header可以保證網站安全。

安全性Header

X-Content-Type-Options: nosniff

阻止瀏覽器探知檔案的 mime type ( disable Content sniffing ),一般情況下瀏覽器會去探知資源的 Content-Type ,以判別資源類型,例如:image/png、text/css,而有些資源的 Content-Type 有可能是錯誤或缺少的,此時就會進行 Content sniffing猜測解析內容,將 X-Content-Type-Options 設成 nosniff,可阻止這種行為。

沒有設成 nosniff 的風險為攻擊者可能使用錯誤的 header 隱藏攻擊的 script ,例如 <script src=”https://example.com/attacker.txt" ></script>,attacker.txt 實際是 js 檔,表面的 header 是 text/plain ,實際上瀏覽器會解析 scrip t的content type ,並且執行 script。

X-Frame-Options: DENY

代表不讓網頁載入 frame ( iframe 跟 object )。

沒有設成 DENY 的風險為可能被惡意嵌入 iframe ,若自己的網站想使用 iframe ,可以設成 SAMEORIGIN (符合同源政策),或是 ALLOW-FROM,例如:ALLOW-FROM: https://www.google.com/ https://github.com,可以嵌入 google 網域跟 github 網域下的網頁。

ALLOW-FROM已棄用。可能根據瀏覽器的不同而失效。

X-XSS-Protection: 1; mode=block

當瀏覽器發現跨站腳本攻擊時,停止加載網頁。

  • 0 為不啟用 XSS 過濾。
  • 1 為啟用 XSS 過濾,遇到 XSS ,僅刪除不安全的部分,不會block。
  • 1;mode=block 是遇到 XSS ,停止加載頁面。

Referrer-Policy: no-referrer-when-downgrade

referrer是訪問來源網址,會將使用者從哪個網站來的資訊帶到 request 的header。例如: referer: https://www.google.com/ 代表從google搜尋來的。

Referrer-Policy 值:

Referrer 的值有兩種形式,一種是完整url,一種是源(origin,只帶domain)。

  • no-referrer: 不帶 header 。
  • no-referrer-when-downgrade : https 到 https 的網站可以, https 到 http 的網站則不會帶header。
  • origin:不帶完整 url ,僅只帶 origin 。
  • origin-when-cross-origin:同源帶完整 url ,不同源帶 origin 。
  • same-origin:同源才帶 header 。
  • strict-origin:同源且 https 到 https 才帶 header , http 就不會帶。
  • strict-origin-when-cross-origin:同源帶完整 url,不同源帶 origin ,但是必須是 https 到 https 。
  • unsafe-url:都帶。

帶Referrer-Policy header,可以幫助google analysis統計,為了 seo 還是需要適當的設定。

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

簡稱 HSTS,告知瀏覽器強制啟用 https , max-age 代表強制維持 https 多少時間,一般為一年31536000秒,includeSubDomains包含子網域,若max-age=0,代表disable。

Content-Security-Policy: default-src ‘self’

為了防止 XSS , CSP 可以設定來源白名單,只讓信任的來源執行腳本,default-src ‘self’ 代表讓網站的所有資源都來自於自己。

各項參數、值: https://content-security-policy.com/

在 laravel 加入安全性 header

php artisan make:middleware AddSecurityHeader

內容如下:

<?phpnamespace App\Http\Middleware;use Closure;class AddSecurityHeader{    public function handle($request, Closure $next)    {        $response = $next($request);        $response->headers->set('Referrer-Policy', 'no-referrer-when-downgrade');        $response->headers->set('X-Content-Type-Options', 'nosniff');        $response->headers->set('X-XSS-Protection', '1; mode=block');        $response->headers->set('X-Frame-Options', 'DENY');        $response->headers->set('Strict-Transport-Security', 'max-age=31536000; includeSubDomains');        $response->headers->set('Referrer-Policy', 'no-referrer-when-downgrade');        return $response;    }}

之後在 Kernel.php 的 middlewareGroups 裡面的 web 註冊:

在 htaccess 加入安全性 header

<IfModule mod_headers.c>
Header set X-Content-Type-Options nosniff
Header set X-XSS-Protection "1; mode=block"
Header set Referrer-Policy no-referrer-when-downgrade
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
</IfModule>

--

--

程式愛好者
程式愛好者

Published in 程式愛好者

「程式愛好者」是為了程式愛好者而創建的文章平台,只要是程式愛好者我們都歡迎加入我們,為中文的程式社群創造更多更優質的資源!

No responses yet