[libvirt] [libvirt-sandbox PATCH 2/2] Sanitize domain name

Guido Günther posted 2 patches 8 years, 8 months ago
[libvirt] [libvirt-sandbox PATCH 2/2] Sanitize domain name
Posted by Guido Günther 8 years, 8 months ago
If one pastes from the output of virt-sansbox-image

  $ virt-sandbox-image list
  docker:/library/ubuntu?tag=17.04
  docker:/library/debian?tag=latest

verbatim

  $ virt-sandbox-image run -c qemu:///session docker:/library/debian?tag=latest

This fails like

  Unable to start sandbox: Failed to create domain: XML error: name library/debian:qbeilwxard cannot contain '/'

so don't allow invalid chars like '/' in domain names
---
 libvirt-sandbox/image/cli.py | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/libvirt-sandbox/image/cli.py b/libvirt-sandbox/image/cli.py
index 3a5ccfa..d5e624c 100644
--- a/libvirt-sandbox/image/cli.py
+++ b/libvirt-sandbox/image/cli.py
@@ -27,6 +27,7 @@ import hashlib
 import json
 import os
 import os.path
+import re
 import shutil
 import sys
 import urllib2
@@ -81,6 +82,10 @@ def prepare(args):
                            templatedir=get_template_dir(args),
                            connect=args.connect)
 
+def random_domain_name(tmpl):
+    randomid = ''.join(random.choice(string.lowercase) for i in range(10))
+    return re.sub('[^a-z0-9-]', '_', tmpl.path[1:], re.I) + ":" + randomid
+
 def run(args):
     if args.connect is not None:
         check_connect(args.connect)
@@ -95,8 +100,7 @@ def run(args):
 
     name = args.name
     if name is None:
-        randomid = ''.join(random.choice(string.lowercase) for i in range(10))
-        name = tmpl.path[1:] + ":" + randomid
+        name = random_domain_name(tmpl)
 
     diskfile = source.get_disk(template=tmpl,
                                templatedir=template_dir,
-- 
2.11.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [libvirt-sandbox PATCH 2/2] Sanitize domain name
Posted by Daniel P. Berrange 8 years, 4 months ago
On Wed, Jun 07, 2017 at 08:02:05AM +0200, Guido Günther wrote:
> If one pastes from the output of virt-sansbox-image
> 
>   $ virt-sandbox-image list
>   docker:/library/ubuntu?tag=17.04
>   docker:/library/debian?tag=latest
> 
> verbatim
> 
>   $ virt-sandbox-image run -c qemu:///session docker:/library/debian?tag=latest
> 
> This fails like
> 
>   Unable to start sandbox: Failed to create domain: XML error: name library/debian:qbeilwxard cannot contain '/'
> 
> so don't allow invalid chars like '/' in domain names
> ---
>  libvirt-sandbox/image/cli.py | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)

Reviewed-by: Daniel P. Berrange <berrange@redhat.com>


Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list