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

SONARKT-507 Migrate VerifiedServerHostnamesCheck to kotlin-analysis-api #549

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@
*/
package org.sonarsource.kotlin.checks

import org.jetbrains.kotlin.analysis.api.resolution.KaFunctionCall
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtLambdaExpression
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.KtReturnExpression
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.util.getParentCall
import org.jetbrains.kotlin.resolve.calls.util.getType
import org.sonar.check.Rule
import org.sonarsource.kotlin.api.checks.AbstractCheck
import org.sonarsource.kotlin.api.checks.FunMatcher
import org.sonarsource.kotlin.api.checks.getParentCall
import org.sonarsource.kotlin.api.checks.predictRuntimeBooleanValue
import org.sonarsource.kotlin.api.frontend.KotlinFileContext
import org.sonarsource.kotlin.api.visiting.withKaSession

@org.sonarsource.kotlin.api.frontend.K1only
@Rule(key = "S5527")
class VerifiedServerHostnamesCheck : AbstractCheck() {

Expand All @@ -47,21 +47,19 @@ class VerifiedServerHostnamesCheck : AbstractCheck() {
}

override fun visitNamedFunction(function: KtNamedFunction, kotlinFileContext: KotlinFileContext) {
val (_, _, bindingContext) = kotlinFileContext
if (VERIFY_MATCHER.matches(function, bindingContext)) {
if (VERIFY_MATCHER.matches(function)) {
val listStatements = function.listStatements()
if (listStatements.size == 1 && onlyReturnsTrue(listStatements[0], bindingContext)) {
if (listStatements.size == 1 && onlyReturnsTrue(listStatements[0])) {
kotlinFileContext.reportIssue(function.nameIdentifier!!, MESSAGE)
}
}
}

override fun visitLambdaExpression(expression: KtLambdaExpression, kotlinFileContext: KotlinFileContext) {
val (_, _, bindingContext) = kotlinFileContext
expression.getParentCall(bindingContext)?.let {
if (HOSTNAME_VERIFIER_MATCHER.matches(it, bindingContext)) {
(expression.getParentCall() as? KaFunctionCall<*>)?.let {
if (HOSTNAME_VERIFIER_MATCHER.matches(it)) {
val listStatements = expression.bodyExpression?.statements
if (listStatements?.size == 1 && listStatements[0].isTrueConstant(bindingContext)) {
if (listStatements?.size == 1 && listStatements[0].isTrueConstant()) {
kotlinFileContext.reportIssue(expression, MESSAGE)
}
}
Expand All @@ -70,16 +68,13 @@ class VerifiedServerHostnamesCheck : AbstractCheck() {

private fun onlyReturnsTrue(
ktExpression: KtExpression,
bindingContext: BindingContext,
): Boolean = when (ktExpression) {
is KtReturnExpression ->
ktExpression.returnedExpression?.isTrueConstant(bindingContext) ?: false
ktExpression.returnedExpression?.isTrueConstant() ?: false
else -> false
}

private fun KtExpression.isTrueConstant(
bindingContext: BindingContext,
) = getType(bindingContext)?.let {
bindingContext[BindingContext.COMPILE_TIME_VALUE, this]?.getValue(it) == true
} ?: false
private fun KtExpression.isTrueConstant() = withKaSession {
predictRuntimeBooleanValue() ?: false
}
}