Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/utils/transcripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import logging
import os
from pathlib import Path
import hashlib

from configuration import configuration
from models.requests import Attachment, QueryRequest
Expand All @@ -18,11 +19,17 @@
logger = logging.getLogger("utils.transcripts")


def _hash_user_id(user_id: str) -> str:
"""Hash the user ID using SHA-256."""
return hashlib.sha256(user_id.encode("utf-8")).hexdigest()


def construct_transcripts_path(user_id: str, conversation_id: str) -> Path:
"""Construct path to transcripts."""
# these two normalizations are required by Snyk as it detects
# this Path sanitization pattern
uid = os.path.normpath("/" + user_id).lstrip("/")
hashed_user_id = _hash_user_id(user_id)
uid = os.path.normpath("/" + hashed_user_id).lstrip("/")
cid = os.path.normpath("/" + conversation_id).lstrip("/")
file_path = (
configuration.user_data_collection_configuration.transcripts_storage or ""
Expand Down Expand Up @@ -59,13 +66,15 @@ def store_transcript( # pylint: disable=too-many-arguments,too-many-positional-
transcripts_path = construct_transcripts_path(user_id, conversation_id)
transcripts_path.mkdir(parents=True, exist_ok=True)

hashed_user_id = _hash_user_id(user_id)

data_to_store = {
"metadata": {
"provider": provider_id,
"model": model_id,
"query_provider": query_request.provider,
"query_model": query_request.model,
"user_id": user_id,
"user_id": hashed_user_id,
"conversation_id": conversation_id,
"timestamp": datetime.now(UTC).isoformat(),
},
Expand Down
8 changes: 6 additions & 2 deletions tests/unit/utils/test_transcripts.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Unit tests for functions defined in utils.transcripts module."""

import hashlib
from configuration import AppConfig
from models.requests import QueryRequest

Expand Down Expand Up @@ -39,11 +40,13 @@ def test_construct_transcripts_path(mocker):

user_id = "user123"
conversation_id = "123e4567-e89b-12d3-a456-426614174000"
hashed_user_id = hashlib.sha256(user_id.encode("utf-8")).hexdigest()

path = construct_transcripts_path(user_id, conversation_id)

assert (
str(path) == "/tmp/transcripts/user123/123e4567-e89b-12d3-a456-426614174000"
str(path)
== f"/tmp/transcripts/{hashed_user_id}/123e4567-e89b-12d3-a456-426614174000"
), "Path should be constructed correctly"


Expand Down Expand Up @@ -97,14 +100,15 @@ def test_store_transcript(mocker):
)

# Assert that the transcript was stored correctly
hashed_user_id = hashlib.sha256(user_id.encode("utf-8")).hexdigest()
mock_json.dump.assert_called_once_with(
{
"metadata": {
"provider": "fake-provider",
"model": "fake-model",
"query_provider": query_request.provider,
"query_model": query_request.model,
"user_id": user_id,
"user_id": hashed_user_id,
"conversation_id": conversation_id,
"timestamp": mocker.ANY,
},
Expand Down
Loading