Skip to content

Commit 7812608

Browse files
committed
feat(spdx): Tolerate invalid SPDX expressions when writing reports
Instead of failing hard and not generating a report, log errors for invalid SPDX expressions coming from detected licenses. This allows to post-process the report later on with third-party tools like [1]. The behavior when reading SPDX reports is unchanged, and validation exceptions are propagated as issues like before. Resolves #10320. [1]: https://tools.spdx.org/app/validate/ Signed-off-by: Sebastian Schuberth <[email protected]>
1 parent 611c2a5 commit 7812608

File tree

7 files changed

+31
-20
lines changed

7 files changed

+31
-20
lines changed

plugins/package-managers/spdx/src/main/kotlin/utils/SpdxDocumentCache.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ internal class SpdxDocumentCache {
4848
documentCache.getOrPut(file) {
4949
logger.info { "Loading SpdxDocument from '$file'." }
5050

51-
runCatching { SpdxModelMapper.read(file) }
51+
runCatching {
52+
SpdxModelMapper.read<SpdxDocument>(file).apply {
53+
packages.forEach { it.validate() }
54+
files.forEach { it.validate() }
55+
}
56+
}
5257
}
5358
}

plugins/reporters/spdx/src/funTest/kotlin/SpdxDocumentReporterFunTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ private val ortResult = OrtResult(
396396
summary = ScanSummary.EMPTY.copy(
397397
licenseFindings = setOf(
398398
LicenseFinding(
399-
license = "GPL-3.0-only",
399+
license = "GPL-2.0-only WITH NOASSERTION",
400400
location = TextLocation("LICENSE", 1)
401401
)
402402
),

plugins/reporters/spdx/src/funTest/resources/spdx-document-reporter-expected-output.spdx.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135
"filesAnalyzed" : false,
136136
"homepage" : "NONE",
137137
"licenseConcluded" : "NOASSERTION",
138-
"licenseDeclared" : "GPL-3.0-only",
138+
"licenseDeclared" : "GPL-2.0-only WITH NOASSERTION",
139139
"name" : "seventh-package",
140140
"versionInfo" : "0.0.1"
141141
}, {
@@ -156,8 +156,8 @@
156156
"hasFiles" : [ "SPDXRef-File-2", "SPDXRef-File-3" ],
157157
"homepage" : "NONE",
158158
"licenseConcluded" : "NOASSERTION",
159-
"licenseDeclared" : "GPL-3.0-only",
160-
"licenseInfoFromFiles" : [ "GPL-3.0-only" ],
159+
"licenseDeclared" : "GPL-2.0-only WITH NOASSERTION",
160+
"licenseInfoFromFiles" : [ "GPL-2.0-only WITH NOASSERTION" ],
161161
"name" : "seventh-package",
162162
"packageVerificationCode" : {
163163
"packageVerificationCodeValue" : "e14acc46fad3a38a1ef2830067619812b51cb4bc"
@@ -215,7 +215,7 @@
215215
"copyrightText" : "NONE",
216216
"fileName" : "LICENSE",
217217
"licenseConcluded" : "NOASSERTION",
218-
"licenseInfoInFiles" : [ "GPL-3.0-only" ]
218+
"licenseInfoInFiles" : [ "GPL-2.0-only WITH NOASSERTION" ]
219219
}, {
220220
"SPDXID" : "SPDXRef-File-3",
221221
"checksums" : [ {

plugins/reporters/spdx/src/funTest/resources/spdx-document-reporter-expected-output.spdx.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ packages:
150150
filesAnalyzed: false
151151
homepage: "NONE"
152152
licenseConcluded: "NOASSERTION"
153-
licenseDeclared: "GPL-3.0-only"
153+
licenseDeclared: "GPL-2.0-only WITH NOASSERTION"
154154
name: "seventh-package"
155155
versionInfo: "0.0.1"
156156
- SPDXID: "SPDXRef-Package-Maven-seventh-package-group-seventh-package-0.0.1-source-artifact"
@@ -170,9 +170,9 @@ packages:
170170
- "SPDXRef-File-3"
171171
homepage: "NONE"
172172
licenseConcluded: "NOASSERTION"
173-
licenseDeclared: "GPL-3.0-only"
173+
licenseDeclared: "GPL-2.0-only WITH NOASSERTION"
174174
licenseInfoFromFiles:
175-
- "GPL-3.0-only"
175+
- "GPL-2.0-only WITH NOASSERTION"
176176
name: "seventh-package"
177177
packageVerificationCode:
178178
packageVerificationCodeValue: "e14acc46fad3a38a1ef2830067619812b51cb4bc"
@@ -226,7 +226,7 @@ files:
226226
fileName: "LICENSE"
227227
licenseConcluded: "NOASSERTION"
228228
licenseInfoInFiles:
229-
- "GPL-3.0-only"
229+
- "GPL-2.0-only WITH NOASSERTION"
230230
- SPDXID: "SPDXRef-File-3"
231231
checksums:
232232
- algorithm: "SHA1"

plugins/reporters/spdx/src/main/kotlin/Extensions.kt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ package org.ossreviewtoolkit.plugins.reporters.spdx
2323

2424
import java.util.concurrent.atomic.AtomicInteger
2525

26+
import org.apache.logging.log4j.kotlin.logger
27+
2628
import org.ossreviewtoolkit.model.ArtifactProvenance
2729
import org.ossreviewtoolkit.model.Hash
2830
import org.ossreviewtoolkit.model.Identifier
@@ -197,7 +199,13 @@ internal fun Package.toSpdxPackage(
197199
packageVerificationCode = packageVerificationCode,
198200
supplier = authors.takeUnless { it.isEmpty() }?.joinToString(prefix = "${SpdxConstants.PERSON} "),
199201
versionInfo = id.version
200-
)
202+
).also { spdxPackage ->
203+
runCatching {
204+
spdxPackage.validate()
205+
}.onFailure {
206+
logger.error { "Validation failed for '${spdxPackage.spdxId}': ${it.message}" }
207+
}
208+
}
201209
}
202210

203211
private fun OrtResult.getVcsScanResult(id: Identifier): ScanResult? =
@@ -290,7 +298,13 @@ internal fun OrtResult.getSpdxFiles(
290298
licenseConcluded = SpdxConstants.NOASSERTION,
291299
licenseInfoInFiles = fileFindings.licenses.map { it.toString() }.ifEmpty { listOf(SpdxConstants.NONE) },
292300
copyrightText = fileFindings.copyrights.sorted().joinToString("\n").ifBlank { SpdxConstants.NONE }
293-
)
301+
).also { spdxFile ->
302+
runCatching {
303+
spdxFile.validate()
304+
}.onFailure {
305+
logger.error { "Validation failed for '${spdxFile.spdxId}': ${it.message}" }
306+
}
307+
}
294308
}
295309

296310
/**

utils/spdx-document/src/main/kotlin/model/SpdxFile.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,6 @@ data class SpdxFile(
169169
VIDEO
170170
}
171171

172-
init {
173-
validate()
174-
}
175-
176172
fun validate(): SpdxFile =
177173
apply {
178174
require(spdxId.startsWith(REF_PREFIX)) {

utils/spdx-document/src/main/kotlin/model/SpdxPackage.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,6 @@ data class SpdxPackage(
190190
@JsonInclude(JsonInclude.Include.NON_EMPTY)
191191
val versionInfo: String = ""
192192
) {
193-
init {
194-
validate()
195-
}
196-
197193
fun validate(): SpdxPackage =
198194
apply {
199195
require(spdxId.startsWith(SpdxConstants.REF_PREFIX)) {

0 commit comments

Comments
 (0)