Skip to content

Commit

Permalink
Forbid usage of java.lang.ClassLoader.getResourceAsStream
Browse files Browse the repository at this point in the history
  • Loading branch information
BraisGabin committed Dec 18, 2021
1 parent 558d7dd commit 4af5025
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 7 deletions.
1 change: 1 addition & 0 deletions config/detekt/detekt.yml
Expand Up @@ -171,6 +171,7 @@ style:
- 'kotlin.io.println'
- 'java.net.URL.openStream'
- 'java.lang.Class.getResourceAsStream'
- 'java.lang.ClassLoader.getResourceAsStream'
ForbiddenVoid:
active: true
LibraryCodeMustSpecifyReturnType:
Expand Down
@@ -1,5 +1,6 @@
package io.gitlab.arturbosch.detekt.core.config

import io.github.detekt.utils.openSafeStream
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.Notification
import io.gitlab.arturbosch.detekt.api.internal.CommaSeparatedPattern
Expand Down Expand Up @@ -67,13 +68,16 @@ internal fun validateConfig(
val notifications = mutableListOf<Notification>()

fun getDeprecatedProperties(): List<Pair<Regex, String>> {
return settings.javaClass.classLoader.getResourceAsStream("deprecation.properties")!!.use { inputStream ->
val prop = Properties().apply { load(inputStream) }

prop.entries.map { entry ->
(entry.key as String).toRegex() to (entry.value as String)
return settings.javaClass.classLoader
.getResource("deprecation.properties")!!
.openSafeStream()
.use { inputStream ->
val prop = Properties().apply { load(inputStream) }

prop.entries.map { entry ->
(entry.key as String).toRegex() to (entry.value as String)
}
}
}
}

fun testKeys(current: Map<String, Any>, base: Map<String, Any>, parentPath: String?) {
Expand Down
1 change: 1 addition & 0 deletions detekt-gradle-plugin/build.gradle.kts
Expand Up @@ -45,6 +45,7 @@ configurations.compileOnly { extendsFrom(pluginCompileOnly) }
dependencies {
compileOnly(libs.kotlin.gradlePluginApi)
implementation(libs.sarif4k)
implementation(projects.detektUtils)

pluginCompileOnly(libs.android.gradle)
pluginCompileOnly(libs.kotlin.gradle)
Expand Down
@@ -1,5 +1,6 @@
package io.gitlab.arturbosch.detekt

import io.github.detekt.utils.openSafeStream
import io.gitlab.arturbosch.detekt.extensions.DetektExtension
import io.gitlab.arturbosch.detekt.internal.DetektAndroid
import io.gitlab.arturbosch.detekt.internal.DetektJvm
Expand Down Expand Up @@ -120,6 +121,6 @@ const val CONFIGURATION_DETEKT = "detekt"
const val CONFIGURATION_DETEKT_PLUGINS = "detektPlugins"

internal fun loadDetektVersion(classLoader: ClassLoader): String = Properties().run {
load(classLoader.getResourceAsStream("versions.properties")!!)
load(classLoader.getResource("versions.properties")!!.openSafeStream())
getProperty("detektVersion")
}
Expand Up @@ -19,3 +19,7 @@ fun URL.openSafeStream(): InputStream {
fun <T> Class<T>.getSafeResourceAsStream(name: String): InputStream? {
return getResource(name)?.openSafeStream()
}

fun ClassLoader.getSafeResourceAsStream(name: String): InputStream? {
return getResource(name)?.openSafeStream()
}

0 comments on commit 4af5025

Please sign in to comment.