From 7667834b6de23ce71ea2e805f6a50eb679444b9e Mon Sep 17 00:00:00 2001 From: Maxim Devaev Date: Mon, 6 Mar 2023 04:18:38 +0200 Subject: structly validation --- kvmd/plugins/msd/otg/storage.py | 2 +- kvmd/validators/kvm.py | 2 -- kvmd/validators/os.py | 7 ++++++- testenv/tests/validators/test_kvm.py | 23 ++++++++--------------- testenv/tests/validators/test_os.py | 18 ++++++------------ 5 files changed, 21 insertions(+), 31 deletions(-) diff --git a/kvmd/plugins/msd/otg/storage.py b/kvmd/plugins/msd/otg/storage.py index d5f90334..80e6fa87 100644 --- a/kvmd/plugins/msd/otg/storage.py +++ b/kvmd/plugins/msd/otg/storage.py @@ -138,7 +138,7 @@ class Storage: def __filter(self, items: list[str]) -> Generator[str, None, None]: for item in sorted(map(str.strip, items)): - if not item.startswith(".__") and item != "lost+found": + if not item.startswith(".") and item != "lost+found": yield item def get_image_by_name(self, name: str) -> Image: diff --git a/kvmd/validators/kvm.py b/kvmd/validators/kvm.py index 5ea6ee0b..32939af7 100644 --- a/kvmd/validators/kvm.py +++ b/kvmd/validators/kvm.py @@ -48,8 +48,6 @@ def valid_msd_image_name(arg: Any) -> str: raise_error(arg, name) for (index, part) in enumerate(list(parts)): parts[index] = valid_printable_filename(part, name=name) - if part.startswith(".__") or part == "lost+found": - raise_error(part, name) return "/".join(parts) diff --git a/kvmd/validators/os.py b/kvmd/validators/os.py index 97aabdf0..702984c5 100644 --- a/kvmd/validators/os.py +++ b/kvmd/validators/os.py @@ -78,7 +78,12 @@ def valid_printable_filename(arg: Any, name: str="") -> str: arg = valid_stripped_string_not_empty(arg, name) - if "/" in arg or "\0" in arg or arg in [".", ".."]: + if ( + "/" in arg + or "\0" in arg + or arg.startswith(".") + or arg == "lost+found" + ): raise_error(arg, name) arg = "".join( diff --git a/testenv/tests/validators/test_kvm.py b/testenv/tests/validators/test_kvm.py index f9990194..c3c2eea9 100644 --- a/testenv/tests/validators/test_kvm.py +++ b/testenv/tests/validators/test_kvm.py @@ -72,20 +72,12 @@ def test_fail__valid_atx_button(arg: Any) -> None: ("\n" + "x" * 1000, "x" * 255), ("test", "test"), ("test test [test] #test$", "test test [test] #test$"), - (".test", ".test"), - ("..test", "..test"), - ("..тест..", "..тест.."), - ("..те\\ст..", "..те\\ст.."), - (".....", "....."), - (".....txt", ".....txt"), - (" .. .", ".. ."), - ("..\n.", ".._."), - ("test/", "test"), - ("/test", "test"), - ("foo/bar.iso", "foo/bar.iso"), - ("//foo//bar.iso", "foo/bar.iso"), - ("foo/lost-found/bar.iso", "foo/lost-found/bar.iso"), - ("/bar.iso/", "bar.iso"), + ("test/", "test"), + ("/test", "test"), + ("foo/bar.iso", "foo/bar.iso"), + ("//foo//bar.iso", "foo/bar.iso"), + ("foo/lost-found/bar.iso", "foo/lost-found/bar.iso"), + ("/bar.iso/", "bar.iso"), ]) def test_ok__valid_msd_image_name(arg: Any, retval: str) -> None: @@ -104,12 +96,13 @@ def test_ok__valid_msd_image_name(arg: Any, retval: str) -> None: "/ ..", ".. /", "/.. /", + ".test", "foo/../bar.iso", "foo/./foo.iso", "foo/lost+found/bar.iso", "../bar.iso", "/../bar.iso", - "foo/.__bar.iso", + "foo/.bar.iso", "", " ", None, diff --git a/testenv/tests/validators/test_os.py b/testenv/tests/validators/test_os.py index cabb8e51..9b229014 100644 --- a/testenv/tests/validators/test_os.py +++ b/testenv/tests/validators/test_os.py @@ -86,18 +86,10 @@ def test_fail__valid_abs_path__dir(arg: Any) -> None: ("dsl-4.11.rc1.iso", "dsl-4.11.rc1.iso"), ("systemrescuecd-x86-5.3.1.iso", "systemrescuecd-x86-5.3.1.iso"), ("ubuntu-16.04.5-desktop-i386.iso", "ubuntu-16.04.5-desktop-i386.iso"), - (" тест(){}[ \t].iso\t", "тест(){}[ _].iso"), - ("\n" + "x" * 1000, "x" * 255), - ("test", "test"), - ("test test [test] #test$", "test test [test] #test$"), - (".test", ".test"), - ("..test", "..test"), - ("..тест..", "..тест.."), - ("..те\\ст..", "..те\\ст.."), - (".....", "....."), - (".....txt", ".....txt"), - (" .. .", ".. ."), - ("..\n.", ".._."), + (" тест(){}[ \t].iso\t", "тест(){}[ _].iso"), + ("\n" + "x" * 1000, "x" * 255), + ("test", "test"), + ("test test [test] #test$", "test test [test] #test$"), ]) def test_ok__valid_printable_filename(arg: Any, retval: str) -> None: assert valid_printable_filename(arg) == retval @@ -110,6 +102,7 @@ def test_ok__valid_printable_filename(arg: Any, retval: str) -> None: "test/", "/test", "../test", + ".test", "./.", "../.", "./..", @@ -117,6 +110,7 @@ def test_ok__valid_printable_filename(arg: Any, retval: str) -> None: "/ ..", ".. /", "/.. /", + "lost+found", "", " ", None, -- cgit v1.2.3