Skip to content

Commit b5c9f51

Browse files
committed
Read each piece of code once, and turn confirmed findings into rules
Code reasoning now runs after each scan on every branch that gets code scanning: the default branch when the code pass ran on it, and any branch whose scan scope gives it every pass. A branch still on the commit it was last read at is not fetched. On a branch that moved, only handlers whose code or access check helpers changed are asked about. Answers are reused across forks and copies by digest, an unreadable answer is tried twice and then left until the code changes, and a provider outage is not held against the code. The per repository setting now limits new questions per scan, and handlers past it are read on later scans instead of never. Cached findings keep the right line after lines are added above them. Each connection has an AI code reasoning switch, on by default. When the pass confirms a high finding, the model drafts one semgrep rule for that kind of mistake and language, once. The draft is run by semgrep before it is offered: it has to match the handler it came from, miss the fixed version, and stay under eight matches in that repository. Drafts are listed on the AI page switched off, and reach scans only once an administrator turns them on. Their tokens count against the reasoning monthly limit. Sixteen built in rules cover what a pattern can see without reasoning: JWT signature checks switched off or the algorithm read from the token, secrets compared with == in Python, JavaScript and PHP, tokens from a non secure random, request bodies poured into models, a Django REST view set open to anybody, request forgery checks switched off or exempted with no signature check, model output handed to a shell or a query, and exception text from broad or database errors returned to the caller. Each was run over real code and every false alarm found is a test case.
1 parent 366af17 commit b5c9f51

14 files changed

Lines changed: 1476 additions & 63 deletions

‎app/core/ai.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,9 @@
118118
"use in a calendar month, in and out "
119119
"together."),
120120
"ai.code_reasoning_per_repo": (25, False,
121-
"Most handlers asked about in one repository per "
122-
"run, the likeliest first."),
121+
"Most handlers not read before that are asked "
122+
"about in one repository per scan, the likeliest "
123+
"first. The rest wait for the next scan."),
123124
# What the custom service charges, per million tokens, for the cost page.
124125
# Kept as text so 0.10 stays 0.10. Empty is not counted.
125126
"ai.price_input": ("", False, "Price per million input tokens."),

‎app/core/analysis.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,14 @@ def _rule_text(rule: str) -> str:
310310
"""
311311
if not rule or not rule.startswith("gcr-"):
312312
return ""
313+
if rule.startswith("gcr-ai-"):
314+
# Drafted from a confirmed finding and turned on by an administrator.
315+
from app.core.db import session_scope
316+
from app.core.models import SastRuleDraft
317+
with session_scope() as db:
318+
row = (db.query(SastRuleDraft.rule_yaml)
319+
.filter(SastRuleDraft.rule_id == rule).first())
320+
return (row[0] or "")[:6000] if row else ""
313321
from app.core.scanner_config import RULES_DIR
314322

315323
path = RULES_DIR / "semgrep-extra.yaml"

‎app/core/code_reasoning.py‎

Lines changed: 316 additions & 52 deletions
Large diffs are not rendered by default.

‎app/core/models.py‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,6 +1732,10 @@ class Connection(Base):
17321732
# Anybody else this account's findings should go to. A rota address, a
17331733
# ticket queue, the vendor who maintains it. Comma separated.
17341734
notify_extra = Column(String(500), default="")
1735+
# Whether the AI code reasoning pass reads repositories under this
1736+
# account. On unless somebody turns it off, which is how it behaved
1737+
# before there was a switch.
1738+
ai_reasoning = Column(Boolean, default=True, nullable=False)
17351739

17361740
api_url = Column(String(300), default="https://api.github.com")
17371741
web_url = Column(String(300), default="https://github.com")
@@ -2025,6 +2029,64 @@ class RepoAssessment(Base):
20252029
started_at = Column(DateTime(timezone=True))
20262030

20272031

2032+
class SastRuleDraft(Base):
2033+
"""A semgrep rule the model drafted from a reasoning finding it was sure of.
2034+
2035+
The point is to pay for a kind of mistake once. When the reasoning pass
2036+
confirms a high finding, the model is asked for a rule that catches the
2037+
same pattern, and from then on semgrep finds it everywhere for nothing.
2038+
One per kind of problem and language, ever: a second finding of the same
2039+
kind does not buy a second draft.
2040+
2041+
A draft is checked by running it, not by reading it: it has to match the
2042+
code it came from, miss the fixed version the model wrote beside it, and
2043+
not fire all over the repository it came from. It is saved switched off
2044+
either way, and only reaches a scan once an administrator turns it on.
2045+
"""
2046+
2047+
__tablename__ = "sast_rule_drafts"
2048+
2049+
id = Column(Integer, primary_key=True)
2050+
# "<category>:<language>", the thing that is drafted for at most once.
2051+
key = Column(String(80), nullable=False, unique=True, index=True)
2052+
rule_id = Column(String(120), default="")
2053+
category = Column(String(40), default="")
2054+
language = Column(String(20), default="")
2055+
rule_yaml = Column(Text, default="")
2056+
# proposed: passed every check, waiting for a person. enabled: in scans.
2057+
# rejected: a person said no. failed: did not pass the checks.
2058+
state = Column(String(16), default="proposed", index=True)
2059+
checks = Column(Text, default="")
2060+
origin_finding_id = Column(Integer)
2061+
origin_repository = Column(String(320), default="")
2062+
origin_matches = Column(Integer, default=0)
2063+
tokens = Column(Integer, default=0)
2064+
created_at = Column(DateTime(timezone=True), default=utcnow)
2065+
decided_by = Column(String(255), default="")
2066+
decided_at = Column(DateTime(timezone=True))
2067+
2068+
2069+
class CodeReasoningHead(Base):
2070+
"""The commit the code reasoning pass last finished reading on one branch.
2071+
2072+
A scan that finds the branch still on that commit does not read it again,
2073+
and does not even fetch it. Only a branch that moved is read, and then
2074+
only the handlers whose code changed are asked about: the cache below
2075+
answers the rest.
2076+
"""
2077+
2078+
__tablename__ = "code_reasoning_heads"
2079+
__table_args__ = (
2080+
UniqueConstraint("repository", "branch", name="uq_code_reasoning_head"),
2081+
)
2082+
2083+
id = Column(Integer, primary_key=True)
2084+
repository = Column(String(320), nullable=False, index=True)
2085+
branch = Column(String(255), nullable=False)
2086+
head = Column(String(64), default="")
2087+
at = Column(DateTime(timezone=True), default=utcnow)
2088+
2089+
20282090
class CodeReasoningCache(Base):
20292091
"""What the code reasoning pass was told about one handler, by digest.
20302092

0 commit comments

Comments
 (0)