顯示具有 CSP 標籤的文章。 顯示所有文章
顯示具有 CSP 標籤的文章。 顯示所有文章

2025年7月21日 星期一

[研究]ASP.NET WebForm, CSP 會導致下面那些 NuGet 套件無法運作 ?

[研究]ASP.NET WebForm, CSP 會導致下面那些 NuGet 套件無法運作 ?

2025-07-21

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

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

ASP.NET WebForm, CSP 會導致下面那些 NuGet 套件無法運作 ?

  • AngleSharp 0.17.1
  • AngleSharp.Css 0.17.0
  • Antlr 3.5.0.2
  • Aspose.Cells 25.6.0
  • Autofac 8.3.0
  • Autofac.WebApi2 6.1.1
  • Bootstrap.Datepicker 1.8.0.1
  • BouncyCastle.Cryptography 2.6.1
  • ByteSize 2.1.2
  • Chart.js 3.7.1
  • ClosedXML 0.105.0
  • ClosedXML.Parser 2.0.0
  • Common.Logging 3.4.1
  • Common.Logging.Core 3.4.1
  • DocumentFormat.OpenXml 3.3.0
  • DocumentFormat.OpenXml.Framework 3.3.0
  • EFUtilities 1.0.2
  • EntityFramework 6.5.1
  • Enums.NET 5.0.0
  • ExcelDataReader 3.7.0
  • ExcelDataReader.DataSet 3.7.0
  • ExcelNumberFormat 1.1.0
  • ExtendedNumerics.BigDecimal 3000.0.4.132
  • FastMember 1.5.0
  • FastMember.Signed 1.5.0
  • Font.Awesome 5.15.4
  • FontAwesome 4.7.0
  • FreeDataExports 1.1.12
  • FreeSpire.PDF 8.6.0
  • HtmlSanitizer 9.0.876
  • HtmlSanitizer 9.0.886
  • Irony 1.5.3
  • Irony.NetCore 1.1.11
  • JetBrains.Annotations 2024.3.0
  • LinqKit 1.3.8
  • LinqKit.Core 1.2.8
  • LogicExtensions 0.0.3
  • MailKit 4.13.0
  • MathNet.Numerics.Signed 5.0.0
  • Microsoft.AspNet.FriendlyUrls 1.0.2
  • Microsoft.AspNet.FriendlyUrls.Core 1.0.2
  • Microsoft.AspNet.FriendlyUrls.Core.zh-Hant 1.0.2
  • Microsoft.AspNet.Identity.Core 2.2.4
  • Microsoft.AspNet.Identity.Core.zh-Hant 2.2.4
  • Microsoft.AspNet.Identity.EntityFramework 2.2.4
  • Microsoft.AspNet.Identity.EntityFramework.zh-Hant 2.2.4
  • Microsoft.AspNet.Identity.Owin 2.2.4
  • Microsoft.AspNet.Identity.Owin.zh-Hant 2.2.4
  • Microsoft.AspNet.Providers.Core 2.0.0
  • Microsoft.AspNet.Providers.Core.zh-Hant 2.0.0
  • Microsoft.AspNet.ScriptManager.MSAjax 5.0.0
  • Microsoft.AspNet.ScriptManager.WebForms 5.0.0
  • Microsoft.AspNet.Web.Optimization 1.1.3
  • Microsoft.AspNet.Web.Optimization.WebForms 1.1.3
  • Microsoft.AspNet.Web.Optimization.zh-Hans 1.1.3
  • Microsoft.AspNet.Web.Optimization.zh-Hant 1.1.3
  • Microsoft.AspNet.WebApi 5.3.0
  • Microsoft.AspNet.WebApi.Client 6.0.0
  • Microsoft.AspNet.WebApi.Client.zh-Hant 6.0.0
  • Microsoft.AspNet.WebApi.Core 5.3.0
  • Microsoft.AspNet.WebApi.Core.zh-Hant 5.3.0
  • Microsoft.AspNet.WebApi.WebHost 5.3.0
  • Microsoft.AspNet.WebApi.WebHost.zh-Hant 5.3.0
  • Microsoft.Bcl.AsyncInterfaces 9.0.6
  • Microsoft.Bcl.AsyncInterfaces 9.0.7
  • Microsoft.Bcl.Build 1.0.21
  • Microsoft.Bcl.Cryptography 9.0.6
  • Microsoft.Bcl.HashCode 6.0.0
  • Microsoft.CSharp 4.7.0
  • Microsoft.Extensions.DependencyInjection 9.0.6
  • Microsoft.Extensions.DependencyInjection.Abstractions 9.0.6
  • Microsoft.IO.RecyclableMemoryStream 3.0.1
  • Microsoft.NETCore.Platforms 7.0.4
  • Microsoft.Owin 4.2.2
  • Microsoft.Owin.Host.SystemWeb 4.2.2
  • Microsoft.Owin.Security 4.2.2
  • Microsoft.Owin.Security.Cookies 4.2.2
  • Microsoft.Owin.Security.OAuth 4.2.2
  • Microsoft.Web.Infrastructure 2.0.0
  • Microsoft.Win32.Primitives 4.3.0
  • MimeKit 4.13.0
  • Modernizr 2.8.3
  • Modernizr.New 3.4.0
  • Moment.js 2.30.1
  • NETStandard.Library 2.0.3
  • NLog 6.0.1
  • NLog.Schema 6.0.1
  • NPOI 2.7.4
  • NReco.PdfGenerator 1.2.1
  • Newtonsoft.Json 13.0.3
  • Newtonsoft.Json.Bson 1.0.3
  • Npoi.Mapper 6.2.2
  • Oracle.ManagedDataAccess 23.9.0
  • Owin 1.0
  • Portable.BouncyCastle 1.9.0
  • ProDotNetZip 1.20.0
  • RBush 4.0.0
  • RBush.Signed 4.0.0
  • Respond 1.4.2
  • RestSharp 106.15.0
  • RestSharp 112.1.0
  • Select2.js 4.0.13
  • SendGrid 9.29.3
  • SendGrid.CSharp.HTTP.Client 3.4.12
  • SendGrid.SmtpApi 1.4.6
  • SharpZipLib 1.4.2
  • SixLabors.Fonts 1.0.1
  • SixLabors.ImageSharp 3.1.10
  • Swashbuckle 5.6.0
  • Swashbuckle.Core 5.6.0
  • System.AppContext 4.3.0
  • System.Buffers 4.6.1
  • System.Collections 4.3.0
  • System.Collections.Concurrent 4.3.0
  • System.Collections.Immutable 9.0.6
  • System.Collections.Immutable 9.0.7
  • System.Configuration.ConfigurationManager 9.0.6
  • System.Console 4.3.1
  • System.Diagnostics.Debug 4.3.0
  • System.Diagnostics.DiagnosticSource 9.0.6
  • System.Diagnostics.Tools 4.3.0
  • System.Diagnostics.Tracing 4.3.0
  • System.Formats.Asn1 9.0.6
  • System.Formats.Asn1 9.0.7
  • System.Globalization 4.3.0
  • System.Globalization.Calendars 4.3.0
  • System.IO 4.3.0
  • System.IO.Compression 4.3.0
  • System.IO.Compression.ZipFile 4.3.0
  • System.IO.FileSystem 4.3.0
  • System.IO.FileSystem.Primitives 4.3.0
  • System.IO.Packaging 9.0.6
  • System.IO.Packaging 9.0.7
  • System.IO.Pipelines 9.0.6
  • System.IO.Pipelines 9.0.7
  • System.IO.UnmanagedMemoryStream 4.3.0
  • System.Linq 4.3.0
  • System.Linq.Dynamic.Core 1.6.6
  • System.Linq.Expressions 4.3.0
  • System.Linq.Queryable 4.3.0
  • System.Memory 4.6.3
  • System.Net.Http 4.3.4
  • System.Net.Primitives 4.3.1
  • System.Net.Sockets 4.3.0
  • System.Numerics.Vectors 4.6.1
  • System.ObjectModel 4.3.0
  • System.Reflection 4.3.0
  • System.Reflection.Emit.ILGeneration 4.7.0
  • System.Reflection.Emit.Lightweight 4.7.0
  • System.Reflection.Extensions 4.3.0
  • System.Reflection.Primitives 4.3.0
  • System.Resources.ResourceManager 4.3.0
  • System.Runtime 4.3.1
  • System.Runtime.CompilerServices.Unsafe 6.1.2
  • System.Runtime.CompilerServices.Unsafe 7.0.0-preview.2.22152.2
  • System.Runtime.Extensions 4.3.1
  • System.Runtime.Handles 4.3.0
  • System.Runtime.InteropServices 4.3.0
  • System.Runtime.InteropServices.RuntimeInformation 4.3.0
  • System.Runtime.Numerics 4.3.0
  • System.Security.AccessControl 6.0.1
  • System.Security.Cryptography.Algorithms 4.3.1
  • System.Security.Cryptography.Encoding 4.3.0
  • System.Security.Cryptography.Pkcs 9.0.6
  • System.Security.Cryptography.Primitives 4.3.0
  • System.Security.Cryptography.X509Certificates 4.3.2
  • System.Security.Cryptography.Xml 9.0.6
  • System.Security.Permissions 9.0.6
  • System.Security.Principal.Windows 5.0.0
  • System.Text.Encoding 4.3.0
  • System.Text.Encoding.CodePages 9.0.6
  • System.Text.Encoding.CodePages 9.0.7
  • System.Text.Encoding.Extensions 4.3.0
  • System.Text.Encodings.Web 9.0.6
  • System.Text.Encodings.Web 9.0.7
  • System.Text.Json 9.0.6
  • System.Text.Json 9.0.7
  • System.Text.RegularExpressions 4.3.1
  • System.Threading 4.3.0
  • System.Threading.Tasks 4.3.0
  • System.Threading.Tasks.Extensions 4.6.3
  • System.Threading.Tasks.Parallel 4.3.0
  • System.Threading.Timer 4.3.0
  • System.ValueTuple 4.5.0
  • System.ValueTuple 4.6.1
  • System.Xml.Linq 3.5.21022.801
  • System.Xml.ReaderWriter 4.3.1
  • System.Xml.XDocument 4.3.0
  • WebActivatorEx 2.2.0
  • WebGrease 1.6.0
  • WkHtmlToPdf 1.0.2
  • X.PagedList 10.5.7
  • XLParser 1.7.5
  • ZString 2.6.0
  • bootstrap 3.4.1
  • bootstrap 5.3.7
  • elmah 1.2.2
  • elmah.corelibrary 1.2.2
  • html5-shiv 3.7.3
  • icheck-bootstrap 3.0.1
  • jQuery 3.7.1
  • jQuery-datetimepicker 2.4.5
  • jQuery.UI.Combined 1.14.1
  • jQuery.Validation 1.21.0
  • js-cookie 2.2.1
  • popper.js 1.16.1
  • starkbank-ecdsa 1.3.3
  • vue 2.6.11

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

