Skip to content

Commit

Permalink
SONARKT-423 Migrate AuthorisingNonAuthenticatedUsersCheck to kotlin-a…
Browse files Browse the repository at this point in the history
…nalysis-api

Co-authored-by: Marharyta Nedzelska <[email protected]>
  • Loading branch information
Godin and leveretka authored Jan 27, 2025
1 parent 5b84ed7 commit 8522295
Showing 1 changed file with 24 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@
*/
package org.sonarsource.kotlin.checks

import org.jetbrains.kotlin.analysis.api.resolution.KaFunctionCall
import org.jetbrains.kotlin.analysis.api.resolution.singleFunctionCallOrNull
import org.jetbrains.kotlin.analysis.api.resolution.successfulFunctionCallOrNull
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.util.getCall
import org.jetbrains.kotlin.resolve.calls.util.getType
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
import org.sonar.check.Rule
import org.sonarsource.kotlin.api.checks.CallAbstractCheck
import org.sonarsource.kotlin.api.checks.ConstructorMatcher
import org.sonarsource.kotlin.api.checks.FunMatcher
import org.sonarsource.kotlin.api.reporting.SecondaryLocation
import org.sonarsource.kotlin.api.checks.predictReceiverExpression
import org.sonarsource.kotlin.api.checks.predictRuntimeBooleanValue
import org.sonarsource.kotlin.api.reporting.SecondaryLocation
import org.sonarsource.kotlin.api.reporting.KotlinTextRanges.textRange
import org.sonarsource.kotlin.api.frontend.KotlinFileContext
import org.sonarsource.kotlin.api.visiting.withKaSession

private const val BUILDER = "android.security.keystore.KeyGenParameterSpec.Builder"

Expand All @@ -39,38 +41,36 @@ private val KEY_GEN_BUILDER_SET_AUTH_MATCHER = FunMatcher(qualifier = BUILDER, n
withArguments("kotlin.Boolean")
}

@org.sonarsource.kotlin.api.frontend.K1only
@Rule(key = "S6288")
class AuthorisingNonAuthenticatedUsersCheck : CallAbstractCheck() {
override val functionsToVisit = listOf(KEY_GEN_BUILDER_BUILD_MATCHER)

override fun visitFunctionCall(
callExpression: KtCallExpression,
resolvedCall: ResolvedCall<*>,
resolvedCall: KaFunctionCall<*>,
kotlinFileContext: KotlinFileContext,
) {
val bindingContext = kotlinFileContext.bindingContext
val call = callExpression.getCall(bindingContext) ?: return
var receiver = call
) = withKaSession {
var receiver = callExpression.resolveToCall()?.successfulFunctionCallOrNull() ?: return
var callElement = callExpression
val secondaryLocations = mutableListOf<SecondaryLocation>()

while (!KEY_GEN_BUILDER_MATCHER.matches(receiver, bindingContext)) {
val callElement = receiver.callElement as? KtCallExpression ?: return
while (!KEY_GEN_BUILDER_MATCHER.matches(receiver)) {

if (KEY_GEN_BUILDER_SET_AUTH_MATCHER.matches(receiver, bindingContext)) {
val argumentExpression = receiver.valueArguments[0]?.getArgumentExpression()!!
argumentExpression.getType(bindingContext)?.let {
val argValue = bindingContext[BindingContext.COMPILE_TIME_VALUE, argumentExpression]
?.getValue(it) as? Boolean
if (argValue != false) return
secondaryLocations.add(SecondaryLocation(kotlinFileContext.textRange(callElement.calleeExpression!!)))
}
if (KEY_GEN_BUILDER_SET_AUTH_MATCHER.matches(receiver)) {
if (receiver.argumentMapping.keys.first().predictRuntimeBooleanValue() != false)
return
secondaryLocations.add(SecondaryLocation(kotlinFileContext.textRange(callElement.calleeExpression!!)))
}
val receiverExpression = callElement.predictReceiverExpression()
callElement = when(receiverExpression) {
is KtCallExpression -> receiverExpression
is KtDotQualifiedExpression -> receiverExpression.selectorExpression as? KtCallExpression
else -> null
} ?: return

receiver = callElement.predictReceiverExpression(bindingContext)
?.getCall(bindingContext) ?: return
receiver = receiverExpression?.resolveToCall()?.singleFunctionCallOrNull() ?: return
}
kotlinFileContext.reportIssue(receiver.calleeExpression!!,
kotlinFileContext.reportIssue(callElement.calleeExpression!!,
"Make sure authorizing non-authenticated users to use this key is safe here.",
secondaryLocations)
}
Expand Down

0 comments on commit 8522295

Please sign in to comment.