如何在 c# 中修复 Fortify 扫描中的 Xpath 注入问题

3

HP Fortify扫描显示了一个XPath注入问题,如下所示:

 string repositoryID = Request.QueryString[repositoryIDKey];
 XmlDocument fullTreeviewMarkup = new SafeXmlDocument().LoadDocument(GetTreeViewMarkupFromSessionStore(sourceGuid));     
 XmlNode repositoryNode = fullTreeviewMarkup.SelectSingleNode( String.Format( "/root/TreeViewNode/TreeViewNode[@Value=\"{0}\"]", repositoryID ) );

如何解决Xpath注入问题。这里的repositoryID是System.GUID。如何验证repositoryID是否为GUID?

repositoryID是一个数字吗?如果是,它有多大/小? - Dave3of5
@Dave3of5 对不起,你是正确的,我没有完全阅读问题! - mybirthname
repositoryID 是 system.GUID。 - fortifysafeer
1
请查看此答案,其中详细介绍了如何在.NET中防止XPath XML注入。 https://dev59.com/r1jUa4cB1Zd3GeqPQVa7 - mybirthname
1个回答

1

既然你确认了repositoryID是System.Guid类型,那么我为你提供的修改如下:

Guid repositoryID;

if(Guid.TryParse(Request.QueryString[repositoryIDKey], out repositoryID))
{
    XmlDocument fullTreeviewMarkup = new SafeXmlDocument().LoadDocument(GetTreeViewMarkupFromSessionStore(sourceGuid));     
    XmlNode repositoryNode = fullTreeviewMarkup.SelectSingleNode( String.Format( "/root/TreeViewNode/TreeViewNode[@Value=\"{0}\"]", repositoryID ) );
}
else
{
    //Send Error
}

HP Fortify安全扫描仍然显示XPath注入问题?有没有办法避免Fortify扫描中的警告? - fortifysafeer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接