parent
82c323f599
commit
ae0c3dc42a
|
|
@ -77,13 +77,7 @@ namespace Templates.Test
|
||||||
[MemberData(nameof(SubresourceIntegrityCheckScriptData))]
|
[MemberData(nameof(SubresourceIntegrityCheckScriptData))]
|
||||||
public async Task CheckScriptSubresourceIntegrity(ScriptTag scriptTag)
|
public async Task CheckScriptSubresourceIntegrity(ScriptTag scriptTag)
|
||||||
{
|
{
|
||||||
string expectedIntegrity;
|
var expectedIntegrity = await GetShaIntegrity(scriptTag);
|
||||||
using (var responseStream = await _httpClient.GetStreamAsync(scriptTag.Src))
|
|
||||||
using (var alg = SHA256.Create())
|
|
||||||
{
|
|
||||||
var hash = alg.ComputeHash(responseStream);
|
|
||||||
expectedIntegrity = "sha256-" + Convert.ToBase64String(hash);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!expectedIntegrity.Equals(scriptTag.Integrity, StringComparison.OrdinalIgnoreCase))
|
if (!expectedIntegrity.Equals(scriptTag.Integrity, StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
|
|
@ -95,13 +89,7 @@ namespace Templates.Test
|
||||||
[MemberData(nameof(SubresourceIntegrityCheckLinkData))]
|
[MemberData(nameof(SubresourceIntegrityCheckLinkData))]
|
||||||
public async Task CheckLinkSubresourceIntegrity(LinkTag linkTag)
|
public async Task CheckLinkSubresourceIntegrity(LinkTag linkTag)
|
||||||
{
|
{
|
||||||
string expectedIntegrity;
|
var expectedIntegrity = await GetShaIntegrity(linkTag);
|
||||||
using (var responseStream = await _httpClient.GetStreamAsync(linkTag.HRef))
|
|
||||||
using (var alg = SHA256.Create())
|
|
||||||
{
|
|
||||||
var hash = alg.ComputeHash(responseStream);
|
|
||||||
expectedIntegrity = "sha256-" + Convert.ToBase64String(hash);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!expectedIntegrity.Equals(linkTag.Integrity, StringComparison.OrdinalIgnoreCase))
|
if (!expectedIntegrity.Equals(linkTag.Integrity, StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
|
|
@ -162,6 +150,27 @@ namespace Templates.Test
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Task<string> GetShaIntegrity(ScriptTag scriptTag)
|
||||||
|
{
|
||||||
|
return GetShaIntegrity(scriptTag.Integrity, scriptTag.Src);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Task<string> GetShaIntegrity(LinkTag linkTag)
|
||||||
|
{
|
||||||
|
return GetShaIntegrity(linkTag.Integrity, linkTag.HRef);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<string> GetShaIntegrity(string integrity, string src)
|
||||||
|
{
|
||||||
|
var prefix = integrity.Substring(0, 6);
|
||||||
|
using (var respStream = await _httpClient.GetStreamAsync(src))
|
||||||
|
using (HashAlgorithm alg = string.Equals(prefix, "sha256") ? (HashAlgorithm)SHA256.Create() : (HashAlgorithm)SHA384.Create())
|
||||||
|
{
|
||||||
|
var hash = alg.ComputeHash(respStream);
|
||||||
|
return $"{prefix}-" + Convert.ToBase64String(hash);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static string GetFileContentFromArchive(ScriptTag scriptTag, string relativeFilePath)
|
private static string GetFileContentFromArchive(ScriptTag scriptTag, string relativeFilePath)
|
||||||
{
|
{
|
||||||
var file = Path.Combine(_artifactsDir, scriptTag.FileName);
|
var file = Path.Combine(_artifactsDir, scriptTag.FileName);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue