From c8b88dadc3bb4e5a4c512d69daae5ed4579dde17 Mon Sep 17 00:00:00 2001 From: Felipe Cardoso Date: Sat, 3 Jan 2026 12:08:43 +0100 Subject: [PATCH] fix(safety): copy default patterns to avoid test pollution MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- backend/app/services/safety/content/filter.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/backend/app/services/safety/content/filter.py b/backend/app/services/safety/content/filter.py index b6585df..bef8964 100644 --- a/backend/app/services/safety/content/filter.py +++ b/backend/app/services/safety/content/filter.py @@ -7,7 +7,7 @@ Filters and sanitizes content for safety, including PII detection and secret sca import asyncio import logging import re -from dataclasses import dataclass, field +from dataclasses import dataclass, field, replace from enum import Enum from typing import Any, ClassVar @@ -254,6 +254,7 @@ class ContentFilter: self._lock = asyncio.Lock() # 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: if pattern.category == ContentCategory.PII and not enable_pii_filter: continue @@ -263,7 +264,7 @@ class ContentFilter: continue if pattern.category == ContentCategory.INJECTION and not enable_injection_filter: continue - self._patterns.append(pattern) + self._patterns.append(replace(pattern)) # Add custom patterns if custom_patterns: