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

Forbid usage of java.lang.ClassLoader.getResourceAsStream #4381

Merged
merged 2 commits into from Dec 18, 2021
Merged
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
5 changes: 3 additions & 2 deletions config/detekt/detekt.yml
Expand Up @@ -169,8 +169,9 @@ style:
methods:
- 'kotlin.io.print'
- 'kotlin.io.println'
- 'java.net.URL.openStream()'
- 'java.lang.Class.getResourceAsStream()'
- '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()
}