Skip to content

Commit 60b30c1

Browse files
committed
#2177 add xmlutlities
1 parent 787493a commit 60b30c1

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

Components/XML/BExIS.Xml.Helpers/XmlMetadataWriter.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Diagnostics;
7+
using System.IO;
78
using System.Linq;
89
using System.Xml;
910
using System.Xml.Linq;
@@ -777,6 +778,11 @@ public XDocument AddAttribute(XDocument metadataXml, BaseUsage attributeUsage, i
777778
{
778779
_tempXDoc = metadataXml;
779780

781+
if (!!XmlUtility.IsSafeXPath(parentXPath))
782+
{
783+
throw new ArgumentException("Potentially unsafe xpath expression.");
784+
}
785+
780786
/*
781787
* In the xml the structure is everytime usage/type
782788
*

Components/XML/BExIS.Xml.Helpers/XmlUtility.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ namespace BExIS.Xml.Helpers
1313
{
1414
public class XmlUtility
1515
{
16+
// Validate that xpath is a simple, safe path: only allows slashes and node names (alphanumeric/underscore)
17+
public static bool IsSafeXPath(string xpath)
18+
{
19+
// Prevent empty, null XPaths
20+
if (string.IsNullOrEmpty(xpath)) return false;
21+
22+
// Only allow XPaths like /a/b/c or /a[1]/b matches, no function calls, no quotes, etc.
23+
// You can adjust the pattern according to the actual requirements.
24+
// This only allows: /node1/node2/...
25+
var safePattern = @"^(/[a-zA-Z_][\w\-]*(\[\d+\])?)*$";
26+
return System.Text.RegularExpressions.Regex.IsMatch(xpath, safePattern);
27+
}
28+
1629
public static string GetXPathToNode(XmlNode node)
1730
{
1831
string xPath = "";

0 commit comments

Comments
 (0)