fix: add provenance header and speaker IDs to Slack transcript imports (#815)
* fix: add provenance header and speaker IDs to Slack transcript imports Slack exports are multi-party chats where no speaker is inherently the "user" or "assistant". The parser previously assigned these roles purely by position, allowing a crafted export to place attacker text in the "user" role — making it appear as the memory owner's words in all future retrieval (data poisoning via stored memory). Changes: - Add provenance header marking Slack transcripts as multi-party with positional (unverified) role assignment - Prefix each message with the original speaker ID ([U1], [U2], etc.) so downstream consumers can distinguish authors - Keep user/assistant role alternation for exchange-pair chunking compatibility with convo_miner.py Tests: - Provenance header presence and content - Speaker ID preservation in output - Attacker-first-message attribution verification Refs: MemPalace/mempalace#809 * fix: move Slack provenance to footer, sanitize speaker IDs, extract constant - Move provenance notice from header to footer to prevent it becoming a standalone ChromaDB drawer via paragraph chunking on exports with fewer than 3 exchange pairs (violates verbatim-always principle) - Sanitize speaker user_id/username: strip brackets, newlines, and control characters to prevent chunk-boundary injection via crafted Slack exports - Extract header string to _SLACK_PROVENANCE_FOOTER module constant, consistent with _TOOL_RESULT_* constants pattern; tests import it instead of duplicating the literal Refs: MemPalace/mempalace#809
This commit is contained in:
committed by
GitHub
parent
a15094ce60
commit
e61dc2adf8
+20
-5
@@ -20,6 +20,12 @@ import re
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
# Provenance footer appended to Slack transcript output so downstream consumers
|
||||
# know the speaker roles are positionally assigned, not verified.
|
||||
_SLACK_PROVENANCE_FOOTER = (
|
||||
"\n[source: slack-export | multi-party chat — speaker roles are positional, not verified]"
|
||||
)
|
||||
|
||||
|
||||
# ─── Noise stripping ─────────────────────────────────────────────────────
|
||||
# Claude Code and other tools inject system tags, hook output, and UI chrome
|
||||
@@ -367,8 +373,13 @@ def _try_chatgpt_json(data) -> Optional[str]:
|
||||
def _try_slack_json(data) -> Optional[str]:
|
||||
"""
|
||||
Slack channel export: [{"type": "message", "user": "...", "text": "..."}]
|
||||
Optimized for 2-person DMs. In channels with 3+ people, alternating
|
||||
speakers are labeled user/assistant to preserve the exchange structure.
|
||||
|
||||
Slack exports are multi-party chats where no speaker is inherently the
|
||||
"user" or "assistant". To preserve exchange-pair chunking (which relies
|
||||
on ``>`` markers from the ``user`` role), we still alternate roles, but
|
||||
prefix each message with the speaker ID so downstream consumers can
|
||||
distinguish the original author. A provenance header marks the
|
||||
transcript as a Slack import.
|
||||
"""
|
||||
if not isinstance(data, list):
|
||||
return None
|
||||
@@ -378,7 +389,10 @@ def _try_slack_json(data) -> Optional[str]:
|
||||
for item in data:
|
||||
if not isinstance(item, dict) or item.get("type") != "message":
|
||||
continue
|
||||
user_id = item.get("user", item.get("username", ""))
|
||||
raw_user_id = item.get("user", item.get("username", ""))
|
||||
# Sanitize speaker ID: strip brackets, newlines, and control chars
|
||||
# to prevent chunk-boundary injection via crafted exports
|
||||
user_id = re.sub(r"[\[\]\n\r\x00-\x1f]", "_", raw_user_id).strip()
|
||||
text = item.get("text", "").strip()
|
||||
if not text or not user_id:
|
||||
continue
|
||||
@@ -391,9 +405,10 @@ def _try_slack_json(data) -> Optional[str]:
|
||||
else:
|
||||
seen_users[user_id] = "user"
|
||||
last_role = seen_users[user_id]
|
||||
messages.append((seen_users[user_id], text))
|
||||
# Prefix with speaker ID so the original author is preserved
|
||||
messages.append((seen_users[user_id], f"[{user_id}] {text}"))
|
||||
if len(messages) >= 2:
|
||||
return _messages_to_transcript(messages)
|
||||
return _messages_to_transcript(messages) + _SLACK_PROVENANCE_FOOTER
|
||||
return None
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user