diff --git a/docs/docusaurus/docs/bpmn/ch04-API.md b/docs/docusaurus/docs/bpmn/ch04-API.md index 4f00c96ddd1..04067211631 100644 --- a/docs/docusaurus/docs/bpmn/ch04-API.md +++ b/docs/docusaurus/docs/bpmn/ch04-API.md @@ -197,7 +197,7 @@ Instead of using Java serialization: The behavior of the *serializable* variable type can be configured through the `serializableVariableTypeMode` property of the engine configuration: -- `DISABLED` (default): serializable variables can neither be written nor read. Existing serializable variables can still be deleted, but reading their value fails. +- `DISABLED` (default): serializable variables can neither be written nor read. Existing serializable variables can still be deleted or overwritten with a value of another type, but reading their value fails. - `READ_ONLY`: existing serializable variables can be read (and overwritten with a value of another type), but no new serializable values can be stored. Use this mode to migrate existing data away from Java serialization. diff --git a/docs/docusaurus/docs/bpmn/ch14-REST.md b/docs/docusaurus/docs/bpmn/ch14-REST.md index 818a6f096bc..14527f89a81 100644 --- a/docs/docusaurus/docs/bpmn/ch14-REST.md +++ b/docs/docusaurus/docs/bpmn/ch14-REST.md @@ -463,6 +463,8 @@ It’s possible to support additional variable-types with a custom JSON represen Variables of type *serializable* (Java serialized objects) are disabled by default. Uploading a serialized object (`application/x-java-serialized-object`) or downloading the data of a *serializable* variable returns a `415` response. To allow it, set the `rest.variables.allow.serializable` property to `true`. The `serializableVariableTypeMode` of the engine needs to allow it as well, see [Serializable variables](bpmn/ch04-API.md#serializable-variables). Only enable this when the REST API is used by fully trusted clients, since deserializing Java objects from a request is a security risk. +A variable with a JSON object or array as value needs to have the `json` type. Without a type such a value could only be stored using Java serialization, therefore the request is rejected with a `400` response. This does not apply to transient variables. + ## Deployment **When using Tomcat, please read [Usage in Tomcat](#usage-In-tomcat).** diff --git a/docs/docusaurus/docs/cmmn/ch08-REST.md b/docs/docusaurus/docs/cmmn/ch08-REST.md index 7ebbc5e3567..58be7690be8 100644 --- a/docs/docusaurus/docs/cmmn/ch08-REST.md +++ b/docs/docusaurus/docs/cmmn/ch08-REST.md @@ -453,6 +453,8 @@ It’s possible to support additional variable-types with a custom JSON represen Variables of type *serializable* (Java serialized objects) are disabled by default. Uploading a serialized object (`application/x-java-serialized-object`) or downloading the data of a *serializable* variable returns a `415` response. To allow it, set the `rest.variables.allow.serializable` property to `true`. The `serializableVariableTypeMode` of the engine needs to allow it as well, see [Serializable variables](bpmn/ch04-API.md#serializable-variables). Only enable this when the REST API is used by fully trusted clients, since deserializing Java objects from a request is a security risk. +A variable with a JSON object or array as value needs to have the `json` type. Without a type such a value could only be stored using Java serialization, therefore the request is rejected with a `400` response. This does not apply to transient variables. + ## Deployment **When using Tomcat, please read [Usage in Tomcat](#usage-in-tomcat).** diff --git a/modules/flowable-app-engine/src/main/java/org/flowable/app/engine/AppEngineConfiguration.java b/modules/flowable-app-engine/src/main/java/org/flowable/app/engine/AppEngineConfiguration.java index c662c887445..86e2a2af651 100644 --- a/modules/flowable-app-engine/src/main/java/org/flowable/app/engine/AppEngineConfiguration.java +++ b/modules/flowable-app-engine/src/main/java/org/flowable/app/engine/AppEngineConfiguration.java @@ -90,8 +90,8 @@ import org.flowable.variable.service.impl.types.DateType; import org.flowable.variable.service.impl.types.DefaultVariableTypes; import org.flowable.variable.service.impl.types.DoubleType; -import org.flowable.variable.service.impl.types.FloatType; import org.flowable.variable.service.impl.types.EmptyCollectionType; +import org.flowable.variable.service.impl.types.FloatType; import org.flowable.variable.service.impl.types.InstantType; import org.flowable.variable.service.impl.types.IntegerType; import org.flowable.variable.service.impl.types.JodaDateTimeType; diff --git a/modules/flowable-cmmn-engine/src/main/java/org/flowable/cmmn/engine/CmmnEngineConfiguration.java b/modules/flowable-cmmn-engine/src/main/java/org/flowable/cmmn/engine/CmmnEngineConfiguration.java index 522b051d6cf..8809d8e0137 100644 --- a/modules/flowable-cmmn-engine/src/main/java/org/flowable/cmmn/engine/CmmnEngineConfiguration.java +++ b/modules/flowable-cmmn-engine/src/main/java/org/flowable/cmmn/engine/CmmnEngineConfiguration.java @@ -323,8 +323,8 @@ import org.flowable.variable.service.impl.types.DateType; import org.flowable.variable.service.impl.types.DefaultVariableTypes; import org.flowable.variable.service.impl.types.DoubleType; -import org.flowable.variable.service.impl.types.FloatType; import org.flowable.variable.service.impl.types.EmptyCollectionType; +import org.flowable.variable.service.impl.types.FloatType; import org.flowable.variable.service.impl.types.InstantType; import org.flowable.variable.service.impl.types.IntegerType; import org.flowable.variable.service.impl.types.JodaDateTimeType; diff --git a/modules/flowable-cmmn-engine/src/test/java/org/flowable/cmmn/test/runtime/FloatVariableTest.java b/modules/flowable-cmmn-engine/src/test/java/org/flowable/cmmn/test/runtime/FloatVariableTest.java new file mode 100644 index 00000000000..7a053330c11 --- /dev/null +++ b/modules/flowable-cmmn-engine/src/test/java/org/flowable/cmmn/test/runtime/FloatVariableTest.java @@ -0,0 +1,66 @@ +/* Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.flowable.cmmn.test.runtime; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.entry; + +import org.flowable.cmmn.api.runtime.CaseInstance; +import org.flowable.cmmn.engine.test.CmmnDeployment; +import org.flowable.cmmn.test.FlowableCmmnTestCase; +import org.flowable.variable.api.history.HistoricVariableInstance; +import org.flowable.variable.api.persistence.entity.VariableInstance; +import org.flowable.variable.service.impl.types.FloatType; +import org.junit.jupiter.api.Test; + +/** + * @author Filip Hrisafov + */ +class FloatVariableTest extends FlowableCmmnTestCase { + + @Test + @CmmnDeployment(resources = "org/flowable/cmmn/test/one-human-task-model.cmmn") + void testFloatVariable() { + CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder() + .caseDefinitionKey("oneTaskCase") + .variable("floatVar", 1.1f) + .variable("maxFloatVar", Float.MAX_VALUE) + .start(); + + VariableInstance variableInstance = cmmnRuntimeService.getVariableInstance(caseInstance.getId(), "floatVar"); + assertThat(variableInstance.getTypeName()).isEqualTo(FloatType.TYPE_NAME); + assertThat(variableInstance.getValue()).isEqualTo(1.1f); + + assertThat(cmmnRuntimeService.getVariables(caseInstance.getId())) + .containsOnly( + entry("floatVar", 1.1f), + entry("maxFloatVar", Float.MAX_VALUE) + ); + + assertThat(cmmnRuntimeService.createCaseInstanceQuery().variableValueEquals("floatVar", 1.1f).list()) + .extracting(CaseInstance::getId) + .containsExactly(caseInstance.getId()); + assertThat(cmmnRuntimeService.createCaseInstanceQuery().variableValueGreaterThan("floatVar", 1.0f).count()).isEqualTo(1); + assertThat(cmmnRuntimeService.createCaseInstanceQuery().variableValueEquals("floatVar", 1.2f).count()).isZero(); + + cmmnRuntimeService.setVariable(caseInstance.getId(), "floatVar", 2.5f); + assertThat(cmmnRuntimeService.getVariable(caseInstance.getId(), "floatVar")).isEqualTo(2.5f); + + HistoricVariableInstance historicVariableInstance = cmmnHistoryService.createHistoricVariableInstanceQuery() + .caseInstanceId(caseInstance.getId()) + .variableName("floatVar") + .singleResult(); + assertThat(historicVariableInstance.getVariableTypeName()).isEqualTo(FloatType.TYPE_NAME); + assertThat(historicVariableInstance.getValue()).isEqualTo(2.5f); + } +} diff --git a/modules/flowable-cmmn-engine/src/test/java/org/flowable/cmmn/test/runtime/SerializableVariableTypeModeTest.java b/modules/flowable-cmmn-engine/src/test/java/org/flowable/cmmn/test/runtime/SerializableVariableTypeModeTest.java new file mode 100644 index 00000000000..85358f676fb --- /dev/null +++ b/modules/flowable-cmmn-engine/src/test/java/org/flowable/cmmn/test/runtime/SerializableVariableTypeModeTest.java @@ -0,0 +1,283 @@ +/* Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.flowable.cmmn.test.runtime; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.tuple; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +import org.flowable.cmmn.api.CmmnManagementService; +import org.flowable.cmmn.api.CmmnRuntimeService; +import org.flowable.cmmn.api.delegate.DelegatePlanItemInstance; +import org.flowable.cmmn.api.delegate.PlanItemJavaDelegate; +import org.flowable.cmmn.api.runtime.CaseInstance; +import org.flowable.cmmn.engine.CmmnEngine; +import org.flowable.cmmn.engine.CmmnEngineConfiguration; +import org.flowable.cmmn.engine.impl.cfg.StandaloneInMemCmmnEngineConfiguration; +import org.flowable.common.engine.impl.logging.LoggingListener; +import org.flowable.common.engine.impl.logging.LoggingSessionConstants; +import org.flowable.job.api.FlowableUnrecoverableJobException; +import org.flowable.job.api.Job; +import org.flowable.variable.api.persistence.entity.VariableInstance; +import org.flowable.variable.service.impl.types.SerializableType; +import org.flowable.variable.service.impl.types.SerializableVariableTypeMode; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import tools.jackson.databind.node.ObjectNode; + +/** + * @author Filip Hrisafov + */ +class SerializableVariableTypeModeTest { + + protected static final String JDBC_URL = "jdbc:h2:mem:cmmnSerializableVariableTypeModeTest"; + + protected List cmmnEngines = new ArrayList<>(); + + @AfterEach + void tearDown() { + // Close in reverse order, the first engine is the one that drops the schema + for (int i = cmmnEngines.size() - 1; i >= 0; i--) { + cmmnEngines.get(i).close(); + } + } + + @Test + void defaultModeIsDisabled() { + assertThat(new StandaloneInMemCmmnEngineConfiguration().getSerializableVariableTypeMode()) + .isEqualTo(SerializableVariableTypeMode.DISABLED); + } + + @Test + void enabledMode() { + CmmnEngine enabledCmmnEngine = buildCmmnEngine(SerializableVariableTypeMode.ENABLED); + CaseInstance caseInstance = startCaseWithSerializableVariable(enabledCmmnEngine); + CmmnRuntimeService runtimeService = enabledCmmnEngine.getCmmnRuntimeService(); + + assertThat(runtimeService.getVariable(caseInstance.getId(), "serializableVar")) + .isEqualTo(new TestSerializableVariable(10)); + + runtimeService.setVariable(caseInstance.getId(), "serializableVar", new TestSerializableVariable(20)); + assertThat(runtimeService.getVariable(caseInstance.getId(), "serializableVar")) + .isEqualTo(new TestSerializableVariable(20)); + } + + @Test + void readOnlyMode() { + CmmnEngine enabledCmmnEngine = buildCmmnEngine(SerializableVariableTypeMode.ENABLED); + CaseInstance caseInstance = startCaseWithSerializableVariable(enabledCmmnEngine); + CmmnRuntimeService runtimeService = buildCmmnEngine(SerializableVariableTypeMode.READ_ONLY).getCmmnRuntimeService(); + + assertThat(runtimeService.getVariable(caseInstance.getId(), "serializableVar")) + .isEqualTo(new TestSerializableVariable(10)); + + assertThatThrownBy(() -> runtimeService.setVariable(caseInstance.getId(), "serializableVar", new TestSerializableVariable(20))) + .isExactlyInstanceOf(FlowableUnrecoverableJobException.class) + .hasMessageContaining("Storing the value of variable 'serializableVar' of type " + TestSerializableVariable.class.getName() + + " is not allowed"); + + assertThatThrownBy(() -> runtimeService.setVariable(caseInstance.getId(), "otherVar", new TestSerializableVariable(30))) + .isExactlyInstanceOf(FlowableUnrecoverableJobException.class) + .hasMessageContaining("Storing the value of variable 'otherVar' of type " + TestSerializableVariable.class.getName() + + " is not allowed"); + + assertThat(runtimeService.getVariable(caseInstance.getId(), "serializableVar")) + .isEqualTo(new TestSerializableVariable(10)); + + // Migrating the existing variable to another type is possible + runtimeService.setVariable(caseInstance.getId(), "serializableVar", "migrated"); + VariableInstance variableInstance = runtimeService.getVariableInstance(caseInstance.getId(), "serializableVar"); + assertThat(variableInstance.getTypeName()).isEqualTo("string"); + assertThat(variableInstance.getValue()).isEqualTo("migrated"); + } + + @Test + void readOnlyModeDoesNotUpdateTrackedObjects() { + CmmnEngine enabledCmmnEngine = buildCmmnEngine(SerializableVariableTypeMode.ENABLED); + CaseInstance caseInstance = startCaseWithSerializableVariable(enabledCmmnEngine); + CmmnEngine readOnlyCmmnEngine = buildCmmnEngine(SerializableVariableTypeMode.READ_ONLY); + + readOnlyCmmnEngine.getCmmnEngineConfiguration().getCommandExecutor().execute(commandContext -> { + TestSerializableVariable value = (TestSerializableVariable) readOnlyCmmnEngine.getCmmnRuntimeService() + .getVariable(caseInstance.getId(), "serializableVar"); + value.setNumber(20); + return null; + }); + + assertThat(enabledCmmnEngine.getCmmnRuntimeService().getVariable(caseInstance.getId(), "serializableVar")) + .isEqualTo(new TestSerializableVariable(10)); + } + + @Test + void disabledMode() { + CmmnEngine enabledCmmnEngine = buildCmmnEngine(SerializableVariableTypeMode.ENABLED); + CaseInstance caseInstance = startCaseWithSerializableVariable(enabledCmmnEngine); + CmmnEngine disabledCmmnEngine = buildCmmnEngine(SerializableVariableTypeMode.DISABLED); + CmmnRuntimeService runtimeService = disabledCmmnEngine.getCmmnRuntimeService(); + + assertThatThrownBy(() -> runtimeService.getVariable(caseInstance.getId(), "serializableVar")) + .isExactlyInstanceOf(FlowableUnrecoverableJobException.class) + .hasMessageContaining("Reading the value of serializable variable 'serializableVar' is not allowed, because serializable variables are disabled"); + + assertThatThrownBy(() -> runtimeService.setVariable(caseInstance.getId(), "otherVar", new TestSerializableVariable(30))) + .isExactlyInstanceOf(FlowableUnrecoverableJobException.class) + .hasMessageContaining("Storing the value of variable 'otherVar' of type " + TestSerializableVariable.class.getName() + + " is not allowed, because serializable variables are disabled"); + + // The variable instance can still be loaded without accessing the value + assertThat(runtimeService.createVariableInstanceQuery().caseInstanceId(caseInstance.getId()).excludeVariableInitialization().list()) + .extracting(VariableInstance::getName, VariableInstance::getTypeName) + .containsExactly(tuple("serializableVar", SerializableType.TYPE_NAME)); + + // Removing the variable is possible + runtimeService.removeVariable(caseInstance.getId(), "serializableVar"); + assertThat(runtimeService.hasVariable(caseInstance.getId(), "serializableVar")).isFalse(); + } + + @Test + void disabledModeVariableCanBeOverwrittenWithAnotherType() { + CmmnEngine enabledCmmnEngine = buildCmmnEngine(SerializableVariableTypeMode.ENABLED); + CaseInstance caseInstance = startCaseWithSerializableVariable(enabledCmmnEngine); + List loggingNodes = new ArrayList<>(); + CmmnRuntimeService runtimeService = buildCmmnEngine(SerializableVariableTypeMode.DISABLED, loggingNodes::addAll).getCmmnRuntimeService(); + + // The existing value does not need to be read for replacing it with a value of another type + runtimeService.setVariable(caseInstance.getId(), "serializableVar", "migrated"); + + VariableInstance variableInstance = runtimeService.getVariableInstance(caseInstance.getId(), "serializableVar"); + assertThat(variableInstance.getTypeName()).isEqualTo("string"); + assertThat(variableInstance.getValue()).isEqualTo("migrated"); + + // The update is logged without the old serializable value + assertThat(loggingNodes) + .filteredOn(loggingNode -> LoggingSessionConstants.TYPE_VARIABLE_UPDATE.equals(loggingNode.path("type").asString())) + .singleElement() + .satisfies(loggingNode -> { + assertThat(loggingNode.path("variableName").asString()).isEqualTo("serializableVar"); + assertThat(loggingNode.path("variableValue").asString()).isEqualTo("migrated"); + assertThat(loggingNode.path("oldVariableType").asString()).isEqualTo(SerializableType.TYPE_NAME); + assertThat(loggingNode.has("oldVariableValue")).isFalse(); + }); + } + + @Test + void disabledModeCaseInstanceCanBeTerminatedAndDeleted() { + CmmnEngine enabledCmmnEngine = buildCmmnEngine(SerializableVariableTypeMode.ENABLED); + CaseInstance caseInstance = startCaseWithSerializableVariable(enabledCmmnEngine); + CmmnEngine disabledCmmnEngine = buildCmmnEngine(SerializableVariableTypeMode.DISABLED); + + disabledCmmnEngine.getCmmnRuntimeService().terminateCaseInstance(caseInstance.getId()); + assertThat(disabledCmmnEngine.getCmmnRuntimeService().createCaseInstanceQuery().caseInstanceId(caseInstance.getId()).count()).isZero(); + + disabledCmmnEngine.getCmmnHistoryService().deleteHistoricCaseInstance(caseInstance.getId()); + assertThat(disabledCmmnEngine.getCmmnHistoryService().createHistoricVariableInstanceQuery().caseInstanceId(caseInstance.getId()).count()).isZero(); + } + + @Test + void disabledModeFailsJobWithoutRetries() { + CmmnEngine disabledCmmnEngine = buildCmmnEngine(SerializableVariableTypeMode.DISABLED); + disabledCmmnEngine.getCmmnRepositoryService().createDeployment() + .addClasspathResource("org/flowable/cmmn/test/runtime/SerializableVariableTypeModeTest.asyncSerializableVariable.cmmn") + .deploy(); + + CaseInstance caseInstance = disabledCmmnEngine.getCmmnRuntimeService().createCaseInstanceBuilder() + .caseDefinitionKey("asyncSerializableVariable") + .start(); + CmmnManagementService managementService = disabledCmmnEngine.getCmmnManagementService(); + Job job = managementService.createJobQuery().caseInstanceId(caseInstance.getId()).singleResult(); + + assertThatThrownBy(() -> managementService.executeJob(job.getId())) + .isExactlyInstanceOf(FlowableUnrecoverableJobException.class) + .hasMessageContaining("Storing the value of variable 'serializableVar'"); + + // Retrying the job would not help, so it is moved to the dead letter jobs right away + assertThat(managementService.createJobQuery().caseInstanceId(caseInstance.getId()).count()).isZero(); + assertThat(managementService.createDeadLetterJobQuery().caseInstanceId(caseInstance.getId()).singleResult()) + .extracting(Job::getRetries) + .isEqualTo(0); + } + + /** + * Starts a case instance with a serializable variable, using a CMMN engine that has serializable variables enabled. + */ + protected CaseInstance startCaseWithSerializableVariable(CmmnEngine enabledCmmnEngine) { + enabledCmmnEngine.getCmmnRepositoryService().createDeployment() + .addClasspathResource("org/flowable/cmmn/test/one-human-task-model.cmmn") + .deploy(); + + return enabledCmmnEngine.getCmmnRuntimeService().createCaseInstanceBuilder() + .caseDefinitionKey("oneTaskCase") + .variable("serializableVar", new TestSerializableVariable(10)) + .start(); + } + + protected CmmnEngine buildCmmnEngine(SerializableVariableTypeMode mode) { + return buildCmmnEngine(mode, null); + } + + protected CmmnEngine buildCmmnEngine(SerializableVariableTypeMode mode, LoggingListener loggingListener) { + // The first CMMN engine creates the schema (and drops it when it is closed), the other ones are using the same database + String databaseSchemaUpdate = cmmnEngines.isEmpty() ? CmmnEngineConfiguration.DB_SCHEMA_UPDATE_CREATE_DROP : CmmnEngineConfiguration.DB_SCHEMA_UPDATE_FALSE; + CmmnEngineConfiguration cmmnEngineConfiguration = new StandaloneInMemCmmnEngineConfiguration(); + cmmnEngineConfiguration.setJdbcUrl(JDBC_URL); + cmmnEngineConfiguration.setDatabaseSchemaUpdate(databaseSchemaUpdate); + cmmnEngineConfiguration.setCmmnEngineName("cmmnSerializableVariableTypeModeTest" + cmmnEngines.size()); + cmmnEngineConfiguration.setSerializableVariableTypeMode(mode); + cmmnEngineConfiguration.setLoggingListener(loggingListener); + CmmnEngine cmmnEngine = cmmnEngineConfiguration.buildCmmnEngine(); + cmmnEngines.add(cmmnEngine); + return cmmnEngine; + } + + public static class SetSerializableVariableDelegate implements PlanItemJavaDelegate { + + @Override + public void execute(DelegatePlanItemInstance planItemInstance) { + planItemInstance.setVariable("serializableVar", new TestSerializableVariable(10)); + } + } + + public static class TestSerializableVariable implements Serializable { + + private static final long serialVersionUID = 1L; + + protected int number; + + public TestSerializableVariable(int number) { + this.number = number; + } + + public int getNumber() { + return number; + } + + public void setNumber(int number) { + this.number = number; + } + + @Override + public boolean equals(Object o) { + return o instanceof TestSerializableVariable other && number == other.number; + } + + @Override + public int hashCode() { + return Integer.hashCode(number); + } + } +} diff --git a/modules/flowable-cmmn-engine/src/test/resources/org/flowable/cmmn/test/runtime/SerializableVariableTypeModeTest.asyncSerializableVariable.cmmn b/modules/flowable-cmmn-engine/src/test/resources/org/flowable/cmmn/test/runtime/SerializableVariableTypeModeTest.asyncSerializableVariable.cmmn new file mode 100644 index 00000000000..40c8d2174f8 --- /dev/null +++ b/modules/flowable-cmmn-engine/src/test/resources/org/flowable/cmmn/test/runtime/SerializableVariableTypeModeTest.asyncSerializableVariable.cmmn @@ -0,0 +1,15 @@ + + + + + + + + + + + + diff --git a/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/CmmnRestResponseFactory.java b/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/CmmnRestResponseFactory.java index de6141cdd98..d8709c6cc59 100644 --- a/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/CmmnRestResponseFactory.java +++ b/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/CmmnRestResponseFactory.java @@ -76,6 +76,7 @@ import org.flowable.common.rest.variable.LocalDateTimeRestVariableConverter; import org.flowable.common.rest.variable.LongRestVariableConverter; import org.flowable.common.rest.variable.RestVariableConverter; +import org.flowable.common.rest.variable.SerializableRestVariableUtil; import org.flowable.common.rest.variable.ShortRestVariableConverter; import org.flowable.common.rest.variable.StringRestVariableConverter; import org.flowable.common.rest.variable.UUIDRestVariableConverter; @@ -479,7 +480,22 @@ public RestVariable createBinaryRestVariable(String name, RestVariableScope scop return restVar; } + /** + * Get the value of a variable that is going to be stored. + * A JSON object or array is only supported when the json type is provided. + */ public Object getVariableValue(RestVariable restVariable) { + return getVariableValue(restVariable, false); + } + + /** + * Get the value of a transient variable. Since it is not stored, a JSON object or array without a type is supported as well. + */ + public Object getTransientVariableValue(RestVariable restVariable) { + return getVariableValue(restVariable, true); + } + + protected Object getVariableValue(RestVariable restVariable, boolean allowUntypedJsonValue) { Object value = null; if (restVariable.getType() != null) { @@ -500,6 +516,9 @@ public Object getVariableValue(RestVariable restVariable) { // Revert to type determined by REST-to-Java mapping when no // explicit type has been provided value = restVariable.getValue(); + if (!allowUntypedJsonValue) { + SerializableRestVariableUtil.validateUntypedVariableValue(restVariable.getName(), value); + } } return value; } diff --git a/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/history/caze/HistoricCaseInstanceVariableDataResource.java b/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/history/caze/HistoricCaseInstanceVariableDataResource.java index b0d1b3c327c..86a1e1ffd23 100644 --- a/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/history/caze/HistoricCaseInstanceVariableDataResource.java +++ b/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/history/caze/HistoricCaseInstanceVariableDataResource.java @@ -62,14 +62,14 @@ public class HistoricCaseInstanceVariableDataResource extends HistoricCaseInstan @Autowired protected CmmnHistoryService historyService; - @ApiResponses(value = { - @ApiResponse(code = 200, message = "Indicates the case instance was found and the requested variable data is returned."), - @ApiResponse(code = 404, message = "Indicates the requested case instance was not found or the process instance does not have a variable with the given name or the variable does not have a binary stream available. Status message provides additional information.") }) @Override public void afterPropertiesSet() { isSerializableVariableAllowed = SerializableRestVariableUtil.isSerializableVariableAllowed(env); } + @ApiResponses(value = { + @ApiResponse(code = 200, message = "Indicates the case instance was found and the requested variable data is returned."), + @ApiResponse(code = 404, message = "Indicates the requested case instance was not found or the process instance does not have a variable with the given name or the variable does not have a binary stream available. Status message provides additional information.") }) @ApiOperation(value = "Get the binary data for a historic case instance variable", tags = {"History Process" }, nickname = "getHistoricCaseInstanceVariableData", notes = "The response body contains the binary value of the variable. When the variable is of type binary, the content-type of the response is set to application/octet-stream, regardless of the content of the variable or the request accept-type header. In case of serializable, application/x-java-serialized-object is used as content-type.") @GetMapping(value = "/cmmn-history/historic-case-instances/{caseInstanceId}/variables/{variableName}/data") @@ -106,13 +106,23 @@ public byte[] getVariableData(@ApiParam(name = "caseInstanceId") @PathVariable(" } public RestVariable getVariableFromRequest(boolean includeBinary, String caseInstanceId, String variableName) { + // The value of the variable is resolved in the same command as the query, after validating that its data can be returned + return cmmnEngineConfiguration.getCommandExecutor().execute(commandContext -> readVariable(includeBinary, caseInstanceId, variableName)); + } + + protected RestVariable readVariable(boolean includeBinary, String caseInstanceId, String variableName) { HistoricCaseInstance caseObject = getHistoricCaseInstanceFromRequest(caseInstanceId); HistoricVariableInstance variable = historyService.createHistoricVariableInstanceQuery() .caseInstanceId(caseObject.getId()) .variableName(variableName) + .excludeVariableInitialization() .singleResult(); + if (variable != null && includeBinary) { + SerializableRestVariableUtil.validateSerializableVariableAllowed(variable.getVariableTypeName(), isSerializableVariableAllowed); + } + if (variable == null || variable.getValue() == null) { throw new FlowableObjectNotFoundException("Historic case instance '" + caseInstanceId + "' variable value for " + variableName + " couldn't be found.", VariableInstanceEntity.class); } else { diff --git a/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/history/task/HistoricTaskInstanceVariableDataResource.java b/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/history/task/HistoricTaskInstanceVariableDataResource.java index c272852e3cc..e5557e84baa 100644 --- a/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/history/task/HistoricTaskInstanceVariableDataResource.java +++ b/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/history/task/HistoricTaskInstanceVariableDataResource.java @@ -154,6 +154,9 @@ protected RestVariable readVariable(boolean includeBinary, String taskId, String variableInstance = getProcessVariable(taskObject, variableName); } } + if (variableInstance != null && includeBinary) { + SerializableRestVariableUtil.validateSerializableVariableAllowed(variableInstance.getVariableTypeName(), isSerializableVariableAllowed); + } Object value = variableInstance != null ? variableInstance.getValue() : null; if (value == null) { diff --git a/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/history/variable/HistoricVariableInstanceBaseResource.java b/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/history/variable/HistoricVariableInstanceBaseResource.java index 00ccfc9b35d..f0bbbc4a58e 100644 --- a/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/history/variable/HistoricVariableInstanceBaseResource.java +++ b/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/history/variable/HistoricVariableInstanceBaseResource.java @@ -106,7 +106,12 @@ protected DataResponse getQueryResponse(Histor } public RestVariable getVariableFromRequest(boolean includeBinary, String varInstanceId) { - HistoricVariableInstance varObject = historyService.createHistoricVariableInstanceQuery().id(varInstanceId).singleResult(); + // The value of the variable is resolved in the same command as the query, after validating that its data can be returned + return cmmnEngineConfiguration.getCommandExecutor().execute(commandContext -> readVariable(includeBinary, varInstanceId)); + } + + protected RestVariable readVariable(boolean includeBinary, String varInstanceId) { + HistoricVariableInstance varObject = historyService.createHistoricVariableInstanceQuery().id(varInstanceId).excludeVariableInitialization().singleResult(); if (varObject == null) { throw new FlowableObjectNotFoundException("Historic variable instance '" + varInstanceId + "' couldn't be found.", VariableInstanceEntity.class); @@ -114,10 +119,20 @@ public RestVariable getVariableFromRequest(boolean includeBinary, String varInst if (restApiInterceptor != null) { restApiInterceptor.accessHistoryVariableInfoById(varObject); } + if (includeBinary) { + validateVariableDataAllowed(varObject.getVariableTypeName()); + } return restResponseFactory.createRestVariable(varObject.getVariableName(), varObject.getValue(), null, varInstanceId, CmmnRestResponseFactory.VARIABLE_HISTORY_VARINSTANCE, includeBinary); } } + /** + * Validates that the binary data of a variable with the given type can be returned, before the value of the variable is resolved. + */ + protected void validateVariableDataAllowed(String variableTypeName) { + // Nothing to validate by default + } + protected void addVariables(HistoricVariableInstanceQuery variableInstanceQuery, List variables) { for (QueryVariable variable : variables) { if (variable.getVariableOperation() == null) { diff --git a/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/history/variable/HistoricVariableInstanceDataResource.java b/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/history/variable/HistoricVariableInstanceDataResource.java index 45fdba52e94..d6e3b82d8e7 100644 --- a/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/history/variable/HistoricVariableInstanceDataResource.java +++ b/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/history/variable/HistoricVariableInstanceDataResource.java @@ -64,6 +64,11 @@ public void afterPropertiesSet() { isSerializableVariableAllowed = SerializableRestVariableUtil.isSerializableVariableAllowed(env); } + @Override + protected void validateVariableDataAllowed(String variableTypeName) { + SerializableRestVariableUtil.validateSerializableVariableAllowed(variableTypeName, isSerializableVariableAllowed); + } + @GetMapping(value = "/cmmn-history/historic-variable-instances/{varInstanceId}/data") @ApiResponses(value = { @ApiResponse(code = 200, message = "Indicates the variable instance was found and the requested variable data is returned."), diff --git a/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/runtime/caze/BaseVariableResource.java b/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/runtime/caze/BaseVariableResource.java index ec141817e01..d519faf013a 100644 --- a/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/runtime/caze/BaseVariableResource.java +++ b/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/runtime/caze/BaseVariableResource.java @@ -131,6 +131,9 @@ protected RestVariable readVariable(String instanceId, String variableName, int Object value = null; boolean valueUnresolvable = false; if (variableInstance != null) { + if (includeBinary) { + SerializableRestVariableUtil.validateSerializableVariableAllowed(variableInstance.getTypeName(), isSerializableVariableAllowed); + } try { value = variableInstance.getValue(); } catch (RuntimeException e) { diff --git a/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/runtime/caze/CaseInstanceCollectionResource.java b/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/runtime/caze/CaseInstanceCollectionResource.java index 9073ee02432..1eb8d14f193 100644 --- a/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/runtime/caze/CaseInstanceCollectionResource.java +++ b/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/runtime/caze/CaseInstanceCollectionResource.java @@ -375,7 +375,7 @@ public CaseInstanceResponse createCaseInstance(@RequestBody CaseInstanceCreateRe if (variable.getName() == null) { throw new FlowableIllegalArgumentException("Variable name is required."); } - transientVariables.put(variable.getName(), restResponseFactory.getVariableValue(variable)); + transientVariables.put(variable.getName(), restResponseFactory.getTransientVariableValue(variable)); } } diff --git a/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/runtime/task/TaskResource.java b/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/runtime/task/TaskResource.java index d1dd3c795da..ce3a86e28e2 100644 --- a/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/runtime/task/TaskResource.java +++ b/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/runtime/task/TaskResource.java @@ -222,7 +222,7 @@ protected void completeTask(Task task, TaskActionRequest actionRequest) { throw new FlowableIllegalArgumentException("Transient variable name is required"); } - Object actualVariableValue = restResponseFactory.getVariableValue(var); + Object actualVariableValue = restResponseFactory.getTransientVariableValue(var); if (var.getVariableScope() != null && RestVariable.RestVariableScope.LOCAL.equals(var.getVariableScope())) { taskCompletionBuilder.transientVariableLocal(var.getName(), actualVariableValue); } else { diff --git a/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/runtime/task/TaskVariableBaseResource.java b/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/runtime/task/TaskVariableBaseResource.java index 372239f7a02..9f43cd57519 100644 --- a/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/runtime/task/TaskVariableBaseResource.java +++ b/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/runtime/task/TaskVariableBaseResource.java @@ -101,6 +101,9 @@ protected RestVariable readVariable(Task task, String variableName, RestVariable if (variableInstance == null) { throw new FlowableObjectNotFoundException("Task '" + taskId + "' doesn't have a variable with name: '" + variableName + "'.", VariableInstanceEntity.class); } else { + if (includeBinary) { + SerializableRestVariableUtil.validateSerializableVariableAllowed(variableInstance.getTypeName(), isSerializableVariableAllowed); + } // A variable whose value cannot be resolved is returned with a null value and is marked as having an unresolvable value return restResponseFactory.createRestVariable(variableInstance, variableScope, taskId, CmmnRestResponseFactory.VARIABLE_TASK, includeBinary); } diff --git a/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/runtime/variable/VariableInstanceBaseResource.java b/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/runtime/variable/VariableInstanceBaseResource.java index 56dc8d34ff8..02c6ecbce26 100644 --- a/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/runtime/variable/VariableInstanceBaseResource.java +++ b/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/runtime/variable/VariableInstanceBaseResource.java @@ -106,7 +106,12 @@ protected DataResponse getQueryResponse(VariableInstan } public RestVariable getVariableFromRequest(boolean includeBinary, String varInstanceId) { - VariableInstance varObject = runtimeService.createVariableInstanceQuery().id(varInstanceId).singleResult(); + // The value of the variable is resolved in the same command as the query, after validating that its data can be returned + return cmmnEngineConfiguration.getCommandExecutor().execute(commandContext -> readVariable(includeBinary, varInstanceId)); + } + + protected RestVariable readVariable(boolean includeBinary, String varInstanceId) { + VariableInstance varObject = runtimeService.createVariableInstanceQuery().id(varInstanceId).excludeVariableInitialization().singleResult(); if (varObject == null) { throw new FlowableObjectNotFoundException("Historic variable instance '" + varInstanceId + "' couldn't be found.", VariableInstanceEntity.class); @@ -114,10 +119,20 @@ public RestVariable getVariableFromRequest(boolean includeBinary, String varInst if (restApiInterceptor != null) { restApiInterceptor.accessVariableInfoById(varObject); } + if (includeBinary) { + validateVariableDataAllowed(varObject.getTypeName()); + } return restResponseFactory.createRestVariable(varObject.getName(), varObject.getValue(), null, varInstanceId, CmmnRestResponseFactory.VARIABLE_HISTORY_VARINSTANCE, includeBinary); } } + /** + * Validates that the binary data of a variable with the given type can be returned, before the value of the variable is resolved. + */ + protected void validateVariableDataAllowed(String variableTypeName) { + // Nothing to validate by default + } + protected void addVariables(VariableInstanceQuery variableInstanceQuery, List variables) { for (QueryVariable variable : variables) { if (variable.getVariableOperation() == null) { diff --git a/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/runtime/variable/VariableInstanceDataResource.java b/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/runtime/variable/VariableInstanceDataResource.java index bfaceb1bbcc..b0a4b9c8d26 100644 --- a/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/runtime/variable/VariableInstanceDataResource.java +++ b/modules/flowable-cmmn-rest/src/main/java/org/flowable/cmmn/rest/service/api/runtime/variable/VariableInstanceDataResource.java @@ -57,6 +57,11 @@ public void afterPropertiesSet() { isSerializableVariableAllowed = SerializableRestVariableUtil.isSerializableVariableAllowed(env); } + @Override + protected void validateVariableDataAllowed(String variableTypeName) { + SerializableRestVariableUtil.validateSerializableVariableAllowed(variableTypeName, isSerializableVariableAllowed); + } + @GetMapping(value = "/cmmn-runtime/variable-instances/{varInstanceId}/data") @ApiResponses(value = { @ApiResponse(code = 200, message = "Indicates the variable instance was found and the requested variable data is returned."), diff --git a/modules/flowable-cmmn-rest/src/test/java/org/flowable/cmmn/rest/service/api/runtime/SerializableVariablesDefaultConfigurationTest.java b/modules/flowable-cmmn-rest/src/test/java/org/flowable/cmmn/rest/service/api/runtime/SerializableVariablesDefaultConfigurationTest.java new file mode 100644 index 00000000000..8eaeddddbf7 --- /dev/null +++ b/modules/flowable-cmmn-rest/src/test/java/org/flowable/cmmn/rest/service/api/runtime/SerializableVariablesDefaultConfigurationTest.java @@ -0,0 +1,202 @@ +/* Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.flowable.cmmn.rest.service.api.runtime; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectOutputStream; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.http.HttpStatus; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.entity.StringEntity; +import org.flowable.cmmn.api.runtime.CaseInstance; +import org.flowable.cmmn.api.runtime.PlanItemInstance; +import org.flowable.cmmn.engine.test.CmmnDeployment; +import org.flowable.cmmn.rest.service.BaseSpringRestTestCase; +import org.flowable.cmmn.rest.service.HttpMultipartHelper; +import org.flowable.cmmn.rest.service.api.CmmnRestUrls; +import org.flowable.task.api.Task; +import org.flowable.variable.api.types.VariableType; +import org.flowable.variable.api.types.VariableTypes; +import org.flowable.variable.service.impl.types.SerializableType; +import org.flowable.variable.service.impl.types.SerializableVariableTypeMode; +import org.junit.jupiter.api.Test; + +import tools.jackson.databind.JsonNode; + +/** + * Tests the default configuration: serializable variables are disabled in the engine and they are not allowed in the REST API. + * + * @author Filip Hrisafov + */ +public class SerializableVariablesDefaultConfigurationTest extends BaseSpringRestTestCase { + + @Test + public void testSerializableVariablesAreDisabledByDefault() { + VariableType serializableType = cmmnEngineConfiguration.getVariableTypes().getVariableType(SerializableType.TYPE_NAME); + assertThat(serializableType).isInstanceOf(SerializableType.class); + assertThat(((SerializableType) serializableType).getMode()).isEqualTo(SerializableVariableTypeMode.DISABLED); + } + + @Test + @CmmnDeployment(resources = "org/flowable/cmmn/rest/service/api/repository/oneHumanTaskCase.cmmn") + public void testCreateSerializableVariable() throws Exception { + CaseInstance caseInstance = runtimeService.createCaseInstanceBuilder().caseDefinitionKey("oneHumanTaskCase").start(); + Task task = taskService.createTaskQuery().caseInstanceId(caseInstance.getId()).singleResult(); + + assertSerializedObjectsNotAllowed(createSerializableVariableRequest( + CmmnRestUrls.createRelativeResourceUrl(CmmnRestUrls.URL_CASE_INSTANCE_VARIABLE_COLLECTION, caseInstance.getId()))); + assertSerializedObjectsNotAllowed(createSerializableVariableRequest( + CmmnRestUrls.createRelativeResourceUrl(CmmnRestUrls.URL_TASK_VARIABLES_COLLECTION, task.getId()))); + + assertThat(runtimeService.hasVariable(caseInstance.getId(), "serializableVariable")).isFalse(); + assertThat(taskService.hasVariable(task.getId(), "serializableVariable")).isFalse(); + } + + @Test + @CmmnDeployment(resources = "org/flowable/cmmn/rest/service/api/repository/oneHumanTaskCase.cmmn") + public void testGetSerializableVariableData() throws Exception { + CaseInstance caseInstance = runtimeService.createCaseInstanceBuilder().caseDefinitionKey("oneHumanTaskCase").start(); + Task task = taskService.createTaskQuery().caseInstanceId(caseInstance.getId()).singleResult(); + PlanItemInstance planItemInstance = runtimeService.createPlanItemInstanceQuery().caseInstanceId(caseInstance.getId()).singleResult(); + // Existing serializable variables, e.g. created before serializable variables were disabled + withSerializableVariablesEnabled(() -> { + runtimeService.setVariable(caseInstance.getId(), "serializableVariable", createSerializableVariable()); + runtimeService.setLocalVariable(planItemInstance.getId(), "planItemSerializableVariable", createSerializableVariable()); + taskService.setVariableLocal(task.getId(), "localSerializableVariable", createSerializableVariable()); + }); + + assertSerializedObjectsNotAllowed(new HttpGet(SERVER_URL_PREFIX + + CmmnRestUrls.createRelativeResourceUrl(CmmnRestUrls.URL_CASE_INSTANCE_VARIABLE_DATA, caseInstance.getId(), "serializableVariable"))); + assertSerializedObjectsNotAllowed(new HttpGet(SERVER_URL_PREFIX + + CmmnRestUrls.createRelativeResourceUrl(CmmnRestUrls.URL_PLAN_ITEM_INSTANCE_VARIABLE_DATA, planItemInstance.getId(), + "planItemSerializableVariable"))); + assertSerializedObjectsNotAllowed(new HttpGet(SERVER_URL_PREFIX + + CmmnRestUrls.createRelativeResourceUrl(CmmnRestUrls.URL_TASK_VARIABLE_DATA, task.getId(), "serializableVariable") + "?scope=global")); + assertSerializedObjectsNotAllowed(new HttpGet(SERVER_URL_PREFIX + + CmmnRestUrls.createRelativeResourceUrl(CmmnRestUrls.URL_TASK_VARIABLE_DATA, task.getId(), "localSerializableVariable"))); + + String variableInstanceId = runtimeService.createVariableInstanceQuery().caseInstanceId(caseInstance.getId()) + .variableName("serializableVariable").excludeVariableInitialization().singleResult().getId(); + assertSerializedObjectsNotAllowed(new HttpGet(SERVER_URL_PREFIX + + CmmnRestUrls.createRelativeResourceUrl(CmmnRestUrls.URL_VARIABLE_INSTANCE_DATA, variableInstanceId))); + + String historicVariableInstanceId = historyService.createHistoricVariableInstanceQuery().caseInstanceId(caseInstance.getId()) + .variableName("serializableVariable").excludeVariableInitialization().singleResult().getId(); + assertSerializedObjectsNotAllowed(new HttpGet(SERVER_URL_PREFIX + + CmmnRestUrls.createRelativeResourceUrl(CmmnRestUrls.URL_HISTORIC_VARIABLE_INSTANCE_DATA, historicVariableInstanceId))); + assertSerializedObjectsNotAllowed(new HttpGet(SERVER_URL_PREFIX + + CmmnRestUrls.createRelativeResourceUrl(CmmnRestUrls.URL_HISTORIC_CASE_INSTANCE_VARIABLE_DATA, caseInstance.getId(), "serializableVariable"))); + assertSerializedObjectsNotAllowed(new HttpGet(SERVER_URL_PREFIX + + CmmnRestUrls.createRelativeResourceUrl(CmmnRestUrls.URL_HISTORIC_TASK_INSTANCE_VARIABLE_DATA, task.getId(), "localSerializableVariable"))); + } + + @Test + @CmmnDeployment(resources = "org/flowable/cmmn/rest/service/api/repository/oneHumanTaskCase.cmmn") + public void testGetSerializableVariable() throws Exception { + CaseInstance caseInstance = runtimeService.createCaseInstanceBuilder().caseDefinitionKey("oneHumanTaskCase").start(); + withSerializableVariablesEnabled(() -> runtimeService.setVariable(caseInstance.getId(), "serializableVariable", createSerializableVariable())); + + // The variable is returned without its value + CloseableHttpResponse response = executeRequest(new HttpGet(SERVER_URL_PREFIX + + CmmnRestUrls.createRelativeResourceUrl(CmmnRestUrls.URL_CASE_INSTANCE_VARIABLE, caseInstance.getId(), "serializableVariable")), + HttpStatus.SC_OK); + JsonNode responseNode = objectMapper.readTree(response.getEntity().getContent()); + closeResponse(response); + assertThat(responseNode.path("name").asString()).isEqualTo("serializableVariable"); + assertThat(responseNode.path("type").asString()).isEqualTo("serializable"); + assertThat(responseNode.path("valueUnresolvable").asBoolean()).isTrue(); + assertThat(responseNode.path("value").isMissingNode() || responseNode.path("value").isNull()).isTrue(); + } + @Test + @CmmnDeployment(resources = "org/flowable/cmmn/rest/service/api/repository/oneHumanTaskCase.cmmn") + public void testCreateVariableWithUntypedJsonValue() throws Exception { + CaseInstance caseInstance = runtimeService.createCaseInstanceBuilder().caseDefinitionKey("oneHumanTaskCase").start(); + + // A JSON object or array without a type would have to be stored using Java serialization + for (String value : List.of("{ \"name\": \"kermit\" }", "[ \"kermit\" ]")) { + HttpPost httpPost = new HttpPost(SERVER_URL_PREFIX + CmmnRestUrls.createRelativeResourceUrl(CmmnRestUrls.URL_CASE_INSTANCE_VARIABLE_COLLECTION, caseInstance.getId())); + httpPost.setEntity(new StringEntity("[ { \"name\": \"untypedVariable\", \"value\": " + value + " } ]")); + CloseableHttpResponse response = executeRequest(httpPost, HttpStatus.SC_BAD_REQUEST); + JsonNode responseNode = objectMapper.readTree(response.getEntity().getContent()); + closeResponse(response); + assertThat(responseNode.path("exception").asString()) + .isEqualTo("Variable 'untypedVariable' has a JSON object or array as value, but no type. Set the type of the variable to 'json'."); + } + assertThat(runtimeService.hasVariable(caseInstance.getId(), "untypedVariable")).isFalse(); + + // With the json type the value is stored as JSON + HttpPost httpPost = new HttpPost(SERVER_URL_PREFIX + CmmnRestUrls.createRelativeResourceUrl(CmmnRestUrls.URL_CASE_INSTANCE_VARIABLE_COLLECTION, caseInstance.getId())); + httpPost.setEntity(new StringEntity("[ { \"name\": \"typedVariable\", \"type\": \"json\", \"value\": { \"name\": \"kermit\" } } ]")); + closeResponse(executeRequest(httpPost, HttpStatus.SC_CREATED)); + assertThat(runtimeService.getVariableInstance(caseInstance.getId(), "typedVariable").getTypeName()).isEqualTo("json"); + } + + protected void assertSerializedObjectsNotAllowed(HttpUriRequest request) throws IOException { + // No JSON content type is added, the request can be a multipart request + CloseableHttpResponse response = internalExecuteRequest(request, HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE, false); + JsonNode responseNode = objectMapper.readTree(response.getEntity().getContent()); + closeResponse(response); + assertThat(responseNode.path("exception").asString()).isEqualTo("Serialized objects are not allowed"); + } + + protected HttpPost createSerializableVariableRequest(String url) throws Exception { + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + try (ObjectOutputStream output = new ObjectOutputStream(buffer)) { + output.writeObject(createSerializableVariable()); + } + + Map additionalFields = new HashMap<>(); + additionalFields.put("name", "serializableVariable"); + additionalFields.put("type", "serializable"); + + HttpPost httpPost = new HttpPost(SERVER_URL_PREFIX + url); + httpPost.setEntity(HttpMultipartHelper.getMultiPartEntity("value", "application/x-java-serialized-object", + new ByteArrayInputStream(buffer.toByteArray()), additionalFields)); + return httpPost; + } + + protected TestSerializableVariable createSerializableVariable() { + TestSerializableVariable serializable = new TestSerializableVariable(); + serializable.setSomeField("some value"); + return serializable; + } + + /** + * Temporarily enables the serializable variable type in the engine, in order to create serializable variables. + */ + protected void withSerializableVariablesEnabled(Runnable runnable) { + VariableTypes variableTypes = cmmnEngineConfiguration.getVariableTypes(); + VariableType disabledType = variableTypes.getVariableType(SerializableType.TYPE_NAME); + int typeIndex = variableTypes.getTypeIndex(disabledType); + SerializableType enabledType = new SerializableType(false, cmmnEngineConfiguration.getVariableLengthVerifier(), + SerializableVariableTypeMode.ENABLED); + variableTypes.removeType(disabledType); + variableTypes.addType(enabledType, typeIndex); + try { + runnable.run(); + } finally { + variableTypes.removeType(enabledType); + variableTypes.addType(disabledType, typeIndex); + } + } +} diff --git a/modules/flowable-cmmn-rest/src/test/java/org/flowable/cmmn/rest/service/api/runtime/SerializableVariablesDisabledTest.java b/modules/flowable-cmmn-rest/src/test/java/org/flowable/cmmn/rest/service/api/runtime/SerializableVariablesDisabledTest.java index e5f8fd96b2a..2602cec2934 100644 --- a/modules/flowable-cmmn-rest/src/test/java/org/flowable/cmmn/rest/service/api/runtime/SerializableVariablesDisabledTest.java +++ b/modules/flowable-cmmn-rest/src/test/java/org/flowable/cmmn/rest/service/api/runtime/SerializableVariablesDisabledTest.java @@ -120,6 +120,7 @@ public void testGetSerializableVariableData() throws Exception { runtimeService.setVariable(caseInstance.getId(), "serializableVariable", serializable); Task task = taskService.createTaskQuery().caseInstanceId(caseInstance.getId()).singleResult(); + taskService.setVariableLocal(task.getId(), "localSerializableVariable", serializable); // We have serializable objects disabled in the REST API, we should get a 415. assertResponseStatus(new HttpGet(SERVER_URL_PREFIX @@ -143,6 +144,9 @@ public void testGetSerializableVariableData() throws Exception { assertResponseStatus(new HttpGet(SERVER_URL_PREFIX + CmmnRestUrls.createRelativeResourceUrl(CmmnRestUrls.URL_HISTORIC_CASE_INSTANCE_VARIABLE_DATA, caseInstance.getId(), "serializableVariable")), HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE); + assertResponseStatus(new HttpGet(SERVER_URL_PREFIX + + CmmnRestUrls.createRelativeResourceUrl(CmmnRestUrls.URL_HISTORIC_TASK_INSTANCE_VARIABLE_DATA, task.getId(), "localSerializableVariable")), + HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE); } public void assertResponseStatus(HttpUriRequest request, int expectedStatusCode) { diff --git a/modules/flowable-common-rest/src/main/java/org/flowable/common/rest/variable/SerializableRestVariableUtil.java b/modules/flowable-common-rest/src/main/java/org/flowable/common/rest/variable/SerializableRestVariableUtil.java index 81d55aad94e..05929f561bd 100644 --- a/modules/flowable-common-rest/src/main/java/org/flowable/common/rest/variable/SerializableRestVariableUtil.java +++ b/modules/flowable-common-rest/src/main/java/org/flowable/common/rest/variable/SerializableRestVariableUtil.java @@ -12,8 +12,12 @@ */ package org.flowable.common.rest.variable; +import java.util.Collection; +import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; +import org.flowable.common.engine.api.FlowableIllegalArgumentException; +import org.flowable.common.rest.exception.FlowableContentNotSupportedException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.env.Environment; @@ -29,6 +33,11 @@ public final class SerializableRestVariableUtil { public static final String ALLOW_SERIALIZABLE_PROPERTY = "rest.variables.allow.serializable"; + /** + * The name of the engine variable type that stores values using Java serialization. + */ + public static final String SERIALIZABLE_VARIABLE_TYPE_NAME = "serializable"; + // The property is read by multiple REST resources, the warning only needs to be logged once private static final AtomicBoolean ENABLED_WARNING_LOGGED = new AtomicBoolean(); @@ -43,4 +52,31 @@ public static boolean isSerializableVariableAllowed(Environment environment) { } return allowed; } + + /** + * Validates that the data of a variable with the given type can be exposed through the REST API. + * This is done based on the type of the variable, before its value is resolved, + * such that no serializable variable is deserialized when serialized objects are not allowed. + * + * @throws FlowableContentNotSupportedException when the variable is a serializable variable and serialized objects are not allowed + */ + public static void validateSerializableVariableAllowed(String variableTypeName, boolean serializableVariableAllowed) { + if (!serializableVariableAllowed && SERIALIZABLE_VARIABLE_TYPE_NAME.equals(variableTypeName)) { + throw new FlowableContentNotSupportedException("Serialized objects are not allowed"); + } + } + + /** + * Validates the value of a variable for which no type has been provided and that is going to be stored. + * A JSON object or array without a type is mapped to a {@link Map} or a {@link Collection}, which can only be stored using Java serialization. + * Such a value is rejected, the json type needs to be used for it instead. + * + * @throws FlowableIllegalArgumentException when the value is a JSON object or array + */ + public static void validateUntypedVariableValue(String variableName, Object value) { + if (value instanceof Map || value instanceof Collection) { + throw new FlowableIllegalArgumentException("Variable '" + variableName + "' has a JSON object or array as value, but no type." + + " Set the type of the variable to 'json'."); + } + } } diff --git a/modules/flowable-dmn-rest/src/main/java/org/flowable/dmn/rest/service/api/DmnRestResponseFactory.java b/modules/flowable-dmn-rest/src/main/java/org/flowable/dmn/rest/service/api/DmnRestResponseFactory.java index 529f7baa884..9e199fbef5d 100644 --- a/modules/flowable-dmn-rest/src/main/java/org/flowable/dmn/rest/service/api/DmnRestResponseFactory.java +++ b/modules/flowable-dmn-rest/src/main/java/org/flowable/dmn/rest/service/api/DmnRestResponseFactory.java @@ -22,8 +22,8 @@ import org.flowable.common.rest.variable.BooleanRestVariableConverter; import org.flowable.common.rest.variable.DateRestVariableConverter; import org.flowable.common.rest.variable.DoubleRestVariableConverter; -import org.flowable.common.rest.variable.FloatRestVariableConverter; import org.flowable.common.rest.variable.EngineRestVariable; +import org.flowable.common.rest.variable.FloatRestVariableConverter; import org.flowable.common.rest.variable.InstantRestVariableConverter; import org.flowable.common.rest.variable.IntegerRestVariableConverter; import org.flowable.common.rest.variable.LocalDateRestVariableConverter; diff --git a/modules/flowable-engine/src/main/java/org/flowable/engine/impl/bpmn/behavior/BusinessRuleTaskActivityBehavior.java b/modules/flowable-engine/src/main/java/org/flowable/engine/impl/bpmn/behavior/BusinessRuleTaskActivityBehavior.java index c1708e1867c..8b6b4c4185b 100644 --- a/modules/flowable-engine/src/main/java/org/flowable/engine/impl/bpmn/behavior/BusinessRuleTaskActivityBehavior.java +++ b/modules/flowable-engine/src/main/java/org/flowable/engine/impl/bpmn/behavior/BusinessRuleTaskActivityBehavior.java @@ -28,8 +28,11 @@ import org.flowable.engine.repository.ProcessDefinition; import org.flowable.variable.api.types.VariableType; import org.flowable.variable.service.impl.types.SerializableType; +import org.flowable.variable.service.impl.types.SerializableVariableTypeMode; import org.kie.api.KieBase; import org.kie.api.runtime.KieSession; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Activity implementation of the BPMN 2.0 business rule task. @@ -47,6 +50,8 @@ public class BusinessRuleTaskActivityBehavior extends TaskActivityBehavior imple private static final long serialVersionUID = 1L; + private static final Logger LOGGER = LoggerFactory.getLogger(BusinessRuleTaskActivityBehavior.class); + protected Set variablesInputExpressions = new HashSet<>(); protected Set rulesExpressions = new HashSet<>(); protected boolean exclude; @@ -92,6 +97,10 @@ public void execute(DelegateExecution execution) { execution.setVariable(resultVariable, outputVariables); } else { // The rule output objects can only be stored using Java serialization + LOGGER.warn("The result variable '{}' of business rule task '{}' in process instance '{}' is only stored as a transient variable," + + " because storing serializable variables is not enabled. It will no longer be available after the next wait state." + + " Use a DMN decision task instead, or set the serializableVariableTypeMode of the process engine configuration to {}.", + resultVariable, execution.getCurrentActivityId(), execution.getProcessInstanceId(), SerializableVariableTypeMode.ENABLED); execution.setTransientVariable(resultVariable, outputVariables); } } diff --git a/modules/flowable-engine/src/main/java/org/flowable/engine/impl/cfg/ProcessEngineConfigurationImpl.java b/modules/flowable-engine/src/main/java/org/flowable/engine/impl/cfg/ProcessEngineConfigurationImpl.java index d2727ee7a11..98a1bc54946 100755 --- a/modules/flowable-engine/src/main/java/org/flowable/engine/impl/cfg/ProcessEngineConfigurationImpl.java +++ b/modules/flowable-engine/src/main/java/org/flowable/engine/impl/cfg/ProcessEngineConfigurationImpl.java @@ -420,10 +420,10 @@ import org.flowable.variable.service.impl.types.DateType; import org.flowable.variable.service.impl.types.DefaultVariableTypes; import org.flowable.variable.service.impl.types.DoubleType; -import org.flowable.variable.service.impl.types.FloatType; import org.flowable.variable.service.impl.types.EmptyCollectionType; import org.flowable.variable.service.impl.types.EntityManagerSession; import org.flowable.variable.service.impl.types.EntityManagerSessionFactory; +import org.flowable.variable.service.impl.types.FloatType; import org.flowable.variable.service.impl.types.InstantType; import org.flowable.variable.service.impl.types.IntegerType; import org.flowable.variable.service.impl.types.JPAEntityListVariableType; diff --git a/modules/flowable-engine/src/test/java/org/flowable/engine/test/api/variables/SerializableVariableTypeModeTest.java b/modules/flowable-engine/src/test/java/org/flowable/engine/test/api/variables/SerializableVariableTypeModeTest.java index a138d180445..2251f0a9d1b 100644 --- a/modules/flowable-engine/src/test/java/org/flowable/engine/test/api/variables/SerializableVariableTypeModeTest.java +++ b/modules/flowable-engine/src/test/java/org/flowable/engine/test/api/variables/SerializableVariableTypeModeTest.java @@ -23,6 +23,8 @@ import java.util.List; import org.flowable.common.engine.api.FlowableException; +import org.flowable.common.engine.impl.logging.LoggingListener; +import org.flowable.common.engine.impl.logging.LoggingSessionConstants; import org.flowable.engine.ManagementService; import org.flowable.engine.ProcessEngine; import org.flowable.engine.RuntimeService; @@ -40,9 +42,10 @@ import org.flowable.variable.service.impl.types.SerializableType; import org.flowable.variable.service.impl.types.SerializableVariableTypeMode; import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import tools.jackson.databind.node.ObjectNode; + /** * @author Filip Hrisafov */ @@ -52,23 +55,6 @@ class SerializableVariableTypeModeTest { protected List processEngines = new ArrayList<>(); - // Process engine with serializable variables enabled, used for creating the serializable data - protected ProcessEngine enabledProcessEngine; - protected ProcessInstance processInstance; - - @BeforeEach - void setUp() { - enabledProcessEngine = buildProcessEngine(SerializableVariableTypeMode.ENABLED, "create-drop"); - enabledProcessEngine.getRepositoryService().createDeployment() - .addClasspathResource("org/flowable/engine/test/api/oneTaskProcess.bpmn20.xml") - .deploy(); - - processInstance = enabledProcessEngine.getRuntimeService().createProcessInstanceBuilder() - .processDefinitionKey("oneTaskProcess") - .variable("serializableVar", new TestSerializableVariable(10)) - .start(); - } - @AfterEach void tearDown() { // Close in reverse order, the first engine is the one that drops the schema @@ -85,6 +71,8 @@ void defaultModeIsDisabled() { @Test void enabledMode() { + ProcessEngine enabledProcessEngine = buildProcessEngine(SerializableVariableTypeMode.ENABLED); + ProcessInstance processInstance = startProcessWithSerializableVariable(enabledProcessEngine); RuntimeService runtimeService = enabledProcessEngine.getRuntimeService(); assertThat(runtimeService.getVariable(processInstance.getId(), "serializableVar", TestSerializableVariable.class)) .extracting(TestSerializableVariable::getNumber) @@ -98,7 +86,9 @@ void enabledMode() { @Test void readOnlyMode() { - RuntimeService runtimeService = buildProcessEngine(SerializableVariableTypeMode.READ_ONLY, "false").getRuntimeService(); + ProcessEngine enabledProcessEngine = buildProcessEngine(SerializableVariableTypeMode.ENABLED); + ProcessInstance processInstance = startProcessWithSerializableVariable(enabledProcessEngine); + RuntimeService runtimeService = buildProcessEngine(SerializableVariableTypeMode.READ_ONLY).getRuntimeService(); assertThat(runtimeService.getVariable(processInstance.getId(), "serializableVar", TestSerializableVariable.class)) .extracting(TestSerializableVariable::getNumber) @@ -127,7 +117,9 @@ void readOnlyMode() { @Test void readOnlyModeDoesNotUpdateTrackedObjects() { - ProcessEngine readOnlyProcessEngine = buildProcessEngine(SerializableVariableTypeMode.READ_ONLY, "false"); + ProcessEngine enabledProcessEngine = buildProcessEngine(SerializableVariableTypeMode.ENABLED); + ProcessInstance processInstance = startProcessWithSerializableVariable(enabledProcessEngine); + ProcessEngine readOnlyProcessEngine = buildProcessEngine(SerializableVariableTypeMode.READ_ONLY); readOnlyProcessEngine.getManagementService().executeCommand(commandContext -> { TestSerializableVariable value = (TestSerializableVariable) readOnlyProcessEngine.getRuntimeService() @@ -143,7 +135,9 @@ void readOnlyModeDoesNotUpdateTrackedObjects() { @Test void disabledMode() { - ProcessEngine disabledProcessEngine = buildProcessEngine(SerializableVariableTypeMode.DISABLED, "false"); + ProcessEngine enabledProcessEngine = buildProcessEngine(SerializableVariableTypeMode.ENABLED); + ProcessInstance processInstance = startProcessWithSerializableVariable(enabledProcessEngine); + ProcessEngine disabledProcessEngine = buildProcessEngine(SerializableVariableTypeMode.DISABLED); RuntimeService runtimeService = disabledProcessEngine.getRuntimeService(); assertThatThrownBy(() -> runtimeService.getVariable(processInstance.getId(), "serializableVar")) @@ -168,9 +162,69 @@ void disabledMode() { assertThat(runtimeService.createProcessInstanceQuery().processInstanceId(processInstance.getId()).count()).isZero(); } + @Test + void disabledModeVariableCanBeOverwrittenWithAnotherType() { + ProcessEngine enabledProcessEngine = buildProcessEngine(SerializableVariableTypeMode.ENABLED); + ProcessInstance processInstance = startProcessWithSerializableVariable(enabledProcessEngine); + RuntimeService runtimeService = buildProcessEngine(SerializableVariableTypeMode.DISABLED).getRuntimeService(); + + // The existing value does not need to be read for replacing it with a value of another type + runtimeService.setVariable(processInstance.getId(), "serializableVar", "migrated"); + + VariableInstance variableInstance = runtimeService.getVariableInstance(processInstance.getId(), "serializableVar"); + assertThat(variableInstance.getTypeName()).isEqualTo("string"); + assertThat(variableInstance.getValue()).isEqualTo("migrated"); + } + + @Test + void disabledModeVariableCanBeOverwrittenWithLoggingSession() { + ProcessEngine enabledProcessEngine = buildProcessEngine(SerializableVariableTypeMode.ENABLED); + ProcessInstance processInstance = startProcessWithSerializableVariable(enabledProcessEngine); + List loggingNodes = new ArrayList<>(); + RuntimeService runtimeService = buildProcessEngine(SerializableVariableTypeMode.DISABLED, loggingNodes::addAll).getRuntimeService(); + + runtimeService.setVariable(processInstance.getId(), "serializableVar", "migrated"); + + assertThat(runtimeService.getVariable(processInstance.getId(), "serializableVar")).isEqualTo("migrated"); + // The update is logged without the old serializable value + assertThat(loggingNodes) + .filteredOn(loggingNode -> LoggingSessionConstants.TYPE_VARIABLE_UPDATE.equals(loggingNode.path("type").asString())) + .singleElement() + .satisfies(loggingNode -> { + assertThat(loggingNode.path("variableName").asString()).isEqualTo("serializableVar"); + assertThat(loggingNode.path("variableType").asString()).isEqualTo("string"); + assertThat(loggingNode.path("variableValue").asString()).isEqualTo("migrated"); + assertThat(loggingNode.path("oldVariableType").asString()).isEqualTo(SerializableType.TYPE_NAME); + assertThat(loggingNode.has("oldVariableValue")).isFalse(); + assertThat(loggingNode.has("oldVariableRawValue")).isFalse(); + }); + } + + @Test + void disabledModeVariableCanBeRemovedWithLoggingSession() { + ProcessEngine enabledProcessEngine = buildProcessEngine(SerializableVariableTypeMode.ENABLED); + ProcessInstance processInstance = startProcessWithSerializableVariable(enabledProcessEngine); + List loggingNodes = new ArrayList<>(); + RuntimeService runtimeService = buildProcessEngine(SerializableVariableTypeMode.DISABLED, loggingNodes::addAll).getRuntimeService(); + + runtimeService.removeVariable(processInstance.getId(), "serializableVar"); + + assertThat(runtimeService.hasVariable(processInstance.getId(), "serializableVar")).isFalse(); + // The removal is logged without the serializable value + assertThat(loggingNodes) + .filteredOn(loggingNode -> LoggingSessionConstants.TYPE_VARIABLE_DELETE.equals(loggingNode.path("type").asString())) + .singleElement() + .satisfies(loggingNode -> { + assertThat(loggingNode.path("variableName").asString()).isEqualTo("serializableVar"); + assertThat(loggingNode.path("variableType").asString()).isEqualTo(SerializableType.TYPE_NAME); + assertThat(loggingNode.has("variableValue")).isFalse(); + assertThat(loggingNode.has("variableRawValue")).isFalse(); + }); + } + @Test void disabledModeFailsJobWithoutRetries() { - ProcessEngine disabledProcessEngine = buildProcessEngine(SerializableVariableTypeMode.DISABLED, "false"); + ProcessEngine disabledProcessEngine = buildProcessEngine(SerializableVariableTypeMode.DISABLED); disabledProcessEngine.getRepositoryService().createDeployment() .addClasspathResource("org/flowable/engine/test/api/variables/SerializableVariableTypeModeTest.asyncSerializableVariable.bpmn20.xml") .deploy(); @@ -192,7 +246,9 @@ void disabledModeFailsJobWithoutRetries() { @Test void disabledModeProcessInstanceCanBeDeleted() { - ProcessEngine disabledProcessEngine = buildProcessEngine(SerializableVariableTypeMode.DISABLED, "false"); + ProcessEngine enabledProcessEngine = buildProcessEngine(SerializableVariableTypeMode.ENABLED); + ProcessInstance processInstance = startProcessWithSerializableVariable(enabledProcessEngine); + ProcessEngine disabledProcessEngine = buildProcessEngine(SerializableVariableTypeMode.DISABLED); RuntimeService runtimeService = disabledProcessEngine.getRuntimeService(); runtimeService.deleteProcessInstance(processInstance.getId(), "test"); @@ -201,7 +257,9 @@ void disabledModeProcessInstanceCanBeDeleted() { @Test void longStringVariablesWorkWhenSerializableIsDisabled() { - RuntimeService runtimeService = buildProcessEngine(SerializableVariableTypeMode.DISABLED, "false").getRuntimeService(); + ProcessEngine enabledProcessEngine = buildProcessEngine(SerializableVariableTypeMode.ENABLED); + ProcessInstance processInstance = startProcessWithSerializableVariable(enabledProcessEngine); + RuntimeService runtimeService = buildProcessEngine(SerializableVariableTypeMode.DISABLED).getRuntimeService(); String longString = "a".repeat(5000); runtimeService.setVariable(processInstance.getId(), "longStringVar", longString); @@ -213,6 +271,8 @@ void longStringVariablesWorkWhenSerializableIsDisabled() { @Test void longStringVariableDoesNotDeserializeNonStringValues() throws Exception { + ProcessEngine enabledProcessEngine = buildProcessEngine(SerializableVariableTypeMode.ENABLED); + ProcessInstance processInstance = startProcessWithSerializableVariable(enabledProcessEngine); String longString = "a".repeat(5000); RuntimeService runtimeService = enabledProcessEngine.getRuntimeService(); runtimeService.setVariable(processInstance.getId(), "longStringVar", longString); @@ -228,19 +288,40 @@ void longStringVariableDoesNotDeserializeNonStringValues() throws Exception { return null; }); - RuntimeService otherRuntimeService = buildProcessEngine(SerializableVariableTypeMode.ENABLED, "false").getRuntimeService(); + RuntimeService otherRuntimeService = buildProcessEngine(SerializableVariableTypeMode.ENABLED).getRuntimeService(); assertThatThrownBy(() -> otherRuntimeService.getVariable(processInstance.getId(), "longStringVar")) .isInstanceOf(FlowableException.class) .hasMessageContaining("Couldn't deserialize object in variable 'longStringVar'") .hasRootCauseInstanceOf(InvalidClassException.class); } - protected ProcessEngine buildProcessEngine(SerializableVariableTypeMode mode, String databaseSchemaUpdate) { + /** + * Starts a process instance with a serializable variable, using a process engine that has serializable variables enabled. + */ + protected ProcessInstance startProcessWithSerializableVariable(ProcessEngine enabledProcessEngine) { + enabledProcessEngine.getRepositoryService().createDeployment() + .addClasspathResource("org/flowable/engine/test/api/oneTaskProcess.bpmn20.xml") + .deploy(); + + return enabledProcessEngine.getRuntimeService().createProcessInstanceBuilder() + .processDefinitionKey("oneTaskProcess") + .variable("serializableVar", new TestSerializableVariable(10)) + .start(); + } + + protected ProcessEngine buildProcessEngine(SerializableVariableTypeMode mode) { + return buildProcessEngine(mode, null); + } + + protected ProcessEngine buildProcessEngine(SerializableVariableTypeMode mode, LoggingListener loggingListener) { + // The first process engine creates the schema (and drops it when it is closed), the other ones are using the same database + String databaseSchemaUpdate = processEngines.isEmpty() ? "create-drop" : "false"; ProcessEngineConfigurationImpl processEngineConfiguration = (ProcessEngineConfigurationImpl) new StandaloneInMemProcessEngineConfiguration() .setJdbcUrl(JDBC_URL) .setDatabaseSchemaUpdate(databaseSchemaUpdate) .setEngineName("serializableVariableTypeModeTest" + processEngines.size()); processEngineConfiguration.setSerializableVariableTypeMode(mode); + processEngineConfiguration.setLoggingListener(loggingListener); ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine(); processEngines.add(processEngine); return processEngine; diff --git a/modules/flowable-engine/src/test/java/org/flowable/standalone/jpa/JPAVariableTest.java b/modules/flowable-engine/src/test/java/org/flowable/standalone/jpa/JPAVariableTest.java index f2dccf92228..4a862a30d93 100644 --- a/modules/flowable-engine/src/test/java/org/flowable/standalone/jpa/JPAVariableTest.java +++ b/modules/flowable-engine/src/test/java/org/flowable/standalone/jpa/JPAVariableTest.java @@ -16,8 +16,12 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import java.io.ByteArrayOutputStream; +import java.io.InvalidClassException; +import java.io.ObjectOutputStream; import java.math.BigDecimal; import java.math.BigInteger; +import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; import java.util.Collections; @@ -34,8 +38,10 @@ import org.flowable.engine.runtime.ProcessInstance; import org.flowable.engine.test.Deployment; import org.flowable.job.api.FlowableUnrecoverableJobException; +import org.flowable.variable.service.impl.persistence.entity.VariableInstanceEntity; import org.flowable.variable.service.impl.types.EntityManagerSession; import org.flowable.variable.service.impl.types.EntityManagerSessionFactory; +import org.flowable.variable.service.impl.types.JPAEntityListVariableType; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; @@ -427,6 +433,31 @@ public void testStoreJPAEntityListAsVariableEdgeCases() { } // https://activiti.atlassian.net/browse/ACT-995 + @Test + @Deployment(resources = { "org/flowable/standalone/jpa/JPAVariableTest.testStoreJPAEntityAsVariable.bpmn20.xml" }) + public void testJPAEntityListDoesNotDeserializeNonStringValues() throws Exception { + Map variables = new HashMap<>(); + variables.put("list", Arrays.asList(simpleEntityFieldAccess, simpleEntityFieldAccess)); + ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("JPAVariableProcess", variables); + + // Simulate tampered bytes in the database + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + try (ObjectOutputStream outputStream = new ObjectOutputStream(buffer)) { + outputStream.writeObject(new ArrayList<>(List.of("1"))); + } + managementService.executeCommand(commandContext -> { + VariableInstanceEntity variableInstance = (VariableInstanceEntity) runtimeService.getVariableInstance(processInstance.getId(), "list"); + assertThat(variableInstance.getTypeName()).isEqualTo(JPAEntityListVariableType.TYPE_NAME); + variableInstance.setBytes(buffer.toByteArray()); + return null; + }); + + assertThatThrownBy(() -> runtimeService.getVariable(processInstance.getId(), "list")) + .isInstanceOf(FlowableException.class) + .hasMessageContaining("Unexpected exception when deserializing JPA id's") + .hasRootCauseInstanceOf(InvalidClassException.class); + } + @Test @Deployment(resources = "org/flowable/standalone/jpa/JPAVariableTest.testQueryJPAVariable.bpmn20.xml") public void testReplaceExistingJPAEntityWithAnotherOfSameType() { diff --git a/modules/flowable-external-job-rest/src/main/java/org/flowable/external/job/rest/service/api/ExternalJobRestResponseFactory.java b/modules/flowable-external-job-rest/src/main/java/org/flowable/external/job/rest/service/api/ExternalJobRestResponseFactory.java index 89cc6d49a7b..cee54e3df3a 100644 --- a/modules/flowable-external-job-rest/src/main/java/org/flowable/external/job/rest/service/api/ExternalJobRestResponseFactory.java +++ b/modules/flowable-external-job-rest/src/main/java/org/flowable/external/job/rest/service/api/ExternalJobRestResponseFactory.java @@ -25,8 +25,8 @@ import org.flowable.common.rest.variable.BooleanRestVariableConverter; import org.flowable.common.rest.variable.DateRestVariableConverter; import org.flowable.common.rest.variable.DoubleRestVariableConverter; -import org.flowable.common.rest.variable.FloatRestVariableConverter; import org.flowable.common.rest.variable.EngineRestVariable; +import org.flowable.common.rest.variable.FloatRestVariableConverter; import org.flowable.common.rest.variable.InstantRestVariableConverter; import org.flowable.common.rest.variable.IntegerRestVariableConverter; import org.flowable.common.rest.variable.JsonObjectRestVariableConverter; @@ -34,6 +34,7 @@ import org.flowable.common.rest.variable.LocalDateTimeRestVariableConverter; import org.flowable.common.rest.variable.LongRestVariableConverter; import org.flowable.common.rest.variable.RestVariableConverter; +import org.flowable.common.rest.variable.SerializableRestVariableUtil; import org.flowable.common.rest.variable.ShortRestVariableConverter; import org.flowable.common.rest.variable.StringRestVariableConverter; import org.flowable.external.job.rest.service.api.acquire.AcquiredExternalWorkerJobResponse; @@ -154,6 +155,10 @@ protected EngineRestVariable createRestVariable(String name, Object value) { return restVariable; } + /** + * Get the value of a variable that is going to be stored. + * A JSON object or array is only supported when the json type is provided. + */ public Object getVariableValue(EngineRestVariable restVariable) { Object value; @@ -175,6 +180,7 @@ public Object getVariableValue(EngineRestVariable restVariable) { // Revert to type determined by REST-to-Java mapping when no // explicit type has been provided value = restVariable.getValue(); + SerializableRestVariableUtil.validateUntypedVariableValue(restVariable.getName(), value); } return value; } diff --git a/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/RestResponseFactory.java b/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/RestResponseFactory.java index ed588da4a0b..673cc8049f1 100644 --- a/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/RestResponseFactory.java +++ b/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/RestResponseFactory.java @@ -41,6 +41,7 @@ import org.flowable.common.rest.variable.LocalDateTimeRestVariableConverter; import org.flowable.common.rest.variable.LongRestVariableConverter; import org.flowable.common.rest.variable.RestVariableConverter; +import org.flowable.common.rest.variable.SerializableRestVariableUtil; import org.flowable.common.rest.variable.ShortRestVariableConverter; import org.flowable.common.rest.variable.StringRestVariableConverter; import org.flowable.common.rest.variable.UUIDRestVariableConverter; @@ -502,7 +503,22 @@ public RestVariable createBinaryRestVariable(String name, RestVariableScope scop return restVar; } + /** + * Get the value of a variable that is going to be stored. + * A JSON object or array is only supported when the json type is provided. + */ public Object getVariableValue(RestVariable restVariable) { + return getVariableValue(restVariable, false); + } + + /** + * Get the value of a transient variable. Since it is not stored, a JSON object or array without a type is supported as well. + */ + public Object getTransientVariableValue(RestVariable restVariable) { + return getVariableValue(restVariable, true); + } + + protected Object getVariableValue(RestVariable restVariable, boolean allowUntypedJsonValue) { Object value = null; if (restVariable.getType() != null) { @@ -523,6 +539,9 @@ public Object getVariableValue(RestVariable restVariable) { // Revert to type determined by REST-to-Java mapping when no // explicit type has been provided value = restVariable.getValue(); + if (!allowUntypedJsonValue) { + SerializableRestVariableUtil.validateUntypedVariableValue(restVariable.getName(), value); + } } return value; } diff --git a/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/history/HistoricDetailDataResource.java b/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/history/HistoricDetailDataResource.java index 7633bd7af12..dc13c241438 100644 --- a/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/history/HistoricDetailDataResource.java +++ b/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/history/HistoricDetailDataResource.java @@ -108,14 +108,17 @@ public RestVariable getVariableFromRequest(boolean includeBinary, String detailI Object value = null; HistoricVariableUpdate variableUpdate = null; HistoricDetail detailObject = historyService.createHistoricDetailQuery().id(detailId).singleResult(); + if (restApiInterceptor != null) { + restApiInterceptor.accessHistoryDetailById(detailObject); + } + if (detailObject instanceof HistoricVariableUpdate) { variableUpdate = (HistoricVariableUpdate) detailObject; + if (includeBinary) { + SerializableRestVariableUtil.validateSerializableVariableAllowed(variableUpdate.getVariableTypeName(), isSerializableVariableAllowed); + } value = variableUpdate.getValue(); } - - if (restApiInterceptor != null) { - restApiInterceptor.accessHistoryDetailById(detailObject); - } if (value == null) { throw new FlowableObjectNotFoundException("Historic detail '" + detailId + "' does not have a variable value.", VariableInstanceEntity.class); diff --git a/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/history/HistoricProcessInstanceVariableDataResource.java b/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/history/HistoricProcessInstanceVariableDataResource.java index e32cfa36899..339cdce79f4 100644 --- a/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/history/HistoricProcessInstanceVariableDataResource.java +++ b/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/history/HistoricProcessInstanceVariableDataResource.java @@ -64,14 +64,14 @@ public class HistoricProcessInstanceVariableDataResource extends HistoricProcess @Autowired protected HistoryService historyService; - @ApiResponses(value = { - @ApiResponse(code = 200, message = "Indicates the process instance was found and the requested variable data is returned."), - @ApiResponse(code = 404, message = "Indicates the requested process instance was not found or the process instance does not have a variable with the given name or the variable does not have a binary stream available. Status message provides additional information.") }) @Override public void afterPropertiesSet() { isSerializableVariableAllowed = SerializableRestVariableUtil.isSerializableVariableAllowed(env); } + @ApiResponses(value = { + @ApiResponse(code = 200, message = "Indicates the process instance was found and the requested variable data is returned."), + @ApiResponse(code = 404, message = "Indicates the requested process instance was not found or the process instance does not have a variable with the given name or the variable does not have a binary stream available. Status message provides additional information.") }) @ApiOperation(value = "Get the binary data for a historic process instance variable", tags = {"History Process" }, nickname = "getHistoricProcessInstanceVariableData", notes = "The response body contains the binary value of the variable. When the variable is of type binary, the content-type of the response is set to application/octet-stream, regardless of the content of the variable or the request accept-type header. In case of serializable, application/x-java-serialized-object is used as content-type.") @GetMapping(value = "/history/historic-process-instances/{processInstanceId}/variables/{variableName}/data") @@ -131,6 +131,9 @@ protected RestVariable readVariable(boolean includeBinary, String processInstanc variableInstance = queryVariable; } } + if (variableInstance != null && includeBinary) { + SerializableRestVariableUtil.validateSerializableVariableAllowed(variableInstance.getVariableTypeName(), isSerializableVariableAllowed); + } Object value = variableInstance != null ? variableInstance.getValue() : null; if (value == null) { diff --git a/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/history/HistoricTaskInstanceVariableDataResource.java b/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/history/HistoricTaskInstanceVariableDataResource.java index dd98081f0b7..0eef8df8ff6 100644 --- a/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/history/HistoricTaskInstanceVariableDataResource.java +++ b/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/history/HistoricTaskInstanceVariableDataResource.java @@ -162,6 +162,9 @@ protected RestVariable readVariable(boolean includeBinary, String taskId, String variableInstance = getProcessVariable(taskObject, variableName); } } + if (variableInstance != null && includeBinary) { + SerializableRestVariableUtil.validateSerializableVariableAllowed(variableInstance.getVariableTypeName(), isSerializableVariableAllowed); + } Object value = variableInstance != null ? variableInstance.getValue() : null; if (value == null) { diff --git a/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/history/HistoricVariableInstanceDataResource.java b/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/history/HistoricVariableInstanceDataResource.java index 50ad020c3f7..7a50aab0d27 100644 --- a/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/history/HistoricVariableInstanceDataResource.java +++ b/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/history/HistoricVariableInstanceDataResource.java @@ -24,6 +24,7 @@ import org.flowable.common.rest.exception.FlowableContentNotSupportedException; import org.flowable.common.rest.variable.SerializableRestVariableUtil; import org.flowable.engine.HistoryService; +import org.flowable.engine.ManagementService; import org.flowable.rest.service.api.BpmnRestApiInterceptor; import org.flowable.rest.service.api.RestResponseFactory; import org.flowable.rest.service.api.engine.variable.RestVariable; @@ -62,6 +63,9 @@ public class HistoricVariableInstanceDataResource implements InitializingBean { @Autowired protected HistoryService historyService; + @Autowired + protected ManagementService managementService; + @Autowired(required=false) protected BpmnRestApiInterceptor restApiInterceptor; @@ -108,7 +112,12 @@ public byte[] getVariableData(@ApiParam(name = "varInstanceId") @PathVariable("v } public RestVariable getVariableFromRequest(boolean includeBinary, String varInstanceId) { - HistoricVariableInstance varObject = historyService.createHistoricVariableInstanceQuery().id(varInstanceId).singleResult(); + // The value of the variable is resolved in the same command as the query, after validating that its data can be returned + return managementService.executeCommand(commandContext -> readVariable(includeBinary, varInstanceId)); + } + + protected RestVariable readVariable(boolean includeBinary, String varInstanceId) { + HistoricVariableInstance varObject = historyService.createHistoricVariableInstanceQuery().id(varInstanceId).excludeVariableInitialization().singleResult(); if (varObject == null) { throw new FlowableObjectNotFoundException("Historic variable instance '" + varInstanceId + "' could not be found.", VariableInstanceEntity.class); @@ -118,6 +127,9 @@ public RestVariable getVariableFromRequest(boolean includeBinary, String varInst restApiInterceptor.accessHistoryVariableInfoById(varObject); } + if (includeBinary) { + SerializableRestVariableUtil.validateSerializableVariableAllowed(varObject.getVariableTypeName(), isSerializableVariableAllowed); + } return restResponseFactory.createRestVariable(varObject.getVariableName(), varObject.getValue(), null, varInstanceId, RestResponseFactory.VARIABLE_HISTORY_VARINSTANCE, includeBinary); } } diff --git a/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/runtime/VariableInstanceDataResource.java b/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/runtime/VariableInstanceDataResource.java index ff8090596ec..006268c9d50 100644 --- a/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/runtime/VariableInstanceDataResource.java +++ b/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/runtime/VariableInstanceDataResource.java @@ -23,6 +23,7 @@ import org.flowable.common.engine.api.FlowableObjectNotFoundException; import org.flowable.common.rest.exception.FlowableContentNotSupportedException; import org.flowable.common.rest.variable.SerializableRestVariableUtil; +import org.flowable.engine.ManagementService; import org.flowable.engine.RuntimeService; import org.flowable.rest.service.api.BpmnRestApiInterceptor; import org.flowable.rest.service.api.RestResponseFactory; @@ -62,6 +63,9 @@ public class VariableInstanceDataResource implements InitializingBean { @Autowired protected RuntimeService runtimeService; + @Autowired + protected ManagementService managementService; + @Autowired(required=false) protected BpmnRestApiInterceptor restApiInterceptor; @@ -108,7 +112,12 @@ public byte[] getVariableData(@ApiParam(name = "varInstanceId") @PathVariable("v } public RestVariable getVariableFromRequest(boolean includeBinary, String varInstanceId) { - VariableInstance varObject = runtimeService.createVariableInstanceQuery().id(varInstanceId).singleResult(); + // The value of the variable is resolved in the same command as the query, after validating that its data can be returned + return managementService.executeCommand(commandContext -> readVariable(includeBinary, varInstanceId)); + } + + protected RestVariable readVariable(boolean includeBinary, String varInstanceId) { + VariableInstance varObject = runtimeService.createVariableInstanceQuery().id(varInstanceId).excludeVariableInitialization().singleResult(); if (varObject == null) { throw new FlowableObjectNotFoundException("Historic variable instance '" + varInstanceId + "' could not be found.", VariableInstanceEntity.class); @@ -118,6 +127,9 @@ public RestVariable getVariableFromRequest(boolean includeBinary, String varInst restApiInterceptor.accessVariableInfoById(varObject); } + if (includeBinary) { + SerializableRestVariableUtil.validateSerializableVariableAllowed(varObject.getTypeName(), isSerializableVariableAllowed); + } return restResponseFactory.createRestVariable(varObject.getName(), varObject.getValue(), null, varInstanceId, RestResponseFactory.VARIABLE_VARINSTANCE, includeBinary); } } diff --git a/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/runtime/process/BaseExecutionVariableResource.java b/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/runtime/process/BaseExecutionVariableResource.java index 8e9d928f754..f35a43a3994 100644 --- a/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/runtime/process/BaseExecutionVariableResource.java +++ b/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/runtime/process/BaseExecutionVariableResource.java @@ -348,6 +348,9 @@ protected RestVariable readVariable(Execution execution, String variableName, Re * and is marked as having an unresolvable value. When the binary value is requested, a value that cannot be resolved fails instead. */ protected RestVariable constructRestVariable(VariableInstance variableInstance, RestVariableScope variableScope, String executionId, boolean includeBinary) { + if (includeBinary) { + SerializableRestVariableUtil.validateSerializableVariableAllowed(variableInstance.getTypeName(), isSerializableVariableAllowed); + } Object value; try { value = variableInstance.getValue(); diff --git a/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/runtime/process/ProcessInstanceCollectionResource.java b/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/runtime/process/ProcessInstanceCollectionResource.java index dc2ca8e6217..bab89335ade 100644 --- a/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/runtime/process/ProcessInstanceCollectionResource.java +++ b/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/runtime/process/ProcessInstanceCollectionResource.java @@ -373,7 +373,7 @@ public ProcessInstanceResponse createProcessInstance(@RequestBody ProcessInstanc if (variable.getName() == null) { throw new FlowableIllegalArgumentException("Variable name is required."); } - transientVariables.put(variable.getName(), restResponseFactory.getVariableValue(variable)); + transientVariables.put(variable.getName(), restResponseFactory.getTransientVariableValue(variable)); } } diff --git a/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/runtime/task/TaskResource.java b/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/runtime/task/TaskResource.java index 4c33dbaf841..57b34def423 100644 --- a/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/runtime/task/TaskResource.java +++ b/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/runtime/task/TaskResource.java @@ -220,7 +220,7 @@ protected void completeTask(Task task, TaskActionRequest actionRequest) { throw new FlowableIllegalArgumentException("Transient variable name is required"); } - Object actualVariableValue = restResponseFactory.getVariableValue(var); + Object actualVariableValue = restResponseFactory.getTransientVariableValue(var); if (var.getVariableScope() != null && RestVariable.RestVariableScope.LOCAL.equals(var.getVariableScope())) { taskCompletionBuilder.transientVariableLocal(var.getName(), actualVariableValue); } else { diff --git a/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/runtime/task/TaskVariableBaseResource.java b/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/runtime/task/TaskVariableBaseResource.java index aec308db994..325175bd8c5 100644 --- a/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/runtime/task/TaskVariableBaseResource.java +++ b/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/runtime/task/TaskVariableBaseResource.java @@ -99,6 +99,9 @@ protected RestVariable readVariable(Task task, String variableName, String scope if (variableInstance == null) { throw new FlowableObjectNotFoundException("Task '" + taskId + "' does not have a variable with name: '" + variableName + "'.", VariableInstanceEntity.class); } else { + if (includeBinary) { + SerializableRestVariableUtil.validateSerializableVariableAllowed(variableInstance.getTypeName(), isSerializableVariableAllowed); + } // A variable whose value cannot be resolved is returned with a null value and is marked as having an unresolvable value return restResponseFactory.createRestVariable(variableInstance, variableScope, taskId, RestResponseFactory.VARIABLE_TASK, includeBinary); } diff --git a/modules/flowable-rest/src/test/java/org/flowable/rest/service/api/runtime/SerializableVariablesDefaultConfigurationTest.java b/modules/flowable-rest/src/test/java/org/flowable/rest/service/api/runtime/SerializableVariablesDefaultConfigurationTest.java new file mode 100644 index 00000000000..17728ef7dc2 --- /dev/null +++ b/modules/flowable-rest/src/test/java/org/flowable/rest/service/api/runtime/SerializableVariablesDefaultConfigurationTest.java @@ -0,0 +1,207 @@ +/* Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.flowable.rest.service.api.runtime; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.ObjectOutputStream; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.http.HttpStatus; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.entity.StringEntity; +import org.flowable.engine.runtime.ProcessInstance; +import org.flowable.engine.test.Deployment; +import org.flowable.rest.service.BaseSpringRestTestCase; +import org.flowable.rest.service.HttpMultipartHelper; +import org.flowable.rest.service.api.RestUrls; +import org.flowable.task.api.Task; +import org.flowable.variable.api.types.VariableType; +import org.flowable.variable.api.types.VariableTypes; +import org.flowable.variable.service.impl.types.SerializableType; +import org.flowable.variable.service.impl.types.SerializableVariableTypeMode; +import org.junit.jupiter.api.Test; + +import tools.jackson.databind.JsonNode; + +/** + * Tests the default configuration: serializable variables are disabled in the engine and they are not allowed in the REST API. + * + * @author Filip Hrisafov + */ +public class SerializableVariablesDefaultConfigurationTest extends BaseSpringRestTestCase { + + @Test + public void testSerializableVariablesAreDisabledByDefault() { + VariableType serializableType = processEngineConfiguration.getVariableTypes().getVariableType(SerializableType.TYPE_NAME); + assertThat(serializableType).isInstanceOf(SerializableType.class); + assertThat(((SerializableType) serializableType).getMode()).isEqualTo(SerializableVariableTypeMode.DISABLED); + } + + @Test + @Deployment(resources = "org/flowable/rest/service/api/runtime/ProcessInstanceVariablesCollectionResourceTest.testProcess.bpmn20.xml") + public void testCreateSerializableVariable() throws Exception { + ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("oneTaskProcess"); + Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult(); + + assertSerializedObjectsNotAllowed(createSerializableVariableRequest( + RestUrls.createRelativeResourceUrl(RestUrls.URL_PROCESS_INSTANCE_VARIABLE_COLLECTION, processInstance.getId()))); + assertSerializedObjectsNotAllowed(createSerializableVariableRequest( + RestUrls.createRelativeResourceUrl(RestUrls.URL_EXECUTION_VARIABLE_COLLECTION, processInstance.getId()))); + assertSerializedObjectsNotAllowed(createSerializableVariableRequest( + RestUrls.createRelativeResourceUrl(RestUrls.URL_TASK_VARIABLES_COLLECTION, task.getId()))); + + assertThat(runtimeService.hasVariable(processInstance.getId(), "serializableVariable")).isFalse(); + assertThat(taskService.hasVariable(task.getId(), "serializableVariable")).isFalse(); + } + + @Test + @Deployment(resources = "org/flowable/rest/service/api/runtime/ProcessInstanceVariablesCollectionResourceTest.testProcess.bpmn20.xml") + public void testGetSerializableVariableData() { + ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("oneTaskProcess"); + Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult(); + // Existing serializable variables, e.g. created before serializable variables were disabled + withSerializableVariablesEnabled(() -> { + runtimeService.setVariable(processInstance.getId(), "serializableVariable", createSerializableVariable()); + taskService.setVariableLocal(task.getId(), "localSerializableVariable", createSerializableVariable()); + }); + + assertSerializedObjectsNotAllowed(new HttpGet(SERVER_URL_PREFIX + + RestUrls.createRelativeResourceUrl(RestUrls.URL_PROCESS_INSTANCE_VARIABLE_DATA, processInstance.getId(), "serializableVariable"))); + assertSerializedObjectsNotAllowed(new HttpGet(SERVER_URL_PREFIX + + RestUrls.createRelativeResourceUrl(RestUrls.URL_EXECUTION_VARIABLE_DATA, processInstance.getId(), "serializableVariable"))); + assertSerializedObjectsNotAllowed(new HttpGet(SERVER_URL_PREFIX + + RestUrls.createRelativeResourceUrl(RestUrls.URL_TASK_VARIABLE_DATA, task.getId(), "serializableVariable") + "?scope=global")); + assertSerializedObjectsNotAllowed(new HttpGet(SERVER_URL_PREFIX + + RestUrls.createRelativeResourceUrl(RestUrls.URL_TASK_VARIABLE_DATA, task.getId(), "localSerializableVariable"))); + + String variableInstanceId = runtimeService.createVariableInstanceQuery().processInstanceId(processInstance.getId()) + .variableName("serializableVariable").excludeVariableInitialization().singleResult().getId(); + assertSerializedObjectsNotAllowed(new HttpGet(SERVER_URL_PREFIX + + RestUrls.createRelativeResourceUrl(RestUrls.URL_VARIABLE_INSTANCE_DATA, variableInstanceId))); + + String historicVariableInstanceId = historyService.createHistoricVariableInstanceQuery().processInstanceId(processInstance.getId()) + .variableName("serializableVariable").excludeVariableInitialization().singleResult().getId(); + assertSerializedObjectsNotAllowed(new HttpGet(SERVER_URL_PREFIX + + RestUrls.createRelativeResourceUrl(RestUrls.URL_HISTORIC_VARIABLE_INSTANCE_DATA, historicVariableInstanceId))); + assertSerializedObjectsNotAllowed(new HttpGet(SERVER_URL_PREFIX + + RestUrls.createRelativeResourceUrl(RestUrls.URL_HISTORIC_PROCESS_INSTANCE_VARIABLE_DATA, processInstance.getId(), "serializableVariable"))); + assertSerializedObjectsNotAllowed(new HttpGet(SERVER_URL_PREFIX + + RestUrls.createRelativeResourceUrl(RestUrls.URL_HISTORIC_TASK_INSTANCE_VARIABLE_DATA, task.getId(), "serializableVariable"))); + assertSerializedObjectsNotAllowed(new HttpGet(SERVER_URL_PREFIX + + RestUrls.createRelativeResourceUrl(RestUrls.URL_HISTORIC_TASK_INSTANCE_VARIABLE_DATA, task.getId(), "localSerializableVariable"))); + + String historicDetailId = historyService.createHistoricDetailQuery().processInstanceId(processInstance.getId()) + .variableUpdates().excludeTaskDetails().singleResult().getId(); + assertSerializedObjectsNotAllowed(new HttpGet(SERVER_URL_PREFIX + + RestUrls.createRelativeResourceUrl(RestUrls.URL_HISTORIC_DETAIL_VARIABLE_DATA, historicDetailId))); + } + + @Test + @Deployment(resources = "org/flowable/rest/service/api/runtime/ProcessInstanceVariablesCollectionResourceTest.testProcess.bpmn20.xml") + public void testGetSerializableVariable() { + ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("oneTaskProcess"); + withSerializableVariablesEnabled(() -> runtimeService.setVariable(processInstance.getId(), "serializableVariable", createSerializableVariable())); + + // The variable is returned without its value + CloseableHttpResponse response = executeRequest(new HttpGet(SERVER_URL_PREFIX + + RestUrls.createRelativeResourceUrl(RestUrls.URL_PROCESS_INSTANCE_VARIABLE, processInstance.getId(), "serializableVariable")), + HttpStatus.SC_OK); + JsonNode responseNode = readContent(response); + closeResponse(response); + assertThat(responseNode.path("name").asString()).isEqualTo("serializableVariable"); + assertThat(responseNode.path("type").asString()).isEqualTo("serializable"); + assertThat(responseNode.path("valueUnresolvable").asBoolean()).isTrue(); + assertThat(responseNode.path("value").isMissingNode() || responseNode.path("value").isNull()).isTrue(); + } + + @Test + @Deployment(resources = "org/flowable/rest/service/api/runtime/ProcessInstanceVariablesCollectionResourceTest.testProcess.bpmn20.xml") + public void testCreateVariableWithUntypedJsonValue() throws Exception { + ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("oneTaskProcess"); + + // A JSON object or array without a type would have to be stored using Java serialization + for (String value : List.of("{ \"name\": \"kermit\" }", "[ \"kermit\" ]")) { + HttpPost httpPost = new HttpPost(SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_PROCESS_INSTANCE_VARIABLE_COLLECTION, processInstance.getId())); + httpPost.setEntity(new StringEntity("[ { \"name\": \"untypedVariable\", \"value\": " + value + " } ]")); + CloseableHttpResponse response = executeRequest(httpPost, HttpStatus.SC_BAD_REQUEST); + JsonNode responseNode = objectMapper.readTree(response.getEntity().getContent()); + closeResponse(response); + assertThat(responseNode.path("exception").asString()) + .isEqualTo("Variable 'untypedVariable' has a JSON object or array as value, but no type. Set the type of the variable to 'json'."); + } + assertThat(runtimeService.hasVariable(processInstance.getId(), "untypedVariable")).isFalse(); + + // With the json type the value is stored as JSON + HttpPost httpPost = new HttpPost(SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_PROCESS_INSTANCE_VARIABLE_COLLECTION, processInstance.getId())); + httpPost.setEntity(new StringEntity("[ { \"name\": \"typedVariable\", \"type\": \"json\", \"value\": { \"name\": \"kermit\" } } ]")); + closeResponse(executeRequest(httpPost, HttpStatus.SC_CREATED)); + assertThat(runtimeService.getVariableInstance(processInstance.getId(), "typedVariable").getTypeName()).isEqualTo("json"); + } + + protected void assertSerializedObjectsNotAllowed(HttpUriRequest request) { + // No JSON content type is added, the request can be a multipart request + CloseableHttpResponse response = internalExecuteRequest(request, HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE, false); + JsonNode responseNode = readContent(response); + closeResponse(response); + assertThat(responseNode.path("exception").asString()).isEqualTo("Serialized objects are not allowed"); + } + + protected HttpPost createSerializableVariableRequest(String url) throws Exception { + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + try (ObjectOutputStream output = new ObjectOutputStream(buffer)) { + output.writeObject(createSerializableVariable()); + } + + Map additionalFields = new HashMap<>(); + additionalFields.put("name", "serializableVariable"); + additionalFields.put("type", "serializable"); + + HttpPost httpPost = new HttpPost(SERVER_URL_PREFIX + url); + httpPost.setEntity(HttpMultipartHelper.getMultiPartEntity("value", "application/x-java-serialized-object", + new ByteArrayInputStream(buffer.toByteArray()), additionalFields)); + return httpPost; + } + + protected TestSerializableVariable createSerializableVariable() { + TestSerializableVariable serializable = new TestSerializableVariable(); + serializable.setSomeField("some value"); + return serializable; + } + + /** + * Temporarily enables the serializable variable type in the engine, in order to create serializable variables. + */ + protected void withSerializableVariablesEnabled(Runnable runnable) { + VariableTypes variableTypes = processEngineConfiguration.getVariableTypes(); + VariableType disabledType = variableTypes.getVariableType(SerializableType.TYPE_NAME); + int typeIndex = variableTypes.getTypeIndex(disabledType); + SerializableType enabledType = new SerializableType(false, processEngineConfiguration.getVariableLengthVerifier(), + SerializableVariableTypeMode.ENABLED); + variableTypes.removeType(disabledType); + variableTypes.addType(enabledType, typeIndex); + try { + runnable.run(); + } finally { + variableTypes.removeType(enabledType); + variableTypes.addType(disabledType, typeIndex); + } + } +} diff --git a/modules/flowable-rest/src/test/java/org/flowable/rest/service/api/runtime/SerializableVariablesDiabledTest.java b/modules/flowable-rest/src/test/java/org/flowable/rest/service/api/runtime/SerializableVariablesDiabledTest.java index 51640df45fa..ca6822f8cc8 100644 --- a/modules/flowable-rest/src/test/java/org/flowable/rest/service/api/runtime/SerializableVariablesDiabledTest.java +++ b/modules/flowable-rest/src/test/java/org/flowable/rest/service/api/runtime/SerializableVariablesDiabledTest.java @@ -149,6 +149,15 @@ public void testGetSerializableVariableData() throws Exception { assertResponseStatus(new HttpGet(SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_HISTORIC_PROCESS_INSTANCE_VARIABLE_DATA, processInstance.getId(), "serializableVariable")), HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE); + assertResponseStatus(new HttpGet(SERVER_URL_PREFIX + + RestUrls.createRelativeResourceUrl(RestUrls.URL_HISTORIC_TASK_INSTANCE_VARIABLE_DATA, task.getId(), "serializableVariable")), + HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE); + + String historicDetailId = historyService.createHistoricDetailQuery().processInstanceId(processInstance.getId()) + .variableUpdates().singleResult().getId(); + assertResponseStatus(new HttpGet(SERVER_URL_PREFIX + + RestUrls.createRelativeResourceUrl(RestUrls.URL_HISTORIC_DETAIL_VARIABLE_DATA, historicDetailId)), + HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE); } public void assertResponseStatus(HttpUriRequest request, int expectedStatusCode) { diff --git a/modules/flowable-variable-service/src/main/java/org/flowable/variable/service/impl/persistence/entity/VariableScopeImpl.java b/modules/flowable-variable-service/src/main/java/org/flowable/variable/service/impl/persistence/entity/VariableScopeImpl.java index 48d9cd4c395..51e0a829af8 100644 --- a/modules/flowable-variable-service/src/main/java/org/flowable/variable/service/impl/persistence/entity/VariableScopeImpl.java +++ b/modules/flowable-variable-service/src/main/java/org/flowable/variable/service/impl/persistence/entity/VariableScopeImpl.java @@ -844,10 +844,14 @@ protected void deleteVariableInstanceForExplicitUserCall(VariableInstanceEntity protected void updateVariableInstance(VariableInstanceEntity variableInstance, Object newVariableValue) { - Object oldVariableValue = variableInstance.getValue(); + VariableServiceConfiguration variableServiceConfiguration = getVariableServiceConfiguration(); + // The old value is only needed for the logging session + Object oldVariableValue = null; + if (variableServiceConfiguration.isLoggingSessionEnabled() && VariableLoggingSessionUtil.isValueReadable(variableInstance)) { + oldVariableValue = variableInstance.getValue(); + } String oldVariableType = variableInstance.getTypeName(); initializeVariableInstanceBackPointer(variableInstance); - VariableServiceConfiguration variableServiceConfiguration = getVariableServiceConfiguration(); variableServiceConfiguration.getVariableInstanceValueModifier().updateVariableValue(variableInstance, newVariableValue, getTenantId()); if (isPropagateToHistoricVariable()) { if (variableServiceConfiguration.getInternalHistoryVariableManager() != null) { diff --git a/modules/flowable-variable-service/src/main/java/org/flowable/variable/service/impl/types/SerializableVariableTypeMode.java b/modules/flowable-variable-service/src/main/java/org/flowable/variable/service/impl/types/SerializableVariableTypeMode.java index 95413f29d08..4fa65d9a100 100644 --- a/modules/flowable-variable-service/src/main/java/org/flowable/variable/service/impl/types/SerializableVariableTypeMode.java +++ b/modules/flowable-variable-service/src/main/java/org/flowable/variable/service/impl/types/SerializableVariableTypeMode.java @@ -32,7 +32,8 @@ public enum SerializableVariableTypeMode { /** * Serializable variables can neither be read nor written. - * Existing variables of this type can still be loaded (e.g. for deletion), but accessing their value fails. + * Existing variables of this type can still be loaded, removed and overwritten with a value of another type, + * but accessing their value fails. */ DISABLED; diff --git a/modules/flowable-variable-service/src/main/java/org/flowable/variable/service/impl/util/VariableLoggingSessionUtil.java b/modules/flowable-variable-service/src/main/java/org/flowable/variable/service/impl/util/VariableLoggingSessionUtil.java index 957f342ec79..0f0c12dc4cb 100644 --- a/modules/flowable-variable-service/src/main/java/org/flowable/variable/service/impl/util/VariableLoggingSessionUtil.java +++ b/modules/flowable-variable-service/src/main/java/org/flowable/variable/service/impl/util/VariableLoggingSessionUtil.java @@ -20,6 +20,7 @@ import org.flowable.common.engine.api.scope.ScopeTypes; import org.flowable.common.engine.impl.logging.LoggingSessionUtil; import org.flowable.common.engine.impl.util.JsonUtil; +import org.flowable.variable.api.types.VariableType; import org.flowable.variable.service.impl.persistence.entity.VariableInstanceEntity; import org.flowable.variable.service.impl.types.BigDecimalType; import org.flowable.variable.service.impl.types.BigIntegerType; @@ -33,6 +34,7 @@ import org.flowable.variable.service.impl.types.JsonType; import org.flowable.variable.service.impl.types.LongType; import org.flowable.variable.service.impl.types.NullType; +import org.flowable.variable.service.impl.types.SerializableType; import org.flowable.variable.service.impl.types.ShortType; import org.flowable.variable.service.impl.types.StringType; import org.flowable.variable.service.impl.types.UUIDType; @@ -69,17 +71,32 @@ public static ObjectNode addLoggingData(String message, VariableInstanceEntity v String variableTypeName = variableInstance.getType().getTypeName(); loggingNode.put("variableType", variableTypeName); - if (variableInstance.getValue() == null) { - loggingNode.putNull("variableRawValue"); - loggingNode.putNull("variableValue"); - - } else { - addVariableValue(variableInstance.getValue(), variableTypeName, "variableRawValue", "variableValue", loggingNode, objectMapper); + if (isValueReadable(variableInstance)) { + if (variableInstance.getValue() == null) { + loggingNode.putNull("variableRawValue"); + loggingNode.putNull("variableValue"); + + } else { + addVariableValue(variableInstance.getValue(), variableTypeName, "variableRawValue", "variableValue", loggingNode, objectMapper); + } } } return loggingNode; } + + /** + * Whether the value of the variable instance can be read for logging purposes. + * The value of a serializable variable cannot be read when the serializable variable type is disabled, + * such a variable is logged without its value. + */ + public static boolean isValueReadable(VariableInstanceEntity variableInstance) { + VariableType variableType = variableInstance.getType(); + if (variableType instanceof SerializableType serializableType) { + return serializableType.getMode().isReadable(); + } + return true; + } public static void addVariableValue(Object variableValue, String variableTypeName, String variableRawValueName, String variableValueName, ObjectNode loggingNode, ObjectMapper objectMapper) { diff --git a/modules/flowable5-test/src/test/java/org/activiti/engine/test/api/variables/SerializableVariableTypeModeTest.java b/modules/flowable5-test/src/test/java/org/activiti/engine/test/api/variables/SerializableVariableTypeModeTest.java new file mode 100644 index 00000000000..14cfad6d623 --- /dev/null +++ b/modules/flowable5-test/src/test/java/org/activiti/engine/test/api/variables/SerializableVariableTypeModeTest.java @@ -0,0 +1,136 @@ +/* Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.activiti.engine.test.api.variables; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl; +import org.activiti.engine.impl.test.PluggableFlowableTestCase; +import org.activiti.engine.impl.variable.SerializableType; +import org.activiti.examples.variables.SomeSerializable; +import org.flowable.common.engine.api.FlowableException; +import org.flowable.engine.runtime.ProcessInstance; +import org.flowable.engine.test.Deployment; +import org.flowable.variable.api.types.VariableType; +import org.flowable.variable.api.types.VariableTypes; +import org.flowable.variable.service.impl.types.SerializableVariableTypeMode; + +/** + * Tests the serializable variable type mode of the embedded Flowable 5 engine. By default the serializable variable type is disabled. + * + * @author Filip Hrisafov + */ +public class SerializableVariableTypeModeTest extends PluggableFlowableTestCase { + + public void testDefaultModeIsDisabled() { + VariableType serializableType = getFlowable5VariableTypes().getVariableType(SerializableType.TYPE_NAME); + assertThat(serializableType).isInstanceOf(SerializableType.class); + assertThat(((SerializableType) serializableType).getMode()).isEqualTo(SerializableVariableTypeMode.DISABLED); + } + + @Deployment(resources = "org/activiti/engine/test/api/oneTaskProcess.bpmn20.xml") + public void testDisabledMode() { + ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("oneTaskProcess"); + + assertThatThrownBy(() -> runtimeService.setVariable(processInstance.getId(), "otherVar", new SomeSerializable("other"))) + .isInstanceOf(FlowableException.class) + .hasMessageContaining("Storing the value of variable 'otherVar' of type " + SomeSerializable.class.getName() + + " is not allowed, because serializable variables are disabled"); + assertThat(runtimeService.hasVariable(processInstance.getId(), "otherVar")).isFalse(); + + // Existing serializable variable, e.g. created before serializable variables were disabled + withSerializableVariableTypeMode(SerializableVariableTypeMode.ENABLED, + () -> runtimeService.setVariable(processInstance.getId(), "serializableVar", new SomeSerializable("test"))); + + assertThatThrownBy(() -> runtimeService.getVariable(processInstance.getId(), "serializableVar")) + .hasMessageContaining("Reading the value of serializable variable 'serializableVar' is not allowed, because serializable variables are disabled"); + + // Removing the variable and deleting the process instance is possible + runtimeService.removeVariable(processInstance.getId(), "serializableVar"); + assertThat(runtimeService.hasVariable(processInstance.getId(), "serializableVar")).isFalse(); + + withSerializableVariableTypeMode(SerializableVariableTypeMode.ENABLED, + () -> runtimeService.setVariable(processInstance.getId(), "serializableVar", new SomeSerializable("test"))); + runtimeService.deleteProcessInstance(processInstance.getId(), "test"); + assertThat(runtimeService.createProcessInstanceQuery().processInstanceId(processInstance.getId()).count()).isZero(); + } + + @Deployment(resources = "org/activiti/engine/test/api/oneTaskProcess.bpmn20.xml") + public void testReadOnlyMode() { + ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("oneTaskProcess"); + withSerializableVariableTypeMode(SerializableVariableTypeMode.ENABLED, + () -> runtimeService.setVariable(processInstance.getId(), "serializableVar", new SomeSerializable("test"))); + + withSerializableVariableTypeMode(SerializableVariableTypeMode.READ_ONLY, () -> { + assertThat(runtimeService.getVariable(processInstance.getId(), "serializableVar")) + .isInstanceOfSatisfying(SomeSerializable.class, value -> assertThat(value.getValue()).isEqualTo("test")); + + assertThatThrownBy(() -> runtimeService.setVariable(processInstance.getId(), "serializableVar", new SomeSerializable("updated"))) + .hasMessageContaining("Storing the value of variable 'serializableVar' of type " + SomeSerializable.class.getName() + + " is not allowed"); + + assertThatThrownBy(() -> runtimeService.setVariable(processInstance.getId(), "otherVar", new SomeSerializable("other"))) + .hasMessageContaining("Storing the value of variable 'otherVar' of type " + SomeSerializable.class.getName() + + " is not allowed"); + + assertThat(runtimeService.getVariable(processInstance.getId(), "serializableVar")) + .isInstanceOfSatisfying(SomeSerializable.class, value -> assertThat(value.getValue()).isEqualTo("test")); + + // Migrating the existing variable to another type is possible + runtimeService.setVariable(processInstance.getId(), "serializableVar", "migrated"); + assertThat(runtimeService.getVariable(processInstance.getId(), "serializableVar")).isEqualTo("migrated"); + }); + } + + @Deployment(resources = "org/activiti/engine/test/api/oneTaskProcess.bpmn20.xml") + public void testFloatVariable() { + ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("oneTaskProcess"); + + // Float values do not need the serializable variable type + runtimeService.setVariable(processInstance.getId(), "floatVar", 1.1f); + + assertThat(getFlowable5VariableTypes().findVariableType(1.1f).getTypeName()).isEqualTo("float"); + assertThat(runtimeService.getVariable(processInstance.getId(), "floatVar")).isEqualTo(1.1f); + assertThat(runtimeService.createProcessInstanceQuery().variableValueEquals("floatVar", 1.1f).count()).isEqualTo(1); + + runtimeService.setVariable(processInstance.getId(), "floatVar", 2.5f); + assertThat(runtimeService.getVariable(processInstance.getId(), "floatVar")).isEqualTo(2.5f); + assertThat(historyService.createHistoricVariableInstanceQuery().processInstanceId(processInstance.getId()).variableName("floatVar") + .singleResult().getValue()).isEqualTo(2.5f); + } + + protected VariableTypes getFlowable5VariableTypes() { + ProcessEngineConfigurationImpl flowable5ProcessEngineConfiguration = (ProcessEngineConfigurationImpl) processEngineConfiguration + .getFlowable5CompatibilityHandler().getRawProcessConfiguration(); + return flowable5ProcessEngineConfiguration.getVariableTypes(); + } + + /** + * Temporarily changes the mode of the serializable variable type of the Flowable 5 engine. + */ + protected void withSerializableVariableTypeMode(SerializableVariableTypeMode mode, Runnable runnable) { + VariableTypes variableTypes = getFlowable5VariableTypes(); + VariableType defaultType = variableTypes.getVariableType(SerializableType.TYPE_NAME); + int typeIndex = variableTypes.getTypeIndex(defaultType); + SerializableType type = new SerializableType(mode); + variableTypes.removeType(defaultType); + variableTypes.addType(type, typeIndex); + try { + runnable.run(); + } finally { + variableTypes.removeType(type); + variableTypes.addType(defaultType, typeIndex); + } + } +}