Skip to content

Sanitize and Uniquify Map Filename Generation to Prevent Collisions and Invalid Filenames #576

Description

@sourcery-ai

There is a potential bug risk in the current map filename generation logic. The filename is derived directly from the destination string, which may contain special characters or result in duplicate filenames if the same destination is used multiple times. This can lead to filename collisions or invalid filenames, especially on filesystems with strict naming rules.

Suggested Solution:

  • Sanitize the destination string to remove or replace special characters.
  • Add a hash of the destination to the filename to ensure uniqueness.

Example implementation:

import hashlib
import re

# Sanitize destination for filename
sanitized_destination = re.sub(r'[^a-zA-Z0-9_]', '_', destination.lower())
# Create a short hash of the destination for uniqueness
dest_hash = hashlib.md5(destination.encode('utf-8')).hexdigest()[:8]
map_filename = f"{sanitized_destination}_{dest_hash}_map.png"
map_path = self.maps_directory / map_filename

Action Items:

  • Update the map filename generation logic to use the above approach or similar.
  • Add tests to ensure filenames are unique and valid for various destination strings.

Note: This code path is currently not in active use and serves as an example, but it should be fixed before any production use.


I created this issue for @ItsSimko from #575 (comment).

Tips and commands

Getting Help

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions