Skip to content

Commit

Permalink
[backend] Default marking are not cleaned when marking is deleted, le…
Browse files Browse the repository at this point in the history
…ading to full crashed platform (#5839)
  • Loading branch information
jpkha authored and SamuelHassine committed Feb 16, 2024
1 parent d1d9194 commit b60c0ed
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
5 changes: 2 additions & 3 deletions opencti-platform/opencti-graphql/src/domain/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,10 @@ export const groupAllowedMarkings = async (context, user, groupId) => {

export const defaultMarkingDefinitions = async (context, group) => {
const defaultMarking = group.default_marking ?? [];
const markingsMap = await getEntitiesMapFromCache(context, SYSTEM_USER, ENTITY_TYPE_MARKING_DEFINITION);
return defaultMarking.map((entry) => {
return defaultMarking.map(async (entry) => {
return {
entity_type: entry.entity_type,
values: entry.values?.map((d) => markingsMap.get(d)),
values: await cleanMarkings(context, entry.values),
};
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { UnsupportedError } from '../config/errors';

export const cleanMarkings = async (context, values) => {
const markingsMap = await getEntitiesMapFromCache(context, SYSTEM_USER, ENTITY_TYPE_MARKING_DEFINITION);
const defaultMarkingValues = values?.map((d) => markingsMap.get(d) ?? d) ?? [];
const defaultMarkingValues = values?.filter((d) => !!d).map((d) => markingsMap.get(d) ?? d) ?? [];
const defaultGroupedMarkings = R.groupBy((m) => m.definition_type, defaultMarkingValues);
return Object.entries(defaultGroupedMarkings).map(([_, markingValues]) => {
const max = Math.max(...markingValues.map((m) => m.x_opencti_order));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe('Marking Definition', () => {
statementMarking1 = await createMarking(markings[2]);
statementMarking2 = await createMarking(markings[3]);
});
describe('Clean Markings', async () => {
describe('Clean Markings use for editing', async () => {
it('Case add only one marking => output one marking added', async () => {
const result = await cleanMarkings(testContext, [clearPAPMarking]);
expect(result.map((r) => r.id)).toEqual([clearPAPMarking.id]);
Expand All @@ -88,6 +88,15 @@ describe('Marking Definition', () => {
const result = await cleanMarkings(testContext, [redPAPMarking, clearPAPMarking, statementMarking1]);
expect(result.map((r) => r.id)).toEqual([redPAPMarking.id, statementMarking1.id]);
});
it('Case add a marking with as undefined (deleted case) => output no marking added', async () => {
const result = await cleanMarkings(testContext, [undefined]);
expect(result.map((r) => r.id)).toEqual([]);
});
it('Case add 2 markings same type AND order different AND a deleted marking => output marking with higher rank added', async () => {
// Case input 2 markings same type AND order different: output marking with higher rank added
const result = await cleanMarkings(testContext, [clearPAPMarking, undefined, redPAPMarking]);
expect(result.map((r) => r.id)).toEqual([redPAPMarking.id]);
});
});

describe('Markings to replace filtered', () => {
Expand Down

0 comments on commit b60c0ed

Please sign in to comment.