Daniel Brötzmann pushed to branch sanitize-filenames at gajim / gajim

Commits:

2 changed files:

Changes:

  • gajim/common/helpers.py
    ... ... @@ -37,7 +37,6 @@
    37 37
     import re
    
    38 38
     import os
    
    39 39
     import subprocess
    
    40
    -import base64
    
    41 40
     import hashlib
    
    42 41
     import shlex
    
    43 42
     import socket
    
    ... ... @@ -58,7 +57,6 @@
    58 57
     from datetime import timedelta
    
    59 58
     from urllib.parse import unquote
    
    60 59
     from urllib.parse import urlparse
    
    61
    -from encodings.punycode import punycode_encode
    
    62 60
     from functools import wraps
    
    63 61
     from pathlib import Path
    
    64 62
     from packaging.version import Version as V
    
    ... ... @@ -358,26 +356,45 @@ def get_file_path_from_dnd_dropped_uri(uri: str) -> str:
    358 356
     
    
    359 357
     def sanitize_filename(filename: str) -> str:
    
    360 358
         '''
    
    361
    -    Make sure the filename we will write does contain only acceptable and latin
    
    362
    -    characters, and is not too long (in that case hash it)
    
    359
    +    Sanitize filename of elements not allowed on Windows
    
    360
    +    https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file
    
    363 361
         '''
    
    364
    -    # 48 is the limit
    
    365
    -    if len(filename) > 48:
    
    366
    -        hash_ = hashlib.md5(filename.encode('utf-8'))
    
    367
    -        filename = base64.b64encode(hash_.digest()).decode('utf-8')
    
    368
    -
    
    369
    -    # make it latin chars only
    
    370
    -    filename = punycode_encode(filename).decode('utf-8')
    
    371
    -    filename = filename.replace('/', '_')
    
    372
    -    if os.name == 'nt':
    
    373
    -        filename = filename.replace('?', '_')\
    
    374
    -                           .replace(':', '_')\
    
    375
    -                           .replace('\\', '_')\
    
    376
    -                           .replace('"', "'")\
    
    377
    -                           .replace('|', '_')\
    
    378
    -                           .replace('*', '_')\
    
    379
    -                           .replace('<', '_')\
    
    380
    -                           .replace('>', '_')
    
    362
    +    disallowed_chars = [
    
    363
    +        '<',
    
    364
    +        '>',
    
    365
    +        ':',
    
    366
    +        '"',
    
    367
    +        '/',
    
    368
    +        '\\',
    
    369
    +        '|',
    
    370
    +        '?',
    
    371
    +        '*',
    
    372
    +        '..',
    
    373
    +        'CON',
    
    374
    +        'PRN',
    
    375
    +        'AUX',
    
    376
    +        'NUL',
    
    377
    +        'COM1',
    
    378
    +        'COM2',
    
    379
    +        'COM3',
    
    380
    +        'COM4',
    
    381
    +        'COM5',
    
    382
    +        'COM6',
    
    383
    +        'COM7',
    
    384
    +        'COM8',
    
    385
    +        'COM9',
    
    386
    +        'LPT1',
    
    387
    +        'LPT2',
    
    388
    +        'LPT3',
    
    389
    +        'LPT4',
    
    390
    +        'LPT5',
    
    391
    +        'LPT6',
    
    392
    +        'LPT7',
    
    393
    +        'LPT8',
    
    394
    +        'LPT9',
    
    395
    +    ]
    
    396
    +    for char in disallowed_chars:
    
    397
    +        filename = filename.replace(char, '_')
    
    381 398
     
    
    382 399
         return filename
    
    383 400
     
    

  • gajim/common/preview_helpers.py
    ... ... @@ -45,6 +45,7 @@
    45 45
     from cryptography.hazmat.primitives.ciphers import algorithms
    
    46 46
     from cryptography.hazmat.primitives.ciphers.modes import GCM
    
    47 47
     
    
    48
    +from gajim.common.helpers import sanitize_filename
    
    48 49
     from gajim.common.i18n import _
    
    49 50
     
    
    50 51
     log = logging.getLogger('gajim.c.preview_helpers')
    
    ... ... @@ -315,6 +316,8 @@ def get_image_paths(uri: str,
    315 316
             # so the filename should not exceed 90
    
    316 317
             web_stem = web_stem[:90]
    
    317 318
     
    
    319
    +    web_stem = sanitize_filename(web_stem)
    
    320
    +
    
    318 321
         name_hash = hashlib.sha1(str(uri).encode()).hexdigest()
    
    319 322
     
    
    320 323
         orig_filename = f'{web_stem}_{name_hash}{extension}'