К основному содержанию
скилл CX Codex

security-patterns · cloudai-x — скилл для Claude Code: что делает и как установить

Скилл security-patterns внедряет проверенные паттерны аутентификации, RBAC, хеширования паролей и защиты API в Claude Code.

MIT ★ 1 424 обновлён 2026-10-06

Установить в Claude Code · Через маркетплейс плагинов Все способы и платформы ↓
/plugin marketplace add CloudAI-X/claude-workflow-v2
/plugin install project-starter@claude-workflow
Открыть на GitHub

Что делает security-patterns

Скилл содержит чеклист из восьми шагов и шаблоны кода для Node.js и TypeScript. Он охватывает выпуск и валидацию JWT с жёсткой фиксацией алгоритма HS256, сессии на базе express-session с хранилищем в Redis, авторизацию OAuth 2.0 с PKCE, а также проверку API-ключей по SHA-256 хешу.

Для контроля доступа описаны модели ролей (RBAC) и разграничение прав на уровне конкретных ресурсов с защитой от массового переназначения полей через валидацию схем. Пароли хешируются через bcrypt с фактором стоимости 12, что исключает быстрый перебор. Токены обновления сохраняются исключительно в cookie с флагами httpOnly и secure, защищая сессию от XSS-атак.

Какие задачи решает

  • настроить авторизацию по JWT с ротацией refresh-токенов в httpOnly cookie
  • внедрить ролевой доступ (RBAC) и проверку прав на уровне отдельных ресурсов
  • хешировать пароли через bcrypt и защитить эндпоинты от атак по времени
  • реализовать безопасный флоу OAuth 2.0 с механизмом PKCE

Примеры запросов агенту

  • Создай middleware для проверки JWT с защитой от подмены алгоритма
  • Настрой RBAC и валидацию владельца для обновления записей в базе
  • Сделай аутентификацию по API-ключам с SHA-256 хешированием

Как установить security-patterns

1Через маркетплейс плагинов

/plugin marketplace add CloudAI-X/claude-workflow-v2
/plugin install project-starter@claude-workflow

Команда ставит плагин project-starter целиком, со всеми его скиллами

2Через Skills CLI (Vercel)

npx skills add CloudAI-X/claude-workflow-v2 --skill security-patterns -a claude-code

3Вручную: положить файлы

~/.claude/skills/security-patterns/SKILL.md

Папка скилла целиком; для одного проекта — .claude/skills/security-patterns/

По шагам, с командами для каждой платформы: Как установить скилл в Claude Code: пять способов

Вопросы о security-patterns

Как установить security-patterns в Claude Code?

Выполните команду npx skills add CloudAI-X/claude-workflow-v2 --skill security-patterns -a claude-code в терминале.

Что делает security-patterns?

Он направляет Claude Code по шагам безопасности: от выбора архитектуры аутентификации до настройки CORS, rate limiting и защиты от атак по времени.

Почему скилл запрещает прямое сравнение API-ключей?

Прямое сравнение строк подвержено timing attacks. Паттерн требует искать ключ в базе по его SHA-256 хешу.

Исходный текст

SKILL.md · MIT
---
name: security-patterns
description: Implements authentication, authorization, encryption, secrets management, and security hardening patterns. Use when designing auth flows, managing secrets, configuring CORS, implementing rate limiting, or when asked about JWT, OAuth, password hashing, API keys, RBAC, or security best practices.
---

# Security Patterns

### When to Load

- **Trigger**: Auth flows, encryption, secrets management, CORS configuration, input validation, rate limiting
- **Skip**: No security surface involved in the current task

## Security Implementation Workflow

Copy this checklist and track progress:

```
Security Implementation Progress:
- [ ] Step 1: Choose authentication strategy
- [ ] Step 2: Implement authorization model
- [ ] Step 3: Set up password hashing
- [ ] Step 4: Configure secrets management
- [ ] Step 5: Enable encryption (transit + rest)
- [ ] Step 6: Configure CORS
- [ ] Step 7: Add rate limiting
- [ ] Step 8: Validate against anti-patterns checklist
```

## Authentication Patterns

### JWT (JSON Web Tokens)

