Move SSD remote config checks to typed Kotlin
ober
3e59836e538bbe8ee573b1498bfaad6b02f3a1c2
--- a/templates/ssd-review.ss +++ b/templates/ssd-review.ss @@ -1772,7 +1772,7 @@ remoteEndpointIsHttps safeRemoteUrlParts remotePinPresent remotePinOrNull remotePinSha256Length remotePinBytesPresent remotePinBytesValid - remotePinAllowedForProtocol remoteConfigHasPin + remotePinAllowedForProtocol remoteConfigPresent remoteConfigHasPin remoteRelativePathSafe remoteOriginMatches shouldCompareMoreCompleteTruth shouldWriteRemoteTruth remoteTruthWriteSkipped @@ -2019,6 +2019,8 @@ (def (remotePinAllowedForProtocol (protocol : String) (pin : (Nullable Bytes))) : Bool (or (equal? protocol "https") (not (remotePinBytesPresent pin)))) + (def (remoteConfigPresent (config : (Nullable RemoteConfig))) : Bool + (not (nullable-null? config))) (def (remoteConfigHasPin (config : RemoteConfig)) : Bool (remotePinBytesPresent (RemoteConfig-spkiSha256 config))) (def (remoteRelativePathSafe (relative : String)) : Bool @@ -8803,7 +8805,8 @@ "" " private fun remoteUrl(relative: String): URL {" " require(remoteRelativePathSafe(relative))" - " val config = remoteConfig ?: throw IllegalStateException(\"Remote SSD service is not locally configured\")" + " if (!remoteConfigPresent(remoteConfig)) throw IllegalStateException(\"Remote SSD service is not locally configured\")" + " val config = checkNotNull(remoteConfig)" " val candidate = URL(config.apiUrl.toString().trimEnd('/') + \"/\" + relative)" " val basePort = urlEffectivePort(config.apiUrl)" " val candidatePort = urlEffectivePort(candidate)" @@ -8820,7 +8823,8 @@ " contentType: String? = null," " acceptGzip: Boolean = false" " ): HttpsURLConnection {" - " val config = remoteConfig ?: throw IllegalStateException(\"Remote SSD service is not locally configured\")" + " if (!remoteConfigPresent(remoteConfig)) throw IllegalStateException(\"Remote SSD service is not locally configured\")" + " val config = checkNotNull(remoteConfig)" " val connection = (remoteUrl(relative).openConnection() as HttpsURLConnection).apply {" " if (remoteConfigHasPin(config)) {" " sslSocketFactory = pinnedSocketFactory(config)" @@ -8853,7 +8857,8 @@ " }" "" " private fun pinnedSocketFactory(config: RemoteConfig): SSLSocketFactory {" - " val expectedPin = config.spkiSha256 ?: throw CertificateException(\"SPKI pin is required for pinned TLS\")" + " if (!remotePinBytesPresent(config.spkiSha256)) throw CertificateException(\"SPKI pin is required for pinned TLS\")" + " val expectedPin = checkNotNull(config.spkiSha256)" " val managerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm())" " managerFactory.init(null as KeyStore?)" " val platform = managerFactory.trustManagers.filterIsInstance<X509TrustManager>().single()"