我有一個非常簡單的 TagHelper,它將當前 CSP 亂數添加到指定的標簽。
這一切正常,直到我開始使用asp-append-version它,此時瀏覽器開始抱怨腳本被阻止:
[僅報告] 拒絕加載腳本,因為它違反了以下內容安全策略指令:“script-src 'strict-dynamic' 'nonce-7TYX/FgHsrvHOORSEBRB3h0e'”。請注意,存在“嚴格動態”,因此禁用了基于主機的白名單。請注意,'script-src-elem' 沒有明確設定,因此 'script-src' 用作后備。
當我查看頁面源代碼時,我可以看到我的 nonce 已正確應用,但是我們最終得到了兩個 src對我來說似乎不正確的屬性:
<script src="/web/scripts/main.js?v=K0HDUmKd22yWAqRgrnhcGk69aHiFH8qUh2kPLDSbV0c"
src="/web/scripts/main.js"
nonce="GLBtUe54MABq4Ld2Wtznf8P2"></script>
這可能是問題嗎?我看到兩個屬性,即使沒有自定義標簽助手(也嘗試在系統中注銷所有自定義標簽助手,但我仍然面臨這個問題)。
它甚至不適用于靜態硬編碼的亂數(根本沒有標簽助手)。
示例腳本參考:
<script asp-append-version="true" asp-add-nonce="true" src="/web/scripts/main.js"></script>
TagHelper 類:
[HtmlTargetElement("script", Attributes = "asp-add-nonce")]
public class NonceTagHelper : TagHelper
{
private readonly ILogger<NonceTagHelper> logger;
private readonly ICspNonceBuilder cspNonceBuilder;
public NonceTagHelper(ILogger<NonceTagHelper> logger, ICspNonceBuilder cspNonceBuilder)
{
this.logger = logger;
this.cspNonceBuilder = cspNonceBuilder;
}
[HtmlAttributeName("asp-add-nonce")]
public bool AddNonce { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output)
{
if (!this.AddNonce)
{
return;
}
var nonce = this.cspNonceBuilder.GetRequestNonce();
output.Attributes.SetAttribute("nonce", nonce);
}
}
如果我洗掉,這一切都開始正常作業asp-append-version- 輸出:
<script nonce="GLBtUe54MABq4Ld2Wtznf8P2" type="module" src="/web/scripts/main.js"></script>
但如果可能的話,我想繼續使用它。我錯過了什么?我不確定它是自定義標簽助手,因為沒有它(硬編碼的亂數)問題是可重現的。
uj5u.com熱心網友回復:
我發現了這個問題。Umbraco 9 包括一個捆綁和縮小包作為標準https://github.com/Shazwazza/Smidge:
@addTagHelper *, Smidge
@inject Smidge.SmidgeHelper SmidgeHelper
Smidge 包括一些針對標簽src屬性的<script>標簽助手。
似乎它正在復制整個屬性:
https://github.com/Shazwazza/Smidge/blob/master/src/Smidge/TagHelpers/SmidgeScriptTagHelper.cs#L48
// Pass through attribute that is also a well-known HTML attribute.
// this is required to make sure that other tag helpers executing against this element have
// the value copied across
if (Source != null)
{
output.CopyHtmlAttribute("src", context);
}
從解決方案中洗掉它可以解決問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/468055.html
標籤:C# asp.net 核心 安全 乌布拉科 内容安全策略
