Skip to content

fix: Improve elastic filters handling (#17508) - #17510

Merged
Mathieu Cartaud (mcartaud) merged 5 commits into
masterfrom
issue/17508-elastic-filters
Aug 6, 2026
Merged

fix: Improve elastic filters handling (#17508)#17510
Mathieu Cartaud (mcartaud) merged 5 commits into
masterfrom
issue/17508-elastic-filters

Conversation

@mcartaud

@mcartaud Mathieu Cartaud (mcartaud) commented Aug 4, 2026

Copy link
Copy Markdown
Member

Proposed changes

  • Parametrize the only_eq_to script field
  • ensure only allowed keys are present in filter-key
  • Escape wildcard values
  • Make internal_script truly internal

Related issues

How to test this PR

Checklist

  • I consider the submitted work as finished
  • I tested the code for its functionality
  • I wrote test cases for the relevant use cases (coverage and e2e)
  • I added/updated the relevant documentation (either on GitHub or on Notion)
  • Where necessary, I refactored code to improve the overall quality

Further comments

Copilot AI lite review requested due to automatic review settings August 4, 2026 15:24
@github-actions github-actions Bot added the filigran team Item from the Filigran team. label Aug 4, 2026
@codecov

codecov Bot commented Aug 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 34.10%. Comparing base (3aafc73) to head (715177c).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #17510   +/-   ##
=======================================
  Coverage   34.09%   34.10%           
=======================================
  Files        3384     3384           
  Lines      138002   138012   +10     
  Branches    37353    37355    +2     
=======================================
+ Hits        47048    47063   +15     
+ Misses      90954    90949    -5     
Flag Coverage Δ
opencti-client-python 48.37% <ø> (ø)
opencti-front 11.06% <ø> (ø)
opencti-graphql 69.22% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens and refines OpenCTI’s ElasticSearch filtering pipeline in opencti-graphql, specifically by preventing unsafe script-based filter operators from being accepted through the generic filter grammar and by introducing a dedicated internal-only path for trusted Painless clauses.

Changes:

  • Forbid internal_script from passing filter format validation, and throw if it somehow reaches query building.
  • Introduce internalScriptFilters as a dedicated internal-only option and migrate report orphan detection to use it.
  • Improve ES query safety by parameterizing scripted field access and escaping wildcard query-string values.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
opencti-platform/opencti-graphql/src/utils/filtering/filtering-utils.ts Strengthens filter validation and changes composed-key validation logic.
opencti-platform/opencti-graphql/src/domain/report.js Migrates orphan-object deletion logic off internal_script operator into internalScriptFilters.
opencti-platform/opencti-graphql/src/database/middleware-loader.ts Extends list/count argument typing to carry internal-only script filters.
opencti-platform/opencti-graphql/src/database/engine.ts Enforces internal_script rejection, adds internalScriptFilters support, and improves ES query escaping/parameterization.

Comment thread opencti-platform/opencti-graphql/src/utils/filtering/filtering-utils.ts Outdated
Comment on lines 61 to 63
return (
filter.key && isNotEmptyField(filter.key)
filter.key && isNotEmptyField(filter.key) && (filter.operator as string) !== 'internal_script'
);
@mcartaud
Mathieu Cartaud (mcartaud) force-pushed the issue/17508-elastic-filters branch 2 times, most recently from fa20eff to 63aa41a Compare August 5, 2026 07:55
// runtime check is needed: raw JSON-parsed filters bypass the enum typing entirely.
return (
filter.key && isNotEmptyField(filter.key)
filter.key && isNotEmptyField(filter.key) && (filter.operator as string) !== 'internal_script'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw a different and specific error if filter.operator = 'internal_script' rather than the generic one 'Incorrect filters format'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather check the operator is valid (ie among an existing list)? We can't check all the not-valid operators like this one because one day the operator existed

script: values[i].toString(),
},
});
} else if (operator === 'internal_script') {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this 'else if' which is not necessary here ? We don't check all the not-existing operators.

Suggestion: instead of the last 'else', we write 'else if(range_operators)'
And then : else { throw Error ('Not supported filter operator') }

@Archidoit

Copy link
Copy Markdown
Member

Missing generic backend tests

@mcartaud

Copy link
Copy Markdown
Member Author

Missing generic backend tests

Done Archidoit

},
});
} else {
throw UnsupportedError('Not supported filter operator');

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw UnsupportedError('Not supported filter operator');
throw UnsupportedError('Not supported filter operator', { filter, filterOperator );

Add the filter to have more content in the error thrown

});

it('should buildLocalMustFilter with internal_script should work', () => {
it('unknown keys must be rejected by buildLocalMustFilter', () => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it('unknown keys must be rejected by buildLocalMustFilter', () => {
it('unknown filter operators must be rejected by buildLocalMustFilter', () => {


await expect(
countAllThings(testContext, ADMIN_USER, { filters: JSON.parse(filtersString) }),
).rejects.toThrow();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add the error message expected

filterGroups: [],
} as FilterGroup;

expect(() => checkFiltersValidity(filterGroup)).toThrowError();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe check also the expected error message

`;

const addMalware = async (name) => {
const result = await queryAsAdmin({ query: MALWARE_ADD_QUERY, variables: { input: { name } } });

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you create new entities in test, you should update the counters to avoid failing tests in raw-test.js (testUpdatedCounter, testCreatedCounter, etc).

@mcartaud
Mathieu Cartaud (mcartaud) merged commit 1589eb3 into master Aug 6, 2026
56 checks passed
@mcartaud
Mathieu Cartaud (mcartaud) deleted the issue/17508-elastic-filters branch August 6, 2026 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

filigran team Item from the Filigran team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: Improve ES query filters

3 participants