顯示具有 Content-Security-Policy 標籤的文章。 顯示所有文章
顯示具有 Content-Security-Policy 標籤的文章。 顯示所有文章

2025年7月17日 星期四

[研究]Content-Security-Policy (CSP)弱點防護設定添加後會導致那些功能或設定無法運作?

[研究]Content-Security-Policy (CSP)弱點防護設定添加後會導致那些功能或設定無法運作?

2025-07-17

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C# + SQL Server 2019 + SQL Server Management Studio (SSMS) 20

********************************************************************************

ASP.NET WebForm 網站,請說明 Global.asax.cs 中添加下面後

Response.Headers.Add("Content-Security-Policy",
    "default-src 'self'; " +
    "script-src 'self'; " +
    "style-src 'self'; " +
    "font-src 'self'; " +
    "img-src 'self'; " +
    "frame-src 'self'; " +
    "object-src 'none'; " +
    "base-uri 'self'; " +
    "form-action 'self';"
);

會導致那些功能或設定無法運作

********************************************************************************

ChatGPT 說:

這是一個相對嚴格的 CSP 設定,以下是各設定的說明以及可能導致的功能失效或影響的場景:

📌 default-src 'self';

說明: 預設所有資源只允許來自本網站來源(同網域)。

影響:

  • 所有未明確指定來源類別的資源(如 media、connect)都會被限制只能來自 self。
  • 如使用外部 CDN 資源(圖片、API、影片)會被阻擋。

****************************************

📌 script-src 'self';

說明: 僅允許來自自己網站的 JavaScript 腳本。

影響:

  • ❌ 無法載入外部 JS,如 Google Analytics (www.googletagmanager.com)、Facebook SDK、CDN JavaScript(如 jQuery CDN)。
  • ❌ 不允許 inline script(如 <script>alert('Hi');</script>)或 eval()。
    • 若未加上 'unsafe-inline' 或 'unsafe-eval',會導致:
    • ClientScript.RegisterStartupScript()、ScriptManager.RegisterStartupScript() 插入的內嵌 JS 被阻擋。
    • 某些 AJAX Callback 或驗證 JS 無法執行。
  • ✅ 可正常執行來自本網站的 .js 檔案(只要是從相同網域載入的)。

****************************************

📌 style-src 'self';

說明: 僅允許來自本網站的 CSS 樣式表。

