Skip to content

Commit e09f684

Browse files
committed
improved host input sanitizing
1 parent e5941a8 commit e09f684

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

bale/drawer.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from typing import Optional
12
from nicegui import ui # type: ignore
23
from bale import elements as el
34
from bale.tabs import Tab
@@ -97,8 +98,12 @@ async def send_key():
9798
with ui.dialog() as host_dialog, el.Card():
9899
with el.DBody(height="[560px]", width="[360px]"):
99100
with el.WColumn():
100-
host_input = el.DInput(label="Host", value=" ")
101-
hostname_input = el.DInput(label="Hostname", value=" ")
101+
all_hosts = list(ssh.get_hosts())
102+
if name != "":
103+
if name in all_hosts:
104+
all_hosts.remove(name)
105+
host_input = el.VInput(label="Host", value=" ", invalid_characters="""'`"$\\;&<>|(){} """, invalid_values=all_hosts, max_length=20)
106+
hostname_input = el.VInput(label="Hostname", value=" ", invalid_characters="""!@#$%^&*'`"\\/:;<>|(){}=+[],? """)
102107
username_input = el.DInput(label="Username", value=" ")
103108
save_em = el.ErrorAggregator(host_input, hostname_input, username_input)
104109
with el.Card() as c:

bale/elements.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,52 @@ def __init__(
131131
self.value = ""
132132

133133

134+
class VInput(ui.input):
135+
def __init__(
136+
self,
137+
label: str | None = None,
138+
*,
139+
placeholder: str | None = None,
140+
value: str = " ",
141+
password: bool = False,
142+
password_toggle_button: bool = False,
143+
on_change: Callable[..., Any] | None = None,
144+
autocomplete: List[str] | None = None,
145+
invalid_characters: str = "",
146+
invalid_values: List[str] = [],
147+
max_length: int = 64,
148+
check: Callable[..., Any] | None = None,
149+
) -> None:
150+
def checks(value: str) -> bool:
151+
if value is None or value == "" or len(value) > max_length:
152+
return False
153+
for invalid_character in invalid_characters:
154+
if invalid_character in value:
155+
return False
156+
for invalid_value in invalid_values:
157+
if invalid_value == value:
158+
return False
159+
if check is not None:
160+
check_status = check(value)
161+
if check_status is not None:
162+
return check_status
163+
return True
164+
165+
super().__init__(
166+
label,
167+
placeholder=placeholder,
168+
value=value,
169+
password=password,
170+
password_toggle_button=password_toggle_button,
171+
on_change=on_change,
172+
autocomplete=autocomplete,
173+
validation={"": lambda value: checks(value)},
174+
)
175+
self.tailwind.width("full")
176+
if value == " ":
177+
self.value = ""
178+
179+
134180
class FInput(ui.input):
135181
def __init__(
136182
self,

0 commit comments

Comments
 (0)