Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APPSEC-2909 Fix SQL injection vulnerability in Activiti Cloud Query Search Api #1643

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.Path;
import java.math.BigDecimal;
import org.activiti.cloud.services.query.rest.filter.FilterOperator;
import org.activiti.cloud.services.query.rest.filter.VariableType;

Expand All @@ -37,7 +38,7 @@ public BigDecimalVariableValueCondition(
}

@Override
protected Object getConvertedValue() {
return value;
protected BigDecimal getConvertedValue() {
return BigDecimal.valueOf(Double.parseDouble(value));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.Path;
import java.time.LocalDate;
import org.activiti.cloud.dialect.CustomPostgreSQLDialect;
import org.activiti.cloud.services.query.rest.exception.IllegalFilterException;
import org.activiti.cloud.services.query.rest.filter.FilterOperator;
Expand Down Expand Up @@ -47,7 +48,7 @@ protected String getFunctionName() {
}

@Override
protected String getConvertedValue() {
return value;
protected LocalDate getConvertedValue() {
return LocalDate.parse(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.Path;
import java.time.Instant;
import java.util.Date;
import org.activiti.cloud.dialect.CustomPostgreSQLDialect;
import org.activiti.cloud.services.query.rest.exception.IllegalFilterException;
import org.activiti.cloud.services.query.rest.filter.FilterOperator;
Expand Down Expand Up @@ -47,7 +49,7 @@ protected String getFunctionName() {
}

@Override
protected String getConvertedValue() {
return value;
protected Date getConvertedValue() {
return Date.from(Instant.parse(value));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ protected String getFunctionName() {

@Override
protected String getConvertedValue() {
return value;
return value.replaceAll("'", "''").replaceAll("\"", "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;

import jakarta.persistence.EntityManager;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.stream.Stream;
import org.activiti.cloud.services.query.app.repository.VariableRepository;
Expand All @@ -31,6 +36,7 @@
import org.activiti.cloud.services.query.rest.filter.VariableFilter;
import org.activiti.cloud.services.query.rest.filter.VariableType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -54,6 +60,9 @@ class SpecificationSupportIT {
@Autowired
VariableRepository variableRepository;

@Autowired
EntityManager entityManager;

@Container
@ServiceConnection
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:15-alpine");
Expand Down Expand Up @@ -192,6 +201,132 @@ void should_findEntitiesByVariableValueUsingSpecification(
assertThat(retrieved).containsExactlyInAnyOrderElementsOf(variables.subList(0, expectedSublistToIndex));
}

@Test
public void testStringVariableValueConditionConvertedValue() {
var criteriaBuilder = entityManager.getCriteriaBuilder();
var query = criteriaBuilder.createQuery(ProcessVariableEntity.class);
var root = query.from(ProcessVariableEntity.class);

// given
var subject = new StringVariableValueCondition(
root.get(ProcessVariableEntity_.value),
FilterOperator.EQUALS,
"va'lue\"",
criteriaBuilder
);

// when
var value = subject.getConvertedValue();

// then
assertThat(value).isEqualTo("va''lue");
}

@Test
public void testBooleanVariableValueConditionConvertedValue() {
var criteriaBuilder = entityManager.getCriteriaBuilder();
var query = criteriaBuilder.createQuery(ProcessVariableEntity.class);
var root = query.from(ProcessVariableEntity.class);

// given
var subject = new BooleanVariableValueCondition(
root.get(ProcessVariableEntity_.value),
FilterOperator.EQUALS,
"true",
criteriaBuilder
);

// when
var value = subject.getConvertedValue();

// then
assertThat(value).isEqualTo(Boolean.TRUE);
}

@Test
public void testBigDecimalVariableValueConditionConvertedValue() {
var criteriaBuilder = entityManager.getCriteriaBuilder();
var query = criteriaBuilder.createQuery(ProcessVariableEntity.class);
var root = query.from(ProcessVariableEntity.class);

// given
var subject = new BigDecimalVariableValueCondition(
root.get(ProcessVariableEntity_.value),
FilterOperator.EQUALS,
"10.00",
criteriaBuilder
);

// when
var value = subject.getConvertedValue();

// then
assertThat(value).isEqualTo(BigDecimal.valueOf(10.00));
}

@Test
public void testDatetimeVariableValueConditionConvertedValue() {
var criteriaBuilder = entityManager.getCriteriaBuilder();
var query = criteriaBuilder.createQuery(ProcessVariableEntity.class);
var root = query.from(ProcessVariableEntity.class);

// given
var subject = new DatetimeVariableValueCondition(
root.get(ProcessVariableEntity_.value),
FilterOperator.EQUALS,
"2024-08-02T00:11:22.000+00:00",
criteriaBuilder
);

// when
var value = subject.getConvertedValue();

// then
assertThat(value).isEqualTo(Date.from(Instant.parse("2024-08-02T00:11:22.000+00:00")));
}

@Test
public void testDateVariableValueConditionConvertedValue() {
var criteriaBuilder = entityManager.getCriteriaBuilder();
var query = criteriaBuilder.createQuery(ProcessVariableEntity.class);
var root = query.from(ProcessVariableEntity.class);

// given
var subject = new DateVariableValueCondition(
root.get(ProcessVariableEntity_.value),
FilterOperator.EQUALS,
"2024-08-02",
criteriaBuilder
);

// when
var value = subject.getConvertedValue();

// then
assertThat(value).isEqualTo(LocalDate.parse("2024-08-02"));
}

@Test
public void testIntegerVariableValueConditionConvertedValue() {
var criteriaBuilder = entityManager.getCriteriaBuilder();
var query = criteriaBuilder.createQuery(ProcessVariableEntity.class);
var root = query.from(ProcessVariableEntity.class);

// given
var subject = new IntegerVariableValueCondition(
root.get(ProcessVariableEntity_.value),
FilterOperator.EQUALS,
"123",
criteriaBuilder
);

// when
var value = subject.getConvertedValue();

// then
assertThat(value).isEqualTo(123);
}

@ParameterizedTest
@MethodSource("provideArgumentsThatShouldThrow")
void should_throw_ResponseStatusException(VariableType variableType, FilterOperator operator) {
Expand Down