fix(security): add token budget limiting and LLM validator sanitization#2057
Open
Mr-Neutr0n wants to merge 2 commits into567-labs:mainfrom
Open
Conversation
Address security findings from issue 567-labs#2056: 1. Retry Amplification Mitigation: - Add optional `token_budget` parameter to retry functions - Add `TokenBudgetExceeded` exception raised when budget is exceeded - Add `get_total_tokens()` helper to extract tokens from usage objects - Prevents runaway costs from adversarial responses that repeatedly fail validation 2. LLM Validator Injection Protection: - Add explicit delimiters around user values in validation prompts - Escape delimiter characters (```, ---) in user input - Use structured format to separate user value from validation rules - Prevents prompt injection attacks in llm_validator Fixes 567-labs#2056
Author
|
Friendly follow-up - is there anything I can improve in this PR? Happy to address any feedback. |
Author
|
Friendly bump! Let me know if there's anything I should update or improve to help move this forward. |
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.
Summary
Addresses security findings from issue #2056:
Retry Amplification Mitigation (Medium severity)
token_budgetparameter tocreate()callsTokenBudgetExceededexception provides detailed context (budget, tokens used, attempts)LLM Validator Injection Protection (Medium severity)
---BEGIN VALUE---/---END VALUE---)\``and---`)Usage
Changes
instructor/core/exceptions.py: AddedTokenBudgetExceededexceptioninstructor/core/retry.py: Addedtoken_budgetparameter andget_total_tokens()helperinstructor/core/patch.py: Wiredtoken_budgetthrough to retry functionsinstructor/validation/llm_validators.py: Added input sanitization and structured promptsinstructor/__init__.py: ExportedTokenBudgetExceededTest Plan
test_token_budget_exceededandtest_token_budget_exceeded_inherits_from_instructor_errortotests/test_exceptions.pytests/test_security_fixes.pywith tests forget_total_tokens()helper and sanitization logicFixes #2056