Skip to content

Commit

Permalink
[backend] Fix cyclic relation bug in tasks and notes (#4920)
Browse files Browse the repository at this point in the history
  • Loading branch information
Goumies committed Nov 28, 2023
1 parent af89ded commit c187d6a
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 133 deletions.
23 changes: 20 additions & 3 deletions opencti-platform/opencti-graphql/src/domain/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import * as R from 'ramda';
import { v4 as uuidv4 } from 'uuid';
import { RELATION_OBJECT } from '../schema/stixRefRelationship';
import { listAllThings, listThings } from '../database/middleware';
import { internalFindByIds, listAllRelations, listEntities, storeLoadById } from '../database/middleware-loader';
import {
internalFindByIds,
listAllRelations,
listEntities,
storeLoadById
} from '../database/middleware-loader';
import {
ABSTRACT_BASIC_RELATIONSHIP,
ABSTRACT_STIX_REF_RELATIONSHIP,
Expand All @@ -13,7 +18,7 @@ import {
import { isStixDomainObjectContainer } from '../schema/stixDomainObject';
import { buildPagination, isInferredIndex, READ_INDEX_STIX_DOMAIN_OBJECTS } from '../database/utils';
import { now } from '../utils/format';
import { elCount } from '../database/engine';
import { elBatchIds, elCount } from '../database/engine';
import { findById as findInvestigationById } from '../modules/workspace/workspace-domain';
import { stixCoreObjectAddRelations } from './stixCoreObject';
import { addFilter } from '../utils/filtering/filtering-utils';
Expand Down Expand Up @@ -148,9 +153,21 @@ export const containersObjectsOfObject = async (context, user, { id, types, filt
return buildPagination(0, null, resolvedObjects.map((r) => ({ node: r })), resolvedObjects.length);
};

export const filterUnwantedEntitiesOut = async ({ context, user, ids }) => {
const filteredOutInvestigatedIds = [];
const entities = await elBatchIds(context, user, ids);
entities?.forEach((entity) => {
if (!['Task', 'Note'].includes(entity.entity_type)) {
filteredOutInvestigatedIds.push(entity.id);
}
});
return filteredOutInvestigatedIds;
};

export const knowledgeAddFromInvestigation = async (context, user, { containerId, workspaceId }) => {
const investigation = await findInvestigationById(context, user, workspaceId);
const toIds = investigation.investigated_entities_ids.filter((id) => id !== containerId);
const ids = investigation.investigated_entities_ids?.filter((id) => id !== containerId);
const toIds = await filterUnwantedEntitiesOut({ context, user, ids });
const containerInput = { toIds, relationship_type: 'object' };
return await stixCoreObjectAddRelations(context, user, containerId, containerInput);
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type { BasicStoreEntity, StoreEntity, StoreEntityReport } from '../../typ
import { nowTime } from '../../utils/format';
import { READ_STIX_INDICES } from '../../database/utils';
import { getParentTypes } from '../../schema/schemaUtils';
import { filterUnwantedEntitiesOut } from '../../domain/container';

const buildStixReportForExport = (workspace: BasicStoreEntityWorkspace, investigatedEntities: StoreEntity[]): StixObject => {
const id = generateStandardId(ENTITY_TYPE_CONTAINER_REPORT, { name: workspace.name, published: workspace.created_at }) as StixId;
Expand Down Expand Up @@ -44,6 +45,8 @@ export const toStixReportBundle = async (context: AuthContext, user: AuthUser, w
export const investigationAddFromContainer = async (context: AuthContext, user: AuthUser, containerId: string) => {
const container = await internalLoadById<BasicStoreEntity>(context, user, containerId);
const investigationToStartCanonicalName = `[${container.entity_type}] "${container.name}" (${nowTime()})`;
const investigationInput = { type: 'investigation', name: investigationToStartCanonicalName, investigated_entities_ids: container.object };
const filteredOutInvestigatedIds = await filterUnwantedEntitiesOut({ context, user, ids: container.object });

const investigationInput = { type: 'investigation', name: investigationToStartCanonicalName, investigated_entities_ids: filteredOutInvestigatedIds };
return addWorkspace(context, user, investigationInput);
};

0 comments on commit c187d6a

Please sign in to comment.