From nobody Tue Feb 10 15:46:26 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1628685712; cv=none; d=zohomail.com; s=zohoarc; b=bcVHSAcXJi/mbT49lgRMWfaamE5WGgjC3hAPYhNIMkm7IfcxJhItULgZ7jLvge3p4D69GD5Nx/kZTrDSVAPAqlPhSqVUdIiv0zUEx01CsGpTAV34RFyZRDRwQz2uub0a4/sOSosE1HrY1dUiCKDzbb3eQgwzydGVxTpXGa41HMU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1628685712; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=jsUDFFIy5Ng+6D5oZS/e+76qBvA/OPvZTE2hv3CX8VA=; b=PPFZQr8syUHGHcs0NCurGc9N/V5ot0mh9nYhEktJF/UsR5g8Ay7BaDxHK8EM9aGWI3EsRaN0IJVkUiEXlWj5rrQZbLxX4sWmBGvMPXZNK+PgvkdFNW58S2+ZhmDsA6eAJ13hqkwubCKNDIrmEnBM4ES5/JWNDzelN1QuuirLIQk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1628685712246657.1495392414018; Wed, 11 Aug 2021 05:41:52 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-492-Ii56hSu_O0i0WDvwFFUfHA-1; Wed, 11 Aug 2021 08:41:45 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6588FC7401; Wed, 11 Aug 2021 12:41:40 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3FC5610013C1; Wed, 11 Aug 2021 12:41:40 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 0C5684A706; Wed, 11 Aug 2021 12:41:40 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 17BCbiNY020070 for ; Wed, 11 Aug 2021 08:37:44 -0400 Received: by smtp.corp.redhat.com (Postfix) id A10FE81F78; Wed, 11 Aug 2021 12:37:44 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.43.2.2]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2AA5181F76 for ; Wed, 11 Aug 2021 12:37:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1628685711; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=jsUDFFIy5Ng+6D5oZS/e+76qBvA/OPvZTE2hv3CX8VA=; b=aV3Nh5hk2MEorT2D4T8MZ6EHWoEgMkIIUFxVgSXtPWwGipELH0+4U4rSfahgrE7gzBUHAE XC9pxL05dXnZVcFAV8q9Aq5QlC+vuiXMYgM8yv6n/RWjPxs3pw001IVXjJ+EyKUXoB7+1d Kx8YO6bWdQXuhB5hpKSjRwLX2cQLOXs= X-MC-Unique: Ii56hSu_O0i0WDvwFFUfHA-1 From: Kristina Hanicova To: libvir-list@redhat.com Subject: [PATCH 2/5] util: add xml validation against schema in virXMLParseHelper() Date: Wed, 11 Aug 2021 14:36:45 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1628685713724100001 Content-Type: text/plain; charset="utf-8" We need this in order to validate XML against schema at one place, rather than have the same code for validation in different functions. I will add '--validate' option to more virsh commands soon and this makes it easier as virXMLParse() is called in every one I plan to change. Signed-off-by: Kristina Hanicova --- src/util/virxml.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/util/virxml.c b/src/util/virxml.c index 0220c5906f..b896a80460 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -33,6 +33,7 @@ #include "virfile.h" #include "virstring.h" #include "virutil.h" +#include "configmake.h" =20 #define VIR_FROM_THIS VIR_FROM_XML =20 @@ -1096,8 +1097,8 @@ catchXMLError(void *ctx, const char *msg G_GNUC_UNUSE= D, ...) * @url: URL of XML document for string parser * @rootelement: Optional name of the expected root element * @ctxt: optional pointer to populate with new context pointer - * @schemafile: unused - * @validate: unused + * @schemafile: optional name of the file the parsed XML will be validated= against + * @validate: if true, the XML will be validated against schema in @schema= file * * Parse XML document provided either as a file or a string. The function * guarantees that the XML document contains a root element. @@ -1114,8 +1115,8 @@ virXMLParseHelper(int domcode, const char *url, const char *rootelement, xmlXPathContextPtr *ctxt, - const char *schemafile G_GNUC_UNUSED, - bool validate G_GNUC_UNUSED) + const char *schemafile, + bool validate) { struct virParserData private; g_autoptr(xmlParserCtxt) pctxt =3D NULL; @@ -1181,6 +1182,15 @@ virXMLParseHelper(int domcode, (*ctxt)->node =3D rootnode; } =20 + if (validate && schemafile !=3D NULL) { + g_autofree char *schema =3D virFileFindResource(schemafile, + abs_top_srcdir "/doc= s/schemas", + PKGDATADIR "/schemas= "); + if (!schema || + (virXMLValidateAgainstSchema(schema, xml) < 0)) + return NULL; + } + return g_steal_pointer(&xml); } =20 --=20 2.31.1