mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-03 13:11:05 +00:00
TestOpenCalibratedCARace plants, 150 to 400 ms into the first Open of an empty traffic directory, a symlink mitmproxy-ca.pem to an outside file or to a missing outside path, a symlinked _ca to an empty outside directory, or a hard link to an outside file, 96 runs, and asserts nothing outside the traffic directory is created or changed. On 5138f35 go-mitmproxy creates or overwrites files outside while it generates the CA inside NewProxy.
TestLockDownKeyDirKeyLinkRace and TestLockDownKeyDirSymlinkSwapRace race LockDownKeyDir against a symlink or hard link swapped onto jwt.key and secret.key, and against keyDir swapped for a symlink, and assert the outside files keep their mode and content.
147 lines
4.3 KiB
Go
147 lines
4.3 KiB
Go
//go:build unix
|
|
|
|
package server
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// keyDirRaceIter and keyDirRaceBudget bound each LockDownKeyDir race test.
|
|
const (
|
|
keyDirRaceIter = 5000
|
|
keyDirRaceBudget = time.Minute
|
|
)
|
|
|
|
// wantUntouched fails if path no longer has mode want and content data.
|
|
func wantUntouched(t *testing.T, path string, want os.FileMode, data string) bool {
|
|
t.Helper()
|
|
fi, err := os.Lstat(path)
|
|
if err != nil {
|
|
t.Errorf("%s: %v", path, err)
|
|
return false
|
|
}
|
|
if fi.Mode().Perm() != want {
|
|
t.Errorf("%s is now %o, want %o", path, fi.Mode().Perm(), want)
|
|
return false
|
|
}
|
|
if fi.Mode().IsRegular() {
|
|
if b, err := os.ReadFile(path); err != nil || string(b) != data {
|
|
t.Errorf("%s content %q (%v), want %q", path, b, err, data)
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// raceLockDownKeyDir runs LockDownKeyDir(keyDir) while race cycles in another
|
|
// goroutine, and checks after every call that check still holds.
|
|
func raceLockDownKeyDir(t *testing.T, keyDir string, race func(), check func() bool) {
|
|
t.Helper()
|
|
stop, done := make(chan struct{}), make(chan struct{})
|
|
go func() {
|
|
defer close(done)
|
|
for {
|
|
select {
|
|
case <-stop:
|
|
return
|
|
default:
|
|
}
|
|
race()
|
|
}
|
|
}()
|
|
start := time.Now()
|
|
iter, accepted := 0, 0
|
|
for ; iter < keyDirRaceIter && time.Since(start) < keyDirRaceBudget; iter++ {
|
|
if LockDownKeyDir(keyDir) == nil {
|
|
accepted++
|
|
}
|
|
if !check() {
|
|
close(stop)
|
|
<-done
|
|
t.Fatalf("iteration %d: a file outside the key directory changed", iter)
|
|
}
|
|
}
|
|
close(stop)
|
|
<-done
|
|
t.Logf("%d iterations in %v, %d accepted, outside files unchanged", iter, time.Since(start).Round(time.Millisecond), accepted)
|
|
}
|
|
|
|
// While LockDownKeyDir runs, another process cycles jwt.key or secret.key through
|
|
// a file of its own, a symlink or a hard link to a host file, and nothing. The
|
|
// host file keeps its mode and content however the swaps land.
|
|
func TestLockDownKeyDirKeyLinkRace(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("long race test")
|
|
}
|
|
for _, name := range []string{"jwt.key", "secret.key"} {
|
|
for _, kind := range []string{"symlink", "hardlink"} {
|
|
t.Run(name+"/"+kind, func(t *testing.T) {
|
|
outside := t.TempDir()
|
|
victim := filepath.Join(outside, "host-file")
|
|
writeMode(t, victim, 0o644) // content "key"
|
|
keyDir := filepath.Join(t.TempDir(), "keys")
|
|
if err := os.MkdirAll(keyDir, 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if kind == "hardlink" {
|
|
probe := filepath.Join(keyDir, "probe")
|
|
if err := os.Link(victim, probe); err != nil {
|
|
t.Skipf("hard link: %v", err)
|
|
}
|
|
os.Remove(probe)
|
|
}
|
|
key := filepath.Join(keyDir, name)
|
|
own, link := filepath.Join(keyDir, "own.tmp"), filepath.Join(keyDir, "link.tmp")
|
|
race := func() {
|
|
_ = os.WriteFile(own, []byte("own"), 0o644)
|
|
_ = os.Rename(own, key)
|
|
if kind == "symlink" {
|
|
_ = os.Symlink(victim, link)
|
|
} else {
|
|
_ = os.Link(victim, link)
|
|
}
|
|
_ = os.Rename(link, key)
|
|
_ = os.Remove(key)
|
|
}
|
|
raceLockDownKeyDir(t, keyDir, race, func() bool { return wantUntouched(t, victim, 0o644, "key") })
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
// While LockDownKeyDir runs, another process swaps the key directory itself for
|
|
// a symlink to a directory elsewhere holding jwt.key and secret.key, and back.
|
|
// That directory and its keys keep their modes and content.
|
|
func TestLockDownKeyDirSymlinkSwapRace(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("long race test")
|
|
}
|
|
outside := filepath.Join(t.TempDir(), "elsewhere")
|
|
if err := os.MkdirAll(outside, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Chmod(outside, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
jwt, sec := filepath.Join(outside, "jwt.key"), filepath.Join(outside, "secret.key")
|
|
writeMode(t, jwt, 0o644)
|
|
writeMode(t, sec, 0o644)
|
|
base := t.TempDir()
|
|
keyDir, aside := filepath.Join(base, "keys"), filepath.Join(base, "keys.real")
|
|
writeMode(t, filepath.Join(keyDir, "jwt.key"), 0o644)
|
|
writeMode(t, filepath.Join(keyDir, "secret.key"), 0o644)
|
|
race := func() {
|
|
_ = os.Rename(keyDir, aside)
|
|
_ = os.Symlink(outside, keyDir)
|
|
_ = os.Remove(keyDir) // the symlink, or an empty directory MkdirAll made meanwhile
|
|
_ = os.Rename(aside, keyDir)
|
|
}
|
|
raceLockDownKeyDir(t, keyDir, race, func() bool {
|
|
return wantUntouched(t, outside, 0o755, "") &&
|
|
wantUntouched(t, jwt, 0o644, "key") &&
|
|
wantUntouched(t, sec, 0o644, "key")
|
|
})
|
|
}
|