JavaScript异常:安全设置不允许在此样式表内执行脚本代码

官方地址:XSLT Security

官方原文如下:

The following sections describe important XSLT security issues. They are not listed in any significant order. You should familiarize yourself with all the issues discussed, and address them in your applications.

Denial of Service Attacks

Untrusted style sheets are those that come from an untrustworthy domain. There is no way to eliminate denial of service (DoS) attacks when processing untrusted style sheets or untrusted documents without removing necessary functionality. If denial of service is a concern, do not accept untrusted style sheets or untrusted documents for transformation.

Cross-Site Attacks

It is not safe to compile and execute an untrusted style sheet within a trusted page (such as a page from your local hard drive). The style sheet may contain the document() function or xsl:include / xsl:import statements, which are capable of loading trusted files and sending them back to the untrusted domain.

XSLT Scripts Are Prohibited by Default

The DOM supports XSLT transformations via calls to the transformNode method and transformNodeToObject method. XSLT supports scripting inside style sheets using the <msxsl:script> element. This allows custom functions to be used in an XSLT transformation. If you require scripting in your XSLT transformations, you can enable the feature by setting theAllowXsltScript Property to true. Note that the default value for AllowXsltScript Property is true for MSXML 3.0 and false for MSXML 6.0.

To allow XSLT scripting (JScript):

doc.setProperty("AllowXsltScript", true);

To disallow XSLT scripting:

doc.setProperty("AllowXsltScript", false);

If you use MSXML 6.0 via script in Internet Explorer to execute transformations, when the AllowXsltScript property is set to false scripting is disabled, no matter what the Internet Explorer settings are.

Internet Explorer 8.0 and earlier versions uses MSXML 3.0 by default, so when using the MIME viewer to transform scripts, the Internet Explorer security settings are used.

The following example demonstrates how to set the Internet Explorer security settings to disallow running scripts.

NoteNote

To run the following example, you must have a network share where you can copy your files.

  1. Create an XML document and copy the content of the following XML code into this document. Copy the XML document that you created to some directory on the network share.

    XML
    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="script.xsl" ?>
    <customers>
       <customer>
          <name>John Smith</name>
          <address>123 Elm St.</address>
          <phone>(123) 456-7890</phone>
       </customer>
       <customer>
          <name>Mary Jones</name>
          <address>456 Oak Ave.</address>
          <phone>(156) 789-0123</phone>
       </customer>
    </customers>
    
    
    
  2. Create an XSL document and copy the content of the following XSL code into this document. Copy the XSL document that you created to some directory on the network share.

    XML
    <?xml version='1.0'?>
    <xsl:stylesheet version="1.0"
          xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
          xmlns:msxsl="urn:schemas-microsoft-com:xslt"
          xmlns:user="http://mycompany.com/mynamespace">
    
    <msxsl:script language="JScript" implements-prefix="user">
       function xml(nodelist) {
          return nodelist.nextNode().xml;
       }
    </msxsl:script>
    
    <xsl:template match="/">
       <xsl:value-of select="user:xml(.)"/>
    </xsl:template>
    
    </xsl:stylesheet>
    
    
    
  3. Open Internet Explorer. Make sure you have only one instance of Internet Explorer running. From the Toolsmenu, select Internet Options. From the Internet Options dialog box, select the Security tab and click Local intranet. Click the Custom Level button; the Security Settings dialog box will appear. Scroll down to theScripting section, and under Active scripting select Disable. This will disable running scripts for files located on the network.

  4. In the Internet Explorer Address bar, type the path to the XML file that you created and press Enter. You should see the following error message: "Security settings do not allow the execution of script code within this stylesheet."

The XSLT document Function Is Disallowed by Default

The DOM supports XSLT transformations via calls to the transformNode and transformNodeToObject methods. The XSLT document function provides a way to retrieve other XML resources from within the XSLT style sheet beyond the initial data provided by the input stream. In MSXML 6.0 this feature is disabled by default. If you must use thedocument function in your XSLT transformations, you can enable the feature by setting the AllowDocumentFunction property to true.

The following is the JScript code to allow the document function:

doc.setProperty("AllowDocumentFunction", true);

To disallow the document function:

doc.setProperty("AllowDocumentFunction", false);

If you enable the document function, you should be aware that the document function runs with the same security settings as the style sheet. If your style sheet is running in a trusted security context, then all files loaded using thedocument function will run in the same security context. For example, if scripts are allowed in the main style sheet, they will be allowed in all the included and imported files. You should not load untrusted documents via the documentfunction.

Loading External Files Is Prohibited by Default

In MSXML 6.0 external files loaded via xsl:include or xsl:import are not processed by default – they must be explicitly enabled by the developer.

If you are using MSXML 6.0 and all of your XSLT style sheets and XML documents come from a secure site, you can allow external schemas by setting the resolveExternals property to true.

To allow external files:

doc.resolveExternals = true;

To disallow external files:

doc.resolveExternals = false;

Error Messages May Reveal Data

Certain types of threats require that you program your application in certain ways. For example, the description of an error may reveal data such as the data being transformed. Errors may also reveal file names. Error messages should not be exposed to callers that are not trusted. You should catch all errors and report errors with your own custom error messages.

总结

Internet Explorer 9 浏览器需要设置 ThreadSafeDocument ActiveX控件对象属性为 AllowXsltScript 属性为true,示例代码:

				
var xslDoc = new ActiveXObject("MSXML2.FreeThreadedDOMDocument.6.0");
xslDoc.setProperty("AllowXsltScript", true);// 设置Script属性
this.xslDom.save(xslDoc);
this.xslDom = null;
var xslTemplate = new ActiveXObject("MSXML2.XSLTemplate.6.0");
xslTemplate.stylesheet=xslDoc;
var xslProcessor=xslTemplate.createProcessor();
if(this.args!=null) {
	for(var name in this.args) {
		xslProcessor.addParameter(name,this.args[name]);
	}
}
xslProcessor.input=this.xmlDom;
xslProcessor.transform();
result = xslProcessor.output;

xslDoc = null;
xslTemplate = null;
xslProcessor = null;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值