From fe79fd64271f6349e9f09bccdaefc3dcd2ca9a31 Mon Sep 17 00:00:00 2001 From: lzy98276 Date: Sat, 21 Nov 2026 21:30:51 +0800 Subject: [PATCH 1/6] feat: add verified integration SDK channel --- .github/workflows/build_ios.yml | 3 + .github/workflows/build_publish.yml | 3 + .github/workflows/codeQL.yml | 3 + .../Linkage/Resources.en-US.resx | 9 + .../Linkage/Resources.ja-JP.resx | 9 + .../SettingsPages/Linkage/Resources.resx | 9 + SecRandom/SecRandom.csproj | 14 + .../Integration/IntegrationChannelService.cs | 556 ++++++++++++++++++ .../Integration/IntegrationContracts.cs | 68 +++ .../Linkage/LinkageSettingsPage.axaml | 30 + .../Linkage/LinkageSettingsPage.axaml.cs | 65 ++ 11 files changed, 769 insertions(+) create mode 100644 SecRandom/Services/Integration/IntegrationChannelService.cs create mode 100644 SecRandom/Services/Integration/IntegrationContracts.cs diff --git a/.github/workflows/build_ios.yml b/.github/workflows/build_ios.yml index d0539e474..51813a8c6 100644 --- a/.github/workflows/build_ios.yml +++ b/.github/workflows/build_ios.yml @@ -25,6 +25,9 @@ on: permissions: contents: read +env: + SECRANDOM_INTEGRATION_PUBLIC_KEY_SPKI_BASE64: ${{ vars.SECRANDOM_INTEGRATION_PUBLIC_KEY_SPKI_BASE64 }} + jobs: build_ios: name: Build_iOS diff --git a/.github/workflows/build_publish.yml b/.github/workflows/build_publish.yml index 3ee1449a7..7258930d1 100644 --- a/.github/workflows/build_publish.yml +++ b/.github/workflows/build_publish.yml @@ -14,6 +14,9 @@ on: permissions: contents: read +env: + SECRANDOM_INTEGRATION_PUBLIC_KEY_SPKI_BASE64: ${{ vars.SECRANDOM_INTEGRATION_PUBLIC_KEY_SPKI_BASE64 }} + jobs: # Release publication is gated on metadata.yaml: clients resolve a channel to a release tag there # and reject the release manifest when its tag/channel disagree, so a stale channel entry diff --git a/.github/workflows/codeQL.yml b/.github/workflows/codeQL.yml index 40a107950..73a82f3b0 100644 --- a/.github/workflows/codeQL.yml +++ b/.github/workflows/codeQL.yml @@ -6,6 +6,9 @@ on: schedule: - cron: "0 3 * * 1" +env: + SECRANDOM_INTEGRATION_PUBLIC_KEY_SPKI_BASE64: ${{ vars.SECRANDOM_INTEGRATION_PUBLIC_KEY_SPKI_BASE64 }} + jobs: analyze: name: CodeQL 分析 diff --git a/SecRandom/Langs/SettingsPages/Linkage/Resources.en-US.resx b/SecRandom/Langs/SettingsPages/Linkage/Resources.en-US.resx index 8355b66ea..f7f1a6fdd 100644 --- a/SecRandom/Langs/SettingsPages/Linkage/Resources.en-US.resx +++ b/SecRandom/Langs/SettingsPages/Linkage/Resources.en-US.resx @@ -157,4 +157,13 @@ ClassIsland has not loaded today's timetable ClassIsland returned an unsupported current state: {0} Unable to read the ClassIsland class state + SDK notification channels + Approved applications with an official signature can receive draw results + Remove + Save notification channel + Official SecRandom Integration SDK application “{0}” ({1}) was found. Allow it to receive draw results and save it as a notification channel + No saved SDK notification channels + Remove notification channel + SecRandom will stop sending draw results to “{0}” + Notification channel removed diff --git a/SecRandom/Langs/SettingsPages/Linkage/Resources.ja-JP.resx b/SecRandom/Langs/SettingsPages/Linkage/Resources.ja-JP.resx index e42079a3f..79f0ebef0 100644 --- a/SecRandom/Langs/SettingsPages/Linkage/Resources.ja-JP.resx +++ b/SecRandom/Langs/SettingsPages/Linkage/Resources.ja-JP.resx @@ -157,4 +157,13 @@ ClassIsland が当日の時間割を読み込んでいません ClassIsland から未対応の現在状態が返されました: {0} ClassIsland の授業状態を読み取れません + SDK 通知チャネル + 公式署名を検証して承認したアプリに抽選結果を送信します + 削除 + 通知チャネルを保存 + 公式 SecRandom Integration SDK アプリ「{0}」({1})が見つかりました。抽選結果の受信を許可し、通知チャネルとして保存する + 保存済みの SDK 通知チャネルはありません + 通知チャネルを削除 + 「{0}」への抽選結果の送信を停止します + 通知チャネルを削除しました diff --git a/SecRandom/Langs/SettingsPages/Linkage/Resources.resx b/SecRandom/Langs/SettingsPages/Linkage/Resources.resx index 13f1595f3..2947b5087 100644 --- a/SecRandom/Langs/SettingsPages/Linkage/Resources.resx +++ b/SecRandom/Langs/SettingsPages/Linkage/Resources.resx @@ -172,4 +172,13 @@ ClassIsland 尚未加载当天课程表 ClassIsland 返回了未支持的当前状态:{0} 无法读取 ClassIsland 的课程状态 + SDK 通知渠道 + 经官方签名验证并获你批准的应用可以接收抽取结果 + 删除 + 保存通知渠道 + 发现官方 SecRandom 集成 SDK 应用“{0}”({1})。是否允许它接收本体抽取结果并保存为通知渠道 + 暂无已保存的 SDK 通知渠道 + 删除通知渠道 + 删除“{0}”后,本体不再向它发送抽取结果 + 通知渠道已删除 diff --git a/SecRandom/SecRandom.csproj b/SecRandom/SecRandom.csproj index b95129f73..0de9d08f2 100644 --- a/SecRandom/SecRandom.csproj +++ b/SecRandom/SecRandom.csproj @@ -6,6 +6,7 @@ true true True + $(SECRANDOM_INTEGRATION_PUBLIC_KEY_SPKI_BASE64) $(NoWarn);AVLN3001 @@ -569,4 +570,17 @@ + + + + <_IntegrationTrustMetadataPath>$(IntermediateOutputPath)IntegrationTrustMetadata.g.cs + + + + + + diff --git a/SecRandom/Services/Integration/IntegrationChannelService.cs b/SecRandom/Services/Integration/IntegrationChannelService.cs new file mode 100644 index 000000000..ecb7a6533 --- /dev/null +++ b/SecRandom/Services/Integration/IntegrationChannelService.cs @@ -0,0 +1,556 @@ +using System.Diagnostics; +using System.IO.Pipes; +using System.Reflection; +using System.Security.Cryptography; +using System.Text; +using System.Text.Json; +using Avalonia.Threading; +using FluentAvalonia.UI.Controls; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using SecRandom.Core.Abstraction.Services.Messaging; +using SecRandom.Shared.Models.Profile; +using SecRandom.Core.Abstraction.Services.Presentation; +using SecRandom.Shared; + +namespace SecRandom.Services.Integration; + +/// +/// Official SDK discovery and result delivery. The service owns only local IPC and persistence; +/// UI approval is raised as an event so the settings shell can show the confirmation dialog. +/// +public sealed class IntegrationChannelService( + IPluginEventBus eventBus, + ILogger logger) : IHostedService, IDisposable +{ + private const string OfficialPackageId = "SecRandom.Integration.Client"; + private const string AppId = "secrandom.app"; + private static readonly UTF8Encoding StrictUtf8 = new(false, true); + private readonly object _gate = new(); + private readonly string _directory = Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SecRandom", "integrations"); + private readonly string _channelsPath = Utils.GetFilePath("config", "integration-channels.json"); + private readonly string _instanceId = Guid.NewGuid().ToString("N"); + private readonly Dictionary _channels = new(StringComparer.Ordinal); + private readonly HashSet _seenInstances = new(StringComparer.Ordinal); + private readonly Dictionary _leases = new(StringComparer.Ordinal); + private readonly CancellationTokenSource _cts = new(); + private IDisposable? _drawSubscription; + private Task? _serverTask; + private Task? _discoveryTask; + private string? _hostManifestPath; + + public string HostPipeName { get; } = "SecRandom_Integration_Host_" + Guid.NewGuid().ToString("N"); + public event Action? Changed; + public event Action? ChannelDiscovered; + public IReadOnlyList Channels + { + get { lock (_gate) return _channels.Values.OrderBy(x => x.DisplayName, StringComparer.CurrentCultureIgnoreCase).ToArray(); } + } + + public Task StartAsync(CancellationToken cancellationToken) + { + Directory.CreateDirectory(_directory); + LoadChannels(); + WriteHostManifest(); + _drawSubscription = eventBus.Subscribe(OnDrawCompleted); + _serverTask = Task.Run(() => HostPipeLoopAsync(_cts.Token), CancellationToken.None); + _discoveryTask = Task.Run(() => DiscoveryLoopAsync(_cts.Token), CancellationToken.None); + return Task.CompletedTask; + } + + public async Task StopAsync(CancellationToken cancellationToken) + { + _cts.Cancel(); + if (_serverTask is not null) + { + try { await _serverTask.WaitAsync(TimeSpan.FromSeconds(3), cancellationToken).ConfigureAwait(false); } + catch (Exception exception) when (exception is OperationCanceledException or TimeoutException) { } + } + if (_discoveryTask is not null) + { + try { await _discoveryTask.WaitAsync(TimeSpan.FromSeconds(3), cancellationToken).ConfigureAwait(false); } + catch (Exception exception) when (exception is OperationCanceledException or TimeoutException) { } + } + _drawSubscription?.Dispose(); + _drawSubscription = null; + if (_hostManifestPath is not null) + { + try { File.Delete(_hostManifestPath); } + catch (IOException) { } + catch (UnauthorizedAccessException) { } + } + ReleaseAllLeases(); + } + + public void Dispose() => _cts.Dispose(); + + public void DiscoverNow() + { + var changed = false; + foreach (var manifest in ReadManifests()) + { + if (!VerifyOfficialManifest(manifest)) + continue; + + lock (_gate) + { + if (_channels.TryGetValue(manifest.AppId, out var approved)) + { + changed |= approved.ProcessId != manifest.ProcessId + || !string.Equals(approved.InstanceId, manifest.InstanceId, StringComparison.Ordinal) + || !string.Equals(approved.Endpoint, manifest.Endpoint, StringComparison.Ordinal) + || !string.Equals(approved.DisplayName, manifest.DisplayName, StringComparison.Ordinal) + || !string.Equals(approved.SdkVersion, manifest.SdkVersion, StringComparison.Ordinal); + _channels[manifest.AppId] = approved with + { + Endpoint = manifest.Endpoint, + ProcessId = manifest.ProcessId, + InstanceId = manifest.InstanceId, + SdkVersion = manifest.SdkVersion, + LastSeen = DateTimeOffset.UtcNow + }; + continue; + } + + if (!_seenInstances.Add(manifest.AppId + "\n" + manifest.InstanceId)) + continue; + } + + var channel = new IntegrationChannel(manifest.AppId, manifest.DisplayName, manifest.SdkVersion, + manifest.Endpoint, manifest.ProcessId, manifest.InstanceId, DateTimeOffset.UtcNow, DateTimeOffset.UtcNow); + ChannelDiscovered?.Invoke(channel); + QueueApprovalPrompt(channel); + } + if (changed) + { + SaveChannels(); + Changed?.Invoke(); + } + } + + public void Approve(IntegrationChannel channel) + { + lock (_gate) + _channels[channel.AppId] = channel; + SaveChannels(); + Changed?.Invoke(); + } + + public bool Remove(string appId) + { + bool removed; + lock (_gate) removed = _channels.Remove(appId); + if (removed) + { + SaveChannels(); + Changed?.Invoke(); + } + return removed; + } + + private async Task DiscoveryLoopAsync(CancellationToken cancellationToken) + { + while (!cancellationToken.IsCancellationRequested) + { + try + { + DiscoverNow(); + RemoveDeadLeases(); + await Task.Delay(TimeSpan.FromSeconds(2), cancellationToken).ConfigureAwait(false); + } + catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) { break; } + catch (Exception exception) { logger.LogDebug(exception, "Integration discovery failed."); } + } + } + + private async Task HostPipeLoopAsync(CancellationToken cancellationToken) + { + while (!cancellationToken.IsCancellationRequested) + { + await using var server = new NamedPipeServerStream(HostPipeName, PipeDirection.InOut, 8, + PipeTransmissionMode.Byte, PipeOptions.Asynchronous | PipeOptions.CurrentUserOnly); + try + { + await server.WaitForConnectionAsync(cancellationToken).ConfigureAwait(false); + await HandleHostRequestAsync(server, cancellationToken).ConfigureAwait(false); + } + catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) { break; } + catch (IOException) { } + catch (Exception exception) { logger.LogDebug(exception, "Integration host pipe failed."); } + } + } + + private async Task HandleHostRequestAsync(NamedPipeServerStream server, CancellationToken cancellationToken) + { + using var reader = new StreamReader(server, StrictUtf8, false, 4096, true); + await using var writer = new StreamWriter(server, StrictUtf8, 4096, true); + var line = await reader.ReadLineAsync(cancellationToken).ConfigureAwait(false); + if (line is null || Encoding.UTF8.GetByteCount(line) > IntegrationProtocol.MaxFrameBytes) + { + await WriteResponseAsync(writer, null, null, new IntegrationError("invalid_request", "Invalid frame.")).ConfigureAwait(false); + return; + } + + IntegrationFrame? request; + try { request = JsonSerializer.Deserialize(line, IntegrationJson.Options); } + catch (JsonException) { request = null; } + if (request is null || request.Version != IntegrationProtocol.Version || request.Type != "rpc") + { + await WriteResponseAsync(writer, request?.Id, null, new IntegrationError("invalid_request", "Invalid request.")).ConfigureAwait(false); + return; + } + + if (request.Method is "system.ping") + { + await WriteResponseAsync(writer, request.Id, new { pong = true }, null).ConfigureAwait(false); + return; + } + + if (request.Method is "linkage.acquire" or "linkage.release" + && request.Params is { ValueKind: JsonValueKind.Object } parameters) + { + var linkage = parameters.Deserialize(IntegrationJson.Options); + if (linkage is null || string.IsNullOrWhiteSpace(linkage.LeaseId)) + { + await WriteResponseAsync(writer, request.Id, null, + new IntegrationError("invalid_request", "A linkage lease is required.")).ConfigureAwait(false); + return; + } + + if (request.Method == "linkage.acquire") + { + if (!IsApprovedRequester(linkage) || !IsPipeClientProcess(server, linkage.ProcessId)) + { + await WriteResponseAsync(writer, request.Id, null, + new IntegrationError("authorization_denied", "The SDK channel is not approved.")).ConfigureAwait(false); + return; + } + + lock (_gate) _leases[linkage.LeaseId] = linkage; + App.AcquireIntegrationLinkage(linkage.LeaseId); + } + else + { + if (!IsOwnedLease(linkage) || !IsPipeClientProcess(server, linkage.ProcessId)) + { + await WriteResponseAsync(writer, request.Id, null, + new IntegrationError("authorization_denied", "The linkage lease does not belong to this process.")).ConfigureAwait(false); + return; + } + lock (_gate) _leases.Remove(linkage.LeaseId); + App.ReleaseIntegrationLinkage(linkage.LeaseId); + } + + await WriteResponseAsync(writer, request.Id, new { accepted = true }, null).ConfigureAwait(false); + return; + } + + await WriteResponseAsync(writer, request.Id, null, + new IntegrationError("unsupported_method", "The method is not supported.")).ConfigureAwait(false); + } + + private void OnDrawCompleted(HostEvents.DrawCompleted completed) => _ = ForwardDrawCompletedAsync(completed); + + private async Task ForwardDrawCompletedAsync(HostEvents.DrawCompleted completed) + { + IntegrationChannel[] channels; + lock (_gate) channels = _channels.Values.ToArray(); + var members = completed.Channel == DrawPresentationChannel.Lottery + ? completed.Prizes.Select(prize => new { id = prize.RecordId.ToString("N"), name = prize.Name, + gender = (string?)null, group = (string?)null, enabled = prize.Exists, weight = prize.Weight }).ToArray() + : completed.Students.Select(student => new { id = student.RecordId.ToString("N"), name = student.Name, + gender = (string?)student.Gender, group = (string?)student.Group, enabled = student.Exists, weight = 1d }).ToArray(); + var result = new + { + round_id = completed.RoundId, + roster_id = completed.Channel == DrawPresentationChannel.Lottery ? completed.PrizeListName : completed.ListName, + mode = (int?)null, + members, + at = new DateTimeOffset(completed.DrawTime).ToUniversalTime(), + idempotency_key = completed.RoundId, + source_app_id = AppId, + roster_kind = completed.Channel == DrawPresentationChannel.Lottery ? 1 : 0 + }; + + foreach (var channel in channels) + { + try + { + var manifest = ReadManifests().FirstOrDefault(x => + string.Equals(x.AppId, channel.AppId, StringComparison.Ordinal) + && x.ProcessId == channel.ProcessId + && string.Equals(x.InstanceId, channel.InstanceId, StringComparison.Ordinal) + && string.Equals(x.Endpoint, channel.Endpoint, StringComparison.Ordinal)); + if (manifest is null || !VerifyOfficialManifest(manifest)) + continue; + await SendResultAsync(manifest.Endpoint, manifest.ProcessId, result).ConfigureAwait(false); + } + catch (Exception exception) { logger.LogDebug(exception, "Integration result delivery failed for {AppId}.", channel.AppId); } + } + } + + private static async Task SendResultAsync(string pipeName, int expectedProcessId, object result) + { + using var pipe = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut, + PipeOptions.Asynchronous | PipeOptions.CurrentUserOnly); + await pipe.ConnectAsync(2000).ConfigureAwait(false); + if (!TryGetPipePeerProcessId(pipe.SafePipeHandle.DangerousGetHandle(), localIsServer: false, + out var serverProcessId) + || serverProcessId != (uint)expectedProcessId) + throw new IOException("The SDK notification pipe belongs to a different process."); + using var writer = new StreamWriter(pipe, StrictUtf8, 4096, true); + using var reader = new StreamReader(pipe, StrictUtf8, false, 4096, true); + var request = new IntegrationFrame(IntegrationProtocol.Version, "rpc", Guid.NewGuid().ToString("N"), + "notification.result", JsonSerializer.SerializeToElement(result, IntegrationJson.Options)); + await writer.WriteLineAsync(JsonSerializer.Serialize(request, IntegrationJson.Options)).ConfigureAwait(false); + await writer.FlushAsync().ConfigureAwait(false); + _ = await reader.ReadLineAsync().ConfigureAwait(false); + } + + private void RemoveDeadLeases() + { + string[] dead; + lock (_gate) dead = _leases.Values.Where(x => !IsProcessAlive(x.ProcessId)).Select(x => x.LeaseId).ToArray(); + foreach (var leaseId in dead) + { + lock (_gate) _leases.Remove(leaseId); + App.ReleaseIntegrationLinkage(leaseId); + } + } + + private void ReleaseAllLeases() + { + string[] leases; + lock (_gate) { leases = _leases.Keys.ToArray(); _leases.Clear(); } + foreach (var lease in leases) App.ReleaseIntegrationLinkage(lease); + } + + private IReadOnlyList ReadManifests() + { + if (!Directory.Exists(_directory)) return []; + var manifests = new List(); + foreach (var file in Directory.EnumerateFiles(_directory, "*.json")) + { + if (string.Equals(Path.GetFileName(file), "host.json", StringComparison.OrdinalIgnoreCase)) continue; + try + { + var manifest = JsonSerializer.Deserialize(File.ReadAllText(file), IntegrationJson.Options); + if (manifest is not null) manifests.Add(manifest); + } + catch (Exception exception) when (exception is IOException or JsonException) { } + } + return manifests; + } + + private bool VerifyOfficialManifest(IntegrationManifest manifest) + { + if (manifest.Protocol != IntegrationProtocol.Name || !IsValidEndpoint(manifest.Endpoint) + || !IsProcessAlive(manifest.ProcessId) || string.IsNullOrWhiteSpace(manifest.PublisherProof)) + return false; + try + { + var proof = JsonSerializer.Deserialize(manifest.PublisherProof, IntegrationJson.Options); + if (proof is null || proof.PackageId != OfficialPackageId || proof.SdkVersion != manifest.SdkVersion) + return false; + var key = typeof(IntegrationChannelService).Assembly.GetCustomAttributes() + .FirstOrDefault(x => x.Key == "SecRandomIntegrationPublicKey")?.Value; + if (!VerifyProof(proof, key)) return false; + using var process = Process.GetProcessById(manifest.ProcessId); + foreach (ProcessModule module in process.Modules) + { + try + { + if (Convert.ToHexString(SHA256.HashData(File.ReadAllBytes(module.FileName))).Equals( + proof.AssemblySha256, StringComparison.OrdinalIgnoreCase)) return true; + } + catch (IOException) { } + catch (UnauthorizedAccessException) { } + } + } + catch (Exception exception) when (exception is JsonException or FormatException or CryptographicException + or InvalidOperationException or NotSupportedException + or System.ComponentModel.Win32Exception) + { + logger.LogDebug(exception, "SDK manifest verification failed for {AppId}.", manifest.AppId); + } + return false; + } + + private static bool VerifyProof(OfficialSdkProof proof, string? publicKey) + { + if (string.IsNullOrWhiteSpace(publicKey) || proof.AssemblySha256.Length != 64 || + proof.AssemblySha256.Any(x => !Uri.IsHexDigit(x))) return false; + var canonical = $"secrandom.integration-sdk/v1\n{proof.PackageId}\n{proof.SdkVersion}\n{proof.AssemblySha256.ToLowerInvariant()}"; + try + { + using var ecdsa = ECDsa.Create(); + ecdsa.ImportSubjectPublicKeyInfo(Convert.FromBase64String(publicKey), out _); + return ecdsa.VerifyData(Encoding.UTF8.GetBytes(canonical), Convert.FromBase64String(proof.Signature), HashAlgorithmName.SHA256); + } + catch (FormatException) { return false; } + catch (CryptographicException) { return false; } + } + + private void LoadChannels() + { + try + { + var loaded = File.Exists(_channelsPath) + ? JsonSerializer.Deserialize>(File.ReadAllText(_channelsPath), IntegrationJson.Options) + : null; + if (loaded is null) return; + lock (_gate) foreach (var channel in loaded.Where(x => !string.IsNullOrWhiteSpace(x.AppId))) _channels[channel.AppId] = channel; + } + catch (Exception exception) when (exception is IOException or JsonException) { logger.LogWarning(exception, "Unable to load integration channels."); } + } + + private void SaveChannels() + { + try + { + Directory.CreateDirectory(Path.GetDirectoryName(_channelsPath)!); + IntegrationChannel[] channels; + lock (_gate) channels = _channels.Values.ToArray(); + var temporary = _channelsPath + ".tmp"; + File.WriteAllText(temporary, JsonSerializer.Serialize(channels, IntegrationJson.Options)); + File.Move(temporary, _channelsPath, true); + } + catch (IOException exception) { logger.LogWarning(exception, "Unable to save integration channels."); } + } + + private void WriteHostManifest() + { + var manifest = new { app_id = AppId, endpoint = HostPipeName, pid = Environment.ProcessId, + instance_id = _instanceId, protocol = IntegrationProtocol.Name }; + _hostManifestPath = Path.Combine(_directory, "host.json"); + var temporary = _hostManifestPath + ".tmp"; + File.WriteAllText(temporary, JsonSerializer.Serialize(manifest, IntegrationJson.Options)); + File.Move(temporary, _hostManifestPath, true); + } + + private static bool IsProcessAlive(int processId) + { + try { return Process.GetProcessById(processId) is { HasExited: false }; } + catch (ArgumentException) { return false; } + catch (InvalidOperationException) { return false; } + } + + private bool IsApprovedRequester(LinkageLeaseRequest request) + { + lock (_gate) + return _channels.TryGetValue(request.AppId, out var channel) + && channel.ProcessId == request.ProcessId + && string.Equals(channel.InstanceId, request.InstanceId, StringComparison.Ordinal) + && DateTimeOffset.UtcNow - channel.LastSeen < TimeSpan.FromSeconds(8) + && IsProcessAlive(request.ProcessId); + } + + private bool IsOwnedLease(LinkageLeaseRequest request) + { + lock (_gate) + return _leases.TryGetValue(request.LeaseId, out var lease) + && lease.ProcessId == request.ProcessId + && string.Equals(lease.AppId, request.AppId, StringComparison.Ordinal) + && string.Equals(lease.InstanceId, request.InstanceId, StringComparison.Ordinal); + } + + private static bool IsPipeClientProcess(NamedPipeServerStream server, int expectedProcessId) + { + return TryGetPipePeerProcessId(server.SafePipeHandle.DangerousGetHandle(), localIsServer: true, + out var processId) + && processId == (uint)expectedProcessId; + } + + private static bool TryGetPipePeerProcessId(IntPtr pipeHandle, bool localIsServer, out uint processId) + { + if (OperatingSystem.IsWindows()) + return localIsServer + ? GetNamedPipeClientProcessId(pipeHandle, out processId) + : GetNamedPipeServerProcessId(pipeHandle, out processId); + + if (OperatingSystem.IsLinux()) + { + var length = (uint)System.Runtime.InteropServices.Marshal.SizeOf(); + UnixPeerCredentials credentials; + if (GetSocketOption(pipeHandle.ToInt32(), 1, 17, out credentials, ref length) == 0 + && credentials.ProcessId > 0) + { + processId = (uint)credentials.ProcessId; + return true; + } + } + else if (OperatingSystem.IsMacOS()) + { + var length = (uint)sizeof(int); + int peerProcessId; + if (GetSocketOption(pipeHandle.ToInt32(), 0, 2, out peerProcessId, ref length) == 0 + && peerProcessId > 0) + { + processId = (uint)peerProcessId; + return true; + } + } + + processId = 0; + return false; + } + + [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)] + private struct UnixPeerCredentials + { + public int ProcessId; + public uint UserId; + public uint GroupId; + } + + private void QueueApprovalPrompt(IntegrationChannel channel) + { + Dispatcher.UIThread.Post(() => _ = PromptForChannelAsync(channel)); + } + + private async Task PromptForChannelAsync(IntegrationChannel channel) + { + try + { + var accepted = await Dispatcher.UIThread.InvokeAsync( + () => App.Current.ConfirmIntegrationChannelAsync(channel)); + if (accepted is true) + Approve(channel); + else if (accepted is null) + lock (_gate) _seenInstances.Remove(channel.AppId + "\n" + channel.InstanceId); + } + catch (Exception exception) + { + lock (_gate) _seenInstances.Remove(channel.AppId + "\n" + channel.InstanceId); + logger.LogDebug(exception, "Integration channel confirmation was unavailable."); + } + } + + [System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)] + [return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)] + private static extern bool GetNamedPipeClientProcessId(IntPtr pipe, out uint clientProcessId); + + [System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)] + [return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)] + private static extern bool GetNamedPipeServerProcessId(IntPtr pipe, out uint serverProcessId); + + [System.Runtime.InteropServices.DllImport("libc", EntryPoint = "getsockopt", SetLastError = true)] + private static extern int GetSocketOption(int socket, int level, int option, + out UnixPeerCredentials value, ref uint optionLength); + + [System.Runtime.InteropServices.DllImport("libc", EntryPoint = "getsockopt", SetLastError = true)] + private static extern int GetSocketOption(int socket, int level, int option, + out int value, ref uint optionLength); + + private static bool IsValidEndpoint(string endpoint) => endpoint.StartsWith("SecRandom_Integration_", StringComparison.Ordinal) + && endpoint.Length <= 96 + && endpoint.All(character => char.IsAsciiLetterOrDigit(character) || character == '_'); + + private static async Task WriteResponseAsync(StreamWriter writer, string? id, object? result, IntegrationError? error) + { + var response = new IntegrationFrame(IntegrationProtocol.Version, "rpc", id, null, null, result, error); + await writer.WriteLineAsync(JsonSerializer.Serialize(response, IntegrationJson.Options)).ConfigureAwait(false); + await writer.FlushAsync().ConfigureAwait(false); + } +} diff --git a/SecRandom/Services/Integration/IntegrationContracts.cs b/SecRandom/Services/Integration/IntegrationContracts.cs new file mode 100644 index 000000000..1853116c9 --- /dev/null +++ b/SecRandom/Services/Integration/IntegrationContracts.cs @@ -0,0 +1,68 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace SecRandom.Services.Integration; + +internal static class IntegrationProtocol +{ + public const int Version = 1; + public const int MaxFrameBytes = 256 * 1024; + public const string Name = "secrandom.integration.v1"; +} + +internal sealed record IntegrationManifest( + [property: JsonPropertyName("app_id")] string AppId, + [property: JsonPropertyName("display_name")] string DisplayName, + [property: JsonPropertyName("sdk_version")] string SdkVersion, + [property: JsonPropertyName("protocol")] string Protocol, + [property: JsonPropertyName("endpoint")] string Endpoint, + [property: JsonPropertyName("pid")] int ProcessId, + [property: JsonPropertyName("instance_id")] string InstanceId, + [property: JsonPropertyName("capabilities")] IReadOnlyList Capabilities, + [property: JsonPropertyName("published_at")] DateTimeOffset PublishedAt, + [property: JsonPropertyName("publisher_proof")] string? PublisherProof); + +internal sealed record OfficialSdkProof( + [property: JsonPropertyName("package_id")] string PackageId, + [property: JsonPropertyName("sdk_version")] string SdkVersion, + [property: JsonPropertyName("assembly_sha256")] string AssemblySha256, + [property: JsonPropertyName("signature")] string Signature); + +internal sealed record IntegrationFrame( + [property: JsonPropertyName("version")] int Version, + [property: JsonPropertyName("type")] string Type, + [property: JsonPropertyName("id")] string? Id, + [property: JsonPropertyName("method")] string? Method, + [property: JsonPropertyName("params")] JsonElement? Params, + [property: JsonPropertyName("result")] object? Result = null, + [property: JsonPropertyName("error")] IntegrationError? Error = null); + +internal sealed record IntegrationError( + [property: JsonPropertyName("code")] string Code, + [property: JsonPropertyName("message")] string Message, + [property: JsonPropertyName("retryable")] bool Retryable = false); + +internal sealed record LinkageLeaseRequest( + [property: JsonPropertyName("app_id")] string AppId, + [property: JsonPropertyName("process_id")] int ProcessId, + [property: JsonPropertyName("instance_id")] string InstanceId, + [property: JsonPropertyName("lease_id")] string LeaseId); + +internal static class IntegrationJson +{ + public static readonly JsonSerializerOptions Options = new(JsonSerializerDefaults.Web) + { + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull + }; +} + +public sealed record IntegrationChannel( + string AppId, + string DisplayName, + string SdkVersion, + string Endpoint, + int ProcessId, + string InstanceId, + DateTimeOffset ApprovedAt, + DateTimeOffset LastSeen); + diff --git a/SecRandom/Views/SettingsPages/Linkage/LinkageSettingsPage.axaml b/SecRandom/Views/SettingsPages/Linkage/LinkageSettingsPage.axaml index 74058e570..9d392840c 100644 --- a/SecRandom/Views/SettingsPages/Linkage/LinkageSettingsPage.axaml +++ b/SecRandom/Views/SettingsPages/Linkage/LinkageSettingsPage.axaml @@ -171,6 +171,36 @@ + + + + + + + + + + + + + + +