Skip to content
Draft
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
43 changes: 25 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"open": "^11.0.0",
"prompt-sync": "^4.2.0",
"run-script-os": "^1.1.6",
"sanitize-filename": "^1.6.4",
"table": "^6.8.1",
"tmp": "^0.2.1",
"tsimportlib": "^0.0.5",
Expand Down
5 changes: 2 additions & 3 deletions src/commands/attest/apply.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import fs from 'fs';
import path from 'path';
import { Flags } from '@oclif/core';
import _ from 'lodash';

Check warning on line 3 in src/commands/attest/apply.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Import lodash methods from subpaths such as "lodash/map" to avoid importing more code than needed.

See more on https://sonarcloud.io/project/issues?id=mitre_saf&issues=AZ84vUkS0PyIudG5zIKi&open=AZ84vUkS0PyIudG5zIKi&pullRequest=8480
import type { ExecJSON } from 'inspecjs';
import { addAttestationToHDF, parseXLSXAttestations, type Attestation } from '@mitre/hdf-converters';
import yaml from 'yaml';
import { basename } from '../../utils/global';
import { basename, resolveSafeChild, safeFilename } from '../../utils/global';
import { BaseCommand } from '../../utils/oclif/base_command';

export default class ApplyAttestation extends BaseCommand<typeof ApplyAttestation> {
Expand Down Expand Up @@ -74,7 +73,7 @@
if (Object.entries(executions).length <= 1) {
fs.writeFileSync(flags.output, JSON.stringify(applied, null, 2));
} else {
fs.writeFileSync(path.join(flags.output, originalFilename), JSON.stringify(applied, null, 2));
fs.writeFileSync(resolveSafeChild(flags.output, safeFilename(originalFilename)), JSON.stringify(applied, null, 2));
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/commands/convert/asff2hdf.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import fs from 'fs';
import path from 'path';
import {
type AwsSecurityFindingFilters,
type DescribeStandardsControlsCommandOutput,
Expand All @@ -13,8 +12,8 @@
import { ASFFResults as Mapper, INPUT_TYPES } from '@mitre/hdf-converters';
import { NodeHttpHandler } from '@smithy/node-http-handler';
import https from 'https';
import _ from 'lodash';

Check warning on line 15 in src/commands/convert/asff2hdf.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Import lodash methods from subpaths such as "lodash/map" to avoid importing more code than needed.

See more on https://sonarcloud.io/project/issues?id=mitre_saf&issues=AZ84vUhZ0PyIudG5zIKc&open=AZ84vUhZ0PyIudG5zIKc&pullRequest=8480
import { basename, checkInput, checkSuffix } from '../../utils/global';
import { checkInput, checkSuffix, resolveSafeChild, safeFilename } from '../../utils/global';
import { createWinstonLogger } from '../../utils/logging';
import { BaseCommand } from '../../utils/oclif/base_command';

Expand Down Expand Up @@ -289,7 +288,7 @@
fs.mkdirSync(flags.output);
_.forOwn(results, (result, filename) => {
fs.writeFileSync(
path.join(flags.output, checkSuffix(basename(filename))),
resolveSafeChild(flags.output, safeFilename(checkSuffix(filename))),
JSON.stringify(result, null, 2),
);
});
Expand Down
5 changes: 2 additions & 3 deletions src/commands/convert/ckl2poam.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { mkdir, readFile } from 'fs/promises';
import path from 'path';
import { Flags } from '@oclif/core';
import { XMLParser } from 'fast-xml-parser';
import _ from 'lodash';

Check warning on line 4 in src/commands/convert/ckl2poam.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Import lodash methods from subpaths such as "lodash/map" to avoid importing more code than needed.

See more on https://sonarcloud.io/project/issues?id=mitre_saf&issues=AZ84vUe70PyIudG5zIKW&open=AZ84vUe70PyIudG5zIKW&pullRequest=8480
import moment from 'moment';
import promptSync from 'prompt-sync';
import XlsxPopulate from 'xlsx-populate';
Expand All @@ -23,7 +22,7 @@
extractSTIGUrl,
replaceSpecialCharacters,
} from '../../utils/ckl2poam';
import { basename, dataURLtoU8Array } from '../../utils/global';
import { basename, dataURLtoU8Array, resolveSafeChild, safeFilename } from '../../utils/global';
import { createWinstonLogger } from '../../utils/logging';
import { BaseCommand } from '../../utils/oclif/base_command';

Expand Down Expand Up @@ -318,7 +317,7 @@
currentRow += flags.rowsToSkip + 1;
}
}
return workBook.toFileAsync(path.join(flags.output, `${basename(fileName)}-${moment(new Date()).format('YYYY-MM-DD-HHmm')}.xlsm`));
return workBook.toFileAsync(resolveSafeChild(flags.output, safeFilename(`${basename(fileName)}-${moment(new Date()).format('YYYY-MM-DD-HHmm')}.xlsm`)));
}));
}
}
5 changes: 2 additions & 3 deletions src/commands/convert/conveyor2hdf.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Flags } from '@oclif/core';
import fs from 'fs';
import { ConveyorResults as Mapper, INPUT_TYPES } from '@mitre/hdf-converters';
import { basename, checkInput, checkSuffix } from '../../utils/global';
import path from 'path';
import { checkInput, checkSuffix, resolveSafeChild, safeFilename } from '../../utils/global';
import { BaseCommand } from '../../utils/oclif/base_command';
export default class Conveyor2HDF extends BaseCommand<typeof Conveyor2HDF> {
static readonly usage
Expand Down Expand Up @@ -38,7 +37,7 @@ export default class Conveyor2HDF extends BaseCommand<typeof Conveyor2HDF> {
fs.mkdirSync(flags.output);
for (const [filename, result] of Object.entries(results)) {
fs.writeFileSync(
path.join(flags.output, checkSuffix(basename(filename))),
resolveSafeChild(flags.output, safeFilename(checkSuffix(filename))),
JSON.stringify(result, null, 2),
);
}
Expand Down
11 changes: 5 additions & 6 deletions src/commands/convert/hdf2asff.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fs from 'fs';
import https from 'https';
import path from 'path';
import {
type AwsSecurityFinding,
SecurityHub,
Expand All @@ -9,8 +8,8 @@
import { FromHdfToAsffMapper as Mapper } from '@mitre/hdf-converters';
import { Flags } from '@oclif/core';
import { NodeHttpHandler } from '@smithy/node-http-handler';
import _ from 'lodash';

Check warning on line 11 in src/commands/convert/hdf2asff.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Import lodash methods from subpaths such as "lodash/map" to avoid importing more code than needed.

See more on https://sonarcloud.io/project/issues?id=mitre_saf&issues=AZ84vUft0PyIudG5zIKX&open=AZ84vUft0PyIudG5zIKX&pullRequest=8480
import { basename, checkSuffix } from '../../utils/global';
import { basename, checkSuffix, resolveSafeChild, safeFilename } from '../../utils/global';
import { BaseCommand } from '../../utils/oclif/base_command';

export default class HDF2ASFF extends BaseCommand<typeof HDF2ASFF> {
Expand Down Expand Up @@ -104,19 +103,19 @@
const outputFolder = flags.output.replace('.json', '') || 'asff-output';
fs.mkdirSync(outputFolder);
if (convertedSlices.length === 1) {
const outfilePath = path.join(
const outfilePath = resolveSafeChild(
outputFolder,
checkSuffix(basename(flags.output)),
safeFilename(checkSuffix(flags.output)),
);
fs.writeFileSync(
outfilePath,
JSON.stringify(convertedSlices[0], null, 2),
);
} else {
for (const [index, slice] of convertedSlices.entries()) {
const outfilePath = path.join(
const outfilePath = resolveSafeChild(
outputFolder,
`${checkSuffix(basename(flags.output || '')).replace('.json', '')}.p${index}.json`,
safeFilename(`${checkSuffix(basename(flags.output || '')).replace('.json', '')}.p${index}.json`),
);
fs.writeFileSync(outfilePath, JSON.stringify(slice, null, 2));
}
Expand Down
4 changes: 2 additions & 2 deletions src/commands/convert/hdf2csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import * as stringify from 'csv-stringify';
import { EventEmitter } from 'events';
import { contextualizeEvaluation, type ContextualizedEvaluation } from 'inspecjs';
import _ from 'lodash';

Check warning on line 8 in src/commands/convert/hdf2csv.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Import lodash methods from subpaths such as "lodash/map" to avoid importing more code than needed.

See more on https://sonarcloud.io/project/issues?id=mitre_saf&issues=AZ84vUiB0PyIudG5zIKe&open=AZ84vUiB0PyIudG5zIKe&pullRequest=8480
import type { Logger } from 'winston';
import type { ControlSetRows } from '../../types/csv';
import { convertRow, csvExportFields } from '../../utils/csv';
import { basename } from '../../utils/global';
import { basename, resolveSafeChild, safeFilename } from '../../utils/global';
import { createWinstonLogger } from '../../utils/logging';
import { BaseCommand } from '../../utils/oclif/base_command';

Expand Down Expand Up @@ -86,7 +86,7 @@
if (flags.interactive) {
const interactiveFlags = await this.getFlags();
inputFile = interactiveFlags.inputFile;
outputFile = path.join(interactiveFlags.outputDirectory, interactiveFlags.outputFileName);
outputFile = resolveSafeChild(interactiveFlags.outputDirectory, safeFilename(interactiveFlags.outputFileName));
includeFields = interactiveFlags.fields.join(',');
truncateFields = Boolean(interactiveFlags.truncateFields);
} else if (this.requiredFlagsProvided(flags)) {
Expand Down Expand Up @@ -324,7 +324,7 @@

for (const [tagName, answerValue] of Object.entries(answers)) {
if (answerValue !== null) {
this.logger.info(`${tagName}=${_.isArray(answerValue) ? answerValue.join(',') : String(answerValue)}`);

Check warning on line 327 in src/commands/convert/hdf2csv.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use Array.isArray() instead of isArray() from lodash.

See more on https://sonarcloud.io/project/issues?id=mitre_saf&issues=AZ84vUiB0PyIudG5zIKf&open=AZ84vUiB0PyIudG5zIKf&pullRequest=8480
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/commands/convert/hdf2html.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs';
import path from 'path';
import { FileExportTypes, FromHDFToHTMLMapper as Mapper } from '@mitre/hdf-converters';
import { Command, Flags } from '@oclif/core';
import { basename } from '../../utils/global';

export default class HDF2HTML extends Command {
static readonly usage = 'convert hdf2html -i <hdf-scan-results-json>... -o <output-html> [-t <output-type>] [-h]';
Expand All @@ -22,7 +22,7 @@ export default class HDF2HTML extends Command {
async run() {
const { flags } = await this.parse(HDF2HTML);

const files = flags.input.map((file, i) => ({ data: fs.readFileSync(file, 'utf8'), fileName: path.basename(file), fileID: `${i}` }));
const files = flags.input.map((file, i) => ({ data: fs.readFileSync(file, 'utf8'), fileName: basename(file), fileID: `${i}` }));

const converter = await new Mapper(files, FileExportTypes[flags.type as keyof typeof FileExportTypes]).toHTML();
fs.writeFileSync(flags.output, converter);
Expand Down
20 changes: 12 additions & 8 deletions src/commands/convert/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs';
import _ from 'lodash';

Check warning on line 2 in src/commands/convert/index.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Import lodash methods from subpaths such as "lodash/map" to avoid importing more code than needed.

See more on https://sonarcloud.io/project/issues?id=mitre_saf&issues=AZ84vUV50PyIudG5zIKU&open=AZ84vUV50PyIudG5zIKU&pullRequest=8480
import path from 'path';
import {
AnchoreGrypeMapper,
Expand Down Expand Up @@ -29,7 +29,7 @@
ZapMapper,
} from '@mitre/hdf-converters';
import { Flags } from '@oclif/core';
import { basename, checkSuffix } from '../../utils/global';
import { basename, checkSuffix, resolveSafeChild, safeFilename } from '../../utils/global';
import { BaseCommand } from '../../utils/oclif/base_command';
import ASFF2HDF from './asff2hdf';
import Zap2HDF from './zap2hdf';
Expand Down Expand Up @@ -75,7 +75,7 @@
filename: basename(filePath),
});
switch (
Convert.detectedType // skipcq: JS-0047
Convert.detectedType
) {
case 'asff': {
return ASFF2HDF.flags;
Expand Down Expand Up @@ -130,7 +130,7 @@
fs.mkdirSync(flags.output);
_.forOwn(results, (result, filename) => {
fs.writeFileSync(
path.join(flags.output, checkSuffix(basename(filename))),
resolveSafeChild(flags.output, safeFilename(checkSuffix(filename))),
JSON.stringify(result, null, 2),
);
});
Expand Down Expand Up @@ -161,7 +161,7 @@
fs.mkdirSync(flags.output);
for (const [filename, result] of Object.entries(results)) {
fs.writeFileSync(
path.join(flags.output, checkSuffix(basename(filename))),
resolveSafeChild(flags.output, safeFilename(checkSuffix(filename))),
JSON.stringify(result, null, 2),
);
}
Expand Down Expand Up @@ -241,9 +241,11 @@
const result = converter.toHdf();
const pluralResults = Array.isArray(result) ? result : [];
const singularResult = pluralResults.length === 0;
const outputBase = path.dirname(flags.output);
const outputPrefix = safeFilename(flags.output.replaceAll(/\.json/gi, ''));
for (const element of pluralResults) {
fs.writeFileSync(
`${flags.output.replaceAll(/\.json/gi, '')}-${basename(_.get(element, 'platform.target_id') || '')}.json`,
resolveSafeChild(outputBase, safeFilename(`${outputPrefix}-${basename(_.get(element, 'platform.target_id') || '')}.json`)),
JSON.stringify(element, null, 2),
);
}
Expand Down Expand Up @@ -294,9 +296,9 @@
fs.mkdirSync(flags.output);
_.forOwn(results, (result) => {
fs.writeFileSync(
path.join(
resolveSafeChild(
flags.output,
basename(`${_.get(result, 'platform.target_id')}.json`),
safeFilename(`${_.get(result, 'platform.target_id')}.json`),
),
JSON.stringify(result, null, 2),
);
Expand Down Expand Up @@ -327,9 +329,11 @@
const result = converter.toHdf();
const pluralResults = Array.isArray(result) ? result : [];
const singularResult = pluralResults.length === 0;
const outputBase = path.dirname(flags.output);
const outputPrefix = safeFilename(flags.output.replaceAll(/\.json/gi, ''));
for (const element of pluralResults) {
fs.writeFileSync(
`${flags.output.replaceAll(/\.json/gi, '')}-${basename(_.get(element, 'platform.target_id') || '')}.json`,
resolveSafeChild(outputBase, safeFilename(`${outputPrefix}-${basename(_.get(element, 'platform.target_id') || '')}.json`)),
JSON.stringify(element, null, 2),
);
}
Expand Down
Loading
Loading