HTTP作为纯文本协议在安全和隐私方面具有天然的缺陷,苹果很久前就决定使用更具有安全性的 HTTPS 作为 iOS 移动应用作为的网络协议标准,不过苹果在今年的WWDC中一再指出,仅仅支持 HTTPS 是远远不够的,因为 HTTPS 会因为配置不当带来安全威胁。
App Transport Security (ATS) 是苹果在 iOS9 中为了保障网络安全通讯引进的组件, ATS 实际上封装了传输层安全(Transport Layer Security - TLS)。
- ATS只允许连接遵守 TLS 1.2 协议的服务器。
- ATS只允许连接使用 AES128+ 和 SHA2+ 强加密算法的服务器。
- ATS只允许连接遵守完全前向保密 (Perfect Forward Secrecy - PFS) 协议的服务器。
现状
到目前为止,绝大部分应用都倾向于手动禁止ATS (在文件Info.plist中设置Allowing Arbitrary Loads),虽然当前苹果还接受这种行为,但苹果已经发布声明从 2017年一月一日 开始将不再接受没有恰当理由却禁止 ATS 的应用。
苹果向来都是说一不二的,当你的应用因为 ATS 的理由被审核人员拒绝上架,并且不接受申诉理由,会不会觉得欲哭无泪呢?
如果你的团队还没有计划支持 ATS,现在行动还为时未晚,前端工程师们和后端团队(架构或基础设施团队)坐在一起讨论下如何在服务器端支持 TLS 。
诊断TLS连接问题
如果你的服务器已经支持 TLS, 在 OSX/macOS 上可以使用命令 nscurl 来检测配置和兼容性的问题。
这里使用 https://taobao.com 作为目标域名做个测试。
nscurl --ats-diagnostics https://taobao.com
下面是在终端中输出的结果:
Starting ATS Diagnostics
Configuring ATS Info.plist keys and displaying the result of HTTPS loads to https://taobao.com.
A test will "PASS" if URLSession:task:didCompleteWithError: returns a nil error.
Use '--verbose' to view the ATS dictionaries used and to display the error received in URLSession:task:didCompleteWithError:.
================================================================================
Default ATS Secure Connection
---
ATS Default Connection
Result : PASS
---
================================================================================
Allowing Arbitrary Loads
---
Allow All Loads
Result : PASS
---
================================================================================
Configuring TLS exceptions for taobao.com
---
TLSv1.2
Result : PASS
---
---
TLSv1.1
Result : PASS
---
---
TLSv1.0
Result : PASS
---
================================================================================
Configuring PFS exceptions for taobao.com
---
Disabling Perfect Forward Secrecy
Result : PASS
---
================================================================================
Configuring PFS exceptions and allowing insecure HTTP for taobao.com
---
Disabling Perfect Forward Secrecy and Allowing Insecure HTTP
Result : PASS
---
================================================================================
Configuring TLS exceptions with PFS disabled for taobao.com
---
TLSv1.2 with PFS disabled
Result : PASS
---
---
TLSv1.1 with PFS disabled
Result : PASS
---
---
TLSv1.0 with PFS disabled
Result : PASS
---
================================================================================
Configuring TLS exceptions with PFS disabled and insecure HTTP allowed for taobao.com
---
TLSv1.2 with PFS disabled and insecure HTTP allowed
Result : PASS
---
---
TLSv1.1 with PFS disabled and insecure HTTP allowed
Result : PASS
---
---
TLSv1.0 with PFS disabled and insecure HTTP allowed
Result : PASS
---
================================================================================
在上面输出结果中, 最重要的是下面的一部分:
Default ATS Secure Connection
---
ATS Default Connection
Result : PASS
---
上面的结果表明该域名已经支持 ATS 链接,也就是说可以建立满足苹果安全要求的连接,如果返回的结果是FAIL ,说明配置不当,需要继续调整。
如果需要调试一些由于采用了 ATS 后产生的问题,需要在Xcode 项目中设置CFNETWORK_DIAGNOSTICS 环境变量为1,这样就会打印出包含被访问的URL和ATS错误在内的NSURLSession错误信息,进而方便调试。
不能支持 TLS 怎么办?
在支持 ATS 的过程中,可能遇到各种各样的问题无法解决,如服务器只支持 HTTP, 或服务器只支持 TLS 1.0 版本等。ATS为此提供了相关配置:
- NSAllowArbitraryLoads - 直接禁止 ATS 安全策略, 允许 HTTP 和 非安全的 HTTPS 建立连接。
- NSExceptionAllowsInsecureHTTPLoads - 允许 HTTP 协议访问目标域名。
- NSExceptionMinimumTLSVersion - 指定支持的 TLS 的最低版本号。
- NSExceptionRequiresForwardSecrecy - 不要求支持完全前向保密。
- ** NSAllowsArbitraryLoadsForMedia** - 通过AVFoundation操作远程媒体流
- NSAllowsArbitraryLoadsInWebContent - 通过 WKWebView 访问和展示网站内容。
- NSThirdPartyExceptionAllowsInsecureHTTPLoads - 允许 HTTP 协议访问第三方服务器目标域名。
- NSThirdPartyExceptionRequiresForwardSecrecy - 不要求第三方服务器支持完全前向保密。
- NSThirdPartyExceptionMinimumTLSVersion - 指定第三方服务器支持的 TLS 的最低版本号。
例如,某第三方服务不支持 ATS 安全策略,需要在info.plist里做如下设置,声明不遵守 TLS 协议。
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>taobao.com</key>
<dict>
<key> NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
在上面例子中,把属性NSThirdPartyExceptionAllowsInsecureHTTPLoads设置成 true ,允许通过 HTTP协议访问 第三方taobao.com 主机。
记住如果不能说服第三方服务提供者及时提高安全基础服务,在未来提交应用审核的时候,需要向苹果澄清为何有部分服务不支持 ATS,至于应用审核人员是否接受你的解释,只能说:祝你好运!
更多
获取更多内容请关注微信公众号豆志昂扬:
- 直接添加公众号豆志昂扬;
- 微信扫描下图二维码;
![](https://img.haomeiwen.com/i1411204/028e2a1c709bbbac.jpg)
网友评论