@mitre/inspec-objects 1.0.1 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,12 +1,21 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.processInSpecProfile = exports.processExecJSON = exports.processProfileJSON = exports.processEvaluation = void 0;
3
+ exports.processEvaluation = processEvaluation;
4
+ exports.processProfileJSON = processProfileJSON;
5
+ exports.processExecJSON = processExecJSON;
6
+ exports.processInSpecProfile = processInSpecProfile;
4
7
  const tslib_1 = require("tslib");
5
8
  const inspecjs_1 = require("inspecjs");
6
9
  const lodash_1 = tslib_1.__importDefault(require("lodash"));
7
10
  const control_1 = tslib_1.__importStar(require("../objects/control"));
8
11
  const profile_1 = tslib_1.__importDefault(require("../objects/profile"));
9
12
  const update_1 = require("../utilities/update");
13
+ /**
14
+ * Processes a contextualized evaluation input and converts it into a Profile object.
15
+ *
16
+ * @param evaluationInput - The contextualized evaluation input containing profile and control data.
17
+ * @returns A Profile object populated with the data from the evaluation input.
18
+ */
10
19
  function processEvaluation(evaluationInput) {
11
20
  const topLevelProfile = evaluationInput.contains[0];
12
21
  const profile = new profile_1.default({
@@ -32,7 +41,12 @@ function processEvaluation(evaluationInput) {
32
41
  });
33
42
  return profile;
34
43
  }
35
- exports.processEvaluation = processEvaluation;
44
+ /**
45
+ * Processes a contextualized profile JSON object and converts it into a Profile instance.
46
+ *
47
+ * @param profileInput - The contextualized profile input containing profile data and controls.
48
+ * @returns A Profile instance populated with the data from the input.
49
+ */
36
50
  function processProfileJSON(profileInput) {
37
51
  const profile = new profile_1.default({
38
52
  name: profileInput.data.name,
@@ -71,11 +85,29 @@ function processProfileJSON(profileInput) {
71
85
  });
72
86
  return profile;
73
87
  }
74
- exports.processProfileJSON = processProfileJSON;
88
+ /**
89
+ * Processes the given ExecJSON execution object and returns a Profile.
90
+ *
91
+ * This function takes an ExecJSON execution object, contextualizes the evaluation,
92
+ * and then processes the evaluation to produce a Profile.
93
+ *
94
+ * @param execJSON - The ExecJSON execution object to be processed.
95
+ * @returns The processed Profile.
96
+ */
75
97
  function processExecJSON(execJSON) {
76
98
  return processEvaluation((0, inspecjs_1.contextualizeEvaluation)(execJSON));
77
99
  }
78
- exports.processExecJSON = processExecJSON;
100
+ /**
101
+ * Processes an InSpec profile from a JSON string.
102
+ *
103
+ * This function takes a JSON string representing an InSpec profile, converts it,
104
+ * and processes it to return a `Profile` object. It handles different versions
105
+ * of the InSpec JSON format and sorts the controls by their ID.
106
+ *
107
+ * @param json - The JSON string representing the InSpec profile.
108
+ * @returns A `Profile` object containing the processed profile data.
109
+ * @throws Will throw an error if the JSON string does not match known InSpec formats.
110
+ */
79
111
  function processInSpecProfile(json) {
80
112
  const convertedFile = (0, inspecjs_1.convertFile)(json, true);
81
113
  let profile = new profile_1.default();
@@ -91,4 +123,3 @@ function processInSpecProfile(json) {
91
123
  profile.controls = lodash_1.default.sortBy(profile.controls, 'id');
92
124
  return profile;
93
125
  }
94
- exports.processInSpecProfile = processInSpecProfile;
@@ -1,3 +1,31 @@
1
1
  import { OvalDefinitionValue, DefinitionCriterion } from '../types/oval';
2
+ /**
3
+ * Extracts all test references from a list of initial criteria.
4
+ *
5
+ * This function recursively traverses the provided criteria and extracts
6
+ * all test references (`@_test_ref`) from each criterion. It returns an
7
+ * array of all found test references.
8
+ *
9
+ * @param initialCriteria - An array of `DefinitionCriterion` objects to extract test references from.
10
+ * @returns An array of strings containing all extracted test references.
11
+ */
2
12
  export declare function extractAllCriteriaRefs(initialCriteria: DefinitionCriterion[]): string[];
13
+ /**
14
+ * Processes an OVAL (Open Vulnerability and Assessment Language) XML string and converts it into a JSON object.
15
+ * Extracts definitions and their associated criteria references and resolved values.
16
+ * The function performs the following steps:
17
+ * 1. Converts the OVAL XML string into a JSON object.
18
+ * 2. Iterates through the OVAL definitions and extracts each definition.
19
+ * 3. For each definition, extracts criteria references and resolves the associated objects and states.
20
+ * 4. Logs warnings if any objects or states cannot be found.
21
+ *
22
+ * The returned record contains:
23
+ * - The original definition.
24
+ * - An array of criteria references.
25
+ * - An array of resolved values, each containing the original criteria, resolved objects, and resolved states.
26
+ *
27
+ * @param {string} [oval] - The OVAL XML string to be processed. If not provided, the function returns `undefined`.
28
+ * @returns {Record<string, OvalDefinitionValue> | undefined} - A record of extracted definitions with their
29
+ * criteria references and resolved values, or `undefined` if no OVAL string is provided.
30
+ */
3
31
  export declare function processOVAL(oval?: string): Record<string, OvalDefinitionValue> | undefined;
@@ -1,9 +1,23 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.processOVAL = exports.extractAllCriteriaRefs = void 0;
3
+ exports.extractAllCriteriaRefs = extractAllCriteriaRefs;
4
+ exports.processOVAL = processOVAL;
4
5
  const xccdf_1 = require("../utilities/xccdf");
5
6
  const logging_1 = require("../utilities/logging");
6
- // https://stackoverflow.com/questions/9133500/how-to-find-a-node-in-a-tree-with-javascript
7
+ /**
8
+ * Search through all arrays of the tree to find a value from a property
9
+ * Code provided by:
10
+ * https://stackoverflow.com/questions/9133500/how-to-find-a-node-in-a-tree-with-javascript
11
+ *
12
+ * @param aTree : The tree array
13
+ * @param fCompair : This function will receive each node. Define based on caller for specific
14
+ condition necessary for the match. It must return true if the condition
15
+ is matched. Example:
16
+ function(oNode){ if(oNode["Name"] === "AA") return true; }
17
+ * @param bGreedy? : Set to `true` to search all node and not stopping after the first match, default is false
18
+ * @return An array with references to the nodes for which fCompair was true. In case no node was found an empty array
19
+ * will be returned
20
+ */
7
21
  function searchTree(aTree, fCompair, bGreedy) {
8
22
  let oNode; // always the current node
9
23
  const aInnerTree = []; // will contain the inner children
@@ -36,6 +50,16 @@ function searchTree(aTree, fCompair, bGreedy) {
36
50
  }
37
51
  return aReturnNodes;
38
52
  }
53
+ /**
54
+ * Extracts all test references from a list of initial criteria.
55
+ *
56
+ * This function recursively traverses the provided criteria and extracts
57
+ * all test references (`@_test_ref`) from each criterion. It returns an
58
+ * array of all found test references.
59
+ *
60
+ * @param initialCriteria - An array of `DefinitionCriterion` objects to extract test references from.
61
+ * @returns An array of strings containing all extracted test references.
62
+ */
39
63
  function extractAllCriteriaRefs(initialCriteria) {
40
64
  const criteriaRefs = [];
41
65
  initialCriteria.forEach(criteria => {
@@ -51,14 +75,31 @@ function extractAllCriteriaRefs(initialCriteria) {
51
75
  });
52
76
  return criteriaRefs;
53
77
  }
54
- exports.extractAllCriteriaRefs = extractAllCriteriaRefs;
78
+ /**
79
+ * Processes an OVAL (Open Vulnerability and Assessment Language) XML string and converts it into a JSON object.
80
+ * Extracts definitions and their associated criteria references and resolved values.
81
+ * The function performs the following steps:
82
+ * 1. Converts the OVAL XML string into a JSON object.
83
+ * 2. Iterates through the OVAL definitions and extracts each definition.
84
+ * 3. For each definition, extracts criteria references and resolves the associated objects and states.
85
+ * 4. Logs warnings if any objects or states cannot be found.
86
+ *
87
+ * The returned record contains:
88
+ * - The original definition.
89
+ * - An array of criteria references.
90
+ * - An array of resolved values, each containing the original criteria, resolved objects, and resolved states.
91
+ *
92
+ * @param {string} [oval] - The OVAL XML string to be processed. If not provided, the function returns `undefined`.
93
+ * @returns {Record<string, OvalDefinitionValue> | undefined} - A record of extracted definitions with their
94
+ * criteria references and resolved values, or `undefined` if no OVAL string is provided.
95
+ */
55
96
  function processOVAL(oval) {
56
97
  var _a;
57
- const logger = (0, logging_1.createWinstonLogger)();
98
+ const logger = (0, logging_1.createWinstonLogger)('ts-inspec-objects');
58
99
  if (!oval) {
59
100
  return undefined;
60
101
  }
61
- const parsed = (0, xccdf_1.convertEncodedXmlIntoJson)(oval);
102
+ const parsed = (0, xccdf_1.convertEncodedXmlIntoJson)(oval, 'withArrayNoEntitiesOption');
62
103
  const extractedDefinitions = {};
63
104
  for (const ovalDefinitions of parsed.oval_definitions) {
64
105
  for (const definitionList of ovalDefinitions.definitions) {
@@ -114,4 +155,3 @@ function processOVAL(oval) {
114
155
  }
115
156
  return extractedDefinitions;
116
157
  }
117
- exports.processOVAL = processOVAL;
@@ -1,11 +1,46 @@
1
1
  import Profile from '../objects/profile';
2
2
  import { BenchmarkGroup, BenchmarkRule, RuleComplexCheck } from '../types/xccdf';
3
3
  import { OvalDefinitionValue } from '../types/oval';
4
- export declare type GroupContextualizedRule = BenchmarkRule & {
4
+ export type GroupContextualizedRule = BenchmarkRule & {
5
5
  group: Omit<BenchmarkGroup, 'Rule' | 'Group'>;
6
6
  };
7
+ /**
8
+ * Extracts all rules from the given benchmark groups, including nested groups.
9
+ *
10
+ * @param groups - An array of benchmark groups to extract rules from.
11
+ * @returns An array of contextualized rules, each rule includes its parent group context.
12
+ */
7
13
  export declare function extractAllRules(groups: BenchmarkGroup[]): GroupContextualizedRule[];
14
+ /**
15
+ * Extracts all nested complex checks from a given `RuleComplexCheck` object.
16
+ *
17
+ * This function recursively traverses the `complex-check` property of the input
18
+ * `RuleComplexCheck` object and collects all nested complex checks into a flat array.
19
+ * Each complex check in the resulting array will have its own `complex-check` property omitted.
20
+ *
21
+ * @param complexCheck - The `RuleComplexCheck` object to extract complex checks from.
22
+ * @returns An array of `RuleComplexCheck` objects with the `complex-check` property omitted.
23
+ */
8
24
  export declare function extractAllComplexChecks(complexCheck: RuleComplexCheck): Omit<RuleComplexCheck, 'complex-check'>[];
25
+ export type InputTextLang = {
26
+ '#text': string;
27
+ '@_lang': string;
28
+ };
29
+ /**
30
+ * Processes an XCCDF XML string and converts it into a Profile object.
31
+ * NOTE: We are using the fast xml parser (FXP) V4 which requires to specify
32
+ * which Whether a single tag should be parsed as an array or an object,
33
+ * it can't be decided by FXP. We process every tag as an array, this is
34
+ * the reason there are numerous tag test, were array index zero [0] is
35
+ * tested.
36
+ *
37
+ * @param xml - The XCCDF XML string to process.
38
+ * @param removeNewlines - A flag indicating whether to remove newlines from the processed data.
39
+ * @param useRuleId - Specifies the rule ID format to use. Can be 'group', 'rule', 'version', or 'cis'.
40
+ * @param ovalDefinitions - Optional OVAL definitions to use for resolving values.
41
+ * @returns A Profile object representing the processed XCCDF data.
42
+ * @throws Will throw an error if the XCCDF file is not properly formatted or if required data is missing.
43
+ */
9
44
  export declare function processXCCDF(xml: string, removeNewlines: false, useRuleId: 'group' | 'rule' | 'version' | 'cis', ovalDefinitions?: Record<string, OvalDefinitionValue & {
10
45
  criteriaRefs?: string[];
11
46
  resolvedValues?: any;