ChatGPT 說:

在 ASP.NET WebForm 專案中啟用 CSP(Content Security Policy) 後,會影響到 執行期會載入 JS/CSS 或執行動態 script/style 相關功能 的 NuGet 套件,特別是以下幾種類型會受限:

✅ 不會受 CSP 影響的 NuGet 套件類型(安全):

這些套件大多為:

  • 後端函式庫(處理資料、壓縮、加密、Excel、PDF)
  • 非動態載入資源、不執行 script 的前端套件
  • Entity Framework、ASP.NET Identity、NLog 等非 UI 相依元件

✅ 例子:

  • Aspose.Cells
  • ClosedXML
  • ExcelDataReader
  • EntityFramework
  • NPOI
  • Autofac
  • Newtonsoft.Json
  • MailKit / MimeKit
  • System.*、Microsoft.* 類別庫(非 WebResource)

⚠️ 可能會受 CSP 限制的 NuGet 套件類型(需注意)

這些元件若在前端透過 JS 動態載入、執行 inline script、style、eval、或使用 <script src="..."> 引用外部資源,在 CSP 預設 script-src 'self' 限制下會 被封鎖執行:

☑️ 建議設定:

  • CSP 中加入 unsafe-inline 或指定可信任的 CDN 來源(如 https://cdn.jsdelivr.net)

🔻 具體列出受影響的套件(⚠️ 若 CSP 嚴格,會壞掉或無法顯示)

套件問題原因
Bootstrap.Datepicker使用 inline script 及外部 CSS
Chart.js動態產生 <canvas> + JS 渲染
Font.Awesome, FontAwesome有時從 CDN 載入字型(需 font-src)
Modernizr, Modernizr.New使用 inline script
Moment.js若從外部載入或使用 eval
Select2.js有 inline JS 和外部資源
Respond動態 CSS 載入
jQuery, jQuery-UI, jQuery-datetimepicker, jQuery.Validation若用 inline JS 或外部載入
vue預設模板會包含 eval 或 inline JS
js-cookie, popper.js使用外部 CDN 時會被阻擋
bootstrap預設範例會用 inline script(如 tooltip)
html5-shiv, icheck-bootstrap需動態加載或 inline style

✅ 部分前端套件若本地化引用、無 inline JS 可正常運作

例如:

bootstrap 3.4.1 與 jQuery 若為本地 .js 檔案引用 + 無 eval、無 inline script,加上:

script-src 'self';

style-src 'self';

→ 可運作

但若套件包含像這樣的 <script>$('#abc').tooltip()</script> 則會因 CSP 阻擋而失效,需加上:

script-src 'self' 'unsafe-inline';

💡 建議做法:

  • 嚴格 CSP: 不建議使用含 inline script/style 的 UI 套件
  • 放寬 CSP: Global.asax.cs加上:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    HttpContext.Current.Response.Headers.Add("Content-Security-Policy",
        "default-src 'self'; " +
        "script-src 'self' 'unsafe-inline' 'unsafe-eval' https://cdn.jsdelivr.net https://cdnjs.cloudflare.com https://unpkg.com; " +
        "style-src 'self' 'unsafe-inline' https://cdn.jsdelivr.net https://fonts.googleapis.com https://cdnjs.cloudflare.com; " +
        "font-src 'self' https://fonts.gstatic.com data:; " +
        "img-src 'self' data: https://cdn.jsdelivr.net; " +
        "connect-src 'self'; " +
        "frame-src 'self'; " +
        "object-src 'none'; " +
        "base-uri 'self'; " +
        "form-action 'self';"
    );
}

