EvaluationContextFactories.java

1
package pro.verron.officestamper.preset;
2
3
4
import org.springframework.expression.*;
5
import org.springframework.expression.spel.SpelEvaluationException;
6
import org.springframework.expression.spel.SpelMessage;
7
import org.springframework.expression.spel.support.*;
8
import pro.verron.officestamper.api.EvaluationContextFactory;
9
import pro.verron.officestamper.api.OfficeStamperException;
10
11
import java.util.ArrayList;
12
13
/// Utility class for configuring the [EvaluationContext] used by officestamper.
14
public class EvaluationContextFactories {
15
16
    private EvaluationContextFactories() {
17
        throw new OfficeStamperException("EvaluationContextConfigurers cannot be instantiated");
18
    }
19
20
    /// Returns an [EvaluationContextFactory] instance that does no customization.
21
    /// This factory does nothing to the [StandardEvaluationContext] class, and therefore all the unfiltered features
22
    /// are accessible. It should be used when there is a need to use the powerful features of the aforementioned class,
23
    /// and there is a trust that the template won't contain any dangerous injections.
24
    ///
25
    /// @return an [EvaluationContextFactory] instance
26
    public static EvaluationContextFactory noopFactory() {
27 1 1. noopFactory : replaced return value with null for pro/verron/officestamper/preset/EvaluationContextFactories::noopFactory → KILLED
        return object -> {
28
            var standardEvaluationContext = new StandardEvaluationContext(object);
29
            var reflectivePropertyAccessor = new ReflectivePropertyAccessor();
30
            var mapAccessor = new MapAccessor();
31
            var propertyAccessors = new ArrayList<PropertyAccessor>();
32
            propertyAccessors.add(reflectivePropertyAccessor);
33
            propertyAccessors.add(mapAccessor);
34 1 1. lambda$noopFactory$0 : removed call to org/springframework/expression/spel/support/StandardEvaluationContext::setPropertyAccessors → SURVIVED
            standardEvaluationContext.setPropertyAccessors(propertyAccessors);
35 1 1. lambda$noopFactory$0 : replaced return value with null for pro/verron/officestamper/preset/EvaluationContextFactories::lambda$noopFactory$0 → KILLED
            return standardEvaluationContext;
36
        };
37
    }
38
39
    /// Returns a default [EvaluationContextFactory] instance.
40
    /// The default factory provides better default security for the [EvaluationContext] used by OfficeStamper. It
41
    /// sets up the context with enhanced security measures, such as limited property accessors, constructor resolvers,
42
    /// and method resolvers. It also sets a type locator, type converter, type comparator, and operator overloader.
43
    /// This factory is recommended to be used when there is a need for improved security and protection against
44
    /// potentially dangerous injections in the template.
45
    ///
46
    /// @return an [EvaluationContextFactory] instance with enhanced security features
47
    public static EvaluationContextFactory defaultFactory() {
48 1 1. defaultFactory : replaced return value with null for pro/verron/officestamper/preset/EvaluationContextFactories::defaultFactory → KILLED
        return object -> {
49
            var standardEvaluationContext = new StandardEvaluationContext(object);
50
51
            var propertyAccessor = DataBindingPropertyAccessor.forReadWriteAccess();
52
            var mapAccessor = new MapAccessor();
53
            var propertyAccessors = new ArrayList<PropertyAccessor>();
54
            propertyAccessors.add(propertyAccessor);
55
            propertyAccessors.add(mapAccessor);
56 1 1. lambda$defaultFactory$0 : removed call to org/springframework/expression/spel/support/StandardEvaluationContext::setPropertyAccessors → KILLED
            standardEvaluationContext.setPropertyAccessors(propertyAccessors);
57
58 1 1. lambda$defaultFactory$0 : removed call to org/springframework/expression/spel/support/StandardEvaluationContext::setConstructorResolvers → SURVIVED
            standardEvaluationContext.setConstructorResolvers(new ArrayList<>());
59
60
            var instanceMethodInvocation = DataBindingMethodResolver.forInstanceMethodInvocation();
61
            var methodResolvers = new ArrayList<MethodResolver>();
62
            methodResolvers.add(instanceMethodInvocation);
63 1 1. lambda$defaultFactory$0 : removed call to org/springframework/expression/spel/support/StandardEvaluationContext::setMethodResolvers → SURVIVED
            standardEvaluationContext.setMethodResolvers(methodResolvers);
64
65
            BeanResolver beanResolver = (_, _) -> {
66
                throw new AccessException("Bean resolution not supported for security reasons.");
67
            };
68 1 1. lambda$defaultFactory$0 : removed call to org/springframework/expression/spel/support/StandardEvaluationContext::setBeanResolver → SURVIVED
            standardEvaluationContext.setBeanResolver(beanResolver);
69
70
            TypeLocator typeLocator = typeName -> {
71
                throw new SpelEvaluationException(SpelMessage.TYPE_NOT_FOUND, typeName);
72
            };
73 1 1. lambda$defaultFactory$0 : removed call to org/springframework/expression/spel/support/StandardEvaluationContext::setTypeLocator → KILLED
            standardEvaluationContext.setTypeLocator(typeLocator);
74
75 1 1. lambda$defaultFactory$0 : removed call to org/springframework/expression/spel/support/StandardEvaluationContext::setTypeConverter → SURVIVED
            standardEvaluationContext.setTypeConverter(new StandardTypeConverter());
76 1 1. lambda$defaultFactory$0 : removed call to org/springframework/expression/spel/support/StandardEvaluationContext::setTypeComparator → SURVIVED
            standardEvaluationContext.setTypeComparator(new StandardTypeComparator());
77 1 1. lambda$defaultFactory$0 : removed call to org/springframework/expression/spel/support/StandardEvaluationContext::setOperatorOverloader → SURVIVED
            standardEvaluationContext.setOperatorOverloader(new StandardOperatorOverloader());
78 1 1. lambda$defaultFactory$0 : replaced return value with null for pro/verron/officestamper/preset/EvaluationContextFactories::lambda$defaultFactory$0 → KILLED
            return standardEvaluationContext;
79
        };
80
    }
81
82
}

