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

[backend] Add path resolution in filtering #6207

Merged
merged 1 commit into from
Mar 2, 2024
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
@@ -1,3 +1,4 @@
import * as R from 'ramda';
import type { Filter, FilterGroup } from '../../generated/graphql';
import { FilterMode, FilterOperator } from '../../generated/graphql';
import {
Expand All @@ -13,7 +14,7 @@ import {
PARTICIPANT_FILTER,
RELATION_FROM_FILTER,
RELATION_TO_FILTER,
WORKFLOW_FILTER,
WORKFLOW_FILTER
} from './filtering-constants';
import type { AuthContext, AuthUser } from '../../types/user';
import type { StixObject } from '../../types/stix-common';
Expand Down Expand Up @@ -43,7 +44,7 @@ export const RESOLUTION_FILTERS = [
export type FilterResolutionMap = Map<string, string>;

// map (filter key) <-> (corresponding prop key in a stix object)
const STIX_RESOLUTION_MAP_PATHS: Record<string, string> = {
export const STIX_RESOLUTION_MAP_PATHS: Record<string, string | string[]> = {
// [ASSIGNEE_FILTER]: 'id', // no resolution required in stix ; assignee_ids are internal ids already
[CREATED_BY_FILTER]: 'id', // created by --> resolve with the standard id (which is the stix.id)
[LABEL_FILTER]: 'value', // labels --> resolve id to stix.name
Expand All @@ -53,7 +54,7 @@ const STIX_RESOLUTION_MAP_PATHS: Record<string, string> = {
[PARTICIPANT_FILTER]: 'id', // participant --> resolve with the standard id (which is the stix.id)
[RELATION_FROM_FILTER]: 'id',
[RELATION_TO_FILTER]: 'id',
[CONNECTED_TO_INSTANCE_FILTER]: 'id', // instance trigger --> resolve with the standard id (which is the stix.id)
[CONNECTED_TO_INSTANCE_FILTER]: ['extensions', STIX_EXT_OCTI, 'id'], // instance trigger --> resolve with the internal id
[CONNECTED_TO_INSTANCE_SIDE_EVENTS_FILTER]: 'id', // instance trigger --> resolve with the standard id (which is the stix.id)
};

Expand Down Expand Up @@ -143,7 +144,7 @@ const buildResolutionMapForFilter = async (context: AuthContext, user: AuthUser,
} else if (filter.key[0] === CONNECTED_TO_INSTANCE_FILTER) {
map.set(v, cachedObject.extensions[STIX_EXT_OCTI].id);
} else {
const cachedValue = cachedObject[path];
const cachedValue = R.path(Array.isArray(path) ? path : [path], cachedObject);
if (typeof cachedValue === 'string') {
map.set(v, cachedValue);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as R from 'ramda';
import { STIX_EXT_OCTI, STIX_EXT_OCTI_SCO } from '../../../types/stix-extensions';
import { generateInternalType, getParentTypes } from '../../../schema/schemaUtils';
import { STIX_TYPE_RELATION, STIX_TYPE_SIGHTING } from '../../../schema/general';
import { stixRefsExtractor } from '../../../schema/stixEmbeddedRelationship';
import { testStringFilter, testNumericFilter, toValidArray, testBooleanFilter } from '../boolean-logic-engine';
import type { TesterFunction } from '../boolean-logic-engine';

import { testBooleanFilter, testNumericFilter, testStringFilter, toValidArray } from '../boolean-logic-engine';
import {
ASSIGNEE_FILTER,
CONFIDENCE_FILTER,
Expand All @@ -28,9 +28,10 @@ import {
SCORE_FILTER,
SEVERITY_FILTER,
TYPE_FILTER,
WORKFLOW_FILTER
WORKFLOW_FILTER,
} from '../filtering-constants';
import type { Filter } from '../../../generated/graphql';
import { STIX_RESOLUTION_MAP_PATHS } from '../filtering-resolution';

//-----------------------------------------------------------------------------------
// Testers for each possible filter.
Expand Down Expand Up @@ -274,7 +275,8 @@ export const testConnectedTo = (stix: any, filter: Filter) => {
if (filter.operator && filter.operator !== 'eq') {
return false;
}
return testStringFilter(filter, [stix.extensions[STIX_EXT_OCTI].id]);
const value = R.path(STIX_RESOLUTION_MAP_PATHS[CONNECTED_TO_INSTANCE_FILTER] as string[], stix) as string;
return testStringFilter(filter, [value]);
};

/**
Expand Down
Loading