mirror of
https://github.com/yogeshojha/rengine.git
synced 2026-10-20 15:21:30 +02:00
The comment rule in CLAUDE.md is one line maximum, never narration: no multi-line blocks, no module-header explainers, no sentences that explain or justify the code to the reader. The tree had drifted a long way from it, and most of the drift is mine. Removed: 232 lines of multi-line comment blocks 203 single-line comments that argued instead of stating 154 comments joined by so/because/otherwise/rather-than 143 narrating comments in the frontend 112 test docstrings (the test name already says what it asserts) 25 test module docstrings and 25 decorative section dividers Collapsed: 324 multi-line docstrings to one factual line 271 one-line docstrings that had a justifying clause bolted on Kept: functional directives, section dividers, short field labels, alembic revision headers, and the eight FastAPI route docstrings that render in /docs. Where a fact was load-bearing it is in CLAUDE.md, which is where it belongs. 284 python tests and 34 frontend tests pass, ruff and ruff-format clean across ten packages, svelte-check 0 errors, prettier and eslint clean, app.main imports. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013dKUV96WkQQETZBMoAG98C
112 lines
2.8 KiB
Python
112 lines
2.8 KiB
Python
"""Every IP a scan knows about, materialised into ip_addresses and enriched in the same write."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import ipaddress
|
|
import uuid
|
|
from typing import TYPE_CHECKING
|
|
|
|
from sqlalchemy import text
|
|
from sqlalchemy.dialects.postgresql import insert
|
|
|
|
from shared.enums.ip import IpSource
|
|
from shared.models.ip_address import IpAddress
|
|
from shared.services.ip_asn import enrich_addresses
|
|
from shared.utils.datetime import utc_now
|
|
|
|
if TYPE_CHECKING:
|
|
from sqlalchemy.orm import Session
|
|
|
|
MAX_IPS = 100_000
|
|
|
|
# every table a scan can park an IP in
|
|
_COLLECT_SQL = """
|
|
SELECT ip FROM subdomains s,
|
|
LATERAL jsonb_array_elements_text(cast(s.resolved_ips AS jsonb)) ip
|
|
WHERE s.scan_id = :sid
|
|
UNION
|
|
SELECT ip FROM http_assets WHERE scan_id = :sid AND ip IS NOT NULL
|
|
UNION
|
|
SELECT ip FROM ports WHERE scan_id = :sid
|
|
UNION
|
|
SELECT ip FROM ip_addresses WHERE scan_id = :sid
|
|
"""
|
|
|
|
|
|
def collect_ips(session: Session, scan_id: uuid.UUID) -> list[str]:
|
|
rows = session.execute(text(_COLLECT_SQL).bindparams(sid=scan_id)).scalars()
|
|
seen: dict[str, None] = {}
|
|
for raw in rows:
|
|
value = (raw or "").strip()
|
|
if not value or value in seen:
|
|
continue
|
|
try:
|
|
ipaddress.ip_address(value)
|
|
except ValueError:
|
|
continue
|
|
seen[value] = None
|
|
if len(seen) >= MAX_IPS:
|
|
break
|
|
return list(seen)
|
|
|
|
|
|
def materialize(
|
|
session: Session,
|
|
*,
|
|
scan_id: uuid.UUID,
|
|
target_id: uuid.UUID,
|
|
project_id: uuid.UUID,
|
|
ips: list[str],
|
|
source: str = IpSource.DNS_RESOLUTION.value,
|
|
) -> int:
|
|
"""Insert missing ip_addresses rows."""
|
|
if not ips:
|
|
return 0
|
|
now = utc_now()
|
|
ips = sorted(set(ips))
|
|
rows = [
|
|
{
|
|
"id": uuid.uuid4(),
|
|
"scan_id": scan_id,
|
|
"target_id": target_id,
|
|
"project_id": project_id,
|
|
"ip": ip,
|
|
"version": ipaddress.ip_address(ip).version,
|
|
"source": source,
|
|
"ptr_hostnames": [],
|
|
"is_cdn": False,
|
|
"discovered_at": now,
|
|
"created_at": now,
|
|
}
|
|
for ip in ips
|
|
]
|
|
statement = (
|
|
insert(IpAddress)
|
|
.values(rows)
|
|
.on_conflict_do_nothing(constraint="uq_ipaddress_scan_ip")
|
|
)
|
|
result = session.execute(statement)
|
|
session.commit()
|
|
enrich_addresses(session, scan_id=scan_id, ips=ips)
|
|
session.commit()
|
|
return int(result.rowcount or 0)
|
|
|
|
|
|
def ensure(
|
|
session: Session,
|
|
*,
|
|
scan_id: uuid.UUID,
|
|
target_id: uuid.UUID,
|
|
project_id: uuid.UUID,
|
|
) -> list[str]:
|
|
"""Collect every known IP and guarantee a row for each."""
|
|
ips = collect_ips(session, scan_id)
|
|
materialize(
|
|
session,
|
|
scan_id=scan_id,
|
|
target_id=target_id,
|
|
project_id=project_id,
|
|
ips=ips,
|
|
)
|
|
return ips
|