-
Notifications
You must be signed in to change notification settings - Fork 282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[CVE-2023-23613] When excluding fields also exclude the term + .keyword
#2375
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/test/java/org/opensearch/security/dlic/dlsfls/FlsKeywordTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.security.dlic.dlsfls; | ||
|
||
import java.util.Arrays; | ||
|
||
import org.apache.hc.core5.http.Header; | ||
import org.apache.hc.core5.http.HttpStatus; | ||
import org.junit.Test; | ||
|
||
import org.opensearch.action.index.IndexRequest; | ||
import org.opensearch.action.support.WriteRequest.RefreshPolicy; | ||
import org.opensearch.client.Client; | ||
import org.opensearch.common.xcontent.XContentType; | ||
import org.opensearch.security.test.DynamicSecurityConfig; | ||
import org.opensearch.security.test.helper.rest.RestHelper.HttpResponse; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.core.IsEqual.equalTo; | ||
import static org.hamcrest.core.IsNot.not; | ||
import static org.hamcrest.core.StringContains.containsString; | ||
|
||
public class FlsKeywordTests extends AbstractDlsFlsTest { | ||
|
||
protected void populateData(Client tc) { | ||
tc.index(new IndexRequest("movies").id("0").setRefreshPolicy(RefreshPolicy.IMMEDIATE) | ||
.source("{\"year\": 2013, \"title\": \"Rush\", \"actors\": [\"Daniel Br\u00FChl\", \"Chris Hemsworth\", \"Olivia Wilde\"]}", XContentType.JSON)).actionGet(); | ||
} | ||
|
||
private Header movieUser = encodeBasicHeader("user_aaa", "password"); | ||
private Header movieNoActorUser = encodeBasicHeader("user_bbb", "password"); | ||
|
||
private String[] actors = new String[] {"Daniel Br\u00FChl", "Chris Hemsworth", "Olivia Wilde"}; | ||
|
||
@Test | ||
public void testKeywordsAreAutomaticallyFiltered() throws Exception { | ||
setup(new DynamicSecurityConfig() | ||
.setSecurityRoles("roles_keyword.yml") | ||
.setSecurityRolesMapping("roles_mappings_keyword.yml")); | ||
|
||
final String searchQuery = "/movies/_search?filter_path=hits.hits._source"; | ||
final String aggQuery = "/movies/_search?filter_path=aggregations.actors.buckets.key"; | ||
final String aggByActorKeyword = "{\"aggs\":{\"actors\":{\"terms\":{\"field\":\"actors.keyword\",\"size\":10}}}}"; | ||
|
||
// At document level, the user should see actors | ||
final HttpResponse searchMovieUser = rh.executeGetRequest(searchQuery, movieUser); | ||
assertThat(searchMovieUser.getStatusCode(), equalTo(HttpStatus.SC_OK)); | ||
assertActorsPresent(searchMovieUser); | ||
|
||
// In aggregate search, the user should see actors | ||
final HttpResponse searchAggregateMovieUser = rh.executePostRequest(aggQuery, aggByActorKeyword, movieUser); | ||
assertThat(searchAggregateMovieUser.getStatusCode(), equalTo(HttpStatus.SC_OK)); | ||
assertActorsPresent(searchAggregateMovieUser); | ||
|
||
// At document level, the user should see no actors | ||
final HttpResponse searchMovieNoActorUser = rh.executeGetRequest(searchQuery, movieNoActorUser); | ||
assertThat(searchMovieNoActorUser.getStatusCode(), equalTo(HttpStatus.SC_OK)); | ||
assertActorsNotPresent(searchMovieNoActorUser); | ||
|
||
// In aggregate search, the user should see no actors | ||
final HttpResponse searchAggregateMovieNoActorUser = rh.executePostRequest(aggQuery, aggByActorKeyword, movieNoActorUser); | ||
assertThat(searchAggregateMovieNoActorUser.getStatusCode(), equalTo(HttpStatus.SC_OK)); | ||
assertActorsNotPresent(searchAggregateMovieNoActorUser); | ||
} | ||
|
||
private void assertActorsPresent(final HttpResponse response) { | ||
Arrays.stream(actors).forEach(actor -> { | ||
assertThat(response.getBody(), containsString(actor)); | ||
}); | ||
} | ||
|
||
private void assertActorsNotPresent(final HttpResponse response) { | ||
Arrays.stream(actors).forEach(actor -> { | ||
assertThat(response.getBody(), not(containsString(actor))); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
_meta: | ||
type: "roles" | ||
config_version: 2 | ||
movies: | ||
index_permissions: | ||
- index_patterns: | ||
- "movies*" | ||
allowed_actions: | ||
- "read" | ||
movies_no_actors: | ||
index_permissions: | ||
- index_patterns: | ||
- "movies*" | ||
fls: | ||
- "~actors" | ||
allowed_actions: | ||
- "read" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
_meta: | ||
type: "rolesmapping" | ||
config_version: 2 | ||
movies: | ||
reserved: false | ||
hidden: false | ||
backend_roles: [] | ||
hosts: [] | ||
users: | ||
- "user_aaa" | ||
and_backend_roles: [] | ||
description: "Movies with all fields" | ||
movies_no_actors: | ||
reserved: false | ||
hidden: false | ||
backend_roles: [] | ||
hosts: [] | ||
users: | ||
- "user_bbb" | ||
and_backend_roles: [] | ||
description: "Movies without actors" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For my knowledge: What would this change accomplish?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When documents are parsed fields are mapped to types, for string mappings a keyword field is automatically created. By additional excluding keyword fields if you wanted to block a field
actors
, we would also blockactors.keyword
to ensure those field values are not included in queries.