[8.6] [TIP] Ensure non-primitive values are not rendered (#150015) (#150072)

# Backport

This will backport the following commits from `main` to `8.6`:
- [[TIP] Ensure non-primitive values are not rendered
(#150015)](https://github.com/elastic/kibana/pull/150015)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Luke
Gmys","email":"lgmys@users.noreply.github.com"},"sourceCommit":{"committedDate":"2023-02-01T15:29:22Z","message":"[TIP]
Ensure non-primitive values are not rendered (#150015)\n\n##
Summary\r\n\r\nShould fix
https://github.com/elastic/security-team/issues/5856\r\n\r\nRight now,
it will just render the complex fields as empty. Should these\r\nbe
ommited or something?\r\n\r\n### Checklist\r\n\r\nDelete any items that
are not applicable to this PR.\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios","sha":"612b8e7d8a686f584e2f719416248e3255cfead0","branchLabelMapping":{"^v8.7.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["bug","release_note:fix","backport:prev-minor","Team:
Protections
Experience","ci:cloud-deploy","v8.7.0"],"number":150015,"url":"https://github.com/elastic/kibana/pull/150015","mergeCommit":{"message":"[TIP]
Ensure non-primitive values are not rendered (#150015)\n\n##
Summary\r\n\r\nShould fix
https://github.com/elastic/security-team/issues/5856\r\n\r\nRight now,
it will just render the complex fields as empty. Should these\r\nbe
ommited or something?\r\n\r\n### Checklist\r\n\r\nDelete any items that
are not applicable to this PR.\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios","sha":"612b8e7d8a686f584e2f719416248e3255cfead0"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v8.7.0","labelRegex":"^v8.7.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/150015","number":150015,"mergeCommit":{"message":"[TIP]
Ensure non-primitive values are not rendered (#150015)\n\n##
Summary\r\n\r\nShould fix
https://github.com/elastic/security-team/issues/5856\r\n\r\nRight now,
it will just render the complex fields as empty. Should these\r\nbe
ommited or something?\r\n\r\n### Checklist\r\n\r\nDelete any items that
are not applicable to this PR.\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios","sha":"612b8e7d8a686f584e2f719416248e3255cfead0"}}]}]
BACKPORT-->

Co-authored-by: Luke Gmys <lgmys@users.noreply.github.com>
This commit is contained in:
Kibana Machine 2023-02-01 11:31:05 -05:00 committed by GitHub
parent 9a1a960b46
commit 1432dc65d9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 7 deletions

View file

@ -17,5 +17,9 @@ describe('unwrapValue()', () => {
expect(
unwrapValue({ fields: { [RawIndicatorFieldId.Type]: ['ip'] } }, RawIndicatorFieldId.Type)
).toEqual('ip');
expect(
unwrapValue({ fields: { [RawIndicatorFieldId.Type]: [{}] } }, RawIndicatorFieldId.Type)
).toEqual(null);
});
});

View file

@ -5,20 +5,24 @@
* 2.0.
*/
import { Indicator, RawIndicatorFieldId } from '../../../../common/types/indicator';
type IndicatorLike = Record<'fields', Record<string, unknown>> | null | undefined;
/**
* Unpacks field value from raw indicator fields. Will return null if fields are missing entirely
* or there is no record for given `fieldId`
*/
export const unwrapValue = <T = string>(
indicator: Partial<Indicator> | null | undefined,
fieldId: RawIndicatorFieldId
): T | null => {
export const unwrapValue = <T = string>(indicator: IndicatorLike, fieldId: string): T | null => {
if (!indicator) {
return null;
}
const valueArray = indicator.fields?.[fieldId];
return Array.isArray(valueArray) ? (valueArray[0] as T) : null;
const fieldValues = indicator.fields?.[fieldId];
if (!Array.isArray(fieldValues)) {
return null;
}
const firstValue = fieldValues[0];
return typeof firstValue === 'object' ? null : (firstValue as T);
};