32 lines
1.2 KiB
Kotlin
32 lines
1.2 KiB
Kotlin
package studio.daybreak.mail
|
|
|
|
import java.net.URL
|
|
|
|
/** Enforces the transport and origin policy for user-configured DAV endpoints. */
|
|
internal object DavUrlPolicy {
|
|
fun parseServerUrl(raw: String): URL {
|
|
val url = try {
|
|
URL(raw)
|
|
} catch (_: Exception) {
|
|
throw IllegalArgumentException("Enter a valid CalDAV or CardDAV server URL.")
|
|
}
|
|
require(url.protocol.equals("https", ignoreCase = true) && url.host.isNotBlank()) {
|
|
"DAV server URLs must use HTTPS."
|
|
}
|
|
require(url.userInfo == null && url.ref == null) {
|
|
"DAV server URLs cannot contain credentials or fragments."
|
|
}
|
|
return url
|
|
}
|
|
|
|
fun requireSameOrigin(baseAddress: String, targetAddress: String) {
|
|
val base = parseServerUrl(baseAddress)
|
|
val target = parseServerUrl(targetAddress)
|
|
val basePort = if (base.port >= 0) base.port else base.defaultPort
|
|
val targetPort = if (target.port >= 0) target.port else target.defaultPort
|
|
require(target.host.equals(base.host, ignoreCase = true) && targetPort == basePort) {
|
|
"The DAV server returned a collection URL on an unexpected host."
|
|
}
|
|
}
|
|
}
|