Removes the logic for sanitizing the base path for static web assets (dotnet/aspnetcore-tooling#700)

Removes the logic for sanitizing the base path for static web assets\n\nCommit migrated from 98b168c894
This commit is contained in:
Javier Calvarro Nelson 2019-06-25 18:35:38 +02:00 committed by GitHub
parent adfe6f09e4
commit 19d9dbcd43
4 changed files with 19 additions and 174 deletions

View File

@ -1,40 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace Microsoft.AspNetCore.Razor.Tasks
{
public class GetDefaultStaticWebAssetsBasePath : Task
{
[Required]
public string BasePath { get; set; }
[Output]
public string SafeBasePath { get; set; }
public override bool Execute()
{
if (string.IsNullOrWhiteSpace(BasePath))
{
Log.LogError($"Base path '{BasePath ?? "(null)"}' must contain non-whitespace characters.");
return !Log.HasLoggedErrors;
}
var safeBasePath = BasePath
.Replace(" ", "")
.Replace(".", "")
.ToLowerInvariant();
if (safeBasePath == "")
{
Log.LogError($"Base path '{BasePath}' must contain non '.' characters.");
return !Log.HasLoggedErrors;
}
SafeBasePath = safeBasePath;
return !Log.HasLoggedErrors;
}
}
}

View File

@ -32,11 +32,6 @@ Copyright (c) .NET Foundation. All rights reserved.
AssemblyFile="$(RazorSdkBuildTasksAssembly)"
Condition="'$(RazorSdkBuildTasksAssembly)' != ''" />
<UsingTask
TaskName="Microsoft.AspNetCore.Razor.Tasks.GetDefaultStaticWebAssetsBasePath"
AssemblyFile="$(RazorSdkBuildTasksAssembly)"
Condition="'$(RazorSdkBuildTasksAssembly)' != ''" />
<UsingTask
TaskName="Microsoft.AspNetCore.Razor.Tasks.GenerateStaticWebAsssetsPropsFile"
AssemblyFile="$(RazorSdkBuildTasksAssembly)"
@ -220,23 +215,8 @@ Copyright (c) .NET Foundation. All rights reserved.
<!-- StaticWebAssets from the current project -->
<!-- Computes a default safe base path from the $(PackageId) that will be a prefix
to all the resources being exported from this library by default. The convention
consists of removing intermediate whitespaces, dots and lower casing all letters
in the package id using an invariant culture.
We don't aim to handle all possible cases for this prefix, as it can get really
complicated (non-unicode characters for example), so for that case,
StaticWebAssetBasePath can be set explicitly and we won't interfere.
-->
<GetDefaultStaticWebAssetsBasePath
BasePath="$(PackageId)"
Condition="'$(StaticWebAssetBasePath)' == ''">
<Output TaskParameter="SafeBasePath" PropertyName="_StaticWebAssetSafeBasePath" />
</GetDefaultStaticWebAssetsBasePath>
<PropertyGroup>
<StaticWebAssetBasePath Condition="$(StaticWebAssetBasePath) == ''">_content/$(_StaticWebAssetSafeBasePath)</StaticWebAssetBasePath>
<StaticWebAssetBasePath Condition="$(StaticWebAssetBasePath) == ''">_content/$(PackageId)</StaticWebAssetBasePath>
</PropertyGroup>
<ItemGroup>

View File

@ -1,95 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using Microsoft.Build.Framework;
using Moq;
using Xunit;
namespace Microsoft.AspNetCore.Razor.Tasks
{
public class GetDefaultStaticWebAssetsBasePathTest
{
[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
[InlineData(" ")]
public void ReturnsError_WhenBasePath_DoesNotContainNonWhitespaceCharacters(string basePath)
{
// Arrange
var expectedError = $"Base path '{basePath ?? "(null)"}' must contain non-whitespace characters.";
var errorMessages = new List<string>();
var buildEngine = new Mock<IBuildEngine>();
buildEngine.Setup(e => e.LogErrorEvent(It.IsAny<BuildErrorEventArgs>()))
.Callback<BuildErrorEventArgs>(args => errorMessages.Add(args.Message));
var task = new GetDefaultStaticWebAssetsBasePath
{
BuildEngine = buildEngine.Object,
BasePath = basePath
};
// Act
var result = task.Execute();
// Assert
Assert.False(result);
var message = Assert.Single(errorMessages);
Assert.Equal(expectedError, message);
}
[Theory]
[InlineData(".")]
[InlineData("..")]
[InlineData(". ")]
[InlineData(" .")]
[InlineData(" . ")]
[InlineData(". .")]
public void ReturnsError_WhenSafeBasePath_MapsToTheEmptyString(string basePath)
{
// Arrange
var expectedError = $"Base path '{basePath}' must contain non '.' characters.";
var errorMessages = new List<string>();
var buildEngine = new Mock<IBuildEngine>();
buildEngine.Setup(e => e.LogErrorEvent(It.IsAny<BuildErrorEventArgs>()))
.Callback<BuildErrorEventArgs>(args => errorMessages.Add(args.Message));
var task = new GetDefaultStaticWebAssetsBasePath
{
BuildEngine = buildEngine.Object,
BasePath = basePath
};
// Act
var result = task.Execute();
// Assert
Assert.False(result);
var message = Assert.Single(errorMessages);
Assert.Equal(expectedError, message);
}
[Theory]
[InlineData("Identity", "identity")]
[InlineData("Microsoft.AspNetCore.Identity", "microsoftaspnetcoreidentity")]
public void ReturnsSafeBasePath_WhenBasePath_ContainsUnsafeCharacters(string basePath, string expectedSafeBasePath)
{
// Arrange
var task = new GetDefaultStaticWebAssetsBasePath
{
BuildEngine = Mock.Of<IBuildEngine>(),
BasePath = basePath
};
// Act
var result = task.Execute();
// Assert
Assert.True(result);
Assert.Equal(expectedSafeBasePath, task.SafeBasePath);
}
}
}

View File

@ -67,13 +67,13 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
Assert.BuildPassed(result);
Assert.FileExists(result, PublishOutputPath, Path.Combine("wwwroot", "_content", "classlibrary", "js", "project-transitive-dep.js"));
Assert.FileExists(result, PublishOutputPath, Path.Combine("wwwroot", "_content", "classlibrary", "js", "project-transitive-dep.v4.js"));
Assert.FileExists(result, PublishOutputPath, Path.Combine("wwwroot", "_content", "classlibrary2", "css", "site.css"));
Assert.FileExists(result, PublishOutputPath, Path.Combine("wwwroot", "_content", "classlibrary2", "js", "project-direct-dep.js"));
Assert.FileExists(result, PublishOutputPath, Path.Combine("wwwroot", "_content", "packagelibrarydirectdependency", "css", "site.css"));
Assert.FileExists(result, PublishOutputPath, Path.Combine("wwwroot", "_content", "packagelibrarydirectdependency", "js", "pkg-direct-dep.js"));
Assert.FileExists(result, PublishOutputPath, Path.Combine("wwwroot", "_content", "packagelibrarytransitivedependency", "js", "pkg-transitive-dep.js"));
Assert.FileExists(result, PublishOutputPath, Path.Combine("wwwroot", "_content", "ClassLibrary", "js", "project-transitive-dep.js"));
Assert.FileExists(result, PublishOutputPath, Path.Combine("wwwroot", "_content", "ClassLibrary", "js", "project-transitive-dep.v4.js"));
Assert.FileExists(result, PublishOutputPath, Path.Combine("wwwroot", "_content", "ClassLibrary2", "css", "site.css"));
Assert.FileExists(result, PublishOutputPath, Path.Combine("wwwroot", "_content", "ClassLibrary2", "js", "project-direct-dep.js"));
Assert.FileExists(result, PublishOutputPath, Path.Combine("wwwroot", "_content", "PackageLibraryDirectDependency", "css", "site.css"));
Assert.FileExists(result, PublishOutputPath, Path.Combine("wwwroot", "_content", "PackageLibraryDirectDependency", "js", "pkg-direct-dep.js"));
Assert.FileExists(result, PublishOutputPath, Path.Combine("wwwroot", "_content", "PackageLibraryTransitiveDependency", "js", "pkg-transitive-dep.js"));
// Validate that static web assets don't get published as content too on their regular path
Assert.FileDoesNotExist(result, PublishOutputPath, Path.Combine("wwwroot", "js", "project-transitive-dep.js"));
@ -95,13 +95,13 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
Assert.BuildPassed(publish);
Assert.FileExists(publish, PublishOutputPath, Path.Combine("wwwroot", "_content", "classlibrary", "js", "project-transitive-dep.js"));
Assert.FileExists(publish, PublishOutputPath, Path.Combine("wwwroot", "_content", "classlibrary", "js", "project-transitive-dep.v4.js"));
Assert.FileExists(publish, PublishOutputPath, Path.Combine("wwwroot", "_content", "classlibrary2", "css", "site.css"));
Assert.FileExists(publish, PublishOutputPath, Path.Combine("wwwroot", "_content", "classlibrary2", "js", "project-direct-dep.js"));
Assert.FileExists(publish, PublishOutputPath, Path.Combine("wwwroot", "_content", "packagelibrarydirectdependency", "css", "site.css"));
Assert.FileExists(publish, PublishOutputPath, Path.Combine("wwwroot", "_content", "packagelibrarydirectdependency", "js", "pkg-direct-dep.js"));
Assert.FileExists(publish, PublishOutputPath, Path.Combine("wwwroot", "_content", "packagelibrarytransitivedependency", "js", "pkg-transitive-dep.js"));
Assert.FileExists(publish, PublishOutputPath, Path.Combine("wwwroot", "_content", "ClassLibrary", "js", "project-transitive-dep.js"));
Assert.FileExists(publish, PublishOutputPath, Path.Combine("wwwroot", "_content", "ClassLibrary", "js", "project-transitive-dep.v4.js"));
Assert.FileExists(publish, PublishOutputPath, Path.Combine("wwwroot", "_content", "ClassLibrary2", "css", "site.css"));
Assert.FileExists(publish, PublishOutputPath, Path.Combine("wwwroot", "_content", "ClassLibrary2", "js", "project-direct-dep.js"));
Assert.FileExists(publish, PublishOutputPath, Path.Combine("wwwroot", "_content", "PackageLibraryDirectDependency", "css", "site.css"));
Assert.FileExists(publish, PublishOutputPath, Path.Combine("wwwroot", "_content", "PackageLibraryDirectDependency", "js", "pkg-direct-dep.js"));
Assert.FileExists(publish, PublishOutputPath, Path.Combine("wwwroot", "_content", "PackageLibraryTransitiveDependency", "js", "pkg-transitive-dep.js"));
}
@ -255,10 +255,10 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
};
return $@"<StaticWebAssets Version=""1.0"">
<ContentRoot BasePath=""_content/classlibrary"" Path=""{projects[2]}"" />
<ContentRoot BasePath=""_content/classlibrary2"" Path=""{projects[3]}"" />
<ContentRoot BasePath=""_content/packagelibrarydirectdependency"" Path=""{projects[1]}"" />
<ContentRoot BasePath=""_content/packagelibrarytransitivedependency"" Path=""{projects[0]}"" />
<ContentRoot BasePath=""_content/ClassLibrary"" Path=""{projects[2]}"" />
<ContentRoot BasePath=""_content/ClassLibrary2"" Path=""{projects[3]}"" />
<ContentRoot BasePath=""_content/PackageLibraryDirectDependency"" Path=""{projects[1]}"" />
<ContentRoot BasePath=""_content/PackageLibraryTransitiveDependency"" Path=""{projects[0]}"" />
</StaticWebAssets>";
}
}