Skip to content

Commit f15fcd2

Browse files
leveretkaGodin
authored andcommitted
SONARKT-400 Migrate WeakSSLContextCheck to kotlin-analysis-api
1 parent e7126b3 commit f15fcd2

File tree

1 file changed

+21
-31
lines changed

1 file changed

+21
-31
lines changed

sonar-kotlin-checks/src/main/java/org/sonarsource/kotlin/checks/WeakSSLContextCheck.kt

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,22 @@
1616
*/
1717
package org.sonarsource.kotlin.checks
1818

19-
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
19+
import org.jetbrains.kotlin.analysis.api.symbols.KaEnumEntrySymbol
20+
import org.jetbrains.kotlin.idea.references.mainReference
2021
import org.jetbrains.kotlin.psi.KtCallExpression
2122
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
2223
import org.jetbrains.kotlin.psi.KtExpression
2324
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
2425
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
25-
import org.jetbrains.kotlin.resolve.BindingContext
26-
import org.jetbrains.kotlin.resolve.BindingContext.REFERENCE_TARGET
27-
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameOrNull
2826
import org.sonar.check.Rule
2927
import org.sonarsource.kotlin.api.checks.AbstractCheck
3028
import org.sonarsource.kotlin.api.checks.FunMatcher
31-
import org.sonarsource.kotlin.api.reporting.SecondaryLocation
32-
import org.sonarsource.kotlin.api.reporting.KotlinTextRanges.textRange
29+
import org.sonarsource.kotlin.api.checks.predictRuntimeStringValue
3330
import org.sonarsource.kotlin.api.frontend.KotlinFileContext
31+
import org.sonarsource.kotlin.api.reporting.KotlinTextRanges.textRange
32+
import org.sonarsource.kotlin.api.reporting.SecondaryLocation
33+
import org.sonarsource.kotlin.api.visiting.withKaSession
3434

35-
@org.sonarsource.kotlin.api.frontend.K1only
3635
@Rule(key = "S4423")
3736
class WeakSSLContextCheck : AbstractCheck() {
3837
private val WEAK_FOR_OK_HTTP = setOf(
@@ -66,38 +65,35 @@ class WeakSSLContextCheck : AbstractCheck() {
6665
}
6766

6867
override fun visitCallExpression(node: KtCallExpression, kotlinFileContext: KotlinFileContext) {
69-
val bindingContext = kotlinFileContext.bindingContext
7068
when {
71-
SSL_CONTEXT_MATCHER.matches(node, bindingContext) ->
72-
handleSSL(node, bindingContext, kotlinFileContext)
73-
OKHTTP_MATCHER.matches(node, bindingContext) ->
74-
handleOkHttp(node, bindingContext, kotlinFileContext)
69+
SSL_CONTEXT_MATCHER.matches(node) ->
70+
handleSSL(node, kotlinFileContext)
71+
OKHTTP_MATCHER.matches(node) ->
72+
handleOkHttp(node, kotlinFileContext)
7573
}
7674
}
7775

7876
private fun handleSSL(
7977
node: KtCallExpression,
80-
bindingContext: BindingContext,
8178
kotlinFileContext: KotlinFileContext,
8279
) {
8380
node.valueArguments
8481
.firstOrNull()
8582
?.getArgumentExpression()
8683
?.let {
87-
if (WEAK_FOR_SSL.contains(it.value(bindingContext)))
84+
if (WEAK_FOR_SSL.contains(it.value()))
8885
reportUnsecureSSLContext(listOf(it), kotlinFileContext)
8986
}
9087
}
9188

9289
private fun handleOkHttp(
9390
node: KtCallExpression,
94-
bindingContext: BindingContext,
9591
kotlinFileContext: KotlinFileContext,
9692
) {
9793
val unsecureVersions = node.valueArguments
9894
.mapNotNull { it.getArgumentExpression() }
9995
.filter {
100-
WEAK_FOR_OK_HTTP.contains(it.value(bindingContext))
96+
WEAK_FOR_OK_HTTP.contains(it.value())
10197
}
10298

10399
reportUnsecureSSLContext(unsecureVersions, kotlinFileContext)
@@ -119,21 +115,15 @@ class WeakSSLContextCheck : AbstractCheck() {
119115
}
120116
}
121117

122-
private fun KtExpression.value(
123-
bindingContext: BindingContext,
124-
): String? = when (this) {
125-
is KtStringTemplateExpression -> asConstant()
126-
is KtNameReferenceExpression -> {
127-
val descriptor = bindingContext[REFERENCE_TARGET, this]
128-
if (descriptor is PropertyDescriptor) descriptor.compileTimeInitializer?.boxedValue().toString()
129-
else null
130-
}
131-
is KtDotQualifiedExpression -> {
132-
val selectorExpression = selectorExpression
133-
if (selectorExpression is KtNameReferenceExpression)
134-
bindingContext[REFERENCE_TARGET, selectorExpression]?.fqNameOrNull()?.asString()
135-
else null
118+
private fun KtExpression.value(): String? = withKaSession {
119+
when (this@value) {
120+
is KtStringTemplateExpression -> asConstant()
121+
is KtNameReferenceExpression -> predictRuntimeStringValue()
122+
is KtDotQualifiedExpression -> {
123+
(selectorExpression?.mainReference?.resolveToSymbol() as? KaEnumEntrySymbol)
124+
?.callableId?.asSingleFqName()?.asString()
125+
}
126+
else -> null
136127
}
137-
else -> null
138128
}
139129
}

0 commit comments

Comments
 (0)