How to prevent XPath/XML injection in .NET

前端 未结 4 1556
醉酒成梦
醉酒成梦 2020-12-01 19:31

How can I prevent XPATH injection in the .NET Framework?

We were previously using string concatenation to build XPATH statements, but found that end users could exec

相关标签:
4条回答
  • 2020-12-01 20:05

    The main idea in preventing an XPath injection is to pre-compile the XPath expression you want to use and to allow variables (parameters) in it, which during the evaluation process will be substituted by user-entered values.

    In .NET:

    1. Have your XPath expresion pre-compiled with XPathExpression.Compile().

    2. Use the XPathExpression.SetContext() Method to specify as context an XsltContext object that resolves some specific variables to the user-entered values.

    You can read more about how to evaluate an XPath expression that contains variables here.

    This text contains good and complete examples.

    0 讨论(0)
  • 2020-12-01 20:09

    Parameterized XPath is possible if you use Saxon as your XPath processor.

    0 讨论(0)
  • 2020-12-01 20:16

    Strongly typed parameters are available if you use a full-blown XsltTransform.

    0 讨论(0)
  • 2020-12-01 20:21

    Instead of strongly typed parameters you could decrease the options for a user. Why give them full control if you do not want that?

    Provide the user with a couple of option to select from and then create the query.

    Allowing the user to enter any string is asking for trouble or a lot of work.

    0 讨论(0)
提交回复
热议问题