Files
rengine/shared/models/subdomain.py
T
Yogesh OjhaandClaude Opus 5 0b853e04b6 chore: production-readiness review across the tree
A full static review of the 3.0 tree, run as two fan-out passes with every
finding verified against the running stack rather than taken on inspection.

Correctness and security
- instance settings encrypted the mask over the stored AI key: `!= MASK` never
  matched `••••••••abcd`, so saving settings destroyed the key and `test_ai`
  posted the mask as a bearer token
- the MFA token was replayable for its full five-minute life; the gate now
  checks revocation and consumes the token after a successful exchange
- logout revoked nothing for a bearer client while answering "Logged out"
- `POST /api-keys/{id}/test` returned the plaintext key inside httpx's error
  string, which embeds the whole URL
- an interest rule could be edited or deleted through another project's id
- redis ran the celery broker under `allkeys-lru`, free to evict a queued task
  or a chord counter; now `noeviction`
- nuclei's stored command carried the OAST token and every auth header
- a finalize step could poison the session and leave a scan RUNNING for ever
- `is:issue` raised TypeError on every search
- offset paging repeated and skipped rows in four dimensions
- a cancelled run kept a stale `scans.*_found` rollup
- every PDF report raised `KeyError: 'software'`
- `version_key` truncated a wide numeric run and inverted release ordering
- psycopg raises a NUL as a bare ValueError, above StatementError, so every
  savepoint guard was blind to the case it was written for

Performance
- endpoints.host had no trigram index: 35.9ms seq scan -> 0.389ms
- free text compiled to one OR, not the prescribed UNION ALL: 132ms -> 41ms
- `/http-assets/summary` and `/ips/summary` materialised whole tables
- the cross-link `ip` kind ran an estate query per page and drew nothing

Frontend
- `$effect` loaders that read the state they write, an unbounded request loop
  on a failing endpoint
- error states that rendered as empty states
- 2FA backup codes were unusable while the copy said they worked
- a "Scan history" dialog that always reported none
- ~400 duplicated lines across five filter bars, now one component

Build and tooling
- every scanner installed at @latest; all 13 pinned to the measured versions
- CI built `web/`, gone since 2.x, with tags that cannot be valid
- the prettier pre-commit hook discarded its filenames and always passed
- the test suite destroyed itself when run twice

Also: subdomains from public code, via a pinned github-subdomains source and a
GitHub API key that subfinder receives too; sublist3r and oneforall removed
after measuring one unique host in 89 across four targets.

Verified: 853 tests, ruff clean over eleven packages, svelte-check 0 errors,
the count-is-a-promise gate at 131 checks and 0 mismatches, and 143 GET routes
with no 5xx. Four migrations, each with an exercised downgrade.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DVkdx5ynZ6aof5kLFczmhX
2026-09-13 18:03:29 +05:30

275 lines
9.0 KiB
Python