影響:

  • ❌ 禁止外部樣式表(如 https://fonts.googleapis.com)。
  • ❌ 不允許 <style> 標籤中內嵌的 CSS(除非加上 'unsafe-inline')。
    • 如 <div style="color:red;"> 可能失效。
    • 若動態產生樣式或使用 ASP.NET 內嵌樣式,樣式可能被瀏覽器拒絕執行。

****************************************

📌 font-src 'self';

說明: 僅允許來自本網站的字型檔。

影響:

❌ Google Fonts(fonts.gstatic.com)等外部字型載入會被阻擋。

✅ 自己網站的 .woff、.ttf 等可使用。

****************************************

📌 img-src 'self';

說明: 只允許自己網站的圖片資源。

影響:

❌ 外部圖片(如 <img src="https://example.com/logo.png">)無法顯示。

❌ 無法嵌入 base64 的圖片(若未加 'data:')。

****************************************

📌 frame-src 'self';

說明: 僅允許自己網域的 <iframe> 來源。

影響:

❌ 不能嵌入 YouTube、Google Maps、Facebook 等 iframe。

✅ 若自己網站內嵌自己網站的頁面,仍可用。

****************************************

📌 object-src 'none';

說明: 禁止 <object>、<embed>、<applet> 等用於 Flash、Silverlight、Java Applet 的元素。

影響:

✅ 增加安全性(防止老舊技術漏洞)。

❌ 若網站仍使用 Flash 等,會無法載入(大多數現代網站不受影響)。

📌 base-uri 'self';

說明: 僅允許 <base href="..."> 設定在同網域。

影響:

若你使用 <base href="https://cdn.example.com/">(來自外部)將會被阻擋。

****************************************

📌 form-action 'self';

說明: 表單的 action 屬性僅允許送出到同網域。

影響:

❌ 表單無法提交到外部網站(例如第三方金流、驗證服務、SSO 服務)。

✅ 增加防止 CSRF 的安全性。

****************************************

⚠️ 總結:會「無法運作」或「功能異常」的典型情況

功能影響說明
Google 字型被阻擋 (fonts.googleapis.com、fonts.gstatic.com)
外部 JavaScript (如 jQuery CDN)被阻擋 (code.jquery.com)
內嵌 JS無效,瀏覽器會擋下
內嵌 CSS無效,除非允許 'unsafe-inline'
第三方 iframe如 YouTube、Google Maps、Facebook 插件無法載入
表單送出到其他網站被阻擋,導致無法登入、支付等
圖片載入自其他網站無法顯示
base64 圖片 (data:image/png;base64...)被阻擋(需加上 img-src 'self' data:)

✅ 建議做法(視情況調整)

你可以依實際需求放寬部分限制,例如:

Response.Headers.Add("Content-Security-Policy",
    "default-src 'self'; " +
    "script-src 'self' 'unsafe-inline' https://www.google.com https://www.gstatic.com; " +
    "style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; " +
    "font-src 'self' https://fonts.gstatic.com; " +
    "img-src 'self' data:; " +
    "frame-src 'self' https://www.youtube.com; " +
    "object-src 'none'; " +
    "base-uri 'self'; " +
    "form-action 'self';"
);


********************************************************************************

設定了 unsafe-inline 或排除某些網址後,HCL AppScan 會否會報告有 CSP 相關弱點

ChatGPT 說:

是的,HCL AppScan 在掃描時若偵測到 Content-Security-Policy (CSP) 中設定了:

  • unsafe-inline
  • unsafe-eval
  • 或允許特定外部資源(如來自 CDN 的 JS、CSS)

通常會報告為「中風險」或「低風險」的 CSP 設定不安全弱點。

也就是你沒設定 CSP 防護,和放寬了 CSP 防護,HCL AppScan 都會報告弱點;

而你設定了嚴格 CSP,很多常用功能和設定可能就異常不能用。

(完)

相關

2025年7月8日 星期二

[研究]ASP.NET WebForm 網站 OWASP ZAP 2.16.1 發現【CSP: Failure to Define Directive with No Fallback】弱點,該如何解 ?

[研究]ASP.NET WebForm 網站 OWASP ZAP 2.16.1 發現【CSP: Failure to Define Directive with No Fallback】弱點,該如何解 ?

2025-07-08

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C# + SQL Server 2019 + SQL Server Management Studio (SSMS) 20

********************************************************************************

續這篇

[研究]ASP.NET WebForm 網站用 OWASP ZAP 2.16.1 掃描發現【Content Security Policy (CSP) Header Not Set】弱點,該如何解 ?

https://shaurong.blogspot.com/2025/07/aspnet-webform-owasp-zap-2161-content.html

ASP.NET WebForm 網站 OWASP ZAP發現【CSP: Failure to Define Directive with No Fallback】弱點

證據

default-src 'self'; script-src 'self' https://www.google.com https://www.gstatic.com; style-src 'self' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com; img-src 'self' data:;frame-src 'self' https://www.google.com;

說明

The Content Security Policy fails to define one of the directives that has no fallback. Missing/excluding them is the same as allowing anything.

解決方案

Ensure that your web server, application server, load balancer, etc. is properly configured to set the Content-Security-Policy header.

ChatGPT 說:

❗ 問題在於:

以下這些高風險指令未設定,建議加上(哪怕是嚴格限制):

指令 說明

object-src 控制 <object>、Flash、Java Applet 等老舊技術

base-uri 控制 <base> 標籤,避免 URL 重導到惡意站

form-action 控制 <form action> 目標,避免送出資料到惡意站點

✅ 建議修改後的 CSP:

在 ASP.NET WebForm 的 Global.asax.cs 裡:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    Response.Headers.Add("Content-Security-Policy",
        "default-src 'self'; " +
        "script-src 'self' https://www.google.com https://www.gstatic.com; " +
        "style-src 'self' https://fonts.googleapis.com; " +
        "font-src 'self' https://fonts.gstatic.com; " +
        "img-src 'self' data:; " +
        "frame-src 'self' https://www.google.com; " +
        "object-src 'none'; " +
        "base-uri 'self'; " +
        "form-action 'self';"
    );
}

