Skip to content

Commit fe08372

Browse files
committed
refactor(go): Migrate GoDep TOML parsing to kotlinx-serialization
This allows to get rid of toml4j which had no releases since 2017 and depends on GSON, which has vulnerabilities. Signed-off-by: Sebastian Schuberth <[email protected]>
1 parent 90f9993 commit fe08372

File tree

4 files changed

+50
-25
lines changed

4 files changed

+50
-25
lines changed

gradle/libs.versions.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ slf4j = "2.0.9"
5959
springCore = "5.3.30"
6060
svnkit = "1.10.11"
6161
sw360Client = "17.0.1-m2"
62-
toml4j = "0.7.2"
6362
wiremock = "3.0.1"
6463
xz = "1.9"
6564

@@ -159,7 +158,6 @@ slf4j = { module = "org.slf4j:slf4j-api ", version.ref = "slf4j" }
159158
springCore = { module = "org.springframework:spring-core", version.ref = "springCore" }
160159
svnkit = { module = "org.tmatesoft.svnkit:svnkit", version.ref = "svnkit" }
161160
sw360Client = { module = "org.eclipse.sw360:client", version.ref = "sw360Client" }
162-
toml4j = { module = "com.moandjiezana.toml:toml4j", version.ref = "toml4j" }
163161
wiremock = { module = "com.github.tomakehurst:wiremock", version.ref = "wiremock" }
164162
xz = { module = "org.tukaani:xz", version.ref = "xz" }
165163

plugins/package-managers/go/build.gradle.kts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,7 @@ dependencies {
4343
implementation(project(":utils:spdx-utils"))
4444

4545
implementation(libs.bundles.kotlinxSerialization)
46-
implementation(libs.toml4j)
47-
constraints {
48-
implementation("com.google.code.gson:gson:2.10.1") {
49-
because("Earlier versions have vulnerabilities.")
50-
}
51-
}
46+
implementation(libs.tomlkt)
5247

5348
funTestImplementation(testFixtures(project(":analyzer")))
5449
}

plugins/package-managers/go/src/main/kotlin/GoDep.kt

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@
1919

2020
package org.ossreviewtoolkit.plugins.packagemanagers.go
2121

22-
import com.moandjiezana.toml.Toml
23-
2422
import java.io.File
2523
import java.io.IOException
2624
import java.net.URI
2725

2826
import kotlin.io.path.copyToRecursively
2927
import kotlin.io.path.createDirectories
3028

29+
import net.peanuuutz.tomlkt.Toml
30+
import net.peanuuutz.tomlkt.decodeFromNativeReader
31+
3132
import org.apache.logging.log4j.kotlin.logger
3233

3334
import org.ossreviewtoolkit.analyzer.AbstractPackageManagerFactory
@@ -59,6 +60,8 @@ import org.ossreviewtoolkit.utils.common.toUri
5960
import org.ossreviewtoolkit.utils.ort.createOrtTempDir
6061
import org.ossreviewtoolkit.utils.ort.showStackTrace
6162

63+
private val toml = Toml { ignoreUnknownKeys = true }
64+
6265
/**
6366
* A map of legacy package manager file names "dep" can import, and their respective lock file names, if any.
6467
*/
@@ -227,26 +230,17 @@ class GoDep(
227230
run("ensure", workingDir = workingDir, environment = mapOf("GOPATH" to gopath.path))
228231
}
229232

230-
val entries = Toml().read(lockfile).toMap()["projects"]
231-
if (entries == null) {
232-
logger.warn { "${lockfile.name} is missing any [[projects]] entries" }
233+
val contents = lockfile.reader().use { toml.decodeFromNativeReader<GoDepLockFile>(it) }
234+
if (contents.projects.isEmpty()) {
235+
logger.warn { "The lockfile '$lockfile' does not contain any projects." }
233236
return emptyList()
234237
}
235238

236239
val projects = mutableListOf<Map<String, String>>()
237240

238-
for (entry in entries as List<*>) {
239-
val project = entry as? Map<*, *> ?: continue
240-
val name = project["name"]
241-
val revision = project["revision"]
242-
243-
if (name !is String || revision !is String) {
244-
logger.warn { "Invalid [[projects]] entry in $lockfile: $entry" }
245-
continue
246-
}
247-
248-
val version = project["version"] as? String ?: revision
249-
projects += mapOf("name" to name, "revision" to revision, "version" to version)
241+
contents.projects.forEach { project ->
242+
val version = project.version ?: project.revision
243+
projects += mapOf("name" to project.name, "revision" to project.revision, "version" to version)
250244
}
251245

252246
return projects
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (C) 2023 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
package org.ossreviewtoolkit.plugins.packagemanagers.go
21+
22+
import kotlinx.serialization.Serializable
23+
24+
/**
25+
* See https://golang.github.io/dep/docs/Gopkg.lock.html.
26+
*/
27+
@Serializable
28+
internal data class GoDepLockFile(
29+
val projects: List<Project> = emptyList()
30+
) {
31+
@Serializable
32+
data class Project(
33+
val name: String,
34+
val packages: List<String>,
35+
val revision: String,
36+
val version: String? = null
37+
)
38+
}

0 commit comments

Comments
 (0)