📦 常見 CDN 對應參考

  • Bootstrap / jQuery / FontAwesome / Chart.js https://cdn.jsdelivr.net, https://cdnjs.cloudflare.com
  • Select2 https://cdnjs.cloudflare.com
  • Vue https://unpkg.com
  • Google Fonts https://fonts.googleapis.com, https://fonts.gstatic.com

(完)

相關

[研究]ASP.NET , WebForm, Nuget 哪些套件會因為 CSP 防護而無法運作 ?

[研究]ASP.NET , WebForm, Nuget 哪些套件會因為 CSP 防護而無法運作 ?

2025-07-21

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

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

ChatGPT 說:

在 ASP.NET WebForm 中,導入 CSP(Content-Security-Policy) 是一個強化網站安全性的好做法,但它也可能會「阻擋」或「影響某些 NuGet 套件的功能」,尤其是那些會產生或使用 inline script/style、eval()、第三方 CDN 資源 的套件。

以下是一些 常見在 WebForm 中使用、可能會因為 CSP 設定而無法正常運作或需要額外設定的 NuGet 套件類型與實例:

🔶 1. WYSIWYG 編輯器套件

這類套件多數會使用 inline JavaScript 與第三方外部資源(如 Google Fonts、CDN script)。

  • CKEditor
  • TinyMCE
  • Summernote
  • Quill

