第一部分:Locker浅析
Locker是Aura里为了让组件(Component)能够在独立的名前空间里运行,防止不安全代码运行,这里有如下限制
1:JavaScript的Strict模式被强制使用
变量必须用Var声明之后才能被使用呢
全局变量必须用Window关键字来声明
引用的第三方库也必须是Strict模式
2:Dom的使用只有在同一个名前空间里可以使用
以下是有效的获取DOM元素的方法
cmp.getElements()
cmp.find()
cmp.find("div1").getElement()
event.getSource().get("v.name")
<lightning:button name="myButton" label="Peek in DOM" aura:id="button1" οnclick="{!c.peekInDom}"/>
上边儿上的Button因为是lightning名前空间,所以不能使用cmp.find("button1").getElement()来找到他,只能使用.getSource()来判断。
3:封装安全
SecureAura => $A
SecureComponent => Component
SecureComponentRef => Component
SecureDocument => Document
SecureElement => SecureObject
SecureWindow => window
常用查看命令:cmp+"",cmp.find("button1")+"",window+"",$A+""
4:eval默认是返回本地的变量,
window.foo = 1;
function bar() {
var foo = 2;
return eval("foo");
}
要想用全局的变量的话要用Function
window.foo = 1;
function bar() {
var foo = 2;
return Function("foo","return foo")(foo);
}
JS小课堂:
eval() 函数计算或执行参数。
如果参数是表达式,则 eval() 计算表达式。如果参数是一个或多个 JavaScript 语句,则 eval() 执行这些语句。
5:Locker限制使用一些MIME Types 只支持以下种类
- application/octet-stream — Default value for binary files
- application/json — JSON format
- application/pdf — Portable Document Format (.pdf)
- video/ — All video/* mime types
- audio/ — All audio/* mime types
- image/ — All image/* mime types
- font/ — All font/* mime types
- text/plain — Text (.txt)
- text/markdown — Markdown (.md)
- application/zip — Zip archive (.zip)
- application/x-bzip — Bzip archive (.bz)
- application/x-rar-compressed — RAR archive (.rar)
- application/x-tar — Tape archive (.tar)
6:支持的Script API
https://MyDomainName.lightning.force.com/auradocs/reference.app
这个已经不用了。
现在用这个 Salesforce DevelopersSalesforce Developer Websitehttps://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/ref_jsapi_intro.htm7:Locker影响哪些功能?
这么说吧,只要用Lightning功能的都受到影响。
8:Locker工具
API Viewer(查看哪些API可以使用)
Locker Console(查看Javacript代码是否可以在Locker里使用)
9:选择合适Locker API给自己的环境
Locker API和Compnent的API是区分开的可以设置成不一样的。
Session Settings=>Use security enhancements in API version=>Locker API
10:把某一个组件的locker功能关掉,
把Salesforce Api Version 设置成39.0或者以下,但是注意如果你的代码里使用了高版本的组件r如:lightning:map的话,你是不能把salesforce Api设置成44.0以下的。
11:不要混用Component的API
如果混用的话,有些component适用了locker,有些不适用的话,你的页面会很难Debug。(其实跑起来也没啥大事儿,毕竟运行是最低要求,连运行都做不到何谈扩展和维护)
举个例子,父组件适用了Locker(40.0以上)而子组件没有适用(39.0及以下),如果父组件里设置了个window._count变量,因为是父组件的变量并且Locker有效了,所以变量的范围是该组件名前空间的Window范围是有效的,但是因为子组件没有启用locker所以Window的范围不同,导致子组件里看不到父组件的变量声明,
父包括子的时候如果父是Locker而子不是的话,父的功能是不能影响子的。
所以最好统一API。
12:Locker需要在支持的浏览器里才会适用,如果不支持的话是无效的,
原理是Locker是需要Javascript 启用Strict模式,所以如果不支持Map,Proxy的话Locker是无效的。
第二部分:内容安全策略概览(CSP)
所有的JavaScript必须倒入到静态资源里才能被引用,script-src 'self'
所有资源必须本地化,包括ont-src, img-src, media-src, frame-src, style-src, and connect-src directives are set to 'self'
资源必须用HTTPS来引用
Inline JavaScript 不被允许,例如:<button οnclick="doSomething()"></button>
在Console里可以看到违反CSP的信息,例如
Refused to load the script 'https://externaljs.docsample.com/externalLib.js' because it violates the following Content Security Policy directive: ...
严格的CSP的启用和Locker无关,不管Locker是否生效,CSP在Winter19之后都是默认生效的。
“Enable Stricter Content Security Policy
严格的CSP会影响到
- Lightning Experience
- Salesforce app
- Standalone apps that you create (for example, myApp.app)
严格的CSP不会影响到
- Salesforce Classic
- Any apps for Salesforce Classic, such as Salesforce Console in Salesforce Classic
- Experience Builder sites, which have their own CSP settings
- Lightning Out, which allows you to run Lightning components in a container outside of Lightning apps, such as Lightning components in Visualforce and Salesforce Tabs + Visualforce sites. The container defines the CSP rules
总结来说是Lighting里创建的东西适用,Classic的不适用