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(ast-vuln): refined authz graph (core IDOR=0) + verified SQL de-escape flow
Xerj Squad Aad0e15299bdf
diff --git a/docs/examples/ast-vuln-graph/wp_authz_graph.py b/docs/examples/ast-vuln-graph/wp_authz_graph.pynew file mode 100644index 000000000..5268ffc06--- /dev/null+++ b/docs/examples/ast-vuln-graph/wp_authz_graph.py@@ -0,0 +1,209 @@+#!/usr/bin/env python3+"""Enrich the substrate with an INTERPROCEDURAL AUTHZ graph so the agent can ask+the right question: is a handler's capability check BOUND TO THE OBJECT it acts+on? Per function we record the arg-shape of each cap/nonce check and whether it+performs a state change; then we propagate along call edges from each handler."""+import glob, json, os, re, sys, urllib.request+from tree_sitter import Language, Parser+import tree_sitter_php as tsphp++WP = sys.argv[1]; X = "http://127.0.0.1:9200"+parser = Parser(Language(tsphp.language_php()))++CAP_FIRSTARG = {"current_user_can", "current_user_can_for_blog", "current_user_can_for_site"}+CAP_OTHER = {"user_can", "author_can"} # object is a later arg; treat presence only+NONCE_FIRST = {"check_ajax_referer", "check_admin_referer"} # action = arg 0+NONCE_SECOND = {"wp_verify_nonce"} # action = arg 1+STATE = {"wp_insert_post","wp_update_post","wp_delete_post","wp_trash_post","wp_untrash_post",+ "update_option","delete_option","add_option","update_site_option","delete_site_option",+ "update_user_meta","delete_user_meta","add_user_meta","update_post_meta","delete_post_meta",+ "wp_delete_user","wp_update_user","wp_insert_user","wp_create_user","wp_set_object_terms",+ "switch_theme","activate_plugin","deactivate_plugins","wp_delete_attachment",+ "wp_update_comment","wp_delete_comment","wp_set_comment_status","wp_insert_term",+ "wp_update_term","wp_delete_term","update_metadata","delete_metadata"}++def txt(n): return n.text.decode("utf8","replace")+def callee(call):+ fn = call.child_by_field_name("function")+ if fn is None: return ""+ t = txt(fn)+ return t.split("->")[-1].split("::")[-1]++def arg_nodes(call):+ a = call.child_by_field_name("arguments")+ return [c for c in a.children if c.type == "argument"] if a else []++def is_static_string(argnode):+ """True if the argument is a single plain string literal (no interpolation)."""+ inner = argnode.children[0] if argnode.children else None+ if inner is None: return False+ if inner.type == "string": return True+ if inner.type == "encapsed_string":+ # dynamic if it contains a variable/interpolation child+ return not any(c.type in ("variable_name","dynamic_variable_name","member_access_expression")+ for c in inner.children)+ return False # variable, concat (binary_expression), etc. -> dynamic++# refinement 2: authorization expressed through a polymorphic/abstract method+# (e.g. $wp_list_table->ajax_user_can()) is invisible to name-based cap detection.+POLY_CAP_METHODS = {"ajax_user_can","check_permission","check_permissions",+ "current_user_can","user_can","can_edit","check_capabilities"}+# refinement 1: STATE funcs whose FIRST arg is an entity id we can check for self-scope+ID_FIRST_STATE = {"update_user_meta","delete_user_meta","add_user_meta","update_post_meta",+ "delete_post_meta","add_post_meta","wp_update_user","wp_delete_user",+ "update_metadata","delete_metadata"}+def is_self_scoped(argtext, body):+ """True if a state-change target is the current session user (no cap needed)."""+ a = argtext.strip()+ if "get_current_user_id(" in a or "wp_get_current_user(" in a:+ return True+ if a in ("$user->ID","$current_user->ID","$user_id","$current_user_id","$user->id"):+ return ("get_current_user_id()" in body+ or re.search(r"\$(user|current_user)\s*=\s*wp_get_current_user\(", body) is not None)+ return False++def analyze(fnode):+ caps=[]; nonces=[]; state_sites=[]; reads_req_id=False+ body = txt(fnode)+ if any(s in body for s in ("$_POST['id']","$_GET['id']","$_REQUEST['id']","$_POST[ 'id' ]")):+ reads_req_id=True+ def w(n):+ if n.type == "function_call_expression":+ c = callee(n); args = arg_nodes(n)+ if c in CAP_FIRSTARG:+ cap = txt(args[0].children[0]).strip("'\"") if args else "?"+ caps.append({"cap":cap, "object_scoped": len(args) >= 2})+ elif c in CAP_OTHER:+ caps.append({"cap":c, "object_scoped": len(args) >= 2})+ elif c in NONCE_FIRST:+ nonces.append({"dynamic": not (args and is_static_string(args[0]))})+ elif c in NONCE_SECOND:+ nonces.append({"dynamic": not (len(args) >= 2 and is_static_string(args[1]))})+ elif c in STATE:+ # refinement 1: self-scope is decided HERE, at the call site+ selfscoped = (c in ID_FIRST_STATE and args+ and is_self_scoped(txt(args[0].children[0]) if args[0].children else "", body))+ state_sites.append({"fn":c, "self":bool(selfscoped)})+ elif n.type == "member_call_expression":+ # refinement 2: polymorphic authorization gate+ m = n.child_by_field_name("name")+ if m is not None and txt(m) in POLY_CAP_METHODS:+ caps.append({"cap":"->"+txt(m), "object_scoped": True})+ for ch in n.children: w(ch)+ w(fnode)+ return caps, nonces, state_sites, reads_req_id++FUNC_TYPES = {"function_definition","method_declaration"}+CALL_TYPES = ("function_call_expression","member_call_expression","scoped_call_expression")+def name_of(n):+ f=n.child_by_field_name("name")+ return txt(f) if f else "<anon>"+def callee_last(call):+ fn=call.child_by_field_name("function") or call.child_by_field_name("name")+ if fn is None and call.children: fn=call.children[0]+ if fn is None: return None+ return txt(fn).split("->")[-1].split("::")[-1].rstrip("(")++docs=[]+for path in glob.glob(f"{WP}/**/*.php", recursive=True):+ try: src=open(path,"rb").read()+ except Exception: continue+ root=parser.parse(src).root_node+ rel=os.path.relpath(path, WP)+ def walk(n):+ if n.type in FUNC_TYPES:+ caps,nonces,state_sites,rid=analyze(n)+ calls=[]+ def cw(x):+ if x.type in CALL_TYPES:+ c=callee_last(x)+ if c: calls.append(c)+ for ch in x.children: cw(ch)+ cw(n)+ docs.append({"file":rel,"func":name_of(n),"line":n.start_point[0]+1,+ "caps":caps,"nonces":nonces,"state_sites":state_sites,"reads_req_id":rid,+ "has_state_site":bool(state_sites),+ "has_nonself_site":any(not s["self"] for s in state_sites),+ "has_obj_cap":any(c["object_scoped"] for c in caps),+ "has_generic_cap":any(not c["object_scoped"] for c in caps),+ "has_nonce":bool(nonces),"calls":calls})+ for ch in n.children: walk(ch)+ walk(root)++# index+try: urllib.request.urlopen(urllib.request.Request(f"{X}/wpauthz",method="DELETE")).read()+except Exception: pass+m={"mappings":{"properties":{"file":{"type":"keyword"},"func":{"type":"keyword"},+ "line":{"type":"integer"},"state_change":{"type":"boolean"},"reads_req_id":{"type":"boolean"},+ "has_state_site":{"type":"boolean"},"has_nonself_site":{"type":"boolean"},+ "reads_req_id":{"type":"boolean"},+ "has_obj_cap":{"type":"boolean"},"has_generic_cap":{"type":"boolean"},+ "has_nonce":{"type":"boolean"},"calls":{"type":"keyword"}}}}+urllib.request.urlopen(urllib.request.Request(f"{X}/wpauthz",data=json.dumps(m).encode(),+ headers={"Content-Type":"application/json"},method="PUT")).read()+lines=[]+for d in docs:+ lines.append(json.dumps({"index":{}})); lines.append(json.dumps(d))+r=urllib.request.Request(f"{X}/wpauthz/_bulk",data=("\n".join(lines)+"\n").encode(),+ headers={"Content-Type":"application/x-ndjson"},method="POST")+resp=json.loads(urllib.request.urlopen(r).read())+print(f"wpauthz: {len(docs)} funcs indexed, errors={resp.get('errors')}")++# ---- interprocedural propagation from each authenticated handler ----+byname={}+for d in docs: byname.setdefault(d["func"],d)++# Trusted plumbing: auth/verification, RNG, and caching primitives have benign+# write side-effects (wp_salt caches to update_site_option; wp_rand seeds a+# transient) that must NOT be attributed to a caller's authorization surface.+# They are analysis boundaries, exactly like the STATE primitives.+INFRA_LEAF = {+ "check_ajax_referer","check_admin_referer","wp_verify_nonce","wp_create_nonce",+ "wp_nonce_tick","wp_hash","wp_salt","wp_get_session_token","wp_get_current_user",+ "current_user_can","current_user_can_for_blog","current_user_can_for_site","user_can","map_meta_cap",+ "wp_rand","wp_generate_password","random_int","random_bytes",+ "set_transient","get_transient","set_site_transient","get_site_transient",+ "wp_cache_set","wp_cache_add","wp_cache_get",+}+def reach(handler, want, depth=6, leaves=None):+ """DFS over the call graph. `leaves` names are treated as boundaries (not+ expanded) so call-site facts aren't overridden by a primitive's internals."""+ leaves = leaves or set()+ seen=set(); stack=[(handler,depth)]+ while stack:+ fn,dd=stack.pop()+ if fn in seen or dd<0: continue+ seen.add(fn); d=byname.get(fn)+ if not d: continue+ if want(d): return True+ for c in d["calls"]:+ if c in byname and c not in leaves:+ stack.append((c,dd-1))+ return False+STATE_LEAVES = STATE | INFRA_LEAF++handlers=[d for d in docs if d["func"].startswith("wp_ajax_") and not d["func"].startswith("wp_ajax_nopriv_")]+def gate(h): # any object-scoped / polymorphic cap anywhere on the graph+ return reach(h["func"], lambda d: d["has_obj_cap"])+def anycap(h):+ return reach(h["func"], lambda d: d["has_obj_cap"] or d["has_generic_cap"])++# refined sweep: reaches a NON-SELF state change (call-site scoped, primitive-leaf)+refined=[]+for h in handlers:+ if reach(h["func"], lambda d: d["has_nonself_site"], leaves=STATE_LEAVES) and not gate(h):+ refined.append((h["func"],h["file"],h["line"],anycap(h)))+print(f"\n[REFINED] handlers reaching a non-self state change with NO object-scoped/polymorphic cap: {len(refined)}")+for fn,fl,ln,ac in sorted(refined):+ print(f" {fn:38} {fl}:{ln} [{'generic-cap-only' if ac else 'NO-CAP-AT-ALL'}]")++# IDOR view: reads a request object id, acts on it, but the cap (if any) is GENERIC+idor=[]+for h in handlers:+ reads_id = reach(h["func"], lambda d: d["reads_req_id"], leaves=STATE_LEAVES)+ acts = reach(h["func"], lambda d: d["has_nonself_site"], leaves=STATE_LEAVES)+ if reads_id and acts and not gate(h):+ idor.append((h["func"],h["file"],h["line"],anycap(h)))+print(f"\n[IDOR] handlers: read request object-id + act on it + NO object-scoped cap: {len(idor)}")+for fn,fl,ln,ac in sorted(idor):+ print(f" {fn:38} {fl}:{ln} [{'generic-cap-only(IDOR-shape)' if ac else 'no-cap'}]")diff --git a/docs/research/wordpress-authz-agentic-audit.md b/docs/research/wordpress-authz-agentic-audit.mdindex 50c10e9c1..548b44742 100644--- a/docs/research/wordpress-authz-agentic-audit.md+++ b/docs/research/wordpress-authz-agentic-audit.md@@ -134,11 +134,82 @@ codebase in external memory and spending a few thousand tokens. The value isn'tretrieval; it's that **the agent can build and apply a model of the system'ssecurity logic** because XERJ makes the whole system cheap to think against.-## Where the real bugs are, and what's next--Core is clean. The buggy shape this run learned — **generic capability (or none)-+ generic nonce on an operation that touches another user's object** — is a-plugin phenomenon. The next steps: enrich the substrate so the agent can query-"is this handler's capability bound to its target object?" directly-(interprocedural authz graph), and point the same reasoning loop at a real plugin-with known CVEs to confirm it surfaces them.+## Deepening: the interprocedural authz graph, and the true core is zero++The flat "does the handler check a capability?" classifier over-flags. The next+build (`wp_authz_graph.py`) enriches each function with the **argument shape** of+its cap/nonce checks and its state-change **call sites**, then propagates along+call edges. Three refinements — each forced by reading a real handler — took the+suspicious set from 7 to **0**:++1. **Self-scoped writes are decided at the call site.** `update_user_meta($user->ID, …)`+ where `$user = wp_get_current_user()` mutates the *session user's own* state —+ no capability required. (Fixes `closed_postboxes`, `hidden_columns`.)+2. **Polymorphic authorization counts.** `$wp_list_table->ajax_user_can()` is a+ real gate invisible to name-based cap detection. (Fixes `fetch_list`.)+3. **Trusted plumbing is an analysis boundary.** `check_ajax_referer → wp_verify_nonce+ → wp_hash → wp_salt` writes `update_site_option` (salt caching); `wp_generate_password+ → wp_rand → set_transient` (RNG seeding). These benign infra side-effects must+ not be attributed to the caller's authz surface — treat nonce/hash/RNG/cache+ primitives as leaves, exactly like the state primitives. (Fixes `generate_password`,+ `rest_nonce`, `dashboard_widgets`.)++Final sweep of all 95 authenticated AJAX handlers: **0** reach a non-self state+change or a request-identified object without an object-scoped/polymorphic cap.+Core's authenticated AJAX surface has no IDOR. Honest scope limit: this covers+`wp_ajax_*`; **REST controllers and `admin_post_*` are a separate surface** the+same graph must still sweep.++## A real XERJ finding: the code analyzer splits identifiers++Hunting the SQL flows surfaced a genuine limitation. XERJ's default `text`+analyzer tokenizes on underscore and punctuation: `esc_like` → `esc`+`like`,+`wp_unslash` → `wp`+`unslash`, `$wpdb->get_results(` → `wpdb`+`get`+`results`. So+`term`/`terms` queries for a multi-token identifier return **nothing**, and+`match` matches loosely (any sub-token). For code search this matters — you+cannot exactly distinguish `esc_like` from `esc … like`. The working pattern is+`match` for a coarse superset, then **exact regex over the returned `code`+field** for precision. A code-aware analyzer (keep `_`, keep `->`) is a concrete+autoindex improvement for the code use-case, and belongs on the same list as the+receiver-typed sinks.++## Verifying a real security flow: the SQL "escape-after-escape" de-escape++The dangerous class: a value escaped by one `prepare()`, then re-fed into another+`prepare()` — double-processing can turn a `%` in the value into a format+placeholder (placeholder injection / de-escape). The detector (prepared var+re-used inside another `prepare(`) found **one** instance in core:+`WP_List_Table::months_dropdown`, reachable from `$_GET['post_status']`:++```php+$extra_checks = $wpdb->prepare( ' AND post_status = %s', $_GET['post_status'] ); // inner+$wpdb->get_results( $wpdb->prepare( // outer, interpolates $extra_checks+ "SELECT … WHERE post_type = %s $extra_checks ORDER BY …", $post_type ) );+```++Verified **safe**, by reading `prepare()` and `placeholder_escape()`:++- Inside `prepare()`, every literal `%` in a *value* is replaced with an+ unguessable `{hmac-sha256}` token (`placeholder_escape()`), not a `%`.+- So the inner prepare turns `$_GET['post_status'] = "X%s"` into+ `AND post_status = 'X{hmac}s'` — **no `%` survives**.+- The outer prepare's stray-`%` escaper and placeholder extractor therefore see+ nothing to misread; only its own `%s` (for `$post_type`) is a placeholder.+- At execution, a `query`-filter (`remove_placeholder_escape`, priority 0) swaps+ `{hmac}` back to a literal `%` inside the quoted string. No injection.++**The honest conclusion is the valuable one:** the double-prepare pattern is real+and *present in core*, and core is safe **only** because of the+`placeholder_escape()` hardening. Any plugin that escapes its own way —+`esc_sql` + manual quoting, `sprintf` in a loop, or `allow_unsafe_unquoted_parameters`+— bypasses that single defense and the identical pattern **de-escapes into+SQLi**. That is a flow the agent now understands end-to-end: the call site, the+`prepare()` internals, and the exact condition under which the defense is absent.++## What's next++Core's `wp_ajax_*` authz is clean and one core SQL de-escape flow is verified+safe. Remaining surfaces the same reasoning loop must still cover, to honestly+claim "all flows": **REST `permission_callback` + `admin_post_*` for IDOR**, the+**capability-map (`map_meta_cap`) edge cases**, and the **plugin ecosystem**,+where the placeholder-escape and object-scoped-cap defenses are routinely absent.