Skip to content

[BUG] NullPointerException in DlsFlsFilterLeafReader.isMasked when sorting by _geo_distance with field masking active #6803

Todo

Description

@DarshitChanpura

What is the bug?

On the 2.19 branch, when field masking is active on an index, a search that sorts by _geo_distance fails on every shard with an HTTP 500 null_pointer_exception:

Cannot invoke "String.endsWith(String)" because "field" is null

DlsFlsFilterLeafReader.getPointValues is invoked with a null field name and passes it to isAllowed, which dereferences it without a null check:

@Override
public PointValues getPointValues(String field) throws IOException {
    return isAllowed(field) ? in.getPointValues(field) : null;
}

private boolean isAllowed(String fieldName) {
    return this.metaFields.contains(fieldName) || (isFls(fieldName) && (!isMasked(fieldName)));
}

private boolean isMasked(String field) {
    // Doc-value masking also applies a parent field's rule to its keyword subfield.
    return this.maskedFieldsMap.anyMatch(field)
        || (field.endsWith(KEYWORD) && this.maskedFieldsMap.anyMatch(handleKeyword(field)));
}

maskedFieldsMap.anyMatch(null) returns false rather than throwing, so evaluation reaches field.endsWith(KEYWORD) and throws there.

Stack trace:

org.opensearch.security.configuration.DlsFlsFilterLeafReader.isMasked
  ← DlsFlsFilterLeafReader.isAllowed
  ← DlsFlsFilterLeafReader.getPointValues
  ← org.apache.lucene.index.ExitableDirectoryReader$ExitableLeafReader.getPointValues
  ← org.apache.lucene.search.comparators.NumericComparator / DoubleComparator
  ← org.opensearch.search.sort.GeoDistanceSortBuilder

The failure is conditional on two things:

  1. The _geo_distance sort must be the first sort key. Moving it to second position avoids the failure.
  2. The sort clause must specify "unit". Removing unit avoids the failure.

Both conditions together suggest the request only breaks when Lucene takes its point-values pruning path for the leading sort field, which is the only path that reaches getPointValues.

The failure is fail-closed: the query is aborted and no field content is returned, so this is a functional/availability regression rather than a data-exposure issue.

This is already fixed on main by #5537, which replaces stripKeywordSuffix with a null-tolerant normalizeFieldName in FieldPrivileges and FieldMasking. Its changelog entry covers this symptom directly:

Fix NullPointerExceptions for "missing values" term aggregations and sorting on geo points (#5537)

The 2.19 branch is still affected, and #5537 is not portable to it as-is. Both files that PR modifies — privileges/dlsfls/FieldPrivileges.java and privileges/dlsfls/FieldMasking.java — do not exist on 2.19, as that package arrived with the DLS/FLS rework in 3.x. A cherry-pick leaves only the CHANGELOG hunk applying. The 2.19 CHANGELOG has no #5537 entry, and I could not find an existing 2.19 backport PR.

This issue therefore tracks a 2.19 backport of #5537 rather than a new defect on main.

How can one reproduce the bug?

  1. Create an index with a geo_point field:
PUT /places-v1
{
  "mappings": {
    "properties": {
      "location": { "type": "geo_point" },
      "status":   { "type": "keyword" },
      "owner":    { "type": "keyword" }
    }
  }
}
  1. Index a document:
POST /places-v1/_doc?refresh=true
{ "location": { "lat": 47.6, "lon": -122.3 }, "status": "active", "owner": "alice" }
  1. Define a role that applies field masking to the index, and map a test user to it:
masked_role:
  index_permissions:
    - index_patterns: ["places-v1"]
      allowed_actions: ["read"]
      masked_fields: ["owner"]
  1. As that user, search with _geo_distance as the first sort key, specifying unit:
GET /places-v1/_search
{
  "sort": [
    {
      "_geo_distance": {
        "location": { "lat": 47.6, "lon": -122.3 },
        "order": "asc",
        "unit": "mi"
      }
    }
  ]
}
  1. See error: HTTP 500, null_pointer_exception on all shards.

Both of the following succeed, which isolates the trigger:

  • the same query with "unit" removed;
  • the same query with a non-geo sort key placed first and the _geo_distance sort second.

The failure does not occur for a user whose roles apply no field masking to the index, since DlsFlsFilterLeafReader is not installed in that case.

What is the expected behavior?

The search should succeed and return hits sorted by distance. A null field name reaching getPointValues should not produce a NullPointerException. This is the behaviour main already has after #5537.

What is your host/environment?

Do you have any screenshots?

N/A — the failure is an API-level HTTP 500.

Do you have any additional context?

The underlying cause was introduced by the fix for GHSA-rrmm-wq7q-h4v5 (resolved in 2.19.3 and 3.0.0), which extended field-masking enforcement to the point-values path. Before that change getPointValues was not overridden in DlsFlsFilterLeafReader, so a null field name never reached this code.

There is a second, latent instance of the same bug on 2.19, which a fix limited to isMasked would miss:

private boolean isFls(final String name) {
    if (!flsEnabled) {
        return true;
    }
    return flsFieldInfos.fieldInfo(handleKeyword(name)) != null;
}

With field masking but no FLS, isFls short-circuits on !flsEnabled and never dereferences the name — which is why the reproducer above reaches isMasked. With FLS enabled, handleKeyword(null) should throw on the same query. Worth covering both in one change.

One semantic note for the 2.19 patch: in isMasked, returning false for a null name reads as "not masked", so isAllowed returns true and in.getPointValues(null) is called; returning true reads as "treat as masked", so isAllowed is false and getPointValues returns null, which Lucene treats as "no point values for this field". The latter is fail-closed and matches what #5537 achieves with its "__null__" sentinel. Getting it backwards would trade the NPE for a weakening of the enforcement added by GHSA-rrmm-wq7q-h4v5.

Happy to open a 2.19 PR if that would help, though I would rather follow the maintainers' preference on the guard semantics given the security surface.

Finally, it would be worth checking whether the other PointValues-backed types named in the advisory (ip, geo_shape, xy_point, xy_shape) can reach these methods with a null field name through their own sort or query paths — on both branches.

Activity

  1. added
    bug
    on Nov 24, 2026
  2. added
    untriaged
    on Nov 24, 2026
  3. srikdhruv commented on Nov 24, 2026

    @srikdhruv
    Contributor

    Hi, I'd like to take this one.

  4. cwperks commented on Nov 24, 2026

    @cwperks
    Member

    Related, but may not be exactly the same: #5537

  5. added a commit that references this issue on Nov 24, 2026
    a4af4f6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

buguntriaged

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions