Files
ARTEX-chixuweihu/traffic/egress_blacklist_test.go
T
ji ruo d786de90af feat(traffic,sandbox): add TUN/netstack transparent gateway, egress controls, CAP_NET_RAW hardening, and i18n fixes
- Implement full-traffic transparent gateway (cmd/artex-gateway, traffic/netstack.go, traffic/transparent.go) using Linux TUN + gVisor netstack deferred TCP handshakes (eliminating nmap/nc false-positive open ports), real ICMPv4 Echo proxying, UDP/DNS interception, and per-container upstream proxy overrides.
- Add egress blacklist + asset-intercept sync, non-HTTP flow/DNS SQLite persistence and APIs, and /system/proxy & /function/traffic TCP/UDP/DNS UI panels.
- Fix sandbox Playwright MCP browser loopback --proxy-server rewrite and NODE_EXTRA_CA_CERTS override in server/mcpstdio.go.
- Strip all SUID/SGID binaries in Dockerfile.sandbox, grant minimal cap_net_raw+ep to nmap/nping/ping with ARTEX_SANDBOX_ALLOW_NET_RAW toggle, and enforce IPv4/IPv6 anti-spoofing in iptables and netstack.
- Remove hardcoded Chinese "(全局)" from GET /api/proxy/routes, localize ContainerRoutesTab / NonHTTPTrafficPanel labels, and expose reactive locale/isEn from useLiveTranslation for React Compiler compatibility.
2026-12-02 02:51:36 +08:00

123 lines
3.5 KiB
Go

package traffic
import (
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"
)
func TestEgressBlacklistRuleMatching(t *testing.T) {
bl := newEgressBlacklist([]EgressBlacklistRule{
{ID: "r1", Enabled: true, Kind: "domain", Pattern: "Exact.Example.COM"},
{ID: "r2", Enabled: true, Kind: "suffix", Pattern: "*.gov.cn"},
{ID: "r3", Enabled: true, Kind: "ip", Pattern: "203.0.113.5"},
{ID: "r4", Enabled: true, Kind: "cidr", Pattern: "198.51.100.0/24"},
{ID: "r5", Enabled: true, Kind: "domain", Pattern: "port-only.example", Port: 8443},
{ID: "r6", Enabled: false, Kind: "domain", Pattern: "disabled.example"},
})
if bl == nil {
t.Fatal("expected compiled blacklist")
}
blocked := []struct {
host string
scheme string
wantID string
}{
{"exact.example.com:443", "https", "r1"},
{"gov.cn", "https", "r2"},
{"sub.dept.gov.cn:80", "http", "r2"},
{"203.0.113.5:8080", "http", "r3"},
{"198.51.100.42:443", "https", "r4"},
{"port-only.example:8443", "https", "r5"},
}
for _, tc := range blocked {
m := bl.Check(tc.host, tc.scheme)
if m == nil || m.RuleID != tc.wantID {
t.Fatalf("Check(%q, %q) = %+v, want rule %s", tc.host, tc.scheme, m, tc.wantID)
}
}
allowed := []struct {
host string
scheme string
}{
{"other.exact.example.com:443", "https"},
{"notgov.cn:443", "https"},
{"203.0.113.6:80", "http"},
{"198.51.101.1:80", "http"},
{"port-only.example:443", "https"},
{"disabled.example:443", "https"},
}
for _, tc := range allowed {
if m := bl.Check(tc.host, tc.scheme); m != nil {
t.Fatalf("Check(%q, %q) unexpectedly blocked: %+v", tc.host, tc.scheme, m)
}
}
}
func TestProxyEnforcesEgressBlacklistInLocalModeAndLogsEvents(t *testing.T) {
// Target HTTP server on loopback (SSRF guard intentionally OFF to verify
// egress blacklist works independently in local mode).
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("REACHED"))
}))
defer target.Close()
tr, addr := startProxy(t, false)
proxyURL, err := url.Parse("http://" + addr)
if err != nil {
t.Fatal(err)
}
client := &http.Client{
Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)},
Timeout: 5 * time.Second,
}
// 1. Allowed before adding rule
resp, err := client.Get(target.URL)
if err != nil {
t.Fatalf("initial GET: %v", err)
}
body, _ := io.ReadAll(resp.Body)
resp.Body.Close()
if resp.StatusCode != 200 || string(body) != "REACHED" {
t.Fatalf("initial GET = %d %q", resp.StatusCode, string(body))
}
// 2. Hot-reload blacklist blocking 127.0.0.1 -> must return 403 + log block event
tr.SetEgressBlacklist([]EgressBlacklistRule{
{ID: "blk-1", Enabled: true, Kind: "ip", Pattern: "127.0.0.1", Source: "custom"},
})
resp2, err := client.Get(target.URL)
if err != nil {
t.Fatalf("blocked GET error: %v", err)
}
body2, _ := io.ReadAll(resp2.Body)
resp2.Body.Close()
if resp2.StatusCode != http.StatusForbidden || !strings.Contains(string(body2), "blocked by ARTEX egress blacklist") {
t.Fatalf("blocked GET = %d %q", resp2.StatusCode, string(body2))
}
events := tr.BlockEvents(10)
if len(events) == 0 || events[0].RuleID != "blk-1" || events[0].Source != "custom" {
t.Fatalf("expected recorded block event, got %+v", events)
}
// 3. Clearing blacklist immediately restores connectivity
tr.SetEgressBlacklist(nil)
resp3, err := client.Get(target.URL)
if err != nil {
t.Fatalf("post-clear GET: %v", err)
}
resp3.Body.Close()
if resp3.StatusCode != 200 {
t.Fatalf("post-clear GET = %d, want 200", resp3.StatusCode)
}
}