Chuyển đến nội dung
Skillpedia
Bảo mật48k · #3779

api-security-best-practices

Skill này làm gì

Công cụ này giúp làm cho API của bạn (những “cánh cửa” để chương trình giao tiếp với nhau) an toàn hơn. Nó kiểm tra xem chỉ người được ủy quyền mới truy cập được thông tin và dữ liệu gửi đi có đúng hay không, ngăn các cuộc tấn công phổ biến.

Khi nào nên dùng

  • ✓Khi tạo một tính năng mới được bảo vệ trong API của bạn.
  • ✓Khi xem xét ai có quyền truy cập dữ liệu nào.
  • ✓Khi điều tra việc lạm dụng API của bạn.

Liên kết skill

https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/api-security-best-practices/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/api-security-best-practices/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Xem xét bảo mật của route API mới này để đảm bảo chỉ người dùng đã xác thực mới truy cập được và dữ liệu gửi đến là hợp lệ.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Công cụ phân tích cách API kiểm tra ai đang gửi yêu cầu (xác thực) và người đó có quyền làm điều họ yêu cầu hay không (phân quyền). Nó cũng kiểm tra xem dữ liệu nhận được có an toàn và đúng như mong đợi không.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

API Security Best Practices

Review the request boundary from caller identity through authorization, validated input, storage and observable response. Preserve the application's actual identity provider and data model rather than introducing a second authentication system.

When to Use

Use when adding a protected endpoint, reviewing object access, replacing permissive request parsing, or investigating an API abuse path. For a concrete defect, start with the failing route and its callers; do not deploy unrelated security infrastructure.

Inputs and prerequisites

Record the routes, caller/tenant model, identity provider, token contract, runtime and locked dependency versions, database schema, proxy topology and authorized test scope. Use synthetic identities in a test environment. Existing task authorization carries forward; production scans, account writes and message sends need their own authority. The Node examples below are integration sketches for Express, jsonwebtoken and Zod; application/database adapters are deliberately named rather than presented as a full runnable service. Confirm APIs against the installed versions before integrating.

1. Authenticate the exact token contract

Prefer the established provider/session middleware. When the service owns an HMAC JWT contract, require a strong server-owned key, a fixed algorithm, exact issuer and audience, and required runtime claims. Do not infer permissions from a decoded token before signature verification. Never accept a caller-selected verification algorithm.

const jwt = require('jsonwebtoken');

// Illustrative first-party access-token contract; not a third-party OAuth adapter.
const ACCESS_POLICY = {
  algorithms: ['HS256'], issuer: 'example-auth', audience: 'example-api'
};
function verifyAccessToken(token, signingKey) {
  const claims = jwt.verify(token, signingKey, ACCESS_POLICY);
  if (!claims || typeof claims !== 'object' ||
      typeof claims.sub !== 'string' || !claims.sub ||
      typeof claims.tenantId !== 'string' || !claims.tenantId ||
      !Number.isSafeInteger(claims.exp) || !Number.isSafeInteger(claims.iat) ||
      claims.exp <= claims.iat) {
    throw new Error('Invalid access claims');
  }
  return { subject: claims.sub, tenantId: claims.tenantId };
}
function readBearer(header) {
  if (typeof header !== 'string' || header.length > 8192) return null;
  const match = /^Bearer ([A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+)$/i.exec(header);
  return match ? match[1] : null;
}

Issue access tokens with the same issuer/audience/algorithm and a short application- approved expiration. Handle verification failure as a generic 401 without echoing the token or exception. Expiration alone does not revoke a token; define revocation or short-lived sessions according to the actual threat model. A service using asymmetric provider keys needs the provider's discovery/JWKS validation and key-rotation policy, not this HMAC example. Never reuse an access token as a refresh token.

Refresh sessions

Use the provider's supported session flow or a server-side opaque refresh design: store only a digest, expiry, user/session family and revocation state. In one atomic transaction consume the old active token and create the replacement. Concurrent reuse must not issue two successors; defined reuse handling revokes the affected family. Check current user status and permissions when issuing new access tokens. Bind refresh to the intended client/session and protect cookie-based requests against CSRF. Do not log tokens, store them plaintext in a database, or return a refresh token through a URL. Test simultaneous refresh, expiry, replay, revocation and transaction failure before calling the flow complete. No database transaction adapter is bundled here.

2. Authorize the resource and operation

Authentication identifies the caller; authorization decides the exact operation on an object and tenant. A role name does not automatically grant cross-tenant access. Apply the owner/tenant predicate in the database mutation to avoid a check-then-write race, and allowlist writable properties. Use 404/403 consistently with the product's resource-disclosure policy.

// Prisma-style sketch; id and tenant types must match your actual schema.
async function deleteOwnedPost(prisma, postId, principal) {
  const result = await prisma.post.deleteMany({

…
Câu hỏi thường gặp

Xác thực (authentication) là gì?

Đó là quá trình xác minh một người dùng hoặc chương trình đúng là người họ tự nhận, thường bằng mật khẩu, token hoặc khóa.

Phân quyền (authorization) là gì?

Đó là quá trình kiểm tra xem người dùng hoặc chương trình đã xác thực có quyền thực hiện một hành động hoặc truy cập một tài nguyên cụ thể hay không.

Tại sao phải kiểm tra dữ liệu đầu vào?

Để đảm bảo chỉ những dữ liệu an toàn và đúng định dạng mới được xử lý, tránh lỗi và các cuộc tấn công lợi dụng dữ liệu bị sai định dạng.

Chi tiết

Mô tả gốc của tác giả

Implement secure API design patterns including authentication, authorization, input validation, rate limiting, and protection against common API vulnerabilities

repo: sickn33/agentic-awesome-skills

Độ phổ biến

Sao GitHub
48k
Vị trí xếp hạng
#3779
Giấy phép
MIT
Cập nhật
15/10/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự