Skip to content

Fix/env variable underscore mapping - #50

Merged
HC-ONLINE merged 3 commits into
mainfrom
fix/env-variable-underscore-mapping
Aug 11, 2026
Merged

Fix/env variable underscore mapping#50
HC-ONLINE merged 3 commits into
mainfrom
fix/env-variable-underscore-mapping

Conversation

@HC-ONLINE

Copy link
Copy Markdown
Owner

Summary

Fix ConfigLoader environment variable mapping so that config fields containing underscores can be overridden via CIBERWEBSCAN_ env vars, and unmappable/invalid variables are logged instead of being silently ignored.

Problem

ConfigLoader._load_env replaced every underscore in the env var name with a dot (_.), which caused:

  1. Fields with underscores could never be set via env vars: CIBERWEBSCAN_ATTACK_COMMAND_INJECTION, CIBERWEBSCAN_HTTP_RETRY_MAX_ATTEMPTS, CIBERWEBSCAN_HTTP_RATE_LIMIT_ADAPTIVE, etc. mapped to non-existent paths (attack.command.injection) and were silently ignored.
  2. Red SQLi integration tests: the test helper could not disable command_injection/subdomain via env vars, so --sqli also ran the command injection attack; combined with the test server returning 500s for SQLi payloads, the retry logic (exponential backoff) plus the adaptive AIMD rate limiter pushed each scan to ~50ssubprocess.TimeoutExpired after 30s.
  3. Whole-section env vars could silently nuke the entire config: CIBERWEBSCAN_HTTP_PROXY produced a ValidationError and the loader fell back to defaults with no log.

Changes

  • src/ciberwebscan/config/loader.py
    • Schema-guided resolution (longest field match first): CIBERWEBSCAN_ATTACK_COMMAND_INJECTIONattack.command_injection.
    • Double-underscore convention (__) to explicitly mark section boundaries: CIBERWEBSCAN_HTTP__RATE_LIMIT__REQUESTS_PER_SECONDhttp.rate_limit.requests_per_second (previously agreed solution, see docs/CONFIGURATION.md).
    • Legacy fallback (_.) is now validated against the schema; unmappable vars — including whole sections — are ignored and logged at debug level: Ignoring env var ... does not map to a config key.
    • Proper handling of PEP 604 unions (ProxyConfig | None) across Python 3.10–3.12 (types.UnionType vs typing.Union).
    • Invalid values on valid keys now log the Pydantic validation error before falling back to defaults (previously silent).
  • tests/unit/config/test_loader.py (new, 27 tests): legacy mapping, underscore-containing fields, __ convention, unmappable vars (ignored + logged), invalid values, case-insensitivity.
  • tests/integration/cli/test_attack_cli.py: the helper now also disables subdomain and command_injection and caps retries to keep scans within the 30s subprocess timeout.
  • Docs: updated docs/CONFIGURATION.md, .env.example, and AGENTS.md.

Verification

  • 27 loader unit tests pass
  • 1243 unit tests pass (1 skipped)
  • All 24 CLI integration tests pass — the 3 SQLi tests went from ~50s (timeout) to 7–9s
  • ruff, prettier, pyright, pre-commit hooks all green

The ConfigLoader mapped every underscore in an env var name to a dot,
so fields that contain underscores (command_injection, max_attempts,
requests_per_second, etc.) could never be overridden via environment
variables and were silently ignored.

Map env vars by walking the Pydantic schema (longest field match first),
keep the legacy underscore-to-dot fallback for compatibility, and
support the double-underscore convention (CIBERWEBSCAN_HTTP__RATE_LIMIT__ADAPTIVE)
to disambiguate section boundaries. Unmappable variables are ignored
with a debug log instead of risking a config-wide fallback.

Also update the CLI integration test helper to disable command
injection and subdomain attacks and cap retries, keeping scans within
the 30s subprocess timeout (SQLi tests went from ~50s to ~9s).
get_origin returns types.UnionType (not typing.Union) on Python 3.11+,
so Optional nested models like ProxyConfig were never recognized as
nested and whole-section env keys (e.g. CIBERWEBSCAN_HTTP_PROXY) leaked
into the config and could trigger a silent full fallback to defaults.

Also validate legacy fallback paths against the schema and log env vars
that cannot be mapped, and log validation errors before the fallback.
Cover the schema-guided resolution, the double-underscore convention,
whole-section rejection (e.g. CIBERWEBSCAN_HTTP_PROXY), the debug log for
unmappable variables and the error log + defaults fallback for invalid
values.
@HC-ONLINE
HC-ONLINE merged commit eeda4dd into main Aug 11, 2026
3 checks passed
@HC-ONLINE
HC-ONLINE deleted the fix/env-variable-underscore-mapping branch August 11, 2026 19:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant