Skip to content

Commit 6accf03

Browse files
committed
better checking for invalid profile names
1 parent 754ad68 commit 6accf03

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed

src/qbpm/__init__.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,11 @@ def __init__(self, name: str, profile_dir: Path) -> None:
2020
self.profile_dir = profile_dir
2121
self.root = self.profile_dir / name
2222

23-
def check(self) -> Optional["Profile"]:
24-
if "/" in self.name:
25-
error("profile name cannot contain slashes")
26-
return None
27-
return self
28-
29-
def exists(self) -> bool:
30-
return self.root.exists() and self.root.is_dir()
23+
def check_name(self) -> bool:
24+
if "/" in self.name or self.name in [".", ".."]:
25+
error("profile name cannot be a path")
26+
return False
27+
return True
3128

3229
def cmdline(self) -> list[str]:
3330
return [

src/qbpm/main.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ def launch_profile(
190190
191191
All QB_ARGS are passed on to qutebrowser."""
192192
profile = Profile(profile_name, **vars(context))
193+
if not profiles.check(profile):
194+
sys.exit(1)
193195
exit_with(launch_qutebrowser(profile, foreground, qb_args))
194196

195197

@@ -223,8 +225,7 @@ def choose(
223225
def edit(context: Context, profile_name: str) -> None:
224226
"""Edit a profile's config.py."""
225227
profile = Profile(profile_name, **vars(context))
226-
if not profile.exists():
227-
error(f"profile {profile.name} not found at {profile.root}")
228+
if not profiles.check(profile):
228229
sys.exit(1)
229230
click.edit(filename=str(profile.root / "config" / "config.py"))
230231

src/qbpm/operations.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ def from_session(
2424

2525

2626
def desktop(profile: Profile) -> bool:
27-
exists = profile.exists()
27+
exists = profiles.check(profile)
2828
if exists:
2929
create_desktop_file(profile)
30-
else:
31-
error(f"profile {profile.name} not found at {profile.root}")
3230
return exists

src/qbpm/profiles.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424

2525
def create_profile(profile: Profile, overwrite: bool = False) -> bool:
26-
if not profile.check():
26+
if not profile.check_name():
2727
return False
2828

2929
if not overwrite and profile.root.exists():
@@ -53,13 +53,19 @@ def create_config(
5353
out(f"config.source(r'{qb_config_dir / 'config.py'}')")
5454

5555

56-
def exists(profile: Profile) -> bool:
57-
if profile.root.exists() and not profile.root.is_dir():
58-
error(f"{profile.root} is not a directory")
56+
def check(profile: Profile) -> bool:
57+
if not profile.check_name():
5958
return False
60-
if not profile.root.exists():
59+
exists = profile.root.exists()
60+
if not exists:
6161
error(f"{profile.root} does not exist")
6262
return False
63+
if not profile.root.is_dir():
64+
error(f"{profile.root} is not a directory")
65+
return False
66+
if not (profile.root / "config").is_dir():
67+
error(f"no config directory in {profile.root}, is it a profile?")
68+
return False
6369
return True
6470

6571

0 commit comments

Comments
 (0)