Browse Source

Fix path sanitation for absolute Windows paths

When using an absolute Windows path (e.g. `C:\Users\USER\Documents\`)
for the `newbot` command the translation table replaced the valid `:`
character in the drive causing it to create the directory at the wrong
place.

Fixes #10096
pull/10102/head
Eric Schneider 2 months ago
committed by GitHub
parent
commit
52967ec103
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 10
      discord/__main__.py

10
discord/__main__.py

@ -28,7 +28,7 @@ from typing import Optional, Tuple, Dict
import argparse import argparse
import sys import sys
from pathlib import Path from pathlib import Path, PurePath, PureWindowsPath
import discord import discord
import importlib.metadata import importlib.metadata
@ -225,8 +225,14 @@ def to_path(parser: argparse.ArgumentParser, name: str, *, replace_spaces: bool
) )
if len(name) <= 4 and name.upper() in forbidden: if len(name) <= 4 and name.upper() in forbidden:
parser.error('invalid directory name given, use a different one') parser.error('invalid directory name given, use a different one')
path = PurePath(name)
if isinstance(path, PureWindowsPath) and path.drive:
drive, rest = path.parts[0], path.parts[1:]
transformed = tuple(map(lambda p: p.translate(_translation_table), rest))
name = drive + '\\'.join(transformed)
name = name.translate(_translation_table) else:
name = name.translate(_translation_table)
if replace_spaces: if replace_spaces:
name = name.replace(' ', '-') name = name.replace(' ', '-')
return Path(name) return Path(name)

Loading…
Cancel
Save