Skip to content

Commit 1ada218

Browse files
committed
Remove trusted certificate on Mac
1 parent fe6544c commit 1ada218

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

dev-proxy/CommandHandlers/CertRemoveCommandHandler.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
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 DevProxy.Abstractions;
56
using System.CommandLine;
67
using System.CommandLine.Invocation;
8+
using System.Diagnostics;
9+
using Titanium.Web.Proxy.Helpers;
710

811
namespace DevProxy.CommandHandlers;
912

@@ -30,6 +33,7 @@ public static void RemoveCert(ILogger logger, InvocationContext invocationContex
3033

3134
logger.LogInformation("Uninstalling the root certificate...");
3235

36+
RemoveTrustedCertificateOnMac();
3337
ProxyEngine.ProxyServer.CertificateManager.RemoveTrustedRootCertificate(machineTrusted: false);
3438

3539
logger.LogInformation("DONE");
@@ -65,4 +69,25 @@ private static bool PromptConfirmation(string message, bool defaultValue)
6569
}
6670
}
6771
}
72+
73+
private static void RemoveTrustedCertificateOnMac()
74+
{
75+
if (!RunTime.IsMac)
76+
{
77+
return;
78+
}
79+
80+
var bashScriptPath = Path.Join(ProxyUtils.AppFolder, "remove-cert.sh");
81+
ProcessStartInfo startInfo = new()
82+
{
83+
FileName = "/bin/bash",
84+
Arguments = bashScriptPath,
85+
UseShellExecute = false,
86+
CreateNoWindow = true,
87+
};
88+
89+
var process = new Process() { StartInfo = startInfo };
90+
process.Start();
91+
process.WaitForExit();
92+
}
6893
}

dev-proxy/dev-proxy.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@
6262
<None Update="devproxy-errors.json">
6363
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
6464
</None>
65+
<None Update="remove-cert.sh">
66+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
67+
</None>
6568
<None Update="toggle-proxy.sh">
6669
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
6770
</None>

dev-proxy/remove-cert.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
set -e
3+
4+
if [ "$(uname -s)" != "Darwin" ]; then
5+
echo "Error: this shell script should be run on macOS."
6+
exit 1
7+
fi
8+
9+
echo -e "\nRemove the self-signed certificate from your Keychain."
10+
11+
cert_name="Dev Proxy CA"
12+
cert_filename="dev-proxy-ca.pem"
13+
14+
# export cert from keychain to PEM
15+
echo "Exporting '$cert_name' certificate..."
16+
security find-certificate -c "$cert_name" -a -p > "$cert_filename"
17+
18+
# add trusted cert to keychain
19+
echo "Removing Dev Proxy trust settings..."
20+
security remove-trusted-cert "$cert_filename"
21+
22+
# remove exported cert
23+
echo "Cleaning up..."
24+
rm "$cert_filename"
25+
echo -e "\033[0;32mDONE\033[0m\n"

0 commit comments

Comments
 (0)