agentleFS
joaoguirunasjoaoguirunas/team-os/.claude/skills/dev-security-patterns/SKILL.md

dev-security-patterns

Skill3 starsChanged 2 months agoMITView on GitHub

Padrões de segurança para software complexo — autenticação, autorização, RLS, OWASP top 10, validação de input, secrets management.

  • Reads credentials

The file

Files
SKILL.md

Security Patterns — Software Complexo

JWT — Autenticação

// Geração
const accessToken = jwt.sign(
  { userId: user.id, role: user.role },
  process.env.JWT_SECRET!,
  { expiresIn: '15m' }   // curto
)
const refreshToken = jwt.sign(
  { userId: user.id },
  process.env.JWT_REFRESH_SECRET!,
  { expiresIn: '7d' }
)

// Validação em middleware
const verifyToken = (req, res, next) => {
  const token = req.headers.authorization?.split(' ')[1]
  if (!token) return res.status(401).json({ error: { code: 'UNAUTHORIZED', requestId: req.requestId } })
  try {
    req.user = jwt.verify(token, process.env.JWT_SECRET!)
    next()
  } catch {
    return res.status(401).json({ error: { code: 'UNAUTHORIZED', requestId: req.requestId } })
  }
}

Regras: Access token 15min. Refresh token 7 dias com rotação. Nunca localStorage — usar httpOnly cookie. Secret mínimo 256 bits.

Autorização — RBAC

const requireRole = (...roles: string[]) => (req, res, next) => {
  if (!roles.includes(req.user.role)) {
    return res.status(403).json({ error: { code: 'FORBIDDEN', requestId: req.requestId } })
  }
  next()
}

// Uso
router.delete('/users/:id', verifyToken, requireRole('admin'), deleteUser)

Row Level Security (RLS — Supabase/Postgres)

-- Habilitar RLS
ALTER TABLE orders ENABLE ROW LEVEL SECURITY;

-- Usuário vê apenas seus dados
CREATE POLICY "user_own_orders" ON orders
  FOR ALL USING (auth.uid() = user_id);

-- Admin vê tudo
CREATE POLICY "admin_all_orders" ON orders
  FOR ALL USING (auth.jwt() ->> 'role' = 'admin');

RLS é a última linha de defesa — aplicar em todas as tabelas com dados de usuário.

Validação de Input

const createUserSchema = z.object({
  email: z.string().email().max(255),
  name: z.string().min(1).max(100).trim(),
})

// Middleware
const validate = (schema) => (req, res, next) => {
  const result = schema.safeParse(req.body)
  if (!result.success) {
    return res.status(400).json({
      error: {
        code: 'VALIDATION_ERROR',
        details: result.error.errors,
        requestId: req.requestId,  // sempre incluir requestId
      }
    })
  }
  req.body = result.data  // dados sanitizados
  next()
}

Nunca confiar em input do cliente. Validar e sanitizar tudo que vem do exterior.

OWASP Top 10 — Checklist

RiscoMitigação
SQL InjectionORM/query builder, nunca concatenar queries
Broken AuthJWT curto, refresh rotation, httpOnly cookies
Sensitive Data ExposureHTTPS, nunca logar dados sensíveis
Broken Access ControlRBAC + RLS obrigatórios
Security MisconfigurationEnv vars para secrets, nada hardcoded
XSSSanitizar output, Content-Security-Policy
Vulnerable Dependenciesnpm audit em CI
Insufficient LoggingLogar autenticações e acessos negados com requestId

Secrets Management

# ✅ Variáveis de ambiente
DATABASE_URL=postgresql://...
JWT_SECRET=super-secret-256-bits

# ❌ Nunca hardcoded
const JWT_SECRET = "minha-chave"

Regras:

  • .env no .gitignore — nunca commitar
  • .env.example com chaves sem valores — commitar
  • Em produção: secret manager (AWS Secrets Manager, Vercel env vars)

Rate Limiting

const authLimiter = rateLimit({
  windowMs: 15 * 60 * 1000,
  max: 10,   // 10 tentativas em 15 min
  message: { error: { code: 'RATE_LIMITED', requestId: 'see-x-request-id-header' } },
})

app.use('/auth', authLimiter)
app.use('/api', rateLimit({ windowMs: 60000, max: 100 }))

Password Hashing

const hash = await bcrypt.hash(password, 12)    // custo 12 mínimo
const valid = await bcrypt.compare(input, hash)

Nunca MD5 ou SHA1 para passwords. Nunca plain text.

Logging Estruturado Seguro

O requestId gerado no middleware de entrada (ver dev-api-design) deve estar presente em todos os logs — é o fio que conecta request → serviços → banco → resposta no debugging.

// ✅ Correto — contexto rico com requestId, dado sensível ausente
logger.info({
  requestId: req.requestId,  // sempre — rastreabilidade end-to-end
  userId: user.id,
  action: 'login_attempt',
  ip: req.ip,
  success: true,
}, 'User authenticated')

logger.error({
  requestId: req.requestId,
  userId: user.id,
  action: 'payment_process',
  orderId: order.id,
  err: error,
}, 'Payment processing failed')

// ❌ Errado — sem requestId, sem contexto, pode vazar dados
console.error('Error:', error)
logger.info('Login', { password: body.password, token: jwt })

Nunca logar: passwords, tokens JWT, dados de cartão, CPF/SSN, qualquer PII desnecessário. Sempre logar: requestId, userId (não email), action, resultado (success/fail), IP em auth events.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.