Skip to content

Commit

Permalink
feat: hardened JSON imports (#1132)
Browse files Browse the repository at this point in the history
---------

Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
  • Loading branch information
jkowalleck committed Dec 8, 2023
1 parent 5c8bf59 commit 0d684b9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.

## unreleased

* Changed
* Hardened JSON imports (via [#1132])

[#1132]: https://github.com/CycloneDX/cyclonedx-node-npm/pull/1132

## 1.14.3 - 2023-12-01

* Fixed
Expand Down
27 changes: 27 additions & 0 deletions src/_helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*!
This file is part of CycloneDX generator for NPM projects.
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 { readFileSync } from 'fs'

export function loadJsonFile (path: string): any {
return JSON.parse(readFileSync(path, 'utf8'))
// may be replaced by `require(f, { with: { type: "json" } })`
// as soon as this spec is properly implemented.
// see https://github.com/tc39/proposal-import-attributes
}
10 changes: 4 additions & 6 deletions src/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import * as normalizePackageData from 'normalize-package-data'
import { type PackageURL } from 'packageurl-js'
import * as path from 'path'

import { loadJsonFile } from './_helpers'
import { makeNpmRunner, type runFunc } from './npmRunner'
import { PropertyNames, PropertyValueBool } from './properties'
import { versionCompare } from './versionCompare'
Expand Down Expand Up @@ -350,8 +351,7 @@ export class BomBuilder {
const packageJsonPath = path.join(data.path, 'package.json')
try {
return Object.assign(
/* eslint-disable-next-line @typescript-eslint/no-var-requires */
require(packageJsonPath),
loadJsonFile(packageJsonPath),
data
)
} catch {
Expand Down Expand Up @@ -568,8 +568,7 @@ export class BomBuilder {
}

private * makeTools (): Generator<Models.Tool> {
/* eslint-disable-next-line @typescript-eslint/no-var-requires */
const packageJsonPaths = ['../package.json']
const packageJsonPaths = [path.resolve(module.path, '..', 'package.json')]

const libs = [
'@cyclonedx/cyclonedx-library'
Expand All @@ -589,8 +588,7 @@ export class BomBuilder {
/* eslint-enable no-labels */

for (const packageJsonPath of packageJsonPaths) {
/* eslint-disable-next-line @typescript-eslint/no-var-requires */
const packageData = require(packageJsonPath)
const packageData = loadJsonFile(packageJsonPath)
normalizePackageData(packageData /* add debug for warnings? */)
const tool = this.toolBuilder.makeTool(packageData)
if (tool !== undefined) {
Expand Down
4 changes: 2 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { Argument, Command, Option } from 'commander'
import { existsSync, openSync, writeSync } from 'fs'
import { dirname, resolve } from 'path'

import { loadJsonFile } from './_helpers'
import { BomBuilder, TreeBuilder } from './builders'

enum OutputFormat {
Expand Down Expand Up @@ -173,8 +174,7 @@ function makeCommand (process: NodeJS.Process): Command {
)
).version(
// that is supposed to be the last option in the list on the help page.
/* eslint-disable-next-line @typescript-eslint/no-var-requires */
require('../package.json').version as string
loadJsonFile(resolve(module.path, '..', 'package.json')).version as string
).allowExcessArguments(
false
)
Expand Down

0 comments on commit 0d684b9

Please sign in to comment.