Files
nfpm/msix/msix_test.go
Dj GilcreaseandClaude Fable 5.1 62f72a6783 feat(msix): default publisher from the signing certificate or vendor (#1135)
* 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>
2026-11-12 01:15:55 +00:00

420 lines
12 KiB
Go

package msix
import (
"archive/zip"
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/xml"
"io"
"log"
"math/big"
"os"
"path/filepath"
"testing"
"time"
"github.com/goreleaser/nfpm/v2"
"github.com/goreleaser/nfpm/v2/files"
"github.com/stretchr/testify/require"
pkcs12 "software.sslmate.com/src/go-pkcs12"
)
func exampleInfo() *nfpm.Info {
return nfpm.WithDefaults(&nfpm.Info{
Name: "MyCompany.TestApp",
Arch: "amd64",
Description: "Test application",
Version: "v1.0.0",
Maintainer: "Test <test@example.com>",
Vendor: "TestCo",
Homepage: "https://example.com",
Overridables: nfpm.Overridables{
Contents: []*files.Content{
{
Source: "../testdata/fake",
Destination: "/app/fake.exe",
},
{
Source: "../testdata/whatever.conf",
Destination: "/app/config.conf",
},
},
MSIX: nfpm.MSIX{
Publisher: "CN=TestCompany, O=TestCompany, C=US",
Properties: nfpm.MSIXProperties{
Logo: "app/fake.exe",
},
Applications: []nfpm.MSIXApplication{
{
ID: "App",
Executable: "app/fake.exe",
EntryPoint: "Windows.FullTrustApplication",
},
},
},
},
})
}
func TestConventionalExtension(t *testing.T) {
require.Equal(t, ".msix", Default.ConventionalExtension())
}
func TestConventionalFileName(t *testing.T) {
info := exampleInfo()
name := Default.ConventionalFileName(info)
require.Equal(t, "MyCompany.TestApp_1.0.0.0_x64.msix", name)
}
func TestArchMapping(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"amd64", "x64"},
{"x86_64", "x64"},
{"386", "x86"},
{"i386", "x86"},
{"i686", "x86"},
{"arm64", "arm64"},
{"aarch64", "arm64"},
{"arm", "arm"},
{"arm7", "arm"},
{"all", "neutral"},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
info := exampleInfo()
info.Arch = tt.input
info.MSIX.Arch = ""
info = ensureValidArch(info)
require.Equal(t, tt.expected, info.Arch)
})
}
}
func TestArchOverride(t *testing.T) {
info := exampleInfo()
info.Arch = "amd64"
info.MSIX.Arch = "x86"
info = ensureValidArch(info)
require.Equal(t, "x86", info.Arch)
}
func TestVersionConversion(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"1.2.3", "1.2.3.0"},
{"v1.2.3", "1.2.3.0"},
{"1.0.0", "1.0.0.0"},
{"2.5", "2.5.0.0"},
{"1", "1.0.0.0"},
{"1.2.3.4", "1.2.3.4"},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
result := convertToMSIXVersion(tt.input)
require.Equal(t, tt.expected, result)
})
}
}
func TestPackageMinimal(t *testing.T) {
info := exampleInfo()
var buf bytes.Buffer
require.NoError(t, Default.Package(info, &buf))
require.Positive(t, buf.Len(), "package should not be empty")
}
func TestPackageWithContents(t *testing.T) {
info := exampleInfo()
info.Contents = append(info.Contents, &files.Content{
Source: "../testdata/whatever.conf",
Destination: "/app/extra.txt",
})
var buf bytes.Buffer
require.NoError(t, Default.Package(info, &buf))
require.Positive(t, buf.Len())
}
func TestNoInfo(t *testing.T) {
info := nfpm.WithDefaults(&nfpm.Info{})
var buf bytes.Buffer
err := Default.Package(info, &buf)
require.Error(t, err)
}
// readManifest extracts AppxManifest.xml from a built package.
func readManifest(t *testing.T, raw []byte) string {
t.Helper()
zr, err := zip.NewReader(bytes.NewReader(raw), int64(len(raw)))
require.NoError(t, err)
f, err := zr.Open("AppxManifest.xml")
require.NoError(t, err)
defer f.Close()
b, err := io.ReadAll(f)
require.NoError(t, err)
return string(b)
}
func decodedPublisher(t *testing.T, raw []byte) string {
t.Helper()
var manifest struct {
Identity struct {
Publisher string `xml:"Publisher,attr"`
} `xml:"Identity"`
}
require.NoError(t, xml.Unmarshal([]byte(readManifest(t, raw)), &manifest))
return manifest.Identity.Publisher
}
// TestMissingPublisher proves the package builds without an explicit publisher
// by falling back to a CN= DN derived from the root vendor.
func TestMissingPublisher(t *testing.T) {
info := exampleInfo()
info.MSIX.Publisher = ""
var buf bytes.Buffer
require.NoError(t, Default.Package(info, &buf))
require.Contains(t, readManifest(t, buf.Bytes()), `Publisher="CN=TestCo"`)
}
func TestNoPublisherVendorOrMaintainer(t *testing.T) {
info := exampleInfo()
info.MSIX.Publisher = ""
info.Vendor = ""
info.Maintainer = ""
var buf bytes.Buffer
err := Default.Package(info, &buf)
require.Error(t, err)
require.Contains(t, err.Error(), "msix.publisher, vendor, or maintainer")
}
func TestPublisherDefaults(t *testing.T) {
tests := []struct {
name string
vendor string
want string
}{
{"plain vendor", "TestCo", "CN=TestCo"},
{"vendor with comma quoted", "TestCo, Inc", `CN="TestCo, Inc"`},
{"vendor with quote doubled", `Test"Co`, `CN="Test""Co"`},
{"vendor with plus quoted", "Test+Co", `CN="Test+Co"`},
{"vendor with equals quoted", "Test=Co", `CN="Test=Co"`},
{"vendor with hash quoted", "Test#Co", `CN="Test#Co"`},
{"vendor with backslash", `Test\Co`, `CN="Test\Co"`},
{"vendor already a DN", "CN=TestCo, O=TestCo, C=US", "CN=TestCo, O=TestCo, C=US"},
{"DN-looking vendor with comma", "CN=Acme, Inc.", `CN="CN=Acme, Inc."`},
{"DN-looking vendor with empty value", "CN=", `CN="CN="`},
{"DN-looking vendor with malformed suffix", "CN=Acme, OU", `CN="CN=Acme, OU"`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
info := exampleInfo()
info.MSIX.Publisher = ""
info.Vendor = tt.vendor
var buf bytes.Buffer
require.NoError(t, Default.Package(info, &buf))
require.Equal(t, tt.want, decodedPublisher(t, buf.Bytes()))
})
}
}
func TestPublisherFromMaintainer(t *testing.T) {
for _, tt := range []struct {
maintainer string
name string
publisher string
}{
{"Jane Doe <jane@example.com>", "Jane Doe", "CN=Jane Doe"},
{`"Jane Doe" <jane@example.com>`, "Jane Doe", "CN=Jane Doe"},
{`"Jane <Ops>" <jane@example.com>`, "Jane <Ops>", `CN="Jane <Ops>"`},
} {
t.Run(tt.maintainer, func(t *testing.T) {
info := exampleInfo()
info.MSIX.Publisher = ""
info.Vendor = ""
info.Maintainer = tt.maintainer
var buf bytes.Buffer
require.NoError(t, Default.Package(info, &buf))
require.Equal(t, tt.publisher, decodedPublisher(t, buf.Bytes()))
var manifest struct {
PublisherDisplayName string `xml:"Properties>PublisherDisplayName"`
}
require.NoError(t, xml.Unmarshal([]byte(readManifest(t, buf.Bytes())), &manifest))
require.Equal(t, tt.name, manifest.PublisherDisplayName)
})
}
}
// TestPublisherFromSigningCert proves that when signing is configured and no
// publisher is set, the identity publisher is the certificate subject, which
// Windows requires to match on signed packages.
func TestPublisherFromSigningCert(t *testing.T) {
pfxPath, passphrase := makeTestPFX(t)
info := exampleInfo()
info.MSIX.Publisher = ""
info.MSIX.Signature = nfpm.MSIXSignature{
PFXFile: pfxPath,
KeyPassphrase: passphrase,
}
var buf bytes.Buffer
require.NoError(t, Default.Package(info, &buf))
require.Equal(t, `CN="nfpm, msix", O=TestCompany, S=Washington, C=US`, decodedPublisher(t, buf.Bytes()))
}
func TestScriptsWarning(t *testing.T) {
var logs bytes.Buffer
log.SetOutput(&logs)
defer log.SetOutput(os.Stderr)
info := exampleInfo()
info.Scripts.PostInstall = "whatever.ps1"
var buf bytes.Buffer
require.NoError(t, Default.Package(info, &buf))
require.Contains(t, logs.String(), "does not support install scripts")
}
// makeTestPFX generates a self-signed code-signing cert and writes it as a
// password-protected PKCS#12 file, returning its path and passphrase.
func makeTestPFX(t *testing.T) (string, string) {
t.Helper()
return makeTestPFXWithSubject(t, nil, "test123")
}
func makeTestPFXWithSubject(t *testing.T, rawSubject []byte, passphrase string) (string, string) {
t.Helper()
key, err := rsa.GenerateKey(rand.Reader, 2048)
require.NoError(t, err)
tmpl := &x509.Certificate{
SerialNumber: big.NewInt(1),
RawSubject: rawSubject,
Subject: pkix.Name{
CommonName: "nfpm, msix",
Organization: []string{"TestCompany"},
Province: []string{"Washington"},
Country: []string{"US"},
},
NotBefore: time.Unix(0, 0),
NotAfter: time.Unix(0, 0).AddDate(20, 0, 0),
KeyUsage: x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageCodeSigning},
}
der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key)
require.NoError(t, err)
cert, err := x509.ParseCertificate(der)
require.NoError(t, err)
pfx, err := pkcs12.Modern.Encode(key, cert, nil, passphrase)
require.NoError(t, err)
path := filepath.Join(t.TempDir(), "test.pfx")
require.NoError(t, os.WriteFile(path, pfx, 0o600))
return path, passphrase
}
func TestMissingLogo(t *testing.T) {
info := exampleInfo()
info.MSIX.Properties.Logo = ""
var buf bytes.Buffer
err := Default.Package(info, &buf)
require.Error(t, err)
require.Contains(t, err.Error(), "msix.properties.logo")
}
func TestMissingApplications(t *testing.T) {
info := exampleInfo()
info.MSIX.Applications = nil
var buf bytes.Buffer
err := Default.Package(info, &buf)
require.Error(t, err)
require.Contains(t, err.Error(), "msix.applications")
}
func TestMissingApplicationID(t *testing.T) {
info := exampleInfo()
info.MSIX.Applications[0].ID = ""
var buf bytes.Buffer
err := Default.Package(info, &buf)
require.Error(t, err)
require.Contains(t, err.Error(), "msix.applications[0].id")
}
func TestMissingApplicationExecutable(t *testing.T) {
info := exampleInfo()
info.MSIX.Applications[0].Executable = ""
var buf bytes.Buffer
err := Default.Package(info, &buf)
require.Error(t, err)
require.Contains(t, err.Error(), "msix.applications[0].executable")
}
func TestSetPackagerDefaults(t *testing.T) {
info := exampleInfo()
info.MSIX.Applications[0].EntryPoint = ""
info.MSIX.Applications[0].VisualElements.BackgroundColor = ""
info.MSIX.Applications[0].VisualElements.Square150x150Logo = ""
info.MSIX.Applications[0].VisualElements.Square44x44Logo = ""
info.MSIX.Dependencies.TargetDeviceFamilies = nil
info.MSIX.Capabilities.Restricted = nil
Default.SetPackagerDefaults(info)
require.Equal(t, "Windows.FullTrustApplication", info.MSIX.Applications[0].EntryPoint)
require.Equal(t, "transparent", info.MSIX.Applications[0].VisualElements.BackgroundColor)
require.Equal(t, info.MSIX.Properties.Logo, info.MSIX.Applications[0].VisualElements.Square150x150Logo)
require.Equal(t, info.MSIX.Properties.Logo, info.MSIX.Applications[0].VisualElements.Square44x44Logo)
require.Len(t, info.MSIX.Dependencies.TargetDeviceFamilies, 1)
require.Equal(t, "Windows.Desktop", info.MSIX.Dependencies.TargetDeviceFamilies[0].Name)
require.Contains(t, info.MSIX.Capabilities.Restricted, "runFullTrust")
}
func TestPackageWithCustomProperties(t *testing.T) {
info := exampleInfo()
info.MSIX.Properties = nfpm.MSIXProperties{
DisplayName: "My Custom App",
PublisherDisplayName: "My Company",
Logo: "Assets/logo.png",
}
var buf bytes.Buffer
require.NoError(t, Default.Package(info, &buf))
require.Positive(t, buf.Len())
}
func TestPackageWithCapabilities(t *testing.T) {
info := exampleInfo()
info.MSIX.Capabilities = nfpm.MSIXCapabilities{
Capabilities: []string{"internetClient"},
DeviceCapabilities: []string{"microphone"},
Restricted: []string{"broadFileSystemAccess"},
}
var buf bytes.Buffer
require.NoError(t, Default.Package(info, &buf))
require.Positive(t, buf.Len())
}
func TestPackageWithDependencies(t *testing.T) {
info := exampleInfo()
info.MSIX.Dependencies = nfpm.MSIXDependencies{
TargetDeviceFamilies: []nfpm.MSIXTargetDeviceFamily{
{
Name: "Windows.Desktop",
MinVersion: "10.0.19041.0",
MaxVersionTested: "10.0.22621.0",
},
},
}
var buf bytes.Buffer
require.NoError(t, Default.Package(info, &buf))
require.Positive(t, buf.Len())
}