影響:

  • 被 CSP 阻擋 inline script (unsafe-inline)
  • 外部資源(如 cdn.jsdelivr.net, cdnjs.cloudflare.com)被 CSP 限制

🔶 2. 圖表/資料視覺化套件

例如:

  • Chart.js
  • Highcharts
  • Google Charts

影響:

  • 動態產生 inline <script>、<style> 被 CSP 阻擋
  • 使用 Google 字體、圖示會被 CSP 阻擋

🔶 3. 前端 UI 套件 / 元件庫

  • Bootstrap(若透過 CDN 載入)
  • jQuery UI
  • FontAwesome
  • Toastr.js(通知訊息)
  • SweetAlert2(彈窗元件)

影響:

  • 若 CSP 未允許對應的 CDN,無法載入資源
  • style-src、script-src 未允許 unsafe-inline,將導致樣式失效、JavaScript 功能失效

🔶 4. 驗證或安全工具

例如:

  • Google reCAPTCHA v2/v3
  • hCaptcha

影響:

  • 需要允許 script-src 和 frame-src 加入 Google 的網域如:
    • https://www.google.com
    • https://www.gstatic.com
    • https://www.recaptcha.net

🔶 5. SPA 或 MVVM 類型的前端工具(混合使用)

  • Knockout.js
  • AngularJS (老版本)
  • React(如果 SSR 或 Hybrid 嵌入 WebForm)

