Skip to content

Commit efd915d

Browse files
committed
Added current user to VidCoderAutomation pipe name, which should prevent CLI actions from acting on another logged in user's VidCoder instance.
1 parent ecf01c2 commit efd915d

3 files changed

Lines changed: 34 additions & 5 deletions

File tree

VidCoder/Automation/AutomationHost.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
using System;
1+
using PipeMethodCalls;
2+
using PipeMethodCalls.NetJson;
3+
using System;
24
using System.Collections.Generic;
35
using System.Diagnostics;
46
using System.Linq;
57
using System.Text;
68
using System.Threading;
79
using System.Threading.Tasks;
8-
using PipeMethodCalls;
9-
using PipeMethodCalls.NetJson;
1010
using VidCoderCommon;
1111
using VidCoderCommon.Services;
12+
using VidCoderCommon.Utilities;
1213

1314
namespace VidCoder.Automation;
1415

@@ -25,9 +26,10 @@ public static async void StartListening()
2526
{
2627
while (true)
2728
{
29+
string pipeName = "VidCoderAutomation" + (CommonUtilities.Beta ? "Beta" : string.Empty) + PipeUtilities.UserPipeSuffix;
2830
using (server = new PipeServer<IVidCoderAutomation>(
2931
new NetJsonPipeSerializer(),
30-
"VidCoderAutomation" + (CommonUtilities.Beta ? "Beta" : string.Empty),
32+
pipeName,
3133
() => new VidCoderAutomation()))
3234
{
3335
await server.WaitForConnectionAsync().ConfigureAwait(false);

VidCoderCommon/Services/AutomationClient.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Text;
88
using System.Threading.Tasks;
99
using VidCoderCommon.Model;
10+
using VidCoderCommon.Utilities;
1011

1112
namespace VidCoderCommon.Services;
1213

@@ -38,7 +39,8 @@ public async Task<AutomationResult> TryActionAsync(Expression<Action<IVidCoderAu
3839
betaString = "Beta";
3940
}
4041

41-
using (var client = new PipeClient<IVidCoderAutomation>(new NetJsonPipeSerializer(), "VidCoderAutomation" + betaString))
42+
string pipeName = "VidCoderAutomation" + betaString + PipeUtilities.UserPipeSuffix;
43+
using (var client = new PipeClient<IVidCoderAutomation>(new NetJsonPipeSerializer(), pipeName))
4244
{
4345
await client.ConnectAsync().ConfigureAwait(false);
4446
await client.InvokeAsync(action).ConfigureAwait(false);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace VidCoderCommon.Utilities;
7+
8+
public static class PipeUtilities
9+
{
10+
/// <summary>
11+
/// To prevent cross-contamination of pipes between users, we append the username to the pipe name.
12+
/// We sanitize the username to ensure it only contains valid characters for a pipe name.
13+
/// </summary>
14+
public static string UserPipeSuffix => SanitizePipeSegment(Environment.UserName);
15+
16+
private static string SanitizePipeSegment(string s)
17+
{
18+
if (string.IsNullOrEmpty(s))
19+
{
20+
return string.Empty;
21+
}
22+
23+
return new string(s.Select(c => (char.IsLetterOrDigit(c) || c == '_' || c == '-' || c == '.') ? c : '_').ToArray());
24+
}
25+
}

0 commit comments

Comments
 (0)