Skip to content

Conversation

h16rkim
Copy link
Contributor

@h16rkim h16rkim commented Oct 3, 2025

Issue #, if available: #3086

Description of changes:

When executing a chat hook, the user’s prompt is sanitized through the sanitize_user_prompt(prompt) function. This function trims the prompt with a substring if its length is too long (over 4096 bytes).

However, in the case of multibyte characters such as those in CJK languages, if a character is cut across a UTF-8 boundary, a panic occurs and the program terminates.

This PR removes such crashes and fixes the sanitize_user_prompt function so that it works correctly.

Screen Capture

crash

@h16rkim h16rkim changed the title fix: ensure sanitize_user_prompt handles UTF-8 boundaries correctly fix(chat): ensure sanitize_user_prompt handles UTF-8 boundaries correctly Oct 3, 2025
@h16rkim h16rkim changed the title fix(chat): ensure sanitize_user_prompt handles UTF-8 boundaries correctly fix:bug(chat): ensure sanitize_user_prompt handles UTF-8 boundaries correctly Oct 3, 2025
// Limit the size of input to first 4096 bytes, respecting character boundaries
const MAX_LEN: usize = 4096;

let truncated = if input.len() > MAX_LEN {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a util function that does this - https://github.com/aws/amazon-q-developer-cli/blob/main/crates/chat-cli/src/cli/chat/util/mod.rs#L19

Can this be used instead?

Copy link
Contributor Author

@h16rkim h16rkim Oct 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. I changed it to use truncate_safe function

@h16rkim
Copy link
Contributor Author

h16rkim commented Oct 6, 2025

@kensave @brandonskiser

There’s another PR that fixes a similar UTF-8 boundary bug — can you review it as well?

#3020

@h16rkim
Copy link
Contributor Author

h16rkim commented Oct 8, 2025

Thank you for your reviews. Can we merge this PR ?

@brandonskiser brandonskiser merged commit fe75781 into aws:main Oct 10, 2025
1 check passed
@brandonskiser
Copy link
Contributor

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants