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

[Security solution] Fix IP/geo location in Top N Flow Table #149369

Merged
merged 4 commits into from
Jan 24, 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
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export const mockSearchStrategyResponse: IEsSearchResponse<unknown> = {
_id: 'dd4fa2d4bd-1526378075029582',
_score: 0,
fields: {
'@timestamp': ['2022-07-18T15:08:48.064Z'],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the bug surfaced because some returned geo data included the timestamp. Adding this field here would break tests in main and proves the fix

'source.geo.continent_name': ['North America'],
'source.geo.region_iso_code': ['US-VA'],
'source.geo.country_iso_code': ['US'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ import type {
NetworkTopNFlowEdges,
NetworkTopNFlowRequestOptions,
AutonomousSystemItem,
} from '../../../../../../common/search_strategy';
import {
NetworkTopTablesFields,
FlowTargetSourceDest,
} from '../../../../../../common/search_strategy';
import { NetworkTopTablesFields } from '../../../../../../common/search_strategy';
import { getOppositeField } from '../helpers';
import {
formatResponseObjectValues,
Expand All @@ -48,7 +46,7 @@ const formatTopNFlowEdges = (
[flowTarget]: {
domain: bucket.domain.buckets.map((bucketDomain) => bucketDomain.key),
ip: bucket.key,
location: getGeoItem(bucket),
location: getGeoItem(bucket, flowTarget),
autonomous_system: getAsItem(bucket),
flows: getOr(0, 'flows.value', bucket),
[`${getOppositeField(flowTarget)}_ips`]: getOr(
Expand All @@ -68,24 +66,22 @@ const formatTopNFlowEdges = (
},
}));

const getFlowTargetFromString = (flowAsString: string) =>
flowAsString === 'source' ? FlowTargetSourceDest.source : FlowTargetSourceDest.destination;

const getGeoItem = (result: NetworkTopNFlowBuckets): GeoItem | null =>
const getGeoItem = (
result: NetworkTopNFlowBuckets,
flowTarget: FlowTargetSourceDest
): GeoItem | null =>
result.location.top_geo.hits.hits.length > 0 && result.location.top_geo.hits.hits[0].fields
? {
geo: formatResponseObjectValues(
getOr(
'',
`${Object.keys(result.location.top_geo.hits.hits[0].fields)[0].split('.geo')[0]}.geo`,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was very silly. If for some reason a non-geo field is in there (ahem, @timestamp), the code breaks. We are able to simply pass flowTarget from where the function is called.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now, it looks ten times better 🤩

`${flowTarget}.geo`,
unflattenObject(
transformLocationFields(getOr({}, `location.top_geo.hits.hits[0].fields`, result))
)
)
),
flowTarget: getFlowTargetFromString(
Object.keys(result.location.top_geo.hits.hits[0].fields)[0].split('.geo')[0]
),
flowTarget,
}
: null;

Expand Down