Skip to content

Fix critical validator bugs and eliminate code duplication#1

Draft
nicocti with Copilot wants to merge 4 commits into
mainfrom
copilot/identify-improve-slow-code
Draft

Fix critical validator bugs and eliminate code duplication#1
nicocti with Copilot wants to merge 4 commits into
mainfrom
copilot/identify-improve-slow-code

Conversation

Copilot AI commented Nov 3, 2025

Copy link
Copy Markdown

Found and fixed 3 critical bugs causing validation failures, plus eliminated ~150 lines of duplicate validation code across 11 classes.

Critical Bugs Fixed

Hex field validator - Logic inverted, rejected all valid hex fields:

# Before: raised error when field name DID end with _hex
if info.field_name and info.field_name.endswith("_hex"):
    raise ValueError(...)

# After: raises error when field name does NOT end with _hex  
if info.field_name and not info.field_name.endswith("_hex"):
    raise ValueError(...)

Hash key validation - Boolean logic error prevented custom hash types:

# Before: inverted logic always failed
if self.__pydantic_extra__ and any(
    not (len(key) > 250 or len(key) < 3 or StixKeyPattern.match(key))
    for key in self.__pydantic_extra__.keys()
):
    raise ValueError("Invalid extra hash key.")

# After: correctly validates 3-250 char keys matching pattern
if self.__pydantic_extra__:
    for key in self.__pydantic_extra__.keys():
        if len(key) < 3 or len(key) > 250 or not StixKeyPattern.match(key):
            raise ValueError("Invalid extra hash key.")

Code Duplication Eliminated

Created factory functions for common validators (common_validators.py):

  • create_first_last_seen_validator() - replaces 5 duplicate implementations
  • create_modified_after_created_validator() - replaces 4 duplicate implementations
  • create_at_least_one_property_validator() - template for 10+ more refactorings

Before:

class Campaign(StixDomain):
    @model_validator(mode="after")
    def validate_last_seen_after_first_seen(self) -> Self:
        if self.first_seen and self.last_seen and self.first_seen > self.last_seen:
            raise ValueError(...)
        return self

After:

class Campaign(StixDomain):
    _validate_last_seen = create_first_last_seen_validator()

Classes Refactored

  • Base types: StixDomain, StixRelationship, StixLanguage, StixExtension
  • SDO: Campaign, Infrastructure, IntrusionSet, Malware, ThreatActor
  • SCO: WindowsPEOptionalHeader (template for remaining classes)

Performance maintained at ~250k validations/sec.

Original prompt

Identify and suggest improvements to slow or inefficient code


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits November 3, 2025 21:33
Co-authored-by: nicocti <15033773+nicocti@users.noreply.github.com>
Co-authored-by: nicocti <15033773+nicocti@users.noreply.github.com>
Co-authored-by: nicocti <15033773+nicocti@users.noreply.github.com>
Copilot AI changed the title [WIP] Identify and suggest improvements for inefficient code Fix critical validator bugs and eliminate code duplication Nov 3, 2025
Copilot AI requested a review from nicocti November 3, 2025 21:43
@nicocti
nicocti force-pushed the main branch 4 times, most recently from 619eb4a to e7fd437 Compare November 8, 2025 23:31
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