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: hardened JSON imports #1242

Merged
merged 2 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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 [#1242])

[#1242]: https://github.com/CycloneDX/cyclonedx-webpack-plugin/pull/1242

## 3.8.3 - 2023-12-01

* Build
Expand Down
11 changes: 9 additions & 2 deletions src/_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ SPDX-License-Identifier: Apache-2.0
Copyright (c) OWASP Foundation. All Rights Reserved.
*/

import { existsSync } from 'fs'
import { existsSync, readFileSync } from 'fs'
import { dirname, isAbsolute, join } from 'path'

export interface PackageDescription {
Expand All @@ -32,7 +32,7 @@ export function getPackageDescription (path: string): PackageDescription | undef
try {
return {
path: packageJson,
packageJson: require(packageJson)
packageJson: loadJsonFile(packageJson)
}
} catch {
return undefined
Expand All @@ -47,3 +47,10 @@ export function getPackageDescription (path: string): PackageDescription | undef
}
return undefined
}

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
}
8 changes: 3 additions & 5 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import * as normalizePackageJson from 'normalize-package-data'
import { join as joinPath, resolve } from 'path'
import { Compilation, type Compiler, sources } from 'webpack'

import { getPackageDescription } from './_helpers'
import { getPackageDescription, loadJsonFile } from './_helpers'
import { Extractor } from './extractor'

type WebpackLogger = Compilation['logger']
Expand Down Expand Up @@ -333,8 +333,7 @@ export class CycloneDxWebpackPlugin {
}

* #makeTools (builder: CDX.Builders.FromNodePackageJson.ToolBuilder, logger: WebpackLogger): Generator<CDX.Models.Tool> {
/* eslint-disable-next-line @typescript-eslint/no-var-requires */
const packageJsonPaths = ['../package.json']
const packageJsonPaths = [resolve(module.path, '..', 'package.json')]

const libs = [
'@cyclonedx/cyclonedx-library'
Expand All @@ -355,8 +354,7 @@ export class CycloneDxWebpackPlugin {

for (const packageJsonPath of packageJsonPaths) {
logger.log('try to build new Tool from PkgPath', packageJsonPath)
/* eslint-disable-next-line @typescript-eslint/no-var-requires */
const packageJson = require(packageJsonPath)
const packageJson = loadJsonFile(packageJsonPath)
normalizePackageJson(packageJson, w => { logger.debug('normalizePackageJson from PkgPath', packageJsonPath, 'caused:', w) })
const tool = builder.makeTool(packageJson)
if (tool !== undefined) {
Expand Down