fix(notifications): use safe UTF-8 slicing for session IDs#32
Closed
fix(notifications): use safe UTF-8 slicing for session IDs#32
Conversation
Greptile OverviewGreptile SummaryThis PR replaces unsafe byte-based string slicing with UTF-8 character boundary aware slicing to prevent panics when slicing multi-byte UTF-8 characters. Changes Made:
Issues Found:
Confidence Score: 2/5
|
| Filename | Overview |
|---|---|
| src/cortex-cli/src/utils/notification.rs | Replaced unsafe byte slicing with UTF-8 aware slicing, but has logic bug that can exceed 8-byte limit when multi-byte chars straddle the boundary |
| src/cortex-cli/src/import_cmd.rs | Correctly replaced all unsafe byte slicing with safe .get() methods that gracefully handle invalid UTF-8 boundaries |
Sequence Diagram
sequenceDiagram
participant Caller
participant send_task_notification
participant String Slicing
participant send_notification
Caller->>send_task_notification: session_id, success
send_task_notification->>String Slicing: Truncate session_id to ~8 bytes
Note over String Slicing: char_indices().take_while(idx < 8)<br/>map to char end positions<br/>get last position
String Slicing-->>send_task_notification: short_id
send_task_notification->>send_notification: title, body, urgency
send_notification-->>Caller: Result
| // Use safe UTF-8 slicing - find the last valid char boundary at or before position 8 | ||
| let short_id = session_id | ||
| .char_indices() | ||
| .take_while(|(idx, _)| *idx < 8) |
There was a problem hiding this comment.
Logic bug: take_while(|(idx, _)| *idx < 8) takes chars whose start index is <8, but doesn't check if they exceed 8 bytes when included. If a 4-byte char starts at index 7, it extends to byte 11.
Suggested change
| .take_while(|(idx, _)| *idx < 8) | |
| .take_while(|(idx, ch)| idx + ch.len_utf8() <= 8) |
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/cortex-cli/src/utils/notification.rs
Line: 69:69
Comment:
Logic bug: `take_while(|(idx, _)| *idx < 8)` takes chars whose start index is <8, but doesn't check if they exceed 8 bytes when included. If a 4-byte char starts at index 7, it extends to byte 11.
```suggestion
.take_while(|(idx, ch)| idx + ch.len_utf8() <= 8)
```
How can I resolve this? If you propose a fix, please make it concise.
echobt
added a commit
that referenced
this pull request
Feb 4, 2026
This PR consolidates the following UTF-8 safety fixes: - #31: Use safe UTF-8 slicing in import command base64 extraction - #32: Use safe UTF-8 slicing for session IDs in notifications - #33: Use char-aware string truncation for UTF-8 safety in resume - #35: Use safe UTF-8 slicing for session IDs in lock command - #37: Validate UTF-8 boundaries in mention parsing All changes ensure safe string operations that respect UTF-8 boundaries: - Replaced direct byte slicing with char-aware methods - Added floor_char_boundary checks before slicing - Prevents panics from slicing multi-byte characters
Contributor
Author
|
Consolidated into #70 - fix: consolidated UTF-8 safety improvements for string slicing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #5274 - Notification session ID slicing panics on multi-byte UTF-8.
Problem
Session ID string handling uses byte-based slicing which can panic on multi-byte UTF-8 characters when the slice boundary falls in the middle of a character.
Solution
Used safe string slicing with char boundaries - iterates through character indices to find the last valid UTF-8 boundary at or before position 8, then uses
.get()for safe slicing.