mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-06 12:24:44 +00:00
go-mitmproxy (cert.NewSelfSignCA) found no key, generated one for 200 to 350 ms, then wrote the three CA files with os.Create by path: a symlink, hard link or directory planted in _ca meanwhile made it create or overwrite files outside the data directory, a window inside NewProxy that no check before the call can see. traffic.Open now creates the CA itself (loadCA, same certificate template and file format as go-mitmproxy) under the verified _ca root, each file with O_CREATE|O_EXCL|O_NOFOLLOW at its final name (key 0600, certificates 0644), never overwriting an existing file and using an existing CA as it is. go-mitmproxy gets the loaded CA through Options.NewCaFunc and no longer receives CaRootPath, so it never opens _ca: not even its own fallback, which creates the CA by path again whenever its load finds no key.
179 lines
4.8 KiB
Go
179 lines
4.8 KiB
Go
//go:build unix
|
|
|
|
package traffic
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/x509"
|
|
"os"
|
|
"path/filepath"
|
|
"syscall"
|
|
"testing"
|
|
|
|
"github.com/lqqyt2423/go-mitmproxy/cert"
|
|
)
|
|
|
|
// The first Open creates the three CA files itself, at their modes and with a
|
|
// single link each, in go-mitmproxy's format (its own loader reads them back,
|
|
// with the same root); a second Open reuses them as they are.
|
|
func TestOpenCreatesCAFilesItself(t *testing.T) {
|
|
dir := filepath.Join(t.TempDir(), "traffic")
|
|
tr, err := Open(dir, "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tr.Close()
|
|
caDir := filepath.Join(dir, "_ca")
|
|
want := map[string]os.FileMode{caKeyName: 0o600, caCertName: 0o644, caCerName: 0o644}
|
|
files := map[string][]byte{}
|
|
for name, mode := range want {
|
|
p := filepath.Join(caDir, name)
|
|
fi, err := os.Lstat(p)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !fi.Mode().IsRegular() || fi.Mode().Perm() != mode {
|
|
t.Errorf("%s is %v, want a regular file at %o", name, fi.Mode(), mode)
|
|
}
|
|
if n := fi.Sys().(*syscall.Stat_t).Nlink; n != 1 {
|
|
t.Errorf("%s has %d links", name, n)
|
|
}
|
|
if files[name], err = os.ReadFile(p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if !bytes.Equal(files[caCertName], files[caCerName]) {
|
|
t.Error("the .pem and .cer certificates differ")
|
|
}
|
|
loaded, err := cert.NewSelfSignCA(caDir) // go-mitmproxy's load()
|
|
if err != nil {
|
|
t.Fatalf("go-mitmproxy does not load the CA: %v", err)
|
|
}
|
|
ours, err := loadCA(dir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !loaded.GetRootCA().Equal(ours.GetRootCA()) {
|
|
t.Error("go-mitmproxy loads a different root")
|
|
}
|
|
pool := x509.NewCertPool()
|
|
if !pool.AppendCertsFromPEM(files[caCertName]) {
|
|
t.Fatal("no certificate in mitmproxy-ca-cert.pem")
|
|
}
|
|
leaf, err := ours.GetCert("example.test")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
lc, err := x509.ParseCertificate(leaf.Certificate[0])
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := lc.Verify(x509.VerifyOptions{Roots: pool, DNSName: "example.test"}); err != nil {
|
|
t.Errorf("a host certificate does not verify against mitmproxy-ca-cert.pem: %v", err)
|
|
}
|
|
|
|
tr, err = Open(dir, "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tr.Close()
|
|
for name := range want {
|
|
b, err := os.ReadFile(filepath.Join(caDir, name))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !bytes.Equal(b, files[name]) {
|
|
t.Errorf("a second Open changed %s", name)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A CA go-mitmproxy created (an older version's data directory) is used as it is.
|
|
func TestOpenUsesExistingCA(t *testing.T) {
|
|
dir := t.TempDir()
|
|
caDir := filepath.Join(dir, "_ca")
|
|
if err := os.MkdirAll(caDir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
old, err := cert.NewSelfSignCA(caDir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
certBefore, err := os.ReadFile(filepath.Join(caDir, caCertName))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tr, err := Open(dir, "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tr.Close()
|
|
ours, err := loadCA(dir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !ours.GetRootCA().Equal(old.GetRootCA()) {
|
|
t.Error("Open replaced the existing CA")
|
|
}
|
|
if b, _ := os.ReadFile(filepath.Join(caDir, caCertName)); !bytes.Equal(b, certBefore) {
|
|
t.Error("Open rewrote the existing certificate")
|
|
}
|
|
}
|
|
|
|
// A key without its certificates (a start that stopped between the files) gets
|
|
// them back from the key.
|
|
func TestOpenRestoresMissingCertificates(t *testing.T) {
|
|
dir := filepath.Join(t.TempDir(), "traffic")
|
|
tr, err := Open(dir, "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tr.Close()
|
|
caDir := filepath.Join(dir, "_ca")
|
|
want, err := os.ReadFile(filepath.Join(caDir, caCertName))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, name := range []string{caCertName, caCerName} {
|
|
if err := os.Remove(filepath.Join(caDir, name)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
tr, err = Open(dir, "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tr.Close()
|
|
for _, name := range []string{caCertName, caCerName} {
|
|
if b, err := os.ReadFile(filepath.Join(caDir, name)); err != nil || !bytes.Equal(b, want) {
|
|
t.Errorf("%s not restored from the key: %v", name, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A certificate without its key is refused, not left beside a new key it does
|
|
// not match, and is not touched.
|
|
func TestOpenRefusesCertificateWithoutKey(t *testing.T) {
|
|
dir := t.TempDir()
|
|
caDir := filepath.Join(dir, "_ca")
|
|
if err := os.MkdirAll(caDir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p := filepath.Join(caDir, caCertName)
|
|
if err := os.WriteFile(p, []byte("stale"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if tr, err := Open(dir, "127.0.0.1:0"); err == nil {
|
|
tr.Close()
|
|
t.Fatal("Open accepted a certificate without its key")
|
|
} else {
|
|
t.Logf("refused: %v", err)
|
|
}
|
|
if b, _ := os.ReadFile(p); string(b) != "stale" {
|
|
t.Error("Open changed the stale certificate")
|
|
}
|
|
if _, err := os.Lstat(filepath.Join(caDir, caKeyName)); !os.IsNotExist(err) {
|
|
t.Errorf("a key was created beside the stale certificate: %v", err)
|
|
}
|
|
}
|