Skip to content

Commit

Permalink
[backend] check indicator pattern syntax when update
Browse files Browse the repository at this point in the history
  • Loading branch information
marieflorescontact committed Oct 22, 2024
1 parent e44e755 commit 82b87ed
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,16 @@ export const indicatorEditField = async (context: AuthContext, user: AuthUser, i
finalInput.push({ key: 'valid_until', value: [newValidUntilDate.toISOString()] });
}
}
// check indicator pattern syntax
const patternEditInput = input.find((e) => e.key === 'pattern');
if (patternEditInput) {
const patternType = indicator.pattern_type.toLowerCase();
const formattedPattern = cleanupIndicatorPattern(patternType, patternEditInput.value[0]);
const check = await checkIndicatorSyntax(context, user, patternType, formattedPattern);
if (check === false) {
throw FunctionalError(`Indicator of type ${indicator.pattern_type} is not correctly formatted.`);
}
}

Check warning on line 348 in opencti-platform/opencti-graphql/src/modules/indicator/indicator-domain.ts

View check run for this annotation

Codecov / codecov/patch

opencti-platform/opencti-graphql/src/modules/indicator/indicator-domain.ts#L342-L348

Added lines #L342 - L348 were not covered by tests
logApp.info('indicatorEditField finalInput', { finalInput });
return stixDomainObjectEditField(context, user, id, finalInput, opts);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, it, describe } from 'vitest';
import gql from 'graphql-tag';
import { queryAsAdmin } from '../../utils/testQuery';
import { adminQuery, queryAsAdmin } from '../../utils/testQuery';
import { ENTITY_DOMAIN_NAME } from '../../../src/schema/stixCyberObservable';
import { MARKING_TLP_GREEN } from '../../../src/schema/identifier';
import type { BasicStoreEntityEdge } from '../../../src/types/store';
Expand Down Expand Up @@ -94,6 +94,15 @@ const CREATE_QUERY = gql`
}
`;

const UPDATE_QUERY = gql`
mutation IndicatorFieldPatch($id: ID!, $input: [EditInput!]!) {
indicatorFieldPatch(id: $id, input: $input) {
id
name
}
}
`;

describe('Indicator resolver standard behavior', () => {
let firstIndicatorInternalId: string;
let secondIndicatorInternalId: string;
Expand Down Expand Up @@ -203,15 +212,15 @@ describe('Indicator resolver standard behavior', () => {
expect(indicatorCreatedEarlier).toBeDefined();
}
});
it('should not update indicator with incorrectly formatted pattern', async () => {
const queryResult = await adminQuery({
query: UPDATE_QUERY,
variables: { id: firstIndicatorInternalId, input: { key: 'pattern', value: ["[domain-name:value &&& 'www.wrong.pattern']"] } },
});
expect(queryResult.errors).toBeDefined();
expect(queryResult.errors[0].message).toBe('Indicator of type stix is not correctly formatted.');
});
it('should update indicator', async () => {
const UPDATE_QUERY = gql`
mutation IndicatorFieldPatch($id: ID!, $input: [EditInput!]!) {
indicatorFieldPatch(id: $id, input: $input) {
id
name
}
}
`;
const queryResult = await queryAsAdminWithSuccess({
query: UPDATE_QUERY,
variables: { id: firstIndicatorInternalId, input: { key: 'name', value: ['Indicator - test'] } },
Expand Down

0 comments on commit 82b87ed

Please sign in to comment.