Skip to content

Commit

Permalink
checklist metadata input validation on export (#5902)
Browse files Browse the repository at this point in the history
* checklist metadata input validation on export

Signed-off-by: kemley76 <[email protected]>

* update lock file

* added @types/validator

Signed-off-by: kemley76 <[email protected]>

* move throwIfInvalid() to not be inline

Signed-off-by: kemley76 <[email protected]>

* remove @types/validator from devDependencies

Signed-off-by: kemley76 <[email protected]>

* validate all metadata fields on checklist metadata

Signed-off-by: kemley76 <[email protected]>

* Merge branch 'master' into ckl-metadata-validation

Signed-off-by: kemley76 <[email protected]>

* refactor nested template strings

Signed-off-by: kemley76 <[email protected]>

* refactor checklist metadata validation assertions

Signed-off-by: kemley76 <[email protected]>

* cleaned up input validation error messaging on ckl export modal

Signed-off-by: Kaden Emley <[email protected]>

* restrict number inputs to positive integers in ckl export modal

Signed-off-by: Kaden Emley <[email protected]>

* added combined ckl metadata validation methods into single helper method

Signed-off-by: Kaden Emley <[email protected]>

* remove repeated imports of the same file

Signed-off-by: Kaden Emley <[email protected]>

* Merge branch 'master' into ckl-metadata-validation

Signed-off-by: Kaden Emley <[email protected]>

* minor formatting change

Signed-off-by: Kaden Emley <[email protected]>

* make checklist metadata date validator accept more formats

Signed-off-by: Kaden Emley <[email protected]>

* swapped error message for format hint in ckl export modal

Signed-off-by: Kaden Emley <[email protected]>

* move vul id mapping info icon to allow dropdown arrow to show

Signed-off-by: Kaden Emley <[email protected]>

* refine integer constraints on checklist export modal fields

Signed-off-by: Kaden Emley <[email protected]>

* move metadata validation functions into util file

Signed-off-by: Kaden Emley <[email protected]>

* add test to ensure errors are thrown upon using invalid checklist metadata

Signed-off-by: Kaden Emley <[email protected]>

* remove unused import

Signed-off-by: Kaden Emley <[email protected]>

---------

Signed-off-by: kemley76 <[email protected]>
Signed-off-by: Kaden Emley <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
kemley76 and mergify[bot] authored Jul 11, 2024
1 parent e01b077 commit 63b42a3
Show file tree
Hide file tree
Showing 12 changed files with 786 additions and 28 deletions.
100 changes: 78 additions & 22 deletions apps/frontend/src/components/global/ExportCKLModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,37 @@
<v-text-field
v-model="file.hostip"
label="Host IP"
:rules="[validateIpAddress]"
:error-messages="
validateFormat(
$v.files.$each[index].hostip,
'###.###.###.###'
)
"
hint="###.###.###.###"
class="pr-2"
/>
<v-text-field
v-model="file.hostmac"
label="Host MAC"
:rules="[validateMacAddress]"
:error-messages="
validateFormat(
$v.files.$each[index].hostmac,
'XX:XX:XX:XX:XX:XX'
)
"
hint="XX:XX:XX:XX:XX:XX"
class="pr-2"
/>
<v-text-field
v-model="file.hostfqdn"
label="Host FQDN"
:error-messages="
validateFormat(
$v.files.$each[index].hostfqdn,
'[hostname].[domain].[tld]'
)
"
hint="[hostname].[domain].[tld]"
class="pr-2"
/>
<v-text-field
Expand Down Expand Up @@ -107,7 +124,7 @@
label="Vul ID Mapping"
class="pr-2"
>
<v-tooltip slot="append" color="#332E2E" bottom>
<v-tooltip slot="prepend-inner" color="#332E2E" bottom>
<template #activator="{on}">
<v-icon color="primary" v-on="on"
>mdi-information-variant-circle</v-icon
Expand Down Expand Up @@ -177,14 +194,14 @@
class="pr-2"
/>
<v-text-field
v-model="profile.version"
v-model.number="profile.version"
label="Version"
type="number"
:placeholder="profile.versionplaceholder"
class="pr-2"
/>
<v-text-field
v-model="profile.releasenumber"
v-model.number="profile.releasenumber"
label="Release Number"
type="number"
:placeholder="profile.releasenumberplaceholder"
Expand Down Expand Up @@ -253,7 +270,7 @@
<v-btn
color="primary"
text
:disabled="!selected.length"
:disabled="!selected.length || $v.$invalid"
@click="exportCKL"
>
Export
Expand All @@ -280,7 +297,8 @@ import {
StigMetadata,
Assettype,
Role,
Techarea
Techarea,
validateChecklistMetadata
} from '@mitre/hdf-converters';
import {ExecJSON} from 'inspecjs';
import {Dependency} from 'inspecjs/src/generated_parsers/v_1_0/exec-json';
Expand All @@ -290,6 +308,10 @@ import Component from 'vue-class-component';
import {Prop, Watch} from 'vue-property-decorator';
import {DateTime} from 'luxon';
import {coerce} from 'semver';
import {validationMixin} from 'vuelidate';
import {or, CustomRule} from 'vuelidate/lib/validators';
import ValidationProperties from 'vue/types/vue';
import {Result} from '@mitre/hdf-converters/src/utils/result';
type ExtendedEvaluationFile = (EvaluationFile | ProfileFile) &
ChecklistMetadata & {
Expand All @@ -303,9 +325,31 @@ type FileData = {
data: string;
};
const isNotSelected: CustomRule = (_, file) => !file.selected;
function validateField(prop: string): CustomRule {
return (_, file: ExtendedEvaluationFile) => {
let results = validateChecklistMetadata(file);
return results.ok || !results.error.invalid.includes(prop);
};
}
@Component({
components: {
LinkItem
mixins: [validationMixin],
components: {LinkItem},
validations: {
files: {
$each: {
hostip: {
ipAddress: or(validateField('hostip'), isNotSelected)
},
hostmac: {
macAddress: or(validateField('hostmac'), isNotSelected)
},
hostfqdn: {
fqdn: or(validateField('hostfqdn'), isNotSelected)
}
}
}
}
})
export default class ExportCKLModal extends Vue {
Expand Down Expand Up @@ -540,20 +584,17 @@ export default class ExportCKLModal extends Vue {
return results;
}
validateIpAddress(value: string): boolean | string {
if (!value) {
return true;
}
const ipPattern = /^(\d{1,3}\.){3}\d{1,3}$/;
return ipPattern.test(value) || 'Invalid IP Address Format';
}
validateMacAddress(value: string): boolean | string {
if (!value) {
return true;
/**
* Checks the input field and generates a formatted error message if necessary
*
* @param field the validation state of the input field
* @param name name of the field that will show up in error message
*/
validateFormat(field: typeof ValidationProperties, hint: string): string[] {
if (_.get(field, '$invalid')) {
return [hint];
}
const macPattern = /^([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}$/;
return macPattern.test(value) || 'Invalid MAC Address Format';
return [];
}
setProperName(name: string, fileIndex: number, profileIndex: number): string {
Expand Down Expand Up @@ -679,6 +720,15 @@ export default class ExportCKLModal extends Vue {
for (const selected of this.selected) {
this.addMetadataToPassthrough(selected);
if ('evaluation' in selected) {
// validate checklist metadata input from user
const result = this.validateInputMetadata(selected);
// display error message upon any invalid user inputs
if (!result.ok) {
SnackbarModule.failure(result.error);
return;
}
const data = new ChecklistResults(selected.evaluation.data).toCkl();
const filename = `${cleanUpFilename(selected.filename)}.ckl`;
fileData.push({
Expand All @@ -690,5 +740,11 @@ export default class ExportCKLModal extends Vue {
saveSingleOrMultipleFiles(fileData, 'ckl');
this.closeModal();
}
validateInputMetadata(metadata: ChecklistMetadata): Result<true, string> {
const result = validateChecklistMetadata(metadata);
if (result.ok) return {ok: true, value: true};
return {ok: false, error: result.error.message};
}
}
</script>
1 change: 1 addition & 0 deletions libs/hdf-converters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from './src/burpsuite-mapper';
export * from './src/ckl-mapper/checklist-mapper';
export * from './src/ckl-mapper/checklistJsonix';
export * from './src/ckl-mapper/checklist-jsonix-converter';
export * from './src/ckl-mapper/checklist-metadata-utils';
export * from './src/converters-from-hdf/asff/reverse-asff-mapper';
export * from './src/converters-from-hdf/caat/reverse-caat-mapper';
export * from './src/converters-from-hdf/html/reverse-html-mapper';
Expand Down
4 changes: 4 additions & 0 deletions libs/hdf-converters/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
"@types/ms": "^0.7.31",
"@types/mustache": "^4.1.2",
"@types/papaparse": "^5.3.2",
"@types/revalidator": "^0.3.12",
"@types/triple-beam": "^1.3.2",
"@types/validator": "^13.12.0",
"@types/xml2js": "^0.4.9",
"axios": "^1.3.5",
"compare-versions": "^6.0.0",
Expand All @@ -48,10 +50,12 @@
"ms": "^2.1.3",
"mustache": "^4.2.0",
"papaparse": "^5.3.1",
"revalidator": "^0.3.1",
"run-script-os": "^1.1.6",
"semver": "^7.6.0",
"tailwindcss": "^3.3.3",
"tw-elements": "^1.0.0-beta2",
"validator": "^13.12.0",
"winston": "^3.6.0",
"xml-formatter": "^3.6.2",
"xml-parser-xo": "^4.1.1",
Expand Down
Loading

0 comments on commit 63b42a3

Please sign in to comment.