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

WW-5339 Enforce exclusion list in CompoundRootAccessor #743

Closed
wants to merge 5 commits into from
Closed
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 @@ -19,10 +19,19 @@
package com.opensymphony.xwork2.ognl.accessor;

import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.ognl.OgnlUtil;
import com.opensymphony.xwork2.ognl.OgnlValueStack;
import com.opensymphony.xwork2.util.CompoundRoot;
import com.opensymphony.xwork2.util.ValueStack;
import ognl.*;
import ognl.ClassResolver;
import ognl.MethodAccessor;
import ognl.MethodFailedException;
import ognl.NoSuchPropertyException;
import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;
import ognl.OgnlRuntime;
import ognl.PropertyAccessor;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -31,10 +40,19 @@

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.util.*;
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;
import java.util.regex.Pattern;

import static com.opensymphony.xwork2.ognl.SecurityMemberAccess.isExcludedPackageNamesStatic;
import static com.opensymphony.xwork2.ognl.SecurityMemberAccess.toPackageName;
import static java.lang.String.format;
import static java.util.Collections.emptySet;
import static org.apache.commons.lang3.BooleanUtils.toBoolean;

/**
Expand Down Expand Up @@ -62,14 +80,26 @@ public String getSourceSetter(OgnlContext context, Object target, Object index)

private final static Logger LOG = LogManager.getLogger(CompoundRootAccessor.class);
private final static Class[] EMPTY_CLASS_ARRAY = new Class[0];
private static Map<MethodCall, Boolean> invalidMethods = new ConcurrentHashMap<>();
private static final Map<MethodCall, Boolean> invalidMethods = new ConcurrentHashMap<>();
private boolean devMode;
private Set<String> excludedClasses = emptySet();
private Set<Pattern> excludedPackageNamePatterns = emptySet();
private Set<String> excludedPackageNames = emptySet();
private Set<String> excludedPackageExemptClasses = emptySet();

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

@Inject
protected void setOgnlUtil(OgnlUtil ognlUtil) {
this.excludedClasses = ognlUtil.getExcludedClasses();
this.excludedPackageNamePatterns = ognlUtil.getExcludedPackageNamePatterns();
this.excludedPackageNames = ognlUtil.getExcludedPackageNames();
this.excludedPackageExemptClasses = ognlUtil.getExcludedPackageExemptClasses();
}

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 @@ -127,7 +157,7 @@ public Object getProperty(Map context, Object target, Object name) throws OgnlEx
return root.cutStack(index);
} else if (name instanceof String) {
if ("top".equals(name)) {
if (root.size() > 0) {
if (!root.isEmpty()) {
return root.get(0);
} else {
return null;
Expand Down Expand Up @@ -190,26 +220,23 @@ public Object callMethod(Map context, Object target, String name, Object[] objec
}

SortedSet<String> set = new TreeSet<>();
StringBuffer sb = new StringBuffer();
for (PropertyDescriptor pd : descriptors.values()) {

for (PropertyDescriptor pd : descriptors.values()) {
StringBuilder sb = new StringBuilder();
sb.append(pd.getName()).append(": ");

int padding = maxSize - pd.getName().length();
for (int i = 0; i < padding; i++) {
sb.append(" ");
}
sb.append(pd.getPropertyType().getName());
set.add(sb.toString());

sb = new StringBuffer();
}

sb = new StringBuffer();
for (Object aSet : set) {
String s = (String) aSet;
sb.append(s).append("\n");
StringBuilder sb = new StringBuilder();
for (String aSet : set) {
sb.append(aSet).append("\n");
}

return sb.toString();
} catch (IntrospectionException | OgnlException e) {
LOG.debug("Got exception in callMethod", e);
Expand Down Expand Up @@ -262,9 +289,28 @@ public Object callStaticMethod(Map transientVars, Class aClass, String s, Object
return null;
}

protected boolean isInaccessibleClass(Class<?> clazz) {
return isClassExcluded(clazz) || isPackageExcluded(clazz);
}

protected boolean isPackageExcluded(Class<?> clazz) {
return !excludedPackageExemptClasses.contains(clazz.getName()) && (isExcludedPackageNames(clazz) || isExcludedPackageNamePatterns(clazz));
}

protected boolean isExcludedPackageNames(Class<?> clazz) {
return isExcludedPackageNamesStatic(clazz, excludedPackageNames);
}

protected boolean isExcludedPackageNamePatterns(Class<?> clazz) {
return excludedPackageNamePatterns.stream().anyMatch(pattern -> pattern.matcher(toPackageName(clazz)).matches());
}

protected boolean isClassExcluded(Class<?> clazz) {
return excludedClasses.contains(clazz.getName());
}

public Class classForName(String className, Map context) throws ClassNotFoundException {
Object root = Ognl.getRoot(context);

try {
if (root instanceof CompoundRoot) {
if (className.startsWith("vs")) {
Expand All @@ -282,8 +328,12 @@ public Class classForName(String className, Map context) throws ClassNotFoundExc
} catch (Exception e) {
LOG.debug("Got exception when tried to get class for name [{}]", className, e);
}

return Thread.currentThread().getContextClassLoader().loadClass(className);
Class loadedClass = Thread.currentThread().getContextClassLoader().loadClass(className);
if (isInaccessibleClass(loadedClass)) {
LOG.warn("Blocked attempt to access class [{}] which is excluded", className);
return null;
}
return loadedClass;
}

private Class[] getArgTypes(Object[] args) {
Expand Down Expand Up @@ -321,9 +371,14 @@ public MethodCall(Class clazz, String name, Class[] args) {

@Override
public boolean equals(Object obj) {
MethodCall mc = (CompoundRootAccessor.MethodCall) obj;

return (mc.clazz.equals(clazz) && mc.name.equals(name) && Arrays.equals(mc.args, args));
if (this == obj) {
return true;
}
if (!(obj instanceof MethodCall)) {
return false;
}
MethodCall mc = (MethodCall) obj;
return mc.clazz.equals(clazz) && mc.name.equals(name) && Arrays.equals(mc.args, args);
}

@Override
Expand Down
Loading
Loading