affaan-m/ECC · security-review · SKILL.md

安全审查

在添加身份验证、处理用户输入、处理密钥、创建 API 端点或实现支付/敏感功能时使用此技能。提供全面的安全清单和模式。

★ 307688 质量 71.0 许可证 MIT 后端 generic

这是公开 Agent Skill 的中文说明,由采集翻译或编辑润色,完整内容以上游 SKILL.md 为准。适用于 Cursor / Claude Code / Codex / Gemini。 打开上游 SKILL.md

安装

通用(npx skills)

npx skills add affaan-m/ECC -s security-review

Cursor

把该 Skill 复制到项目的 .cursor/skills/security-review/ 目录

Claude Code

/plugin marketplace add affaan-m/ECC

中文说明

# 安全性審查技能

此技能確保所有程式碼遵循安全性最佳實務並識別潛在漏洞。

## 何時啟用

- 實作認證或授權
- 處理使用者輸入或檔案上傳
- 建立新的 API 端點
- 處理密鑰或憑證
- 實作支付功能
- 儲存或傳輸敏感資料
- 整合第三方 API

## 安全性檢查清單

### 1. 密鑰管理

#### FAIL: 絕不這樣做
```typescript
const apiKey = "sk-proj-xxxxx"  // 寫死的密鑰
const dbPassword = "password123" // 在原始碼中
```

#### PASS: 總是這樣做
```typescript
const apiKey = process.env.OPENAI_API_KEY
const dbUrl = process.env.DATABASE_URL

// 驗證密鑰存在
if (!apiKey) {
  throw new Error('OPENAI_API_KEY not configured')
}
```

#### 驗證步驟
- [ ] 無寫死的 API 金鑰、Token 或密碼
- [ ] 所有密鑰在環境變數中
- [ ] `.env.local` 在 .gitignore 中
- [ ] git 歷史中無密鑰
- [ ] 生產密鑰在託管平台(Vercel、Railway)中

### 2. 輸入驗證

#### 總是驗證使用者輸入
```typescript
import { z } from 'zod'

// 定義驗證 schema
const CreateUserSchema = z.object({
  email: z.string().email(),
  name: z.string().min(1).max(100),
  age: z.number().int().min(0).max(150)
})

// 處理前驗證
export async function createUser(input: unknown) {
  try {
    const validated = CreateUserSchema.parse(input)
    return await db.users.create(validated)
  } catch (error) {
    if (error instanceof z.ZodError) {
      return { success: false, errors: error.errors }
    }
    throw error
  }
}
```

#### 檔案上傳驗證
```typescript
function validateFileUpload(file: File) {
  // 大小檢查(最大 5MB)
  const maxSize = 5 * 1024 * 1024
  if (file.size > maxSize) {
    throw new Error('File too large (max 5MB)')
  }

  // 類型檢查
  const allowedTypes = ['image/jpeg', 'image/png', 'image/gif']
  if (!allowedTypes.includes(file.type)) {
    throw new Error('Invalid file type')
  }

  // 副檔名檢查
  const allowedExtensions = ['.jpg', '.jpeg', '.png', '.gif']
  const extension = file.name.toLowerCase().match(/\.[^.]+$/)?.[0]
  if (!extension || !allowedExtensions.includes(extension)) {
    throw new Error('Invalid file extension')
  }

  return true
}
```

#### 驗證步驟
- [ ] 所有使用者輸入以 schema 驗證
- [ ] 檔案上傳受限(大小、類型、副檔名)
- [ ] 查詢中不直接使用使用者輸入
- [ ] 白名單驗證(非黑名單)
- [ ] 錯誤訊息不洩露敏感資訊

### 3. SQL 注入預防

#### FAIL: 絕不串接 SQL
```typescript
// 危險 - SQL 注入漏洞
const query = `SELECT * FROM

返回目录