```typescript
import jwt from "jsonwebtoken";

function generateTokens(user: User) {
  const accessToken = jwt.sign(
    { sub: user.id, role: user.role },
    process.env.JWT_SECRET!,
    { expiresIn: "15m", algorithm: "HS256" },
  );
  const refreshToken = jwt.sign(
    { sub: user.id, tokenVersion: user.tokenVersion },
    process.env.JWT_REFRESH_SECRET!,
    { expiresIn: "7d" },
  );
  return { accessToken, refreshToken };
}

// WRONG: localStorage (XSS vulnerable) | CORRECT: httpOnly cookie for refresh, memory for access
res.cookie("refreshToken", refreshToken, {
  httpOnly: true,
  secure: true,
  sameSite: "strict",
  maxAge: 7 * 24 * 60 * 60 * 1000,
  path: "/api/auth/refresh",
});
```

### JWT Verification Middleware

```typescript
function authenticate(req: Request, res: Response, next: NextFunction) {
  const header = req.headers.authorization;
  if (!header?.startsWith("Bearer ")) {
    return res.status(401).json({ error: "Missing token" });
  }

  try {
    const token = header.slice(7);
    const payload = jwt.verify(token, process.env.JWT_SECRET!, {
      algorithms: ["HS256"], // pin: never let the token choose its algorithm
    }) as JwtPayload;
    req.user = { id: payload.sub, role: payload.role };
    next();
  } catch (err) {
    if (err instanceof jwt.TokenExpiredError) {
      return res.status(401).json({ error: "Token expired" });
    }
    return res.status(401).json({ error: "Invalid token" });
  }
}
```

### Session-Based Auth

```typescript
import session from "express-session";
import { RedisStore } from "connect-redis";

app.use(
  session({
    store: new RedisStore({ client: redisClient }),
    secret: process.env.SESSION_SECRET!,
    resave: false,
    saveUninitialized: false,
    cookie: {
      httpOnly: true,
      secure: process.env.NODE_ENV === "production",
      sameSite: "strict",
      maxAge: 24 * 60 * 60 * 1000, // 24 hours
    },
  }),
);
```

### OAuth 2.0 / OIDC Flow Summary

```
Authorization Code Flow with PKCE (all clients):
1. Redirect to provider: /authorize?response_type=code&client_id=...&redirect_uri=...&scope=openid email
   &state=RANDOM&nonce=RANDOM&code_challenge=...&code_challenge_method=S256
2. User authenticates, provider redirects back with ?code=AUTHORIZATION_CODE&state=... (reject if state differs)
3. Backend exchanges code for tokens (POST /token with code_verifier, plus client_secret for confidential clients)
4. Backend validates the id_token (signature, iss, aud, exp, nonce), then creates session/JWT

Public clients (SPAs, mobile): same flow without client_secret
NEVER use Implicit Flow (deprecated, tokens exposed in URL)
```

### API Key Authentication

```typescript
async function authenticateApiKey(
  req: Request,
  res: Response,
  next: NextFunction,
) {
  const apiKey = req.headers["x-api-key"] as string;
  if (!apiKey) return res.status(401).json({ error: "API key required" });

  // WRONG: Direct comparison (timing attack) | CORRECT: Hash-based lookup
  const hashedKey = crypto.createHash("sha256").update(apiKey).digest("hex");
  const keyRecord = await db.apiKey.findUnique({ where: { hash: hashedKey } });
  if (!keyRecord || keyRecord.revokedAt)
    return res.status(401).json({ error: "Invalid API key" });

  req.apiClient = { id: keyRecord.clientId, scopes: keyRecord.scopes };
  next();
}
```

## Authorization Models

### RBAC (Role-Based Access Control)

```typescript
const PERMISSIONS = {
  admin: [
    "users:read",
    "users:write",
    "users:delete",
    "posts:read",
    "posts:write",
    "posts:delete",
  ],
  editor: ["posts:read", "posts:write", "posts:delete", "users:read"],
  viewer: ["posts:read", "users:read"],
} as const;

type Role = keyof typeof PERMISSIONS;

function authorize(...requiredPermissions: string[]) {
  return (req: Request, res: Response, next: NextFunction) => {
    const userPermissions = PERMISSIONS[req.user.role as Role] || [];
    const hasPermission = requiredPermissions.every((p) =>
      (userPermissions as readonly string[]).includes(p),
    );
    if (!hasPermission)
      return res.status(403).json({ error: "Insufficient permissions" });
    next();
  };
}

// Usage: app.delete("/api/users/:id", authenticate, authorize("users:delete"), deleteUser);
```

### Resource-Level Authorization

