mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-18 18:04:17 +00:00
* 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.
137 lines
3.7 KiB
Go
137 lines
3.7 KiB
Go
package plugin
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestPiNativeLockReclaimPreservesRacingOwner(t *testing.T) {
|
|
for _, kind := range []string{"renewed", "replaced", "new content"} {
|
|
t.Run(kind, func(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "settings.json.lock")
|
|
if err := os.Mkdir(path, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
old := time.Now().Add(-time.Minute)
|
|
if err := os.Chtimes(path, old, old); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var expected os.FileInfo
|
|
piBeforeLockReclaim = func(path string) {
|
|
switch kind {
|
|
case "renewed":
|
|
now := time.Now()
|
|
if err := os.Chtimes(path, now, now); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
case "replaced":
|
|
if err := os.Rename(path, path+".previous"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Mkdir(path, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Chtimes(path, old, old); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
case "new content":
|
|
if err := os.WriteFile(filepath.Join(path, "keep"), []byte("keep"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Chtimes(path, old, old); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
expected, _ = os.Lstat(path)
|
|
}
|
|
t.Cleanup(func() { piBeforeLockReclaim = func(string) {} })
|
|
if _, err := acquirePiNativeLock(path); !errors.Is(err, ErrPiExtensionsBusy) {
|
|
t.Fatalf("err=%v", err)
|
|
}
|
|
after, err := os.Lstat(path)
|
|
if err != nil || !os.SameFile(expected, after) || !expected.ModTime().Equal(after.ModTime()) {
|
|
t.Fatalf("racing owner's directory changed: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPiNativeLockReclaimsOnlyStaleEmptyDirectories(t *testing.T) {
|
|
for _, kind := range []string{"stale", "fresh", "future", "nonempty", "file", "symlink"} {
|
|
t.Run(kind, func(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "settings.json.lock")
|
|
if kind == "file" {
|
|
if err := os.WriteFile(path, []byte("keep"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
} else if kind == "symlink" {
|
|
dest := t.TempDir()
|
|
if err := os.Symlink(dest, path); err != nil {
|
|
t.Skipf("symlinks unavailable: %v", err)
|
|
}
|
|
} else if err := os.Mkdir(path, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if kind == "nonempty" {
|
|
if err := os.WriteFile(filepath.Join(path, "keep"), []byte("keep"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
stamp := time.Now().Add(-piLockStale - time.Minute)
|
|
if kind == "fresh" {
|
|
stamp = time.Now()
|
|
}
|
|
if kind == "future" {
|
|
stamp = time.Now().Add(time.Hour)
|
|
}
|
|
if kind != "symlink" {
|
|
if err := os.Chtimes(path, stamp, stamp); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
before, err := os.Lstat(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Hold the old inode so removal cannot immediately recycle its identity.
|
|
if kind == "stale" {
|
|
f, err := openPiLockAnchor(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer f.Close()
|
|
// Windows Lstat defers reading the file ID until SameFile. Capture
|
|
// it from this handle before replacement, not from the reused path.
|
|
before, err = f.Stat()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
lock, err := acquirePiNativeLock(path)
|
|
if kind == "stale" {
|
|
if err != nil {
|
|
t.Fatalf("stale directory not reclaimed: %v", err)
|
|
}
|
|
defer lock.release()
|
|
if os.SameFile(before, lock.ours) {
|
|
t.Fatal("stale directory was reused")
|
|
}
|
|
if err := lock.verify(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return
|
|
}
|
|
if !errors.Is(err, ErrPiExtensionsBusy) {
|
|
t.Fatalf("err=%v", err)
|
|
}
|
|
after, err := os.Lstat(path)
|
|
if err != nil || !os.SameFile(before, after) || !before.ModTime().Equal(after.ModTime()) {
|
|
t.Fatalf("foreign path changed: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|