Mutations

27

1.1
Location : noopFactory
Killed by : pro.verron.officestamper.test.SpelInstantiationTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.SpelInstantiationTest]/[test-template:testDateInstantiationAndResolution(pro.verron.officestamper.test.utils.ContextFactory)]/[test-template-invocation:#2]
replaced return value with null for pro/verron/officestamper/preset/EvaluationContextFactories::noopFactory → KILLED

34

1.1
Location : lambda$noopFactory$0
Killed by : none
removed call to org/springframework/expression/spel/support/StandardEvaluationContext::setPropertyAccessors → SURVIVED
Covering tests

35

1.1
Location : lambda$noopFactory$0
Killed by : pro.verron.officestamper.test.SpelInstantiationTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.SpelInstantiationTest]/[test-template:testDateInstantiationAndResolution(pro.verron.officestamper.test.utils.ContextFactory)]/[test-template-invocation:#2]
replaced return value with null for pro/verron/officestamper/preset/EvaluationContextFactories::lambda$noopFactory$0 → KILLED

48

1.1
Location : defaultFactory
Killed by : pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest]/[test-template:fails(pro.verron.officestamper.test.utils.ContextFactory)]/[test-template-invocation:#2]
replaced return value with null for pro/verron/officestamper/preset/EvaluationContextFactories::defaultFactory → KILLED

56

1.1
Location : lambda$defaultFactory$0
Killed by : pro.verron.officestamper.test.CustomFunctionTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.CustomFunctionTests]/[test-template:trifunctions(pro.verron.officestamper.test.utils.ContextFactory, java.lang.String, java.lang.String)]/[test-template-invocation:#10]
removed call to org/springframework/expression/spel/support/StandardEvaluationContext::setPropertyAccessors → KILLED

58

1.1
Location : lambda$defaultFactory$0
Killed by : none
removed call to org/springframework/expression/spel/support/StandardEvaluationContext::setConstructorResolvers → SURVIVED
Covering tests

63

1.1
Location : lambda$defaultFactory$0
Killed by : none
removed call to org/springframework/expression/spel/support/StandardEvaluationContext::setMethodResolvers → SURVIVED
Covering tests

68

1.1
Location : lambda$defaultFactory$0
Killed by : none
removed call to org/springframework/expression/spel/support/StandardEvaluationContext::setBeanResolver → SURVIVED
Covering tests

73

1.1
Location : lambda$defaultFactory$0
Killed by : pro.verron.officestamper.test.SpelInjectionTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.SpelInjectionTest]/[test-template:spelInjectionTest(pro.verron.officestamper.test.utils.ContextFactory)]/[test-template-invocation:#2]
removed call to org/springframework/expression/spel/support/StandardEvaluationContext::setTypeLocator → KILLED

75

1.1
Location : lambda$defaultFactory$0
Killed by : none
removed call to org/springframework/expression/spel/support/StandardEvaluationContext::setTypeConverter → SURVIVED
Covering tests

76

1.1
Location : lambda$defaultFactory$0
Killed by : none
removed call to org/springframework/expression/spel/support/StandardEvaluationContext::setTypeComparator → SURVIVED
Covering tests

77

1.1
Location : lambda$defaultFactory$0
Killed by : none
removed call to org/springframework/expression/spel/support/StandardEvaluationContext::setOperatorOverloader → SURVIVED
Covering tests

78

1.1
Location : lambda$defaultFactory$0
Killed by : pro.verron.officestamper.test.SpelInjectionTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.SpelInjectionTest]/[test-template:spelInjectionTest(pro.verron.officestamper.test.utils.ContextFactory)]/[test-template-invocation:#2]
replaced return value with null for pro/verron/officestamper/preset/EvaluationContextFactories::lambda$defaultFactory$0 → KILLED

Active mutators

Tests examined


Report generated by PIT 1.23.1 support