Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KTOR-6695 Add configuration warning when nothing is provided for CSRF #3968

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -38,6 +38,11 @@ public val CSRF: RouteScopedPlugin<CSRFConfig> = createRouteScopedPlugin("CSRF",
val headerChecks = pluginConfig.headerChecks
val onFailure = pluginConfig.handleFailure

if (!checkHost && allowedOrigins.isEmpty() && headerChecks.isEmpty()) {
application.log.warn("No validation options provided for CSRF plugin - requests will not be verified!")
return@createRouteScopedPlugin
}

onCall { call ->

if (call.request.httpMethod in setOf(HttpMethod.Get, HttpMethod.Head, HttpMethod.Options)) {
Expand Down
Expand Up @@ -10,6 +10,7 @@ import io.ktor.http.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.server.testing.*
import org.slf4j.*
import kotlin.test.*

class CSRFTest {
Expand Down Expand Up @@ -216,6 +217,26 @@ class CSRFTest {
}
}

@Test
fun logsWarningWhenMisconfigured() {
val warnings = mutableListOf<String>()
val testLogger = object : Logger by LoggerFactory.getLogger("") {
override fun warn(message: String?) {
message?.let(warnings::add)
}
}
testApplication {
environment {
log = testLogger
}
install(CSRF)
}
assertEquals(
"No validation options provided for CSRF plugin - requests will not be verified!",
warnings.firstOrNull()
)
}

private fun ApplicationTestBuilder.configureCSRF(csrfOptions: CSRFConfig.() -> Unit) {
routing {
route("/csrf") {
Expand Down