```typescript
// WRONG: Only checking role, not ownership -- any editor can edit ANY post
// CORRECT: Check ownership or admin role
app.put(
  "/api/posts/:id",
  authenticate,
  authorize("posts:write"),
  async (req, res) => {
    const post = await db.post.findUnique({ where: { id: req.params.id } });
    if (!post) return res.status(404).json({ error: "Not found" });
    if (post.authorId !== req.user.id && req.user.role !== "admin") {
      return res
        .status(403)
        .json({ error: "Not authorized to edit this post" });
    }
    // WRONG: data: req.body -- mass assignment lets the client overwrite authorId
    const { title, body } = UpdatePostSchema.parse(req.body); // allowlisted fields only
    const updated = await db.post.update({
      where: { id: post.id },
      data: { title, body },
    });
    res.json(updated);
  },
);
```

## Password Handling

```typescript
import bcrypt from "bcrypt";
// WRONG: plaintext or MD5/SHA256 (too fast, brute-forceable)
// CORRECT: bcrypt with appropriate cost factor
const SALT_ROUNDS = 12; // ~250ms on modern hardware

async function hashPassword(password: string): Promise<string> {
  return bcrypt.hash(password, SALT_ROUNDS);
}
async function verifyPassword(
  password: string,
  hash: string,
): Promise<boolean> {
  return bcrypt.compare(password, hash); // constant-time comparison built-in
}

// Registration
await db.user.create({
  data: { email, password: await hashPassword(req.body.password) },
});

// Login -- WRONG: "Invalid password" (reveals email exists) | CORRECT: generic message
// Always run bcrypt, even for unknown emails, or response time reveals which emails exist
const user = await db.user.findUnique({ where: { email } });
const hash = user?.password ?? DUMMY_HASH; // bcrypt hash of a random string, created at startup
if (!(await verifyPassword(req.body.password, hash)) || !user) {
  return res.status(401).json({ error: "Invalid email or password" });
}
```

### Password Policies

```typescript
function validatePassword(password: string): string[] {
  const errors: string[] = [];
  if (password.length < 12) errors.push("Minimum 12 characters");
  if (Buffer.byteLength(password) > 72) errors.push("Maximum 72 bytes"); // bcrypt ignores the rest

  // Check against breached password lists (haveibeenpwned API or local)
  // Do NOT enforce arbitrary complexity rules (uppercase + number + symbol)
  // NIST 800-63B recommends length over complexity
  return errors;
}
```

## Secrets Management

```python
# WRONG: Hardcoded values in source code
# API_KEY = "some-value-here"

# CORRECT: Environment variables loaded from .env
from dotenv import load_dotenv
import os

load_dotenv()
api_key = os.getenv("API_KEY")
db_url = os.getenv("DATABASE_URL")

# CORRECT: Secrets manager for production
# AWS: Secrets Manager, Parameter Store
# GCP: Secret Manager
# HashiCorp Vault for self-hosted
```

### Secret Rotation

```
1. Generate new secret value
2. Deploy code that accepts BOTH old and new values
3. Update all consumers to use the new value
4. Verify old value is no longer in use
5. Revoke old value

Never: Rotate in-place without a transition period
```

## Encryption Patterns

### In Transit

```typescript
// Redirect HTTP to HTTPS in production
app.use((req, res, next) => {
  if (
    req.headers["x-forwarded-proto"] !== "https" &&
    process.env.NODE_ENV === "production"
  ) {
    return res.redirect(301, `https://${req.hostname}${req.url}`);
  }
  next();
});
// HSTS header
app.use((req, res, next) => {
  res.setHeader(
    "Strict-Transport-Security",
    "max-age=31536000; includeSubDomains",
  );
  next();
});
```

### At Rest

```typescript
import crypto from "crypto";
const ALGORITHM = "aes-256-gcm";

function encrypt(
  plaintext: string,
  key: Buffer,
): { ciphertext: string; iv: string; tag: string } {
  const iv = crypto.randomBytes(16);
  const cipher = crypto.createCipheriv(ALGORITHM, key, iv);
  let ciphertext =
    cipher.update(plaintext, "utf8", "hex") + cipher.final("hex");
  return {
    ciphertext,
    iv: iv.toString("hex"),
    tag: cipher.getAuthTag().toString("hex"),
  };
}

function decrypt(
  ciphertext: string,
  key: Buffer,
  iv: string,
  tag: string,
): string {
  const decipher = crypto.createDecipheriv(
    ALGORITHM,
    key,
    Buffer.from(iv, "hex"),
  );
  decipher.setAuthTag(Buffer.from(tag, "hex"));
  return decipher.update(ciphertext, "hex", "utf8") + decipher.final("utf8");
}
// Use for PII, sensitive data. Encryption key in secrets manager, NOT in code.
```

## CORS Configuration

```typescript
import cors from "cors";

