Skip to content

Commit

Permalink
Painless: ensure type "UnmodifiableMap" for params (opensearch-projec…
Browse files Browse the repository at this point in the history
…t#13885)

Signed-off-by: Rohit Ashiwal <[email protected]>
  • Loading branch information
r1walz authored and akolarkunnu committed Jun 5, 2024
1 parent ecfd1e8 commit 4e700ed
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Allow clearing `remote_store.compatibility_mode` setting ([#13646](https://github.com/opensearch-project/OpenSearch/pull/13646))
- Fix ReplicaShardBatchAllocator to batch shards without duplicates ([#13710](https://github.com/opensearch-project/OpenSearch/pull/13710))
- Don't return negative scores from `multi_match` query with `cross_fields` type ([#13829](https://github.com/opensearch-project/OpenSearch/pull/13829))
- Painless: ensure type "UnmodifiableMap" for params ([#13885](https://github.com/opensearch-project/OpenSearch/pull/13885))
- Pass parent filter to inner hit query ([#13903](https://github.com/opensearch-project/OpenSearch/pull/13903))
- Fix NPE on restore searchable snapshot ([#13911](https://github.com/opensearch-project/OpenSearch/pull/13911))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,9 @@ public void testInvalidAssignment() {
assertEquals(iae.getMessage(), "invalid assignment: cannot assign a value to addition operation [+]");
iae = expectScriptThrows(IllegalArgumentException.class, () -> exec("Double.x() = 1;"));
assertEquals(iae.getMessage(), "invalid assignment: cannot assign a value to method call [x/0]");

expectScriptThrows(UnsupportedOperationException.class, () -> exec("params['modifyingParamsMap'] = 2;"));
expectScriptThrows(UnsupportedOperationException.class, () -> exec("params.modifyingParamsMap = 2;"));
}

public void testCannotResolveSymbol() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,50 @@
- match: { error.root_cause.0.position.offset: 13 }
- match: { error.root_cause.0.position.start: 0 }
- match: { error.root_cause.0.position.end: 38 }

---
"Test modifying params map from script leads to exception":
- skip:
features: "node_selector"

- do:
put_script:
id: "except"
body: {"script": {"lang": "painless", "source": "params.that = 3"}}

- do:
indices.create:
index: "test"
body:
settings:
index:
number_of_shards: 1
number_of_replicas: 0
mappings:
properties:
this:
type: "integer"
that:
type: "integer"

- do:
index:
index: "test"
id: 1
body: {"this": 1, "that": 2}

- do:
catch: /unsupported_operation_exception/
node_selector:
version: "2.15.0 - "
update:
index: "test"
id: 1
body:
script:
id: "except"
params: {"this": 2}

- match: { error.caused_by.position.offset: 6 }
- match: { error.caused_by.position.start: 0 }
- match: { error.caused_by.position.end: 15 }
2 changes: 1 addition & 1 deletion server/src/main/java/org/opensearch/script/Script.java
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ public Script(StreamInput in) throws IOException {
@SuppressWarnings("unchecked")
Map<String, String> options = (Map<String, String>) (Map) in.readMap();
this.options = options;
this.params = in.readMap();
this.params = Collections.unmodifiableMap(in.readMap());
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion server/src/main/java/org/opensearch/script/UpdateScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

package org.opensearch.script;

import java.util.Collections;
import java.util.Map;

/**
Expand All @@ -53,7 +54,7 @@ public abstract class UpdateScript {
private final Map<String, Object> ctx;

public UpdateScript(Map<String, Object> params, Map<String, Object> ctx) {
this.params = params;
this.params = Collections.unmodifiableMap(params);
this.ctx = ctx;
}

Expand Down

0 comments on commit 4e700ed

Please sign in to comment.