跳至正文
OpenAgentSkill
浏览已开放 · 目录记录日期 2026-11-21 · 自动更新和账户服务暂时关闭。
安全

bola-detector

Use when writing or reviewing any API endpoint that retrieves, updates, or deletes a record by user-supplied ID. Triggers on route handlers with :id params, controller functions calling findById/findOne/get_object_or_404, Prisma findUnique, Spring repository.findById, or any ORM lookup by identifier. Does NOT trigger on generic "review this endpoint" requests — use api-security-review for holistic reviews. This skill focuses narrowly on object-level authorization. Detects BOLA / IDOR — OWASP API1:2023.

apisec-inc · 已有静态检查记录

安装与使用

运行命令前先阅读来源与依赖说明。复制命令不会自动安装或执行。

npx skills add apisec-inc/apisec-skills --skill bola-detector

技能说明


name: bola-detector description: > Use when writing or reviewing any API endpoint that retrieves, updates, or deletes a record by user-supplied ID. Triggers on route handlers with :id params, controller functions calling findById/findOne/get_object_or_404, Prisma findUnique, Spring repository.findById, or any ORM lookup by identifier. Does NOT trigger on generic "review this endpoint" requests — use api-security-review for holistic reviews. This skill focuses narrowly on object-level authorization. Detects BOLA / IDOR — OWASP API1:2023.

BOLA / IDOR Detector — OWASP API1:2023

1. Role

You are an API security specialist focused exclusively on detecting Broken Object Level Authorization (BOLA), also known as Insecure Direct Object Reference (IDOR). When reviewing or generating any API endpoint that accesses a resource by identifier, you apply the full analysis below before approving the code. You never assume an endpoint is safe simply because it requires authentication.


2. What Is BOLA?

BOLA (Broken Object Level Authorization) is classified as OWASP API1:2023 — the #1 vulnerability in API security.

It occurs when an API endpoint accepts an object identifier from the client and returns or mutates the corresponding object without verifying that the authenticated user owns or is authorized to access that specific object.

Why authentication is not enough

An auth token (JWT, session cookie, API key) proves identity — it answers "who is this?". It does not answer "does this user own object X?". These are two separate checks:

CheckQuestionMechanism
AuthenticationWho is calling?Token validation, session lookup
Authorization (object-level)Does this caller own this resource?Query-level filter, policy check

Skipping the second check means any authenticated user can access any other user's data simply by changing the ID in the request. This is trivially exploitable — attackers iterate IDs or use UUIDs leaked in logs, URLs, or prior responses.


3. Core Vulnerable Pattern vs Safe Pattern

Vulnerable — Node.js / Express / Mongoose

// DELETE /api/orders/:id
router.delete('/orders/:id', authMiddleware, async (req, res) => {
  // BUG: fetches by ID alone — any authenticated user can delete any order
  const order = await Order.findById(req.params.id);
  if (!order) return res.status(404).json({ error: 'Not found' });

  await order.deleteOne();
  res.json({ message: 'Deleted' });
});

Safe — Node.js / Express / Mongoose

// DELETE /api/orders/:id
router.delete('/orders/:id', authMiddleware, async (req, res) => {
  // SAFE: ownership enforced at the query level — only the owner's order is matched
  const order = await Order.findOne({
    _id: req.params.id,
    userId: req.user.id,    // <-- ownership filter in the query
  });
  if (!order) return res.status(404).json({ error: 'Not found' });

  await order.deleteOne();
  res.json({ message: 'Deleted' });
});

The Diff

- const order = await Order.findById(req.params.id);
+ const order = await Order.findOne({
+   _id: req.params.id,
+   userId: req.user.id,
+ });

The only change is replacing a bare ID lookup with a compound query that includes the authenticated user's ID. This is the fundamental BOLA fix across all languages and ORMs.


4. Six-Point Analysis Checklist

Run every one of these checks on every endpoint under review. An endpoint must pass all six to be considered safe.

4.1 Identity Established First

Is authentication middleware present and executed before the handler? This is a prerequisite for BOLA analysis — if auth is missing entirely, defer to auth-rbac-scaffold for the full authentication finding. For BOLA purposes, flag it as Critical and move on.

// PASS — authMiddleware runs before handler
router.get('/orders/:id', authMiddleware, getOrder);

// FAIL — no auth middleware, handler runs unauthenticated
router.get('/orders/:id', getOrder);

If identity is not established, stop — the endpoint is Critical severity.

4.2 Ownership Checked at Query Level (Not Post-Fetch)

The ownership filter must be part of the database query itself, not a conditional check after fetching.

// FAIL — post-fetch check (TOCTOU risk, still fetches unauthorized data into memory)
const order = await Order.findById(req.params.id);
if (order.userId.toString() !== req.user.id) {
  return res.status(403).json({ error: 'Forbidden' });
}

// PASS — query-level check (unauthorized record is never loaded)
const order = await Order.findOne({
  _id: req.params.id,
  userId: req.user.id,
});

Post-fetch checks are Medium severity — they prevent data return but still load unauthorized data into application memory and are vulnerable to race conditions.

4.3 Role-Based Bypass Explicitly Checked

If admins are allowed to skip ownership checks, that bypass must be explicit and auditable.

// PASS — explicit admin bypass with audit trail
router.get('/admin/orders/:id', authMiddleware, requireRole('admin'), async (req, res) => {
  // Admin can access any order — role verified by requireRole middleware
  const order = await Order.findById(req.params.id);
  if (!order) return res.status(404).json({ error: 'Not found' });
  res.json(order);
});

// FAIL — implicit admin bypass via boolean flag
router.get('/orders/:id', authMiddleware, async (req, res) => {
  let query = { _id: req.params.id };
  // Weak: req.user.isAdmin is a claim in the token — not verified server-side
  if (!req.user.isAdmin) {
    query.userId = req.user.id;
  }
  const order = await Order.findOne(query);
  res.json(order);
});

4.4 Bulk/List Endpoints Scoped to Requesting User

Endpoints that return collections are BOLA targets if they return all records instead of only the c

阅读完整来源 ↗