@@ -21,7 +21,7 @@ use crate::ui::types::{
2121 AppState , ChatFile , DeleteChatRequest , LoadChatRequest , NewChatRequest , RenameChatRequest ,
2222 SelectRequest ,
2323} ;
24- use crate :: ui:: utils:: get_cache_dir;
24+ use crate :: ui:: utils:: { get_cache_dir, is_chat_id_safe } ;
2525
2626fn validate_image_upload (
2727 filename : Option < & str > ,
@@ -373,6 +373,9 @@ pub async fn delete_chat(
373373 Extension ( app) : Extension < Arc < AppState > > ,
374374 Json ( req) : Json < DeleteChatRequest > ,
375375) -> impl IntoResponse {
376+ if !is_chat_id_safe ( & req. id ) {
377+ return ( StatusCode :: BAD_REQUEST , "invalid chat id" ) . into_response ( ) ;
378+ }
376379 let path = format ! ( "{}/{}.json" , app. chats_dir, req. id) ;
377380 match fs:: remove_file ( & path) . await {
378381 Ok ( _) => ( StatusCode :: OK , "Deleted" ) . into_response ( ) ,
@@ -384,6 +387,9 @@ pub async fn load_chat(
384387 Extension ( app) : Extension < Arc < AppState > > ,
385388 Json ( req) : Json < LoadChatRequest > ,
386389) -> impl IntoResponse {
390+ if !is_chat_id_safe ( & req. id ) {
391+ return ( StatusCode :: BAD_REQUEST , "invalid chat id" ) . into_response ( ) ;
392+ }
387393 let path = format ! ( "{}/{}.json" , app. chats_dir, req. id) ;
388394 if let Ok ( bytes) = fs:: read ( & path) . await {
389395 if let Ok ( chat) = serde_json:: from_slice :: < ChatFile > ( & bytes) {
@@ -399,6 +405,9 @@ pub async fn rename_chat(
399405 Extension ( app) : Extension < Arc < AppState > > ,
400406 Json ( req) : Json < RenameChatRequest > ,
401407) -> impl IntoResponse {
408+ if !is_chat_id_safe ( & req. id ) {
409+ return ( StatusCode :: BAD_REQUEST , "invalid chat id" ) . into_response ( ) ;
410+ }
402411 let path = format ! ( "{}/{}.json" , app. chats_dir, req. id) ;
403412 if let Ok ( bytes) = fs:: read ( & path) . await {
404413 if let Ok ( mut chat) = serde_json:: from_slice :: < ChatFile > ( & bytes) {
@@ -427,6 +436,9 @@ pub async fn append_message(
427436 Extension ( app) : Extension < Arc < AppState > > ,
428437 Json ( req) : Json < AppendMessageRequest > ,
429438) -> impl IntoResponse {
439+ if !is_chat_id_safe ( & req. id ) {
440+ return ( StatusCode :: BAD_REQUEST , "invalid chat id" ) . into_response ( ) ;
441+ }
430442 if let Err ( e) = append_chat_message ( & app, & req. id , & req. role , & req. content , req. images ) . await {
431443 error ! ( "append message error: {}" , e) ;
432444 return ( StatusCode :: INTERNAL_SERVER_ERROR , "append failed" ) . into_response ( ) ;
0 commit comments