Skip to content

Commit 919a8ce

Browse files
authored
fix(setup): sync Windows CA certificates to WSL (#819)
Sync trusted Windows root certificates into the managed WSL gateway before downloading the OpenClaw CLI, fixing onboarding behind enterprise TLS interception. Install one validated certificate per trust-store file through a root-owned transactional update, rebuild trust on rollback, and ensure command stdin respects setup timeout and cancellation. Co-authored-by: conanssam <[email protected]>
1 parent b37b1be commit 919a8ce

7 files changed

Lines changed: 412 additions & 7 deletions

File tree

‎src/OpenClaw.SetupEngine/CommandRunner.cs‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,25 @@ public async Task<CommandResult> RunAsync(
9393
process.BeginOutputReadLine();
9494
process.BeginErrorReadLine();
9595

96-
if (stdinInput != null)
97-
{
98-
await process.StandardInput.WriteAsync(stdinInput);
99-
process.StandardInput.Close();
100-
}
101-
10296
using var timeoutCts = CancellationTokenSource.CreateLinkedTokenSource(ct);
10397
timeoutCts.CancelAfter(timeout);
10498

10599
try
106100
{
101+
if (stdinInput != null)
102+
{
103+
try
104+
{
105+
await process.StandardInput.WriteAsync(stdinInput.AsMemory(), timeoutCts.Token);
106+
await process.StandardInput.FlushAsync(timeoutCts.Token);
107+
process.StandardInput.Close();
108+
}
109+
catch (IOException) when (process.HasExited)
110+
{
111+
// The child exited before consuming all input. Preserve its exit status/output.
112+
}
113+
}
114+
107115
await process.WaitForExitAsync(timeoutCts.Token);
108116
}
109117
catch (OperationCanceledException) when (!ct.IsCancellationRequested)

‎src/OpenClaw.SetupEngine/OpenClaw.SetupEngine.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11+
<InternalsVisibleTo Include="OpenClaw.SetupEngine.Tests" />
1112
<ProjectReference Include="..\OpenClaw.Connection\OpenClaw.Connection.csproj" />
1213
<ProjectReference Include="..\OpenClaw.Shared\OpenClaw.Shared.csproj" />
1314
</ItemGroup>

‎src/OpenClaw.SetupEngine/SetupPipeline.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public static List<SetupStep> BuildDefaultSteps()
5050
new CreateWslInstanceStep(),
5151
new ConfigureWslInstanceStep(),
5252
new ValidateWslLockdownStep(),
53+
new SyncWindowsCaCertsStep(),
5354
new InstallCliStep(),
5455
new ConfigureGatewayStep(),
5556
new InstallGatewayServiceStep(),

‎src/OpenClaw.SetupEngine/SetupSteps.cs‎

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
using System.Net.Http;
44
using System.Net.Sockets;
55
using System.Runtime.InteropServices;
6+
using System.Security.Cryptography;
7+
using System.Security.Cryptography.X509Certificates;
8+
using System.Text;
69
using System.Text.Json;
710
using OpenClaw.Connection;
811
using OpenClaw.Shared;
@@ -1176,6 +1179,239 @@ private static void ValidateConfValue(Dictionary<string, Dictionary<string, stri
11761179
// GATEWAY INSTALL STEPS
11771180
// ═══════════════════════════════════════════════════════════════════
11781181

1182+
/// <summary>
1183+
/// Exports Windows trusted root CA certificates into the WSL
1184+
/// distro's system trust store. This resolves curl exit-60 failures that
1185+
/// occur on corporate networks where a TLS-intercepting proxy injects a
1186+
/// self-signed certificate that the WSL Ubuntu instance does not trust.
1187+
/// The step is non-fatal: if the Windows store cannot be read or the WSL
1188+
/// trust store cannot be updated, it logs a warning and continues so that
1189+
/// setups on non-proxied networks are not blocked.
1190+
/// </summary>
1191+
public sealed class SyncWindowsCaCertsStep : SetupStep
1192+
{
1193+
private const string ManagedCertDirectory = "/usr/local/share/ca-certificates/openclaw-windows";
1194+
private const string InstallScript = """
1195+
set -euo pipefail
1196+
1197+
target=/usr/local/share/ca-certificates/openclaw-windows
1198+
parent=/usr/local/share/ca-certificates
1199+
backup_parent=/var/lib/openclaw-setup
1200+
backup="$backup_parent/windows-ca-certificates.backup"
1201+
install -d -m 0700 -o root -g root "$backup_parent"
1202+
staging="$(mktemp -d "$parent/.openclaw-windows.XXXXXX")"
1203+
chmod 0755 "$staging"
1204+
old_moved=0
1205+
new_installed=0
1206+
1207+
cleanup() {
1208+
rm -rf -- "$staging"
1209+
if [ "$new_installed" -eq 1 ]; then
1210+
rm -rf -- "$target"
1211+
fi
1212+
if [ "$old_moved" -eq 1 ] && [ -d "$backup" ]; then
1213+
mv -- "$backup" "$target"
1214+
fi
1215+
rm -rf -- "$backup"
1216+
if [ "$old_moved" -eq 1 ] || [ "$new_installed" -eq 1 ]; then
1217+
update-ca-certificates --fresh >/dev/null 2>&1 || true
1218+
fi
1219+
}
1220+
trap cleanup EXIT
1221+
1222+
if [ -d "$backup" ]; then
1223+
rm -rf -- "$target"
1224+
mv -- "$backup" "$target"
1225+
update-ca-certificates --fresh
1226+
fi
1227+
1228+
count=0
1229+
while IFS=$'\t' read -r fingerprint payload; do
1230+
if [ -z "$fingerprint" ] && [ -z "$payload" ]; then
1231+
continue
1232+
fi
1233+
case "$fingerprint" in
1234+
''|*[!0-9A-Fa-f]*) echo "Invalid certificate fingerprint" >&2; exit 64 ;;
1235+
esac
1236+
if [ "${#fingerprint}" -ne 64 ]; then
1237+
echo "Invalid certificate fingerprint length" >&2
1238+
exit 64
1239+
fi
1240+
case "$payload" in
1241+
''|*[!A-Za-z0-9+/=]*) echo "Invalid certificate payload" >&2; exit 64 ;;
1242+
esac
1243+
1244+
fingerprint="${fingerprint,,}"
1245+
cert_path="$staging/$fingerprint.crt"
1246+
{
1247+
printf '%s\n' '-----BEGIN CERTIFICATE-----'
1248+
printf '%s\n' "$payload" | fold -w 64
1249+
printf '%s\n' '-----END CERTIFICATE-----'
1250+
} > "$cert_path"
1251+
chmod 0644 "$cert_path"
1252+
1253+
if ! openssl x509 -in "$cert_path" -noout >/dev/null 2>&1; then
1254+
echo "Invalid X.509 certificate for $fingerprint" >&2
1255+
exit 65
1256+
fi
1257+
actual="$(openssl x509 -in "$cert_path" -outform DER | sha256sum | cut -d ' ' -f1)"
1258+
if [ "$actual" != "$fingerprint" ]; then
1259+
echo "Certificate fingerprint mismatch for $fingerprint" >&2
1260+
exit 65
1261+
fi
1262+
count=$((count + 1))
1263+
done
1264+
1265+
if [ "$count" -eq 0 ]; then
1266+
echo "No certificates received" >&2
1267+
exit 64
1268+
fi
1269+
1270+
if [ -d "$target" ]; then
1271+
mv -- "$target" "$backup"
1272+
old_moved=1
1273+
fi
1274+
mv -- "$staging" "$target"
1275+
new_installed=1
1276+
update-ca-certificates --fresh
1277+
1278+
rm -rf -- "$backup"
1279+
old_moved=0
1280+
new_installed=0
1281+
trap - EXIT
1282+
""";
1283+
private const string RollbackScript = """
1284+
set -e
1285+
target=/usr/local/share/ca-certificates/openclaw-windows
1286+
backup=/var/lib/openclaw-setup/windows-ca-certificates.backup
1287+
if [ -d "$target" ]; then
1288+
rm -rf -- "$target"
1289+
update-ca-certificates --fresh
1290+
fi
1291+
rm -rf -- "$backup"
1292+
""";
1293+
1294+
private readonly Func<WindowsCaCertificateExport> _exportCertificates;
1295+
1296+
public SyncWindowsCaCertsStep() : this(ExportWindowsTrustedRoots) { }
1297+
1298+
internal SyncWindowsCaCertsStep(Func<WindowsCaCertificateExport> exportCertificates)
1299+
=> _exportCertificates = exportCertificates;
1300+
1301+
public override string Id => "sync-ca-certs";
1302+
public override string DisplayName => "Sync Windows CA certificates to WSL";
1303+
public override bool CanRetry => false;
1304+
1305+
public override async Task<StepResult> ExecuteAsync(SetupContext ctx, CancellationToken ct)
1306+
{
1307+
var distro = ctx.DistroName!;
1308+
1309+
WindowsCaCertificateExport export;
1310+
try
1311+
{
1312+
export = _exportCertificates();
1313+
}
1314+
catch (Exception ex)
1315+
{
1316+
ctx.Logger.Warn($"Could not read Windows CA store: {ex.Message} — skipping CA sync");
1317+
return StepResult.Ok("Skipped: could not read Windows CA store");
1318+
}
1319+
1320+
foreach (var warning in export.Warnings)
1321+
ctx.Logger.Warn(warning);
1322+
1323+
if (export.CertificateCount == 0)
1324+
{
1325+
ctx.Logger.Warn("Windows CA store returned no certificates — skipping CA sync");
1326+
return StepResult.Ok("Skipped: Windows CA store is empty");
1327+
}
1328+
1329+
var result = await ctx.Commands.RunAsync(
1330+
WslConstants.WslExePath,
1331+
["-d", distro, "-u", "root", "--", "bash", "-c", NormalizeShellScript(InstallScript)],
1332+
TimeSpan.FromSeconds(60),
1333+
workingDirectory: WslConstants.SafeWindowsWorkingDirectory,
1334+
stdinInput: export.Manifest,
1335+
ct: ct);
1336+
1337+
if (result.ExitCode != 0)
1338+
{
1339+
ctx.Logger.Warn($"update-ca-certificates exited {result.ExitCode}: {result.Stderr.Trim()}");
1340+
return StepResult.Ok($"Skipped: could not update WSL CA certificates (exit {result.ExitCode})");
1341+
}
1342+
1343+
ctx.Logger.Info($"Synced {export.CertificateCount} Windows CA certificates to WSL trust store");
1344+
return StepResult.Ok($"Synced {export.CertificateCount} Windows CA certificates to WSL");
1345+
}
1346+
1347+
public override async Task RollbackAsync(SetupContext ctx, CancellationToken ct)
1348+
{
1349+
var distro = ctx.DistroName!;
1350+
var result = await ctx.Commands.RunAsync(
1351+
WslConstants.WslExePath,
1352+
["-d", distro, "-u", "root", "--", "bash", "-c", NormalizeShellScript(RollbackScript)],
1353+
TimeSpan.FromSeconds(30),
1354+
workingDirectory: WslConstants.SafeWindowsWorkingDirectory,
1355+
ct: ct);
1356+
if (result.ExitCode == 0)
1357+
{
1358+
ctx.Logger.Info($"[Rollback] Removed {ManagedCertDirectory} from WSL trust store");
1359+
return;
1360+
}
1361+
1362+
throw new InvalidOperationException(
1363+
$"Could not remove synced CA certificates (exit {result.ExitCode}): {result.Stderr.Trim()}");
1364+
}
1365+
1366+
internal static WindowsCaCertificateExport BuildCertificateManifest(
1367+
IEnumerable<byte[]> rawCertificates,
1368+
IReadOnlyList<string>? warnings = null)
1369+
{
1370+
var certificates = new SortedDictionary<string, string>(StringComparer.Ordinal);
1371+
foreach (var rawCertificate in rawCertificates)
1372+
{
1373+
var fingerprint = Convert.ToHexString(SHA256.HashData(rawCertificate)).ToLowerInvariant();
1374+
certificates.TryAdd(fingerprint, Convert.ToBase64String(rawCertificate));
1375+
}
1376+
1377+
var manifest = new StringBuilder();
1378+
foreach (var (fingerprint, payload) in certificates)
1379+
manifest.Append(fingerprint).Append('\t').Append(payload).Append('\n');
1380+
1381+
return new WindowsCaCertificateExport(manifest.ToString(), certificates.Count, warnings ?? []);
1382+
}
1383+
1384+
private static string NormalizeShellScript(string script)
1385+
=> script.Replace("\r", "", StringComparison.Ordinal);
1386+
1387+
private static WindowsCaCertificateExport ExportWindowsTrustedRoots()
1388+
{
1389+
var rawCertificates = new List<byte[]>();
1390+
var warnings = new List<string>();
1391+
foreach (var location in new[] { StoreLocation.LocalMachine, StoreLocation.CurrentUser })
1392+
{
1393+
try
1394+
{
1395+
using var store = new X509Store(StoreName.Root, location);
1396+
store.Open(OpenFlags.ReadOnly);
1397+
foreach (var certificate in store.Certificates)
1398+
rawCertificates.Add(certificate.RawData);
1399+
}
1400+
catch (Exception ex) when (ex is CryptographicException or UnauthorizedAccessException)
1401+
{
1402+
warnings.Add($"Could not read Windows {location} Root store: {ex.Message}");
1403+
}
1404+
}
1405+
1406+
return BuildCertificateManifest(rawCertificates, warnings);
1407+
}
1408+
}
1409+
1410+
internal sealed record WindowsCaCertificateExport(
1411+
string Manifest,
1412+
int CertificateCount,
1413+
IReadOnlyList<string> Warnings);
1414+
11791415
public sealed class InstallCliStep : SetupStep
11801416
{
11811417
public override string Id => "install-cli";
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System.Diagnostics;
2+
3+
namespace OpenClaw.SetupEngine.Tests;
4+
5+
public class CommandRunnerTests
6+
{
7+
private static readonly string s_largeStdin = new('x', 8 * 1024 * 1024);
8+
9+
[Fact]
10+
public async Task RunAsync_LargeStdinWriteObeysTimeout()
11+
{
12+
var runner = CreateRunner();
13+
var (executable, arguments) = SleepingCommand();
14+
var stopwatch = Stopwatch.StartNew();
15+
16+
var result = await runner.RunAsync(
17+
executable,
18+
arguments,
19+
TimeSpan.FromMilliseconds(250),
20+
stdinInput: s_largeStdin);
21+
22+
Assert.True(result.TimedOut);
23+
Assert.InRange(stopwatch.Elapsed, TimeSpan.Zero, TimeSpan.FromSeconds(5));
24+
}
25+
26+
[Fact]
27+
public async Task RunAsync_LargeStdinWriteObeysCallerCancellation()
28+
{
29+
var runner = CreateRunner();
30+
var (executable, arguments) = SleepingCommand();
31+
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(250));
32+
var stopwatch = Stopwatch.StartNew();
33+
34+
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => runner.RunAsync(
35+
executable,
36+
arguments,
37+
TimeSpan.FromSeconds(30),
38+
stdinInput: s_largeStdin,
39+
ct: cts.Token));
40+
41+
Assert.InRange(stopwatch.Elapsed, TimeSpan.Zero, TimeSpan.FromSeconds(5));
42+
}
43+
44+
private static CommandRunner CreateRunner()
45+
=> new(new SetupLogger(filePath: null, LogLevel.Trace));
46+
47+
private static (string Executable, string[] Arguments) SleepingCommand()
48+
=> OperatingSystem.IsWindows()
49+
? ("cmd.exe", ["/d", "/s", "/c", "ping 127.0.0.1 -n 30 >nul"])
50+
: ("/bin/sh", ["-c", "sleep 30"]);
51+
}

‎tests/OpenClaw.SetupEngine.Tests/SetupPipelineTests.cs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,15 @@ public void BuildDefaultSteps_IncludesCurrentSetupFlow()
6060
{
6161
var steps = SetupStepFactory.BuildDefaultSteps();
6262

63-
Assert.Equal(18, steps.Count);
63+
Assert.Equal(19, steps.Count);
6464
Assert.IsType<PreflightOsStep>(steps[0]);
6565
Assert.IsType<PreflightWslStep>(steps[1]);
6666
Assert.IsType<CleanupStaleDistroStep>(steps[2]);
6767
Assert.IsType<CleanupStaleGatewayStep>(steps[3]);
6868
Assert.Contains(steps, s => s is ValidateWslLockdownStep);
69+
var caSyncIndex = steps.FindIndex(s => s is SyncWindowsCaCertsStep);
70+
var cliInstallIndex = steps.FindIndex(s => s is InstallCliStep);
71+
Assert.Equal(cliInstallIndex - 1, caSyncIndex);
6972
Assert.Contains(steps, s => s is RunGatewayWizardStep);
7073
Assert.IsType<StartKeepaliveStep>(steps[^1]);
7174
}

0 commit comments

Comments
 (0)