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

feat: Add license text as evidence #1243

Merged
merged 19 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ Options:
--output-reproducible Whether to go the extra mile and make the output reproducible.
This requires more resources, and might result in loss of time- and random-based-values.
(env: BOM_REPRODUCIBLE)
--gather-license-texts Search for license files in components and include them as license evidence.
This feature is experimental. (default: false)
--output-format <format> Which output format to use.
(choices: "JSON", "XML", default: "JSON")
--output-file <file> Path to the output file.
Expand Down
15 changes: 15 additions & 0 deletions src/_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,18 @@ export function tryRemoveSecretsFromUrl (url: string): string {
return url
}
}

export const LICENSE_FILENAME_PATTERN = /^(?:UN)?LICEN[CS]E|.\.LICEN[CS]E$|^NOTICE$/i
export const LICENSE_FILENAME_BASE = new Set(['licence', 'license'])
export const LICENSE_FILENAME_EXT = new Set(['.apache', '.bsd', '.gpl', '.mit'])
export const MAP_TEXT_EXTENSION_MIME = new Map([
['', 'text/plain'],
['.htm', 'text/html'],
['.html', 'text/html'],
['.md', 'text/markdown'],
['.txt', 'text/plain'],
['.rst', 'text/prs.fallenstein.rst'],
['.xml', 'text/xml'],
['.license', 'text/plain'],
['.licence', 'text/plain']
])
71 changes: 68 additions & 3 deletions src/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,18 @@ Copyright (c) OWASP Foundation. All Rights Reserved.
*/

import { type Builders, Enums, type Factories, Models, Utils } from '@cyclonedx/cyclonedx-library'
import { existsSync } from 'fs'
import { existsSync, readdirSync, readFileSync } from 'fs'
import * as normalizePackageData from 'normalize-package-data'
import * as path from 'path'

import { isString, loadJsonFile, tryRemoveSecretsFromUrl } from './_helpers'
import { join, parse } from 'path'

import {
isString,
LICENSE_FILENAME_BASE, LICENSE_FILENAME_EXT,
LICENSE_FILENAME_PATTERN,
loadJsonFile, MAP_TEXT_EXTENSION_MIME,
tryRemoveSecretsFromUrl
} from './_helpers'
import { makeNpmRunner, type runFunc } from './npmRunner'
import { PropertyNames, PropertyValueBool } from './properties'
import { versionCompare } from './versionCompare'
Expand All @@ -37,6 +44,7 @@ interface BomBuilderOptions {
reproducible?: BomBuilder['reproducible']
flattenComponents?: BomBuilder['flattenComponents']
shortPURLs?: BomBuilder['shortPURLs']
gatherLicenseTexts?: BomBuilder['gatherLicenseTexts']
}

type cPath = string
Expand All @@ -47,6 +55,7 @@ export class BomBuilder {
componentBuilder: Builders.FromNodePackageJson.ComponentBuilder
treeBuilder: TreeBuilder
purlFactory: Factories.FromNodePackageJson.PackageUrlFactory
licenseFetcher: LicenseFetcher

ignoreNpmErrors: boolean

Expand All @@ -56,6 +65,7 @@ export class BomBuilder {
reproducible: boolean
flattenComponents: boolean
shortPURLs: boolean
gatherLicenseTexts: boolean

console: Console

Expand All @@ -71,6 +81,7 @@ export class BomBuilder {
this.componentBuilder = componentBuilder
this.treeBuilder = treeBuilder
this.purlFactory = purlFactory
this.licenseFetcher = new LicenseFetcher()

this.ignoreNpmErrors = options.ignoreNpmErrors ?? false
this.metaComponentType = options.metaComponentType ?? Enums.ComponentType.Library
Expand All @@ -79,6 +90,7 @@ export class BomBuilder {
this.reproducible = options.reproducible ?? false
this.flattenComponents = options.flattenComponents ?? false
this.shortPURLs = options.shortPURLs ?? false
this.gatherLicenseTexts = options.gatherLicenseTexts ?? false

this.console = console_
}
Expand Down Expand Up @@ -465,6 +477,23 @@ export class BomBuilder {
l.acknowledgement = Enums.LicenseAcknowledgement.Declared
})

if (this.gatherLicenseTexts) {
if (this.packageLockOnly) {
this.console.warn('WARN | Adding license text is ignored (package-lock-only is configured!) for %j', data.name)
} else {
component.evidence = new Models.ComponentEvidence()
for (const license of this.licenseFetcher.fetchLicenseEvidence(data?.path as string)) {
if (license != null) {
// only create a evidence if a license attachment is found
if (component.evidence == null) {
component.evidence = new Models.ComponentEvidence()
}
component.evidence.licenses.add(license)
}
}
}
}

if (isOptional || isDevOptional) {
component.scope = Enums.ComponentScope.Optional
}
Expand Down Expand Up @@ -668,3 +697,39 @@ export class TreeBuilder {
const structuredClonePolyfill: <T>(value: T) => T = typeof structuredClone === 'function'
? structuredClone
: function (value) { return JSON.parse(JSON.stringify(value)) }

export class LicenseFetcher {
jkowalleck marked this conversation as resolved.
Show resolved Hide resolved
* fetchLicenseEvidence (path: string): Generator<Models.License | null, void, void> {
const files = readdirSync(path)
for (const file of files) {
if (!LICENSE_FILENAME_PATTERN.test(file)) {
continue
}

const contentType = this.getMimeForLicenseFile(file)
if (contentType === undefined) {
continue
}

const fp = join(path, file)
yield new Models.NamedLicense(
`file: ${file}`,
{
text: new Models.Attachment(
readFileSync(fp).toString('base64'),
{
contentType,
encoding: Enums.AttachmentEncoding.Base64
}
)
})
}
}

private getMimeForLicenseFile (filename: string): string | undefined {
jkowalleck marked this conversation as resolved.
Show resolved Hide resolved
const { name, ext } = parse(filename.toLowerCase())
return LICENSE_FILENAME_BASE.has(name) && LICENSE_FILENAME_EXT.has(ext)
? 'text/plain'
: MAP_TEXT_EXTENSION_MIME.get(ext)
}
}
10 changes: 9 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ interface CommandOptions {
flattenComponents: boolean
shortPURLs: boolean
outputReproducible: boolean
gatherLicenseTexts: boolean
outputFormat: OutputFormat
outputFile: string
validate: boolean | undefined
Expand Down Expand Up @@ -115,6 +116,12 @@ function makeCommand (process: NodeJS.Process): Command {
).env(
'BOM_REPRODUCIBLE'
)
).addOption(
new Option(
jkowalleck marked this conversation as resolved.
Show resolved Hide resolved
'--gather-license-texts',
'Search for license files in components and include them as license evidence.\n' +
'This feature is experimental. (default: false)'
).default(false)
).addOption(
(function () {
const o = new Option(
Expand Down Expand Up @@ -249,7 +256,8 @@ export async function run (process: NodeJS.Process): Promise<number> {
omitDependencyTypes: options.omit,
reproducible: options.outputReproducible,
flattenComponents: options.flattenComponents,
shortPURLs: options.shortPURLs
shortPURLs: options.shortPURLs,
gatherLicenseTexts: options.gatherLicenseTexts
},
myConsole
).buildFromProjectDir(projectDir, process)
Expand Down