Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update LanguageServerHost to use NamedPipe #69816

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
using System.Collections.Immutable;
using System.CommandLine;
using System.Diagnostics;
using System.IO.Pipes;
using System.Runtime.InteropServices;
using Microsoft.CodeAnalysis.Contracts.Telemetry;
using Microsoft.CodeAnalysis.LanguageServer;
using Microsoft.CodeAnalysis.LanguageServer.BrokeredServices;
using Microsoft.CodeAnalysis.LanguageServer.BrokeredServices.Services.HelloWorld;
using Microsoft.CodeAnalysis.LanguageServer.HostWorkspace;
using Microsoft.CodeAnalysis.LanguageServer.LanguageServer;
using Microsoft.CodeAnalysis.LanguageServer.Logging;
Expand Down Expand Up @@ -44,7 +44,7 @@ static async Task RunAsync(ServerConfiguration serverConfiguration, Cancellation
LoggerFactory.Create(builder =>
{
builder.SetMinimumLevel(serverConfiguration.MinimumLogLevel);
builder.AddConsole(options => options.LogToStandardErrorThreshold = LogLevel.Trace);
builder.AddConsole();
// The console logger outputs control characters on unix for colors which don't render correctly in VSCode.
builder.AddSimpleConsole(formatterOptions => formatterOptions.ColorBehavior = LoggerColorBehavior.Disabled);
})
Expand Down Expand Up @@ -100,7 +100,11 @@ static async Task RunAsync(ServerConfiguration serverConfiguration, Cancellation
// TODO: Remove, the path should match exactly. Workaround for https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1830914.
Microsoft.CodeAnalysis.EditAndContinue.EditAndContinueMethodDebugInfoReader.IgnoreCaseWhenComparingDocumentNames = Path.DirectorySeparatorChar == '\\';

var server = new LanguageServerHost(Console.OpenStandardInput(), Console.OpenStandardOutput(), exportProvider, loggerFactory.CreateLogger(nameof(LanguageServerHost)));
// Named pipe server is actually created from the client, so here we create a client stream.
beccamc marked this conversation as resolved.
Show resolved Hide resolved
var pipeServer = new NamedPipeClientStream(serverName: ".", serverConfiguration.PipeName, PipeDirection.InOut, PipeOptions.Asynchronous);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dibarbet PipeOptions.CurrentUserOnly isn't working. It connects but doesn't send any messages. I could investigate further (I don't know if the issue is between .net and nodjs?) but I don't know if it's worth the time. I don't think this is less secure than the stdin/out was? Thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if the client is not creating a pipe that applies to the current user.

Honestly I'm not super familiar with named pipes - but it seems like a lower privileged user could hijack the pipe name and then get access to files from the server that they normally couldn't get. I'm not sure it would be as easy to hijack stdin/out. But maybe it doesn't matter if the pipe is created in the user's tmp dir?

But I don't really know much about named pipes. Not sure @jaredpar might have additional insight?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is less secure than the stdin/out was?

You can't get a handle to another processes stdin/out, but depending on how the pipe is created, this could be. This is a pretty common source of security bugs in the Linux world, especially since /tmp is world-accessible. The potential attack here is imagine you have two users on a machine, one launches VS Code. The other malicious user sees the pipe get created, and connects to it prior to our LSP connecting to it. In that case, you're now able to send LSP messages to the other user's client, which could be potentially bad/broken. (Imagine getting didOpen for files you can't otherwise read, or sending LSP messages back which executes stuff on the VS Code side...)

It's entirely possible we're OK here if the pipe creation API is already doing the right thing, but we'd need to validate that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also don't see a way to configure access on the nodejs side - https://nodejs.org/api/net.html#netcreateserveroptions-connectionlistener

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found it here - https://nodejs.org/api/net.html#serverlistenoptions-callback - looks like the default should be current user, so that looks ok (unless vscode is launching as a different user)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where do you see the default as current user?

Copy link
Member

@dibarbet dibarbet Sep 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Under server.listen options (from https://nodejs.org/api/net.html#serverlistenoptions-callback ), e.g.
image

await pipeServer.ConnectAsync(cancellationToken);

var server = new LanguageServerHost(pipeServer, pipeServer, exportProvider, loggerFactory.CreateLogger(nameof(LanguageServerHost)));
server.Start();

try
Expand Down Expand Up @@ -171,6 +175,12 @@ static CliRootCommand CreateCommandLineParser()
Required = false
};

var pipeNameOption = new CliOption<string>("--pipe")
{
Description = "The name of the pipe to use for RPC with the parent process.",
Required = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we still allow for stdin/stdout for a bit longer, if only so that way we don't have to merge/flow this simultaneously with the VS Code side of things?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

David and I discussed this, and determined it would be fine to merge both PRs close together. The only risk is that someone else updates roslyn version before I get PR in, correct?

};

var rootCommand = new CliRootCommand()
{
debugOption,
Expand All @@ -181,7 +191,8 @@ static CliRootCommand CreateCommandLineParser()
sessionIdOption,
sharedDependenciesOption,
extensionAssemblyPathsOption,
extensionLogDirectoryOption
extensionLogDirectoryOption,
pipeNameOption
};
rootCommand.SetAction((parseResult, cancellationToken) =>
{
Expand All @@ -193,6 +204,7 @@ static CliRootCommand CreateCommandLineParser()
var sharedDependenciesPath = parseResult.GetValue(sharedDependenciesOption);
var extensionAssemblyPaths = parseResult.GetValue(extensionAssemblyPathsOption) ?? Array.Empty<string>();
var extensionLogDirectory = parseResult.GetValue(extensionLogDirectoryOption)!;
var pipe = parseResult.GetValue(pipeNameOption)!;

var serverConfiguration = new ServerConfiguration(
LaunchDebugger: launchDebugger,
Expand All @@ -202,7 +214,8 @@ static CliRootCommand CreateCommandLineParser()
SessionId: sessionId,
SharedDependenciesPath: sharedDependenciesPath,
ExtensionAssemblyPaths: extensionAssemblyPaths,
ExtensionLogDirectory: extensionLogDirectory);
ExtensionLogDirectory: extensionLogDirectory,
PipeName: pipe);

return RunAsync(serverConfiguration, cancellationToken);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@ internal record class ServerConfiguration(
string? SessionId,
string? SharedDependenciesPath,
IEnumerable<string> ExtensionAssemblyPaths,
string ExtensionLogDirectory);
string ExtensionLogDirectory,
string PipeName);
Loading