Files
skillshare/internal/plugin/pi_registration_private_windows_test.go
T
Willie 539f1d0f03 fix(pi): recover Windows stale locks and protect private registrations (#361)
* test(pi): reproduce Windows private registration access failures

Use NTFS DACL assertions instead of POSIX mode bits and verify that broadly accessible existing records cannot be read or silently reused. Keep the Unix mode assertion unchanged.

* fix(pi): reclaim Windows locks and protect private registration state

Anchor stale directories with delete sharing and delete the verified empty Windows directory by handle, keeping inode, lease and racing-owner guards. Create new registration directories with protected owner/SYSTEM DACLs and validate existing owner/access rules without changing their ACLs. Leave Unix permissions and directory-only removal unchanged.

* test(pi): capture Windows lock identity before path replacement

Windows Lstat lazily fetches file IDs by pathname. Snapshot the stale fixture through its anchored handle and add a matching-mtime replacement regression before first verification or release.

* fix(pi): snapshot Windows lock IDs and accept privileged default owners

Capture acquired lock identity through a handle before Windows can lazily resolve a replaced pathname. Elevated tokens can default file ownership to Administrators; permit privileged owners only alongside a verified current-user-only DACL. Refuse unsafe existing registration directories without modifying their ACLs or writing records.

* docs(pi): record Windows acceptance and isolated regression workflow

Document private Windows ACL requirements and refusal without permission rewrites. Add pinned devcontainer kit packaging, token-isolated full/basic acceptance, preserved UTF-8 logs and a reproducible UTM runbook. Preserve the original Unix permission assertion.

* test(backup): isolate explicit agent backup from sync snapshots

Sync automatically backs up target-local agents. Creating the fixture before sync makes the exact-one-backup assertion depend on both commands sharing a timestamp second. Create it after successful sync so the explicit backup is the only operation that snapshots it, without weakening the assertion.
2026-10-03 23:19:34 +08:00

133 lines
4.2 KiB
Go

package plugin
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
"unsafe"
"golang.org/x/sys/windows"
)
func assertPiRecordPrivate(t *testing.T, file string) {
t.Helper()
user, err := windows.GetCurrentProcessToken().GetTokenUser()
if err != nil {
t.Fatal(err)
}
for _, path := range []string{filepath.Dir(file), file} {
sd, err := windows.GetNamedSecurityInfo(path, windows.SE_FILE_OBJECT, windows.DACL_SECURITY_INFORMATION)
if err != nil {
t.Fatal(err)
}
acl, _, err := sd.DACL()
if err != nil || acl == nil {
t.Fatalf("missing private DACL: %v", err)
}
ownerAccess := false
for i := uint32(0); i < uint32(acl.AceCount); i++ {
var ace *windows.ACCESS_ALLOWED_ACE
if err := windows.GetAce(acl, i, &ace); err != nil {
t.Fatal(err)
}
if ace.Header.AceType != windows.ACCESS_ALLOWED_ACE_TYPE {
continue
}
sid := (*windows.SID)(unsafe.Pointer(&ace.SidStart))
if sid.Equals(user.User.Sid) {
ownerAccess = true
continue
}
if sid.IsWellKnown(windows.WinLocalSystemSid) || sid.IsWellKnown(windows.WinBuiltinAdministratorsSid) {
continue
}
if ace.Mask != 0 {
t.Fatalf("private registration grants another principal access: %s", sd.String())
}
}
if !ownerAccess {
t.Fatal("private registration does not grant its owner access")
}
}
}
func setPiTestPublicACL(t *testing.T, path string) {
t.Helper()
sd, err := windows.SecurityDescriptorFromString("D:P(A;OICI;FA;;;WD)")
if err != nil {
t.Fatal(err)
}
acl, _, err := sd.DACL()
if err != nil {
t.Fatal(err)
}
if err := windows.SetNamedSecurityInfo(path, windows.SE_FILE_OBJECT, windows.DACL_SECURITY_INFORMATION|windows.PROTECTED_DACL_SECURITY_INFORMATION, nil, nil, acl, nil); err != nil {
t.Fatal(err)
}
}
func TestPiRegistrationWindowsRefusesUnsafeDirectoryWithoutChangingACL(t *testing.T) {
f := newPiFixture(t)
f.global(map[string]any{"packages": []any{map[string]any{"source": "npm:demo", "extensions": []string{"-a.ts"}}}})
dir := filepath.Join(f.svc.StateDir, "pi-registrations")
if err := os.MkdirAll(dir, 0o700); err != nil {
t.Fatal(err)
}
setPiTestPublicACL(t, dir)
before, err := windows.GetNamedSecurityInfo(dir, windows.SE_FILE_OBJECT, windows.DACL_SECURITY_INFORMATION)
if err != nil {
t.Fatal(err)
}
same := unchanged(t, f.svc.ConfigPath, f.settingsPath())
req := Request{Action: "import", From: "pi", Plugin: "npm:demo"}
plan, err := f.svc.Preview(context.Background(), req)
if err != nil {
t.Fatal(err)
}
if _, err := f.svc.Apply(context.Background(), req, plan.Revision); err == nil || !strings.Contains(err.Error(), "private") {
t.Fatalf("unsafe private directory accepted: %v", err)
}
same()
after, err := windows.GetNamedSecurityInfo(dir, windows.SE_FILE_OBJECT, windows.DACL_SECURITY_INFORMATION)
if err != nil || before.String() != after.String() {
t.Fatalf("existing ACL changed: %v", err)
}
entries, err := os.ReadDir(dir)
if err != nil || len(entries) != 0 {
t.Fatalf("raw record written in unsafe directory: %v", err)
}
}
func TestPiRegistrationWindowsRejectsBroadExistingACL(t *testing.T) {
f := newPiFixture(t)
f.global(map[string]any{"packages": []any{map[string]any{"source": "npm:demo", "extensions": []string{"-a.ts"}}}})
applyPluginRequest(t, f.svc, Request{Action: "import", From: "pi", Plugin: "npm:demo"})
d, err := f.svc.load()
if err != nil {
t.Fatal(err)
}
b := d.packages["demo"].Bindings["pi"]
file, err := f.svc.piRegistrationPath(b.PiRegistration)
if err != nil {
t.Fatal(err)
}
setPiTestPublicACL(t, file)
same := unchanged(t, file, f.svc.ConfigPath, f.settingsPath())
if _, err := f.svc.readPiRegistration(b.PiRegistration, "pi", b.ID); err == nil || !strings.Contains(err.Error(), "private") {
t.Fatalf("broadly accessible record accepted: %v", err)
}
// Re-import must not silently rewrite an existing record or change its ACL.
p, err := f.svc.Preview(context.Background(), Request{Action: "import", From: "pi", Plugin: b.ID})
if err == nil && !p.Blocked {
if _, err = f.svc.Apply(context.Background(), Request{Action: "import", From: "pi", Plugin: b.ID}, p.Revision); err == nil {
t.Fatal("unsafe existing record reused")
}
}
same()
if _, err := os.Stat(file); err != nil {
t.Fatal(err)
}
}