Files
rengine/shared/services/scan_factory.py
T
Yogesh Ojha a548afffa2 feat(settings): tables that save on change, Users, sheets on their pages
Settings has six tabs: General, API keys, Proxies, Notifications, AI and
Users. Each tab is one card whose controls save on change; list tabs are
tables with a row menu and their Add action in the tab row. A non-admin
sees General and AI, read-only.

Out-of-band testing is the Callback server sheet on the Arsenal Nuclei
tab, with a Need a callback count that filters the library to the same
rows. Bounty Hub settings are a sheet on the Bounty Hub page. The old
settings routes redirect to both.

- API keys store the last test result on the row and clear it when the
  key changes. Providers report whether a test exists. The usage count
  is gone: only ViewDNS ever incremented it.
- Notification channels store the last delivery, written by the
  notifier on its own connection. A channel offers the seven event
  types the product emits, program changes on by default, and levels
  named All events, Warning and critical, Critical only.
- A default proxy applies to a scan launched without a context and is
  preselected for new contexts. A pool hands each scan one endpoint,
  proxy URLs bracket IPv6 hosts, and deleting a proxy clears it from
  its contexts.
- Users can be added with a role, disabled and promoted. An account
  cannot change itself, and one that owns records answers 409.
- Retention windows are validated and include keep forever. The
  instance name cannot be blank and names every page title.
- The AI list drops the two switches nothing read; each feature states
  what it sends. The out-of-band Off help says the checks are skipped.

Connectors, Bounty Hub and Remote control lose their extra page
padding. Migration 5d2e9a8c1b47 adds the test and delivery columns and
drops proxies.mode.

Claude-Session: https://claude.ai/code/session_01RVAHQxZ7JAkoEiTCDiHYue
2026-09-27 10:01:19 +05:30

125 lines
3.8 KiB
Python

import uuid
from shared.enums.scan import ScanScope, ScanStatus
from shared.models.scan import Scan
from shared.services import target_seeds
from shared.services.proxy_resolve import scan_proxy_url
from shared.services.scan_resolve import (
ResolvedScanConfig,
_mask_auth,
merge_engine_context,
seal_headers,
)
class ScanFactoryError(Exception):
pass
def build_scan_row(
*,
resolved: ResolvedScanConfig,
engine,
context,
target,
project_id: uuid.UUID,
created_by: uuid.UUID,
schedule_id: uuid.UUID | None = None,
schedule_type: str | None = None,
parent_scan_id: uuid.UUID | None = None,
run_group_id: uuid.UUID | None = None,
dimension: str | None = None,
) -> Scan:
"""Assemble an unsaved PENDING Scan from an already-resolved config."""
execution_config = resolved.model_dump()
execution_config["headers"] = seal_headers(execution_config.get("headers"))
if dimension:
execution_config["_dimension"] = dimension
execution_config["_auth_header_names"] = list(resolved._auth_header_names)
execution_config["_auth"] = (
_mask_auth(context.auth) if context is not None else {"auth_type": "none"}
)
return Scan(
project_id=project_id,
target_id=target.id,
engine_id=engine.id,
engine_name=engine.name,
context_id=context.id if context is not None else None,
context_name=context.name if context is not None else None,
execution_config=execution_config,
status=ScanStatus.PENDING.value,
subdomains_found=0,
ips_found=0,
open_ports_found=0,
vulnerabilities_found=0,
endpoints_found=0,
created_by=created_by,
schedule_id=schedule_id,
schedule_type=schedule_type,
scope=(ScanScope.FOCUSED.value if resolved.seed_only else ScanScope.FULL.value),
parent_scan_id=parent_scan_id,
run_group_id=run_group_id,
)
def build_scan_for_target_sync(
session,
*,
project_id: uuid.UUID,
target_id: uuid.UUID,
engine_id: uuid.UUID,
context_id: uuid.UUID | None,
created_by: uuid.UUID,
schedule_id: uuid.UUID | None = None,
schedule_type: str | None = None,
intensity: str | None = None,
) -> Scan:
"""Resolve engine/context/target on a sync Session and flush a PENDING Scan."""
from shared.models.scan_context import ScanContext # noqa: PLC0415
from shared.models.scan_engine import ScanEngine # noqa: PLC0415
from shared.models.target import Target # noqa: PLC0415
engine = session.get(ScanEngine, engine_id)
if engine is None or engine.project_id != project_id:
msg = "scan engine not found"
raise ScanFactoryError(msg)
context = None
if context_id is not None:
context = session.get(ScanContext, context_id)
if context is None or context.project_id != project_id:
msg = "scan context not found"
raise ScanFactoryError(msg)
target = session.get(Target, target_id)
if target is None or target.project_id != project_id:
msg = "target not found"
raise ScanFactoryError(msg)
proxy_url = scan_proxy_url(session, context)
resolved = merge_engine_context(
engine,
context,
target.target_value,
target.target_type.value,
proxy_url=proxy_url,
intensity=intensity,
)
if target.seed_scans:
stored = target_seeds.load_sync(session, [target.id])
target_seeds.apply(resolved, stored.get(target.id) or [], seed_only=False)
scan = build_scan_row(
resolved=resolved,
engine=engine,
context=context,
target=target,
project_id=project_id,
created_by=created_by,
schedule_id=schedule_id,
schedule_type=schedule_type,
)
session.add(scan)
session.flush()
return scan