454

所以,昨晚发布的 iOS 新的 beta SDK 有“App Transport Security”,它鼓励开发者使用 https 而不是 http。原则上,这是一个好主意,我已经在我们的暂存/生产环境中使用了 https。但是,当 iOS 应用程序连接到我在笔记本电脑上运行的 Web 服务时,我没有在本地开发环境中设置 https。

从今天早上玩了一下,似乎 URL 加载系统会,即使你给它一个 http URL,也会决定使用 https。有谁知道如何禁用这种行为——即使只是针对特定的 URL?

4

8 回答 8

719

有关完整详细信息,请参阅 Apple 的Info.plist 参考(感谢@gnasher729)。

您可以在 Info.plist 中为特定域添加例外:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>testdomain.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <true/>
            <key>NSExceptionMinimumTLSVersion</key>
            <string>TLSv1.2</string>
            <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
            <false/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <true/>
            <key>NSThirdPartyExceptionMinimumTLSVersion</key>
            <string>TLSv1.2</string>
            <key>NSRequiresCertificateTransparency</key>
            <false/>
        </dict>
    </dict>
</dict>

每个例外域的所有键都是可选的。演讲者没有详细说明任何键,但我认为它们都相当明显。

(来源:WWDC 2015 session 703,“Privacy and Your App”</a>,30:18)

如果您的应用有充分的理由这样做,您还可以使用单个密钥忽略所有应用传输安全限制:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

如果您的应用没有充分的理由,您可能会面临被拒绝的风险:

将 NSAllowsArbitraryLoads 设置为 true 将允许它工作,但 Apple 非常清楚,他们打算拒绝没有特定原因使用此标志的应用程序。我能想到的使用 NSAllowsArbitraryLoads 的主要原因是用户创建的内容(链接共享、自定义 Web 浏览器等)。在这种情况下,Apple 仍然希望您包含对您控制的 URL 强制执行 ATS 的异常。

如果您确实需要访问未通过 TLS 1.2 提供的特定 URL,则需要为这些域编写特定的例外,而不是使用 NSAllowsArbitraryLoads 设置为 yes。您可以在 NSURLSesssion WWDC 会话中找到更多信息。

请谨慎分享 NSAllowsArbitraryLoads 解决方案。这不是 Apple 推荐的修复程序。

kcharwood(感谢@marco-tolman)

于 2015-06-09T12:57:33.047 回答
113

作为接受的答案提供了所需的信息,有关使用和禁用应用程序传输安全性的更多信息,可以在此找到更多信息

对于 Per-Domain Exceptions 将这些添加到Info.plist

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSExceptionDomains</key>
  <dict>
    <key>yourserver.com</key>
    <dict>
      <!--Include to allow subdomains-->
      <key>NSIncludesSubdomains</key>
      <true/>
      <!--Include to allow HTTP requests-->
      <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
      <true/>
      <!--Include to specify minimum TLS version-->
      <key>NSTemporaryExceptionMinimumTLSVersion</key>
      <string>TLSv1.1</string>
    </dict>
  </dict>
</dict>

但是,如果我不知道我需要使用的所有不安全域怎么办?Info.plist 中使用以下键

<key>NSAppTransportSecurity</key>
<dict>
  <!--Include to allow all connections (DANGER)-->
  <key>NSAllowsArbitraryLoads</key>
      <true/>
</dict>

有关更多详细信息,您可以从此链接获取。

于 2015-06-10T08:54:51.007 回答
60

跟着这个

我已经通过在 info.plist 中添加一些键来解决它。我遵循的步骤是:

  1. 打开我的项目info.plist文件

  2. NSAppTransportSecurity添加了一个名为Dictionary.

  3. 添加了一个名为NSAllowsArbitraryLoadsas的子Boolean键并将其值设置YES为如下图所示。 在此处输入图像描述

清理项目,现在一切都像以前一样运行良好。

参考链接

于 2015-09-28T07:37:47.550 回答
35

如果您只想禁用本地开发服务器的应用程序传输策略,那么以下解决方案效果很好。当您无法设置 HTTPS 或设置 HTTPS 不切实际时(例如,使用 Google App Engine 开发服务器时),它很有用。

正如其他人所说,绝对不应该为生产应用程序关闭 ATP。

1) 使用不同的 plist 进行调试

复制您的 Plist 文件和 NSAllowsArbitraryLoads。使用此 Plist 进行调试。

XCode 调试

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

2)排除本地服务器

或者,您可以使用单个 plist 文件并排除特定服务器。但是,您似乎无法排除 IP 4 地址,因此您可能需要改用服务器名称(在系统偏好设置 -> 共享中找到,或在本地 DNS 中配置)。

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>server.local</key>
        <dict/>
        <key>NSExceptionAllowsInsecureHTTPLoads</key>
        <true/>
    </dict>
</dict>
于 2015-09-24T14:40:37.390 回答
31

我已解决为 plist 文件。

  1. 添加一个 NSAppTransportSecurity : 字典。
  2. 将名为“NSAllowsArbitraryLoads”的子项添加为布尔值:是

在此处输入图像描述

于 2015-10-08T05:54:33.540 回答
22

上面的配置对我不起作用。我尝试了很多键组合,这一个工作正常:

在此处输入图像描述

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>mydomain.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
    </dict>
</dict>
于 2015-09-25T09:51:23.883 回答
18

编译@adurdin 和@User 给出的答案

将以下内容添加到您的 info.plist 并localhost.com使用您相应的域名进行更改,您也可以添加多个域:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSExceptionDomains</key>
    <dict>
        <key>localhost.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <false/>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <false/>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <true/>
            <key>NSExceptionMinimumTLSVersion</key>
            <string>TLSv1.2</string>
            <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
            <false/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <true/>
            <key>NSThirdPartyExceptionMinimumTLSVersion</key>
            <string>TLSv1.2</string>
            <key>NSRequiresCertificateTransparency</key>
            <false/>
        </dict>
    </dict>
</dict>
</plist>

您的 info.plist 必须如下所示:

在此处输入图像描述

于 2015-08-10T13:44:33.000 回答
16

这对我有用:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <false/>
    <key>NSExceptionDomains</key>
    <dict>
        <key><!-- your_remote_server.com / localhost --></key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <true/>
        </dict>
    <!-- add more domain here -->
    </dict>
</dict>

我只是想添加这个来帮助别人并节省一些时间:

如果您正在使用:CFStreamCreatePairWithSocketToHost。确保您host的内容与您的内容相同,.plist或者如果您有单独的套接字域,只需将其添加到那里。

CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)/*from .plist*/, (unsigned int)port, &readStream, &writeStream);

希望这会有所帮助。干杯。:)

于 2015-10-19T09:08:00.453 回答