@ claude- flow/ 安全
Claude Flow V3 - CVE修正,输入验证,路径安全,以及安全证书管理的综合安全模块.
特征
- CVE 补救 - 修理CVE-2(微软密码哈兴)、CVE-3(硬码证书)、HH-1(指令注射)、HH-2(Path Traversal)
- 密码显示 - 使用可配置弹(建议12发以上)进行安全、基于bcrypt的密码散开
- 证书生成 - 加密安全证书和API密钥生成
- 安全命令执行 - 基于允许列表的命令执行,防止注射攻击
- 路径验证 - 防止道路交叉和连锁攻击
- 输入验证 - 所有输入类型的基于Zod的系统验证
- 脚本生成 - 使用 HMAC 签名创建安全标志
安装
npm install @claude-flow/security
独立使用(没有 Ruflo CLI)
这是一个平坦的图书馆——每一个原始的(PasswordHasher, (中文(简体) ),
PathValidator, (中文(简体) ), SafeExecutor, (中文(简体) ), InputValidator, (中文(简体) ), CredentialGenerator(中文(简体) )
可独立导入。 没有CLI,没有MCP服务器,没有守护进程. 这个
原始人关闭了CVE-2(弱散列),CVE-3(默认证书),
HH-1(指令注射)和HH-2(路径转弯)——放下
输入任何需要在系统边界验证的节点应用程序.
道具 — 验证输入, 散列密码, 生成 API 密钥
// recipe.mjs
import {
InputValidator,
EmailSchema,
PasswordHasher,
createCredentialGenerator,
} from '@claude-flow/security';
// 1. Validate user input at the boundary (Zod schemas, no surprises)
const email = InputValidator.validate(EmailSchema, '[email protected]');
// ↳ throws if invalid; returns the typed value otherwise
// 2. Hash a password with bcrypt at the recommended cost (CVE-2)
// Default policy: ≥8 chars, ≥1 upper, ≥1 lower, ≥1 digit.
// Tune with new PasswordHasher({ rounds, requireUppercase, ... }).
const hasher = new PasswordHasher({ rounds: 12 });
const hash = await hasher.hash('CorrectHorseBatteryStaple9');
const ok = await hasher.verify('CorrectHorseBatteryStaple9', hash); // true
// 3. Generate a high-entropy API key (CVE-3) — uses crypto.randomBytes
// under the hood and refuses to emit keys below 32 bytes of entropy.
const creds = createCredentialGenerator();
const apiKey = creds.generateApiKey('ck_live_');
console.log(apiKey.keyId, apiKey.key.slice(0, 16) + '…');
// → 7e2b1a9c-… ck_live_xX9aQ…
道具 — 拒绝命令注射+路径转弯
import {
createDevelopmentExecutor,
createProjectPathValidator,
} from '@claude-flow/security';
// SafeExecutor — only the allow-listed commands ever run (HIGH-1)
const exec = createDevelopmentExecutor({ projectRoot: process.cwd() });
const { stdout } = await exec.execute('git', ['status', '--porcelain']);
// PathValidator — refuses traversal and symlinks out of the project (HIGH-2)
const paths = createProjectPathValidator(process.cwd());
const result = await paths.validate('../../etc/passwd');
if (!result.isValid) console.log('blocked:', result.errors.join('; '));
// → blocked: Path traversal pattern detected
快速启动
import { createSecurityModule } from '@claude-flow/security';
// Create a complete security module
const security = createSecurityModule({
projectRoot: '/workspaces/project',
hmacSecret: process.env.HMAC_SECRET!,
bcryptRounds: 12,
allowedCommands: ['git', 'npm', 'node']
});
// Hash a password
const hash = await security.passwordHasher.hash('userPassword123');
// Validate a path
const pathResult = await security.pathValidator.validate('/workspaces/project/src/file.ts');
// Execute command safely
const output = await security.safeExecutor.execute('git', ['status']);
// Generate secure credentials
const creds = await security.credentialGenerator.generate();
API 参考
密码搜索( CVE-2 Fix)
import { PasswordHasher, createPasswordHasher } from '@claude-flow/security';
const hasher = createPasswordHasher({ rounds: 12 });
// Hash password
const hash = await hasher.hash('password');
// Verify password
const isValid = await hasher.verify('password', hash);
// Check if hash needs rehashing
const needsRehash = hasher.needsRehash(hash);
证书生成( CVE-3 Fix)
import { CredentialGenerator, generateCredentials } from '@claude-flow/security';
const generator = new CredentialGenerator();
// Generate API key
const apiKey = await generator.generateApiKey({
prefix: 'cf',
length: 32
});
// Generate complete credentials
const creds = generateCredentials({
includeApiKey: true,
includeSecret: true
});
安全命令执行(HIGH-1 Fix)
import { SafeExecutor, createDevelopmentExecutor } from '@claude-flow/security';
const executor = createDevelopmentExecutor();
// Execute allowed command
const result = await executor.execute('git', ['status']);
// With timeout
const result2 = await executor.execute('npm', ['install'], {
timeout: 60000,
cwd: '/workspaces/project'
});
路径验证( HIGH-2 Fix)
import { PathValidator, createProjectPathValidator } from '@claude-flow/security';
const validator = createProjectPathValidator('/workspaces/project');
// Validate path
const result = await validator.validate('../../../etc/passwd');
// { valid: false, reason: 'Path traversal detected' }
// Safe path
const result2 = await validator.validate('/workspaces/project/src/index.ts');
// { valid: true, normalized: '/workspaces/project/src/index.ts' }
输入验证
import {
InputValidator,
SafeStringSchema,
EmailSchema,
PasswordSchema,
SpawnAgentSchema
} from '@claude-flow/security';
// Validate email
const email = EmailSchema.parse('[email protected]');
// Validate password
const password = PasswordSchema.parse('SecurePass123!');
// Validate agent spawn request
const agentRequest = SpawnAgentSchema.parse({
type: 'coder',
name: 'code-agent-1'
});
// Sanitize HTML
import { sanitizeHtml } from '@claude-flow/security';
const safe = sanitizeHtml('<script>alert("xss")</script>Hello');
// 'Hello'
脚本生成
import { TokenGenerator, quickGenerate } from '@claude-flow/security';
const generator = new TokenGenerator({
hmacSecret: process.env.HMAC_SECRET!
});
// Generate signed token
const token = await generator.generate({
type: 'session',
expiresIn: 3600
});
// Verify token
const verified = await generator.verify(token);
// Quick generation
const sessionToken = quickGenerate.sessionToken();
const verificationCode = quickGenerate.verificationCode();
安全常数
import {
MIN_BCRYPT_ROUNDS, // 12
MAX_BCRYPT_ROUNDS, // 14
MIN_PASSWORD_LENGTH, // 8
MAX_PASSWORD_LENGTH, // 72 (bcrypt limit)
DEFAULT_TOKEN_EXPIRATION, // 3600 (1 hour)
DEFAULT_SESSION_EXPIRATION // 86400 (24 hours)
} from '@claude-flow/security';
安保审计
import { auditSecurityConfig } from '@claude-flow/security';
const warnings = auditSecurityConfig({
bcryptRounds: 10,
hmacSecret: 'short'
});
// ['bcryptRounds (10) below recommended minimum (12)',
// 'hmacSecret should be at least 32 characters']
验证Schemas
| 司马 | 说明 |
|---|---|
SafeStringSchema | 带长度限制的基本安全字符串 |
IdentifierSchema | 数字标识符 |
FilenameSchema | 安全文件名 |
EmailSchema | 电子邮件地址 |
PasswordSchema | 安全密码 |
UUIDSchema | UUID v4 格式 |
HttpsUrlSchema | 仅限 HTTPS URL |
SemverSchema | 语义版本 |
PortSchema | 有效端口号 |
IPv4Schema | IPv4 地址 |
SpawnAgentSchema | 代理产卵请求 |
TaskInputSchema | 任务定义 |
SecurityConfigSchema | 安全配置 |
依赖关系
bcrypt- 密码散开zod- Schema验证
相关软件包
- @ claude- flow/ 分享 - 共有类型和公用事业
- @ claude- flow/ swarm (英语) - 沼泽协调(安全剂产卵)
许可证
麻省理工学院