Skip to content

[2.19] Handle null field name in DlsFlsFilterLeafReader.isMasked - #6806

Merged
cwperks merged 4 commits into
opensearch-project:2.19from
srikdhruv:fix/2.19-null-field-ismasked
Nov 25, 2026
Merged

cwperks merged 4 commits into
opensearch-project:2.19from
srikdhruv:fix/2.19-null-field-ismasked

Conversation

@srikdhruv

@srikdhruv srikdhruv commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Description

  • Category: Bug fix
  • Why these changes are required?
    On 2.19, some _geo_distance sorts fail on every shard with HTTP 500 (Cannot invoke "String.endsWith(String)" because "field" is null), for example a sort with "unit": "mi" as the first sort key ([BUG] NullPointerException in DlsFlsFilterLeafReader.isMasked when sorting by _geo_distance with field masking active #6803). For these sorts, OpenSearch asks the index reader for point values with a null field name. DlsFlsFilterLeafReader passes that null to isMasked, which calls field.endsWith(...) on it. The reader wraps every search on 2.19, so users without any DLS, FLS or masking rules (for example all_access) are affected too.
    main is already fixed by Handle cases gracefully where null field name is passed to LeafReader methods #5537, but that change can't be applied to 2.19 because the classes it edits don't exist there.
  • What is the old behavior before changes and new behavior after changes?
    Before: HTTP 500. After: isMasked treats a null field name as masked, so getPointValues(null) returns null ("no point values"), the same answer Lucene's own reader gives. The search succeeds with the same results. Behavior for real field names doesn't change.

Issues Resolved

Resolves #6803

Is this a backport? No. This is a 2.19-only fix; main is already covered by #5537.

Do these changes introduce new permission(s) to be displayed in the static dropdown on the front-end? No, no new permissions are introduced.

Testing

New integration test GeoDistanceSortWithFieldMaskingIntegrationTest. It indexes three documents at known distances and runs each query as six users:

  • Without FLS: all_access; read only; masked field owner; masked field own*.
  • With FLS: ~status and masked owner; location, owner and masked owner.
_geo_distance sort Users without FLS, before Users with FLS, before All users, after
unit: mi, first sort key 500 200 200
order: desc, no unit 500 200 200
Two origin points, no unit 500 200 200
unit: mi, second sort key 200 200 200
Ascending, one origin point, no unit 200 200 200

For each 200, the test also checks hit order, sort distances, masked values and FLS visibility. It also runs terms aggregations with missing, which caused the same error on 3.0; they pass before and after on 2.19.

The existing unit tests matching *DlsFlsFilterLeafReader*, *FieldMasking*, *Fls* and *Dls*, the existing integration tests matching *Fls*, *Dls* and *Masking*, and spotless all pass with this change.

Check List

  • New functionality includes testing
  • Commits are signed per the DCO using --signoff

The documentation, security dashboards plugin PR and API changes companion PR checklist items were removed because they do not apply: this is a bug fix with no user-facing documentation change, no new permissions and no request/response schema changes.

AI Usage

This change was developed with AI assistance (Kiro). The author directed the approach, reviewed every line, and verified behavior with the tests described above.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

A _geo_distance sort with a non-default unit as the first sort key makes
Lucene call getPointValues with a null field name. On 2.19, isMasked
passed that null to the masked-field matchers and to String.endsWith,
which threw a NullPointerException and failed the search with HTTP 500.
DlsFlsFilterLeafReader wraps every search, so this affected users
without any DLS, FLS or field masking too.

isMasked now treats a null field name as masked, so the reader reports
no values for it, the same as the FLS check already does. main handles
this through normalizeFieldName (opensearch-project#5537), which does not exist on 2.19.

Resolves opensearch-project#6803

Signed-off-by: Dhruv Srikanth <dhruvsrk@amazon.com>
@srikdhruv
srikdhruv force-pushed the fix/2.19-null-field-ismasked branch from 56e56ad to a4af4f6 Compare November 24, 2026 22:16
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

PR Reviewer Guide 🔍

(Review updated until commit b0d4c09)

Here are some key observations to aid the review process:

🧪 PR contains tests
🔒 No security concerns identified
✅ No TODO sections
🔀 No multiple PR themes
⚡ No major issues detected

@github-actions

Copy link
Copy Markdown
Contributor

Persistent review updated to latest commit a4af4f6

Covers a user whose roles have no DLS, FLS or masking rules, which matches the reported case.

Signed-off-by: Dhruv Srikanth <dhruvsrk@amazon.com>
@github-actions

Copy link
Copy Markdown
Contributor

Persistent review updated to latest commit 7a6c26e

Any _geo_distance sort that does not use Lucene's built-in distance sort (a non-meter unit, descending order, several origin points) builds a comparator with a null field name. Add descending and two-point cases, and terms aggregations with missing values, which do not reach the null path on 2.19.

Signed-off-by: Dhruv Srikanth <dhruvsrk@amazon.com>
@github-actions

Copy link
Copy Markdown
Contributor

Persistent review updated to latest commit 9fb4037

The null field name comes from any _geo_distance sort that does not use Lucene's built-in distance sort, not only one with a unit.

Signed-off-by: Dhruv Srikanth <dhruvsrk@amazon.com>
@github-actions

Copy link
Copy Markdown
Contributor

Persistent review updated to latest commit b0d4c09

@codecov

codecov Bot commented Nov 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 68.49%. Comparing base (cb59a67) to head (b0d4c09).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             2.19    #6806      +/-   ##
==========================================
- Coverage   68.49%   68.49%   -0.01%     
==========================================
  Files         348      348              
  Lines       24821    24823       +2     
  Branches     4141     4142       +1     
==========================================
+ Hits        17002    17003       +1     
- Misses       5890     5893       +3     
+ Partials     1929     1927       -2     
Files with missing lines Coverage Δ
...security/configuration/DlsFlsFilterLeafReader.java 72.63% <100.00%> (+0.31%) ⬆️

... and 7 files with indirect coverage changes

🚀 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.

@srikdhruv
srikdhruv marked this pull request as ready for review November 25, 2026 01:12
@srikdhruv

Copy link
Copy Markdown
Contributor Author

I noticed that the earlier fixes for this NPE (#5504, #5537) guarded against a null field name but left its source open. #5537's comment notes that "OpenSearch code calls diverse LeafReader methods with a null field param" and that "the semantics are not quite clear". I had an agent trace it, and the null comes from OpenSearch core, in code inherited from Elasticsearch 7.10 and kept through OpenSearch's Lucene 9.1 upgrade (opensearch-project/OpenSearch#2487).

I believe a one-line fix in core would stop the null from reaching reader wrappers in the first place, rather than only guarding against it in the security plugin. In GeoDistanceSortBuilder.comparatorSource(...).newComparator:

-   return new DoubleComparator(numHits, null, null, reversed, filterPruning(pruning)) {
+   return new DoubleComparator(numHits, null, null, reversed, Pruning.NONE) {

How the null reaches the security plugin

  • GeoDistanceSortBuilder.build uses Lucene's LatLonDocValuesField.newDistanceSort only for an ascending sort with one origin point, mode min, unit meters and no nested sort. Every other _geo_distance sort, for example "unit": "mi", "order": "desc" or several origin points, uses OpenSearch's own comparator, new DoubleComparator(numHits, null, ...). Its distances are computed from doc values, so it has no field name.
  • For the first sort key, Lucene enables pruning, and NumericLeafComparator calls reader.getPointValues(field) with that null. On core main, which uses Lucene 10.6, it also calls reader.getDocValuesSkipper(null). Lucene's own reader returns null, which disables skipping, so the sort works on a bare index. DlsFlsFilterLeafReader is the first wrapper on that path that doesn't accept null.
  • That's why the trigger is wider than unit: any _geo_distance sort that misses Lucene's built-in distance sort, used as the first sort key, reaches it.

What I think happened

  • Elasticsearch 7.10 (Lucene 8.7) moved this comparator to Lucene's skip-capable DoubleComparator and kept the null field name.
  • In its Lucene 9.1 upgrade, Elasticsearch disabled skipping for this comparator. Today it passes Pruning.NONE.
  • OpenSearch's Lucene 9.1 upgrade ([Upgrade] Lucene 9.1.0-snapshot-ea989fe8f30 OpenSearch#2487) set the other custom comparators to false but passed the geo comparator's request through. Enable Point based optimization for custom comparators OpenSearch#8168 later re-enabled skipping for the numeric comparators and replaced their null field with the real field name, but didn't change the geo comparator.
  • Lucene's documentation for NumericLeafComparator.getNumericDocValues says that if you override it, "you should probably always disable skipping". The geo comparator overrides it. Lucene's own computed-value sort (DoubleValuesSource) also uses Pruning.NONE.

With a null field, skipping never actually happens for this comparator, so the change shouldn't affect sort results; it only stops the null calls. I've tested it locally. Does it make sense to open an issue on opensearch-project/OpenSearch and raise a PR for it?

@cwperks
cwperks merged commit 9f75385 into opensearch-project:2.19 Nov 25, 2026
83 checks passed
@DarshitChanpura

Copy link
Copy Markdown
Member

#6803 claims there is a second latent NPE in isFls/handleKeyword. That is wrong and should be disregarded — on 2.19 handleKeyword already null-guards, and flsFieldInfos.fieldInfo(null) is a HashMap.get(null) returning null, so isFls(null) returns false without throwing. No guard is needed there, and adding one would be dead code. Sorry for the noise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants