Skip to content

Commit 22007a7

Browse files
committed
bugfix: fix LFI issue
1 parent d4451bb commit 22007a7

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
lines changed

ChuanhuChatbot.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ def create_new_model():
6868
historySearchTextbox = gr.Textbox(show_label=False, container=False, placeholder=i18n(
6969
"搜索(支持正则)..."), lines=1, elem_id="history-search-tb")
7070
with gr.Column(min_width=52, scale=1, elem_id="gr-history-header-btns"):
71-
uploadFileBtn = gr.UploadButton(
72-
interactive=True, label="", file_types=[".json"], elem_id="gr-history-upload-btn")
71+
uploadHistoryBtn = gr.UploadButton(
72+
interactive=True, label="", file_types=[".json"], elem_id="gr-history-upload-btn", type="binary")
7373
historyRefreshBtn = gr.Button("", elem_id="gr-history-refresh-btn")
7474

7575

@@ -708,8 +708,8 @@ def create_greeting(request: gr.Request):
708708
js='(a,b)=>{return clearChatbot(a,b);}',
709709
)
710710
historySelectList.select(**load_history_from_file_args)
711-
uploadFileBtn.upload(upload_chat_history, [current_model, uploadFileBtn], [
712-
saveFileName, systemPromptTxt, chatbot, single_turn_checkbox, temperature_slider, top_p_slider, n_choices_slider, stop_sequence_txt, max_context_length_slider, max_generation_slider, presence_penalty_slider, frequency_penalty_slider, logit_bias_txt, user_identifier_txt, use_streaming_checkbox]).then(**refresh_history_args)
711+
uploadHistoryBtn.upload(upload_chat_history, [current_model, uploadHistoryBtn], [
712+
saveFileName, systemPromptTxt, chatbot, single_turn_checkbox, temperature_slider, top_p_slider, n_choices_slider, stop_sequence_txt, max_context_length_slider, max_generation_slider, presence_penalty_slider, frequency_penalty_slider, logit_bias_txt, user_identifier_txt, use_streaming_checkbox, historySelectList]).then(**refresh_history_args)
713713
historyDownloadBtn.click(None, [
714714
user_name, historySelectList], None, js='(a,b)=>{return downloadHistory(a,b,".json");}')
715715
historyMarkdownDownloadBtn.click(None, [

modules/models/base_model.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -986,28 +986,37 @@ def export_markdown(self, filename, chatbot):
986986
filename += ".md"
987987
save_file(filename, self)
988988

989+
def upload_chat_history(self, new_history_file_content=None):
990+
logging.debug(f"{self.user_name} 加载对话历史中……")
991+
if new_history_file_content is not None:
992+
if isinstance(new_history_file_content, bytes):
993+
try:
994+
# Try to parse the content as JSON
995+
json_content = json.loads(new_history_file_content.decode('utf-8'))
996+
997+
# If successful, save the content to a file
998+
new_history_filename = new_auto_history_filename(self.user_name)
999+
new_history_file_path = os.path.join(HISTORY_DIR, self.user_name, new_history_filename)
1000+
1001+
# Ensure the directory exists
1002+
os.makedirs(os.path.dirname(new_history_file_path), exist_ok=True)
1003+
1004+
# Write the content to the file
1005+
with open(new_history_file_path, 'w', encoding='utf-8') as f:
1006+
json.dump(json_content, f, ensure_ascii=False, indent=2)
1007+
1008+
self.history_file_path = new_history_filename
1009+
logging.info(f"History file uploaded and saved as {new_history_filename}")
1010+
except json.JSONDecodeError:
1011+
logging.error("Uploaded content is not valid JSON. Using default history.")
1012+
else:
1013+
logging.warning("Unexpected type for new_history_file_content. Using default history.")
1014+
return *self.load_chat_history(new_history_file_path), init_history_list(self.user_name)
1015+
9891016
def load_chat_history(self, new_history_file_path=None):
9901017
logging.debug(f"{self.user_name} 加载对话历史中……")
9911018
if new_history_file_path is not None:
992-
if type(new_history_file_path) != str:
993-
# copy file from new_history_file_path.name to os.path.join(HISTORY_DIR, self.user_name)
994-
new_history_file_path = new_history_file_path.name
995-
target_path = os.path.join(HISTORY_DIR, self.user_name, new_history_file_path)
996-
# Check if the file is in the history directory
997-
assert os.path.realpath(new_history_file_path).startswith(os.path.realpath(HISTORY_DIR))
998-
assert os.path.realpath(target_path).startswith(os.path.realpath(HISTORY_DIR))
999-
assert self.user_name in [i[0] for i in auth_list]
1000-
shutil.copyfile(
1001-
new_history_file_path,
1002-
os.path.join(
1003-
HISTORY_DIR,
1004-
self.user_name,
1005-
os.path.basename(new_history_file_path),
1006-
),
1007-
)
1008-
self.history_file_path = os.path.basename(new_history_file_path)
1009-
else:
1010-
self.history_file_path = new_history_file_path
1019+
self.history_file_path = new_history_file_path
10111020
try:
10121021
if self.history_file_path == os.path.basename(self.history_file_path):
10131022
history_file_path = os.path.join(

modules/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def export_markdown(current_model, *args):
9999

100100

101101
def upload_chat_history(current_model, *args):
102-
return current_model.load_chat_history(*args)
102+
return current_model.upload_chat_history(*args)
103103

104104

105105
def set_token_upper_limit(current_model, *args):

0 commit comments

Comments
 (0)