Skip to content
Open
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
9 changes: 7 additions & 2 deletions pyrit/backend/services/attack_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -870,8 +870,13 @@ async def _persist_base64_pieces_async(request: AddMessageRequest) -> None:
piece.converted_value = file_path
continue

# Already an existing file on disk — keep as-is
if Path(piece.original_value).is_file():
# Already an existing file on disk — keep as-is.
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.

nit: I think the length check is unnecessary, and also I suggest you don't need to the extra variable is_existing_file, which is set in the try/catch block and used [only] right after it. instead, this might be more readable:

(basically just add a try: <current-code> except (OSError): <pass>

try:
    if Path(piece.original_value).is_file():
        if piece.converted_value is None:
            piece.converted_value = piece.original_value
        continue
except (OSError, ValueError):
    pass

This will take care of cases where the content is indeed resolvable to a file, and pass over it when it's not.

# Guard against base64 strings that would exceed OS path length limits.
try:
is_existing_file = len(piece.original_value) < 4096 and Path(piece.original_value).is_file()
except OSError:
is_existing_file = False
if is_existing_file:
if piece.converted_value is None:
piece.converted_value = piece.original_value
continue
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/backend/test_attack_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -1596,6 +1596,35 @@ async def test_non_path_data_types_are_skipped(self, attack_service) -> None:

assert request.pieces[0].original_value == "thinking step"

@pytest.mark.asyncio
async def test_long_base64_audio_does_not_crash(self, attack_service) -> None:
"""Base64 audio data longer than OS path limits should be saved, not crash with OSError."""
# Simulate a base64-encoded WAV file (>4096 chars, exceeds Linux filename limit of 255)
long_b64 = "UklGRiQ" + "A" * 5000 # fake WAV header + padding
request = AddMessageRequest(
role="user",
pieces=[
MessagePieceRequest(
data_type="audio_path",
original_value=long_b64,
mime_type="audio/wav",
)
],
send=False,
target_conversation_id="test-id",
)

with patch("pyrit.backend.services.attack_service.data_serializer_factory") as mock_factory:
mock_serializer = AsyncMock()
mock_serializer.value = "/tmp/saved_audio.wav"
mock_factory.return_value = mock_serializer

await AttackService._persist_base64_pieces_async(request)

mock_factory.assert_called_once()
mock_serializer.save_b64_image.assert_called_once_with(data=long_b64)
assert request.pieces[0].original_value == "/tmp/saved_audio.wav"


# ============================================================================
# Related Conversations Tests
Expand Down
Loading