Skip to content

Commit 73cb7b0

Browse files
authored
Force https for jetpack sites (#16279)
2 parents 8c4f0f1 + 7adf54f commit 73cb7b0

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

Modules/Sources/Networking/Model/Site.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,19 @@ public enum SiteVisibility: Int, Codable, GeneratedFakeable {
333333
///
334334
public extension Site {
335335

336+
private var jetpackCanonicalURL: String {
337+
guard let originalURL = URL(string: url),
338+
originalURL.scheme?.lowercased() == "http"
339+
else {
340+
return url
341+
}
342+
343+
var components = URLComponents(url: originalURL, resolvingAgainstBaseURL: false)
344+
components?.scheme = "https"
345+
346+
return components?.string ?? url
347+
}
348+
336349
/// Returns the TimeZone using the gmtOffset
337350
///
338351
var siteTimezone: TimeZone {
@@ -347,7 +360,7 @@ public extension Site {
347360
}
348361

349362
func toJetpackSite() -> JetpackSite {
350-
JetpackSite(siteID: siteID, siteAddress: url, applicationPasswordAvailable: applicationPasswordAvailable)
363+
JetpackSite(siteID: siteID, siteAddress: jetpackCanonicalURL, applicationPasswordAvailable: applicationPasswordAvailable)
351364
}
352365
}
353366

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import Foundation
2+
import Testing
3+
@testable import Networking
4+
@testable import NetworkingCore
5+
6+
struct SiteTests {
7+
8+
@Test(arguments: [
9+
"http://awesomesite.com",
10+
"http://awesomesite.com/",
11+
"http://awesomesite.whatever.com",
12+
"HTTP://awesomesite.com",
13+
"http://192.168.0.12",
14+
"https://awesomesite.com"
15+
])
16+
private func forcingHttpsForJetpack(siteAddress: String) {
17+
let site = Site.defaultMock().copy(url: siteAddress)
18+
let jetpack = site.toJetpackSite()
19+
20+
#expect(jetpack.siteAddress.isHttpsScheme)
21+
}
22+
}
23+
24+
fileprivate extension String {
25+
var isHttpsScheme: Bool {
26+
return URL(string: self)?.scheme?.lowercased() == "https"
27+
}
28+
}

RELEASE-NOTES.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
23.6
55
-----
6-
6+
- [*] Handle sites configured with `http` siteAddress.
77

88
23.5
99
-----

0 commit comments

Comments
 (0)