Skip to content

Commit a97e6a6

Browse files
committed
use HTTPS everywhere, even on localhost, using the mkcert tool
1 parent 746f96c commit a97e6a6

13 files changed

+51
-102
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
.vscode
66

77
Assets/secrets/
8+
TestCertificates/*.pem
89
TestDeployment/data/
910

1011
node_modules/

Package.resolved

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ let package:Package = .init(
7474
.package(url: "https://github.com/tayloraswift/swift-hash", .upToNextMinor(
7575
from: "0.5.0")),
7676
.package(url: "https://github.com/tayloraswift/swift-mongodb", .upToNextMinor(
77-
from: "0.8.2")),
77+
from: "0.8.3")),
7878

7979
.package(url: "https://github.com/apple/swift-atomics", .upToNextMinor(
8080
from: "1.1.0")),
8181
.package(url: "https://github.com/apple/swift-nio", .upToNextMinor(
82-
from: "2.57.0")),
82+
from: "2.58.0")),
8383
.package(url: "https://github.com/apple/swift-nio-http2", .upToNextMinor(
8484
from: "1.27.0")),
8585
.package(url: "https://github.com/apple/swift-nio-ssl", .upToNextMinor(
@@ -177,6 +177,7 @@ let package:Package = .init(
177177
.target(name: "HTML"),
178178
.target(name: "HTTP"),
179179
.product(name: "NIOHTTP1", package: "swift-nio"),
180+
.product(name: "NIOHTTP2", package: "swift-nio-http2"),
180181
.product(name: "NIOSSL", package: "swift-nio-ssl"),
181182
.product(name: "TraceableErrors", package: "swift-grammar"),
182183
]),
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1+
import NIOSSL
2+
13
@frozen public
24
struct Localhost:ServerAuthority
35
{
4-
@inlinable internal
5-
init()
6+
public
7+
let tls:NIOSSLContext
8+
9+
@inlinable public
10+
init(tls:NIOSSLContext)
611
{
12+
self.tls = tls
713
}
814

915
@inlinable public static
10-
var scheme:ServerScheme { .http }
16+
var scheme:ServerScheme { .https }
1117
@inlinable public static
1218
var domain:String { "127.0.0.1" }
1319
}

Sources/HTTPServer/Authorities/ServerAuthority.swift

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,24 @@ import HTML
22
import NIOCore
33
import NIOHTTP1
44
import NIOPosix
5+
import NIOSSL
56
import TraceableErrors
67

78
public
8-
protocol ServerAuthority<SecurityContext>:Sendable
9+
protocol ServerAuthority:Sendable
910
{
10-
associatedtype SecurityContext = Never
11-
1211
static
1312
var scheme:ServerScheme { get }
1413
static
1514
var domain:String { get }
1615

17-
var tls:SecurityContext? { get }
16+
var tls:NIOSSLContext { get }
1817

1918
static
2019
func redact(error:any Error) -> String
2120
}
22-
23-
extension ServerAuthority where Self == Localhost
24-
{
25-
@inlinable public static
26-
var localhost:Self { .init() }
27-
}
28-
29-
extension ServerAuthority<Never>
21+
extension ServerAuthority
3022
{
31-
@inlinable public
32-
var tls:SecurityContext? { nil }
3323
/// Dumps detailed information about the caught error. This information will be shown to
3424
/// *anyone* accessing the server. In production, we strongly recommend overriding this
3525
/// default implementation to avoid inadvertently exposing sensitive data via type
@@ -66,8 +56,6 @@ extension ServerAuthority
6656
{
6757
switch self.scheme
6858
{
69-
case .http(port: 80): return "http://\(self.domain)\(uri)"
70-
case .http(port: let port): return "http://\(self.domain):\(port)\(uri)"
7159
case .https(port: 443): return "https://\(self.domain)\(uri)"
7260
case .https(port: let port): return "https://\(self.domain):\(port)\(uri)"
7361
}

Sources/HTTPServer/Authorities/ServerScheme.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22
enum ServerScheme
33
{
44
case https(port:Int = 443)
5-
case http(port:Int = 80)
65
}
76
extension ServerScheme
87
{
98
@inlinable public static
109
var https:Self { .https() }
11-
12-
@inlinable public static
13-
var http:Self { .http() }
1410
}
1511
extension ServerScheme
1612
{
@@ -19,7 +15,6 @@ extension ServerScheme
1915
{
2016
switch self
2117
{
22-
case .http(port: let port): return port
2318
case .https(port: let port): return port
2419
}
2520
}
@@ -28,7 +23,6 @@ extension ServerScheme
2823
{
2924
switch self
3025
{
31-
case .http: return "http"
3226
case .https: return "https"
3327
}
3428
}

Sources/HTTPServer/Channels/HTTPServerDelegate.swift

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,9 @@ extension HTTPServerDelegate
3434
address: channel.remoteAddress,
3535
server: self)
3636

37-
guard let tls:NIOSSLContext = authority.tls as? NIOSSLContext
38-
else
39-
{
40-
return channel.pipeline.configureHTTPServerPipeline(withErrorHandling: true)
41-
.flatMap
42-
{
43-
channel.pipeline.addHandler(endpoint)
44-
}
45-
}
46-
return channel.pipeline.addHandler(NIOSSLServerHandler.init(context: tls))
37+
38+
return channel.pipeline.addHandler(NIOSSLServerHandler.init(
39+
context: authority.tls))
4740
.flatMap
4841
{
4942
channel.pipeline.configureHTTPServerPipeline(withErrorHandling: true)

Sources/UnidocServer/Authorities/ServerAuthority (ext).swift

Lines changed: 0 additions & 19 deletions
This file was deleted.

Sources/UnidocServer/Authorities/Swiftinit.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ import NIOSSL
33

44
struct Swiftinit
55
{
6-
private
7-
let context:NIOSSLContext
6+
let tls:NIOSSLContext
87

9-
init(context:NIOSSLContext)
8+
init(tls:NIOSSLContext)
109
{
11-
self.context = context
10+
self.tls = tls
1211
}
1312
}
1413
extension Swiftinit:ServerAuthority
@@ -19,8 +18,6 @@ extension Swiftinit:ServerAuthority
1918
static
2019
var domain:String { "swiftinit.org" }
2120

22-
var tls:NIOSSLContext? { self.context }
23-
2421
static
2522
func redact(error _:any Error) -> String
2623
{

Sources/UnidocServer/Authorities/SwiftinitTest.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ import NIOSSL
33

44
struct SwiftinitTest
55
{
6-
private
7-
let context:NIOSSLContext
6+
let tls:NIOSSLContext
87

9-
init(context:NIOSSLContext)
8+
init(tls:NIOSSLContext)
109
{
11-
self.context = context
10+
self.tls = tls
1211
}
1312
}
1413
extension SwiftinitTest:ServerAuthority
@@ -19,8 +18,6 @@ extension SwiftinitTest:ServerAuthority
1918
static
2019
var domain:String { "test.swiftinit.org" }
2120

22-
var tls:NIOSSLContext? { self.context }
23-
2421
static
2522
func redact(error _:any Error) -> String
2623
{

0 commit comments

Comments
 (0)