Skip to content

Commit

Permalink
[api] Prevent name update if already available in aliases (#2681)
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-julien committed Dec 28, 2022
1 parent 12e2f6b commit 5778643
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
22 changes: 17 additions & 5 deletions opencti-platform/opencti-graphql/src/database/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ import {
STOP_TIME,
VALID_FROM,
VALID_UNTIL,
VALUE_FIELD,
X_DETECTION,
X_WORKFLOW_ID,
} from '../schema/identifier';
Expand Down Expand Up @@ -1395,8 +1396,9 @@ const innerUpdateAttribute = (instance, rawInput) => {
}
return updatedInputs;
};
const prepareAttributes = (instanceType, elements) => {
return R.map((input) => {
const prepareAttributes = (instance, elements) => {
const instanceType = instance.entity_type;
return elements.map((input) => {
// Check integer
if (R.includes(input.key, numericAttributes)) {
return {
Expand All @@ -1417,14 +1419,24 @@ const prepareAttributes = (instanceType, elements) => {
};
}
// Specific case for Label
if (instanceType === ENTITY_TYPE_LABEL && input.key === 'value') {
if (input.key === VALUE_FIELD && instanceType === ENTITY_TYPE_LABEL) {
return {
key: input.key,
value: input.value.map((v) => v.toLowerCase())
};
}
// Specific case for name in aliased entities
// If name change already inside aliases, name must be kep untouched
if (input.key === NAME_FIELD && isStixObjectAliased(instanceType)) {
const aliasField = resolveAliasesField(instanceType);
const name = normalizeName(input.value.at(0));
if ((instance[aliasField] ?? []).includes(name)) {
return null;
}
}
// No need to rework the input
return input;
}, elements);
}).filter((i) => isNotEmptyField(i));
};

const getPreviousInstanceValue = (key, instance) => {
Expand Down Expand Up @@ -1454,7 +1466,7 @@ export const updateAttributeRaw = async (context, user, instance, inputs, opts =
const elements = Array.isArray(inputs) ? inputs : [inputs];
const instanceType = instance.entity_type;
// Prepare attributes
const preparedElements = prepareAttributes(instanceType, elements);
const preparedElements = prepareAttributes(instance, elements);
// region Check date range
const inputKeys = inputs.map((i) => i.key);
if (inputKeys.includes(START_TIME) || inputKeys.includes(STOP_TIME)) {
Expand Down
1 change: 1 addition & 0 deletions opencti-platform/opencti-graphql/src/schema/identifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const transformObjectToUpperKeys = (data) => {
export const INTERNAL_FROM_FIELD = 'i_relations_from';
export const INTERNAL_TO_FIELD = 'i_relations_to';
export const NAME_FIELD = 'name';
export const VALUE_FIELD = 'value';
export const FIRST_SEEN = 'first_seen';
export const LAST_SEEN = 'last_seen';
export const START_TIME = 'start_time';
Expand Down

0 comments on commit 5778643

Please sign in to comment.