Skip to content

Commit 0fef20d

Browse files
committed
fix: Add prompt confirmation to remove certificates and --force option to bypass
1 parent 9900992 commit 0fef20d

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

dev-proxy/CommandHandlers/CertRemoveCommandHandler.cs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,67 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System.CommandLine;
6+
using System.CommandLine.Invocation;
7+
58
namespace DevProxy.CommandHandlers;
69

710
public static class CertRemoveCommandHandler
811
{
9-
public static void RemoveCert(ILogger logger)
12+
public static void RemoveCert(ILogger logger, InvocationContext invocationContext, Option<bool> forceOption)
1013
{
1114
logger.LogTrace("RemoveCert() called");
15+
ArgumentNullException.ThrowIfNull(logger);
16+
ArgumentNullException.ThrowIfNull(invocationContext);
17+
ArgumentNullException.ThrowIfNull(forceOption);
1218

1319
try
1420
{
21+
var isForced = invocationContext.ParseResult.GetValueForOption(forceOption);
22+
if (!isForced)
23+
{
24+
var isConfirmed = PromptConfirmation("Do you want to remove the root certificate", defaultValue: false);
25+
if (!isConfirmed)
26+
{
27+
return;
28+
}
29+
}
30+
1531
logger.LogInformation("Uninstalling the root certificate...");
32+
1633
ProxyEngine.ProxyServer.CertificateManager.RemoveTrustedRootCertificate(machineTrusted: false);
34+
1735
logger.LogInformation("DONE");
1836
}
1937
catch (Exception ex)
2038
{
2139
logger.LogError(ex, "Error removing certificate");
2240
}
41+
finally
42+
{
43+
logger.LogTrace("RemoveCert() finished");
44+
}
45+
}
2346

24-
logger.LogTrace("RemoveCert() finished");
47+
private static bool PromptConfirmation(string message, bool defaultValue)
48+
{
49+
while (true)
50+
{
51+
Console.Write(message + $" ({(defaultValue ? "Y/n" : "y/N")}): ");
52+
var answer = Console.ReadLine();
53+
54+
if (string.IsNullOrWhiteSpace(answer))
55+
{
56+
return defaultValue;
57+
}
58+
else if (answer.StartsWith("y", StringComparison.OrdinalIgnoreCase))
59+
{
60+
return true;
61+
}
62+
else if (answer.StartsWith("n", StringComparison.OrdinalIgnoreCase))
63+
{
64+
return false;
65+
}
66+
}
2567
}
2668
}

dev-proxy/ProxyHost.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,13 @@ private static Command CreateCertEnsureCommand(ILogger logger)
399399

400400
private static Command CreateCertRemoveCommand(ILogger logger)
401401
{
402+
var forceOption = new Option<bool>("--force", "Force the root certificate removal");
403+
forceOption.AddAlias("-f");
404+
402405
var certRemoveCommand = new Command("remove", "Remove the certificate from Root Store");
403-
certRemoveCommand.SetHandler(() => CertRemoveCommandHandler.RemoveCert(logger));
406+
certRemoveCommand.SetHandler((context) => CertRemoveCommandHandler.RemoveCert(logger, context, forceOption));
407+
408+
certRemoveCommand.AddOptions(new[] { forceOption }.OrderByName());
404409
return certRemoveCommand;
405410
}
406411

0 commit comments

Comments
 (0)