ChatterBot 1.2.14__tar.gz → 1.2.16__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. {chatterbot-1.2.14 → chatterbot-1.2.16}/ChatterBot.egg-info/PKG-INFO +2 -3
  2. {chatterbot-1.2.14 → chatterbot-1.2.16}/ChatterBot.egg-info/requires.txt +1 -2
  3. {chatterbot-1.2.14 → chatterbot-1.2.16}/PKG-INFO +2 -3
  4. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/__init__.py +1 -1
  5. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/chatterbot.py +5 -0
  6. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/logic/unit_conversion.py +4 -2
  7. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/trainers.py +206 -30
  8. {chatterbot-1.2.14 → chatterbot-1.2.16}/pyproject.toml +1 -2
  9. {chatterbot-1.2.14 → chatterbot-1.2.16}/tests/test_chatbot.py +13 -1
  10. {chatterbot-1.2.14 → chatterbot-1.2.16}/ChatterBot.egg-info/SOURCES.txt +0 -0
  11. {chatterbot-1.2.14 → chatterbot-1.2.16}/ChatterBot.egg-info/dependency_links.txt +0 -0
  12. {chatterbot-1.2.14 → chatterbot-1.2.16}/ChatterBot.egg-info/top_level.txt +0 -0
  13. {chatterbot-1.2.14 → chatterbot-1.2.16}/LICENSE +0 -0
  14. {chatterbot-1.2.14 → chatterbot-1.2.16}/README.md +0 -0
  15. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/__main__.py +0 -0
  16. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/adapters.py +0 -0
  17. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/comparisons.py +0 -0
  18. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/components.py +0 -0
  19. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/constants.py +0 -0
  20. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/conversation.py +0 -0
  21. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/corpus.py +0 -0
  22. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/exceptions.py +0 -0
  23. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/ext/__init__.py +0 -0
  24. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/ext/django_chatterbot/__init__.py +0 -0
  25. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/ext/django_chatterbot/abstract_models.py +0 -0
  26. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/ext/django_chatterbot/admin.py +0 -0
  27. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/ext/django_chatterbot/apps.py +0 -0
  28. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/ext/django_chatterbot/migrations/0001_initial.py +0 -0
  29. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/ext/django_chatterbot/migrations/0002_statement_extra_data.py +0 -0
  30. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/ext/django_chatterbot/migrations/0003_change_occurrence_default.py +0 -0
  31. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/ext/django_chatterbot/migrations/0004_rename_in_response_to.py +0 -0
  32. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/ext/django_chatterbot/migrations/0005_statement_created_at.py +0 -0
  33. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/ext/django_chatterbot/migrations/0006_create_conversation.py +0 -0
  34. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/ext/django_chatterbot/migrations/0007_response_created_at.py +0 -0
  35. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/ext/django_chatterbot/migrations/0008_update_conversations.py +0 -0
  36. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/ext/django_chatterbot/migrations/0009_tags.py +0 -0
  37. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/ext/django_chatterbot/migrations/0010_statement_text.py +0 -0
  38. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/ext/django_chatterbot/migrations/0011_blank_extra_data.py +0 -0
  39. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/ext/django_chatterbot/migrations/0012_statement_created_at.py +0 -0
  40. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/ext/django_chatterbot/migrations/0013_change_conversations.py +0 -0
  41. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/ext/django_chatterbot/migrations/0014_remove_statement_extra_data.py +0 -0
  42. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/ext/django_chatterbot/migrations/0015_statement_persona.py +0 -0
  43. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/ext/django_chatterbot/migrations/0016_statement_stemmed_text.py +0 -0
  44. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/ext/django_chatterbot/migrations/0017_tags_unique.py +0 -0
  45. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/ext/django_chatterbot/migrations/0018_text_max_length.py +0 -0
  46. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/ext/django_chatterbot/migrations/0019_alter_statement_id_alter_tag_id_and_more.py +0 -0
  47. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/ext/django_chatterbot/migrations/0020_alter_statement_conversation_and_more.py +0 -0
  48. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/ext/django_chatterbot/migrations/0021_increase_text_max_length_to_1100.py +0 -0
  49. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/ext/django_chatterbot/migrations/__init__.py +0 -0
  50. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/ext/django_chatterbot/model_admin.py +0 -0
  51. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/ext/django_chatterbot/models.py +0 -0
  52. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/ext/django_chatterbot/settings.py +0 -0
  53. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/ext/sqlalchemy_app/__init__.py +0 -0
  54. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/ext/sqlalchemy_app/models.py +0 -0
  55. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/filters.py +0 -0
  56. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/languages.py +0 -0
  57. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/logic/__init__.py +0 -0
  58. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/logic/best_match.py +0 -0
  59. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/logic/llm_adapters.py +0 -0
  60. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/logic/logic_adapter.py +0 -0
  61. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/logic/mathematical_evaluation.py +0 -0
  62. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/logic/mcp_tools.py +0 -0
  63. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/logic/specific_response.py +0 -0
  64. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/logic/time_adapter.py +0 -0
  65. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/parsing.py +0 -0
  66. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/preprocessors.py +0 -0
  67. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/response_selection.py +0 -0
  68. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/search.py +0 -0
  69. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/storage/__init__.py +0 -0
  70. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/storage/django_storage.py +0 -0
  71. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/storage/mongodb.py +0 -0
  72. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/storage/redis.py +0 -0
  73. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/storage/sql_storage.py +0 -0
  74. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/storage/storage_adapter.py +0 -0
  75. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/tagging.py +0 -0
  76. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/utils.py +0 -0
  77. {chatterbot-1.2.14 → chatterbot-1.2.16}/chatterbot/vectorstores.py +0 -0
  78. {chatterbot-1.2.14 → chatterbot-1.2.16}/setup.cfg +0 -0
  79. {chatterbot-1.2.14 → chatterbot-1.2.16}/tests/test_adapter_validation.py +0 -0
  80. {chatterbot-1.2.14 → chatterbot-1.2.16}/tests/test_benchmarks.py +0 -0
  81. {chatterbot-1.2.14 → chatterbot-1.2.16}/tests/test_cli.py +0 -0
  82. {chatterbot-1.2.14 → chatterbot-1.2.16}/tests/test_comparisons.py +0 -0
  83. {chatterbot-1.2.14 → chatterbot-1.2.16}/tests/test_conversations.py +0 -0
  84. {chatterbot-1.2.14 → chatterbot-1.2.16}/tests/test_corpus.py +0 -0
  85. {chatterbot-1.2.14 → chatterbot-1.2.16}/tests/test_examples.py +0 -0
  86. {chatterbot-1.2.14 → chatterbot-1.2.16}/tests/test_filters.py +0 -0
  87. {chatterbot-1.2.14 → chatterbot-1.2.16}/tests/test_initialization.py +0 -0
  88. {chatterbot-1.2.14 → chatterbot-1.2.16}/tests/test_languages.py +0 -0
  89. {chatterbot-1.2.14 → chatterbot-1.2.16}/tests/test_parsing.py +0 -0
  90. {chatterbot-1.2.14 → chatterbot-1.2.16}/tests/test_preprocessors.py +0 -0
  91. {chatterbot-1.2.14 → chatterbot-1.2.16}/tests/test_response_selection.py +0 -0
  92. {chatterbot-1.2.14 → chatterbot-1.2.16}/tests/test_search.py +0 -0
  93. {chatterbot-1.2.14 → chatterbot-1.2.16}/tests/test_tagging.py +0 -0
  94. {chatterbot-1.2.14 → chatterbot-1.2.16}/tests/test_turing.py +0 -0
  95. {chatterbot-1.2.14 → chatterbot-1.2.16}/tests/test_utils.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ChatterBot
