fix: make calculate() tolerant of omitted optional inputs#25
Open
dmchaledev wants to merge 1 commit into
Open
Conversation
The public calculate() API contradicted its own type declarations
(index.d.ts marks scan_window, compliance_needs, scan_intensity,
scan_frequency, and scanning_tools as optional):
- Omitting compliance_needs threw `TypeError: Cannot read properties
of undefined (reading 'length')`.
- Omitting scan_window produced a NaN scan_window_utilization and a
bogus "poor" efficiency rating.
Normalize raw inputs once at the top of the calculation engine,
applying the same defaults and range clamps the web component uses
(target_hosts 1-50000, scan_window 1-24, medium/weekly defaults,
empty arrays for tools/compliance). The Shadow DOM path is unaffected
since _collectInputs already supplies complete, clamped values.
Adds 5 regression tests covering omitted optional fields, a bare
{ target_hosts } input, and range clamping. Updates README input
schema and CHANGELOG.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Egbm6uvXq2wZHw4Pbdg7c8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The public, exported
calculate()function contradicts its own published type declarations.index.d.tsmarksscan_window,compliance_needs,scan_intensity,scan_frequency, andscanning_toolsas optional, but the runtime requires several of them:compliance_needsthrowsTypeError: Cannot read properties of undefined (reading 'length')(e.g.hailbytes-vuln-calculator.js:76).scan_windowsilently corrupts the result —scan_window_utilizationbecomesNaNand the efficiency rating falls through to a bogus"poor".Reproduction (with the test suite's DOM shim):
Anyone using the documented programmatic API (
import { calculate }) per the TS types hits this immediately.Fix
Add a single
normalizeInputs()step at the top of the calculation engine that applies the same defaults and range clamps the web component's_collectInputs()already uses:target_hosts→ clamped 1–50,000 (default 1000)scan_window→ clamped 1–24 (default 8)scan_intensity→ defaultmedium,scan_frequency→ defaultweeklyscanning_tools/compliance_needs→ default to[]when missing or non-arrayThe Shadow DOM UI path is unaffected, since
_collectInputs()already supplies complete, clamped values — normalization is a no-op for it.Tests
Adds 5 regression tests (omitted
compliance_needs, omittedscan_window→ finite utilization, default-equivalence with explicit8, a bare{ target_hosts }input, and clamping of out-of-range values). Full suite: 70 passing (was 65).Also updates the README input schema (notes which fields are optional + the defaults) and the CHANGELOG.
🤖 Generated with Claude Code
https://claude.ai/code/session_01Egbm6uvXq2wZHw4Pbdg7c8
Generated by Claude Code