Fix #2439 - ensure resource urls are constructed consistently on client and server

This commit is contained in:
Shaun Walker 2022-09-28 09:43:02 -04:00
parent 72cc44641b
commit b7a3713946
5 changed files with 28 additions and 16 deletions

View File

@ -75,7 +75,7 @@ namespace Oqtane.Modules
var scripts = new List<object>();
foreach (Resource resource in Resources.Where(item => item.ResourceType == ResourceType.Script))
{
var url = (resource.Url.Contains("://")) ? resource.Url : PageState.Alias.BaseUrl + (!resource.Url.StartsWith("/") ? "/" : "") + resource.Url;
var url = (resource.Url.Contains("://")) ? resource.Url : PageState.Alias.BaseUrl + resource.Url;
scripts.Add(new { href = url, bundle = resource.Bundle ?? "", integrity = resource.Integrity ?? "", crossorigin = resource.CrossOrigin ?? "", es6module = resource.ES6Module });
}
if (scripts.Any())

View File

@ -35,7 +35,8 @@ namespace Oqtane.Themes
var scripts = new List<object>();
foreach (Resource resource in Resources.Where(item => item.ResourceType == ResourceType.Script))
{
scripts.Add(new { href = resource.Url, bundle = resource.Bundle ?? "", integrity = resource.Integrity ?? "", crossorigin = resource.CrossOrigin ?? "", es6module = resource.ES6Module });
var url = (resource.Url.Contains("://")) ? resource.Url : PageState.Alias.BaseUrl + resource.Url;
scripts.Add(new { href = url, bundle = resource.Bundle ?? "", integrity = resource.Integrity ?? "", crossorigin = resource.CrossOrigin ?? "", es6module = resource.ES6Module });
}
if (scripts.Any())
{

View File

@ -36,7 +36,7 @@
foreach (Resource resource in PageState.Page.Resources.Where(item => item.ResourceType == ResourceType.Stylesheet))
{
var prefix = "app-stylesheet-" + resource.Level.ToString().ToLower();
var url = (resource.Url.Contains("://")) ? resource.Url : PageState.Alias.BaseUrl + (!resource.Url.StartsWith("/") ? "/" : "") + resource.Url;
var url = (resource.Url.Contains("://")) ? resource.Url : PageState.Alias.BaseUrl + resource.Url;
links.Add(new { id = prefix + "-" + batch + "-" + (links.Count + 1).ToString("00"), rel = "stylesheet", href = url, type = "text/css", integrity = resource.Integrity ?? "", crossorigin = resource.CrossOrigin ?? "", insertbefore = prefix });
}
if (links.Any())

View File

@ -20,6 +20,7 @@ using Oqtane.Enums;
using Oqtane.Security;
using Oqtane.Extensions;
using Oqtane.Themes;
using Oqtane.UI;
namespace Oqtane.Pages
{
@ -179,7 +180,7 @@ namespace Oqtane.Pages
{
ThemeType = page.ThemeType;
}
ProcessThemeResources(ThemeType);
ProcessThemeResources(ThemeType, alias);
}
else // page not found
{
@ -203,7 +204,7 @@ namespace Oqtane.Pages
var assemblies = AppDomain.CurrentDomain.GetOqtaneAssemblies();
foreach (Assembly assembly in assemblies)
{
ProcessHostResources(assembly);
ProcessHostResources(assembly, alias);
}
// set culture if not specified
@ -436,7 +437,7 @@ namespace Oqtane.Pages
"</script>";
}
private void ProcessHostResources(Assembly assembly)
private void ProcessHostResources(Assembly assembly, Alias alias)
{
var types = assembly.GetTypes().Where(item => item.GetInterfaces().Contains(typeof(IHostResources)));
foreach (var type in types)
@ -445,12 +446,12 @@ namespace Oqtane.Pages
foreach (var resource in obj.Resources)
{
resource.Level = ResourceLevel.App;
ProcessResource(resource, 0);
ProcessResource(resource, 0, alias);
}
}
}
private void ProcessThemeResources(string ThemeType)
private void ProcessThemeResources(string ThemeType, Alias alias)
{
var type = Type.GetType(ThemeType);
if (type != null)
@ -462,40 +463,41 @@ namespace Oqtane.Pages
foreach (var resource in obj.Resources.Where(item => item.ResourceType == ResourceType.Stylesheet))
{
resource.Level = ResourceLevel.Page;
ProcessResource(resource, count++);
ProcessResource(resource, count++, alias);
}
}
}
}
private void ProcessResource(Resource resource, int count)
private void ProcessResource(Resource resource, int count, Alias alias)
{
var url = (resource.Url.Contains("://")) ? resource.Url : alias.BaseUrl + resource.Url;
switch (resource.ResourceType)
{
case ResourceType.Stylesheet:
if (!HeadResources.Contains(resource.Url, StringComparison.OrdinalIgnoreCase))
if (!HeadResources.Contains(url, StringComparison.OrdinalIgnoreCase))
{
string id = "";
if (resource.Level == ResourceLevel.Page)
{
id = "id=\"app-stylesheet-" + resource.Level.ToString().ToLower() + "-" + DateTime.UtcNow.ToString("yyyyMMddHHmmssfff") + "-" + count.ToString("00") + "\" ";
}
HeadResources += "<link " + id + "rel=\"stylesheet\" href=\"" + resource.Url + "\"" + CrossOrigin(resource.CrossOrigin) + Integrity(resource.Integrity) + " />" + Environment.NewLine;
HeadResources += "<link " + id + "rel=\"stylesheet\" href=\"" + url + "\"" + CrossOrigin(resource.CrossOrigin) + Integrity(resource.Integrity) + " type=\"text/css\"/>" + Environment.NewLine;
}
break;
case ResourceType.Script:
if (resource.Location == Shared.ResourceLocation.Body)
{
if (!BodyResources.Contains(resource.Url, StringComparison.OrdinalIgnoreCase))
if (!BodyResources.Contains(url, StringComparison.OrdinalIgnoreCase))
{
BodyResources += "<script src=\"" + resource.Url + "\"" + CrossOrigin(resource.CrossOrigin) + Integrity(resource.Integrity) + "></script>" + Environment.NewLine;
BodyResources += "<script src=\"" + url + "\"" + CrossOrigin(resource.CrossOrigin) + Integrity(resource.Integrity) + "></script>" + Environment.NewLine;
}
}
else
{
if (!HeadResources.Contains(resource.Url, StringComparison.OrdinalIgnoreCase))
{
HeadResources += "<script src=\"" + resource.Url + "\"" + CrossOrigin(resource.CrossOrigin) + Integrity(resource.Integrity) + "></script>" + Environment.NewLine;
HeadResources += "<script src=\"" + url + "\"" + CrossOrigin(resource.CrossOrigin) + Integrity(resource.Integrity) + "></script>" + Environment.NewLine;
}
}
break;

View File

@ -8,6 +8,8 @@ namespace Oqtane.Models
/// </summary>
public class Resource
{
private string _url;
/// <summary>
/// A <see cref="ResourceType"/> so the Interop can properly create `script` or `link` tags
/// </summary>
@ -16,7 +18,14 @@ namespace Oqtane.Models
/// <summary>
/// Path to the resources.
/// </summary>
public string Url { get; set; }
public string Url
{
get => _url;
set
{
_url = (value.Contains("://")) ? value : (!value.StartsWith("/") ? "/" : "") + value;
}
}
/// <summary>
/// Integrity checks to increase the security of resources accessed. Especially common in CDN resources.