影響:

  • 若套件內部使用 eval() 或 new Function(),需 CSP 加入 unsafe-eval 才能運作
  • 無法插入 <script>,或腳本被阻擋

🔶 解決方式(針對 CSP 衝突):

問題解法
inline <script> 被擋加入:script-src 'self' 'unsafe-inline'(不建議,但必要時可)
第三方資源被擋加入對應來源,例如 script-src https://cdn.jsdelivr.net
Google 字體被擋加入:style-src https://fonts.googleapis.com 和 font-src https://fonts.gstatic.com
使用 eval() 的 JS 被擋加入:script-src 'unsafe-eval'(非常不建議,會大幅降低安全性)
iframe 被擋加入:frame-src https://www.google.com

✅ 實際範例(最小允許設定)

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

結論:

若你在 WebForm 使用以下類型的 NuGet 套件:

  • 動態產生 script 的元件
  • 外部資源依賴重的前端工具(WYSIWYG 編輯器、圖表、驗證工具)
  • 引入第三方字型、CDN script

那麼在未調整 CSP 前,這些功能極可能會失效。

警告:只要CSP設定了任何排除,HCL AppScan掃描結果就會報告系統有 CSP 弱點。不排除,一堆常用功能套件可能癱瘓掉。

(完)

相關

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月10日 星期四

[研究]CSP弱點線上檢測網站 - CSP Evaluator

[研究]CSP弱點線上檢測網站 - CSP Evaluator

2025-07-10

CSP Evaluator 網站

https://csp-evaluator.withgoogle.com/

CSP Evaluator allows developers and security experts to check if a Content Security Policy (CSP) serves as a strong mitigation against cross-site scripting attacks. It assists with the process of reviewing CSP policies, which is usually a manual task, and helps identify subtle CSP bypasses which undermine the value of a policy. CSP Evaluator checks are based on a large-scale study and are aimed to help developers to harden their CSP and improve the security of their applications. This tool (also available as a Chrome extension) is provided only for the convenience of developers and Google provides no guarantees or warranties for this tool.

CSP 評估器可協助開發者和安全專家檢查內容安全策略 (CSP) 是否能夠有效緩解跨站腳本攻擊。它能夠協助審核 CSP 策略(這通常是一項手動任務),並有助於識別那些可能損害策略價值的微妙 CSP 繞過方法。 CSP 評估器的檢查是基於一項大規模研究,旨在幫助開發者強化 CSP 並提高其應用的安全性。此工具(也可作為 Chrome 擴充功能)僅為方便開發者而提供,Google 不對此工具提供任何擔保或保證。

(完)

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


(完)

相關