// WRONG: Allow everything
app.use(cors()); // origin: *, credentials: false

// WRONG: Wildcard with credentials
app.use(cors({ origin: "*", credentials: true })); // browsers reject this

// CORRECT: Explicit allowed origins
const ALLOWED_ORIGINS = [
  "https://myapp.com",
  "https://admin.myapp.com",
  ...(process.env.NODE_ENV !== "production" ? ["http://localhost:3000"] : []),
];

app.use(
  cors({
    origin: (origin, callback) => {
      if (!origin || ALLOWED_ORIGINS.includes(origin)) {
        callback(null, true);
      } else {
        callback(new Error("Not allowed by CORS"));
      }
    },
    credentials: true,
    methods: ["GET", "POST", "PUT", "PATCH", "DELETE"],
    allowedHeaders: ["Content-Type", "Authorization"],
    maxAge: 86400, // cache preflight for 24 hours
  }),
);
```

## Rate Limiting

```typescript
import rateLimit, { ipKeyGenerator } from "express-rate-limit";
import RedisStore from "rate-limit-redis";

// Global rate limit
app.use(
  rateLimit({
    windowMs: 15 * 60 * 1000, // 15 minutes
    limit: 100, // 100 requests per window
    standardHeaders: true, // RateLimit-* headers
    legacyHeaders: false,
    store: new RedisStore({
      sendCommand: (...args) => redisClient.sendCommand(args),
    }),
  }),
);

// Strict limit on auth endpoints
app.use(
  "/api/auth/login",
  rateLimit({
    windowMs: 15 * 60 * 1000,
    limit: 5, // 5 login attempts per 15 min
    message: { error: "Too many login attempts. Try again later." },
  }),
);

// Per-API-key rate limiting for developer APIs
app.use(
  "/api/v1/",
  rateLimit({
    windowMs: 60 * 1000, // 1 minute
    limit: 60, // 60 requests per minute
    keyGenerator: (req) => req.apiClient?.id ?? ipKeyGenerator(req.ip),
  }),
);
```

## Security Headers

```typescript
import helmet from "helmet";

app.use(helmet()); // Sets many secure headers at once

// Key headers helmet sets:
// X-Content-Type-Options: nosniff
// X-Frame-Options: SAMEORIGIN
// Strict-Transport-Security: max-age=31536000; includeSubDomains
// Content-Security-Policy: default-src 'self'

// Customize CSP for your app
app.use(
  helmet.contentSecurityPolicy({
    directives: {
      defaultSrc: ["'self'"],
      scriptSrc: ["'self'"],
      styleSrc: ["'self'", "'unsafe-inline'"],
      imgSrc: ["'self'", "data:", "https://cdn.example.com"],
      connectSrc: ["'self'", "https://api.example.com"],
    },
  }),
);
```

## Input Validation

```typescript
import { z } from "zod";

// WRONG: Trusting user input directly (SQL injection risk)
app.post("/api/users", (req, res) => {
  db.query(`SELECT * FROM users WHERE email = '${req.body.email}'`);
});

// CORRECT: Validate with schema, use parameterized queries
const CreateUserSchema = z.object({
  email: z.string().email().max(255),
  name: z.string().trim().min(1).max(100),
  age: z.number().int().min(13).max(150).optional(),
});

