// Copyright (c) Tailscale Inc & contributors // SPDX-License-Identifier: BSD-3-Clause package winutil import ( "cmp" "fmt" ) // SideloadingStatus is whether Windows allows installing app packages // from outside the Microsoft Store and, if not, why. type SideloadingStatus string const ( SideloadingEnabled SideloadingStatus = "enabled" SideloadingDisabledByPolicy SideloadingStatus = "disabled-by-policy" SideloadingDisabledByUser SideloadingStatus = "disabled-by-user" SideloadingDisabledByDefault SideloadingStatus = "disabled-by-default" // SideloadingUnsupported means that the Windows build is too old // for the Windows App Runtime, whether or not sideloading is enabled. SideloadingUnsupported SideloadingStatus = "unsupported" ) const ( // winAppRuntimeMinBuild is the oldest Windows build // supported by the Windows App Runtime. winAppRuntimeMinBuild = 17763 // sideloadingDefaultBuild is the first Windows build // that allows sideloading unless it is explicitly disabled. sideloadingDefaultBuild = 19041 ) // SideloadingState is the Windows configuration that determines // whether app packages can be sideloaded. // The Windows App Runtime required by the Windowed UI // cannot be installed unless they can be. // // Each setting is nil if its registry value is not set. // A policy setting takes precedence over the local one. type SideloadingState struct { Build uint32 // Windows build number PolicyAllowAllTrustedApps *uint32 PolicyDevMode *uint32 LocalAllowAllTrustedApps *uint32 LocalDevMode *uint32 } // Status reports whether sideloading is enabled, either on its own // or by Developer Mode, and why it is disabled otherwise. // It reports [SideloadingUnsupported] on Windows builds // that cannot run the Windows App Runtime at all. func (s SideloadingState) Status() SideloadingStatus { if s.Build < winAppRuntimeMinBuild { return SideloadingUnsupported } if devMode := cmp.Or(s.PolicyDevMode, s.LocalDevMode); devMode != nil && *devMode == 1 { return SideloadingEnabled } switch { case s.PolicyAllowAllTrustedApps != nil: if *s.PolicyAllowAllTrustedApps != 1 { return SideloadingDisabledByPolicy } case s.LocalAllowAllTrustedApps != nil: if *s.LocalAllowAllTrustedApps != 1 { return SideloadingDisabledByUser } case s.Build < sideloadingDefaultBuild: return SideloadingDisabledByDefault } return SideloadingEnabled } func (s SideloadingState) String() string { val := func(v *uint32) string { if v == nil { return "unset" } return fmt.Sprint(*v) } return fmt.Sprintf("%s (build=%d policy=%s policyDevMode=%s local=%s localDevMode=%s)", s.Status(), s.Build, val(s.PolicyAllowAllTrustedApps), val(s.PolicyDevMode), val(s.LocalAllowAllTrustedApps), val(s.LocalDevMode)) }