@kenhkl/agent-gate 0.9.1 → 0.9.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/dashboard.html +175 -3
- package/dist/shared/storage/db.d.ts +2 -0
- package/dist/shared/storage/db.js +16 -3
- package/dist/v1/cli.js +112 -45
- package/dist/v1/server.js +160 -93
- package/dist/v3/cli.js +140 -73
- package/package.json +1 -1
package/dist/dashboard.html
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
<meta charset="UTF-8">
|
|
5
5
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
6
|
<title>AgentGate Dashboard</title>
|
|
7
|
-
<style>:root{--bg:#FAFAFA;--card-bg:#fff;--border:#F0F0F0;--text:#1d1d1f;--muted:#8E8E93;--accent:#007AFF;--green:#34C759;--red:#FF3B30;--orange:#FF9500;--yellow:#FFCC00}*{margin:0;padding:0;box-sizing:border-box}body{background:var(--bg);color:var(--text);font:14px/1.5 -apple-system,BlinkMacSystemFont,sans-serif;-webkit-font-smoothing:antialiased;overflow-x:hidden}.header{background:var(--card-bg);border-bottom:1px solid var(--border);padding:10px 16px;display:flex;align-items:center;justify-content:space-between;position:sticky;top:0;z-index:10}.header h1{font-size:15px;font-weight:700}.live-dot{width:7px;height:7px;border-radius:50%;background:var(--green);display:inline-block;margin-right:6px;animation:pulse 2s infinite}@keyframes pulse{0%,100%{opacity:1}50%{opacity:.4}}.grid{display:grid;grid-template-columns:repeat(6,1fr);gap:8px;padding:12px 16px}.card{background:var(--card-bg);border:1px solid var(--border);border-radius:8px;padding:12px}.card .label{font-size:11px;color:var(--muted);text-transform:uppercase;letter-spacing:.5px;font-weight:600}.card .value{font-size:24px;font-weight:700;margin-top:2px}.section{padding:0 16px 12px}.risk-bar{display:flex;height:6px;border-radius:3px;overflow:hidden;background:var(--border);margin-top:4px}.risk-bar div{height:100%}.risk-legend{display:flex;gap:14px;margin-top:4px;font-size:11px;color:var(--muted)}.risk-legend span{display:flex;align-items:center;gap:3px}.risk-dot{width:6px;height:6px;border-radius:2px;display:inline-block}.tabs{display:flex;gap:0;padding:0 16px;border-bottom:1px solid var(--border);margin-bottom:8px}.tab{padding:6px 14px;font-size:12px;font-weight:600;color:var(--muted);cursor:pointer;border-bottom:2px solid transparent;user-select:none}.tab.active{color:var(--accent);border-bottom-color:var(--accent)}.table-wrap{overflow-x:auto;-webkit-overflow-scrolling:touch}table{width:100%;border-collapse:collapse;font-size:11px;white-space:nowrap}th,td{text-align:left;padding:5px 8px;border-bottom:1px solid var(--border)}th{color:var(--muted);font-weight:600;font-size:10px;position:sticky;top:0;background:var(--bg)}td code{background:#F5F5F7;padding:1px 5px;border-radius:3px;font-size:10px}.badge{display:inline-block;padding:1px 6px;border-radius:8px;font-size:10px;font-weight:600}.badge-allow{background:rgba(52,199,89,.12);color:var(--green)}.badge-deny{background:rgba(255,59,48,.12);color:var(--red)}.ag-pagination{display:flex;align-items:center;justify-content:center;gap:8px;padding:10px 14px;font-size:12px;color:var(--muted);background:#FAFAFA;border-top:1px solid var(--border)}.ag-pagination button{padding:4px 12px;border-radius:6px;border:1px solid var(--border);background:transparent;color:var(--text);font-size:12px;cursor:pointer}.ag-pagination button:hover{background:var(--accent);color:#fff;border-color:var(--accent)}.ag-pagination button:disabled{opacity:0.4;cursor:default}.footer{padding:10px 16px;color:var(--muted);font-size:10px;border-top:1px solid var(--border);text-align:center}#timeline-panel,#agents-panel{display:none}.ag-storage-banner{display:none;align-items:center;justify-content:space-between;gap:8px;margin:12px 16px 0;padding:10px 12px;border:1px solid rgba(255,149,0,.35);background:rgba(255,204,0,.14);border-radius:8px;font-size:12px}.ag-storage-banner.show{display:flex}.ag-sb-text{color:var(--text)}.ag-sb-text b{color:var(--orange)}.ag-sb-actions{display:flex;align-items:center;gap:8px;flex:none}.ag-sb-status{font-size:11px;color:var(--muted)}.ag-sb-btn{padding:5px 14px;border-radius:6px;border:1px solid var(--orange);background:var(--orange);color:#fff;font-size:12px;font-weight:600;cursor:pointer;white-space:nowrap}.ag-sb-btn:hover{background:#e08600;border-color:#e08600}.ag-sb-btn:disabled{opacity:.5;cursor:default}</style>
|
|
7
|
+
<style>:root{--bg:#FAFAFA;--card-bg:#fff;--border:#F0F0F0;--text:#1d1d1f;--muted:#8E8E93;--accent:#007AFF;--green:#34C759;--red:#FF3B30;--orange:#FF9500;--yellow:#FFCC00}*{margin:0;padding:0;box-sizing:border-box}body{background:var(--bg);color:var(--text);font:14px/1.5 -apple-system,BlinkMacSystemFont,sans-serif;-webkit-font-smoothing:antialiased;overflow-x:hidden}.header{background:var(--card-bg);border-bottom:1px solid var(--border);padding:10px 16px;display:flex;align-items:center;justify-content:space-between;position:sticky;top:0;z-index:10}.header h1{font-size:15px;font-weight:700}.live-dot{width:7px;height:7px;border-radius:50%;background:var(--green);display:inline-block;margin-right:6px;animation:pulse 2s infinite}@keyframes pulse{0%,100%{opacity:1}50%{opacity:.4}}.grid{display:grid;grid-template-columns:repeat(6,1fr);gap:8px;padding:12px 16px}.card{background:var(--card-bg);border:1px solid var(--border);border-radius:8px;padding:12px}.card .label{font-size:11px;color:var(--muted);text-transform:uppercase;letter-spacing:.5px;font-weight:600}.card .value{font-size:24px;font-weight:700;margin-top:2px}.section{padding:0 16px 12px}.risk-bar{display:flex;height:6px;border-radius:3px;overflow:hidden;background:var(--border);margin-top:4px}.risk-bar div{height:100%}.risk-legend{display:flex;gap:14px;margin-top:4px;font-size:11px;color:var(--muted)}.risk-legend span{display:flex;align-items:center;gap:3px}.risk-dot{width:6px;height:6px;border-radius:2px;display:inline-block}.tabs{display:flex;gap:0;padding:0 16px;border-bottom:1px solid var(--border);margin-bottom:8px}.tab{padding:6px 14px;font-size:12px;font-weight:600;color:var(--muted);cursor:pointer;border-bottom:2px solid transparent;user-select:none}.tab.active{color:var(--accent);border-bottom-color:var(--accent)}.table-wrap{overflow-x:auto;-webkit-overflow-scrolling:touch}table{width:100%;border-collapse:collapse;font-size:11px;white-space:nowrap}th,td{text-align:left;padding:5px 8px;border-bottom:1px solid var(--border)}th{color:var(--muted);font-weight:600;font-size:10px;position:sticky;top:0;background:var(--bg)}td code{background:#F5F5F7;padding:1px 5px;border-radius:3px;font-size:10px}.badge{display:inline-block;padding:1px 6px;border-radius:8px;font-size:10px;font-weight:600}.badge-allow{background:rgba(52,199,89,.12);color:var(--green)}.badge-deny{background:rgba(255,59,48,.12);color:var(--red)}.ag-pagination{display:flex;align-items:center;justify-content:center;gap:8px;padding:10px 14px;font-size:12px;color:var(--muted);background:#FAFAFA;border-top:1px solid var(--border)}.ag-pagination button{padding:4px 12px;border-radius:6px;border:1px solid var(--border);background:transparent;color:var(--text);font-size:12px;cursor:pointer}.ag-pagination button:hover{background:var(--accent);color:#fff;border-color:var(--accent)}.ag-pagination button:disabled{opacity:0.4;cursor:default}.footer{padding:10px 16px;color:var(--muted);font-size:10px;border-top:1px solid var(--border);text-align:center}#timeline-panel,#agents-panel{display:none}.ag-storage-banner{display:none;align-items:center;justify-content:space-between;gap:8px;margin:12px 16px 0;padding:10px 12px;border:1px solid rgba(255,149,0,.35);background:rgba(255,204,0,.14);border-radius:8px;font-size:12px}.ag-storage-banner.show{display:flex}.ag-sb-text{color:var(--text)}.ag-sb-text b{color:var(--orange)}.ag-sb-actions{display:flex;align-items:center;gap:8px;flex:none}.ag-sb-status{font-size:11px;color:var(--muted)}.ag-sb-btn{padding:5px 14px;border-radius:6px;border:1px solid var(--orange);background:var(--orange);color:#fff;font-size:12px;font-weight:600;cursor:pointer;white-space:nowrap}.ag-sb-btn:hover{background:#e08600;border-color:#e08600}.ag-sb-btn:disabled{opacity:.5;cursor:default}.ag-chain-btn{padding:2px 8px;border-radius:5px;border:1px solid var(--accent);background:transparent;color:var(--accent);font-size:10px;font-weight:600;cursor:pointer;white-space:nowrap}.ag-chain-btn:hover{background:var(--accent);color:#fff}.ag-modal{position:fixed;inset:0;display:none;align-items:center;justify-content:center;z-index:100}.ag-modal-backdrop{position:absolute;inset:0;background:rgba(0,0,0,.35)}.ag-modal-box{position:relative;background:var(--card-bg);border-radius:12px;max-width:92vw;max-height:88vh;overflow:auto;padding:16px 18px;box-shadow:0 12px 48px rgba(0,0,0,.22);min-width:520px}.ag-modal-head{display:flex;align-items:center;justify-content:space-between;gap:12px;margin-bottom:8px}.ag-modal-title{font-size:15px;font-weight:700}.ag-modal-close{border:none;background:transparent;font-size:22px;line-height:1;color:var(--muted);cursor:pointer;padding:0 4px}.ag-modal-close:hover{color:var(--text)}.ag-modal-meta{display:flex;flex-wrap:wrap;gap:8px;align-items:center;margin-bottom:12px;font-size:11px;color:var(--muted)}.ag-modal-meta .badge{font-size:10px}.ag-modal-graph{overflow:auto;padding:8px 0;border-top:1px solid var(--border);border-bottom:1px solid var(--border)}.ag-modal-reason{margin-top:10px;font-size:12px;color:var(--text);line-height:1.5;word-break:break-word}.ag-modal-legend{margin-top:6px;font-size:10px;color:var(--muted);display:flex;gap:14px;flex-wrap:wrap}.ag-modal-legend b{font-weight:600;color:var(--text)}.ag-modal-property{display:inline-flex;align-items:center;gap:6px;padding:2px 8px;border-radius:6px;background:rgba(255,59,48,.08);color:var(--red);font-size:11px;font-weight:700}.ag-modal-expr{display:flex;align-items:center;flex-wrap:wrap;gap:6px;margin:6px 0 4px;font-size:11px}.ag-expr-label{color:var(--muted);font-size:10px;font-weight:600;margin-right:4px}.ag-expr-token{display:inline-flex;flex-direction:column;align-items:center;padding:3px 8px;border-radius:6px;background:#F5F5F7;border:1px solid var(--border)}.ag-expr-token b{font-size:11px}.ag-expr-token small{font-size:9px;color:var(--muted)}.ag-expr-arrow{color:var(--muted);font-size:12px}</style>
|
|
8
8
|
</head>
|
|
9
9
|
<body>
|
|
10
10
|
<div class="header">
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
<div class="tab" data-panel="timeline">Timeline</div>
|
|
37
37
|
</div>
|
|
38
38
|
<div id="calls-panel"><div class="table-wrap">
|
|
39
|
-
<table><thead><tr><th>Time</th><th>Agent</th><th>Session</th><th>Command</th><th>Decision</th><th>Risk</th><th>Latency</th><th>Reason</th></tr></thead><tbody id="recent-table"></tbody></table>
|
|
39
|
+
<table><thead><tr><th>Time</th><th>Agent</th><th>Session</th><th>Command</th><th>Decision</th><th>Risk</th><th>Latency</th><th>Reason</th><th>Action</th></tr></thead><tbody id="recent-table"></tbody></table>
|
|
40
40
|
</div><div id="ag-pagination" class="ag-pagination"></div></div>
|
|
41
41
|
<div id="agents-panel"><div class="table-wrap">
|
|
42
42
|
<table><thead><tr><th>Agent Type</th><th>Hostname</th><th>Sessions</th><th>Calls</th></tr></thead><tbody id="agents-table"></tbody></table>
|
|
@@ -45,11 +45,25 @@
|
|
|
45
45
|
<table><thead><tr><th>Time</th><th>Allow</th><th>Deny</th></tr></thead><tbody id="timeline-table"></tbody></table>
|
|
46
46
|
</div></div>
|
|
47
47
|
<div class="footer" id="footer">Loading versions...</div>
|
|
48
|
+
<div class="ag-modal" id="chain-modal">
|
|
49
|
+
<div class="ag-modal-backdrop" id="chain-modal-backdrop"></div>
|
|
50
|
+
<div class="ag-modal-box">
|
|
51
|
+
<div class="ag-modal-head">
|
|
52
|
+
<span class="ag-modal-title" id="chain-modal-title">组合式攻击链</span>
|
|
53
|
+
<button class="ag-modal-close" id="chain-modal-close" title="关闭">×</button>
|
|
54
|
+
</div>
|
|
55
|
+
<div class="ag-modal-meta" id="chain-modal-meta"></div>
|
|
56
|
+
<div class="ag-modal-expr" id="chain-modal-expr"></div>
|
|
57
|
+
<div class="ag-modal-graph" id="chain-modal-graph"></div>
|
|
58
|
+
<div class="ag-modal-reason" id="chain-modal-reason"></div>
|
|
59
|
+
</div>
|
|
60
|
+
</div>
|
|
48
61
|
<script>
|
|
49
62
|
(async function(){
|
|
50
63
|
var _currentPage = 1;
|
|
51
64
|
var _pageSize = 20;
|
|
52
65
|
var _totalRecords = 0;
|
|
66
|
+
var _currentRows = [];
|
|
53
67
|
|
|
54
68
|
function setText(id, v){ var el=document.getElementById(id); if(el)el.textContent=v; }
|
|
55
69
|
// HTML 转义。dashboard 展示的 agent_type / session_id / command / reason /
|
|
@@ -156,8 +170,9 @@
|
|
|
156
170
|
var rb=document.getElementById('risk-bar'),rl=document.getElementById('risk-legend');
|
|
157
171
|
if(rb)rb.innerHTML='<div style="width:'+lv.critical/tr*100+'%;background:var(--red)"></div><div style="width:'+lv.high/tr*100+'%;background:var(--orange)"></div><div style="width:'+lv.medium/tr*100+'%;background:var(--yellow)"></div><div style="width:'+lv.low/tr*100+'%;background:var(--muted)"></div>';
|
|
158
172
|
if(rl)rl.innerHTML='<span><span class="risk-dot" style="background:var(--red)"></span>Critical '+lv.critical+'</span><span><span class="risk-dot" style="background:var(--orange)"></span>High '+lv.high+'</span><span><span class="risk-dot" style="background:var(--yellow)"></span>Medium '+lv.medium+'</span><span><span class="risk-dot" style="background:var(--muted)"></span>Low '+lv.low+'</span>';
|
|
173
|
+
_currentRows = arr;
|
|
159
174
|
var tb=document.getElementById('recent-table');
|
|
160
|
-
if(tb)tb.innerHTML=arr.map(function(x){var t=x.called_at?new Date(x.called_at.replace(' ','T')+'Z'):null;return'<tr><td>'+(t?t.toLocaleTimeString('zh-CN',{hour:'2-digit',minute:'2-digit',second:'2-digit'}):'-')+'</td><td>'+esc(x.agent_type||'-')+'</td><td>'+esc(x.session_id||'-')+'</td><td><code>'+esc(x.command||x.action_detail||'-')+'</code></td><td><span class="badge '+(x.decision==='Allow'?'badge-allow':'badge-deny')+'">'+esc(x.decision)+'</span></td><td style="color:'+(x.risk_level==='critical'?'var(--red)':x.risk_level==='high'?'var(--orange)':x.risk_level==='medium'?'var(--yellow)':'var(--muted)')+'">'+esc(x.risk_level||'-')+'</td><td>'+Math.round(x.analysis_duration_ms||0)+'ms</td><td>'+esc(x.reason||'-')+'</td></tr>'}).join('');
|
|
175
|
+
if(tb)tb.innerHTML=arr.map(function(x,i){var t=x.called_at?new Date(x.called_at.replace(' ','T')+'Z'):null;var hasChain=!!parseChainGraph(x);return'<tr><td>'+(t?t.toLocaleTimeString('zh-CN',{hour:'2-digit',minute:'2-digit',second:'2-digit'}):'-')+'</td><td>'+esc(x.agent_type||'-')+'</td><td>'+esc(x.session_id||'-')+'</td><td><code>'+esc(x.command||x.action_detail||'-')+'</code></td><td><span class="badge '+(x.decision==='Allow'?'badge-allow':'badge-deny')+'">'+esc(x.decision)+'</span></td><td style="color:'+(x.risk_level==='critical'?'var(--red)':x.risk_level==='high'?'var(--orange)':x.risk_level==='medium'?'var(--yellow)':'var(--muted)')+'">'+esc(x.risk_level||'-')+'</td><td>'+Math.round(x.analysis_duration_ms||0)+'ms</td><td>'+esc(x.reason||'-')+'</td><td>'+(hasChain?'<button class="ag-chain-btn" data-chain="'+i+'">查看攻击链</button>':'')+'</td></tr>'}).join('');
|
|
161
176
|
// Pagination
|
|
162
177
|
var totalPages = Math.max(1, Math.ceil(_totalRecords / _pageSize));
|
|
163
178
|
var pg = document.getElementById('ag-pagination');
|
|
@@ -174,6 +189,163 @@
|
|
|
174
189
|
if(tt)tt.innerHTML=tl.map(function(t){return'<tr><td>'+t.time+'</td><td style="color:var(--green)">'+t.allow+'</td><td style="color:var(--red)">'+t.deny+'</td></tr>'}).join('');
|
|
175
190
|
}catch(e){console.error(e)}
|
|
176
191
|
}
|
|
192
|
+
// ==================== 组合式攻击链图(SVG) ====================
|
|
193
|
+
// 数据契约(AgentMoss violation_graph):
|
|
194
|
+
// {property_id,property_short,property_name,
|
|
195
|
+
// nodes:[{session_id,seq,stage,action_detail,resources,role,witness}],
|
|
196
|
+
// edges:[{from,to,type,label}]}
|
|
197
|
+
var CHAIN_ROLE_LABEL = {
|
|
198
|
+
source: '敏感数据源', carrier: '数据载体', sink: '外传点',
|
|
199
|
+
witness: '违例见证',
|
|
200
|
+
LISTEN: '开据点', EXEC: '拿遥控器', SCAN: '探路',
|
|
201
|
+
CRED: '偷钥匙', EXFIL: '往外搬', PERSIST: '赖着不走', PRIVESC: '抢 root'
|
|
202
|
+
};
|
|
203
|
+
function parseChainGraph(x){
|
|
204
|
+
var g = x && x.violation_graph;
|
|
205
|
+
if (!g) return null;
|
|
206
|
+
if (typeof g === 'string') { try { g = JSON.parse(g); } catch(e){ return null; } }
|
|
207
|
+
if (!g || !Array.isArray(g.nodes) || g.nodes.length === 0) return null;
|
|
208
|
+
return g;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function renderChainSvg(g){
|
|
212
|
+
var nodes = g.nodes || [], edges = g.edges || [];
|
|
213
|
+
var NW = 160, NH = 78, GX = 48, GY = 32, PER_ROW = 4;
|
|
214
|
+
var rows = Math.max(1, Math.ceil(nodes.length / PER_ROW));
|
|
215
|
+
var cols = Math.min(nodes.length, PER_ROW);
|
|
216
|
+
var W = 40 + cols * NW + (cols - 1) * GX;
|
|
217
|
+
var H = 40 + rows * NH + (rows - 1) * GY;
|
|
218
|
+
var pos = nodes.map(function(_, i){
|
|
219
|
+
var r = Math.floor(i / PER_ROW), c = i % PER_ROW;
|
|
220
|
+
return { x: 20 + c * (NW + GX), y: 20 + r * (NH + GY) };
|
|
221
|
+
});
|
|
222
|
+
var parts = [];
|
|
223
|
+
parts.push('<defs><marker id="ag-chain-arrow" markerWidth="6" markerHeight="6" refX="5" refY="3" orient="auto"><path d="M0,0 L6,3 L0,6 Z" fill="#8E8E93"/></marker></defs>');
|
|
224
|
+
// 边:同一行用直线;换行用正交折线(避免斜线穿图)
|
|
225
|
+
edges.forEach(function(e){
|
|
226
|
+
var a = pos[e.from], b = pos[e.to];
|
|
227
|
+
if (!a || !b) return;
|
|
228
|
+
var dash = e.type === 'data' ? ' stroke-dasharray="6 4"' : '';
|
|
229
|
+
var rawLabel = e.label || '';
|
|
230
|
+
if (rawLabel.length > 24) rawLabel = rawLabel.slice(0, 23) + '…';
|
|
231
|
+
var labelText = rawLabel ? ('载体: ' + rawLabel) : '';
|
|
232
|
+
var sameRow = Math.abs(a.y - b.y) < 1;
|
|
233
|
+
var d, lx, ly;
|
|
234
|
+
if (sameRow) {
|
|
235
|
+
var x1 = a.x + NW, y1 = a.y + NH / 2, x2 = b.x, y2 = b.y + NH / 2;
|
|
236
|
+
d = 'M' + x1 + ' ' + y1 + ' L' + (x2 - 6) + ' ' + y2;
|
|
237
|
+
lx = (x1 + x2) / 2; ly = y1 - 7;
|
|
238
|
+
} else {
|
|
239
|
+
// 跨行:从上一行节点底部中心 → 行间中点 → 下一节点顶部中心
|
|
240
|
+
var ax = a.x + NW / 2, bx = b.x + NW / 2;
|
|
241
|
+
var midY = (a.y + NH + b.y) / 2;
|
|
242
|
+
d = 'M' + ax + ' ' + (a.y + NH) + ' L' + ax + ' ' + midY +
|
|
243
|
+
' L' + bx + ' ' + midY + ' L' + bx + ' ' + (b.y - 6);
|
|
244
|
+
lx = (ax + bx) / 2; ly = midY - 5;
|
|
245
|
+
}
|
|
246
|
+
parts.push('<path d="' + d + '" fill="none" stroke="#8E8E93" stroke-width="1.5" stroke-linejoin="round"' + dash + ' marker-end="url(#ag-chain-arrow)"/>');
|
|
247
|
+
if (labelText) {
|
|
248
|
+
parts.push('<text x="' + lx + '" y="' + ly + '" text-anchor="middle" font-size="9" fill="#8E8E93">' + esc(labelText) + '</text>');
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
// 节点
|
|
252
|
+
nodes.forEach(function(n, i){
|
|
253
|
+
var p = pos[i], last = i === nodes.length - 1;
|
|
254
|
+
var witness = !!n.witness || last;
|
|
255
|
+
var stroke = witness ? '#FF3B30' : '#D1D1D6';
|
|
256
|
+
var badgeColor = witness ? '#FF3B30' : '#007AFF';
|
|
257
|
+
var roleKey = n.role || n.stage || '';
|
|
258
|
+
var roleText = CHAIN_ROLE_LABEL[roleKey] || roleKey;
|
|
259
|
+
var fullCmd = String(n.action_detail || '');
|
|
260
|
+
var fullTitle = String((n.session_id || '-') + ' #' + n.seq);
|
|
261
|
+
// 完整内容走原生 tooltip(鼠标悬停显示)
|
|
262
|
+
var tip = fullTitle + (roleText ? ' [' + roleText + ']' : '') + '\n' + fullCmd;
|
|
263
|
+
if (n.resources && n.resources.length) tip += '\n资源: ' + n.resources.join(', ');
|
|
264
|
+
parts.push('<g><title>'+esc(tip)+'</title>');
|
|
265
|
+
parts.push('<rect x="'+p.x+'" y="'+p.y+'" width="'+NW+'" height="'+NH+'" rx="7" fill="#fff" stroke="'+stroke+'" stroke-width="'+(witness ? 2 : 1)+'"/>');
|
|
266
|
+
// 序号徽章:与性质表达式里的节点编号一一对应
|
|
267
|
+
parts.push('<circle cx="'+(p.x+13)+'" cy="'+(p.y+13)+'" r="8" fill="'+badgeColor+'" opacity="0.12"/>');
|
|
268
|
+
parts.push('<text x="'+(p.x+13)+'" y="'+(p.y+16)+'" text-anchor="middle" font-size="9" font-weight="700" fill="'+badgeColor+'">'+(i+1)+'</text>');
|
|
269
|
+
var shortTitle = fullTitle.length > 15 ? fullTitle.slice(0, 14) + '…' : fullTitle;
|
|
270
|
+
parts.push('<text x="'+(p.x+25)+'" y="'+(p.y+16)+'" font-size="9" font-weight="700" fill="#1d1d1f">'+esc(shortTitle)+'</text>');
|
|
271
|
+
if (roleText) {
|
|
272
|
+
parts.push('<text x="'+(p.x+NW-8)+'" y="'+(p.y+16)+'" text-anchor="end" font-size="7" font-weight="700" fill="'+(witness ? '#FF3B30' : '#007AFF')+'">'+esc(roleText)+'</text>');
|
|
273
|
+
}
|
|
274
|
+
// 命令最多 2 行,每行 22 字符;超长省略,完整内容见 tooltip
|
|
275
|
+
var lines = [];
|
|
276
|
+
for (var k = 0; k < fullCmd.length && lines.length < 2; k += 22) lines.push(fullCmd.slice(k, k + 22));
|
|
277
|
+
if (fullCmd.length > 44) lines[1] = lines[1].slice(0, 18) + '...';
|
|
278
|
+
lines.forEach(function(line, li){
|
|
279
|
+
parts.push('<text x="'+(p.x+8)+'" y="'+(p.y+34+li*12)+'" font-size="9" fill="#444" font-family="monospace">'+esc(line)+'</text>');
|
|
280
|
+
});
|
|
281
|
+
if (n.resources && n.resources.length) {
|
|
282
|
+
var res = String(n.resources[0]);
|
|
283
|
+
if (res.length > 22) res = '…' + res.slice(-21);
|
|
284
|
+
parts.push('<text x="'+(p.x+8)+'" y="'+(p.y+NH-6)+'" font-size="8" fill="#8E8E93">'+esc(res)+'</text>');
|
|
285
|
+
}
|
|
286
|
+
if (witness) {
|
|
287
|
+
parts.push('<text x="'+(p.x+NW-8)+'" y="'+(p.y+NH-6)+'" text-anchor="end" font-size="8" font-weight="700" fill="#FF3B30">违例见证</text>');
|
|
288
|
+
}
|
|
289
|
+
parts.push('</g>');
|
|
290
|
+
});
|
|
291
|
+
return '<svg xmlns="http://www.w3.org/2000/svg" width="'+W+'" height="'+H+'" viewBox="0 0 '+W+' '+H+'">'+parts.join('')+'</svg>';
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// 性质表达式 → 节点编号(与节点上的序号徽章一一对应)
|
|
295
|
+
function renderPropertyExpr(g){
|
|
296
|
+
var expr = g.property_expression || [];
|
|
297
|
+
if (!expr.length) return '';
|
|
298
|
+
var parts = expr.map(function(t){
|
|
299
|
+
var idx = Array.isArray(t.node_index) ? t.node_index : [t.node_index];
|
|
300
|
+
var nums = idx.map(function(i){ return String(i + 1); }).join(',');
|
|
301
|
+
var label = CHAIN_ROLE_LABEL[t.token] || '';
|
|
302
|
+
return '<span class="ag-expr-token"><b>'+esc(t.token)+'</b><small>'+(label ? esc(label) + ' · ' : '')+'节点 '+nums+'</small></span>';
|
|
303
|
+
});
|
|
304
|
+
return '<span class="ag-expr-label">性质表达式</span>' + parts.join('<span class="ag-expr-arrow">→</span>');
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
function openChainModal(row){
|
|
308
|
+
var g = parseChainGraph(row);
|
|
309
|
+
if (!g) return;
|
|
310
|
+
setText('chain-modal-title', row.decision === 'Deny' ? '组合式攻击链' : '调用链');
|
|
311
|
+
var meta = [];
|
|
312
|
+
if (row.decision) meta.push('<span class="badge '+(row.decision === 'Allow' ? 'badge-allow' : 'badge-deny')+'">'+esc(row.decision)+'</span>');
|
|
313
|
+
if (g.property_short || g.property_id) {
|
|
314
|
+
var pname = g.property_name ? '(' + g.property_name + ')' : '';
|
|
315
|
+
meta.push('<span class="ag-modal-property">违反性质:' + esc(g.property_short || '') + ' · ' + esc(g.property_id || '') + pname + '</span>');
|
|
316
|
+
}
|
|
317
|
+
if (row.risk_level) meta.push('<span>风险:'+esc(row.risk_level)+'</span>');
|
|
318
|
+
if (row.risk_type) meta.push('<span>类型:'+esc(row.risk_type)+'</span>');
|
|
319
|
+
var layers = row.violated_layers;
|
|
320
|
+
if (typeof layers === 'string') { try { layers = JSON.parse(layers); } catch(e){ layers = []; } }
|
|
321
|
+
if (Array.isArray(layers) && layers.length) meta.push('<span>触发层:'+esc(layers.join(', '))+'</span>');
|
|
322
|
+
meta.push('<span>会话:'+esc(row.session_id || '-')+'</span>');
|
|
323
|
+
document.getElementById('chain-modal-meta').innerHTML = meta.join('');
|
|
324
|
+
document.getElementById('chain-modal-expr').innerHTML = renderPropertyExpr(g);
|
|
325
|
+
document.getElementById('chain-modal-graph').innerHTML = renderChainSvg(g)
|
|
326
|
+
+ '<div class="ag-modal-legend"><span>—— 实线 = 动作时序</span><span>- - 虚线 = 数据流(共享同一文件/载体)</span><span>连线标签 = 共享载体路径</span><span style="color:var(--red)">红框 = 违例见证节点</span><span>① 序号 = 性质表达式对应节点</span></div>';
|
|
327
|
+
setText('chain-modal-reason', row.reason || '');
|
|
328
|
+
document.getElementById('chain-modal').style.display = 'flex';
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function closeChainModal(){
|
|
332
|
+
var m = document.getElementById('chain-modal');
|
|
333
|
+
if (m) m.style.display = 'none';
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
var chainTb = document.getElementById('recent-table');
|
|
337
|
+
if (chainTb) chainTb.addEventListener('click', function(ev){
|
|
338
|
+
var btn = ev.target && ev.target.closest ? ev.target.closest('[data-chain]') : null;
|
|
339
|
+
if (!btn) return;
|
|
340
|
+
var i = Number(btn.getAttribute('data-chain'));
|
|
341
|
+
if (_currentRows[i]) openChainModal(_currentRows[i]);
|
|
342
|
+
});
|
|
343
|
+
var chainClose = document.getElementById('chain-modal-close');
|
|
344
|
+
if (chainClose) chainClose.addEventListener('click', closeChainModal);
|
|
345
|
+
var chainBackdrop = document.getElementById('chain-modal-backdrop');
|
|
346
|
+
if (chainBackdrop) chainBackdrop.addEventListener('click', closeChainModal);
|
|
347
|
+
document.addEventListener('keydown', function(e){ if (e.key === 'Escape') closeChainModal(); });
|
|
348
|
+
|
|
177
349
|
refresh(); setInterval(refresh, 10000);
|
|
178
350
|
// Tab switching
|
|
179
351
|
document.querySelectorAll('.tab').forEach(function(tab){
|
|
@@ -37,6 +37,8 @@ interface ToolCallRecord {
|
|
|
37
37
|
reason: string;
|
|
38
38
|
confidence: number;
|
|
39
39
|
analysis_duration_ms: number;
|
|
40
|
+
/** 结构化攻击链图(JSON 字符串;AgentMoss violation_graph 原样存储) */
|
|
41
|
+
violation_graph?: string;
|
|
40
42
|
}
|
|
41
43
|
interface ToolCallRich extends ToolCallRecord {
|
|
42
44
|
id: number;
|
|
@@ -609,6 +609,7 @@ CREATE TABLE IF NOT EXISTS tool_calls (
|
|
|
609
609
|
reason TEXT DEFAULT '',
|
|
610
610
|
confidence INTEGER DEFAULT 0,
|
|
611
611
|
analysis_duration_ms REAL DEFAULT 0,
|
|
612
|
+
violation_graph TEXT DEFAULT '{}',
|
|
612
613
|
called_at TEXT DEFAULT (datetime('now')),
|
|
613
614
|
UNIQUE(session_id, seq)
|
|
614
615
|
);
|
|
@@ -635,6 +636,17 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
|
|
|
635
636
|
this.db = new Database(path);
|
|
636
637
|
this.db.pragma("journal_mode = WAL");
|
|
637
638
|
this.db.exec(SCHEMA);
|
|
639
|
+
this.ensureColumn("tool_calls", "violation_graph", "violation_graph TEXT DEFAULT '{}'");
|
|
640
|
+
}
|
|
641
|
+
/** 幂等补列(旧数据库平滑升级,失败不阻断启动)。 */
|
|
642
|
+
ensureColumn(table, column, ddl) {
|
|
643
|
+
try {
|
|
644
|
+
const cols = this.db.prepare(`PRAGMA table_info(${table})`).all();
|
|
645
|
+
if (!cols.some((c) => c.name === column)) {
|
|
646
|
+
this.db.exec(`ALTER TABLE ${table} ADD COLUMN ${ddl}`);
|
|
647
|
+
}
|
|
648
|
+
} catch {
|
|
649
|
+
}
|
|
638
650
|
}
|
|
639
651
|
static isAvailable() {
|
|
640
652
|
try {
|
|
@@ -676,8 +688,8 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
|
|
|
676
688
|
return this.db.prepare(`
|
|
677
689
|
INSERT INTO tool_calls (session_id, seq, tool_name, command, description, cwd,
|
|
678
690
|
action_type, action_detail, decision, risk_level, risk_type, violated_layers,
|
|
679
|
-
reason, confidence, analysis_duration_ms)
|
|
680
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
691
|
+
reason, confidence, analysis_duration_ms, violation_graph)
|
|
692
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
681
693
|
`).run(
|
|
682
694
|
rec.session_id,
|
|
683
695
|
seq,
|
|
@@ -693,7 +705,8 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
|
|
|
693
705
|
rec.violated_layers,
|
|
694
706
|
rec.reason,
|
|
695
707
|
rec.confidence,
|
|
696
|
-
rec.analysis_duration_ms
|
|
708
|
+
rec.analysis_duration_ms,
|
|
709
|
+
rec.violation_graph || "{}"
|
|
697
710
|
);
|
|
698
711
|
})(record);
|
|
699
712
|
return Number(result.lastInsertRowid);
|
package/dist/v1/cli.js
CHANGED
|
@@ -177,8 +177,8 @@ function readFdToken() {
|
|
|
177
177
|
if (!raw) return "";
|
|
178
178
|
const fd = parseInt(raw, 10);
|
|
179
179
|
if (!Number.isFinite(fd)) return "";
|
|
180
|
-
const
|
|
181
|
-
if (
|
|
180
|
+
const cached2 = fdTokenCache.get(fd);
|
|
181
|
+
if (cached2 !== void 0) return cached2;
|
|
182
182
|
try {
|
|
183
183
|
const buf = Buffer.alloc(8192);
|
|
184
184
|
const n = (0, import_node_fs.readSync)(fd, buf, 0, buf.length, null);
|
|
@@ -1006,15 +1006,15 @@ var init_json_storage = __esm({
|
|
|
1006
1006
|
const archiveCount = total - keep;
|
|
1007
1007
|
const toArchive = flat.slice(0, archiveCount);
|
|
1008
1008
|
const archiveIds = new Set(toArchive.map((x) => String(x.rec.id)));
|
|
1009
|
-
const { mkdirSync:
|
|
1010
|
-
const { join:
|
|
1009
|
+
const { mkdirSync: mkdirSync5, writeFileSync: writeFileSync4 } = require("fs");
|
|
1010
|
+
const { join: join5 } = require("path");
|
|
1011
1011
|
const { gzipSync } = require("zlib");
|
|
1012
|
-
const archiveDir =
|
|
1013
|
-
if (!(0, import_fs2.existsSync)(archiveDir))
|
|
1012
|
+
const archiveDir = join5(this.dataDir, "archive");
|
|
1013
|
+
if (!(0, import_fs2.existsSync)(archiveDir)) mkdirSync5(archiveDir, { recursive: true, mode: 448 });
|
|
1014
1014
|
const stamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-");
|
|
1015
|
-
const file =
|
|
1015
|
+
const file = join5(archiveDir, `tool_calls-${stamp}.jsonl.gz`);
|
|
1016
1016
|
const payload = toArchive.map((x) => JSON.stringify(x.rec)).join("\n") + "\n";
|
|
1017
|
-
|
|
1017
|
+
writeFileSync4(file, gzipSync(Buffer.from(payload, "utf-8")), { mode: 384 });
|
|
1018
1018
|
for (const [sid, recs] of bySession) {
|
|
1019
1019
|
const remain = recs.filter((r) => !archiveIds.has(String(r.id)));
|
|
1020
1020
|
if (remain.length !== recs.length) this.saveToolCalls(sid, remain);
|
|
@@ -1141,6 +1141,7 @@ CREATE TABLE IF NOT EXISTS tool_calls (
|
|
|
1141
1141
|
reason TEXT DEFAULT '',
|
|
1142
1142
|
confidence INTEGER DEFAULT 0,
|
|
1143
1143
|
analysis_duration_ms REAL DEFAULT 0,
|
|
1144
|
+
violation_graph TEXT DEFAULT '{}',
|
|
1144
1145
|
called_at TEXT DEFAULT (datetime('now')),
|
|
1145
1146
|
UNIQUE(session_id, seq)
|
|
1146
1147
|
);
|
|
@@ -1167,6 +1168,17 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
|
|
|
1167
1168
|
this.db = new Database(path);
|
|
1168
1169
|
this.db.pragma("journal_mode = WAL");
|
|
1169
1170
|
this.db.exec(SCHEMA);
|
|
1171
|
+
this.ensureColumn("tool_calls", "violation_graph", "violation_graph TEXT DEFAULT '{}'");
|
|
1172
|
+
}
|
|
1173
|
+
/** 幂等补列(旧数据库平滑升级,失败不阻断启动)。 */
|
|
1174
|
+
ensureColumn(table, column, ddl) {
|
|
1175
|
+
try {
|
|
1176
|
+
const cols = this.db.prepare(`PRAGMA table_info(${table})`).all();
|
|
1177
|
+
if (!cols.some((c) => c.name === column)) {
|
|
1178
|
+
this.db.exec(`ALTER TABLE ${table} ADD COLUMN ${ddl}`);
|
|
1179
|
+
}
|
|
1180
|
+
} catch {
|
|
1181
|
+
}
|
|
1170
1182
|
}
|
|
1171
1183
|
static isAvailable() {
|
|
1172
1184
|
try {
|
|
@@ -1208,8 +1220,8 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
|
|
|
1208
1220
|
return this.db.prepare(`
|
|
1209
1221
|
INSERT INTO tool_calls (session_id, seq, tool_name, command, description, cwd,
|
|
1210
1222
|
action_type, action_detail, decision, risk_level, risk_type, violated_layers,
|
|
1211
|
-
reason, confidence, analysis_duration_ms)
|
|
1212
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
1223
|
+
reason, confidence, analysis_duration_ms, violation_graph)
|
|
1224
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
1213
1225
|
`).run(
|
|
1214
1226
|
rec.session_id,
|
|
1215
1227
|
seq,
|
|
@@ -1225,7 +1237,8 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
|
|
|
1225
1237
|
rec.violated_layers,
|
|
1226
1238
|
rec.reason,
|
|
1227
1239
|
rec.confidence,
|
|
1228
|
-
rec.analysis_duration_ms
|
|
1240
|
+
rec.analysis_duration_ms,
|
|
1241
|
+
rec.violation_graph || "{}"
|
|
1229
1242
|
);
|
|
1230
1243
|
})(record);
|
|
1231
1244
|
return Number(result.lastInsertRowid);
|
|
@@ -1322,11 +1335,11 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
|
|
|
1322
1335
|
const row = this.db.prepare("SELECT COUNT(*) as total FROM tool_calls").get();
|
|
1323
1336
|
let dbBytes = 0;
|
|
1324
1337
|
try {
|
|
1325
|
-
const { statSync: statSync3, existsSync:
|
|
1338
|
+
const { statSync: statSync3, existsSync: existsSync4 } = require("fs");
|
|
1326
1339
|
dbBytes = statSync3(this.dbPath).size;
|
|
1327
1340
|
for (const suffix of ["-wal", "-shm"]) {
|
|
1328
1341
|
const p = this.dbPath + suffix;
|
|
1329
|
-
if (
|
|
1342
|
+
if (existsSync4(p)) dbBytes += statSync3(p).size;
|
|
1330
1343
|
}
|
|
1331
1344
|
} catch {
|
|
1332
1345
|
}
|
|
@@ -1359,15 +1372,15 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
|
|
|
1359
1372
|
"SELECT * FROM tool_calls WHERE id <= ? ORDER BY id"
|
|
1360
1373
|
).all(cutoff.id);
|
|
1361
1374
|
if (rows.length === 0) return { archived: 0, file: "" };
|
|
1362
|
-
const { writeFileSync:
|
|
1363
|
-
const { dirname:
|
|
1375
|
+
const { writeFileSync: writeFileSync4, mkdirSync: mkdirSync5, existsSync: existsSync4 } = require("fs");
|
|
1376
|
+
const { dirname: dirname5, join: join5 } = require("path");
|
|
1364
1377
|
const { gzipSync } = require("zlib");
|
|
1365
|
-
const archiveDir =
|
|
1366
|
-
if (!
|
|
1378
|
+
const archiveDir = join5(dirname5(this.dbPath), "archive");
|
|
1379
|
+
if (!existsSync4(archiveDir)) mkdirSync5(archiveDir, { recursive: true, mode: 448 });
|
|
1367
1380
|
const stamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-");
|
|
1368
|
-
const file =
|
|
1381
|
+
const file = join5(archiveDir, `tool_calls-${stamp}.jsonl.gz`);
|
|
1369
1382
|
const payload = rows.map((r) => JSON.stringify(r)).join("\n") + "\n";
|
|
1370
|
-
|
|
1383
|
+
writeFileSync4(file, gzipSync(Buffer.from(payload, "utf-8")), { mode: 384 });
|
|
1371
1384
|
const deleted = this.db.transaction(() => {
|
|
1372
1385
|
const res = this.db.prepare("DELETE FROM tool_calls WHERE id <= ?").run(cutoff.id);
|
|
1373
1386
|
this.db.prepare(`
|
|
@@ -1473,14 +1486,14 @@ function getDb(dbPath) {
|
|
|
1473
1486
|
if (rawDb) return rawDb;
|
|
1474
1487
|
try {
|
|
1475
1488
|
const Database = require("better-sqlite3");
|
|
1476
|
-
const { existsSync:
|
|
1477
|
-
const { join:
|
|
1489
|
+
const { existsSync: existsSync4, mkdirSync: mkdirSync5 } = require("fs");
|
|
1490
|
+
const { join: join5, dirname: dirname5 } = require("path");
|
|
1478
1491
|
const home = process.env.HOME || process.env.USERPROFILE || "/tmp";
|
|
1479
|
-
const xdgData = process.env.XDG_DATA_HOME ||
|
|
1480
|
-
const defaultPath =
|
|
1492
|
+
const xdgData = process.env.XDG_DATA_HOME || join5(home, ".local", "share");
|
|
1493
|
+
const defaultPath = join5(xdgData, "agent_gate", "agent_gate_ts.db");
|
|
1481
1494
|
const path = dbPath || defaultPath;
|
|
1482
|
-
const dir =
|
|
1483
|
-
if (!
|
|
1495
|
+
const dir = dirname5(path);
|
|
1496
|
+
if (!existsSync4(dir)) mkdirSync5(dir, { recursive: true });
|
|
1484
1497
|
rawDb = new Database(path);
|
|
1485
1498
|
rawDb.pragma("journal_mode = WAL");
|
|
1486
1499
|
return rawDb;
|
|
@@ -1523,6 +1536,46 @@ var init_db = __esm({
|
|
|
1523
1536
|
}
|
|
1524
1537
|
});
|
|
1525
1538
|
|
|
1539
|
+
// src/shared/instance-id.ts
|
|
1540
|
+
function getInstanceId() {
|
|
1541
|
+
if (cached) return cached;
|
|
1542
|
+
const env2 = (process.env.AGENT_GATE_INSTANCE_ID || "").trim();
|
|
1543
|
+
if (env2) {
|
|
1544
|
+
cached = env2;
|
|
1545
|
+
return cached;
|
|
1546
|
+
}
|
|
1547
|
+
const filePath = process.env.AGENT_GATE_INSTANCE_ID_PATH || (0, import_path4.join)((0, import_os.homedir)(), ".config", "agentgate", "instance_id");
|
|
1548
|
+
try {
|
|
1549
|
+
if ((0, import_fs4.existsSync)(filePath)) {
|
|
1550
|
+
const v2 = (0, import_fs4.readFileSync)(filePath, "utf-8").trim();
|
|
1551
|
+
if (v2) {
|
|
1552
|
+
cached = v2;
|
|
1553
|
+
return cached;
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
const v = (0, import_crypto2.randomUUID)();
|
|
1557
|
+
(0, import_fs4.mkdirSync)((0, import_path4.dirname)(filePath), { recursive: true });
|
|
1558
|
+
(0, import_fs4.writeFileSync)(filePath, v + "\n", { mode: 384 });
|
|
1559
|
+
cached = v;
|
|
1560
|
+
return cached;
|
|
1561
|
+
} catch {
|
|
1562
|
+
cached = `ephemeral-${(0, import_crypto2.randomUUID)()}`;
|
|
1563
|
+
return cached;
|
|
1564
|
+
}
|
|
1565
|
+
}
|
|
1566
|
+
var import_fs4, import_os, import_path4, import_crypto2, cached;
|
|
1567
|
+
var init_instance_id = __esm({
|
|
1568
|
+
"src/shared/instance-id.ts"() {
|
|
1569
|
+
"use strict";
|
|
1570
|
+
init_cjs_shims();
|
|
1571
|
+
import_fs4 = require("fs");
|
|
1572
|
+
import_os = require("os");
|
|
1573
|
+
import_path4 = require("path");
|
|
1574
|
+
import_crypto2 = require("crypto");
|
|
1575
|
+
cached = "";
|
|
1576
|
+
}
|
|
1577
|
+
});
|
|
1578
|
+
|
|
1526
1579
|
// src/shared/multi-session.ts
|
|
1527
1580
|
function isMultiSessionEnabled() {
|
|
1528
1581
|
const raw = (process.env.AGENT_GATE_MULTI_SESSION ?? "").trim().toLowerCase();
|
|
@@ -1569,6 +1622,9 @@ function buildSessionHistory(queryToolCalls2, sessionId, listAgentTypes) {
|
|
|
1569
1622
|
return [...bySession.entries()].slice(0, maxSessions).map(([sid, actions]) => ({
|
|
1570
1623
|
session_id: sid,
|
|
1571
1624
|
...sessionAgent.get(sid) ? { agent_id: sessionAgent.get(sid) } : {},
|
|
1625
|
+
// AgentGate 自己的存储属于本安装实例;带上实例 ID 让 AgentMoss 在
|
|
1626
|
+
// 展示层按「同实例不隐藏」处理(否则用户自己的历史会话也会被脱敏)。
|
|
1627
|
+
instance_id: getInstanceId(),
|
|
1572
1628
|
action_history: actions.slice(0, DEFAULT_ACTIONS_PER_SESSION)
|
|
1573
1629
|
}));
|
|
1574
1630
|
} catch {
|
|
@@ -1583,6 +1639,7 @@ var init_multi_session = __esm({
|
|
|
1583
1639
|
"src/shared/multi-session.ts"() {
|
|
1584
1640
|
"use strict";
|
|
1585
1641
|
init_cjs_shims();
|
|
1642
|
+
init_instance_id();
|
|
1586
1643
|
DEFAULT_HISTORY_LIMIT = 500;
|
|
1587
1644
|
DEFAULT_MAX_SESSIONS = 5;
|
|
1588
1645
|
DEFAULT_ACTIONS_PER_SESSION = 100;
|
|
@@ -1710,7 +1767,7 @@ var init_version = __esm({
|
|
|
1710
1767
|
"src/version.ts"() {
|
|
1711
1768
|
"use strict";
|
|
1712
1769
|
init_cjs_shims();
|
|
1713
|
-
_version = true ? "0.9.
|
|
1770
|
+
_version = true ? "0.9.3" : (() => {
|
|
1714
1771
|
try {
|
|
1715
1772
|
const require2 = createRequire(importMetaUrl);
|
|
1716
1773
|
const pkg = require2("../package.json");
|
|
@@ -1735,6 +1792,8 @@ var init_models = __esm({
|
|
|
1735
1792
|
agent_type: import_zod.z.string().describe("Agent identifier, e.g. opendesk, claude-code"),
|
|
1736
1793
|
agent_version: import_zod.z.string().default("0.1.0"),
|
|
1737
1794
|
session_id: import_zod.z.string().describe("Session unique identifier"),
|
|
1795
|
+
/** 安装级实例 ID(仅展示层脱敏用;缺省由 AgentGate 自动生成)。 */
|
|
1796
|
+
instance_id: import_zod.z.string().optional(),
|
|
1738
1797
|
tool_name: import_zod.z.string().optional().describe("Tool name, e.g. bash, file_write"),
|
|
1739
1798
|
command: import_zod.z.string().optional().describe("Tool command or description"),
|
|
1740
1799
|
a_next: import_zod.z.object({
|
|
@@ -1777,6 +1836,8 @@ var init_models = __esm({
|
|
|
1777
1836
|
violated_layers: import_zod.z.array(import_zod.z.string()).default([]),
|
|
1778
1837
|
confidence: import_zod.z.number().int().min(0).max(100).default(100),
|
|
1779
1838
|
analysis_duration_ms: import_zod.z.number().default(0),
|
|
1839
|
+
// 结构化攻击链图(组合式拦截时非空;前端弹窗画图用)
|
|
1840
|
+
violation_graph: import_zod.z.any().nullable().optional(),
|
|
1780
1841
|
oversize: import_zod.z.boolean().optional().describe("\u672C\u5730\u5B58\u50A8\u662F\u5426\u8D85\u8FC7\u6E05\u7406\u9608\u503C\uFF08agg \u7528\uFF09"),
|
|
1781
1842
|
thresholdMB: import_zod.z.number().optional().describe("\u5B58\u50A8\u6E05\u7406\u9608\u503C MB\uFF08agg \u7528\uFF09"),
|
|
1782
1843
|
collected_context: import_zod.z.object({
|
|
@@ -1849,6 +1910,8 @@ function normalize(input, systemContext) {
|
|
|
1849
1910
|
// 历史上只在 metadata.agent_type 里带,AgentMoss 读顶层 req.agent_id → 空 →
|
|
1850
1911
|
// 定制规则从未生效(OpenDesk memory-* 操作走 L3 耗时 1~137s)。
|
|
1851
1912
|
agent_id: input.agent_type || "",
|
|
1913
|
+
// 实例 ID:仅用于 AgentMoss 展示层脱敏(同实例不隐藏),不参与判定。
|
|
1914
|
+
instance_id: input.instance_id || getInstanceId(),
|
|
1852
1915
|
metadata: {
|
|
1853
1916
|
...input.metadata,
|
|
1854
1917
|
agent_type: input.agent_type,
|
|
@@ -1867,6 +1930,7 @@ var init_normalizer = __esm({
|
|
|
1867
1930
|
"use strict";
|
|
1868
1931
|
init_cjs_shims();
|
|
1869
1932
|
init_multi_session();
|
|
1933
|
+
init_instance_id();
|
|
1870
1934
|
}
|
|
1871
1935
|
});
|
|
1872
1936
|
|
|
@@ -1875,22 +1939,22 @@ async function collectSystemInfo() {
|
|
|
1875
1939
|
try {
|
|
1876
1940
|
return {
|
|
1877
1941
|
system: process.platform,
|
|
1878
|
-
distribution: `${
|
|
1879
|
-
hostname:
|
|
1880
|
-
arch:
|
|
1881
|
-
cpu_count:
|
|
1882
|
-
memory_total_mb: Math.round(
|
|
1942
|
+
distribution: `${import_os2.default.type()} ${import_os2.default.release()}`,
|
|
1943
|
+
hostname: import_os2.default.hostname(),
|
|
1944
|
+
arch: import_os2.default.arch(),
|
|
1945
|
+
cpu_count: import_os2.default.cpus().length,
|
|
1946
|
+
memory_total_mb: Math.round(import_os2.default.totalmem() / (1024 * 1024))
|
|
1883
1947
|
};
|
|
1884
1948
|
} catch {
|
|
1885
1949
|
return { system: "unknown" };
|
|
1886
1950
|
}
|
|
1887
1951
|
}
|
|
1888
|
-
var
|
|
1952
|
+
var import_os2;
|
|
1889
1953
|
var init_system_info = __esm({
|
|
1890
1954
|
"src/shared/collector/system-info.ts"() {
|
|
1891
1955
|
"use strict";
|
|
1892
1956
|
init_cjs_shims();
|
|
1893
|
-
|
|
1957
|
+
import_os2 = __toESM(require("os"));
|
|
1894
1958
|
}
|
|
1895
1959
|
});
|
|
1896
1960
|
|
|
@@ -1917,7 +1981,7 @@ var init_process_info = __esm({
|
|
|
1917
1981
|
// src/shared/collector/user-info.ts
|
|
1918
1982
|
async function collectUserInfo() {
|
|
1919
1983
|
try {
|
|
1920
|
-
const userInfo =
|
|
1984
|
+
const userInfo = import_os3.default.userInfo();
|
|
1921
1985
|
return {
|
|
1922
1986
|
uid: userInfo.uid,
|
|
1923
1987
|
gid: userInfo.gid,
|
|
@@ -1928,12 +1992,12 @@ async function collectUserInfo() {
|
|
|
1928
1992
|
return { uid: -1 };
|
|
1929
1993
|
}
|
|
1930
1994
|
}
|
|
1931
|
-
var
|
|
1995
|
+
var import_os3;
|
|
1932
1996
|
var init_user_info = __esm({
|
|
1933
1997
|
"src/shared/collector/user-info.ts"() {
|
|
1934
1998
|
"use strict";
|
|
1935
1999
|
init_cjs_shims();
|
|
1936
|
-
|
|
2000
|
+
import_os3 = __toESM(require("os"));
|
|
1937
2001
|
}
|
|
1938
2002
|
});
|
|
1939
2003
|
|
|
@@ -2086,10 +2150,10 @@ async function analyzeRoute(c) {
|
|
|
2086
2150
|
const toolName = input.tool_name || input.a_next?.action_type || "unknown";
|
|
2087
2151
|
const command = input.command || input.a_next?.action_detail || "";
|
|
2088
2152
|
const key = cacheKey(input.session_id, input.cwd, command);
|
|
2089
|
-
const
|
|
2090
|
-
if (
|
|
2153
|
+
const cached2 = getCached(key);
|
|
2154
|
+
if (cached2) {
|
|
2091
2155
|
return c.json({
|
|
2092
|
-
...
|
|
2156
|
+
...cached2,
|
|
2093
2157
|
analysis_duration_ms: 0,
|
|
2094
2158
|
collected_context: {},
|
|
2095
2159
|
...storageOversizeInfo()
|
|
@@ -2127,7 +2191,9 @@ async function analyzeRoute(c) {
|
|
|
2127
2191
|
violated_layers: JSON.stringify(mossResult.violated_layers),
|
|
2128
2192
|
reason: mossResult.reason,
|
|
2129
2193
|
confidence: mossResult.confidence,
|
|
2130
|
-
analysis_duration_ms: performance.now() - t0
|
|
2194
|
+
analysis_duration_ms: performance.now() - t0,
|
|
2195
|
+
// 结构化攻击链图(组合式拦截才有;前端弹窗画图用)
|
|
2196
|
+
violation_graph: JSON.stringify(mossResult.violation_graph || null)
|
|
2131
2197
|
});
|
|
2132
2198
|
setCache(key, mossResult);
|
|
2133
2199
|
if (isMultiSessionEnabled()) {
|
|
@@ -2151,6 +2217,7 @@ async function analyzeRoute(c) {
|
|
|
2151
2217
|
violated_layers: mossResult.violated_layers,
|
|
2152
2218
|
confidence: mossResult.confidence,
|
|
2153
2219
|
analysis_duration_ms: Math.round(durationMs * 10) / 10,
|
|
2220
|
+
violation_graph: mossResult.violation_graph ?? null,
|
|
2154
2221
|
...storageOversizeInfo(),
|
|
2155
2222
|
collected_context: {
|
|
2156
2223
|
os: context.os,
|
|
@@ -2388,17 +2455,17 @@ async function dashboardRoute(c) {
|
|
|
2388
2455
|
return c.html(UNAUTHORIZED_HTML, 401, DASHBOARD_SECURITY_HEADERS);
|
|
2389
2456
|
}
|
|
2390
2457
|
}
|
|
2391
|
-
const { readFileSync:
|
|
2392
|
-
const { resolve, dirname:
|
|
2458
|
+
const { readFileSync: readFileSync4, existsSync: existsSync4 } = await import("fs");
|
|
2459
|
+
const { resolve, dirname: dirname5 } = await import("path");
|
|
2393
2460
|
const { fileURLToPath } = await import("url");
|
|
2394
|
-
const __dirname =
|
|
2461
|
+
const __dirname = dirname5(fileURLToPath(importMetaUrl));
|
|
2395
2462
|
const paths = [
|
|
2396
2463
|
resolve(__dirname, "dashboard.html"),
|
|
2397
2464
|
resolve(__dirname, "../dashboard.html"),
|
|
2398
2465
|
resolve(__dirname, "../src/v1/dashboard.html")
|
|
2399
2466
|
];
|
|
2400
|
-
const htmlPath = paths.find((p) =>
|
|
2401
|
-
const html =
|
|
2467
|
+
const htmlPath = paths.find((p) => existsSync4(p)) || paths[0];
|
|
2468
|
+
const html = readFileSync4(htmlPath, "utf-8");
|
|
2402
2469
|
return c.html(html, 200, DASHBOARD_SECURITY_HEADERS);
|
|
2403
2470
|
}
|
|
2404
2471
|
var import_hono, import_cors, import_logger, import_cookie, PUBLIC_PAGE_PATHS, UNAUTHORIZED_HTML, AGENT_FORBIDDEN_HTML;
|