【问题标题】:NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802) on a subdomain?子域上的 NSURLSession/NSURLConnection HTTP 加载失败(kCFStreamErrorDomainSSL,-9802)?
【发布时间】:2016-01-18 11:20:24
【问题描述】:

我们的应用在我们的生产和开发环境中都运行良好,但在我们的暂存环境中,我们遇到了常见错误:

NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)

据我了解,当您尝试连接到非 https URL 时会发生此错误。

  • 我们将 ngrok 用于我们的本地隧道,它有一个 https 网址并且工作正常。
  • 对于生产,我们还使用https://ourdomain.com,它工作正常。
  • 对于暂存,我们使用 https://staging.ourdomain.com 并发生错误。

我已经看到许多解决方案说明这样做:

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

但我的雇主反对为了让 staging 工作而禁用 ATS 的想法,他也反对为我们的 staging URL 添加例外的想法。

任何想法为什么我们的暂存 URL 会导致此错误抛出或如何修复它?

ATS 诊断输出:

Starting ATS Diagnostics

Configuring ATS Info.plist keys and displaying the result of HTTPS loads to https://staging.domain.co.
A test will "PASS" if URLSession:task:didCompleteWithError: returns a nil error.
================================================================================

Default ATS Secure Connection
---
ATS Default Connection
ATS Dictionary:
{
}
Result : PASS
---

================================================================================

Allowing Arbitrary Loads

---
Allow All Loads
ATS Dictionary:
{
    NSAllowsArbitraryLoads = true;
}
Result : PASS
---

================================================================================

Configuring TLS exceptions for staging.domain.co

---
TLSv1.2
ATS Dictionary:
{
    NSExceptionDomains =     {
        "staging.domain.co" =         {
            NSExceptionMinimumTLSVersion = "TLSv1.2";
        };
    };
}
Result : PASS
---

---
TLSv1.1
ATS Dictionary:
{
    NSExceptionDomains =     {
        "staging.domain.co" =         {
            NSExceptionMinimumTLSVersion = "TLSv1.1";
        };
    };
}
Result : PASS
---

---
TLSv1.0
ATS Dictionary:
{
    NSExceptionDomains =     {
        "staging.domain.co" =         {
            NSExceptionMinimumTLSVersion = "TLSv1.0";
        };
    };
}
Result : PASS
---

================================================================================

Configuring PFS exceptions for staging.domain.co

---
Disabling Perfect Forward Secrecy
ATS Dictionary:
{
    NSExceptionDomains =     {
        "staging.domain.co" =         {
            NSExceptionRequiresForwardSecrecy = false;
        };
    };
}
Result : PASS
---

================================================================================

Configuring PFS exceptions and allowing insecure HTTP for staging.domain.co

---
Disabling Perfect Forward Secrecy and Allowing Insecure HTTP
ATS Dictionary:
{
    NSExceptionDomains =     {
        "staging.domain.co" =         {
            NSExceptionAllowsInsecureHTTPLoads = true;
            NSExceptionRequiresForwardSecrecy = false;
        };
    };
}
Result : PASS
---

================================================================================

Configuring TLS exceptions with PFS disabled for staging.domain.co

---
TLSv1.2 with PFS disabled
ATS Dictionary:
{
    NSExceptionDomains =     {
        "staging.domain.co" =         {
            NSExceptionMinimumTLSVersion = "TLSv1.2";
            NSExceptionRequiresForwardSecrecy = false;
        };
    };
}
Result : PASS
---

---
TLSv1.1 with PFS disabled
ATS Dictionary:
{
    NSExceptionDomains =     {
        "staging.domain.co" =         {
            NSExceptionMinimumTLSVersion = "TLSv1.1";
            NSExceptionRequiresForwardSecrecy = false;
        };
    };
}
Result : PASS
---

