summaryrefslogtreecommitdiff
path: root/Hutch/Networking/SRHTClient.swift
diff options
context:
space:
mode:
authorChristian Cleberg <[email protected]>2026-03-18 19:16:02 -0500
committerChristian Cleberg <[email protected]>2026-03-18 19:16:02 -0500
commit7fba8922e1540741240555560cc9504342bdb091 (patch)
tree8f87280f5b50dcf2199de2e8305e020276d51421 /Hutch/Networking/SRHTClient.swift
parent1745311950fd68ad81957b3ba64f8566cd86ce19 (diff)
downloadhutch-7fba8922e1540741240555560cc9504342bdb091.tar.gz
hutch-7fba8922e1540741240555560cc9504342bdb091.tar.bz2
hutch-7fba8922e1540741240555560cc9504342bdb091.zip
Guard authenticated text fetches to sr.ht hosts and add regression test
Diffstat (limited to 'Hutch/Networking/SRHTClient.swift')
-rw-r--r--Hutch/Networking/SRHTClient.swift14
1 files changed, 14 insertions, 0 deletions
diff --git a/Hutch/Networking/SRHTClient.swift b/Hutch/Networking/SRHTClient.swift
index a24fe24..6032cab 100644
--- a/Hutch/Networking/SRHTClient.swift
+++ b/Hutch/Networking/SRHTClient.swift
@@ -435,6 +435,9 @@ final class SRHTClient: Sendable {
guard let token = _token.withLock({ $0 }), !token.isEmpty else {
throw SRHTError.unauthorized
}
+ guard Self.isTrustedAuthenticatedTextURL(url) else {
+ throw SRHTError.invalidAuthenticatedURL(url)
+ }
var request = URLRequest(url: url)
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
@@ -512,6 +515,17 @@ final class SRHTClient: Sendable {
// MARK: - Data Helper
+private extension SRHTClient {
+ static func isTrustedAuthenticatedTextURL(_ url: URL) -> Bool {
+ guard url.scheme?.localizedCaseInsensitiveCompare("https") == .orderedSame,
+ let host = url.host?.lowercased() else {
+ return false
+ }
+
+ return host.hasSuffix(".sr.ht")
+ }
+}
+
private extension Data {
mutating func append(_ string: String) {
if let data = string.data(using: .utf8) {