Skip to content

Commit

Permalink
Merge pull request #164 from markadrake/feature/protect_file_extensions
Browse files Browse the repository at this point in the history
Feature/protect file extensions
  • Loading branch information
Shazwazza committed Oct 26, 2022
2 parents eb018d4 + d223669 commit e38b7dc
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 16 deletions.
16 changes: 9 additions & 7 deletions src/Smidge.Core/CompositeFiles/DefaultUrlManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using Smidge.Models;
using System.Text;
Expand All @@ -14,12 +14,14 @@ public class DefaultUrlManager : IUrlManager
private readonly IHasher _hasher;
private readonly IRequestHelper _requestHelper;
private readonly UrlManagerOptions _options;
private readonly ISmidgeConfig _config;

public DefaultUrlManager(IOptions<SmidgeOptions> options, IHasher hasher, IRequestHelper requestHelper)
public DefaultUrlManager(IOptions<SmidgeOptions> options, IHasher hasher, IRequestHelper requestHelper, ISmidgeConfig config)
{
_hasher = hasher;
_requestHelper = requestHelper;
_options = options.Value.UrlOptions;
_config = config;
}

public string AppendCacheBuster(string url, bool debug, string cacheBusterValue)
Expand All @@ -40,7 +42,7 @@ public string GetUrl(string bundleName, string fileExtension, bool debug, string
throw new ArgumentException($"'{nameof(cacheBusterValue)}' cannot be null or whitespace.", nameof(cacheBusterValue));
}

const string handler = "~/{0}/{1}{2}.{3}{4}";
string handler = _config.KeepFileExtensions ? "~/{0}/{1}.{3}{4}{2}" : "~/{0}/{1}{2}.{3}{4}";
return _requestHelper.Content(
string.Format(
handler,
Expand Down Expand Up @@ -135,16 +137,16 @@ public ParsedUrlPath ParsePath(string input)
}

//can start with 'v' or 'd' (d == debug)
var prefix = parts[parts.Length - 1][0];
var prefix = _config.KeepFileExtensions ? parts[parts.Length - 2][0] : parts[parts.Length - 1][0];
if (prefix != 'v' && prefix != 'd')
{
//invalid
return null;
}
result.Debug = prefix == 'd';

result.CacheBusterValue = parts[parts.Length - 1].Substring(1);
var ext = parts[parts.Length - 2];
result.CacheBusterValue = _config.KeepFileExtensions ? parts[parts.Length - 2].Substring(1) : parts[parts.Length - 1].Substring(1);
var ext = _config.KeepFileExtensions ? parts[parts.Length - 1] : parts[parts.Length - 2];
if (!Enum.TryParse(ext, true, out WebFileType type))
{
//invalid
Expand All @@ -161,7 +163,7 @@ private string GetCompositeUrl(string fileKey, string fileExtension, string cach
{
//Create a delimited URL query string

const string handler = "~/{0}/{1}{2}.v{3}";
string handler = _config.KeepFileExtensions ? "~/{0}/{1}.v{3}{2}" : "~/{0}/{1}{2}.v{3}";
return _requestHelper.Content(
string.Format(
handler,
Expand Down
1 change: 1 addition & 0 deletions src/Smidge.Core/ISmidgeConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ public interface ISmidgeConfig
{
string DataFolder { get; }
string Version { get; }
bool KeepFileExtensions { get; }
}
}
6 changes: 4 additions & 2 deletions src/Smidge.Core/SmidgeConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration;
using System.IO;

namespace Smidge
Expand Down Expand Up @@ -34,6 +34,8 @@ public SmidgeConfig()
public string Version => _config["version"] ?? "1";

public string DataFolder => (_config["dataFolder"] ?? "Smidge").Replace('/', Path.DirectorySeparatorChar);


public bool KeepFileExtensions => bool.Parse((_config["keepFileExtensions"] ?? "false"));

}
}
3 changes: 2 additions & 1 deletion src/Smidge.Web/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"smidge": {
"dataFolder": "Smidge", //where the cache files are stored
"version": "125" //can be any string
"version": "125", //can be any string
"keepFileExtensions": true // set to true to keep file extensions
}
}
69 changes: 63 additions & 6 deletions test/Smidge.Tests/DefaultUrlManagerTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using Xunit;
using Smidge.CompositeFiles;
using Smidge;
Expand All @@ -25,7 +25,8 @@ public void Parse_Path()
var manager = new DefaultUrlManager(
Mock.Of<IOptions<SmidgeOptions>>(x => x.Value == options),
Mock.Of<IHasher>(),
Mock.Of<IRequestHelper>());
Mock.Of<IRequestHelper>(),
Mock.Of<ISmidgeConfig>());

var result = manager.ParsePath(path);

Expand All @@ -48,13 +49,39 @@ public void Make_Bundle_Url()
var creator = new DefaultUrlManager(
Mock.Of<IOptions<SmidgeOptions>>(x => x.Value == options),
hasher.Object,
urlHelper);
urlHelper,
Mock.Of<ISmidgeConfig>());

