mirror of
https://github.com/goreleaser/nfpm
synced 2026-11-29 08:54:49 +01:00
* chore(deps): bump go-msix to v1.0.1 and go-pkcs12 to v0.7.3 go-msix v1.0.1 carries the Authenticode signing fixes needed for packages signed on Linux to validate on Windows; go-pkcs12 becomes a direct dependency for the signing tests. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015Hnro326ZTAu2EqUTGGkCB * feat(msix): default publisher from the signing certificate or vendor `msix.publisher` is no longer required. When signing is configured it defaults to the certificate subject, which Windows requires to byte-match on signed packages; otherwise it becomes a CN= distinguished name built from the root vendor, falling back to the maintainer name (with RFC 2253 escaping). `publisher_display_name` prefers vendor/maintainer over the package name, `$NFPM_PASSPHRASE` now also feeds the MSIX signer, and the root `scripts` block is reported as ignored instead of silently dropped. The vendor/maintainer fallback lives in internal/maintainer so the MSI packager can share it. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015Hnro326ZTAu2EqUTGGkCB * ci: validate nfpm-signed MSIX on real Windows Build and Authenticode-sign the MSIX on Linux with nfpm's pure-Go signer and a throwaway openssl certificate, then validate the signature with the real Windows trust stack (Get-AuthenticodeSignature, signtool verify) and deploy it with Add-AppxPackage on a Windows runner. The existing signtool-signed job remains as the control, and the install script now also runs the installed executable. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015Hnro326ZTAu2EqUTGGkCB * ci: integrate MSIX signature validation with merge queue checks * fix(msix): derive publishers using Windows DN syntax * fix: preserve address-only maintainer fallback * fix(msix): Preserve signing errors without publisher defaults Return the PFX loading failure before publisher validation when no vendor or maintainer fallback exists. Cover corrupt and missing PFX files and incorrect passphrases with and without fallback metadata. * test(msix): Remove duplicate publisher display name test * fix(msix): Validate publisher defaults and isolate signing attempts Reject unsupported certificate subjects and invalid publisher metadata. Keep derived identities local to each packaging attempt, load signing credentials once, and reject explicit certificate mismatches. Honor explicitly empty MSIX passphrases in the standard environment parsers. Cover signing retries, successful Info reuse, metadata boundaries, passwordless PFX files, and Windows SDK validation with signed positive and negative controls. * test(msix): Cover parser entry points and invalid certificate subjects * fix(msix): Match Windows T61 decoding and validate derived DNs Preserve certificate ASN.1 value tags and apply Windows UTF-8-first decoding for T.61 strings, with the existing 8-bit fallback. Pass vendor values through only when the complete value is a valid Windows DN; otherwise quote them as CN values. Cover raw T.61 subjects in native comparisons and signed packaging with derived and explicit publishers. Isolate the global-passphrase test from empty and nonempty ambient MSIX overrides. * fix(msix): Fall back for invalid derived display names --------- Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
391 lines
12 KiB
Go
391 lines
12 KiB
Go
// Package msix implements nfpm.Packager providing .msix bindings.
|
|
package msix
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"io/fs"
|
|
"log"
|
|
"os"
|
|
"regexp"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
"unicode/utf8"
|
|
|
|
"github.com/goreleaser/nfpm/v2"
|
|
"github.com/goreleaser/nfpm/v2/files"
|
|
"github.com/goreleaser/nfpm/v2/internal/maintainer"
|
|
"go.digitalxero.dev/go-msix"
|
|
)
|
|
|
|
const packagerName = "msix"
|
|
|
|
// nolint: gochecknoinits
|
|
func init() {
|
|
nfpm.RegisterPackager(packagerName, Default)
|
|
}
|
|
|
|
// nolint: gochecknoglobals
|
|
var archToMSIX = map[string]string{
|
|
"amd64": "x64",
|
|
"x86_64": "x64",
|
|
"386": "x86",
|
|
"i386": "x86",
|
|
"i686": "x86",
|
|
"arm64": "arm64",
|
|
"aarch64": "arm64",
|
|
"arm": "arm",
|
|
"arm7": "arm",
|
|
"all": "neutral",
|
|
}
|
|
|
|
func ensureValidArch(info *nfpm.Info) *nfpm.Info {
|
|
if info.MSIX.Arch != "" {
|
|
info.Arch = info.MSIX.Arch
|
|
} else if arch, ok := archToMSIX[info.Arch]; ok {
|
|
info.Arch = arch
|
|
}
|
|
return info
|
|
}
|
|
|
|
// Default msix packager.
|
|
// nolint: gochecknoglobals
|
|
var Default = &MSIX{}
|
|
|
|
// MSIX is an msix packager implementation.
|
|
type MSIX struct{}
|
|
|
|
// ConventionalFileName returns the conventional file name for an MSIX package.
|
|
func (m *MSIX) ConventionalFileName(info *nfpm.Info) string {
|
|
info = ensureValidArch(info)
|
|
version := convertToMSIXVersion(info.Version)
|
|
return fmt.Sprintf("%s_%s_%s.msix", info.Name, version, info.Arch)
|
|
}
|
|
|
|
// ConventionalExtension returns the file extension for MSIX packages.
|
|
func (*MSIX) ConventionalExtension() string {
|
|
return ".msix"
|
|
}
|
|
|
|
const dnKeyPattern = `(CN|L|O|OU|E|C|S|STREET|T|G|I|SN|DC|SERIALNUMBER|Description|PostalCode|POBox|Phone|X21Address|dnQualifier|OID\.(0|[1-9][0-9]*)(\.(0|[1-9][0-9]*))+)`
|
|
|
|
// ST_Publisher_2010_v2 from the Windows foundation manifest schema, with
|
|
// Windows doubled-quote escaping and XML control characters excluded.
|
|
// nolint: gochecknoglobals
|
|
var publisherPattern = regexp.MustCompile(`^` + dnKeyPattern + `=` + dnValuePattern + `(, ` + dnKeyPattern + `=` + dnValuePattern + `)*$`)
|
|
|
|
const dnValuePattern = `([^,+=\x00-\x1f"<>#;]+|"([^"\x00-\x1f]|"")*")`
|
|
|
|
// ensureDN turns a plain name into a publisher distinguished name by prefixing
|
|
// "CN=" and quoting per Windows X.500 syntax; valid Windows DNs pass through.
|
|
func ensureDN(name string) string {
|
|
if validatePublisher(name) == nil {
|
|
return name
|
|
}
|
|
return "CN=" + quoteDNValue(name)
|
|
}
|
|
|
|
// SetPackagerDefaults sets default values for MSIX-specific fields.
|
|
func (*MSIX) SetPackagerDefaults(info *nfpm.Info) {
|
|
needsFullTrust := false
|
|
for i := range info.MSIX.Applications {
|
|
if info.MSIX.Applications[i].EntryPoint == "" {
|
|
info.MSIX.Applications[i].EntryPoint = "Windows.FullTrustApplication"
|
|
}
|
|
if info.MSIX.Applications[i].VisualElements.BackgroundColor == "" {
|
|
info.MSIX.Applications[i].VisualElements.BackgroundColor = "transparent"
|
|
}
|
|
// Default Square150x150Logo and Square44x44Logo to the package Logo
|
|
if info.MSIX.Applications[i].VisualElements.Square150x150Logo == "" && info.MSIX.Properties.Logo != "" {
|
|
info.MSIX.Applications[i].VisualElements.Square150x150Logo = info.MSIX.Properties.Logo
|
|
}
|
|
if info.MSIX.Applications[i].VisualElements.Square44x44Logo == "" && info.MSIX.Properties.Logo != "" {
|
|
info.MSIX.Applications[i].VisualElements.Square44x44Logo = info.MSIX.Properties.Logo
|
|
}
|
|
if info.MSIX.Applications[i].EntryPoint == "Windows.FullTrustApplication" {
|
|
needsFullTrust = true
|
|
}
|
|
}
|
|
|
|
// Auto-add runFullTrust restricted capability when any app uses FullTrustApplication
|
|
if needsFullTrust {
|
|
hasRunFullTrust := false
|
|
for _, c := range info.MSIX.Capabilities.Restricted {
|
|
if c == "runFullTrust" {
|
|
hasRunFullTrust = true
|
|
break
|
|
}
|
|
}
|
|
if !hasRunFullTrust {
|
|
info.MSIX.Capabilities.Restricted = append(info.MSIX.Capabilities.Restricted, "runFullTrust")
|
|
}
|
|
}
|
|
|
|
if len(info.MSIX.Dependencies.TargetDeviceFamilies) == 0 {
|
|
info.MSIX.Dependencies.TargetDeviceFamilies = []nfpm.MSIXTargetDeviceFamily{
|
|
{
|
|
Name: "Windows.Desktop",
|
|
MinVersion: "10.0.17763.0",
|
|
MaxVersionTested: "10.0.22621.0",
|
|
},
|
|
}
|
|
}
|
|
}
|
|
|
|
// Package writes a new MSIX package to the given writer using the given info.
|
|
func (m *MSIX) Package(info *nfpm.Info, w io.Writer) error {
|
|
// Derived identity and application defaults belong to this attempt. A caller
|
|
// may retry with corrected credentials or reuse Info for another certificate.
|
|
packageInfo := *info
|
|
packageInfo.MSIX.Applications = slices.Clone(info.MSIX.Applications)
|
|
packageInfo.MSIX.Capabilities.Restricted = slices.Clone(info.MSIX.Capabilities.Restricted)
|
|
info = &packageInfo
|
|
m.SetPackagerDefaults(info)
|
|
info = ensureValidArch(info)
|
|
|
|
if err := nfpm.PrepareForPackager(info, packagerName); err != nil {
|
|
return err
|
|
}
|
|
|
|
if info.Scripts != (nfpm.Scripts{}) {
|
|
log.Printf("warning: msix is declarative and does not support install scripts, ignoring scripts")
|
|
}
|
|
|
|
// Load once so the derived identity and signature use the same certificate.
|
|
var signing msix.Signing
|
|
if info.MSIX.Signature.PFXFile != "" {
|
|
var err error
|
|
signing, err = loadSigning(info)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else if info.MSIX.Publisher == "" {
|
|
if v := maintainer.VendorOrMaintainer(info.Vendor, info.Maintainer); v != "" {
|
|
info.MSIX.Publisher = ensureDN(v)
|
|
}
|
|
}
|
|
|
|
if err := validate(info); err != nil {
|
|
return err
|
|
}
|
|
|
|
builder := msix.NewBuilder().
|
|
WithIdentity(msix.NewIdentity().
|
|
WithName(info.Name).
|
|
WithVersion(convertToMSIXVersion(info.Version)).
|
|
WithPublisher(info.MSIX.Publisher).
|
|
WithProcessorArchitecture(info.Arch).
|
|
WithResourceID(info.MSIX.Identity.ResourceID).
|
|
Build()).
|
|
WithProperties(buildProperties(info)).
|
|
WithDependencies(buildDependencies(info)).
|
|
WithCapabilities(buildCapabilities(info)).
|
|
AddResource(msix.NewResource().WithLanguage("en-us").Build())
|
|
|
|
for _, app := range buildApplications(info) {
|
|
builder.AddApplication(app)
|
|
}
|
|
|
|
addContents(builder, info)
|
|
|
|
if signing != nil {
|
|
builder.WithSigning(signing)
|
|
}
|
|
|
|
return builder.Build(context.Background(), w)
|
|
}
|
|
|
|
func validate(info *nfpm.Info) error {
|
|
if info.MSIX.Publisher == "" {
|
|
return fmt.Errorf("package msix.publisher, vendor, or maintainer must be provided")
|
|
}
|
|
if err := validatePublisher(info.MSIX.Publisher); err != nil {
|
|
return err
|
|
}
|
|
if name := publisherDisplayName(info); !validDisplayName(name) {
|
|
return fmt.Errorf("package msix.properties.publisher_display_name must be a display name of 1 to 256 characters or an ms-resource reference, without line breaks or leading/trailing whitespace")
|
|
}
|
|
if info.MSIX.Properties.Logo == "" {
|
|
return fmt.Errorf("package %s must be provided", "msix.properties.logo")
|
|
}
|
|
if len(info.MSIX.Applications) == 0 {
|
|
return fmt.Errorf("package %s must be provided", "msix.applications")
|
|
}
|
|
for i, app := range info.MSIX.Applications {
|
|
if app.ID == "" {
|
|
return fmt.Errorf("package %s must be provided", fmt.Sprintf("msix.applications[%d].id", i))
|
|
}
|
|
if app.Executable == "" {
|
|
return fmt.Errorf("package %s must be provided", fmt.Sprintf("msix.applications[%d].executable", i))
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func validDisplayName(name string) bool {
|
|
if name == "" || strings.TrimSpace(name) != name || strings.ContainsAny(name, "\r\n") || !validXMLText(name) {
|
|
return false
|
|
}
|
|
// Resource references have a separate 256-character limit after the prefix.
|
|
name = strings.TrimPrefix(name, "ms-resource:")
|
|
return utf8.RuneCountInString(name) <= 256
|
|
}
|
|
|
|
func buildProperties(info *nfpm.Info) msix.Properties {
|
|
displayName := info.MSIX.Properties.DisplayName
|
|
if displayName == "" {
|
|
displayName = info.Name
|
|
}
|
|
return msix.NewProperties().
|
|
WithDisplayName(displayName).
|
|
WithPublisherDisplayName(publisherDisplayName(info)).
|
|
WithLogo(info.MSIX.Properties.Logo).
|
|
WithDescription(info.Description).
|
|
Build()
|
|
}
|
|
|
|
func publisherDisplayName(info *nfpm.Info) string {
|
|
if name := info.MSIX.Properties.PublisherDisplayName; name != "" {
|
|
return name
|
|
}
|
|
if name := maintainer.VendorOrMaintainer(info.Vendor, info.Maintainer); validDisplayName(name) {
|
|
return name
|
|
}
|
|
return info.Name
|
|
}
|
|
|
|
func buildDependencies(info *nfpm.Info) msix.Dependencies {
|
|
deps := msix.NewDependencies()
|
|
for _, f := range info.MSIX.Dependencies.TargetDeviceFamilies {
|
|
deps.AddTargetDeviceFamily(f.Name, f.MinVersion, f.MaxVersionTested)
|
|
}
|
|
return deps.Build()
|
|
}
|
|
|
|
func buildApplications(info *nfpm.Info) []msix.Application {
|
|
apps := make([]msix.Application, 0, len(info.MSIX.Applications))
|
|
for _, app := range info.MSIX.Applications {
|
|
displayName := app.VisualElements.DisplayName
|
|
if displayName == "" {
|
|
displayName = info.Name
|
|
}
|
|
description := app.VisualElements.Description
|
|
if description == "" {
|
|
description = info.Description
|
|
}
|
|
ve := msix.NewVisualElements().
|
|
WithDisplayName(displayName).
|
|
WithDescription(description).
|
|
WithBackgroundColor(app.VisualElements.BackgroundColor).
|
|
WithSquare150x150Logo(app.VisualElements.Square150x150Logo).
|
|
WithSquare44x44Logo(app.VisualElements.Square44x44Logo).
|
|
Build()
|
|
apps = append(apps, msix.NewApplication().
|
|
WithID(app.ID).
|
|
WithExecutable(app.Executable).
|
|
WithEntryPoint(app.EntryPoint).
|
|
WithVisualElements(ve).
|
|
Build())
|
|
}
|
|
return apps
|
|
}
|
|
|
|
func buildCapabilities(info *nfpm.Info) msix.Capabilities {
|
|
caps := msix.NewCapabilities()
|
|
for _, c := range info.MSIX.Capabilities.Capabilities {
|
|
caps.AddCapability(c)
|
|
}
|
|
for _, c := range info.MSIX.Capabilities.DeviceCapabilities {
|
|
caps.AddDeviceCapability(msix.NewDeviceCapability().WithName(c).Build())
|
|
}
|
|
for _, c := range info.MSIX.Capabilities.Restricted {
|
|
caps.AddRestricted(c)
|
|
}
|
|
return caps.Build()
|
|
}
|
|
|
|
func addContents(builder msix.Builder, info *nfpm.Info) {
|
|
for _, content := range info.Contents {
|
|
switch content.Type {
|
|
case files.TypeDir:
|
|
// Directories are implicit in MSIX — skip
|
|
continue
|
|
case files.TypeSymlink:
|
|
log.Printf("warning: msix does not support symlinks, skipping %s", content.Destination)
|
|
continue
|
|
default:
|
|
// Treat everything else (TypeFile, TypeConfig, etc.) as regular files.
|
|
// AddFile defers errors to Build, so they surface from builder.Build.
|
|
dest := normalizePathForMSIX(content.Destination)
|
|
if content.Source != "" {
|
|
builder.AddFile(dest, content.Source)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func loadSigning(info *nfpm.Info) (msix.Signing, error) {
|
|
pfxPath := info.MSIX.Signature.PFXFile
|
|
if _, err := os.Stat(pfxPath); err != nil {
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
return nil, &nfpm.ErrSigningFailure{Err: fmt.Errorf("PFX file not found: %w", err)}
|
|
}
|
|
return nil, &nfpm.ErrSigningFailure{Err: fmt.Errorf("unable to access PFX file: %w", err)}
|
|
}
|
|
|
|
cert, key, chain, err := msix.LoadPFX(pfxPath, info.MSIX.Signature.KeyPassphrase)
|
|
if err != nil {
|
|
return nil, &nfpm.ErrSigningFailure{Err: fmt.Errorf("loading PFX: %w", err)}
|
|
}
|
|
publisher, err := certificatePublisher(cert.RawSubject)
|
|
if err != nil {
|
|
return nil, &nfpm.ErrSigningFailure{Err: fmt.Errorf("format signing certificate publisher: %w", err)}
|
|
}
|
|
if info.MSIX.Publisher != "" && info.MSIX.Publisher != publisher {
|
|
return nil, &nfpm.ErrSigningFailure{Err: fmt.Errorf("msix.publisher does not match the signing certificate subject; omit publisher to derive it from the certificate")}
|
|
}
|
|
info.MSIX.Publisher = publisher
|
|
|
|
return msix.NewSigning().
|
|
WithCertificate(cert).
|
|
WithPrivateKey(key).
|
|
WithCertChain(chain...).
|
|
Build(), nil
|
|
}
|
|
|
|
// normalizePathForMSIX converts Unix-style paths to Windows-style package paths.
|
|
func normalizePathForMSIX(path string) string {
|
|
// Remove leading slash
|
|
path = strings.TrimPrefix(path, "/")
|
|
// Convert forward slashes — go-msix normalizes internally but be explicit
|
|
return path
|
|
}
|
|
|
|
// convertToMSIXVersion converts a semver-style version to MSIX's 4-part format.
|
|
// MSIX requires Major.Minor.Build.Revision format.
|
|
func convertToMSIXVersion(version string) string {
|
|
version = strings.TrimPrefix(version, "v")
|
|
|
|
// Split on dots
|
|
parts := strings.SplitN(version, ".", 4)
|
|
|
|
// Ensure all parts are valid numbers, default to 0
|
|
result := make([]string, 4)
|
|
for i := range 4 {
|
|
if i < len(parts) {
|
|
if _, err := strconv.Atoi(parts[i]); err == nil {
|
|
result[i] = parts[i]
|
|
} else {
|
|
result[i] = "0"
|
|
}
|
|
} else {
|
|
result[i] = "0"
|
|
}
|
|
}
|
|
|
|
return strings.Join(result, ".")
|
|
}
|