app.post("/api/users", async (req, res) => {
  const result = CreateUserSchema.safeParse(req.body);
  if (!result.success) {
    return res.status(400).json({ errors: result.error.flatten() });
  }
  // Use parameterized query (ORM or prepared statement)
  await db.user.create({ data: result.data });
});
```

## Common Anti-Patterns Summary

```
AVOID                              DO INSTEAD
-------------------------------------------------------------------
JWT in localStorage                httpOnly secure cookie (refresh), memory (access)
MD5/SHA for passwords              bcrypt or argon2 with proper cost factor
Hardcoded secrets in code          Environment variables + secrets manager
cors({ origin: '*' })             Explicit allowed origins list
"Invalid password" message         "Invalid email or password" (no enumeration)
No rate limiting on auth           Strict rate limits on login/register
Rolling your own crypto            Use established libraries (jose, bcrypt)
Trusting user input                Validate with zod/joi, parameterized queries
Same API key forever               Rotate keys regularly, support multiple active
No HTTPS redirect                  Force HTTPS + HSTS header
Symmetric JWT for multi-service    Use RS256/ES256 (asymmetric) for distributed
No input length limits             Max length on all string inputs
```

© CloudAI-X · MIT · CloudAI-X/claude-workflow-v2

Похожие скиллы

Все скиллы по теме «Безопасность» →

Сначала — security-patterns от других авторов.

security-patterns Eyadkelleh Скилл security-patterns для Claude Code и Codex ищет и проверяет шаблоны ключей API, платёжных данных, email, IP-адресов и других чувствительных данных. Используется для разрешённого тестирования безопасности. CX Codex +1 663установок security-patterns OrchestKit Скилл security-patterns для Claude Code помогает внедрять аутентификацию и защитные слои. Подключается при работе с проверкой ввода, защитой от prompt injection и маскированием персональных данных. CX Codex +1 739установок add-authentication-system davila7 Команда add-authentication-system добавляет в приложение систему аутентификации с выбранным методом входа. Она охватывает управление пользователями, сессии, права доступа и защиту от типичных атак. CX Codex +1 382установок security-best-practices Mindrally Скилл security-best-practices задаёт правила безопасной разработки бэкенда и микросервисов. Помогает выстроить проверку пользовательского ввода, защиту API и аутентификацию без утечек секретов или стектрейсов. CX Codex +1 994установок auth-implementation-patterns Seth Hobson Скилл auth-implementation-patterns для Claude Code о проектировании аутентификации и авторизации: JWT, OAuth2, сессии, роли. Разводит аутентификацию (проверка личности, выдача токенов) и авторизацию (проверка прав и доступа). CX Codex +1 19 388установок authentication-patterns zebbern Скилл authentication-patterns даёт правила выбора между сессиями, JWT и OAuth, шаблоны библиотек вроде NextAuth и чек-лист безопасности. Помогает настроить вход пользователей без типовых уязвимостей. CX Codex +1 ★ 4 719звёзд репо auth-security majiayu000 Скилл auth-security для Claude Code задаёт правила безопасной авторизации по OAuth 2.1 и RFC 9700: PKCE, ротацию refresh-токенов и строгую проверку JWT. CX Codex +1 117установок auth-patterns davepoon Скилл auth-patterns для Claude Code собирает шаблоны аутентификации Next.js: настройку NextAuth.js v5, защиту маршрутов через middleware, RBAC и работу с сессиями в серверных компонентах. CX Codex +1 183установок

Из того же набора46

Весь набор →
refactorer Субагент refactorer, который улучшает структуру кода: извлекает методы и классы, устраняет дублирование, применяет паттерны проектирования. Работает пошагово, запускает тесты перед и после каждого изменения. CX Codex ★ 1 424звёзд репо bootstrap-repo Команда bootstrap-repo запускает десять параллельных субагентов для разбора кодовой базы и собирает архитектурный отчёт в файле CODEBASE.md. CX Codex +1 ★ 1 424звёзд репо test-architect Субагент test-architect проектирует стратегию тестирования, пишет тесты и проверяет покрытие. Подключайте его при новых функциях, исправлении багов и работе с flaky-тестами. CX Codex ★ 1 424звёзд репо devops-infrastructure Скилл devops-infrastructure для Claude Code пишет Dockerfile, настраивает GitHub Actions, планирует развёртывания и организует мониторинг. Используется при контейнеризации, работе с пайплайнами, Terraform и production-инфраструктурой. CX Codex +1 ★ 1 424звёзд репо database-design Скилл database-design помогает проектировать схемы баз данных, устранять проблему N+1, подбирать индексы и планировать миграции без простоя сервиса. CX Codex +1 ★ 1 424звёзд репо orchestrator orchestrator разбивает сложную работу на этапы, поручает независимые части специализированным субагентам и сводит результаты. Он пригодится при изменениях в нескольких модулях и работе с GitHub PR. CX Codex ★ 1 424звёзд репо error-handling Скилл error-handling для Claude Code предлагает паттерны обработки ошибок, логирования и восстановления после сбоев. Помогает проектировать слой исключений и реализовывать retry и circuit breaker. CX Codex +1 ★ 1 424звёзд репо project-starter Плагин project-starter добавляет в Claude Code субагентов, скиллы, хуки и слэш-команды для разработки. Помогает разбирать задачи, проверять изменения и вести Git-рабочий процесс. ★ 1 424звёзд репо

Спонсоры проекта

Компании, которые помогают держать аналитику открытой