後來問題依舊,幾番和 ChatGPT 討論和測試,依然有相同問題,暫時擱置。

(完)

相關

[研究]ASP.NET WebForm 網站用 OWASP ZAP 2.16.1 掃描發現【Content Security Policy (CSP) Header Not Set】弱點,該如何解 ?

[研究]ASP.NET WebForm 網站用 OWASP ZAP 2.16.1 掃描發現【Content Security Policy (CSP) Header Not Set】弱點,該如何解 ?

2025-07-07

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C# + SQL Server 2019 + SQL Server Management Studio (SSMS) 20

********************************************************************************

ASP.NET WebForm 網站用 OWASP ZIP 2.16.1 掃描發現【Content Security Policy (CSP) Header Not Set】弱點,該如何解 ?

證據: (沒寫)

來源: 被動 (10038 - Content Security Policy (CSP) Header Not Set)

說明:

Content Security Policy (CSP) is an added layer of security that helps to detect and mitigate certain types of attacks, including Cross Site Scripting (XSS) and data injection attacks. These attacks are used for everything from data theft to site defacement or distribution of malware. CSP provides a set of standard HTTP headers that allow website owners to declare approved sources of content that browsers should be allowed to load on that page — covered types are JavaScript, CSS, HTML frames, fonts, images and embeddable objects such as Java applets, ActiveX, audio and video files.

解決方案:

Ensure that your web server, application server, load balancer, etc. is configured to set the Content-Security-Policy header.

參考資料

https://developer.mozilla.org/en-US/docs/Web/Security/CSP/Introducing_Content_Security_Policy

https://cheatsheetseries.owasp.org/cheatsheets/Content_Security_Policy_Cheat_Sheet.html

https://www.w3.org/TR/CSP/

********************************************************************************

ChatGPT 說:

針對 ASP.NET WebForm 網站,OWASP ZAP 2.16.1 掃描出【Content Security Policy (CSP) Header Not Set】弱點,解決方法如下:

【弱點說明】

CSP(Content Security Policy,內容安全政策)是透過 HTTP Response Header 控制瀏覽器僅允許來自指定來源的資源(JS、CSS、圖片、字型、iframe、影片、物件等),可有效降低:

  • XSS(跨站腳本)
  • 資料注入
  • 惡意外部資源載入

【ASP.NET WebForm 解決方式】

通常在 Global.asax.cs 的 Application_BeginRequest 事件中加入 CSP Header:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    // 基本範例,限制所有資源來源
    Response.Headers.Add("Content-Security-Policy", "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self'; font-src 'self';");

    // 依實際需求開放特定來源,例如允許 Google 字型與 reCAPTCHA
    // Response.Headers.Add("Content-Security-Policy", "default-src 'self'; script-src 'self' https://www.google.com https://www.gstatic.com; style-src 'self' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com; img-src 'self' data:;");
}

【常見注意事項】

分階段導入建議:

初期可用 Content-Security-Policy-Report-Only 測試,不會強制阻擋資源,僅回報違規內容,利於觀察:

Response.Headers.Add("Content-Security-Policy-Report-Only", "default-src 'self'; report-uri /csp-report-endpoint");

避免破壞現有功能:

  • 若網站使用 CDN、Google 字型、外部 API、第三方驗證(如 reCAPTCHA),需在 script-src、style-src 等開放指定來源。

慎用 unsafe-inline、unsafe-eval:

  • unsafe-inline:允許內嵌 <script> 或 style,易有 XSS 風險。
  • 強烈建議重構前端程式碼,避免使用內嵌腳本或樣式。

