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

(完)

相關

沒有留言:

張貼留言