logfire icon indicating copy to clipboard operation
logfire copied to clipboard

Scrubbing: support masking PII inside values without redacting the whole value

Open dmontagu opened this issue 4 weeks ago • 3 comments

Description

Scrubbing is built to find a sensitive key or value and redact all of it. It is hard to use for a different, common need: masking PII inside free text while keeping the rest of the text. For example, an LLM prompt or tool result contains a customer's email address and phone number. The span should keep the text with [EMAIL] and [PHONE] in place of those values, because the rest of the prompt is what makes the trace useful.

Today the only way to do this is a ScrubbingOptions.callback that returns a masked copy of match.value. That works, but three behaviors of the scrubber make it fragile:

  1. The callback runs only when a pattern matches. To mask emails inside a value, you must add a pattern that fires on every value that contains an email. The value-level masking logic then exists twice: once as the trigger pattern, and once in the callback.
  2. A value that is entirely one match is kept as is. BaseScrubber.scrub returns a string unchanged when the match spans the whole string (the "the value is literally password" case). With an email pattern, a value that is only an email address is never redacted or passed to the callback. The workaround is a zero-width lookahead pattern such as (?=<email regex>), so that a match can never cover the whole value.
  3. The callback can't easily tell a key match from a value match. On a key match, match.value is the attribute's value, and the pattern matched the key. A callback that returns a masked value for every match also overrides the default redaction of sensitive keys such as password. The workaround is to check match.pattern_match.string is match.value and return None otherwise.

A working setup looks like this, and each part of it is there only to work around the points above:

EMAIL = r'[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}'


def mask_pii_in_values(match: logfire.ScrubMatch) -> str | None:
    # Point 3: leave key matches to the default redaction.
    if match.pattern_match.string is not match.value:
        return None
    return mask_pii(match.value)  # the application's own masking function


logfire.configure(
    scrubbing=logfire.ScrubbingOptions(
        callback=mask_pii_in_values,
        # Points 1 and 2: a zero-width trigger, so that a value which is only an email still reaches the callback.
        extra_patterns=[f'(?={EMAIL})'],
    ),
)

Proposal

Add a supported way to transform every exported value, separate from pattern matching. For example:

logfire.configure(
    scrubbing=logfire.ScrubbingOptions(
        # Called for every string value in span attributes, span events and log bodies, after the default scrubbing.
        mask_value=mask_pii,
    ),
)

Open points:

  • whether the hook receives the path as well as the value (mask_value(path, value));
  • whether it runs before or after the default patterns;
  • whether it also applies to the span name and the formatted message.

Other LLM observability SDKs accept a single masking function, so this would also make it simpler to move an existing masking function to Logfire. A docs example in the scrubbing guide would help either way: "mask PII inside text without redacting the whole value", using the hook or the current workaround.

dmontagu avatar Nov 06 '26 06:11 dmontagu

Would you welcome a small external contribution here? On current main, the guarded workaround leaves an email in pydantic_ai.all_messages unchanged because this safe key bypasses the callback. Should the new hook cover GenAI values and receive the path so applications can preserve SDK metadata? I'd like to agree that first slice and coordinate with #2236, which changes zero-width matching, before implementing it.

yashb98 avatar Nov 06 '26 22:11 yashb98

No, I'm working on this.

alexmojaki avatar Nov 09 '26 10:11 alexmojaki

But @yashb98 please tell me if you have real needs here as a user so that I can take that into account in my design. Unfortunately these days it's hard to tell real concerns apart from hypothetical issues noticed by an AI scan.

alexmojaki avatar Nov 09 '26 10:11 alexmojaki