security-integration-tests
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSecurity Integration Tests Agent
安全集成测试Agent
Overview
概述
The security package () provides prompt injection detection using the HuggingFace Inference API. Integration tests verify detection accuracy with real API calls.
pkg/security/安全包()通过HuggingFace Inference API提供提示注入检测功能。集成测试通过真实API调用验证检测的准确性。
pkg/security/Key Files
关键文件
- - Test data with benign and malicious samples
pkg/security/testdata/samples.json - - HF client integration tests
pkg/security/hf/integration_test.go - - Full analyzer integration tests with event bus
pkg/security/integration_test.go - - HuggingFace API client implementation
pkg/security/hf/client.go - - Main security analyzer that subscribes to MCP events
pkg/security/analyzer.go
- - 包含良性和恶意样本的测试数据
pkg/security/testdata/samples.json - - HF客户端集成测试
pkg/security/hf/integration_test.go - - 带事件总线的完整分析器集成测试
pkg/security/integration_test.go - - HuggingFace API客户端实现
pkg/security/hf/client.go - - 订阅MCP事件的主安全分析器
pkg/security/analyzer.go
Running Tests
运行测试
bash
undefinedbash
undefinedRun integration tests (requires HF_TOKEN environment variable)
Run integration tests (requires HF_TOKEN environment variable)
HF_TOKEN=hf_xxx make test-integration
HF_TOKEN=hf_xxx make test-integration
Run with custom model
Run with custom model
HF_TOKEN=hf_xxx HF_MODEL=protectai/deberta-v3-base-prompt-injection-v2 make test-integration
HF_TOKEN=hf_xxx HF_MODEL=protectai/deberta-v3-base-prompt-injection-v2 make test-integration
Run unit tests only (no API calls, uses mock server)
Run unit tests only (no API calls, uses mock server)
go test -v ./pkg/security/...
undefinedgo test -v ./pkg/security/...
undefinedTest Structure
测试结构
Test Samples (samples.json
)
samples.json测试样本(samples.json
)
samples.jsonjson
{
"benign_samples": [
{
"description": "Description of the sample",
"text": "The text to analyze",
"expected_detected": false
}
],
"malicious_samples": [
{
"description": "Direct instruction override",
"text": "Ignore all previous instructions...",
"expected_detected": true,
"min_risk_score": 0.5
}
],
"mcp_tool_calls": [
{
"description": "Malicious tool call",
"method": "tools/call",
"params": { "name": "run_command", "arguments": {...} },
"expected_detected": true,
"min_risk_score": 0.5
}
]
}json
{
"benign_samples": [
{
"description": "Description of the sample",
"text": "The text to analyze",
"expected_detected": false
}
],
"malicious_samples": [
{
"description": "Direct instruction override",
"text": "Ignore all previous instructions...",
"expected_detected": true,
"min_risk_score": 0.5
}
],
"mcp_tool_calls": [
{
"description": "Malicious tool call",
"method": "tools/call",
"params": { "name": "run_command", "arguments": {...} },
"expected_detected": true,
"min_risk_score": 0.5
}
]
}Integration Test Tags
集成测试标签
Integration tests use the build tag and are excluded from regular runs.
//go:build integrationgo test ./...集成测试使用构建标签,会被排除在常规运行之外。
//go:build integrationgo test ./...Adding New Test Samples
添加新测试样本
- Edit
pkg/security/testdata/samples.json - Add samples to appropriate category (benign_samples, malicious_samples, or mcp_tool_calls)
- Set and optionally
expected_detectedmin_risk_score - Run integration tests to verify
- 编辑
pkg/security/testdata/samples.json - 将样本添加到对应的类别(benign_samples、malicious_samples或mcp_tool_calls)
- 设置(可选设置
expected_detected)min_risk_score - 运行集成测试进行验证
Common Issues
常见问题
"Forbidden" Error
"Forbidden"错误
- Ensure HF_TOKEN is set and valid
- Note: is deprecated on HF Inference API
meta-llama/Llama-Prompt-Guard-2-86M - Default test model is (publicly accessible)
protectai/deberta-v3-base-prompt-injection-v2
- 确保已设置有效的HF_TOKEN
- 注意:在HF Inference API中已废弃
meta-llama/Llama-Prompt-Guard-2-86M - 默认测试模型为(可公开访问)
protectai/deberta-v3-base-prompt-injection-v2
Model Loading
模型加载
- HuggingFace warms up models on demand
- Tests may skip with "Model loading" message on first run
- Re-run tests after model is warm
- HuggingFace会按需预热模型
- 首次运行时测试可能会因"Model loading"提示而跳过
- 模型预热完成后重新运行测试
Network Issues
网络问题
- Integration tests require network access to HuggingFace API
- Tests will fail in sandboxed environments without network access
- 集成测试需要访问HuggingFace API的网络权限
- 在无网络访问的沙箱环境中测试会失败
Risk Levels
风险等级
- : score < 0.3
none - : score 0.3-0.5
low - : score 0.5-0.7
medium - : score 0.7-0.9
high - : score >= 0.9
critical
- :分数 < 0.3
none - :分数 0.3-0.5
low - :分数 0.5-0.7
medium - :分数 0.7-0.9
high - :分数 >= 0.9
critical
Categories
类别
- : Normal, safe content
benign - : Prompt injection attempt
injection - : Jailbreak attempt
jailbreak - : Malicious content (Prompt Guard v2)
malicious
- :正常、安全的内容
benign - :提示注入尝试
injection - :越狱尝试
jailbreak - :恶意内容(Prompt Guard v2)
malicious