Files
VirtualBuddy/VirtualUI/Source/VM Configuration/VMConfigurationSheet.swift
Guilherme Rambo 4181d09e17 Guest<>host port and Unix socket forwarding (#824)
* Custom socket tunnels

* Improve tunnel configuration UI

* Tweak tunnel editing UI to be less confusing

* Create tunnel socket parent path if it doesn't already exist

* Security: Harden tunnel approval verification

* Security: harden tunnel approval UI against tampered strings, use only digest for validating approvals

* Security: refuse to start a TCP listener when something already answers at its address; replace existing socket when appropriate

* Fix newly added tunnel that replaces an existing listener being blocked by address in use

* Security: make tunnel status text from the other side safe to display

Status and failure text is chosen by the other side of a tunnel and shown
next to the tunnel in settings. Remove invisible, reordering and
line-breaking characters from it and limit its length.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>

* Security: require a trusted owner for every folder enclosing a tunnel socket

Each component of a listener's path, as written and as resolved through
links, has to belong to this account or to a system account. Set the
socket's permissions without following links.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>

* Security: check who owns a Unix socket tunnel destination

Apply the path ownership check to destinations, and require the service
that accepts the connection to run as this account or a system account.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>

* Security: give each tunnel its own share of a virtual machine's connections

Raise the limit to 64 connections per virtual machine with at most 32 for
one tunnel, so holding one tunnel's connections open can't starve the
others. Pause a listener that runs out of descriptors or memory instead
of stopping it for good.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>

* Security: keep the reason a tunnel failed on the host from the guest

The guest is only told that a tunnel isn't available on the host.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>

* Update service tunnels audit report with the status of F5 to F10

Notes what was fixed for each finding and what was left as is.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>

* Support managed preference restrictions on tunnels

* Remove audit file

* Tunnels UI tweaks

* Rewrite service tunnels documentation

---------

Co-authored-by: Claude Mythos 5.2 <noreply@anthropic.com>
2026-11-25 18:17:16 -03:00

201 lines
7.1 KiB
Swift

//
// VMConfigurationSheet.swift
// VirtualUI
//
// Created by Guilherme Rambo on 18/07/22.
//
import SwiftUI
import VirtualCore
public struct VMConfigurationSheet: View {
public static let minWidth: CGFloat = 520
@EnvironmentObject private var viewModel: VMConfigurationViewModel
/// The VM configuration as it existed when the user opened the configuration UI.
/// Can be used to reset aspects of the configuration to their previous values.
private var initialConfiguration: VBMacConfiguration
/// The configuration that gets saved with the VM.
/// Setting this saves the configuration.
@Binding private var savedConfiguration: VBMacConfiguration
@State private var tunnelSaveError: String?
@State private var showValidationErrors = false
private var showsCancelButton: Bool { viewModel.context == .postInstall }
private var customConfirmationButtonAction: ((VBMacConfiguration) -> Void)? = nil
/// Initializes the VM configuration sheet, bound to a VM configuration model.
/// - Parameter configuration: The binding that will be updated when the user saves the configuration by clicking the "Done" button.
public init(configuration: Binding<VBMacConfiguration>) {
self.init(configuration: configuration, showingValidationErrors: false)
}
init(configuration: Binding<VBMacConfiguration>, showingValidationErrors: Bool = false, customConfirmationButtonAction: ((VBMacConfiguration) -> Void)? = nil) {
self.initialConfiguration = configuration.wrappedValue
self._savedConfiguration = configuration
self._showValidationErrors = .init(wrappedValue: showingValidationErrors)
self.customConfirmationButtonAction = customConfirmationButtonAction
}
@Environment(\.dismiss) private var dismiss
@Environment(\.containerPadding) private var containerPadding
@Environment(\.maxContentWidth) private var maxContentWidth
private var isInstall: Bool { viewModel.context == .preInstall }
public var body: some View {
ScrollView(.vertical) {
VMConfigurationView(initialConfiguration: initialConfiguration)
.environmentObject(viewModel)
.frame(maxWidth: isInstall ? maxContentWidth : nil)
.padding(containerPadding)
.frame(maxWidth: .infinity)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.virtualBuddyVariableBlurBar(edge: .bottom) { buttons }
.alert("Unable to Save Service Tunnels", isPresented: Binding(get: { tunnelSaveError != nil }, set: { if !$0 { tunnelSaveError = nil } })) {
Button("OK") { tunnelSaveError = nil }
} message: { Text(tunnelSaveError ?? "") }
.resizableSheet(minWidth: Self.minWidth, maxWidth: .infinity, minHeight: 500, maxHeight: .infinity)
}
@ViewBuilder
private var buttons: some View {
VStack(alignment: .leading, spacing: 16) {
if showValidationErrors {
validationErrors
}
HStack {
if showsCancelButton {
Button("Cancel") {
dismiss()
}
.keyboardShortcut(.cancelAction)
}
Spacer()
Button(viewModel.context == .preInstall ? "Continue" : "Done") {
validateAndSave()
}
.keyboardShortcut(.defaultAction)
.disabled(showValidationErrors)
}
}
.onChange(of: viewModel.config) { _, newValue in
guard showValidationErrors else { return }
Task {
if await viewModel.validate() == .supported {
showValidationErrors = false
}
}
}
}
@ViewBuilder
private var validationErrors: some View {
if case .unsupported(let errors) = viewModel.supportState {
ForEach(errors, id: \.self) { Text($0) }
.foregroundColor(.red)
}
}
private func validateAndSave() {
showValidationErrors = true
Task {
let state = await viewModel.validate()
guard state.allowsSaving else { return }
if viewModel.context == .postInstall, let reviewed = viewModel.tunnelDraft {
do { try viewModel.vm.approveServiceTunnels(reviewed) }
catch {
showValidationErrors = false
tunnelSaveError = error.localizedDescription
return
}
}
savedConfiguration = viewModel.config
if let customConfirmationButtonAction {
customConfirmationButtonAction(savedConfiguration)
} else {
dismiss()
}
}
}
}
#if DEBUG
struct VMConfigurationSheet_Previews: PreviewProvider {
static var height: Double { 1200 }
static var previews: some View {
_Template(vm: .preview, context: .preInstall)
.previewDisplayName("Pre Install")
_Template(vm: .preview, context: .postInstall)
.previewDisplayName("Post Install")
_Template(vm: .previewLinux, context: .postInstall)
.previewDisplayName("Linux - Post")
_Template(vm: .previewLinux, context: .preInstall)
.previewDisplayName("Linux - Pre")
}
struct _Template: View {
@State var vm: VBVirtualMachine
var context: VMConfigurationContext
@State private var library = VMLibraryController.preview
var body: some View {
Group {
if context == .postInstall {
PreviewSheet {
VMConfigurationSheet(configuration: $vm.configuration)
.environmentObject(VMConfigurationViewModel(vm, context: context))
.frame(width: VMConfigurationSheet.minWidth, height: VMConfigurationSheet_Previews.height, alignment: .top)
}
} else {
VMConfigurationSheet(configuration: $vm.configuration)
.environmentObject(VMConfigurationViewModel(vm, context: context))
.frame(width: VMConfigurationSheet.minWidth, height: VMConfigurationSheet_Previews.height, alignment: .top)
.background(BlurHashFullBleedBackground(blurHash: .virtualBuddyBackground))
}
}
.environment(library.templatesController)
}
}
}
/// Simulates a macOS sheet for SwiftUI previews.
struct PreviewSheet<Content: View>: View {
var content: () -> Content
init(@ViewBuilder _ content: @escaping () -> Content) {
self.content = content
}
var body: some View {
ZStack {}
.frame(width: VMConfigurationSheet.minWidth, height: VMConfigurationSheet_Previews.height)
.padding()
.background(Color.black.opacity(0.5))
.overlay {
content()
.controlGroup()
}
}
}
#endif