2025年7月8日 星期二

[研究]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


(完)

相關

沒有留言:

張貼留言