mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-03 13:11:05 +00:00
63 lines
1.7 KiB
Go
63 lines
1.7 KiB
Go
//go:build unix
|
|
|
|
package server
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"testing"
|
|
)
|
|
|
|
// An upgraded data directory keeps the MITM CA key a previous version wrote
|
|
// world-readable. The startup lockdown runs before the isolation self-test, so it
|
|
// must tighten that key to 0600 itself; the self-test then sees a closed key.
|
|
func TestLockDownKeyDirTightensCAKeyBeforeSelfTest(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("unix permission bits")
|
|
}
|
|
data := t.TempDir()
|
|
ca := filepath.Join(data, "traffic", "_ca")
|
|
if err := os.MkdirAll(ca, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
key := filepath.Join(ca, "mitmproxy-ca.pem")
|
|
if err := os.WriteFile(key, []byte("key"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Chmod(key, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := LockDownKeyDir(filepath.Join(data, "keys")); err != nil {
|
|
t.Fatalf("LockDownKeyDir: %v", err)
|
|
}
|
|
if err := LockDownCAKey(data); err != nil {
|
|
t.Fatalf("LockDownCAKey: %v", err)
|
|
}
|
|
fi, err := os.Stat(key)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := fi.Mode().Perm(); got != 0o600 {
|
|
t.Fatalf("CA key mode %o, want 600 after startup lockdown", got)
|
|
}
|
|
}
|
|
|
|
// A CA key that cannot be tightened must fail the start, not pass silently.
|
|
func TestLockDownKeyDirFailsClosedOnCAKeySymlink(t *testing.T) {
|
|
data := t.TempDir()
|
|
ca := filepath.Join(data, "traffic", "_ca")
|
|
if err := os.MkdirAll(ca, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
target := filepath.Join(data, "elsewhere")
|
|
if err := os.WriteFile(target, []byte("key"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Symlink(target, filepath.Join(ca, "mitmproxy-ca.pem")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := LockDownCAKey(data); err == nil {
|
|
t.Fatal("LockDownCAKey accepted a symlinked CA key")
|
|
}
|
|
}
|