-
-
Couldn't load subscription status.
- Fork 14
feat: add license file gatherer utility #1249
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
Merged
Merged
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 a2da270 feat: license file gathering
jkowalleck 028dc6c feat: license file gathering
jkowalleck 51da56a feat: license file gathering
jkowalleck e096713 feat: license file gathering
jkowalleck b8d2fd0 feat: license file gathering
jkowalleck 626fdb1 feat: license file gathering
jkowalleck 49a08c6 feat: license file gathering
jkowalleck 54e22ed feat: license file gathering
jkowalleck a1a7b81 feat: license file gathering
jkowalleck d754f77 feat: license file gathering
jkowalleck 110fb06 feat: license file gathering
jkowalleck dffd67c feat: license file gathering
jkowalleck 8bbbdfe wip
jkowalleck 7d7c112 wip
jkowalleck 9681a3a wip
jkowalleck 3dabf6d wip
jkowalleck cf80a9d Merge remote-tracking branch 'origin/main' into feat/license-file-gat…
jkowalleck 7c516b4 wip
jkowalleck 1dcad1a wip
jkowalleck c17b4bc wip
jkowalleck 2205fee wip
jkowalleck 61bf083 wip
jkowalleck 706918d wip
jkowalleck 4813b25 wip
jkowalleck f42a690 wip
jkowalleck fe64d90 wip
jkowalleck f1d02ec tests
jkowalleck 2296edf tests
jkowalleck 0fedd41 tests
jkowalleck a707089 tests
jkowalleck 5aa4057 tests
jkowalleck 2b86c5e tests
jkowalleck d97ae31 tests
jkowalleck 3f2bf42 tests
jkowalleck 174232e tests
jkowalleck 7e0bbc3 tests
jkowalleck 74db0a4 tests
jkowalleck fd30cce tests
jkowalleck 7d793ef tests
jkowalleck fdf0ad7 docs
jkowalleck 37857b9 docs
jkowalleck 8fde217 docs
jkowalleck 7ef897d Merge remote-tracking branch 'origin/main' into feat/license-file-gat…
jkowalleck 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
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| 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] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| 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})) | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.