import uuid
from datetime import datetime
from pydantic import BaseModel, ConfigDict
from sqlalchemy import BigInteger, Column
from sqlalchemy.types import JSON
from sqlmodel import Field, SQLModel, UniqueConstraint
from shared.definitions.asset_query import MAX_QUERY_LENGTH
from shared.models.asset_query import MatchEvidence, QueryError
from shared.models.crosslink import CrossLink
from shared.utils.datetime import utc_now
class Subdomain(SQLModel, table=True):
__tablename__ = "subdomains"
__table_args__ = (
UniqueConstraint("scan_id", "name", name="uq_subdomain_scan_name"),
)
id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True)
scan_id: uuid.UUID = Field(foreign_key="scans.id", index=True, ondelete="CASCADE")
target_id: uuid.UUID = Field(
foreign_key="targets.id", index=True, ondelete="CASCADE"
)
project_id: uuid.UUID = Field(foreign_key="projects.id", index=True)
name: str = Field(max_length=500, index=True)
sources: list = Field(default_factory=list, sa_column=Column(JSON, nullable=False))
resolved_ips: list = Field(
default_factory=list, sa_column=Column(JSON, nullable=False)
)
cname: str | None = Field(default=None, max_length=500)
is_active: bool = Field(default=False, index=True)
is_wildcard: bool = Field(default=False)
is_excluded: bool = Field(default=False, index=True)
is_important: bool = Field(default=False)
http_url: str | None = Field(default=None, max_length=2000)
final_url: str | None = Field(default=None, max_length=2000)
http_status: int | None = Field(default=None, index=True)
page_title: str | None = Field(default=None, max_length=1000)
content_type: str | None = Field(default=None, max_length=255)
content_length: int | None = Field(default=None)
response_time: float | None = Field(default=None)
webserver: str | None = Field(default=None, max_length=255)
tech: list = Field(default_factory=list, sa_column=Column(JSON, nullable=False))
is_cdn: bool = Field(default=False)
cdn_name: str | None = Field(default=None, max_length=100)
waf: str | None = Field(default=None, max_length=100)
asn: int | None = Field(default=None, sa_type=BigInteger)
asn_org: str | None = Field(default=None, max_length=255)
favicon_hash: str | None = Field(default=None, max_length=64)
tls_not_after: datetime | None = Field(default=None)
tls_checked_at: datetime | None = Field(default=None)
tls_expired: bool | None = Field(default=None)
tls_self_signed: bool | None = Field(default=None)
screenshot_path: str | None = Field(default=None, max_length=500)
screenshot_phash: int | None = Field(
default=None, sa_column=Column(BigInteger, nullable=True)
)
hygiene_issues: list | None = Field(
default=None, sa_column=Column(JSON(none_as_null=True), nullable=True)
)
hygiene_checked: list | None = Field(
default=None, sa_column=Column(JSON(none_as_null=True), nullable=True)
)
interest_score: int = Field(default=0, index=True)
interest_band: str | None = Field(default=None, max_length=16, index=True)
interest_kinds: list = Field(
default_factory=list, sa_column=Column(JSON, nullable=False)
)
discovered_at: datetime = Field(default_factory=utc_now)
created_at: datetime = Field(default_factory=utc_now)
class SubdomainRead(BaseModel):
id: uuid.UUID
scan_id: uuid.UUID
target_id: uuid.UUID
name: str
sources: list[str] = Field(default_factory=list)
resolved_ips: list[str] = Field(default_factory=list)
cname: str | None = None
is_active: bool
is_wildcard: bool
is_excluded: bool = False
is_important: bool = False
http_url: str | None = None
final_url: str | None = None
http_status: int | None = None
page_title: str | None = None
content_type: str | None = None
content_length: int | None = None
response_time: float | None = None
webserver: str | None = None
tech: list[str] = Field(default_factory=list)
is_cdn: bool = False
cdn_name: str | None = None
waf: str | None = None
asn: int | None = None
asn_org: str | None = None
favicon_hash: str | None = None
tls_not_after: datetime | None = None
tls_expired: bool | None = None
tls_self_signed: bool | None = None
screenshot_path: str | None = None
hygiene_issues: list[str] = Field(default_factory=list)
hygiene_checked: list[str] = Field(default_factory=list)
interest_score: int = 0
interest_band: str | None = None
interest_kinds: list[str] = Field(default_factory=list)
discovered_at: datetime
class SubdomainRow(SubdomainRead):
target_value: str | None = None
ports: list[int] = Field(default_factory=list)
endpoint_count: int = 0
title_count: int = 0
title_targets: int = 1
favicon_count: int = 0
favicon_targets: int = 1
render_hash: str | None = None
render_count: int = 0
render_targets: int = 1
vuln_count: int = 0
vuln_severity: str | None = None
vuln_kev: bool = False
matched_in: list[MatchEvidence] = Field(default_factory=list)
cross_links: list[CrossLink] = Field(default_factory=list)
class SubdomainSummary(BaseModel):
total: int
active: int
sources: dict[str, int] = Field(default_factory=dict)
class Facet(BaseModel):
value: str
label: str
count: int
class SubdomainFilter(BaseModel):
model_config = ConfigDict(extra="forbid")
q: str | None = Field(default=None, max_length=MAX_QUERY_LENGTH)
statuses: list[str] = Field(default_factory=list, max_length=10)
tech: list[str] = Field(default_factory=list, max_length=200)
services: list[str] = Field(default_factory=list, max_length=200)
cert: list[str] = Field(default_factory=list, max_length=10)
hygiene: list[str] = Field(default_factory=list, max_length=40)
sources: list[str] = Field(default_factory=list, max_length=200)
cdn: str = Field(default="any", max_length=10)
waf: str = Field(default="any", max_length=20)
live: bool = False
screenshot: bool = False
issues: bool = False
new: bool = False
sort: str = Field(default="name", max_length=20)
order: str = Field(default="asc", max_length=4)
limit: int = Field(default=50, ge=1, le=200)
offset: int = Field(default=0, ge=0, le=100_000_000)
def has_facets(self) -> bool:
return bool(
self.statuses
or self.tech
or self.services
or self.cert
or self.hygiene
or self.sources
or self.cdn != "any"
or self.waf != "any"
or self.live
or self.screenshot
or self.issues
or self.new
)
class SubdomainSearchResult(BaseModel):
items: list[SubdomainRow] = Field(default_factory=list)
total: int = 0
total_capped: bool = False
error: QueryError | None = None
class RenderGroup(BaseModel):
"""Web assets whose screenshots render the same page."""
hash: str
label: str | None = None
count: int = 0
hosts: list[str] = Field(default_factory=list)
screenshot_path: str | None = None
query: str
class RenderGroups(BaseModel):
groups: list[RenderGroup] = Field(default_factory=list)
total_groups: int = 0
grouped: int = 0
ungrouped: int = 0
blank: int = 0
unrendered: int = 0
error: QueryError | None = None
class SubdomainFacets(BaseModel):
status: list[Facet] = Field(default_factory=list)
tech: list[Facet] = Field(default_factory=list)
service: list[Facet] = Field(default_factory=list)
source: list[Facet] = Field(default_factory=list)
cert: list[Facet] = Field(default_factory=list)
hygiene: list[Facet] = Field(default_factory=list)
class HygieneCheckCount(BaseModel):
key: str
failing: int = 0
applicable: int = 0
query: str
class HygieneSummary(BaseModel):
hosts: int = 0
evaluated: int = 0
pending: int = 0
clean: int = 0
warning: int = 0
info: int = 0
checks: list[HygieneCheckCount] = Field(default_factory=list)
class SubdomainRelation(BaseModel):
kind: str
reason: str
value: str
label: str = ""
query: str = ""
targets: int = 1
hosts: list[str] = Field(default_factory=list)
total: int = 0
class TargetSubdomainRead(BaseModel):
name: str
sources: list[str] = Field(default_factory=list)
resolved_ips: list[str] = Field(default_factory=list)
cname: str | None = None
is_active: bool
is_wildcard: bool
is_excluded: bool = False
is_important: bool = False
http_status: int | None = None
page_title: str | None = None
content_length: int | None = None
response_time: float | None = None
webserver: str | None = None
tech: list[str] = Field(default_factory=list)
is_cdn: bool = False
cdn_name: str | None = None
waf: str | None = None
tls_not_after: datetime | None = None
tls_expired: bool | None = None
screenshot_path: str | None = None
scan_count: int
last_scan_id: uuid.UUID
first_seen: datetime
last_seen: datetime