Skip to content

Commit c27f37a

Browse files
authored
Sanitize device names in paths sezanzeb#507 (sezanzeb#508)
1 parent aa6a505 commit c27f37a

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

inputremapper/configs/paths.py

+13
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,26 @@ def remove(path):
8686
os.remove(path)
8787

8888

89+
def sanitize_path_component(group_name: str) -> str:
90+
"""replace characters listed in
91+
https://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words
92+
with an underscore
93+
"""
94+
for c in '/\\?%*:|"<>':
95+
if c in group_name:
96+
group_name = group_name.replace(c, "_")
97+
return group_name
98+
99+
89100
def get_preset_path(group_name=None, preset=None):
90101
"""Get a path to the stored preset, or to store a preset to."""
91102
presets_base = os.path.join(CONFIG_PATH, "presets")
92103

93104
if group_name is None:
94105
return presets_base
95106

107+
group_name = sanitize_path_component(group_name)
108+
96109
if preset is not None:
97110
# the extension of the preset should not be shown in the ui.
98111
# if a .json extension arrives this place, it has not been

tests/test.py

+6
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,12 @@ def read_write_history_pipe():
247247
"info": evdev.device.DeviceInfo(6, 1, 6, 1),
248248
"name": "YuBiCofooYuBiKeYbar",
249249
},
250+
"/dev/input/event52": {
251+
"capabilities": {evdev.ecodes.EV_KEY: keyboard_keys},
252+
"phys": "usb-0000:03:00.0-3/input1",
253+
"info": evdev.device.DeviceInfo(2, 1, 2, 1),
254+
"name": "Qux/Device?",
255+
},
250256
}
251257

252258

tests/unit/test_groups.py

+9
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,14 @@ def test_find_groups(self):
125125
"key": "input-remapper Bar Device",
126126
}
127127
),
128+
json.dumps(
129+
{
130+
"paths": ["/dev/input/event52"],
131+
"names": ["Qux/Device?"],
132+
"types": [KEYBOARD],
133+
"key": "Qux/Device?",
134+
}
135+
),
128136
]
129137
),
130138
)
@@ -142,6 +150,7 @@ def test_list_group_names(self):
142150
"Foo Device",
143151
"Bar Device",
144152
"gamepad",
153+
"Qux/Device?",
145154
],
146155
)
147156

tests/unit/test_presets.py

+18
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ def test_create_preset_3(self):
108108
self.assertTrue(os.path.exists(f"{PRESETS}/Foo Device/pre set 2.json"))
109109
self.assertTrue(os.path.exists(f"{PRESETS}/Foo Device/pre set 3.json"))
110110

111+
def test_create_preset_4(self):
112+
create_preset("Qux/Device?", "bla")
113+
self.assertTrue(os.path.exists(f"{PRESETS}/Qux_Device_/bla.json"))
114+
111115

112116
class TestDeletePreset(unittest.TestCase):
113117
def tearDown(self):
@@ -127,6 +131,11 @@ def test_delete_preset(self):
127131
# if no preset in the directory, remove the directory
128132
self.assertFalse(os.path.exists(f"{PRESETS}/Foo Device"))
129133

134+
create_preset("Qux/Device?", "bla")
135+
self.assertTrue(os.path.exists(f"{PRESETS}/Qux_Device_/bla.json"))
136+
delete_preset("Qux/Device?", "bla")
137+
self.assertFalse(os.path.exists(f"{PRESETS}/Qux_Device_/bla.json"))
138+
130139

131140
class TestRenamePreset(unittest.TestCase):
132141
def tearDown(self):
@@ -137,12 +146,18 @@ def test_rename_preset(self):
137146
create_preset("Foo Device", "preset 1")
138147
create_preset("Foo Device", "preset 2")
139148
create_preset("Foo Device", "foobar")
149+
create_preset("Qux/Device?", "bla")
150+
140151
rename_preset("Foo Device", "preset 1", "foobar")
141152
rename_preset("Foo Device", "preset 2", "foobar")
153+
rename_preset("Qux/Device?", "bla", "blubb")
154+
142155
self.assertFalse(os.path.exists(f"{PRESETS}/Foo Device/preset 1.json"))
143156
self.assertTrue(os.path.exists(f"{PRESETS}/Foo Device/foobar.json"))
144157
self.assertTrue(os.path.exists(f"{PRESETS}/Foo Device/foobar 2.json"))
145158
self.assertTrue(os.path.exists(f"{PRESETS}/Foo Device/foobar 3.json"))
159+
self.assertFalse(os.path.exists(f"{PRESETS}/Qux_Device_/bla.json"))
160+
self.assertTrue(os.path.exists(f"{PRESETS}/Qux_Device_/blubb.json"))
146161

147162

148163
class TestFindPresets(unittest.TestCase):
@@ -152,6 +167,9 @@ def tearDown(self):
152167

153168
def test_get_presets(self):
154169
os.makedirs(os.path.join(PRESETS, "1234"))
170+
os.makedirs(f"{PRESETS}/Qux_Device_")
171+
os.mknod(f"{PRESETS}/Qux_Device_/blubb.json")
172+
self.assertEqual(len(get_presets("Qux/Device?")), 1)
155173

156174
os.mknod(os.path.join(PRESETS, "1234", "picture.png"))
157175
self.assertEqual(len(get_presets("1234")), 0)

0 commit comments

Comments
 (0)