Skip to content

Commit 018859c

Browse files
committed
[backend] fix stix tests and logic engine
1 parent 5a704b3 commit 018859c

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

opencti-platform/opencti-graphql/src/utils/filtering/boolean-logic-engine.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export const testGenericFilter = <T extends string | number | boolean>(
6161
|| (op === 'ends_with' && adaptedFilterValues.every((v) => stixCandidates.some((c) => typeof c === 'string' && typeof v === 'string' && c.endsWith(v))))
6262
|| (op === 'not_ends_with' && adaptedFilterValues.every((v) => !stixCandidates.some((c) => typeof c === 'string' && typeof v === 'string' && c.endsWith(v))))
6363
|| (op === 'only_eq_to' && adaptedFilterValues.every((v) => stixCandidates.includes(v)) && stixCandidates.every((c) => adaptedFilterValues.includes(c)))
64-
|| (op === 'not_only_eq_to' && !adaptedFilterValues.every((v) => stixCandidates.every((c) => c === v)))
64+
|| (op === 'not_only_eq_to' && !(adaptedFilterValues.every((v) => stixCandidates.includes(v)) && stixCandidates.every((c) => adaptedFilterValues.includes(c))))
6565
|| (op === 'search' && adaptedFilterValues.every((v) => stixCandidates.some((c) => typeof c === 'string' && typeof v === 'string'
6666
&& (v.split(' ').some((word) => c.includes(word)))))) // a stix candidate should contain at least one of the filter values words
6767

@@ -82,8 +82,8 @@ export const testGenericFilter = <T extends string | number | boolean>(
8282
|| (op === 'not_starts_with' && adaptedFilterValues.some((v) => !stixCandidates.some((c) => typeof c === 'string' && typeof v === 'string' && c.startsWith(v))))
8383
|| (op === 'ends_with' && adaptedFilterValues.some((v) => stixCandidates.some((c) => typeof c === 'string' && typeof v === 'string' && c.endsWith(v))))
8484
|| (op === 'not_ends_with' && adaptedFilterValues.some((v) => !stixCandidates.some((c) => typeof c === 'string' && typeof v === 'string' && c.endsWith(v))))
85-
|| (op === 'only_eq_to' && adaptedFilterValues.some((v) => stixCandidates.includes(v)) && stixCandidates.every((c) => adaptedFilterValues.includes(c)))
86-
|| (op === 'not_only_eq_to' && !adaptedFilterValues.some((v) => stixCandidates.every((c) => c === v)))
85+
|| (op === 'only_eq_to' && stixCandidates.length === 1 && adaptedFilterValues.some((v) => stixCandidates[0] === v))
86+
|| (op === 'not_only_eq_to' && !(stixCandidates.length === 1 && adaptedFilterValues.some((v) => stixCandidates[0] === v)))
8787
|| (op === 'search' && adaptedFilterValues.some((v) => stixCandidates.some((c) => typeof c === 'string' && typeof v === 'string'
8888
&& (v.split(' ').some((word) => c.includes(word)))))) // a stix candidate should contain at least one of the filter values words
8989

opencti-platform/opencti-graphql/tests/03-integration/01-database/filters-test.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { isStixMatchFilterGroup_MockableForUnitTests } from '../../../src/utils/
77

88
// -- File to test stix filtering (filters on events: in the context of playbooks, streams, triggers)
99
// -- with different keys, operators, modes, combinations
10+
// -- applied on data of the file DATA-TEST-FILTERS.json
1011

1112
const WHITE_TLP = { standard_id: 'marking-definition--613f2e26-407d-48c7-9eca-b8e91df99dc9', internal_id: null };
1213

@@ -180,7 +181,7 @@ describe('Filters testing', () => {
180181
mode: 'and',
181182
filters: [{
182183
key: ['objectLabel'],
183-
values: ['attack-pattern'],
184+
values: ['identity'],
184185
operator: 'not_only_eq_to',
185186
mode: 'or',
186187
}],
@@ -200,6 +201,8 @@ describe('Filters testing', () => {
200201
filterGroups: [],
201202
};
202203
filteredObjects = await applyFilters(filters);
204+
expect(filteredObjects.length).toBe(1);
205+
expect(filteredObjects[0].name).toBe('ANSSI');
203206
// With not_only_eq_to & AND local mode
204207
filtersNot = {
205208
mode: 'and',
@@ -213,7 +216,7 @@ describe('Filters testing', () => {
213216
};
214217
filteredObjectsNot = await applyFilters(filtersNot);
215218
expect(stixBundle.objects.length - filteredObjects.length).toBe(filteredObjectsNot.length);
216-
expect(filteredObjects.map((n) => n.node.name).includes('ANSSI')).toBeFalsy();
219+
expect(filteredObjectsNot.map((n) => n.name).includes('ANSSI')).toBeFalsy();
217220
// With only_eq_to & OR local mode
218221
filters = {
219222
mode: 'and',
@@ -227,6 +230,18 @@ describe('Filters testing', () => {
227230
};
228231
filteredObjects = await applyFilters(filters);
229232
expect(filteredObjects.length).toBe(10);
233+
filters = {
234+
mode: 'and',
235+
filters: [{
236+
key: ['objectLabel'],
237+
values: ['identity', 'note'],
238+
operator: 'only_eq_to',
239+
mode: 'or',
240+
}],
241+
filterGroups: [],
242+
};
243+
filteredObjects = await applyFilters(filters);
244+
expect(filteredObjects.length).toBe(11);
230245
});
231246

232247
// revoked

opencti-platform/opencti-graphql/tests/data/filters/DATA-TEST-FILTERS.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"spec_version": "2.1",
99
"name": "ANSSI",
1010
"identity_class": "organization",
11-
"labels": ["identity"],
11+
"labels": ["identity", "organization"],
1212
"created": "2020-02-23T23:40:53.575Z",
1313
"modified": "2020-02-27T08:45:39.351Z",
1414
"x_opencti_organization_type": "CSIRT",

0 commit comments

Comments
 (0)