security-audit-skill

ตัวอย่างการใช้งาน

ยกมาตรงตามผลรันจริง ไม่ได้แก้ไข

# security-audit-skill real run

- Date: 2026-10-15 23:00 +0700
- Tool: Claude Code 2.1.300, headless `claude -p`, later turns with `--resume` (one session)
- Model: claude-sonnet-5-5
- Isolation: --setting-sources project --strict-mcp-config (no user skills, memory or MCP)
- Skill: cloudflare/security-audit-skill @ c1c8a8c1471069fb0e188eeaff69b8e8db6564a8 `skills/security-audit/SKILL.md` (MIT, Cloudflare)
- Exact first prompt: `/security-audit Please do a focused security review of app/server.js in this toy repo. Guidance mode only, no report files.`
- User replies are sample text written by our team. Assistant text is verbatim from the session log. Tool calls are shown as one line notes; local paths in them are shortened to `~` and `<project>`.

## Turn 1

### User

/security-audit Please do a focused security review of app/server.js in this toy repo. Guidance mode only, no report files.

> [event] skill loaded: security-audit

> [tool call] Read {"file_path": "<project>/app/server.js"}

### Assistant

# Security review of `app/server.js` (guidance mode)

This was a source-only review. I ran nothing, wrote no files, and the repo is a toy, so severities assume the app were exposed. Three issues are confirmed from source alone. The fourth, the fake auth, is context for the first.

## Confirmed findings

**1. SQL injection and missing ownership check in `/orders/:id`** (`server.js:15`). Severity: high.
- **Boundary crossed:** `req.params.id` is concatenated straight into the SQL string. The path is URL-decoded, so a request like `/orders/1%20OR%201=1` is enough, or a `UNION SELECT` against `sqlite_master`. Any caller can read any row or table in the database.
- **Separate flaw:** `req.user` is set on line 13 but never used. Even with a plain numeric id, `GET /orders/2` returns bob's order to alice, which is a cross-user read (IDOR).
- **Error leak:** line 16 sends `err.message` to the client. That gives an attacker SQL error feedback for crafting injections.
- **Limit on impact:** `db.get` runs a single statement, so stacked queries and writes are not possible here. The impact is read-only.
- **Fix:**
  - Use a parameterized query: `db.get('SELECT * FROM orders WHERE id = ? AND owner = ?', [id, req.user], ...)`.
  - Validate that `id` is an integer.
  - Return 404 for a row that doesn't exist or isn't the caller's, so the two cases look the same.
  - Return a generic 500 message instead of `err.message`.

**2. Path traversal and arbitrary file read in `/receipts`** (`server.js:21-22`). Severity: high.
- **Boundary crossed:** `path.join(__dirname, 'receipts', req.query.name)` normalizes `..` segments, so `?name=../../../../etc/passwd` escapes the `receipts` directory. The process can then read any file it has permission for, including source files and `.env`.
- **Not an exploit by itself:** an absolute path such as `/etc/passwd` is not a bypass, because `path.join` just nests it under `receipts/`. The `..` segments are what escape.
- **Secondary issues:**
  - A missing `name` makes `path.join` throw a `TypeError`, and `?name[]=x` does the same. Express catches the throw, but the response can include a stack trace outside production mode.
  - `readFileSync` blocks the event loop on every request.
- **Fix:**
  - Resolve the path and check containment: `const base = path.resolve(__dirname, 'receipts'); const p = path.resolve(base, name); if (!p.startsWith(base + path.sep)) return res.status(400).end();`.
  - Also require `typeof name === 'string'`.
  - Better still, accept only a strict pattern such as `/^[\w-]+\.txt$/`.
  - Use async `fs.readFile`, or `res.sendFile(name, { root: base })`, which refuses traversal.
  - Optionally resolve symlinks with `fs.realpath` before the containment check.

**3. Reflected XSS in `/hello`** (`server.js:24`). Severity: medium.
- **Boundary crossed:** `req.query.name` is interpolated into HTML, and `res.send` with a string defaults to `text/html`. A link like `/hello?name=<script>…</script>` runs script in the victim's browser on this origin.
- **Why not higher:** it is reflected rather than stored, and it needs the victim to click a link. The app has no sessions or cookies in this code, so the practical damage is limited.
- **Fix:**
  - HTML-escape the value (for example with the `escape-html` package).
  - Or return `text/plain` or JSON.
  - A CSP header is a useful backstop but is not the primary fix.

## Context, not a standalone finding

**Client-asserted identity** (`server.js:13`): the user comes from the `X-User` header, which any caller can set. The code labels it "fake auth", so I am treating it as a placeholder. If it were real, every ownership check would be bypassable. It also means findings 1 and 2 are reachable with no credentials at all. Replace it with a real session or token before relying on `req.user` for any access decision.

## Not flagged
- `app.listen(3001)` binds to all interfaces. In a deployed setting that widens exposure, but it needs a deployment fact I can't see from the source.
- Missing rate limiting, security headers and HTTPS are hardening items with no concrete boundary violation here.

## Suggested order
Fix 1 and 2 first, since both give unauthenticated data disclosure. Fix 3 is a one-line change. Add a regression test for each: an injection payload, a `../` name, and a `<script>` name.

I can write the patched `server.js` if you want it.