Skip to content

armgit / ARTEX

Public Imported from GitHub
0

ARTEX - AI autonomous penetration testing system (English translation; upstream: Autumn-27/ARTEX, AGPL-3.0)

Guard masked MCP env against a destination change

BaaRai4f785a8b4f09
diff --git a/server/secrets_api_test.go b/server/secrets_api_test.go
index 77ff1a1..1266654 100644
--- a/server/secrets_api_test.go
+++ b/server/secrets_api_test.go
@@ -81,3 +81,66 @@ func TestSecretsNeverReturnedInClear(t *testing.T) {
t.Fatal("stored credential changed")
}
}
+
+// A masked MCP env value is only kept while the destination (transport,
+// command, args, url) is unchanged; otherwise the secrets must be re-entered.
+func TestMCPMaskedEnvDestinationGuard(t *testing.T) {
+ s, _, request := trafficEvidenceServer(t)
+ pg := s.m.PG()
+ const secret = "mcp-guard-secret-0006"
+ httpID, err := pg.SaveMCP(&db.MCPServer{Name: "t-guard-http", Transport: "http", URL: "http://127.0.0.1:9/a",
+ Env: json.RawMessage(`{"K":"` + secret + `"}`)})
+ if err != nil {
+ t.Fatal(err)
+ }
+ stdioID, err := pg.SaveMCP(&db.MCPServer{Name: "t-guard-stdio", Transport: "stdio", Command: "npx",
+ Args: json.RawMessage(`["pkg"]`), Env: json.RawMessage(`{"K":"` + secret + `"}`)})
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { pg.DeleteMCP(httpID); pg.DeleteMCP(stdioID) })
+ stored := func(id int64) db.MCPServer {
+ all, _ := pg.ListMCP()
+ for _, m := range all {
+ if m.ID == id {
+ return *m
+ }
+ }
+ t.Fatalf("mcp %d missing", id)
+ return db.MCPServer{}
+ }
+ save := func(id int64, edit func(*db.MCPServer), want int) {
+ t.Helper()
+ m := stored(id)
+ m.Env = maskMCPEnv(m.Env)
+ edit(&m)
+ body, _ := json.Marshal(m)
+ if resp := request("POST", "/api/mcp", string(body)); resp.Code != want {
+ t.Fatalf("want HTTP %d, got %d %s", want, resp.Code, resp.Body.String())
+ }
+ }
+ keeps := func(id int64, url string) {
+ t.Helper()
+ if m := stored(id); !strings.Contains(string(m.Env), secret) || m.URL != url {
+ t.Fatalf("stored row changed: url=%q", m.URL)
+ }
+ }
+
+ save(httpID, func(m *db.MCPServer) { m.Name = "t-guard-http2" }, 200)
+ keeps(httpID, "http://127.0.0.1:9/a")
+ save(httpID, func(m *db.MCPServer) { m.URL = "http://127.0.0.1:9/evil" }, 400)
+ keeps(httpID, "http://127.0.0.1:9/a")
+ save(httpID, func(m *db.MCPServer) { m.Transport = "sse" }, 400)
+ keeps(httpID, "http://127.0.0.1:9/a")
+ save(stdioID, func(m *db.MCPServer) { m.Command = "sh" }, 400)
+ save(stdioID, func(m *db.MCPServer) { m.Args = json.RawMessage(`["other"]`) }, 400)
+ keeps(stdioID, "")
+ save(stdioID, func(m *db.MCPServer) { m.Args = json.RawMessage(`[ "pkg" ]`) }, 200) // same args, other spacing
+ keeps(stdioID, "")
+ save(httpID, func(m *db.MCPServer) {
+ m.URL, m.Env = "http://127.0.0.1:9/b", json.RawMessage(`{"K":"new-explicit-value"}`)
+ }, 200)
+ if m := stored(httpID); m.URL != "http://127.0.0.1:9/b" || !strings.Contains(string(m.Env), "new-explicit-value") {
+ t.Fatal("explicit env with a new url was not saved")
+ }
+}
diff --git a/server/server_mgmt.go b/server/server_mgmt.go
index e4d48fe..c236627 100644
--- a/server/server_mgmt.go
+++ b/server/server_mgmt.go
@@ -882,8 +882,11 @@ func maskMCPEnv(raw json.RawMessage) json.RawMessage {
// unmaskMCPEnv puts the stored value back for every env key sent as the mask,
// so a client echoing the list response never overwrites the stored secret.
+// As in notify.PrepareConfigUpdate, a stored secret is only kept for the
+// destination it was configured for: when transport, command, args or url
+// change, every env value must be re-entered (or cleared) explicitly.
func unmaskMCPEnv(pg *db.DB, m *db.MCPServer) error {
- var env, stored map[string]any
+ var env, storedEnv map[string]any
if json.Unmarshal(m.Env, &env) != nil {
return nil // not an object: SaveMCP stores or rejects it as before
}
@@ -891,17 +894,24 @@ func unmaskMCPEnv(pg *db.DB, m *db.MCPServer) error {
if err != nil {
return err
}
+ var stored *db.MCPServer
for _, cur := range all {
if m.ID != 0 && cur.ID == m.ID {
- _ = json.Unmarshal(cur.Env, &stored)
+ stored = cur
+ _ = json.Unmarshal(cur.Env, &storedEnv)
}
}
+ moved := stored != nil && (stored.Transport != m.Transport || stored.Command != m.Command ||
+ stored.URL != m.URL || canonicalMCPArgs(stored.Args) != canonicalMCPArgs(m.Args))
for k, v := range env {
if s, ok := v.(string); ok && notify.IsMasked(s) {
- old, ok := stored[k]
+ old, ok := storedEnv[k]
if !ok {
return fmt.Errorf("env %q: masked value but nothing stored to keep", k)
}
+ if moved {
+ return fmt.Errorf("the MCP destination (transport, command, args or url) changed: re-enter the env values (%q is masked) or clear them; stored secrets are not sent to a new destination", k)
+ }
env[k] = old
}
}
@@ -909,6 +919,19 @@ func unmaskMCPEnv(pg *db.DB, m *db.MCPServer) error {
return err
}
+// canonicalMCPArgs normalizes args JSON for comparison; empty means [] as in SaveMCP.
+func canonicalMCPArgs(raw json.RawMessage) string {
+ var v any
+ if len(raw) == 0 || string(raw) == "null" {
+ return "[]"
+ }
+ if json.Unmarshal(raw, &v) != nil {
+ return string(raw)
+ }
+ out, _ := json.Marshal(v)
+ return string(out)
+}
+
func (s *Server) pgSaveMCP(w http.ResponseWriter, r *http.Request) {
pg := s.pg(w)
if pg == nil {
diff --git a/web/src/app/(main)/function/tasks/detail/_tabs/credentials-tab.tsx b/web/src/app/(main)/function/tasks/detail/_tabs/credentials-tab.tsx
index a8b8bf4..644f46e 100644
--- a/web/src/app/(main)/function/tasks/detail/_tabs/credentials-tab.tsx
+++ b/web/src/app/(main)/function/tasks/detail/_tabs/credentials-tab.tsx
@@ -2,7 +2,7 @@
import * as React from "react";
-import { CopyIcon, EyeIcon, EyeOffIcon, PlusIcon, TrashIcon } from "lucide-react";
+import { PlusIcon, TrashIcon } from "lucide-react";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
@@ -15,11 +15,10 @@ import type { TaskCredential } from "@/lib/types";
// Credentials tab (#3): the task's test accounts — user-declared (program alias
// accounts) and agent-recorded (accounts the workers registered themselves).
-// Passwords render masked; reveal and copy are explicit.
+// Passwords are masked server-side and never sent to the browser.
export function CredentialsTab({ taskId }: { taskId: string }) {
const [items, setItems] = React.useState<TaskCredential[] | null>(null);
const [error, setError] = React.useState("");
- const [revealed, setRevealed] = React.useState<Set<number>>(new Set());
const [domain, setDomain] = React.useState("");
const [username, setUsername] = React.useState("");
const [password, setPassword] = React.useState("");
@@ -74,15 +73,6 @@ export function CredentialsTab({ taskId }: { taskId: string }) {
}
}
- function toggle(id: number) {
- setRevealed((prev) => {
- const next = new Set(prev);
- if (next.has(id)) next.delete(id);
- else next.add(id);
- return next;
- });
- }
-
return (
<Card>
<CardHeader>
@@ -115,24 +105,8 @@ export function CredentialsTab({ taskId }: { taskId: string }) {
</Badge>
<div className="ml-auto flex items-center gap-1">
<code className="max-w-40 truncate rounded bg-muted px-2 py-0.5 text-sm">
- {c.password === "" ? "— agent generates —" : revealed.has(c.id) ? c.password : "•".repeat(Math.min(c.password.length, 12))}
+ {c.password === "" ? "— agent generates —" : "stored, hidden"}
</code>
- {c.password !== "" ? (
- <>
- <Button size="icon" variant="ghost" className="size-7" aria-label={revealed.has(c.id) ? "Hide password" : "Reveal password"} onClick={() => toggle(c.id)}>
- {revealed.has(c.id) ? <EyeOffIcon /> : <EyeIcon />}
- </Button>
- <Button
- size="icon"
- variant="ghost"
- className="size-7"
- aria-label="Copy password"
- onClick={() => navigator.clipboard?.writeText(c.password).catch(() => {})}
- >
- <CopyIcon />
- </Button>
- </>
- ) : null}
<Button size="icon" variant="ghost" className="size-7" aria-label="Delete credential" onClick={() => remove(c.id)}>
<TrashIcon />
</Button>