博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How to prevent XXE attack ( XmlDocument in .net)
阅读量:4647 次
发布时间:2019-06-09

本文共 1778 字,大约阅读时间需要 5 分钟。

External resources are resolved using the XmlResolver provided via XmlDocument.XmlResolverproperty. If your XML documents **should not contain any external resource **(for example DTDs or schemas) simply set this property to null:

XmlDocument xmlDoc = new XmlDocument();xmlDoc.XmlResolver = null;xmlDoc.LoadXml(OurOutputXMLString);

  

If you want to filter where these URLs come from (for example to allow only certain domains) just derive your own class from XmlUrlResolver and override the ResolveUri() method. There you can check what the URL is and sanitize it (for example you can allow only URLs within your local network or from trusted sources).

For example:

class CustomUrlResovler : XmlUrlResolver{    public override Uri ResolveUri(Uri baseUri, string relativeUri)    {        Uri uri = new Uri(baseUri, relativeUri);        if (IsUnsafeHost(uri.Host))            return null;        return base.ResolveUri(baseUri, relativeUri);    }    private bool IsUnsafeHost(string host)    {        return false;     }}

  

Where IsUnsafeHost() is a custom function that check if the given host is allowed or not. See  here on SO for few ideas. Just return null from ResolveUri() to save your code from this kind of attacks. In case the URI is allowed you can simply return the default XmlUrlResolver.ResolveUri() implementation.

To use it:

XmlDocument xmlDoc = new XmlDocument();xmlDoc.XmlResolver = new CustomUrlResolver();xmlDoc.LoadXml(OurOutputXMLString);

  

For more details about how XML external resources are resolved just read  on MS Docs. If your code is more complex than this example then you should definitely read  for  property.

 

https://stackoverflow.com/questions/14230988/how-to-prevent-xxe-attack-xmldocument-in-net

转载于:https://www.cnblogs.com/jakl/p/9260562.html

你可能感兴趣的文章
BZOJ 1613: [Usaco2007 Jan]Running贝茜的晨练计划
查看>>
ubuntu 重启命令,ubuntu 重启网卡方法
查看>>
Linux的学习:
查看>>
JavaScript中的原型继承原理
查看>>
Python logger模块
查看>>
jquery控制css的display(控制元素的显示与隐藏)
查看>>
关于python做人工智能的一个网页(很牛逼)
查看>>
判断控件的CGRect是否重合,获取控件的最大XY值
查看>>
POJ-1128 Frame Stacking
查看>>
浏览器调试淘宝首页看到有趣的招聘信息
查看>>
ASP.NET Identity “角色-权限”管理 4
查看>>
[转][译]ASP.NET MVC 4 移动特性
查看>>
SOC CPU
查看>>
get_result --perl
查看>>
163镜像地址
查看>>
ehcache memcache redis 三大缓存男高音
查看>>
eclipse 快捷键Open Implementation 直接退出
查看>>
minix中管道文件和设备文件的读写
查看>>
JAXB - Annotations, Annotations for Enums: XmlEnum, XmlEnumValue
查看>>
context 插图
查看>>