用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/killvxk/cybersecurity-skills-zh --skill implementing-api-schema-validation-security命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
正在显示 SKILL.md
| name | implementing-api-schema-validation-security |
| description | 使用OpenAPI规范和JSON Schema实现API Schema验证,强制执行输入/输出契约,防止注入、数据泄露和批量赋值攻击。 |
| domain | cybersecurity |
| subdomain | api-security |
| tags | ["api-security","schema-validation","openapi","json-schema","input-validation","data-leakage-prevention","mass-assignment","api-gateway"] |
| version | 1.0 |
| author | mahipal |
| license | Apache-2.0 |
API Schema验证(Schema Validation)确保通过API交换的所有数据符合OpenAPI规范(OAS)或JSON Schema文档中预定义的结构。这可以防止注入攻击(SQLi、XSS、XXE),通过拒绝未知属性来阻断批量赋值(Mass Assignment),通过验证响应Schema来防止数据泄露(Data Leakage),并确保所有API交互的类型安全。Schema验证在API网关层面(运行时强制执行)和开发阶段(安全左移)均可运行。
openapi: 3.1.0
info:
title: Secure E-Commerce API
version: 2.0.0
servers:
- url: https://api.example.com/v2
description: Production (HTTPS enforced)
security:
- OAuth2:
- read:products
- write:orders
paths:
/products:
post:
operationId: createProduct
security:
- OAuth2: [write:products]
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/ProductCreate'
responses:
'201':
description: Product created
content:
application/json:
schema:
$ref: '#/components/schemas/Product'
'400':
[, , ]
[, , , , ]
[, , ]
[, ]
[]
"""FastAPI API Schema验证中间件
对所有请求和响应负载强制执行严格的Schema验证,
防止注入、批量赋值和数据泄露攻击。
"""
from fastapi import FastAPI, Request, Response, HTTPException
from fastapi.middleware import Middleware
from pydantic import BaseModel, Field, field_validator, ConfigDict
from typing import List, Optional
import re
import json
from starlette.middleware.base import BaseHTTPMiddleware
app = FastAPI()
# 带安全约束的严格Pydantic模型
class ProductCreate(BaseModel):
model_config = ConfigDict(extra='forbid') # 拒绝未知字段(防批量赋值)
name: str = Field(min_length=1, max_length=200, pattern=r'^[a-zA-Z0-9\s\-\.]+$')
description: Optional[str] = Field(default=None, max_length=2000)
price: float = Field(gt=0, le=999999.99)
category: str = Field(pattern=r'^(electronics|clothing|food|furniture|other)$')
tags: Optional[List[str]] = Field(default=None, max_length=10)
@field_validator('name')
@classmethod
def sanitize_name(cls, v):
# 通过HTML实体防止XSS
dangerous_patterns = [, , , ]
lower_v = v.lower()
pattern dangerous_patterns:
pattern lower_v:
ValueError()
v
():
v :
v
sql_patterns = [
,
]
pattern sql_patterns:
re.search(pattern, v, re.IGNORECASE):
ValueError()
v
():
v :
v
(v) > :
ValueError()
tag v:
re.(, tag) (tag) > :
ValueError()
v
():
model_config = ConfigDict(extra=)
:
name:
price:
category:
tags: [] = []
created_at:
():
SCHEMA_MAP = {
: {
: {: ProductResponse},
: {: ProductResponse},
}
}
():
response = call_next(request)
content_type = response.headers.get(, )
content_type:
response
path = request.url.path
method = request.method
route_config = .SCHEMA_MAP.get(path, {}).get(method)
route_config:
response
body =
chunk response.body_iterator:
body += chunk
:
data = json.loads(body)
model = route_config[]
(data, ):
item data:
model.model_validate(item)
:
model.model_validate(data)
Exception e:
()
Response(
content=json.dumps({: }),
status_code=,
media_type=
)
Response(
content=body,
status_code=response.status_code,
headers=(response.headers),
media_type=response.media_type
)
app.add_middleware(ResponseValidationMiddleware)
():
# 上传OpenAPI Schema到Cloudflare API Shield
curl -X POST "https://api.cloudflare.com/client/v4/zones/{zone_id}/api_gateway/user_schemas" \
-H "Authorization: Bearer ${CF_API_TOKEN}" \
-H "Content-Type: multipart/form-data" \
-F "file=@openapi.yaml" \
-F "kind=openapi_v3"
# 以拦截模式启用Schema验证
curl -X PATCH "https://api.cloudflare.com/client/v4/zones/{zone_id}/api_gateway/settings/schema_validation" \
-H "Authorization: Bearer ${CF_API_TOKEN}" \
-H "Content-Type: application/json" \
-d '{
"validation_default_mitigation_action": "block",
"validation_override_mitigation_action": null
}'
# GitHub Actions工作流 - CI中的Schema验证
name: API Schema Security Check
on:
pull_request:
paths: ['api/**', 'openapi/**']
jobs:
schema-security:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: 验证OpenAPI Schema
run: |
npm install -g @stoplight/spectral-cli
spectral lint openapi.yaml --ruleset .spectral-security.yaml
- name: 检查安全反模式
run: |
python3 scripts/schema_security_check.py openapi.yaml
- name: 运行契约测试
run: |
npm install -g dredd
dredd openapi.yaml http://localhost:3000 --hookfiles=./test/hooks.js
| 反模式 | 风险 | 修复方案 |
|---|---|---|
additionalProperties: true 或缺失 | 批量赋值 | 设置 additionalProperties: false |
字符串字段无 maxLength | 缓冲区溢出、DoS | 添加适当的 maxLength 约束 |
字符串字段无 pattern | 注入攻击 | 添加正则模式限制输入 |
固定值字段无 enum | 处理意外输入 | 对已知值字段使用 enum |
format: password 无TLS | 凭据暴露 | 强制仅使用HTTPS服务器URL |
| 缺少错误响应Schema | 信息泄露 | 定义所有4xx/5xx响应Schema |
请求体中包含 readOnly 字段 | 数据篡改 | 服务端强制执行 readOnly |
基于 SOC 职业分类