Skip to content

Commit

Permalink
Merge pull request #806 from apache/WW-5339-astmap-block
Browse files Browse the repository at this point in the history
WW-5339 Add option to block custom OGNL maps
  • Loading branch information
kusalk authored Dec 5, 2023
2 parents 80e8361 + 002e598 commit 6fcb501
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
import com.opensymphony.xwork2.util.fs.DefaultFileManagerFactory;
import com.opensymphony.xwork2.util.location.LocatableProperties;
import com.opensymphony.xwork2.util.reflection.ReflectionProvider;
import ognl.ClassResolver;
import ognl.PropertyAccessor;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -390,6 +391,7 @@ public static ContainerBuilder bootstrapFactories(ContainerBuilder builder) {

.factory(ObjectTypeDeterminer.class, DefaultObjectTypeDeterminer.class, Scope.SINGLETON)
.factory(PropertyAccessor.class, CompoundRoot.class.getName(), CompoundRootAccessor.class, Scope.SINGLETON)
.factory(ClassResolver.class, CompoundRoot.class.getName(), CompoundRootAccessor.class, Scope.SINGLETON)

.factory(ExpressionCacheFactory.class, DefaultOgnlExpressionCacheFactory.class, Scope.SINGLETON)
.factory(BeanInfoCacheFactory.class, DefaultOgnlBeanInfoCacheFactory.class, Scope.SINGLETON)
Expand Down
9 changes: 5 additions & 4 deletions core/src/main/java/com/opensymphony/xwork2/ognl/OgnlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor;
import com.opensymphony.xwork2.util.CompoundRoot;
import com.opensymphony.xwork2.util.reflection.ReflectionException;
import ognl.ClassResolver;
Expand Down Expand Up @@ -856,10 +855,12 @@ protected Map<String, Object> createDefaultContext(Object root) {
return createDefaultContext(root, null);
}

protected Map<String, Object> createDefaultContext(Object root, ClassResolver classResolver) {
ClassResolver resolver = classResolver;
protected Map<String, Object> createDefaultContext(Object root, ClassResolver resolver) {
if (resolver == null) {
resolver = container.getInstance(CompoundRootAccessor.class);
resolver = container.getInstance(ClassResolver.class, CompoundRoot.class.getName());
if (resolver == null) {
throw new IllegalStateException("Cannot find ClassResolver");
}
}

SecurityMemberAccess memberAccess = container.getInstance(SecurityMemberAccess.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor;
import com.opensymphony.xwork2.util.CompoundRoot;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.ValueStackFactory;
import ognl.ClassResolver;
import ognl.MethodAccessor;
import ognl.OgnlRuntime;
import ognl.PropertyAccessor;
Expand All @@ -50,6 +50,11 @@ protected void setXWorkConverter(XWorkConverter converter) {
this.xworkConverter = converter;
}

@Inject(value = "com.opensymphony.xwork2.util.CompoundRoot")
protected void setClassResolver(ClassResolver classResolver) {
this.compoundRootAccessor = (CompoundRootAccessor) classResolver;
}

@Inject("system")
protected void setTextProvider(TextProvider textProvider) {
this.textProvider = textProvider;
Expand All @@ -75,9 +80,6 @@ protected void setContainer(Container container) throws ClassNotFoundException {
for (String name : names) {
Class<?> cls = Class.forName(name);
OgnlRuntime.setPropertyAccessor(cls, container.getInstance(PropertyAccessor.class, name));
if (compoundRootAccessor == null && CompoundRoot.class.isAssignableFrom(cls)) {
compoundRootAccessor = (CompoundRootAccessor) container.getInstance(PropertyAccessor.class, name);
}
}

names = container.getInstanceNames(MethodAccessor.class);
Expand All @@ -91,9 +93,6 @@ protected void setContainer(Container container) throws ClassNotFoundException {
Class<?> cls = Class.forName(name);
OgnlRuntime.setNullHandler(cls, new OgnlNullHandlerWrapper(container.getInstance(NullHandler.class, name)));
}
if (compoundRootAccessor == null) {
throw new IllegalStateException("Couldn't find the compound root accessor");
}
this.container = container;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -77,12 +76,18 @@ public String getSourceSetter(OgnlContext context, Object target, Object index)
private final static Class[] EMPTY_CLASS_ARRAY = new Class[0];
private static final Map<MethodCall, Boolean> invalidMethods = new ConcurrentHashMap<>();
private boolean devMode;
private boolean disallowCustomOgnlMap;

@Inject(StrutsConstants.STRUTS_DEVMODE)
protected void setDevMode(String mode) {
this.devMode = BooleanUtils.toBoolean(mode);
}

@Inject(value = StrutsConstants.STRUTS_DISALLOW_CUSTOM_OGNL_MAP, required = false)
public void useDisallowCustomOgnlMap(String disallowCustomOgnlMap) {
this.disallowCustomOgnlMap = BooleanUtils.toBoolean(disallowCustomOgnlMap);
}

public void setProperty(Map context, Object target, Object name, Object value) throws OgnlException {
CompoundRoot root = (CompoundRoot) target;
OgnlContext ognlContext = (OgnlContext) context;
Expand Down Expand Up @@ -275,6 +280,14 @@ public Object callStaticMethod(Map transientVars, Class aClass, String s, Object
public Class classForName(String className, Map context) throws ClassNotFoundException {
Object root = Ognl.getRoot(context);

if (disallowCustomOgnlMap) {
String nodeClassName = ((OgnlContext) context).getCurrentNode().getClass().getName();
if ("ognl.ASTMap".equals(nodeClassName)) {
LOG.error("Constructing OGNL ASTMap's from custom classes is forbidden. Attempted class: {}", className);
return null;
}
}

try {
if (root instanceof CompoundRoot) {
if (className.startsWith("vs")) {
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/org/apache/struts2/StrutsConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ public final class StrutsConstants {
/** The name of the parameter to determine whether static field access will be allowed in OGNL expressions or not */
public static final String STRUTS_ALLOW_STATIC_FIELD_ACCESS = "struts.ognl.allowStaticFieldAccess";

public static final String STRUTS_DISALLOW_CUSTOM_OGNL_MAP = "struts.ognl.disallowCustomOgnlMap";

public static final String STRUTS_MEMBER_ACCESS = "struts.securityMemberAccess";

public static final String STRUTS_OGNL_GUARD = "struts.ognlGuard";
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/resources/struts-beans.xml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@
<bean type="com.opensymphony.xwork2.util.TextParser" name="struts"
class="com.opensymphony.xwork2.util.OgnlTextParser" scope="singleton"/>

<bean type="ognl.ClassResolver" name="com.opensymphony.xwork2.util.CompoundRoot"
class="com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor"/>

<bean type="ognl.PropertyAccessor" name="com.opensymphony.xwork2.util.CompoundRoot"
class="com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor"/>
<bean type="ognl.PropertyAccessor" name="java.lang.Object"
Expand Down
28 changes: 28 additions & 0 deletions core/src/test/java/com/opensymphony/xwork2/ognl/MyCustomMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 com.opensymphony.xwork2.ognl;

import java.util.HashMap;

public class MyCustomMap<K, V> extends HashMap<K, V> {
@Override
public V get(Object key) {
return (V) "System compromised";
}
}
12 changes: 12 additions & 0 deletions core/src/test/java/com/opensymphony/xwork2/ognl/OgnlUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
import com.opensymphony.xwork2.inject.ContainerBuilder;
import com.opensymphony.xwork2.interceptor.ChainingInterceptor;
import com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor;
import com.opensymphony.xwork2.test.StubConfigurationProvider;
import com.opensymphony.xwork2.test.User;
import com.opensymphony.xwork2.util.Bar;
Expand All @@ -35,6 +36,7 @@
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.location.LocatableProperties;
import com.opensymphony.xwork2.util.reflection.ReflectionContextState;
import ognl.ClassResolver;
import ognl.InappropriateExpressionException;
import ognl.MethodFailedException;
import ognl.NoSuchPropertyException;
Expand Down Expand Up @@ -1623,6 +1625,16 @@ public void testOgnlDefaultCacheFactoryCoverage() {
assertEquals("Eviction limit for cache mismatches limit for factory ?", 15, ognlCache.getEvictionLimit());
}

public void testCustomOgnlMapBlocked() throws Exception {
String vulnerableExpr = "#@com.opensymphony.xwork2.ognl.MyCustomMap@{}.get(\"ye\")";
assertEquals("System compromised", ognlUtil.getValue(vulnerableExpr, ognlUtil.createDefaultContext(null), null));

((CompoundRootAccessor) container.getInstance(ClassResolver.class, CompoundRoot.class.getName()))
.useDisallowCustomOgnlMap(Boolean.TRUE.toString());

assertThrows(OgnlException.class, () -> ognlUtil.getValue(vulnerableExpr, ognlUtil.createDefaultContext(null), null));
}

/**
* Generate a new OgnlUtil instance (not configured by the {@link ContainerBuilder}) that can be used for
* basic tests, with its Expression and BeanInfo factories set to LRU mode.
Expand Down

0 comments on commit 6fcb501

Please sign in to comment.