Skip to content

fix(policies): guard extractPresetEntries against null/undefined input#1042

Open
Junior00619 wants to merge 1 commit intoNVIDIA:mainfrom
Junior00619:fix/policy-extract-null-guard
Open

fix(policies): guard extractPresetEntries against null/undefined input#1042
Junior00619 wants to merge 1 commit intoNVIDIA:mainfrom
Junior00619:fix/policy-extract-null-guard

Conversation

@Junior00619
Copy link
Copy Markdown
Contributor

@Junior00619 Junior00619 commented Mar 28, 2026

Problem

extractPresetEntries() throws an unhandled TypeError: Cannot read properties of null (reading 'match') when called with null or undefined. The function is exported as part of the public module API but lacks the same defensive guard that its sibling parseCurrentPolicy() already implements.

The current call site in applyPreset() is protected indirectly — loadPreset() returns null and the caller bails before reaching extractPresetEntries() — but direct consumers of the exported function hit the crash:

const { extractPresetEntries } = require("./policies");
extractPresetEntries(null); // TypeError

Fix
Add an early-return null guard consistent with the existing parseCurrentPolicy() pattern (line 79). One line, zero behavioral change for the happy path.

Tests
Added 15 regression tests for two exported pure functions that previously had zero direct test coverage:

extractPresetEntries (8 tests): null, undefined, empty string, missing section, extraction correctness, trailing whitespace stripping, real preset integration, metadata exclusion
parseCurrentPolicy (7 tests): null, undefined, empty string, metadata stripping, no-separator passthrough, whitespace trimming, edge-case separator position
All 37 policy tests pass. Full suite: 615 pass, 2 pre-existing failures in install-preflight.test.js (unrelated).

extractPresetEntries() crashes with TypeError when called with null or
undefined because it calls .match() on the input without a falsy check.
While applyPreset() guards against this via loadPreset() returning null
first, the function is exported and callable directly — any caller
passing an unexpected falsy value hits an unhandled crash.

Add an early return for falsy input, consistent with the existing
parseCurrentPolicy() pattern which already guards against null/empty.

Add 15 regression tests covering both extractPresetEntries (8 tests)
and parseCurrentPolicy (7 tests), which previously had zero direct
test coverage despite being exported pure functions.
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 28, 2026

📝 Walkthrough

Walkthrough

A defensive improvement to extractPresetEntries() that early-returns null for falsy input before regex matching, combined with comprehensive test coverage for both extractPresetEntries() and parseCurrentPolicy() functions, validating behavior for edge cases and real preset data.

Changes

Cohort / File(s) Summary
Defensive Input Handling
bin/lib/policies.js
Added early null-check in extractPresetEntries() to short-circuit before regex matching when input is falsy.
Test Coverage Expansion
test/policies.test.js
Added 115 lines of test cases for extractPresetEntries() and parseCurrentPolicy(), covering null/undefined/empty inputs, YAML section extraction, metadata header removal, separator handling, and validation against all real presets.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A null-check hops in line,
Before the regex dares to shine,
Tests now dance in every case,
Presets parsed at proper pace! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title directly and clearly describes the main change: adding a guard against null/undefined input to extractPresetEntries, which is exactly what the one-line code change accomplishes.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands and usage tips.

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