Files
nfpm/msix/coverage_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

325 lines
11 KiB
Go

package msix
import (
"bytes"
"io"
"log"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/goreleaser/nfpm/v2"
"github.com/goreleaser/nfpm/v2/files"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestEnsureValidArchUnknown(t *testing.T) {
info := &nfpm.Info{Arch: "riscv64"}
require.Equal(t, "riscv64", ensureValidArch(info).Arch,
"an arch absent from archToMSIX must pass through untouched")
}
func TestEnsureDN(t *testing.T) {
for _, tt := range []struct {
name string
in string
want string
}{
{"plain name", "TestCo", "CN=TestCo"},
{"already a DN", "CN=TestCo, O=TestCo", "CN=TestCo, O=TestCo"},
{"already a DN with a dotted attribute", "OID.2.5.4.3=TestCo", "OID.2.5.4.3=TestCo"},
{"a leading digit is not an attribute", "1.2.3=x", `CN="1.2.3=x"`},
{"comma is quoted", "Test, Inc", `CN="Test, Inc"`},
{"plus is quoted", "Test+Co", `CN="Test+Co"`},
{"quote is doubled", `Test"Co`, `CN="Test""Co"`},
{"backslash is literal", `Test\Co`, `CN="Test\Co"`},
{"angle brackets are quoted", "Test<Co>", `CN="Test<Co>"`},
{"semicolon is quoted", "Test;Co", `CN="Test;Co"`},
{"leading space is quoted", " TestCo", `CN=" TestCo"`},
{"trailing space is quoted", "TestCo ", `CN="TestCo "`},
{"interior space is not escaped", "Test Co", "CN=Test Co"},
{"leading hash is quoted", "#TestCo", `CN="#TestCo"`},
{"interior hash is quoted", "Test#Co", `CN="Test#Co"`},
} {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.want, ensureDN(tt.in))
})
}
}
func TestConvertToMSIXVersionEdgeCases(t *testing.T) {
for _, tt := range []struct {
in string
want string
}{
{"1.2.3.4", "1.2.3.4"},
{"1.2.3", "1.2.3.0"},
{"1", "1.0.0.0"},
{"", "0.0.0.0"},
{"v1.2.3.4", "1.2.3.4"},
{"1.x.3.4", "1.0.3.4"},
{"notanumber", "0.0.0.0"},
{"1.2.3-rc1", "1.2.0.0"},
} {
t.Run(tt.in, func(t *testing.T) {
require.Equal(t, tt.want, convertToMSIXVersion(tt.in))
})
}
}
func TestNormalizePathForMSIX(t *testing.T) {
require.Equal(t, "app/fake.exe", normalizePathForMSIX("/app/fake.exe"))
require.Equal(t, "app/fake.exe", normalizePathForMSIX("app/fake.exe"))
}
// TestSetPackagerDefaultsIdempotent proves an explicit runFullTrust capability
// is not duplicated when the defaults are applied.
func TestSetPackagerDefaultsIdempotent(t *testing.T) {
info := exampleInfo()
info.MSIX.Capabilities.Restricted = []string{"runFullTrust"}
Default.SetPackagerDefaults(info)
require.Equal(t, []string{"runFullTrust"}, info.MSIX.Capabilities.Restricted)
// Applying the defaults twice must not append a second entry either.
Default.SetPackagerDefaults(info)
require.Equal(t, []string{"runFullTrust"}, info.MSIX.Capabilities.Restricted)
}
// TestSetPackagerDefaultsNoLogo covers the guards that leave the per-app logos
// empty when the package has no logo. Package cannot reach this state because
// validate rejects an empty logo, so the defaults are applied directly.
func TestSetPackagerDefaultsNoLogo(t *testing.T) {
info := &nfpm.Info{
Name: "MyCompany.TestApp",
Overridables: nfpm.Overridables{
MSIX: nfpm.MSIX{
Applications: []nfpm.MSIXApplication{{ID: "App", Executable: "app/fake.exe"}},
},
},
}
Default.SetPackagerDefaults(info)
require.Empty(t, info.MSIX.Applications[0].VisualElements.Square150x150Logo)
require.Empty(t, info.MSIX.Applications[0].VisualElements.Square44x44Logo)
require.Equal(t, "Windows.FullTrustApplication", info.MSIX.Applications[0].EntryPoint)
require.Equal(t, "transparent", info.MSIX.Applications[0].VisualElements.BackgroundColor)
require.Equal(t, []string{"runFullTrust"}, info.MSIX.Capabilities.Restricted)
}
// TestSetPackagerDefaultsNoFullTrust covers the arm where no application needs
// full trust, so no restricted capability is added.
func TestSetPackagerDefaultsNoFullTrust(t *testing.T) {
info := exampleInfo()
info.MSIX.Applications[0].EntryPoint = "MyApp.Main"
Default.SetPackagerDefaults(info)
require.Empty(t, info.MSIX.Capabilities.Restricted)
}
// TestPublisherDisplayNameFallbacks covers the display-name fallback chain.
// go-msix exposes Properties as an opaque interface, so the built manifest is
// the observable surface.
func TestPublisherDisplayNameFallbacks(t *testing.T) {
t.Run("falls back to vendor", func(t *testing.T) {
info := exampleInfo()
info.MSIX.Properties.PublisherDisplayName = ""
var buf bytes.Buffer
require.NoError(t, Default.Package(info, &buf))
require.Contains(t, readManifest(t, buf.Bytes()),
"<PublisherDisplayName>TestCo</PublisherDisplayName>")
})
t.Run("falls back to package name", func(t *testing.T) {
// An explicit publisher is required here: validate rejects an empty one,
// which is what an empty vendor and maintainer would otherwise produce.
info := exampleInfo()
info.MSIX.Properties.PublisherDisplayName = ""
info.Vendor = ""
info.Maintainer = ""
var buf bytes.Buffer
require.NoError(t, Default.Package(info, &buf))
require.Contains(t, readManifest(t, buf.Bytes()),
"<PublisherDisplayName>MyCompany.TestApp</PublisherDisplayName>")
})
t.Run("display name falls back to package name", func(t *testing.T) {
info := exampleInfo()
info.MSIX.Properties.DisplayName = ""
var buf bytes.Buffer
require.NoError(t, Default.Package(info, &buf))
require.Contains(t, readManifest(t, buf.Bytes()),
"<DisplayName>MyCompany.TestApp</DisplayName>")
})
}
// TestApplicationVisualElementsFallbacks proves an application with no visual
// metadata inherits the package display name and description.
func TestApplicationVisualElementsFallbacks(t *testing.T) {
info := exampleInfo()
info.MSIX.Applications[0].VisualElements.DisplayName = ""
info.MSIX.Applications[0].VisualElements.Description = ""
var buf bytes.Buffer
require.NoError(t, Default.Package(info, &buf))
manifest := readManifest(t, buf.Bytes())
require.Contains(t, manifest, `DisplayName="MyCompany.TestApp"`)
require.Contains(t, manifest, `Description="Test application"`)
}
func TestAddContentsSkips(t *testing.T) {
t.Run("directories are skipped", func(t *testing.T) {
info := exampleInfo()
info.Contents = append(info.Contents, &files.Content{
Destination: "/app/data",
Type: files.TypeDir,
})
var buf bytes.Buffer
require.NoError(t, Default.Package(info, &buf))
})
t.Run("symlinks are skipped with a warning", func(t *testing.T) {
var logs bytes.Buffer
log.SetOutput(&logs)
t.Cleanup(func() { log.SetOutput(os.Stderr) })
info := exampleInfo()
info.Contents = append(info.Contents, &files.Content{
Source: "/app/fake.exe",
Destination: "/app/link.exe",
Type: files.TypeSymlink,
})
var buf bytes.Buffer
require.NoError(t, Default.Package(info, &buf))
require.Contains(t, logs.String(), "msix does not support symlinks")
require.NotContains(t, readManifest(t, buf.Bytes()), "link.exe")
})
// Package cannot reach the empty-source guard: nfpm's globbing rejects a
// content with no source first. Passing a nil builder proves AddFile is
// never called for such an entry — it would panic if it were.
t.Run("contents without a source are dropped", func(t *testing.T) {
info := &nfpm.Info{
Overridables: nfpm.Overridables{
Contents: []*files.Content{{Destination: "/app/ghost.exe"}},
},
}
require.NotPanics(t, func() { addContents(nil, info) })
})
}
// TestBuildError proves errors deferred by AddFile surface from Build. The
// builder does not open sources until it writes the package.
func TestBuildError(t *testing.T) {
info := exampleInfo()
info.Contents = append(info.Contents, &files.Content{
Source: "/does/not/exist/app.exe",
Destination: "/app/missing.exe",
})
err := Default.Package(info, io.Discard)
require.Error(t, err)
}
func TestConfigureSigningMissingPFX(t *testing.T) {
info := exampleInfo()
info.MSIX.Signature.PFXFile = "/does/not/exist.pfx"
err := Default.Package(info, io.Discard)
require.Error(t, err)
require.Contains(t, err.Error(), "PFX file not found")
}
// TestConfigureSigningPFXNotAccessible covers the stat failure that is not
// ErrNotExist: a path whose parent is a regular file yields ENOTDIR on unix.
// Windows reports that as ERROR_PATH_NOT_FOUND, which maps to fs.ErrNotExist
// and takes the not-found branch instead.
func TestConfigureSigningPFXNotAccessible(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("windows maps a non-directory path component to ErrNotExist")
}
info := exampleInfo()
info.MSIX.Signature.PFXFile = filepath.Join("../testdata/whatever.conf", "nope.pfx")
err := Default.Package(info, io.Discard)
require.Error(t, err)
require.Contains(t, err.Error(), "unable to access PFX file")
}
func TestConfigureSigningWrongPassphrase(t *testing.T) {
pfxPath, _ := makeTestPFX(t)
info := exampleInfo()
info.MSIX.Signature = nfpm.MSIXSignature{
PFXFile: pfxPath,
KeyPassphrase: "wrong-passphrase",
}
err := Default.Package(info, io.Discard)
require.Error(t, err)
var expectedError *nfpm.ErrSigningFailure
require.ErrorAs(t, err, &expectedError)
}
// TestPublisherFromBadPFX ensures signing failures are reported even when
// no publisher fallback is available.
func TestPublisherFromBadPFX(t *testing.T) {
pfxPath := filepath.Join(t.TempDir(), "corrupt.pfx")
require.NoError(t, os.WriteFile(pfxPath, []byte("not a pfx"), 0o600))
validPFX, _ := makeTestPFX(t)
for _, tt := range []struct {
name string
path string
passphrase string
}{
{name: "corrupt", path: pfxPath},
{name: "missing", path: filepath.Join(t.TempDir(), "missing.pfx")},
{name: "wrong passphrase", path: validPFX, passphrase: "wrong-passphrase"},
} {
t.Run(tt.name, func(t *testing.T) {
for _, fallback := range []struct {
name string
vendor string
}{
{name: "vendor fallback", vendor: "FallbackCo"},
{name: "no fallback"},
} {
t.Run(fallback.name, func(t *testing.T) {
info := exampleInfo()
info.MSIX.Publisher = ""
info.Vendor = fallback.vendor
info.Maintainer = ""
info.MSIX.Signature = nfpm.MSIXSignature{
PFXFile: tt.path,
KeyPassphrase: tt.passphrase,
}
err := Default.Package(info, io.Discard)
require.Error(t, err)
var expectedError *nfpm.ErrSigningFailure
require.ErrorAs(t, err, &expectedError)
if tt.name == "missing" {
require.ErrorIs(t, err, os.ErrNotExist)
assert.Contains(t, err.Error(), "PFX file not found")
} else {
assert.Contains(t, err.Error(), "loading PFX")
}
assert.NotContains(t, err.Error(), "msix.publisher, vendor, or maintainer")
assert.Empty(t, info.MSIX.Publisher, "failed attempts must not persist fallback metadata")
})
}
})
}
}