Skip to content

Check same-position patterns after a scrubbing callback allows a match - #2826

Merged
alexmojaki merged 2 commits into
mainfrom
codex/scrubbing-same-position-callbacks
Nov 18, 2026
Merged

alexmojaki merged 2 commits into
mainfrom
codex/scrubbing-same-position-callbacks

Conversation

@alexmojaki

Copy link
Copy Markdown
Contributor

Summary

  • Preserve individual built-in and custom regex patterns alongside the combined fast-path pattern.
  • When a callback allows a match, check other patterns anchored at that same position before searching later positions.
  • Count these allowed matches toward the existing limit, and document the callback behavior.

This addresses the same-position callback-whitelisting gap only; it does not complete pydantic/platform#38102.

Verification

  • uv run pytest tests/test_secret_scrubbing.py -q --tb=short (110 passed)
  • pyright logfire-sdk/logfire/_internal/scrubbing.py tests/test_secret_scrubbing.py (0 errors)
  • Ruff check and format checks on changed Python files
  • Commit hooks passed

@veria-ai

veria-ai Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

PR overview

No security issues were found in this pull request.

Reviews

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 3 files

Confidence score: 5/5

  • In logfire-sdk/logfire/_internal/scrubbing.py, a user pattern identical to a default can make the callback run twice for the same match. Deduplicate the patterns before compiling them.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="logfire-sdk/logfire/_internal/scrubbing.py">

<violation number="1" location="logfire-sdk/logfire/_internal/scrubbing.py:285">
P3: The individual pattern list is not deduplicated, so a user `extra_patterns` entry identical to a default pattern makes the callback run twice for the same match. Deduplicate the pattern sequence before compiling it.</violation>
</file>

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

patterns = [_DEFAULT_PATTERN, *(patterns or [])]
self._pattern = re.compile('|'.join(patterns), re.IGNORECASE | re.DOTALL)
self._individual_patterns = tuple(
re.compile(pattern, re.IGNORECASE | re.DOTALL) for pattern in [*DEFAULT_PATTERNS, *patterns[1:]]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: The individual pattern list is not deduplicated, so a user extra_patterns entry identical to a default pattern makes the callback run twice for the same match. Deduplicate the pattern sequence before compiling it.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At logfire-sdk/logfire/_internal/scrubbing.py, line 285:

<comment>The individual pattern list is not deduplicated, so a user `extra_patterns` entry identical to a default pattern makes the callback run twice for the same match. Deduplicate the pattern sequence before compiling it.</comment>

<file context>
@@ -281,6 +281,9 @@ def __init__(
         patterns = [_DEFAULT_PATTERN, *(patterns or [])]
         self._pattern = re.compile('|'.join(patterns), re.IGNORECASE | re.DOTALL)
+        self._individual_patterns = tuple(
+            re.compile(pattern, re.IGNORECASE | re.DOTALL) for pattern in [*DEFAULT_PATTERNS, *patterns[1:]]
+        )
         self._callback = callback
</file context>
Suggested change
re.compile(pattern, re.IGNORECASE | re.DOTALL) for pattern in [*DEFAULT_PATTERNS, *patterns[1:]]
re.compile(pattern, re.IGNORECASE | re.DOTALL) for pattern in dict.fromkeys([*DEFAULT_PATTERNS, *patterns[1:]])

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(AI) The individual configured patterns are preserved as separate rules by design, including repeated entries. Each can reach the callback after an unchanged result and counts toward the shared 100-match limit. Deduplicating them would change that configured-rule behavior, so I am leaving the sequence as provided.

@github-actions

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  logfire-sdk/logfire/_internal
  scrubbing.py
Project Total  

This report was generated by python-coverage-comment-action

@alexmojaki
alexmojaki added this pull request to the merge queue Nov 18, 2026
Merged via the queue into main with commit 4559f3e Nov 18, 2026
37 checks passed
@alexmojaki
alexmojaki deleted the codex/scrubbing-same-position-callbacks branch November 18, 2026 14:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant