Allow build server to run on net46

This commit is contained in:
Ajay Bhargav Baaskaran 2018-05-02 10:38:26 -07:00
parent b2c59700d5
commit 41c0a69c5b
5 changed files with 57 additions and 9 deletions

View File

@ -2,7 +2,7 @@
// 'node' indicates to Jenkins that the enclosed block runs on a node that matches // 'node' indicates to Jenkins that the enclosed block runs on a node that matches
// the label 'windows-with-vs' // the label 'windows-with-vs'
simpleNode('Windows_NT','latest') { simpleNode('Windows.10.Enterprise.RS3.ASPNET') {
stage ('Checking out source') { stage ('Checking out source') {
checkout scm checkout scm
} }

View File

@ -124,14 +124,16 @@ namespace Microsoft.AspNetCore.Razor.Tasks
string commandLineCommands, string commandLineCommands,
out int result) out int result)
{ {
#if !NET46
if (!SuppressCurrentUserOnlyPipeOptions && !Enum.IsDefined(typeof(PipeOptions), PipeOptionCurrentUserOnly)) if (!SuppressCurrentUserOnlyPipeOptions && !Enum.IsDefined(typeof(PipeOptions), PipeOptionCurrentUserOnly))
{ {
// For security reasons, we don't want to spin up a server that doesn't // For security reasons, we don't want to spin up a server that doesn't
// restrict requests only to the current user. // restrict requests only to the current user.
result = -1; result = -1;
return false; return ForceServer;
} }
#endif
Log.LogMessage(StandardOutputLoggingImportance, "Server execution started."); Log.LogMessage(StandardOutputLoggingImportance, "Server execution started.");
using (_razorServerCts = new CancellationTokenSource()) using (_razorServerCts = new CancellationTokenSource())

View File

@ -4,6 +4,10 @@
using System; using System;
using System.IO; using System.IO;
using System.IO.Pipes; using System.IO.Pipes;
#if NET46
using System.Security.AccessControl;
using System.Security.Principal;
#endif
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -67,9 +71,14 @@ namespace Microsoft.AspNetCore.Razor.Tools
ServerLogger.Log("Named pipe '{0}' connected", pipeName); ServerLogger.Log("Named pipe '{0}' connected", pipeName);
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
// The original code in Roslyn checks that the server pipe is owned by the same user for security. #if NET46
// We plan to rely on the BCL for this but it's not yet implemented: // Verify that we own the pipe.
// See https://github.com/dotnet/corefx/issues/25427 if (!CheckPipeConnectionOwnership(stream))
{
ServerLogger.Log("Owner of named pipe is incorrect");
return null;
}
#endif
return new NamedPipeClient(stream, GetNextIdentifier()); return new NamedPipeClient(stream, GetNextIdentifier());
} }
@ -80,6 +89,44 @@ namespace Microsoft.AspNetCore.Razor.Tools
} }
} }
#if NET46
/// <summary>
/// Check to ensure that the named pipe server we connected to is owned by the same
/// user.
/// </summary>
private static bool CheckPipeConnectionOwnership(NamedPipeClientStream pipeStream)
{
try
{
if (PlatformInformation.IsWindows)
{
using (var currentIdentity = WindowsIdentity.GetCurrent())
{
var currentOwner = currentIdentity.Owner;
var remotePipeSecurity = GetPipeSecurity(pipeStream);
var remoteOwner = remotePipeSecurity.GetOwner(typeof(SecurityIdentifier));
return currentOwner.Equals(remoteOwner);
}
}
// We don't need to verify on non-windows as that will be taken care of by the
// PipeOptions.CurrentUserOnly flag.
return false;
}
catch (Exception ex)
{
ServerLogger.LogException(ex, "Checking pipe connection");
return false;
}
}
private static ObjectSecurity GetPipeSecurity(PipeStream pipeStream)
{
return pipeStream.GetAccessControl();
}
#endif
private static PipeOptions GetPipeOptions() private static PipeOptions GetPipeOptions()
{ {
var options = PipeOptions.Asynchronous; var options = PipeOptions.Asynchronous;

View File

@ -25,9 +25,8 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
public Task Build_SimpleMvc_UsingDotnetMSBuildAndWithoutBuildServer_CanBuildSuccessfully() public Task Build_SimpleMvc_UsingDotnetMSBuildAndWithoutBuildServer_CanBuildSuccessfully()
=> Build_SimpleMvc_WithoutBuildServer_CanBuildSuccessfully(MSBuildProcessKind.Dotnet); => Build_SimpleMvc_WithoutBuildServer_CanBuildSuccessfully(MSBuildProcessKind.Dotnet);
[ConditionalFact(Skip = "https://github.com/aspnet/Razor/issues/2208")] [ConditionalFact]
[OSSkipCondition(OperatingSystems.Linux)] [OSSkipCondition(OperatingSystems.Linux | OperatingSystems.MacOSX)]
[OSSkipCondition(OperatingSystems.MacOSX)]
[InitializeTestProject("SimpleMvc")] [InitializeTestProject("SimpleMvc")]
public Task Build_SimpleMvc_UsingDesktopMSBuildAndWithoutBuildServer_CanBuildSuccessfully() public Task Build_SimpleMvc_UsingDesktopMSBuildAndWithoutBuildServer_CanBuildSuccessfully()
=> Build_SimpleMvc_WithoutBuildServer_CanBuildSuccessfully(MSBuildProcessKind.Desktop); => Build_SimpleMvc_WithoutBuildServer_CanBuildSuccessfully(MSBuildProcessKind.Desktop);

View File

@ -30,7 +30,7 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
public Task Build_SimpleMvc_WithServer_UsingDotnetMSBuild_CanBuildSuccessfully() public Task Build_SimpleMvc_WithServer_UsingDotnetMSBuild_CanBuildSuccessfully()
=> Build_SimpleMvc_CanBuildSuccessfully(MSBuildProcessKind.Dotnet); => Build_SimpleMvc_CanBuildSuccessfully(MSBuildProcessKind.Dotnet);
[ConditionalFact(Skip = "https://github.com/aspnet/Razor/issues/2208")] [ConditionalFact]
[OSSkipCondition(OperatingSystems.Linux | OperatingSystems.MacOSX)] [OSSkipCondition(OperatingSystems.Linux | OperatingSystems.MacOSX)]
[InitializeTestProject("SimpleMvc")] [InitializeTestProject("SimpleMvc")]
public Task Build_SimpleMvc_WithServer_UsingDesktopMSBuild_CanBuildSuccessfully() public Task Build_SimpleMvc_WithServer_UsingDesktopMSBuild_CanBuildSuccessfully()