Skip to content

Commit

Permalink
fix: Configure CORS correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Jul 11, 2024
1 parent a7666ba commit 2ed4cf3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 21 deletions.
7 changes: 5 additions & 2 deletions configuration.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ contributors-repositories = [
"revanced-manager",
]
api-version = 1
cors = { host = "*.revanced.app", sub-domains = [] }
endpoint = "https://api.revanced.app"
cors-allowed-hosts = [
"revanced.app",
"*.revanced.app"
]
endpoint = "https://api.revanced.app"
10 changes: 6 additions & 4 deletions src/main/kotlin/app/revanced/api/configuration/HTTP.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ fun Application.configureHTTP() {
val configurationRepository = get<ConfigurationRepository>()

install(CORS) {
allowHost(
host = configurationRepository.cors.host,
subDomains = configurationRepository.cors.subDomains,
)
configurationRepository.corsAllowedHosts.forEach { host ->
allowHost(
host = host,
schemes = listOf("http", "https")
)
}
}

install(RateLimit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import java.io.File
* @property integrations The source of the integrations.
* @property contributorsRepositoryNames The names of the repositories to get contributors from.
* @property apiVersion The version to use for the API.
* @property cors The CORS configuration.
* @property corsAllowedHosts The hosts allowed to make requests to the API.
* @property endpoint The endpoint of the API.
*/
@Serializable
Expand All @@ -31,7 +31,8 @@ internal class ConfigurationRepository(
val contributorsRepositoryNames: Set<String>,
@SerialName("api-version")
val apiVersion: Int = 1,
val cors: Cors,
@SerialName("cors-allowed-hosts")
val corsAllowedHosts: Set<String>,
val endpoint: String,
) {
/**
Expand Down Expand Up @@ -61,19 +62,6 @@ internal class ConfigurationRepository(
@SerialName("public-key-file")
val publicKeyFile: File,
)

/**
* The CORS configuration.
*
* @property host The host of the API to configure CORS.
* @property subDomains The subdomains to allow for CORS.
*/
@Serializable
internal class Cors(
val host: String,
@SerialName("sub-domains")
val subDomains: List<String>,
)
}

private object RegexSerializer : KSerializer<Regex> {
Expand Down

0 comments on commit 2ed4cf3

Please sign in to comment.