var url = creator.GetUrl("my-bundle", ".js", false, "1");

Assert.Equal("/sg/my-bundle.js.v1", url);
}

[Fact]
public void Make_Bundle_Url_Keep_File_Extensions()
{
var websiteInfo = new Mock<IWebsiteInfo>();
websiteInfo.Setup(x => x.GetBasePath()).Returns(string.Empty);
websiteInfo.Setup(x => x.GetBaseUrl()).Returns(new Uri("http://test.com"));

var urlHelper = new RequestHelper(websiteInfo.Object);
var hasher = new Mock<IHasher>();
hasher.Setup(x => x.Hash(It.IsAny<string>())).Returns("blah");
var options = new SmidgeOptions { UrlOptions = new UrlManagerOptions { BundleFilePath = "sg" } };
var config = new Mock<ISmidgeConfig>();
config.Setup(m => m.KeepFileExtensions).Returns(true);
var creator = new DefaultUrlManager(
Mock.Of<IOptions<SmidgeOptions>>(x => x.Value == options),
hasher.Object,
urlHelper,
config.Object);

var url = creator.GetUrl("my-bundle", ".js", false, "1");

Assert.Equal("/sg/my-bundle.v1.js", url);
}


[Fact]
public void Make_Composite_Url()
{
Expand All @@ -69,7 +96,8 @@ public void Make_Composite_Url()
var creator = new DefaultUrlManager(
Mock.Of<IOptions<SmidgeOptions>>(x => x.Value == options),
hasher.Object,
urlHelper);
urlHelper,
Mock.Of<ISmidgeConfig>());

var url = creator.GetUrls(
new List<IWebFile> { new JavaScriptFile("Test1.js"), new JavaScriptFile("Test2.js") }, ".js", "1");
Expand All @@ -79,6 +107,33 @@ public void Make_Composite_Url()
Assert.Equal("test1.test2", url.First().Key);
}

[Fact]
public void Make_Composite_Url_Keep_File_Extensions()
{
var websiteInfo = new Mock<IWebsiteInfo>();
websiteInfo.Setup(x => x.GetBasePath()).Returns(string.Empty);
websiteInfo.Setup(x => x.GetBaseUrl()).Returns(new Uri("http://test.com"));

var urlHelper = new RequestHelper(websiteInfo.Object);
var hasher = new Mock<IHasher>();
hasher.Setup(x => x.Hash(It.IsAny<string>())).Returns((string s) => s.ToLower());
var options = new SmidgeOptions { UrlOptions = new UrlManagerOptions { CompositeFilePath = "sg", MaxUrlLength = 100 } };
var config = new Mock<ISmidgeConfig>();
config.Setup(m => m.KeepFileExtensions).Returns(true);
var creator = new DefaultUrlManager(
Mock.Of<IOptions<SmidgeOptions>>(x => x.Value == options),
hasher.Object,
urlHelper,
config.Object);

var url = creator.GetUrls(
new List<IWebFile> { new JavaScriptFile("Test1.js"), new JavaScriptFile("Test2.js") }, ".js", "1");

Assert.Single(url);
Assert.Equal("/sg/Test1.Test2.v1.js", url.First().Url);
Assert.Equal("test1.test2", url.First().Key);
}

[Fact]
public void Make_Composite_Url_Splits()
{
Expand All @@ -93,7 +148,8 @@ public void Make_Composite_Url_Splits()
var creator = new DefaultUrlManager(
Mock.Of<IOptions<SmidgeOptions>>(x => x.Value == options),
hasher.Object,
urlHelper);
urlHelper,
Mock.Of<ISmidgeConfig>());

var url = creator.GetUrls(
new List<IWebFile> { new JavaScriptFile("Test1.js"), new JavaScriptFile("Test2.js") }, ".js", "1");
Expand All @@ -119,7 +175,8 @@ public void Throws_When_Single_Dependency_Too_Long()
var creator = new DefaultUrlManager(
Mock.Of<IOptions<SmidgeOptions>>(x => x.Value == options),
hasher.Object,
urlHelper);
urlHelper,
Mock.Of<ISmidgeConfig>());

Assert.Throws<InvalidOperationException>(() => creator.GetUrls(
new List<IWebFile> { new JavaScriptFile("Test1.js") }, ".js", "1"));
Expand Down

0 comments on commit e38b7dc

Please sign in to comment.