【進階示例:CSP 常見設定】

Response.Headers.Add("Content-Security-Policy",
    "default-src 'self'; " +
    "script-src 'self' https://www.google.com https://www.gstatic.com; " +
    "style-src 'self' https://fonts.googleapis.com; " +
    "font-src 'self' https://fonts.gstatic.com; " +
    "img-src 'self' data:; " +
    "frame-src 'self' https://www.google.com;");

【其他參考資源】

MDN 官方文件:CSP

https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CSP

OWASP CSP Cheat Sheet

https://cheatsheetseries.owasp.org/cheatsheets/Content_Security_Policy_Cheat_Sheet.html

【總結】

  • ✅ 建議從 Content-Security-Policy-Report-Only 開始測試,逐步收斂政策。
  • ✅ 明確設定允許的資源來源,降低 XSS 等攻擊面。
  • ✅ 設定位置建議放在 Global.asax.cs,或在 IIS/Web.config 統一透過 httpProtocol 加入 Header(如 IIS 無特別設定可用程式碼方式處理)。

********************************************************************************

ChatGPT 說:

根據你的網站環境:

  • ✅ 有用 Google reCAPTCHA
  • ❌ 無外部 CDN
  • ❌ 無第三方 iframe

以下是安全且符合你需求的 CSP 設定,可加入 Global.asax.cs:

【完整範例:Global.asax.cs】

protected void Application_BeginRequest(object sender, EventArgs e)
{
    // 正式上線用,強制生效
    Response.Headers.Add("Content-Security-Policy",
        "default-src 'self'; " +
        "script-src 'self' https://www.google.com https://www.gstatic.com; " +
        "style-src 'self'; " +
        "img-src 'self' data:; " +
        "font-src 'self'; " +
        "frame-src https://www.google.com; " +
        "connect-src 'self';"
    );
}


【說明】

  • 政策類型 說明
  • default-src 'self' 預設只允許同源內容 (本網站)
  • script-src 允許本網站及 Google reCAPTCHA 所需的 JS 檔
  • style-src 'self' 僅允許網站本身 CSS
  • img-src 'self' data: 圖片來源限制本網站,可額外允許 Base64 圖片
  • font-src 'self' 字型來源限制本網站
  • frame-src 僅允許載入 Google reCAPTCHA 生成的 iframe
  • connect-src 'self' 僅允許網站本身的 AJAX、API 請求

【開發階段建議:觀察模式】

若怕 CSP 設太嚴格破壞功能,可先用 Report-Only 模式:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    Response.Headers.Add("Content-Security-Policy-Report-Only",
        "default-src 'self'; " +
        "script-src 'self' https://www.google.com https://www.gstatic.com; " +
        "style-src 'self'; " +
        "img-src 'self' data:; " +
        "font-src 'self'; " +
        "frame-src https://www.google.com; " +
        "connect-src 'self';"
    );
}

這樣瀏覽器只回報違規,不會強制擋掉,適合邊測試邊調整。

【其他提醒】

  • 若 reCAPTCHA 是 v2 Invisible 或 v3 版本,來源 https://www.google.com 和 https://www.gstatic.com 足夠。
  • 若網站未來改用 Google 字型或其他第三方服務,需另外調整 script-src、style-src 或其他規則。
  • 若想收集違規報告,可加上 report-uri,不過新版 CSP 改用 report-to,可再討論設置。

【結論】

  • ✅ 這份 CSP 適合你目前環境
  • ✅ 可強化 XSS、防止不明外部內容載入
  • ✅ 可先用 Report-Only 模式安全測試

********************************************************************************

還是有問題,續這篇

[研究]ASP.NET WebForm 網站 OWASP ZAP 2.16.1 發現【CSP: Failure to Define Directive with No Fallback】弱點,該如何解 ?
https://shaurong.blogspot.com/2025/07/aspnet-webform-owasp-zap-2161-csp.html


(完)

相關