Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion WebGoat/Content/XPathInjection.aspx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.XPath;
using System.Text.RegularExpressions;

namespace OWASP.WebGoat.NET
{
Expand All @@ -23,9 +24,15 @@ protected void Page_Load(object sender, EventArgs e)

private void FindSalesPerson(string state)
{
// Accept only alphabetic 2-letter abbreviations for state (e.g., "ny", "ca")
if (string.IsNullOrEmpty(state) || !System.Text.RegularExpressions.Regex.IsMatch(state, "^[a-zA-Z]{2}$"))
Copy link

Copilot AI Dec 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fully qualified namespace is unnecessary here since RegularExpressions is already imported at line 9. Use Regex.IsMatch directly instead of System.Text.RegularExpressions.Regex.IsMatch for better code readability and consistency with the import statement.

Suggested change
if (string.IsNullOrEmpty(state) || !System.Text.RegularExpressions.Regex.IsMatch(state, "^[a-zA-Z]{2}$"))
if (string.IsNullOrEmpty(state) || !Regex.IsMatch(state, "^[a-zA-Z]{2}$"))

Copilot uses AI. Check for mistakes.
{
// Invalid state input, do not proceed or handle accordingly
return;
Comment on lines +27 to +31
Copy link

Copilot AI Dec 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Silent failure on invalid input may lead to confusion for users and make debugging difficult. Consider logging the validation failure or returning an appropriate error message to inform the caller why no results were returned. This would help distinguish between "no salespeople found in that state" versus "invalid state code provided".

Copilot uses AI. Check for mistakes.
}
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(xml);
XmlNodeList list = xDoc.SelectNodes("//salesperson[state='" + state + "']");
XmlNodeList list = xDoc.SelectNodes("//salesperson[state='" + state.ToLower() + "']");
Copy link

Copilot AI Dec 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The XPath query is still vulnerable to injection attacks despite the input validation. While the regex check limits input to 2 letters, string concatenation remains unsafe for constructing XPath queries. Consider using XPath parameterization with XPathExpression and SetContext, or use LINQ to XML which provides safer query mechanisms. For example, you could use XmlDocument.SelectNodes with XPathNavigator and compile the expression, or migrate to LINQ to XML's safer querying approach.

Copilot uses AI. Check for mistakes.
if (list.Count > 0)
{

Expand Down