3
- Version: 1.2.14
3
+ Version: 1.2.16
4
4
  Summary: ChatterBot is a machine learning, conversational dialog engine
5
5
  Author: Gunther Cox
6
6
  License-Expression: BSD-3-Clause
@@ -42,8 +42,7 @@ Requires-Dist: coverage; extra == "test"
42
42
  Requires-Dist: sphinx<9.2,>=5.3; extra == "test"
43
43
  Requires-Dist: sphinx-sitemap>=2.6.0; extra == "test"
44
44
  Requires-Dist: huggingface_hub; extra == "test"
45
- Requires-Dist: django<6.0,>=4.1; extra == "test"
46
- Requires-Dist: click<9.0,>=8.4; extra == "test"
45
+ Requires-Dist: django<6.0,>=5.2.15; extra == "test"
47
46
  Provides-Extra: dev
48
47
  Requires-Dist: pint>=0.8.1; extra == "dev"
49
48
  Requires-Dist: pyyaml<7.0,>=6.0; extra == "dev"
@@ -27,5 +27,4 @@ coverage
27
27
  sphinx<9.2,>=5.3
28
28
  sphinx-sitemap>=2.6.0
29
29
  huggingface_hub
30
- django<6.0,>=4.1
31
- click<9.0,>=8.4
30
+ django<6.0,>=5.2.15
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ChatterBot
3
- Version: 1.2.14
3
+ Version: 1.2.16
4
4
  Summary: ChatterBot is a machine learning, conversational dialog engine