---
TLSv1.0 with PFS disabled
ATS Dictionary:
{
    NSExceptionDomains =     {
        "staging.domain.co" =         {
            NSExceptionMinimumTLSVersion = "TLSv1.0";
            NSExceptionRequiresForwardSecrecy = false;
        };
    };
}
Result : PASS
---

================================================================================

Configuring TLS exceptions with PFS disabled and insecure HTTP allowed for staging.domain.co

---
TLSv1.2 with PFS disabled and insecure HTTP allowed
ATS Dictionary:
{
    NSExceptionDomains =     {
        "staging.domain.co" =         {
            NSExceptionAllowsInsecureHTTPLoads = true;
            NSExceptionMinimumTLSVersion = "TLSv1.2";
            NSExceptionRequiresForwardSecrecy = false;
        };
    };
}
Result : PASS
---

---
TLSv1.1 with PFS disabled and insecure HTTP allowed
ATS Dictionary:
{
    NSExceptionDomains =     {
        "staging.domain.co" =         {
            NSExceptionAllowsInsecureHTTPLoads = true;
            NSExceptionMinimumTLSVersion = "TLSv1.1";
            NSExceptionRequiresForwardSecrecy = false;
        };
    };
}
Result : PASS
---

---
TLSv1.0 with PFS disabled and insecure HTTP allowed
ATS Dictionary:
{
    NSExceptionDomains =     {
        "staging.domain.co" =         {
            NSExceptionAllowsInsecureHTTPLoads = true;
            NSExceptionMinimumTLSVersion = "TLSv1.0";
            NSExceptionRequiresForwardSecrecy = false;
        };
    };
}
Result : PASS
---

================================================================================

【问题讨论】:

  • 您找到解决方案了吗?我还跑了nscurl,所有测试都通过了。

标签: ios swift ios9


【解决方案1】:

应用传输安全不仅仅是 HTTP 与 HTTPS。您需要使用正确配置的服务器+证书以避免 ATS 问题。来自 Apple 文档 [1]:

服务器必须至少支持传输层安全 (TLS) 协议版本 1.2。连接密码仅限于那些 提供前向保密。证书必须使用 SHA256 或 更大的签名哈希算法,具有 2048 位或更高位 RSA 密钥或 256 位或更大的椭圆曲线 (ECC) 密钥。无效的 证书会导致硬故障并且没有连接。

如果您使用的是 OS X 10.11(或更高版本),则可以使用 nscurl 进行故障排除。弹出打开终端并运行:

/usr/bin/nscurl --ats-diagnostics https://staging.ourdomain.com

[1]https://developer.apple.com/library/ios/technotes/App-Transport-Security-Technote/index.html

【讨论】:

  • 你能把 nscurl 的输出粘贴到我们看看吗?可能想在您使用时添加“--verbose”开关。
  • 嗯...第一个测试通过 - “默认 ATS 安全连接” - 应该表明您的服务器配置正确;你不应该遇到 ATS 问题。话虽如此,我想 nscurl 可能不是一个足够的测试。如果不是,我会感到惊讶,但这是可能的。您可以尝试 SSL Labs 站点来检查您的登台服务器吗?它的输出将更全面地说明您的服务器是如何配置的。 ssllabs.com/ssltest/index.html 另外,您是否尝试过 NSAllowArbitraryLoads 密钥,是否解决了问题?
  • 我没有尝试过任意加载。我已经向我的经理提出了该解决方案,但他不喜欢规避问题的想法,即使它处于阶段。
  • 我同意你的经理。但是,为了解决此问题,我建议您进行更改以打开 NSAllowArbitraryLoads just 以查看它是否有效。您可以在之后立即恢复更改。如果它有效,那么你有一个 ATS 问题。如果它不起作用,那么您没有 ATS 问题。 nscurl 的结果表明您没有 ATS 问题。
猜你喜欢
  • 2015-09-04
  • 2016-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-10
  • 2015-12-29
  • 1970-01-01
  • 2015-10-18
相关资源
最近更新 更多