mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-04 13:43:55 +08:00
Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b53eccd8d1 | ||
|
|
fd9e601026 | ||
|
|
df5e047b60 |
@@ -6,6 +6,7 @@ All notable changes to this project are documented in this file. The format is b
|
||||
|
||||
### Security
|
||||
|
||||
- **`GET /api/traffic` no longer returns the global proxy password.** With traffic capture off, its `proxy` field was the saved global egress proxy URL with its `user:pass` in clear, although `GET /api/settings` masks the same setting, and the dashboard requests it on every load. The field now shows `__masked__` when a global proxy is set (sending it back to `PUT /api/settings` keeps the stored proxy, as before), and still shows the local recording proxy address with capture on. Proxy validation errors returned by `PUT /api/settings` no longer quote the submitted address.
|
||||
- **A custom HTTP tool's own proxy goes through the target gate.** With the gate on, only the tool URL was checked: the proxy in the tool's `proxy` field was dialled without any check, so a tool whose target was approved could send its request to any host and port, loopback and private addresses included, through a proxy nobody approved. With the gate on, the proxy's host and port are now a target like the URL, as a proxy named on a Bash command line is: the call waits for an approval or is refused, the approval request shows the proxy without its credentials, and the connection to the proxy goes through the gate's dialer. That connection is checked and dialled as a direct hop: the approval pins the addresses it resolved rather than the name alone, so a proxy name that later answers another address is refused instead of being dialled unpinned. The recording and global proxies (`use_recording_proxy`) are unchanged. `ARTEX_SANDBOX=required` is unchanged: the sandbox egress policy already checks the proxy connection. With the gate off (no task scope, a task with the gate off, or the custom tool test endpoint) the proxy is not checked, as before, and neither is the tool URL, which can already name a loopback or private address in that mode. Behaviour change: with the gate on, a tool that uses its own proxy now raises an approval request for the proxy host and port (or is refused when none is granted) until the proxy is approved for the task.
|
||||
- **Text deny and ask rules see a Bash command as the shell decodes it.** Regex and string intercept rules on `tool_input` were matched on the raw text, but Bash removes quotes and backslashes and decodes `$'...'` before it runs a command, so `r\m -rf ./x`, `r''m -rf`, `"r"m -rf`, `/bin/r\m -rf`, `rm -r''f`, `rm -R -$'f'` or a backslash-newline inside a word ran `rm -rf` while the built-in recursive-force-delete rule did not match; so did `rm -rf ./x` on the line after another command, since the input encodes the newline as `\n` and its `n` hid the word boundary. A deny or ask rule on a Bash call now also meets the command decoded with the word decoder argv rules use: the simple commands joined by `; `, every word with its quotes, backslash escapes and line continuations removed, a word with an expansion kept as written. This covers every built-in regex rule and operator deny and ask rules; allow rules are still matched on the text as written only. The word decoder now also decodes ANSI-C strings (`$'r\x6d'`), so argv deny and ask rules meet `$'nmap'` and `bash -c $'nmap x'` too.
|
||||
- **`fuzzy_ip` asset rules and the URL host check match every spelling of an IP address.** `fuzzy_ip` compared the raw strings, so a block rule `::1` let `insert_assets` register `0:0:0:0:0:0:0:1` or `::0001`, and a rule written expanded missed the compressed form. The rule is now also matched against the canonical form of each address (zeros compressed, lower case, IPv4-mapped addresses unmapped, zone dropped), and a pattern that is a whole address is canonicalized too; a partial pattern such as `10.0.` keeps its substring meaning. `cidr` rules now match a zone-qualified address (`fe80::1%eth0`), and a zone-qualified IPv6 URL host (`http://[fe80::1%25eth0]/`) is classified as an IP instead of a domain, so `exact_ip`, `fuzzy_ip` and `cidr` rules see it, in `insert_assets` and for stored assets.
|
||||
|
||||
+10
-2
@@ -3764,9 +3764,17 @@ func (s *Server) getTraffic(w http.ResponseWriter, r *http.Request) {
|
||||
pq.MatchNone = q.Get("company") != "" && len(hosts) == 0 // a company owning no host matches nothing
|
||||
ex, matched, _ := tr.Page(pq, page, size)
|
||||
count, _ := tr.Count() // global total, for the stat card
|
||||
enabled := s.m.TrafficEnabled()
|
||||
// The proxy agents use: the local recording proxy with capture on, else
|
||||
// the global egress proxy, which may carry user:pass and is masked as in
|
||||
// GET /api/settings.
|
||||
proxy := maskedSecret(s.m.GlobalProxy())
|
||||
if enabled {
|
||||
proxy = tr.ProxyAddr()
|
||||
}
|
||||
writeJSON(w, 200, map[string]any{
|
||||
"enabled": s.m.TrafficEnabled(), // reflect the capture toggle
|
||||
"proxy": s.m.ProxyAddr(),
|
||||
"enabled": enabled, // reflect the capture toggle
|
||||
"proxy": proxy,
|
||||
"count": count, // total recorded (unfiltered)
|
||||
"total": matched, // rows matching the current filter (for pagination)
|
||||
"page": page,
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/Autumn-27/artex/traffic"
|
||||
)
|
||||
|
||||
// GET /api/traffic names the proxy agents use. With capture off that is the
|
||||
// global egress proxy, which may carry user:pass: it is masked as in
|
||||
// GET /api/settings. With capture on it is the local recording proxy, shown
|
||||
// as is.
|
||||
func TestGetTrafficMasksGlobalProxy(t *testing.T) {
|
||||
tr, err := traffic.Open(t.TempDir(), ":0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { tr.Close() })
|
||||
get := func(m *Manager) string {
|
||||
t.Helper()
|
||||
w := httptest.NewRecorder()
|
||||
(&Server{m: m}).getTraffic(w, httptest.NewRequest(http.MethodGet, "/api/traffic", nil))
|
||||
if w.Code != http.StatusOK || strings.Contains(w.Body.String(), "gp-secret") {
|
||||
t.Fatalf("GET /api/traffic: HTTP %d or the proxy password returned in clear: %s", w.Code, w.Body.String())
|
||||
}
|
||||
var body struct {
|
||||
Proxy string `json:"proxy"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return body.Proxy
|
||||
}
|
||||
const gp = "http://user:gp-secret@lab-proxy:8080"
|
||||
if got := get(&Manager{traffic: tr, globalProxy: gp}); got != "__masked__" {
|
||||
t.Fatalf("capture off: proxy = %q, want __masked__", got)
|
||||
}
|
||||
if got := get(&Manager{traffic: tr}); got != "" {
|
||||
t.Fatalf("capture off, no global proxy: proxy = %q, want empty", got)
|
||||
}
|
||||
if got := get(&Manager{traffic: tr, globalProxy: gp, trafficOn: true}); got != tr.ProxyAddr() {
|
||||
t.Fatalf("capture on: proxy = %q, want the recording proxy %q", got, tr.ProxyAddr())
|
||||
}
|
||||
}
|
||||
|
||||
// The proxy value GET /api/traffic returns, sent back to PUT /api/settings,
|
||||
// keeps the stored global proxy; a refused proxy does not echo its password.
|
||||
func TestTrafficProxyRoundTripKeepsGlobalProxy(t *testing.T) {
|
||||
s, _, request := trafficEvidenceServer(t)
|
||||
pg := s.m.PG()
|
||||
t.Cleanup(func() { pg.Exec(`DELETE FROM settings WHERE key IN ('global_proxy','traffic_capture')`) })
|
||||
const gp = "http://user:gp-secret@lab-proxy:8080"
|
||||
if w := request("PUT", "/api/settings", `{"global_proxy":"`+gp+`","traffic_capture":false}`); w.Code != 200 {
|
||||
t.Fatalf("PUT /api/settings: HTTP %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
w := request("GET", "/api/traffic", "")
|
||||
if w.Code != 200 || strings.Contains(w.Body.String(), "gp-secret") {
|
||||
t.Fatalf("GET /api/traffic: HTTP %d or the proxy password returned in clear: %s", w.Code, w.Body.String())
|
||||
}
|
||||
var got struct {
|
||||
Proxy string `json:"proxy"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if w := request("PUT", "/api/settings", `{"global_proxy":"`+got.Proxy+`"}`); w.Code != 200 {
|
||||
t.Fatalf("PUT /api/settings with the value read back: HTTP %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
if v, _, err := pg.GetSetting("global_proxy"); err != nil || v != gp || s.m.GlobalProxy() != gp {
|
||||
t.Fatalf("stored global proxy after the round trip = %q / %q (%v), want it unchanged", v, s.m.GlobalProxy(), err)
|
||||
}
|
||||
for _, bad := range []string{"http://user:gp-secret@", "user:gp-secret@lab-proxy:8080", "http://user:gp-secret@lab proxy:8080", "ftp://user:gp-secret@lab-proxy:21"} {
|
||||
w := request("PUT", "/api/settings", `{"global_proxy":"`+bad+`"}`)
|
||||
if w.Code != 400 || strings.Contains(w.Body.String(), "gp-secret") || strings.Contains(w.Body.String(), "user") {
|
||||
t.Fatalf("PUT /api/settings %q: HTTP %d %s, want a 400 that does not quote the address", bad, w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
if v, _, _ := pg.GetSetting("global_proxy"); v != gp {
|
||||
t.Fatalf("a refused proxy replaced the stored one: %q", v)
|
||||
}
|
||||
}
|
||||
+24
-1
@@ -1,6 +1,9 @@
|
||||
package traffic
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestValidateProxyURL(t *testing.T) {
|
||||
ok := []string{
|
||||
@@ -27,6 +30,26 @@ func TestValidateProxyURL(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Validation errors reach the PUT /api/settings response and logs: they never
|
||||
// quote the address, which may carry a password.
|
||||
func TestValidateProxyURLErrorsOmitAddress(t *testing.T) {
|
||||
for _, raw := range []string{
|
||||
"http://user:pw-secret@", // no host
|
||||
"user:pw-secret@proxy.example:3128", // no scheme, parsed as scheme "user"
|
||||
"http://user:pw-secret@bad host:3128", // url.Parse error, which quotes its input
|
||||
"ftp://user:pw-secret@proxy.example:21", // unsupported scheme
|
||||
"//user:pw-secret@proxy.example:3128", // no scheme
|
||||
} {
|
||||
_, err := ValidateProxyURL(raw)
|
||||
if err == nil {
|
||||
t.Fatalf("ValidateProxyURL(%q) expected error", raw)
|
||||
}
|
||||
if strings.Contains(err.Error(), "pw-secret") || strings.Contains(err.Error(), "user") {
|
||||
t.Fatalf("ValidateProxyURL(%q) error quotes the address: %v", raw, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetUpstreamProxyStoreClear(t *testing.T) {
|
||||
tr := &Traffic{}
|
||||
if got := tr.upstream.Load(); got != nil {
|
||||
|
||||
+6
-4
@@ -563,21 +563,23 @@ func (t *Traffic) RewriteRules() *rewrite.Set { return t.rewriteRules.Load() }
|
||||
// ValidateProxyURL parses and checks a proxy URL (http/https/socks5, optional
|
||||
// user:pass), returning the parsed URL. Exposed so callers can validate a global
|
||||
// proxy before persisting it even when the traffic proxy itself is disabled.
|
||||
// Errors never quote the address (nor the url.Parse error, which does): it may
|
||||
// carry a password, and callers return them to the client.
|
||||
func ValidateProxyURL(raw string) (*url.URL, error) {
|
||||
raw = strings.TrimSpace(raw)
|
||||
u, err := url.Parse(raw)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse proxy address %q: %w", raw, err)
|
||||
return nil, errors.New("proxy address is not a valid URL")
|
||||
}
|
||||
switch u.Scheme {
|
||||
case "http", "https", "socks5":
|
||||
case "":
|
||||
return nil, fmt.Errorf("proxy %q is missing a scheme (use http://, https://, or socks5://)", raw)
|
||||
return nil, errors.New("proxy address is missing a scheme (use http://, https://, or socks5://)")
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported proxy scheme %q (use http, https, or socks5)", u.Scheme)
|
||||
return nil, errors.New("unsupported proxy scheme (use http, https, or socks5)")
|
||||
}
|
||||
if u.Host == "" {
|
||||
return nil, fmt.Errorf("proxy %q is missing a host", raw)
|
||||
return nil, errors.New("proxy address is missing a host")
|
||||
}
|
||||
return u, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user