■ ■ ■ ■ ■ ■ ■
opencti-platform/opencti-graphql/src/graphql/sseMiddleware.js
| skipped 451 lines |
452 | 452 | | await listAllRelations(req.session.user, relationTypes, allRelOptions); |
453 | 453 | | } |
454 | 454 | | }; |
| 455 | + | const isFiltersEntityTypeMatch = (filters, type) => { |
| 456 | + | let match = false; |
| 457 | + | const fromAllTypes = [type, ...getParentTypes(type)]; |
| 458 | + | // eslint-disable-next-line no-restricted-syntax |
| 459 | + | for (const filter of filters.entity_type.values) { |
| 460 | + | if (fromAllTypes.includes(filter.id)) { |
| 461 | + | match = true; |
| 462 | + | } |
| 463 | + | } |
| 464 | + | return match; |
| 465 | + | }; |
455 | 466 | | const publishRelationDependencies = async (cache, filterCache, channel, req, streamFilters, element) => { |
456 | 467 | | const { user } = req.session; |
457 | 468 | | const { id: eventId, data: eventData } = element; |
458 | 469 | | const { type, data: stix, message } = eventData; |
459 | | - | const fromId = stix.type === 'relationship' ? stix.source_ref : stix.sighting_of_ref; |
460 | | - | const toId = stix.type === 'relationship' ? stix.target_ref : stix.where_sighted_refs[0]; |
| 470 | + | const isRel = stix.type === 'relationship'; |
| 471 | + | const fromId = isRel ? stix.source_ref : stix.sighting_of_ref; |
| 472 | + | const toId = isRel ? stix.target_ref : stix.where_sighted_refs[0]; |
| 473 | + | // Pre-filter by type to prevent resolutions as much as possible. |
| 474 | + | const filters = adaptFiltersFrontendFormat(streamFilters); |
| 475 | + | if (filters.entity_type && filters.entity_type.values.length > 0) { |
| 476 | + | const fromType = isRel ? stix.extensions[STIX_EXT_OCTI].source_type : stix.extensions[STIX_EXT_OCTI].sighting_of_type; |
| 477 | + | const matchingFrom = isFiltersEntityTypeMatch(filters, fromType); |
| 478 | + | const toType = isRel ? stix.extensions[STIX_EXT_OCTI].target_type : stix.extensions[STIX_EXT_OCTI].where_sighted_types[0]; |
| 479 | + | const matchingTo = isFiltersEntityTypeMatch(filters, toType); |
| 480 | + | if (!matchingFrom && !matchingTo) { |
| 481 | + | return; |
| 482 | + | } |
| 483 | + | } |
461 | 484 | | const [fromStix, toStix] = await Promise.all([stixLoadById(user, fromId), stixLoadById(user, toId)]); |
462 | 485 | | if (fromStix && toStix) { |
463 | 486 | | // As we resolved at now, data can be deleted now. |
| skipped 206 lines |