5
5
  Author: Gunther Cox
6
6
  License-Expression: BSD-3-Clause
@@ -42,8 +42,7 @@ Requires-Dist: coverage; extra == "test"
42
42
  Requires-Dist: sphinx<9.2,>=5.3; extra == "test"
43
43
  Requires-Dist: sphinx-sitemap>=2.6.0; extra == "test"
44
44
  Requires-Dist: huggingface_hub; extra == "test"
45
- Requires-Dist: django<6.0,>=4.1; extra == "test"
46
- Requires-Dist: click<9.0,>=8.4; extra == "test"
45
+ Requires-Dist: django<6.0,>=5.2.15; extra == "test"
47
46
  Provides-Extra: dev
48
47
  Requires-Dist: pint>=0.8.1; extra == "dev"
49
48
  Requires-Dist: pyyaml<7.0,>=6.0; extra == "dev"
@@ -4,7 +4,7 @@ ChatterBot is a machine learning, conversational dialog engine.
4
4
  from .chatterbot import ChatBot
5
5
 
6
6
 
7
- __version__ = '1.2.14'
7
+ __version__ = '1.2.16'
8
8
 
9
9
  __all__ = (
10
10
  'ChatBot',
@@ -332,6 +332,11 @@ class ChatBot(object):
332
332
  if most_common.count > 1:
333
333
  result = most_common.statement
334
334
 
335
+ if result is None:
336
+ raise self.ChatBotException(
337
+ f'No logic adapter was able to process the statement "{input_statement.text}".'
338
+ )
339
+
335
340
  response = Statement(
336
341
  text=result.text,
337
342
  in_response_to=input_statement.text,
@@ -117,8 +117,10 @@ class UnitConversion(LogicAdapter, MCPToolAdapter):
117
117
  """
118
118
  response = Statement(text='')
119
119
 
120
- from_parsed = match.group("from")
121
- target_parsed = match.group("target")
120
+ # Units are captured as any non-whitespace text, so a question such
121
+ # as "How many meters are in one kilometer?" captures "kilometer?"
122
+ from_parsed = match.group("from").rstrip('?!.,;:')
123
+ target_parsed = match.group("target").rstrip('?!.,;:')
122
124
  n_statement = match.group("number")
123
125
 
124
126
  if n_statement == 'a' or n_statement == 'an':
@@ -3,6 +3,8 @@ import csv
3
3
  import time
4
4
  import glob
5
5
  import json
6
+ import shutil
7
+ import secrets
6
8
  import tarfile
7
9
  from typing import List, Union
8
10
  from tqdm import tqdm
@@ -10,6 +12,30 @@ from dateutil import parser as date_parser
10
12
  from chatterbot.chatterbot import ChatBot
11
13
  from chatterbot.conversation import Statement
12
14
 
15
+ # True where a directory fd opened with O_NOFOLLOW can fully anchor both
16
+ # directory-metadata operations (mkdir/rename via dir_fd) and the archive
17
+ # writes tarfile itself performs (via the /proc/self/fd/N magic path). Both
18
+ # are required together: anchoring mkdir/rename alone is not enough, since
19
+ # tarfile.extractall() writes through plain path strings and will silently
20
+ # recreate a missing destination through a symlink planted over data_directory.
21
+ # Currently Linux-only, since it depends on procfs.
22
+ _DIR_FD_EXTRACTION_SUPPORTED = (
23
+ hasattr(os, 'O_NOFOLLOW')
24
+ and os.mkdir in os.supports_dir_fd
25
+ and os.rename in os.supports_dir_fd
26
+ and os.path.isdir('/proc/self/fd')
27
+ )
28
+
29
+ # The download path opens the archive itself rather than handing a path to
30
+ # tarfile, so it needs no procfs magic path and can be anchored to a directory
31
+ # fd anywhere the relevant calls accept one.
32
+ _DIR_FD_DOWNLOAD_SUPPORTED = (
33
+ hasattr(os, 'O_NOFOLLOW')
34
+ and os.open in os.supports_dir_fd
35
+ and os.rename in os.supports_dir_fd
36
+ and os.unlink in os.supports_dir_fd
37
+ )
38
+
13
39
 
14
40
  class Trainer(object):
15
41
  """
@@ -579,38 +605,119 @@ class UbuntuCorpusTrainer(CsvFileTrainer):
579
605
  """
580
606
  import requests
581
607
 
608
+ self._reject_symlinked_path(self.data_directory)
609
+
582
610
  # Create the data directory if it does not already exist
583
611
  if not os.path.exists(self.data_directory):
584
- os.makedirs(self.data_directory)
612
+ try:
613
+ os.makedirs(self.data_directory)
614
+ except FileExistsError as e:
615
+ raise self.TrainerInitializationException(
616
+ 'Unable to create data directory {}: {}'.format(self.data_directory, e)
617
+ ) from e
585
618
 
586
619
  file_name = url.split('/')[-1]
587
620
  file_path = os.path.join(self.data_directory, file_name)
588
621
 
622
+ self._reject_symlinked_path(file_path)
623
+
589
624
  # Do not download the data if it already exists
590
625
  if self.is_downloaded(file_path):
591
626
  return file_path
592
627
 
593
- with open(file_path, 'wb') as open_file:
594
- if show_status:
595
- print('Downloading %s' % url)
596
- response = requests.get(url, stream=True)
597
- total_length = response.headers.get('content-length')
628
+ # Write to an unpredictably-named staging file and commit it with a
629
+ # rename, the same way extract() stages its output. os.rename()
630
+ # replaces a symlink at the destination instead of writing through it,
631
+ # so the archive lands inside data_directory even if the path is
632
+ # swapped after the check above. Where supported, the open, the rename
633
+ # and the cleanup are all anchored to a directory fd held open with
634
+ # O_NOFOLLOW, which keeps them bound to the directory inode verified
635
+ # here rather than to whatever the path resolves to later on.
636
+ base_fd = None
637
+ if _DIR_FD_DOWNLOAD_SUPPORTED:
638
+ base_fd = self._open_verified_directory_fd(self.data_directory)
639
+
640
+ staging_name = 'tmp_' + secrets.token_hex(8)
641
+ staging_path = os.path.join(self.data_directory, staging_name)
642
+ open_flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL
643
+ # O_NOFOLLOW is POSIX-only, and O_BINARY exists only on Windows, where
644
+ # os.open() would otherwise translate newlines inside the archive.
645
+ open_flags |= getattr(os, 'O_NOFOLLOW', 0) | getattr(os, 'O_BINARY', 0)
598
646
 
599
- if total_length is None:
600
- # No content length header
601
- open_file.write(response.content)
647
+ try:
648
+ if base_fd is not None:
649
+ staging_fd = os.open(staging_name, open_flags, 0o666, dir_fd=base_fd)
602
650
  else:
603
- for data in tqdm(
604
- response.iter_content(chunk_size=4096),
605
- desc='Downloading',
606
- disable=not show_status
607
- ):
608
- open_file.write(data)
651
+ staging_fd = os.open(staging_path, open_flags, 0o666)
652
+
653
+ with os.fdopen(staging_fd, 'wb') as open_file:
654
+ if show_status:
655
+ print('Downloading %s' % url)
656
+ response = requests.get(url, stream=True)
657
+ total_length = response.headers.get('content-length')
658
+
659
+ if total_length is None:
660
+ # No content length header. Stream regardless of whether
661
+ # progress is shown, to avoid buffering the whole body in memory.
662
+ for data in response.iter_content(chunk_size=4096):
663
+ open_file.write(data)
664
+ else:
665
+ for data in tqdm(
666
+ response.iter_content(chunk_size=4096),
667
+ desc='Downloading',
668
+ disable=not show_status
669
+ ):
670
+ open_file.write(data)
671
+
672
+ try:
673
+ if base_fd is not None:
674
+ os.rename(staging_name, file_name, src_dir_fd=base_fd, dst_dir_fd=base_fd)
675
+ else:
676
+ os.rename(staging_path, file_path)
677
+ except OSError as e:
678
+ raise self.TrainerInitializationException(
679
+ 'Refusing to commit downloaded archive to {}: {}'.format(file_path, e)
680
+ ) from e
681
+ except BaseException:
682
+ try:
683
+ if base_fd is not None:
684
+ os.unlink(staging_name, dir_fd=base_fd)
685
+ else:
686
+ os.unlink(staging_path)
687
+ except OSError:
688
+ pass
689
+ raise
690
+ finally:
691
+ if base_fd is not None:
692
+ os.close(base_fd)
609
693
 
610
694
  if show_status:
611
695
  print('Download location: %s' % file_path)
612
696
  return file_path
613
697
 
698
+ def _reject_symlinked_path(self, path):
699
+ # A trailing separator forces lstat to resolve the final component,
700
+ # so normpath first or islink silently returns False for a symlink.
701
+ if os.path.islink(os.path.normpath(path)):
702
+ raise self.TrainerInitializationException(
703
+ 'Refusing to use a symbolic link as a data path: {}'.format(path)
704
+ )
705
+
706
+ def _reject_symlinked_data_path(self):
707
+ self._reject_symlinked_path(self.data_directory)
708
+ self._reject_symlinked_path(self.data_path)
709
+
710
+ def _open_verified_directory_fd(self, path):
711
+ # O_NOFOLLOW makes this fail instead of dereferencing a symlink. The
712
+ # returned fd stays bound to this exact directory inode even if the
713
+ # path is later replaced, so dir_fd-relative ops can't be redirected.
714
+ try:
715
+ return os.open(path, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW)
716
+ except OSError as e:
717
+ raise self.TrainerInitializationException(
718
+ 'Refusing to use data directory: unable to safely open {}: {}'.format(path, e)
719
+ ) from e
720
+
614
721
  def extract(self, file_path: str):
615
722
  """
616
723
  Extract a tar file at the specified file path.
@@ -618,22 +725,28 @@ class UbuntuCorpusTrainer(CsvFileTrainer):
618
725
  if not self.disable_progress:
619
726
  print('Extracting {}'.format(file_path))
620
727
 
621
- if os.path.islink(self.data_path):
622
- raise self.TrainerInitializationException(
623
- 'Refusing to extract archive to a symbolic link: {}'.format(self.data_path)
624
- )
728
+ self._reject_symlinked_data_path()
625
729
 
626
- if not os.path.exists(self.data_path):
627
- os.makedirs(self.data_path)
730
+ if not os.path.exists(self.data_directory):
731
+ try:
732
+ os.makedirs(self.data_directory)
733
+ except FileExistsError as e:
734
+ raise self.TrainerInitializationException(
735
+ 'Unable to create data directory {}: {}'.format(self.data_directory, e)
736
+ ) from e
628
737
 
629
738
  def is_within_directory(directory, target):
630
739
 
631
740
  abs_directory = os.path.realpath(directory)
632
741
  abs_target = os.path.realpath(target)
633
742
 
634
- prefix = os.path.commonprefix([abs_directory, abs_target])
743
+ try:
744
+ common_path = os.path.commonpath([abs_directory, abs_target])
745
+ except ValueError:
746
+ # Raised when paths don't share a common root (e.g. different drives)
747
+ return False
635
748
 
636
- return prefix == abs_directory
749
+ return common_path == abs_directory
637
750
 
638
751
  def safe_extract(tar, path='.', members=None, *, numeric_owner=False):
639
752
 
@@ -644,15 +757,78 @@ class UbuntuCorpusTrainer(CsvFileTrainer):
644
757
  if not is_within_directory(path, member_path):
645
758
  raise Exception('Attempted Path Traversal in Tar File')
646
759
 
647
- tar.extractall(path, members, numeric_owner=numeric_owner)
760
+ extract_kwargs = {}
761
+ if hasattr(tarfile, 'data_filter'):
762
+ # Belt-and-suspenders: also apply the standard library's own
763
+ # vetted extraction filter (PEP 706) alongside the checks above.
764
+ extract_kwargs['filter'] = 'data'
765
+
766
+ tar.extractall(path, members, numeric_owner=numeric_owner, **extract_kwargs)
767
+
768
+ # Extract into an unpredictably-named staging directory rather than
769
+ # data_path directly, then commit it into place with a single atomic
770
+ # rename. extractall() writes members one at a time, so checking
771
+ # data_path once up front leaves a window for an attacker to swap it
772
+ # for a symlink mid-extraction. Renaming a directory onto an existing
773
+ # symlink fails closed with ENOTDIR instead of writing through it, so
774
+ # no window remains at the point the data actually lands at data_path.
775
+ #
776
+ # Where supported, the mkdir, the extraction writes, and the final
777
+ # rename are all additionally anchored to a directory fd opened with
778
+ # O_NOFOLLOW, via dir_fd for mkdir/rename and via the /proc/self/fd/N
779
+ # magic path for the archive writes themselves (tarfile has no dir_fd
780
+ # support of its own, and will happily recreate a missing destination
781
+ # through a symlink planted over data_directory if given a plain
782
+ # path). Anchoring only the mkdir/rename and not the archive writes
783
+ # would be worse than not anchoring at all: it would silently leave
784
+ # the real destination empty while extracting through the symlink.
785
+ base_fd = None
786
+ extraction_root = self.data_directory
787
+ if _DIR_FD_EXTRACTION_SUPPORTED:
788
+ base_fd = self._open_verified_directory_fd(self.data_directory)
789
+ extraction_root = '/proc/self/fd/{}'.format(base_fd)
790
+
791
+ staging_name = 'tmp_' + secrets.token_hex(8)
792
+ staging_dir = os.path.join(extraction_root, staging_name)
648
793
 
649
794
  try:
650
- with tarfile.open(file_path, 'r') as tar:
651
- safe_extract(tar, path=self.data_path, members=tqdm(tar, disable=self.disable_progress))
652
- except tarfile.ReadError as e:
653
- raise self.TrainerInitializationException(
654
- f'The provided data file is not a valid tar file: {file_path}'
655
- ) from e
795
+ try:
796
+ if base_fd is not None:
797
+ os.mkdir(staging_name, dir_fd=base_fd)
798
+ else:
799
+ os.mkdir(staging_dir)
800
+ except OSError as e:
801
+ raise self.TrainerInitializationException(
802
+ 'Unable to create a staging directory for extraction: {}'.format(e)
803
+ ) from e
804
+
805
+ try:
806
+ with tarfile.open(file_path, 'r') as tar:
807
+ safe_extract(tar, path=staging_dir, members=tqdm(tar, disable=self.disable_progress))
808
+ except tarfile.ReadError as e:
809
+ raise self.TrainerInitializationException(
810
+ f'The provided data file is not a valid tar file: {file_path}'
811
+ ) from e
812
+
813
+ # Re-check immediately before the commit to narrow the race window further
814
+ self._reject_symlinked_data_path()
815
+
816
+ final_name = os.path.basename(self.data_path)
817
+ try:
818
+ if base_fd is not None:
819
+ os.rename(staging_name, final_name, src_dir_fd=base_fd, dst_dir_fd=base_fd)
820
+ else:
821
+ os.rename(staging_dir, self.data_path)
822
+ except OSError as e:
823
+ raise self.TrainerInitializationException(
824
+ 'Refusing to commit extracted archive to {}: {}'.format(self.data_path, e)
825
+ ) from e
826
+ except BaseException:
827
+ shutil.rmtree(staging_dir, ignore_errors=True)
828
+ raise
829
+ finally:
830
+ if base_fd is not None:
831
+ os.close(base_fd)
656
832
 
657
833
  self.chatbot.logger.info('File extracted to {}'.format(self.data_path))
658
834
 
@@ -74,8 +74,7 @@ test = [
74
74
  "sphinx>=5.3,<9.2",
75
75
  "sphinx-sitemap>=2.6.0",
76
76
  "huggingface_hub",
77
- "django>=4.1,<6.0",
78
- "click>=8.4,<9.0"
77
+ "django>=5.2.15,<6.0"
79
78
  ]
80
79
  dev = [
81
80
  "pint>=0.8.1",
@@ -1,6 +1,6 @@
1
1
  from tests.base_case import ChatBotTestCase
2
2
  from chatterbot import ChatBot
3
- from chatterbot.logic import LogicAdapter
3
+ from chatterbot.logic import LogicAdapter, MathematicalEvaluation
4
4
  from chatterbot.conversation import Statement
5
5
  from chatterbot import languages
6
6
 
@@ -418,6 +418,18 @@ class ChatBotLogicAdapterTestCase(ChatBotTestCase):
418
418
  self.assertEqual(statement.confidence, 0.5)
419
419
  self.assertEqual(statement.text, 'Good morning.')
420
420
 
421
+ def test_no_adapter_can_process_statement(self):
422
+ """
423
+ If no logic adapter can process the statement,
424
+ a ChatBotException should be raised.
425
+ """
426
+ self.chatbot.logic_adapters = [MathematicalEvaluation(self.chatbot)]
427
+
428
+ with self.assertRaises(ChatBot.ChatBotException) as exc:
429
+ self.chatbot.generate_response(Statement(text='Howdy!'))
430
+
431
+ self.assertIn('No logic adapter was able to process', str(exc.exception))
432
+
421
433
  def test_chatbot_set_for_all_logic_adapters(self):
422
434
  for sub_adapter in self.chatbot.logic_adapters:
423
435
  self.assertEqual(sub_adapter.chatbot, self.chatbot)
File without changes
File without changes
File without changes