shared/redaction: redact_value() doesn't handle pattern.sub() exceptions — crashes on malformed patterns #10567

Open
opened 2026-04-18 17:20:36 +00:00 by HAL9000 · 0 comments
Owner

Metadata

Commit Message:

fix(shared/redaction): add exception handling to redact_value() for pattern.sub() failures

Branch Name: fix/redaction-pattern-exception-handling

Background and Context

The redact_value() function in src/cleveragents/shared/redaction.py (lines 145-157) calls pattern.sub() without exception handling. If a registered pattern causes an exception during substitution—due to a malformed regex pattern, ReDoS (Regular Expression Denial of Service) attack, timeout, or memory exhaustion—the function will crash and propagate the exception up the call stack.

Since this function is called from secrets_masking_processor() which is integrated into the structlog pipeline, an unhandled exception in pattern substitution can crash the entire application's logging system, potentially causing cascading failures.

Expected Behavior

The redact_value() function should gracefully handle exceptions during pattern substitution:

  • If a pattern fails to substitute, the function should log a warning and continue processing with the next pattern
  • The function should never crash due to a malformed or problematic pattern
  • The original value should be returned (with partial redactions from successful patterns applied)
  • The application should remain stable and continue operating

Acceptance Criteria

  • Exception handling is added around pattern.sub() calls in redact_value()
  • When a pattern fails, a warning is logged with the exception details
  • Processing continues with the next pattern after a failure
  • Unit tests verify graceful handling of:
    • ReDoS patterns (e.g., (a+)+b)
    • Invalid regex patterns
    • Timeout scenarios (if applicable)
    • Memory exhaustion scenarios (if applicable)
  • Integration tests verify the function works correctly in the structlog pipeline
  • No exceptions propagate from redact_value() to the caller
  • Code coverage remains >= 97%

Subtasks

  • Add try-except block around pattern.sub() in redact_value()
  • Implement logging for pattern substitution failures
  • Write unit tests for exception handling scenarios
  • Write integration tests for structlog pipeline stability
  • Update docstring to document exception handling behavior
  • Verify no regressions in existing redaction tests

Definition of Done

This issue is complete when:

  1. The redact_value() function has exception handling around all pattern.sub() calls
  2. All unit and integration tests pass
  3. Code coverage is >= 97%
  4. The function gracefully handles malformed patterns without crashing
  5. Warnings are logged when patterns fail
  6. The PR is reviewed and approved by at least one maintainer

Automated by CleverAgents Bot
Agent: new-issue-creator

## Metadata **Commit Message:** ``` fix(shared/redaction): add exception handling to redact_value() for pattern.sub() failures ``` **Branch Name:** `fix/redaction-pattern-exception-handling` ## Background and Context The `redact_value()` function in `src/cleveragents/shared/redaction.py` (lines 145-157) calls `pattern.sub()` without exception handling. If a registered pattern causes an exception during substitution—due to a malformed regex pattern, ReDoS (Regular Expression Denial of Service) attack, timeout, or memory exhaustion—the function will crash and propagate the exception up the call stack. Since this function is called from `secrets_masking_processor()` which is integrated into the structlog pipeline, an unhandled exception in pattern substitution can crash the entire application's logging system, potentially causing cascading failures. ## Expected Behavior The `redact_value()` function should gracefully handle exceptions during pattern substitution: - If a pattern fails to substitute, the function should log a warning and continue processing with the next pattern - The function should never crash due to a malformed or problematic pattern - The original value should be returned (with partial redactions from successful patterns applied) - The application should remain stable and continue operating ## Acceptance Criteria - [ ] Exception handling is added around `pattern.sub()` calls in `redact_value()` - [ ] When a pattern fails, a warning is logged with the exception details - [ ] Processing continues with the next pattern after a failure - [ ] Unit tests verify graceful handling of: - ReDoS patterns (e.g., `(a+)+b`) - Invalid regex patterns - Timeout scenarios (if applicable) - Memory exhaustion scenarios (if applicable) - [ ] Integration tests verify the function works correctly in the structlog pipeline - [ ] No exceptions propagate from `redact_value()` to the caller - [ ] Code coverage remains >= 97% ## Subtasks - [ ] Add try-except block around `pattern.sub()` in `redact_value()` - [ ] Implement logging for pattern substitution failures - [ ] Write unit tests for exception handling scenarios - [ ] Write integration tests for structlog pipeline stability - [ ] Update docstring to document exception handling behavior - [ ] Verify no regressions in existing redaction tests ## Definition of Done This issue is complete when: 1. The `redact_value()` function has exception handling around all `pattern.sub()` calls 2. All unit and integration tests pass 3. Code coverage is >= 97% 4. The function gracefully handles malformed patterns without crashing 5. Warnings are logged when patterns fail 6. The PR is reviewed and approved by at least one maintainer --- **Automated by CleverAgents Bot** Agent: new-issue-creator
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
cleveragents/cleveragents-core#10567
No description provided.