Two-stage Tree-sitter AST and LLM diff scanning for pull requests. Catches auth bypasses, IDOR ownership gaps, TOCTOU races, silent exception swallows, and AI-hallucinated guards.
SARIF 2.1.0
| 18 | export async function getInvoiceRecord(req: Request, res: Response) { | |
| 19 | const { invoiceId } = req.params; | |
| 20 | ||
| 21 | - | // [idor_ownership] CWE-639: Unscoped lookup by user-controlled invoiceId |
| 22 | - | const invoice = await prisma.invoice.findUnique({ where: { id: invoiceId } }); |
| 21 | + | // Recommended patch: enforce authenticated tenant ownership predicate |
| 22 | + | const { tenantId } = req.auth; |
| 23 | + | const invoice = await prisma.invoice.findFirstOrThrow({ |
| 24 | + | where: { id: invoiceId, tenantId } |
| 25 | + | }); |
| 26 | return res.json(invoice); | |
| 27 | } |
Tree-sitter parses changed AST nodes in invoice.controller.ts and flags rule candidate idor_ownership: request parameter req.params.invoiceId passed directly to prisma.invoice.findUnique without an ownership predicate.
LLM evaluates surrounding route middleware and model context. Confirms req.auth.tenantId is available in session scope yet omitted from the query predicate. Suppresses false positives where admin or public scope guards exist upstream.
Emits SARIF 2.1.0 alert mapped directly to line 22 for GitHub Code Scanning PR annotations.
{
"$schema": "https://json.schemastore.org/sarif-2.1.0.json",
"version": "2.1.0",
"runs": [{
"tool": {
"driver": {
"name": "LogicGuard",
"version": "0.1.0"
}
},
"results": [{
"ruleId": "idor_ownership",
"level": "error",
"message": {
"text": "Direct prisma.invoice.findUnique lookup uses req.params.invoiceId without tenantId ownership filter."
},
"locations": [{
"physicalLocation": {
"artifactLocation": { "uri": "apps/billing-service/src/routes/invoice.controller.ts" },
"region": { "startLine": 22 }
}
}]
}]
}]
}
$ uvx logicguard rules
LogicGuard v0.1.0 | 5 built-in business logic rules active:
auth_bypass Missing auth or role enforcement on route/handler
idor_ownership Direct object access without caller/tenant ownership scope
race_toctou Check-then-act race condition on state or balance mutation
silent_except Swallowed exception causing fail-open or masked auth failure
ai_hallucinated_guard Stubbed, tautological, or unawaited AI-generated guard
$ logicguard scan --offline-only
Stage 1 (Tree-sitter AST): scanned 14 diff hunks -> 1 candidate
[ERROR] idor_ownership apps/billing-service/src/routes/invoice.controller.ts:22
SARIF 2.1.0 artifact: logicguard.sarif
Runs via openrouter/free ($0), Anthropic, Ollama, or --offline-only.
Sending raw diffs straight to an LLM is noisy and expensive. LogicGuard filters deterministically with Tree-sitter first, then verifies only structural candidates.
Parses changed functions, route decorators, ORM queries, lock scopes, and exception handlers via Tree-sitter grammars. Isolates exact candidate spans before any model inference runs.
--offline-only
Sub-second local scan
Evaluates flagged AST candidates against caller middleware, session context, and upstream invariants to confirm exploitability and dismiss safe internal patterns.
openrouter/free
SARIF 2.1.0 output
Use openrouter/free in CI or locally for zero-cost Stage 2 verification on every PR.
Connect your Anthropic key for deep semantic verification across complex authorization boundaries.
Run local weights entirely inside your workstation or private runner with zero outbound source transfer.
Skip Stage 2 LLM calls completely and enforce pure Stage 1 Tree-sitter AST rules with no API key required.
Inspect the active rule set anytime with uvx logicguard rules. Every rule pairs a Tree-sitter AST matcher with semantic Stage 2 verification.
auth_bypass
CWE-287 / CWE-306
Detects newly exposed API routes, RPC endpoints, or privileged state-mutating handlers that omit authentication middleware, session verification, or role guards.
idor_ownership
CWE-639 / BOLA
Flags database reads, updates, or deletes driven by a user-controlled ID parameter without verifying ownership against user_id or tenant_id.
race_toctou
CWE-362 / CWE-367
Catches non-atomic check-then-act sequences on wallet balances, promo redemptions, and quota counters missing row locks or atomic updates.
silent_except
CWE-390 / CWE-636
Identifies bare except: pass or empty catch {} blocks in auth, signature, or validation flows that fail open silently.
ai_hallucinated_guard
AI-CODE AUDIT
Flags placeholder auth functions, tautological checks, or unawaited async verifiers frequently introduced by AI coding tools.
Inspect active rules immediately with uvx or install the CLI globally via pipx. Published live on PyPI as logicguard==0.1.0.
uvx logicguard rules
pipx install logicguard
uvx logicguard scan --offline-only
Drop Aviation3006/logicguard@v0.1.0 into your pull request workflow. Pair with openrouter/free for $0 scanning and native SARIF 2.1.0 alerts in GitHub Security.
name: LogicGuard Security Diff Scan
on: [pull_request]
permissions:
contents: read
security-events: write
jobs:
logicguard:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: LogicGuard Security Diff Scan ($0 via openrouter/free)
uses: Aviation3006/logicguard@v0.1.0
with:
model: openrouter/free
format: sarif
output: logicguard.sarif
env:
OPENROUTER_API_KEY: ${{ secrets.OPENROUTER_API_KEY }}
- name: Upload SARIF 2.1.0 to GitHub Security
if: always()
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: logicguard.sarif
LogicGuard executes directly inside your CI runner or local shell. Run purely offline with Tree-sitter AST rules, route through local Ollama weights, or bring your own OpenRouter or Anthropic key.
Deterministic Tree-sitter AST rule evaluation with zero outbound HTTP requests or external credentials.
Point Stage 2 verification at local Ollama endpoints so proprietary code never leaves your hardware.
Your CI runner talks directly to OpenRouter or Anthropic. No LogicGuard SaaS middleman touches your diffs.
Inspect every AST grammar query, rule heuristic, and prompt directly in the public GitHub repository.
Each rule combines a clean Tree-sitter AST query with a focused Stage 2 verification prompt. Read the contributor guide or pick up an issue from the open queue.