Skip to content

fix: make calculate() tolerate omitted optional inputs#29

Open
dmchaledev wants to merge 1 commit into
mainfrom
claude/sleepy-rubin-n4xig1
Open

fix: make calculate() tolerate omitted optional inputs#29
dmchaledev wants to merge 1 commit into
mainfrom
claude/sleepy-rubin-n4xig1

Conversation

@dmchaledev

Copy link
Copy Markdown
Contributor

Problem

The exported pure calculate() API crashes or returns garbage when called with the very fields that the published index.d.ts marks as optional:

export interface VulnInputs {
  scan_window?: number;        // optional
  compliance_needs?: string[]; // optional
  ...
}

Verified against the current code:

Input Result before
calculate({ target_hosts, scan_intensity, scan_frequency, scan_window, scanning_tools }) — omit compliance_needs throws TypeError: Cannot read properties of undefined (reading 'length') (calcAsmResourcesdata.compliance_needs.length)
omit scan_window scan_window_utilization: NaN, efficiency_rating: "poor" (bogus)

So any consumer who follows the published type contract and omits an optional field gets a crash or nonsense output. The DOM path is unaffected because _collectInputs() already fills/clamps these — but the pure function (a documented public export) had no such guard.

Fix

Add a normalizeInputs() step at the entry of the calculation engine that:

  • defaults scan_intensity (medium), scan_frequency (weekly), scan_window (8)
  • clamps target_hosts to 1–50000 and scan_window to 1–24 — the same limits the form UI already enforces in _collectInputs(), now centralized so the API and UI agree
  • coerces non-array scanning_tools / compliance_needs to []
  • preserves an empty scanning_tools list as "no tools" → zero tool cost (existing documented behavior; the web component keeps its own openvas fallback in _run())

No behavior change for valid in-range inputs — all 65 pre-existing tests still pass.

Tests

Added 6 regression tests (Input normalization & optional fields) covering omitted compliance_needs/scan_window, non-array values, out-of-range clamping, and a fully empty {} input.

# tests 71
# pass 71
# fail 0

🤖 Generated with Claude Code

https://claude.ai/code/session_01VyuV5gj943Td821veoK9AL


Generated by Claude Code

The exported calculate() API crashed or produced NaN when called with the
fields that index.d.ts marks as optional:

  - omitting `compliance_needs` threw "Cannot read properties of undefined
    (reading 'length')"
  - omitting `scan_window` produced NaN window utilization and a bogus
    "poor" efficiency rating

Add a normalizeInputs() step that defaults and clamps inputs to the same
limits the form UI already enforces (target_hosts 1–50000, scan_window
1–24), coerces non-array tool/compliance lists, and supplies sensible
defaults. Empty scanning_tools is preserved as "no tools" (zero cost);
the web component keeps its own openvas fallback.

Add 6 regression tests covering omitted/invalid/out-of-range inputs.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VyuV5gj943Td821veoK9AL
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.

2 participants