Skip to content

Comments

Add XContentFieldFilter (#81970)#3

Open
MitchLewis930 wants to merge 1 commit intopr_013_beforefrom
pr_013_after
Open

Add XContentFieldFilter (#81970)#3
MitchLewis930 wants to merge 1 commit intopr_013_beforefrom
pr_013_after

Conversation

@MitchLewis930
Copy link

@MitchLewis930 MitchLewis930 commented Jan 30, 2026

PR_013


Note

Medium Risk
Touches _source filtering during indexing and get responses, so mistakes could change returned/stored source fields; logic is localized but affects a core data path and adds content-type auto-detection for compressed inputs.

Overview
Adds XContentFieldFilter, a reusable field-filtering abstraction for _source bytes that chooses between map-based filtering (when exclude wildcards are present) and streaming parser-based filtering otherwise, including content-type detection when needed.

Refactors SourceFieldMapper and ShardGetService to use this new filter for include/exclude source filtering, replacing manual convertToMap + XContentMapValues.filter rebuild logic.

Introduces deprecated helper XContentHelper.xContentTypeMayCompressed to detect content type on possibly-compressed bytes for the new streaming filter path.

Written by Cursor Bugbot for commit 4560a0c. This will update automatically on new commits. Configure here.

This commit introduces XContentFieldFilter, which applies field includes/excludes to 
XContent without having to realise the xcontent itself as a java map.  SourceFieldMapper
and ShardGetService are cut over to use this class
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

XContentBuilder builder = new XContentBuilder(contentType.xContent(), streamOutput);
XContentParser parser = contentType.xContent().createParser(parserConfig, originalSource.streamInput());
builder.copyCurrentStructure(parser);
return BytesReference.bytes(builder);
Copy link

Choose a reason for hiding this comment

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

XContentParser resource leak in parser-based filtering branch

Medium Severity

The XContentParser created at line 70 is never closed. XContentParser extends Closeable and wraps an input stream that needs proper cleanup. This resource leak occurs every time the filter is applied when excludes doesn't contain wildcards. The codebase pattern (visible in IndexAbstraction.java and IndexRouting.java) uses try-with-resources for parsers created with streamInput(), but this implementation omits that.

Fix in Cursor Fix in Web

XContentParser parser = contentType.xContent().createParser(parserConfig, originalSource.streamInput());
builder.copyCurrentStructure(parser);
return BytesReference.bytes(builder);
};
Copy link

Choose a reason for hiding this comment

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

Compressed source not decompressed before parsing in filter

High Severity

The parser-based filtering branch detects content type using xContentTypeMayCompressed() which handles compressed bytes, but then passes originalSource.streamInput() directly to the parser without decompressing. If the source is compressed, the parser receives compressed bytes and will fail or produce incorrect results. The map-based branch correctly handles compression via XContentHelper.convertToMap. This is a regression since the old code in ShardGetService always used convertToMap which handles compression properly.

Fix in Cursor Fix in Web

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.

2 participants