FEAT Restructuring Attacks#1059
Merged
bashirpartovi merged 8 commits intomicrosoft:mainfrom Aug 8, 2025
Merged
Conversation
Contributor
|
Can you change this PR description slightly and publish as a blog? |
rlundeen2
approved these changes
Aug 8, 2025
Contributor
rlundeen2
left a comment
There was a problem hiding this comment.
Really like these changes. It's also a lot of code and a lot I could have missed. But signing off on the structure, pattern, and approach.
I don't mind merging and then fixing bugs.
romanlutz
reviewed
Aug 8, 2025
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.
Major Refactor: Extracting Strategy Pattern and Creating Prompt Generator Module
Overview
This is a significant architectural refactor that addresses design inconsistencies in our attack system and introduces a new module for generators that don't follow the traditional attack pattern.
Background and Motivation
Problem Statement
The original
pyrit.attacksmodule contained several components that didn't align with the core attack pattern:Design Philosophy
True attacks have a specific target and objective they're trying to achieve, while generators create variations or new content based on input. This fundamental difference warranted separate architectural patterns.
Solution Architecture
1. Strategy Pattern Extraction
We extracted the core execution logic from
AttackStrategyinto a genericStrategypattern:Strategy: Base abstract class for all execution strategiesStrategyContext: Base context for strategy executionStrategyResult: Base result type for all strategiesThis extraction follows the principle of composition over inheritance and makes the codebase more modular.
2. AttackStrategy Refactoring
AttackStrategynow extendsStrategy, inheriting the core execution pattern while adding attack-specific functionality:AttackContext) with objectives, memory labels, and conversation references3. New Prompt Generator Module
Created
pyrit.executor.promptgenmodule containing:PromptGeneratorStrategy: Base class for generators extendingStrategyFuzzerGenerator: Refactored from FuzzerAttack, properly designed as a generatorAnecdoctorGenerator: Refactored from AnecdoctorAttack, following generator patternsThese components now have clean, purpose-built APIs that reflect their true nature as content generators.
Key Technical Improvements
Enhanced
execute_asyncOverloadingThe new pattern provides better
execute_asyncmethod overloading:This design:
Event System Architecture
The new event system provides:
Robust Parameter Handling with
get_kwarg_paramThe use of
get_kwarg_paramutility function is a safety mechanism, not an anti-pattern:**kwargsThis approach is necessary because Python's dynamic nature allows any type to be passed at runtime, and we need to validate parameters that come through the generic
**kwargsinterface.Migration Impact
Backward Compatibility
New Capabilities
Code Organization
Before
After
Benefits
Architectural Clarity: Clear separation between attacks (which have objectives) and generators (which create content)
Code Maintainability: Each component follows its natural design patterns instead of being forced into inappropriate abstractions
Extensibility: The Strategy pattern and event system make it easy to add new execution patterns and custom behaviors
Type Safety: Better type checking and parameter validation throughout the system
Testing: More focused, testable components with clear responsibilities
Future Implications
This refactor establishes a solid foundation for:
The Strategy pattern provides a clean extension point for future execution paradigms while maintaining backward compatibility with existing attack infrastructure.
Example Screenshots
New Fuzzer example and its new printer: