Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
2188ac3
feat: license file gathering
jkowalleck Jun 10, 2025
a2da270
feat: license file gathering
jkowalleck Jun 10, 2025
028dc6c
feat: license file gathering
jkowalleck Jun 10, 2025
51da56a
feat: license file gathering
jkowalleck Jun 10, 2025
e096713
feat: license file gathering
jkowalleck Jun 10, 2025
b8d2fd0
feat: license file gathering
jkowalleck Jun 10, 2025
626fdb1
feat: license file gathering
jkowalleck Jun 10, 2025
49a08c6
feat: license file gathering
jkowalleck Jun 10, 2025
54e22ed
feat: license file gathering
jkowalleck Jun 10, 2025
a1a7b81
feat: license file gathering
jkowalleck Jun 10, 2025
d754f77
feat: license file gathering
jkowalleck Jun 10, 2025
110fb06
feat: license file gathering
jkowalleck Jun 10, 2025
dffd67c
feat: license file gathering
jkowalleck Jun 10, 2025
8bbbdfe
wip
jkowalleck Jun 11, 2025
7d7c112
wip
jkowalleck Jun 11, 2025
9681a3a
wip
jkowalleck Jun 11, 2025
3dabf6d
wip
jkowalleck Jun 11, 2025
cf80a9d
Merge remote-tracking branch 'origin/main' into feat/license-file-gat…
jkowalleck Jun 11, 2025
7c516b4
wip
jkowalleck Jun 11, 2025
1dcad1a
wip
jkowalleck Jun 11, 2025
c17b4bc
wip
jkowalleck Jun 11, 2025
2205fee
wip
jkowalleck Jun 11, 2025
61bf083
wip
jkowalleck Jun 11, 2025
706918d
wip
jkowalleck Jun 12, 2025
4813b25
wip
jkowalleck Jun 12, 2025
f42a690
wip
jkowalleck Jun 12, 2025
fe64d90
wip
jkowalleck Jun 12, 2025
f1d02ec
tests
jkowalleck Jun 12, 2025
2296edf
tests
jkowalleck Jun 12, 2025
0fedd41
tests
jkowalleck Jun 12, 2025
a707089
tests
jkowalleck Jun 12, 2025
5aa4057
tests
jkowalleck Jun 12, 2025
2b86c5e
tests
jkowalleck Jun 12, 2025
d97ae31
tests
jkowalleck Jun 12, 2025
3f2bf42
tests
jkowalleck Jun 12, 2025
174232e
tests
jkowalleck Jun 12, 2025
7e0bbc3
tests
jkowalleck Jun 12, 2025
74db0a4
tests
jkowalleck Jun 12, 2025
fd30cce
tests
jkowalleck Jun 12, 2025
7d793ef
tests
jkowalleck Jun 12, 2025
fdf0ad7
docs
jkowalleck Jun 12, 2025
37857b9
docs
jkowalleck Jun 12, 2025
8fde217
docs
jkowalleck Jun 12, 2025
7ef897d
Merge remote-tracking branch 'origin/main' into feat/license-file-gat…
jkowalleck Jun 12, 2025
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/_helpers/mime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*!
This file is part of CycloneDX JavaScript Library.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

SPDX-License-Identifier: Apache-2.0
Copyright (c) OWASP Foundation. All Rights Reserved.
*/

import {parse} from 'node:path'

type MimeType = string

const MIME_TEXT_PLAIN: MimeType = 'text/plain'

const MAP_TEXT_EXTENSION_MIME: Readonly<Record<string, MimeType>> = {
'': MIME_TEXT_PLAIN,
// https://www.iana.org/assignments/media-types/media-types.xhtml
'.csv': 'text/csv',
'.htm': 'text/html',
'.html': 'text/html',
'.md': 'text/markdown',
'.txt': MIME_TEXT_PLAIN,
'.rst': 'text/prs.fallenstein.rst',
'.xml': 'text/xml', // not `application/xml` -- our scope is text!
// add more mime types above this line. pull-requests welcome!
// license-specific files
'.license': MIME_TEXT_PLAIN,
'.licence': MIME_TEXT_PLAIN
} as const

const LICENSE_FILENAME_BASE = new Set(['licence', 'license'])
const LICENSE_FILENAME_EXT = new Set([
'.apache',
'.bsd',
'.gpl',
'.mit'
])

export function getMimeForLicenseFile(filename: string): MimeType | undefined {
const {name, ext} = parse(filename.toLowerCase())
return LICENSE_FILENAME_BASE.has(name) && LICENSE_FILENAME_EXT.has(ext)
? MIME_TEXT_PLAIN
: MAP_TEXT_EXTENSION_MIME[ext]
}
1 change: 1 addition & 0 deletions src/builders/index.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ Copyright (c) OWASP Foundation. All Rights Reserved.
*/

export * as FromNodePackageJson from './fromNodePackageJson.node'
export * as License from './license'
103 changes: 103 additions & 0 deletions src/builders/license.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*!
This file is part of CycloneDX JavaScript Library.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

SPDX-License-Identifier: Apache-2.0
Copyright (c) OWASP Foundation. All Rights Reserved.
*/

import type NATIVE_FS from 'node:fs'
import type NATIVE_PATH from "node:path";

import {getMimeForLicenseFile} from '../_helpers/mime'
import {AttachmentEncoding} from '../enums/attachmentEncoding'
import {Attachment} from '../models/attachment'


interface FsUtils {
readdirSync: typeof NATIVE_FS.readdirSync
readFileSync: typeof NATIVE_FS.readFileSync
statSync: typeof NATIVE_FS.statSync
}

interface PathUtils {
join: typeof NATIVE_PATH.join
}

export interface FetchResult {
file: string
filePath: string
text: Attachment
}

const LICENSE_FILENAME_PATTERN = /^(?:UN)?LICEN[CS]E|.\.LICEN[CS]E$|^NOTICE$/i

type ErrorReporter = (e:Error) => void

/* eslint-disable-next-line @typescript-eslint/no-empty-function -- ack */
function noop ():void {}

export class LicenseEvidenceFetcher {
readonly #fs: FsUtils
readonly #path: PathUtils

/**
* `fs` and `path` can be supplied, if any compatibility-layer or drop-in replacement is used.
*
* @param options.fs - If omitted, the native `node:fs` is used.
* @param options.path - If omitted, the native `node:path` is used.
*/
constructor(options: { fs?: FsUtils, path?: PathUtils } = {}) {
/* eslint-disable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-require-imports -- needed */
this.#fs = options.fs ?? require('node:fs')
this.#path = options.path ?? require('node:path')
/* eslint-enable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-require-imports */
}

* fetch(prefixPath: string, onError: ErrorReporter = noop): Generator<FetchResult> {
const files = this.#fs.readdirSync(prefixPath)
for (const file of files) {
if (!LICENSE_FILENAME_PATTERN.test(file)) {
continue
}
const filePath = this.#path.join(prefixPath, file)
if (!this.#fs.statSync(filePath).isFile()) {
// Ignore all directories - they are not files :-)
// Don't follow symlinks for security reasons!
continue
}
const contentType = getMimeForLicenseFile(file)
if (contentType === undefined) {
continue
}
try {
yield {
file,
filePath,
text: new Attachment(
this.#fs.readFileSync(filePath).toString('base64'),
{
contentType,
encoding: AttachmentEncoding.Base64
}
)
}
}
/* c8 ignore next 3 */
catch (e) {
onError(new Error(`skipped license file ${filePath}`, {cause: e}))
}
}
}
}
Loading