Skip to content

fix(auditlog-ng): strip prompt/response text from AiMessageContext - #450

Merged
LucasAlvesSoares merged 5 commits into
SAP:mainfrom
mtsvetanov071:fix/auditlog-ng-strip-message-context-text
Nov 5, 2026
Merged

LucasAlvesSoares merged 5 commits into
SAP:mainfrom
mtsvetanov071:fix/auditlog-ng-strip-message-context-text

Conversation

@mtsvetanov071

Copy link
Copy Markdown
Contributor

Disclaimer: Do not include SAP-internal or customer-specific information in this PR (e.g. internal system URLs, customer names, tenant IDs, or confidential configurations). This is a public repository.

Description

Several AI audit events (AiPromptReceived, AiGuardrailTriggered, AiModelInvocation, AiResponseGenerated, AiMessageSent, AiTaskCreated, AiTaskUpdated, AiUserInputRequested, AiTurnStart, AiTurnEnd) embed an AiMessageContext (proto field message_context). Two of its fields — prompt_text and response_text — hold the verbatim model input/output and are flagged in the proto as "may contain sensitive data".

This change strips those two free-text fields centrally in AuditClient.send(), before validation and serialization, so that no event type forwards the raw prompt/response content to the audit log — current or future.

Correlation fields (uid, name, ai_role_id) and the token counts (prompt_tokens, completion_tokens, total_tokens) are preserved, so events remain useful for correlation and metrics. Events without a message_context field, or with an unset one, are left untouched (the helper guards with HasField so it never auto-creates an empty sub-message).

Related Issue

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • Code refactoring
  • Dependency update

How to Test

  1. Build an AI audit event with a populated message context, e.g.:
    event = AiPromptReceived()
    event.common.tenant_id = "tenant-123"
    event.message_context.prompt_text = "sensitive prompt"
    event.message_context.response_text = "sensitive response"
    event.message_context.uid = "conversation-123"
    client.send(event)
  2. Inspect the emitted record (or the in-place event after send()).
  3. Expected result: prompt_text and response_text are empty; uid and token counts are retained.

Automated tests:

pytest tests/core/unit/auditlog_ng/unit/test_client.py

All 31 tests pass (5 new in TestStripMessageContextText, covering stripping, field preservation, unset contexts, events without the field, and end-to-end via send()).

Checklist

  • I have read the Contributing Guidelines
  • I have verified that my changes solve the issue
  • I have added/updated automated tests to cover my changes
  • All tests pass locally
  • I have verified that my code follows the Code Guidelines
  • I have updated documentation (if applicable)
  • I have added type hints for all public APIs
  • My code does not contain sensitive information (credentials, tokens, etc.)
  • I have followed Conventional Commits for commit messages
  • (New modules only) I have attached e2e evidence showing the module working against a real BTP service
  • (Security-sensitive changes only) I have run /security-review and addressed all findings

Breaking Changes

None. The stripped fields are optional and were never required for a valid event. Callers that set prompt_text/response_text will simply find them absent in the emitted record; all other fields are unaffected.

Additional Notes

The scrubbing is enforced unconditionally in the single send() choke point rather than per-event, so any AI event type added later is covered automatically without further changes.

Several AI audit events embed an AiMessageContext whose prompt_text and
response_text fields hold the verbatim model input/output and may contain
sensitive data. Strip those two fields centrally in AuditClient.send()
before validation and serialization, so no current or future event type
forwards that content to the audit log.

Correlation fields (uid, name, ai_role_id) and token counts are preserved.
Events without a message_context field, or with an unset one, are left
untouched.
@mtsvetanov071
mtsvetanov071 requested a review from a team as a code owner November 5, 2026 09:06

@LucasAlvesSoares LucasAlvesSoares 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.

img

@LucasAlvesSoares
LucasAlvesSoares merged commit a51145e into SAP:main Nov 5, 2026
13 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants