2021-03-11 13:14:28 +01:00
|
|
|
|
using System.IO;
|
2021-02-18 11:06:02 +01:00
|
|
|
|
using Umbraco.Cms.Core.Configuration.Models;
|
2021-03-11 13:14:28 +01:00
|
|
|
|
using Umbraco.Cms.Core.Exceptions;
|
2021-02-18 11:06:02 +01:00
|
|
|
|
using Umbraco.Cms.Core.Hosting;
|
2020-03-17 16:26:56 +01:00
|
|
|
|
|
2021-02-18 11:06:02 +01:00
|
|
|
|
namespace Umbraco.Extensions
|
2020-03-17 16:26:56 +01:00
|
|
|
|
{
|
|
|
|
|
|
public static class ModelsBuilderConfigExtensions
|
|
|
|
|
|
{
|
|
|
|
|
|
private static string _modelsDirectoryAbsolute = null;
|
|
|
|
|
|
|
2020-09-18 12:53:10 +02:00
|
|
|
|
public static string ModelsDirectoryAbsolute(this ModelsBuilderSettings modelsBuilderConfig, IHostingEnvironment hostingEnvironment)
|
2020-03-17 16:26:56 +01:00
|
|
|
|
{
|
|
|
|
|
|
if (_modelsDirectoryAbsolute is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var modelsDirectory = modelsBuilderConfig.ModelsDirectory;
|
2020-09-02 14:44:01 +02:00
|
|
|
|
var root = hostingEnvironment.MapPathContentRoot("~/");
|
2020-03-17 16:26:56 +01:00
|
|
|
|
|
|
|
|
|
|
_modelsDirectoryAbsolute = GetModelsDirectory(root, modelsDirectory,
|
|
|
|
|
|
modelsBuilderConfig.AcceptUnsafeModelsDirectory);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return _modelsDirectoryAbsolute;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// internal for tests
|
|
|
|
|
|
internal static string GetModelsDirectory(string root, string config, bool acceptUnsafe)
|
|
|
|
|
|
{
|
|
|
|
|
|
// making sure it is safe, ie under the website root,
|
|
|
|
|
|
// unless AcceptUnsafeModelsDirectory and then everything is OK.
|
|
|
|
|
|
|
|
|
|
|
|
if (!Path.IsPathRooted(root))
|
2021-03-11 13:14:28 +01:00
|
|
|
|
throw new ConfigurationException($"Root is not rooted \"{root}\".");
|
2020-03-17 16:26:56 +01:00
|
|
|
|
|
|
|
|
|
|
if (config.StartsWith("~/"))
|
|
|
|
|
|
{
|
|
|
|
|
|
var dir = Path.Combine(root, config.TrimStart("~/"));
|
|
|
|
|
|
|
|
|
|
|
|
// sanitize - GetFullPath will take care of any relative
|
|
|
|
|
|
// segments in path, eg '../../foo.tmp' - it may throw a SecurityException
|
|
|
|
|
|
// if the combined path reaches illegal parts of the filesystem
|
|
|
|
|
|
dir = Path.GetFullPath(dir);
|
|
|
|
|
|
root = Path.GetFullPath(root);
|
|
|
|
|
|
|
|
|
|
|
|
if (!dir.StartsWith(root) && !acceptUnsafe)
|
2021-03-11 13:14:28 +01:00
|
|
|
|
throw new ConfigurationException($"Invalid models directory \"{config}\".");
|
2020-03-17 16:26:56 +01:00
|
|
|
|
|
|
|
|
|
|
return dir;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (acceptUnsafe)
|
|
|
|
|
|
return Path.GetFullPath(config);
|
|
|
|
|
|
|
2021-03-11 13:14:28 +01:00
|
|
|
|
throw new ConfigurationException($"Invalid models directory \"{config}\".");
|
2020-03-17 16:26:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|