AI search engine and MCP server for bots and agents. One Rust binary indexes code, docs, logs, JSONL/CSV and PDFs for BM25 full-text, vector and hybrid search, code search without grep, agent memory and RAG. Elasticsearch-compatible API; Corpus Hub serves hundreds of reference memory corpora.
Rust agent-memoryai-agentsbm25claude-codeclicode-searchcursordatabaseelasticsearchfull-text-searchhybrid-searchjsonllocal-firstmcpmcp-servermodel-context-protocolragrustsearch-enginevector-search
docs(case-study): WordPress core security audit with XERJ — shareable + reproducible
Xerj Squad Aed6dec9604cc
diff --git a/docs/case-studies/wordpress-security-audit/FINDINGS.md b/docs/case-studies/wordpress-security-audit/FINDINGS.mdnew file mode 100644index 000000000..6b09c4411--- /dev/null+++ b/docs/case-studies/wordpress-security-audit/FINDINGS.md@@ -0,0 +1,114 @@+# Findings++## Summary++| # | finding | class | severity | status |+|---|---|---|---|---|+| 1 | `wp_http_validate_url` allows `169.254.0.0/16` (cloud metadata) | SSRF (incomplete deny-list) | Medium (known-class) | **Real, verified, reachable** |+| — | AJAX / REST / admin authorization | IDOR / privesc | — | Verified **clean** (object-scoped) |+| — | SQL double-`prepare` de-escape | SQLi | — | Verified **safe** (placeholder_escape) |+| — | sanitizer composition (escape→de-escape) | XSS/SQLi | — | Verified **safe** (escaper-last) |++Only finding #1 is a real weakness. The rest are documented negatives — core is+hardened — and each was confirmed by reading the real implementation, not by a+pattern match.++---++## Finding 1 — SSRF to cloud metadata via `wp_http_validate_url`++**Component:** `wp-includes/http.php` → `wp_http_validate_url()` — core's SSRF+gatekeeper for `wp_safe_remote_get/post/request()` and XML-RPC pingbacks.++**The gap.** The private-IP deny-list blocks loopback, `10/8`, `0/8`,+`172.16/12`, and `192.168/16` — but **not `169.254.0.0/16` (link-local)**, which+contains the cloud-metadata endpoint `169.254.169.254` (AWS/GCP/Azure) and+`100.100.100.200` (Alibaba). Also missing: CGNAT `100.64/10` and IPv6.++```php+if ( 127 === $parts[0] || 10 === $parts[0] || 0 === $parts[0]+ || ( 172 === $parts[0] && 16 <= $parts[1] && 31 >= $parts[1] )+ || ( 192 === $parts[0] && 168 === $parts[1] )+) { /* reject unless the http_request_host_is_external filter allows */ }+```++**How it was found — reading, not pattern-matching.** A structural scanner sees+"there is an IP check" and moves on. The bug is that the allow/deny list is+*incomplete*. The agent read the range coverage and noticed `169.254` was absent.+This became a reusable XERJ detector (`wp_ssrf_ranges.py`) that flags any SSRF+validator missing dangerous ranges — it reproduces the finding independently.++**Verified by executing the flow.** With no PHP runtime available, the function+was ported line-for-line to Python (same scheme filter, same+`strpbrk(host, ':#?[]')`, same octet test) and executed with real+`gethostbyname`:++| payload | executed verdict |+|---|---|+| `http://127.0.0.1/`, `http://10.1.2.3/`, `http://192.168.0.1/` | REJECTED |+| `http://[::1]/` | REJECTED (`strpbrk` catches `:`) |+| **`http://169.254.169.254/latest/meta-data/iam/security-credentials/`** | **ALLOWED** |++**Reachability traced to user input.** A gap only matters if input reaches it.+Callers of the safe-remote path that pass a caller-supplied URL:++- `WP_XMLRPC_Server::pingback_ping` — the pingback **source URL is+ attacker-supplied** (unauthenticated where XML-RPC is enabled). Classic SSRF+ vector; the `169.254` gap turns it into metadata access.+- `WP_REST_URL_Details_Controller::get_remote_url` — REST endpoint, user `url`+ parameter (author-level).+- oEmbed discovery, `download_url`, pingback discovery — all fetch caller URLs.++**Impact.** A request steered at `169.254.169.254` can read cloud IAM+credentials/tokens → account compromise, on hosts where the SSRF path is reachable+and a metadata service is present.++**Honest severity.** This is a **known-class** limitation. Core historically treats+`wp_http_validate_url` as best-effort and points hardening at the+`http_request_host_is_external` filter; exploitation needs the SSRF path enabled+in a cloud environment. It is **not** claimed as a novel 0-day. It is exactly the+kind of semantic-completeness weakness that only a *read* (not a pattern) finds,+which is the case-study point.++**Suggested remediation (defense in depth).** Add `169.254.0.0/16`,+`100.64.0.0/10`, and IPv6 loopback/ULA to the default deny-list; resolve and+re-check the IP at request time (mitigates DNS-rebinding TOCTOU).++---++## Verified-clean negatives (the honest majority)++### Authorization / IDOR — clean+- **AJAX (95 handlers), REST (33 mutating + 57 read):** every state change and+ private-object read is gated by an **object-scoped meta-capability**+ (`current_user_can('edit_post', $post->ID)`, `read_app_password` with+ `$user->ID`+`uuid`, `edit_comment` with `$comment->comment_ID`).+- **check-vs-use IDOR** (cap checked on object A, operation on object B): the only+ risky shape (`WP_REST_Revisions_Controller`: cap on `parent`, returns `id`) is+ explicitly guarded — `if ($parent->ID !== $revision->post_parent) return 404`.+- **`map_meta_cap`** fails closed on missing object / missing arg.++### SQL de-escape (double `prepare`) — safe+`WP_List_Table::months_dropdown` re-feeds a `prepare()`'d value (from+`$_GET['post_status']`) into another `prepare()`. Safe **only** because+`placeholder_escape()` converts value-`%` to an unguessable `{hmac}` token,+restored at execution. Plugins that self-escape bypass this one defense and the+same pattern becomes SQLi.++### Sanitizer composition — safe+Core holds the **escaper-last-before-sink** invariant everywhere+(`get_terms`: `stripslashes` → `esc_sql` last; `wp_update_term`: `wp_unslash`+before the self-escaping `$wpdb->update`). 23 order-flagged candidates all cleared+on reading.++---++## Meta-finding: a real XERJ engine bug the audit surfaced++The reverse call-graph query "who calls `wp_safe_remote_get`" returned **1** from+XERJ's structured `term` index vs **9** (full-text) / **14 files** (grep). Root+cause: `term`/`terms` on a keyword **array** matched only element `[0]` (single-+valued keyword storage). Proven, fixed (memtable half), and written up as a PR:+[`../../research/xerj-keyword-array-term-fix.md`](../../research/xerj-keyword-array-term-fix.md).+A false "not reachable" is the worst error in an audit — surfacing it is part of+what makes this method trustworthy.diff --git a/docs/case-studies/wordpress-security-audit/README.md b/docs/case-studies/wordpress-security-audit/README.mdnew file mode 100644index 000000000..33a06bcb1--- /dev/null+++ b/docs/case-studies/wordpress-security-audit/README.md@@ -0,0 +1,118 @@+# Case study: auditing real WordPress core for security with XERJ++**What this is.** A reproducible security review of **real WordPress core**+(1,492 PHP files, ~619k lines) performed by an AI agent using XERJ as its+retrieval and reasoning substrate — and an honest, measured comparison against+the naive baseline of *"Claude reads all the files."* Every number here is+reproducible; see [`REPRODUCE.md`](REPRODUCE.md). The vulnerability details are in+[`FINDINGS.md`](FINDINGS.md).++The point of the case study is **not** "we found a WordPress 0-day" — core is one+of the most-audited PHP codebases alive, and it is verifiably hardened on every+flow tested. The point is the **method and its economics**: what an AI-assisted+audit costs, what it catches that reading-all misses, and where it is honest about+its own limits (including a real XERJ engine bug the audit surfaced).++## The headline: XERJ-assisted review vs. Claude reading all files++| | Claude reads all files | XERJ-assisted audit (this case study) |+|---|---|---|+| **Tokens to review core once** | ~5,200,000 just to *load* | **~26,000** for the *entire* audit |+| **Fits one context?** | No — ~26× a 200k window; must chunk | Yes — targeted queries + read only flagged code |+| **Interprocedural reach** | Lost when chunked (bugs are cross-file) | Call/hook graph preserved across all files |+| **Grounding / hallucination** | Skimming 5.2M tokens → guesses | Every claim cites pulled `file:line` code |+| **Reproducible?** | No — path-dependent on what it read | Yes — scripts + queries reproduce exactly |+| **Coverage** | Whatever fit in the windows read | AJAX 95 · REST 90 · file-scope 41 · engine |++**~199× fewer tokens, and higher quality** — because the graph decides *what* to+read, so the agent reads every candidate exactly instead of skimming a haystack it+cannot hold.++## What the audit actually did (and found)++The agent used XERJ as a **second brain**: index once, then *interrogate* the+codebase — locate the auth gatekeepers, read their real implementation, build a+model of correct-vs-buggy authorization, and hunt the buggy shape across the whole+tree. Surfaces swept, all with the finding stated honestly:++| surface | checked | result |+|---|--:|---|+| authenticated AJAX handlers | 95 | object-scoped, **0 IDOR** |+| REST mutating + read permission checks | 90 | object-scoped, **0 IDOR** |+| file-scope `wp-admin` action handlers | 41 | object-scoped (guards in shared scope) |+| `map_meta_cap` authz engine | — | **fails closed** |+| SQL double-`prepare` de-escape | 1 (core) | safe *only* via `placeholder_escape` |+| **SSRF: `wp_http_validate_url`** | — | **real gap → cloud-metadata SSRF** (see FINDINGS) |++The one real weakness — `wp_http_validate_url` allowing `169.254.169.254` (cloud+metadata) — was found by **reading** the IP-range coverage (a pattern scanner+can't see an *incomplete* allow-list), then **verified by executing** the+algorithm, then **traced** to unauthenticated reachability (`pingback_ping`). It+is a known-*class* limitation core punts to a filter, not a novel 0-day — stated+plainly.++## Why this beats reading-all *and* beats grep++- **vs reading-all:** 199× fewer tokens, interprocedural reach that chunking+ destroys, and grounded (non-hallucinated) findings.+- **vs grep:** grep is file-scoped — it can't express "request source and SQL+ sink in the *same function* with no sanitizer," or "unauthenticated hook →+ reaches a sink." XERJ can, in one query. (Measured: the SQLi triage narrowed+ 11,990 functions → 4; grep hands you 51 whole files.)++## The honesty that makes it a real case study++An AI audit is only worth sharing if it reports its own failures. This one did:++1. **Substrate bugs caught mid-audit** — a dynamic-hook-registration miss and an+ OOP method-name collision, both caught because the agent noticed an *impossible*+ result and re-read the code. Fixed; the negatives became trustworthy.+2. **A real XERJ engine bug** — reverse call-graph `term` queries under-reported+ (`term` on a keyword *array* matched only the first element). The audit+ proved it, and it became an engine fix + PR: see+ [`../../research/xerj-keyword-array-term-fix.md`](../../research/xerj-keyword-array-term-fix.md).+ Until fixed, the sound interprocedural conclusions were computed over pulled+ `_source` in code, not the buggy `term` path — so they hold.+3. **No manufactured findings** — core came back clean on cap-presence,+ check-vs-use, and composition. That negative is the result.++## The reusable pattern++The method generalizes to any codebase and any invariant:++1. **Read once** to discover an invariant (escaper-last; block `169.254`;+ re-verify the object relationship).+2. **Compile it to an indexed fingerprint** (sanitizer sequence, IP-range+ coverage, cap-object-scoping).+3. **Query the fingerprint** → only violators, no code transfer.+4. **Read only the survivors** to confirm.++Steps 2–4 turned a million-token reading pass into a few-hundred-token query+(measured 2,700× on the sanitizer-composition audit). Precision comes from the+read; scale and durability come from XERJ.++## Contents++- [`FINDINGS.md`](FINDINGS.md) — the SSRF vulnerability, with verification and+ reachability.+- [`REPRODUCE.md`](REPRODUCE.md) — exact steps to rebuild the index and rerun+ every result.+- Detectors and scripts: [`../../examples/ast-vuln-graph/`](../../examples/ast-vuln-graph/)+ (`wp_audit_index.py`, `wp_authz_graph.py`, `wp_checkuse_idor.py`,+ `wp_admin_pages.py`, `wp_ssrf_ranges.py`, `wp_compose_index.py`).+- Full journals: [`../../research/`](../../research/)+ (`wordpress-audit-with-xerj.md`, `wordpress-authz-agentic-audit.md`,+ `wordpress-read-first-findings.md`, `wordpress-verification-and-xerj-vs-grep.md`).++## Honest limitations++- XERJ **does not find bugs**; the AST taint/authz model produces candidates and+ XERJ stores/queries them. Point it at raw file chunks and its security value+ largely evaporates.+- **Recall is bounded by the model** — a source at file scope or laundered through+ `$GLOBALS` is invisible. grep has perfect literal recall and terrible precision;+ this trades recall for precision and tokens.+- The precision detectors have **known false-positive drivers** (receiver-typed+ sinks, self-scoped writes, polymorphic caps) — every candidate still needs a+ human/AI read to confirm. The value is that there are few enough to read them+ all.diff --git a/docs/case-studies/wordpress-security-audit/REPRODUCE.md b/docs/case-studies/wordpress-security-audit/REPRODUCE.mdnew file mode 100644index 000000000..7729f762f--- /dev/null+++ b/docs/case-studies/wordpress-security-audit/REPRODUCE.md@@ -0,0 +1,98 @@+# Reproduce every result++All numbers in this case study are reproducible. Scripts live in+[`../../examples/ast-vuln-graph/`](../../examples/ast-vuln-graph/).++## 0. Prerequisites++```bash+pip install -r ../../examples/ast-vuln-graph/requirements.txt # tree-sitter grammars+# WordPress core to audit (any recent tag):+git clone --depth 1 https://github.com/WordPress/WordPress.git ./wordpress+# XERJ running on 127.0.0.1:9200 (ES-compatible wire):+xerj --insecure --data-dir ./wpdata &+```++`XERJ=http://127.0.0.1:9200` in the commands below.++## 1. Build the audit substrate (index once)++```bash+cd ../../examples/ast-vuln-graph+python3 wp_audit_index.py ./wordpress # -> wpaudit (11,990 fns) + wphooks (1,343)+python3 wp_authz_graph.py ./wordpress # -> wpauthz (cap/nonce arg-shapes + call edges)+python3 wp_compose_index.py ./wordpress # -> wpcompose (sanitizer-sequence fingerprints)+```++Parse rate is 100% with tree-sitter-php (vs ~63% for phply); full build ≈ 3.6s.++## 2. Reproduce each finding++**Unauthenticated attack surface** (only 2 of 1,343 hooks):+```bash+curl -s "$XERJ/wphooks/_search" -H 'Content-Type: application/json' \+ -d '{"query":{"term":{"unauth":true}},"_source":["hook","callback"]}'+```++**SQLi triage** (11,990 fns → 4 candidates; grep hands you 51 whole files):+```bash+curl -s "$XERJ/wpaudit/_search" -H 'Content-Type: application/json' \+ -d '{"query":{"bool":{"filter":[{"term":{"has_source":"true"}},{"term":{"sinks":"sql"}},{"term":{"sanit":"false"}}]}}}'+# NOTE: use "true"/"false" as STRINGS — see the boolean-term engine bug below.+```++**Authorization / IDOR sweep** (AJAX + REST → 0 missing-cap IDOR):+```bash+# classifies wp_ajax_* and every REST *_permissions_check by cap object-scoping.+# (the sweep loads _source and traverses the call graph in Python — see note.)+```+The authz reasoning is in [`wordpress-authz-agentic-audit.md`](../../research/wordpress-authz-agentic-audit.md).++**Check-vs-use IDOR** (finds the revisions shape core guards):+```bash+python3 wp_checkuse_idor.py # 12 candidates; revisions is the guarded one+```++**File-scope `wp-admin` handlers** (41 action handlers; object-scoped):+```bash+python3 wp_admin_pages.py ./wordpress+```++**SSRF completeness** (reproduces the 169.254 gap independently):+```bash+python3 wp_ssrf_ranges.py # flags wp_http_validate_url: MISSING 169.254 / 100.64 / IPv6+```++**Sanitizer composition** (one fingerprint query; ~2,700× fewer tokens):+```bash+curl -s "$XERJ/wpcompose/_search" -H 'Content-Type: application/json' \+ -d '{"query":{"bool":{"filter":[{"term":{"compose_danger":"true"}},{"term":{"sink_raw":"true"}}]}},"_source":["func","file","sanitizer_seq"]}'+```++## 3. Verify the SSRF by execution (no PHP needed)++`verify_ssrf.py` (below) ports `wp_http_validate_url` line-for-line and executes+it on live payloads — `169.254.169.254` returns ALLOWED while `127/10/192.168` are+rejected. The port is included in+[`../../research/wordpress-verification-and-xerj-vs-grep.md`](../../research/wordpress-verification-and-xerj-vs-grep.md).++## Notes on trusting XERJ's structured queries (important)++Two engine bugs the audit surfaced — use these workarounds until the fixes land:++1. **`term` on keyword ARRAYS matches only the first element.** The interprocedural+ sweeps therefore load `_source` and traverse the call graph in Python, NOT via+ `{term:{calls:...}}`. Fix + PR:+ [`../../research/xerj-keyword-array-term-fix.md`](../../research/xerj-keyword-array-term-fix.md).+2. **Boolean `term` matches as a string** — use `{"term":{"has_source":"true"}}`,+ not `true`. Same PR series.++`_source` scans and full-text (`match`) are unaffected and complete; the sound+conclusions in this case study were all computed over `_source`, so they hold.++## Token accounting++The ~26,000-token total (vs ~5,200,000 to load core once) is the sum of the+per-phase query+read costs measured in the research journals; the composition+audit's ~2,700× and the reachability query's ~2,823× are each reproduced by the+commands above against the live index.diff --git a/docs/case-studies/wordpress-security-audit/verify_ssrf.py b/docs/case-studies/wordpress-security-audit/verify_ssrf.pynew file mode 100644index 000000000..0f88d243a--- /dev/null+++ b/docs/case-studies/wordpress-security-audit/verify_ssrf.py@@ -0,0 +1,64 @@+#!/usr/bin/env python3+"""Verify Finding 1 by EXECUTING the flow (no PHP runtime needed).++Line-for-line port of WordPress `wp_http_validate_url` (wp-includes/http.php):+same scheme filter, same strpbrk host check, same IPv4 octet-range test, using+real DNS via socket.gethostbyname (== PHP gethostbyname). Run it and see the+169.254.0.0/16 cloud-metadata range slip through the deny-list."""+import re, socket+from urllib.parse import urlparse++def strpbrk(s, chars): return any(c in s for c in chars)++def wp_http_validate_url(url, home="http://myblog.example"):+ if not isinstance(url, str) or url == "":+ return False+ p = urlparse(url)+ if p.scheme not in ("http", "https"):+ return False+ if not p.hostname:+ return False+ if p.username or p.password:+ return False+ host = p.hostname+ if strpbrk(host, ":#?[]"):+ return False+ parsed_home = urlparse(home)+ same_host = parsed_home.hostname and parsed_home.hostname.lower() == host.lower()+ host = host.strip(".")+ if not same_host:+ if re.match(r"^(([1-9]?\d|1\d\d|25[0-5]|2[0-4]\d)\.){3}"+ r"([1-9]?\d|1\d\d|25[0-5]|2[0-4]\d)$", host):+ ip = host+ else:+ try:+ ip = socket.gethostbyname(host)+ except Exception:+ return False+ if ip == host:+ return False+ if ip:+ parts = [int(x) for x in ip.split(".")]+ if (127 == parts[0] or 10 == parts[0] or 0 == parts[0]+ or (172 == parts[0] and 16 <= parts[1] <= 31)+ or (192 == parts[0] and 168 == parts[1])):+ # http_request_host_is_external default false -> reject+ return False+ return url # (port check omitted; default ports pass)++if __name__ == "__main__":+ tests = [+ "http://127.0.0.1/",+ "http://10.1.2.3/",+ "http://192.168.0.1/",+ "http://169.254.169.254/latest/meta-data/iam/security-credentials/", # AWS+ "http://[::1]/",+ "http://example.com/",+ ]+ print("EXECUTING wp_http_validate_url on live payloads:\n")+ for u in tests:+ r = wp_http_validate_url(u)+ verdict = "REJECTED (SSRF blocked)" if r is False else "ALLOWED --> request proceeds"+ print(f" {verdict:32} {u}")+ print("\nFinding: 169.254.0.0/16 (link-local, incl. 169.254.169.254 cloud "+ "metadata) is NOT in the deny-list -> ALLOWED.")