mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-11-03 03:16:10 -06:00 
			
		
		
		
	Prevent index out of bounds for tag lists
This commit is contained in:
		@@ -316,16 +316,35 @@ class Document(models.Model):
 | 
			
		||||
 | 
			
		||||
        return mylist
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def fill_list(input_list, length, filler):
 | 
			
		||||
        while len(input_list) < length:
 | 
			
		||||
            input_list.append(slugify(filler))
 | 
			
		||||
 | 
			
		||||
        return input_list
 | 
			
		||||
 | 
			
		||||
    def generate_source_filename(self):
 | 
			
		||||
        # Create filename based on configured format
 | 
			
		||||
        if settings.PAPERLESS_FILENAME_FORMAT is not None:
 | 
			
		||||
            path = settings.PAPERLESS_FILENAME_FORMAT.format(
 | 
			
		||||
                   correspondent=slugify(self.correspondent),
 | 
			
		||||
                   title=slugify(self.title),
 | 
			
		||||
                   created=slugify(self.created),
 | 
			
		||||
                   added=slugify(self.added),
 | 
			
		||||
                   tag=defaultdict(str, self.many_to_dictionary(self.tags)),
 | 
			
		||||
                   tags=self.many_to_list(self.tags))
 | 
			
		||||
            list_length = 10
 | 
			
		||||
            tags = self.many_to_list(self.tags)
 | 
			
		||||
            while True:
 | 
			
		||||
                tags = Document.fill_list(tags, list_length, None)
 | 
			
		||||
                try:
 | 
			
		||||
                    path = settings.PAPERLESS_FILENAME_FORMAT.format(
 | 
			
		||||
                           correspondent=slugify(self.correspondent),
 | 
			
		||||
                           title=slugify(self.title),
 | 
			
		||||
                           created=slugify(self.created),
 | 
			
		||||
                           added=slugify(self.added),
 | 
			
		||||
                           tag=defaultdict(str, self.many_to_dictionary(self.tags)),
 | 
			
		||||
                           tags=tags)
 | 
			
		||||
                    break
 | 
			
		||||
                except IndexError:
 | 
			
		||||
                    list_length *= 10
 | 
			
		||||
 | 
			
		||||
                if list_length > 1000:
 | 
			
		||||
                    path = ""
 | 
			
		||||
                    break
 | 
			
		||||
        else:
 | 
			
		||||
            path = ""
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -234,6 +234,60 @@ class TestDate(TestCase):
 | 
			
		||||
 | 
			
		||||
        document.delete()
 | 
			
		||||
 | 
			
		||||
    @override_settings(MEDIA_ROOT="/tmp/paperless-tests-{}".
 | 
			
		||||
                       format(str(uuid4())[:8]))
 | 
			
		||||
    @override_settings(PAPERLESS_FILENAME_FORMAT="{tags[0]}")
 | 
			
		||||
    def test_tags_out_of_bounds_0(self):
 | 
			
		||||
        document = Document()
 | 
			
		||||
        document.file_type = "pdf"
 | 
			
		||||
        document.storage_type = Document.STORAGE_TYPE_UNENCRYPTED
 | 
			
		||||
        document.save()
 | 
			
		||||
 | 
			
		||||
        # Ensure that filename is properly generated
 | 
			
		||||
        tmp = document.source_filename
 | 
			
		||||
        self.assertEqual(document.generate_source_filename(),
 | 
			
		||||
                         "none-0000001.pdf")
 | 
			
		||||
        document.create_source_directory()
 | 
			
		||||
        Path(document.source_path).touch()
 | 
			
		||||
 | 
			
		||||
        document.delete()
 | 
			
		||||
 | 
			
		||||
    @override_settings(MEDIA_ROOT="/tmp/paperless-tests-{}".
 | 
			
		||||
                       format(str(uuid4())[:8]))
 | 
			
		||||
    @override_settings(PAPERLESS_FILENAME_FORMAT="{tags[10000000]}")
 | 
			
		||||
    def test_tags_out_of_bounds_10000000(self):
 | 
			
		||||
        document = Document()
 | 
			
		||||
        document.file_type = "pdf"
 | 
			
		||||
        document.storage_type = Document.STORAGE_TYPE_UNENCRYPTED
 | 
			
		||||
        document.save()
 | 
			
		||||
 | 
			
		||||
        # Ensure that filename is properly generated
 | 
			
		||||
        tmp = document.source_filename
 | 
			
		||||
        self.assertEqual(document.generate_source_filename(),
 | 
			
		||||
                         "0000001.pdf")
 | 
			
		||||
        document.create_source_directory()
 | 
			
		||||
        Path(document.source_path).touch()
 | 
			
		||||
 | 
			
		||||
        document.delete()
 | 
			
		||||
 | 
			
		||||
    @override_settings(MEDIA_ROOT="/tmp/paperless-tests-{}".
 | 
			
		||||
                       format(str(uuid4())[:8]))
 | 
			
		||||
    @override_settings(PAPERLESS_FILENAME_FORMAT="{tags[99]}")
 | 
			
		||||
    def test_tags_out_of_bounds_99(self):
 | 
			
		||||
        document = Document()
 | 
			
		||||
        document.file_type = "pdf"
 | 
			
		||||
        document.storage_type = Document.STORAGE_TYPE_UNENCRYPTED
 | 
			
		||||
        document.save()
 | 
			
		||||
 | 
			
		||||
        # Ensure that filename is properly generated
 | 
			
		||||
        tmp = document.source_filename
 | 
			
		||||
        self.assertEqual(document.generate_source_filename(),
 | 
			
		||||
                         "none-0000001.pdf")
 | 
			
		||||
        document.create_source_directory()
 | 
			
		||||
        Path(document.source_path).touch()
 | 
			
		||||
 | 
			
		||||
        document.delete()
 | 
			
		||||
 | 
			
		||||
    @override_settings(MEDIA_ROOT="/tmp/paperless-tests-{}".
 | 
			
		||||
                       format(str(uuid4())[:8]))
 | 
			
		||||
    @override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/" +
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user