mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-02 17:06:09 +00:00
The new pool dialog took /, /etc, /var/lib/dpkg or /home as a directory pool's path, or as a share's mount point, and a path inside or around another pool's. A pool on /etc offers the host's own files as volumes, which one Delete removes; a share mounted there hides them; and a file under two pools is listed, and deleted, through both. These paths are now refused: the system directories (/var only within /var/lib/libvirt, outside libvirt's own state), /home, /mnt, /media, /opt and /srv themselves (a directory in them is fine), and a path is compared once "..", doubled and trailing slashes are resolved. A symbolic link on the host is not resolved.
107 lines
5.3 KiB
Python
107 lines
5.3 KiB
Python
"""The storage pool target and source checks run in linear time and take
|
|
paths of letters, digits and "/", "_", ".", ":" and "-" (not first, not right
|
|
after "/"): both values go into the pool XML."""
|
|
|
|
import time
|
|
from types import SimpleNamespace
|
|
|
|
from django.template.loader import render_to_string
|
|
from django.test import SimpleTestCase
|
|
from lxml import html
|
|
from storages.forms import AddStgPool
|
|
|
|
|
|
def target_error(target):
|
|
form = AddStgPool({"name": "pool1", "stg_type": "dir", "target": target})
|
|
form.is_valid()
|
|
return "target" in form.errors
|
|
|
|
|
|
def source_error(source, stg_type="logical"):
|
|
form = AddStgPool({"name": "pool1", "stg_type": stg_type, "source": source, "target": "/mnt/pool1"})
|
|
form.is_valid()
|
|
return "source" in form.errors
|
|
|
|
|
|
class PoolTargetTests(SimpleTestCase):
|
|
def test_accepted_and_refused_targets(self):
|
|
for target, refused in (
|
|
("/var/lib/libvirt/images", False),
|
|
("/srv/iso-pool", False),
|
|
("/a//b", False),
|
|
("a-b.c_d/e", False),
|
|
("/a/-b", True), # a dash right after a slash
|
|
("-x", True),
|
|
("/a b", True),
|
|
("'/a", True), # any first character but "-" passed
|
|
("</a", True),
|
|
):
|
|
with self.subTest(target=target):
|
|
self.assertEqual(target_error(target), refused)
|
|
|
|
def test_a_system_directory_is_refused(self):
|
|
# a pool there offers the host's own files as volumes to delete
|
|
for target, refused in (("/", True), ("/etc", True), ("//etc/", True), ("/srv/../etc", True), ("/usr/lib", True),
|
|
("/var", True), ("/var/lib", True), ("/var/lib/dpkg", True), ("/var/lib/libvirt", True),
|
|
("/var/lib/libvirt/qemu", True), ("/tmp/x", True), ("/home", True), ("/srv", True),
|
|
("/mnt", True), ("/etc-images", False), ("/srv/etc", False), ("/home/vms", False),
|
|
("/mnt/share", False), ("/var/lib/libvirt/images", False),
|
|
("/var/lib/libvirt/images/iso", False), ("/var/lib/libvirt/isos", False)):
|
|
with self.subTest(target=target):
|
|
self.assertEqual(target_error(target), refused)
|
|
|
|
def test_a_path_inside_or_around_another_pools_is_refused(self):
|
|
# a file in both would be listed, and deleted, through either pool
|
|
pools = [{"name": "default", "type": "dir", "path": "/srv/vms/images"},
|
|
{"name": "ceph", "type": "rbd", "path": "rbd-pool"}]
|
|
for target, error in (
|
|
("/srv/vms/images/", "Pool default uses this path"),
|
|
("/srv/vms/images/iso", "Pool default uses /srv/vms/images: one pool's directory cannot hold another's"),
|
|
("/srv/vms", "Pool default uses /srv/vms/images: one pool's directory cannot hold another's"),
|
|
("/srv/vms/images-2", None),
|
|
("rbd-pool", None),
|
|
):
|
|
with self.subTest(target=target):
|
|
form = AddStgPool({"name": "pool1", "stg_type": "dir", "target": target}, pools=pools)
|
|
form.is_valid()
|
|
self.assertEqual(form.errors.get("target"), [error] if error else None)
|
|
|
|
def test_accepted_and_refused_sources(self):
|
|
for source, refused in (
|
|
("/dev/vdb", False),
|
|
("/dev/mapper/vg0-data", False),
|
|
("/dev/disk/by-id/wwn-0x5000c500a1b2c3d4", False),
|
|
("/dev/disk/by-path/pci-0000:00:1f.2-ata-1", False),
|
|
("/export/vm_images.v2", False),
|
|
("", True),
|
|
("-x", True), # would read as an option
|
|
("/a/-b", True),
|
|
("/a b", True),
|
|
("/a'b", True), # the value goes into the pool XML
|
|
("/a<b", True),
|
|
("'/a", True),
|
|
):
|
|
for stg_type in ("logical", "netfs"):
|
|
with self.subTest(source=source, stg_type=stg_type):
|
|
self.assertEqual(source_error(source, stg_type), refused)
|
|
|
|
def test_a_crafted_target_is_checked_at_once(self):
|
|
# the previous expression backtracked exponentially: 22 dashes took over a second
|
|
start = time.monotonic()
|
|
self.assertTrue(target_error("," + "-" * 98 + "!"))
|
|
self.assertLess(time.monotonic() - start, 1)
|
|
|
|
def test_the_create_form_leaves_the_path_check_to_the_server(self):
|
|
# a stricter pattern or a shorter maxlength in the page refuses paths the form accepts
|
|
superuser = SimpleNamespace(user=SimpleNamespace(is_superuser=True))
|
|
ceph = [{"uuid": "00000000-0000-0000-0000-000000000001", "usage": "client.libvirt"}]
|
|
page = html.fromstring(render_to_string("create_stg_block.html", {"request": superuser, "ceph_secrets": ceph}))
|
|
fields = page.xpath("//input[@name='target' or @name='source']")
|
|
self.assertEqual(len(fields), 5) # directory, LVM device, share, its mount point, ISO directory
|
|
for field in fields:
|
|
with self.subTest(id=field.get("id")):
|
|
self.assertIsNone(field.get("pattern"))
|
|
self.assertGreaterEqual(int(field.get("maxlength", 100)), 100)
|
|
for name in ("netfs_host", "ceph_host"): # a host name is up to 253 characters
|
|
with self.subTest(name=name):
|
|
self.assertGreaterEqual(int(page.xpath(f"//input[@name='{name}']/@maxlength")[0]), 253)
|