Spaces:
Paused
Paused
File size: 4,446 Bytes
4e7644b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | import { Modal } from "@agentscope-ai/design";
import React from "react";
import type {
SecurityScanErrorResponse,
BlockedSkillFinding,
BlockedSkillRecord,
SkillScannerConfig,
} from "../api/modules/security";
import type { TFunction } from "i18next";
export function tryParseScanError(
error: unknown,
): SecurityScanErrorResponse | null {
if (!(error instanceof Error)) return null;
const msg = error.message || "";
const jsonStart = msg.indexOf("{");
if (jsonStart === -1) return null;
try {
const parsed = JSON.parse(msg.substring(jsonStart));
if (parsed?.type === "security_scan_failed") {
return parsed as SecurityScanErrorResponse;
}
} catch {
return null;
}
return null;
}
/** Cap long finding lists so modals stay readable; full history remains in alerts. */
const MAX_FINDINGS_IN_MODAL = 5;
function renderFindings(findings: BlockedSkillFinding[], t: TFunction) {
const total = findings.length;
const shown = findings.slice(0, MAX_FINDINGS_IN_MODAL);
const moreCount = total - shown.length;
return React.createElement(
"div",
{ style: { maxHeight: 300, overflow: "auto", marginTop: 8 } },
shown.map((f, i) =>
React.createElement(
"div",
{
key: i,
style: {
padding: "8px 12px",
marginBottom: 4,
background: "#fafafa",
borderRadius: 6,
border: "1px solid #f0f0f0",
},
},
React.createElement(
"strong",
{ style: { marginBottom: 4, display: "block" } },
f.title,
),
React.createElement(
"div",
{ style: { fontSize: 12, color: "#666" } },
f.file_path + (f.line_number ? `:${f.line_number}` : ""),
),
f.description &&
React.createElement(
"div",
{ style: { fontSize: 12, color: "#999", marginTop: 2 } },
f.description,
),
),
),
moreCount > 0 &&
React.createElement(
"div",
{
key: "more",
style: {
fontSize: 12,
color: "#888",
marginTop: 8,
padding: "8px 12px",
},
},
t("security.skillScanner.scanError.moreFindings", { count: moreCount }),
),
);
}
export function showScanErrorModal(
scanError: SecurityScanErrorResponse,
t: TFunction,
) {
const findings = scanError.findings || [];
Modal.error({
title: t("security.skillScanner.scanError.title"),
width: 640,
content: React.createElement(
"div",
null,
React.createElement(
"p",
null,
t("security.skillScanner.scanError.description"),
),
renderFindings(findings, t),
),
});
}
export function showScanWarnModal(
findings: BlockedSkillFinding[],
t: TFunction,
) {
Modal.warning({
title: t("security.skillScanner.scanError.title"),
width: 640,
content: React.createElement(
"div",
null,
React.createElement(
"p",
null,
t("security.skillScanner.scanError.warnDescription"),
),
renderFindings(findings, t),
),
});
}
/**
* Check an error for a scan failure, show the modal if found, and return
* whether it was handled.
*/
export function handleScanError(error: unknown, t: TFunction): boolean {
const scanError = tryParseScanError(error);
if (scanError) {
showScanErrorModal(scanError, t);
return true;
}
return false;
}
/**
* After a successful operation, check if the scanner recorded any
* warn-mode findings and show a warning modal if so.
*/
export async function checkScanWarnings(
skillName: string,
fetchAlerts: () => Promise<BlockedSkillRecord[]>,
fetchScannerCfg: () => Promise<SkillScannerConfig>,
t: TFunction,
): Promise<void> {
try {
const [alerts, scannerCfg] = await Promise.all([
fetchAlerts(),
fetchScannerCfg(),
]);
if (!alerts.length) return;
if (
scannerCfg?.whitelist?.some(
(w: { skill_name: string }) => w.skill_name === skillName,
)
) {
return;
}
const latestForSkill = alerts
.filter((a) => a.skill_name === skillName && a.action === "warned")
.pop();
if (!latestForSkill) return;
showScanWarnModal(latestForSkill.findings || [], t);
} catch {
// best-effort; don't break the caller on failure
}
}
|