forked from cardosofelipe/fast-next-template
fix(safety): copy default patterns to avoid test pollution
The ContentFilter was appending references to DEFAULT_PATTERNS objects, so when tests modified patterns (e.g., disabling them), those changes persisted across test runs. Use dataclass replace() to create copies. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -7,7 +7,7 @@ Filters and sanitizes content for safety, including PII detection and secret sca
|
|||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field, replace
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from typing import Any, ClassVar
|
from typing import Any, ClassVar
|
||||||
|
|
||||||
@@ -254,6 +254,7 @@ class ContentFilter:
|
|||||||
self._lock = asyncio.Lock()
|
self._lock = asyncio.Lock()
|
||||||
|
|
||||||
# Load default patterns based on configuration
|
# Load default patterns based on configuration
|
||||||
|
# Use replace() to create a copy of each pattern to avoid mutating shared defaults
|
||||||
for pattern in self.DEFAULT_PATTERNS:
|
for pattern in self.DEFAULT_PATTERNS:
|
||||||
if pattern.category == ContentCategory.PII and not enable_pii_filter:
|
if pattern.category == ContentCategory.PII and not enable_pii_filter:
|
||||||
continue
|
continue
|
||||||
@@ -263,7 +264,7 @@ class ContentFilter:
|
|||||||
continue
|
continue
|
||||||
if pattern.category == ContentCategory.INJECTION and not enable_injection_filter:
|
if pattern.category == ContentCategory.INJECTION and not enable_injection_filter:
|
||||||
continue
|
continue
|
||||||
self._patterns.append(pattern)
|
self._patterns.append(replace(pattern))
|
||||||
|
|
||||||
# Add custom patterns
|
# Add custom patterns
|
||||||
if custom_patterns:
|
if custom_patterns:
|
||||||
|
|||||||
Reference in New Issue
Block a user