xml外部实体泄露信息
注意:本技巧假定您对文档类型定义(DTD)有基本的了解,并具有检查处理过的XML文件的XML解析器。 验证不是必需的; Internet Explorer 5.0或更高版本就足够了。
文件
在本技巧中,我从邮件合并系统中提取了一封示例信,并在其底部添加了标准免责声明。 最终,我希望从中央位置(例如服务器)控制免责声明,以便可以从文档本身进行外部控制。
基本文档包含该字母本身:
清单1.文档
<?xml version="1.0"?>
<letter>
<salutation>
Dear <customerName>Valued Customer</customerName>,
</salutation>
<body>
<paragraph>
Thank you for your recent letter/phone call/email
telling us of your problem/concern/question. We
want you to know that we take all issues seriously
and are working to quickly resolve yours.
</paragraph>
<paragraph>
We pride ourselves on our personalized service
and will be in contact with you shortly.
</paragraph>
</body>
<closing>Sincerely,</closing>
<signature>Customer Service Employee 334992</signature>
</letter>
添加一般实体
通用实体很像应用程序中的变量。 您可以为它们设置一个值,在需要它们的地方引用它们,然后将它们的值插入该位置。 此过程称为扩展实体。 实体必须声明为文档类型定义(DTD)的一部分。 对于集中控制的信息,它们通常将是外部DTD子集的一部分,但是为了方便起见,此示例显示了将实体声明为内部子集的一部分。
清单2.声明实体
<?xml version="1.0"?>
<!DOCTYPE letter [
<!ENTITY disclaimer "DISCLAIMER GOES HERE">
]>
<letter>
<salutation>
Dear <customerName>Valued Customer</customerName>,
</salutation>
...
清单显示了一个通用实体,名称为disclaimer
,值为DISCLAIMER GOES HERE
。 接下来,您从文档中调用它。
包括一般实体
将通用实体插入到XML文档的主体中,方法是在其前面加上与号(&),然后在其后加上分号(;),如下所示:
清单3.引用通用实体
<?xml version="1.0"?>
<!DOCTYPE letter [
<!ENTITY disclaimer "DISCLAIMER GOES HERE">
]>
<letter>
<salutation>
Dear <customerName>Valued Customer</customerName>,
</salutation>
<body>
...
</body>
<closing>Sincerely,</closing>
<signature>Customer Service Employee 334992</signature>
&disclaimer;
</letter>
当符合XML 1.0的处理器读取文档本身时,实体将被扩展,其内容将替换引用,因此整个文档显示为:
清单4.具有扩展实体的文档
<letter>
<salutation>
Dear
<customerName>Valued Customer</customerName>
,
</salutation>
<body>
<paragraph>Thank you for your recent letter/phone call/email telling
us of your problem/concern/question. We want you to know that we take
all issues seriously and are working to quickly resolve yours.</paragraph>
<paragraph>We pride ourselves on our personalized service and will
be in contact with you shortly.</paragraph>
</body>
<closing>Sincerely,</closing>
<signature>Customer Service Employee 334992</signature>
DISCLAIMER GOES HERE
</letter>
但是,没有规则规定必须在DTD中直接定义扩展值。
外部实体
到目前为止, disclaimer
实体是内部实体; 该文档可以独立存在,并且不依赖于其他文档。 现在,我将向您展示如何创建外部实体。
外部实体(如DTD本身)可以引用SYSTEM
或PUBLIC
标识符。 本技巧仅涉及SYSTEM
,但是原理是相同的。 下面是对与当前文档位于同一目录中的文件的引用。
清单5.引用本地文件
<?xml version="1.0"?>
<!DOCTYPE letter [
<!ENTITY disclaimer SYSTEM "./disclaimer.xml">
]>
<letter>
<salutation>
Dear <customerName>Valued Customer</customerName>,
</salutation>
...
实体引用保持不变,但是现在展开时,它使用文件的内容,因此文档变为:
清单6.展开的本地文件
<letter>
<salutation>
Dear
<customerName>Valued Customer</customerName>
,
</salutation>
<body>
<paragraph>Thank you for your recent letter/phone call/email telling
us of your problem/concern/question. We want you to know that we take
all issues seriously and are working to quickly resolve yours.</paragraph>
<paragraph>We pride ourselves on our personalized service and will
be in contact with you shortly.</paragraph>
</body>
<closing>Sincerely,</closing>
<signature>Customer Service Employee 334992</signature>
<disclaimer>This is a test. It is only a test. If this were a
real customer emergency, it would, of course, be handled with the
utmost sincerity. Thank you for your attention.</disclaimer>
</letter>
包括远程信息
没有规则指示系统标识符必须引用本地资源。 SYSTEM
标识符可以轻松指向远程文件:
清单7.引用远程信息
<?xml version="1.0"?>
<!DOCTYPE letter [
<!ENTITY disclaimer SYSTEM "http://www.vanguardreport.com/disclaimer.xml">
]>
<letter>
<salutation>
Dear <customerName>Valued Customer</customerName>,
</salutation>
...
处理文件后,远程文件的信息将扩展到文档中。
摘要
本技巧演示了通过通用实体将外部信息包含在XML文档中的能力。 需要注意的是,可以通过这种方式包含任何信息:即使扩展实体,文档也必须格式正确。
否则,此方法非常适合诸如披露,版权声明或必须在一系列文档中频繁重复的其他声明之类的信息,或者适合必须由中央资源(例如服务器)控制但仍可访问的信息作为XML文档的一部分。
翻译自: https://www.ibm.com/developerworks/xml/library/x-tipgentity/index.html
xml外部实体泄露信息