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-5417 bump ognl version to fix security issue #915

Merged
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
@@ -0,0 +1,240 @@
/*
* 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 org.apache.struts2.ognl;

import com.opensymphony.xwork2.XWorkTestCase;
import com.opensymphony.xwork2.ognl.OgnlValueStack;
import com.opensymphony.xwork2.util.ValueStackFactory;
import ognl.OgnlRuntime;
import org.apache.struts2.StrutsConstants;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertNotEquals;

public class OgnlSetPossiblePropertyTest extends XWorkTestCase {
private OgnlValueStack vs;

public <T> T setUpClass(Class<T> holderClass) throws Exception {
Map<String, String> properties = new HashMap<>();
properties.put(StrutsConstants.STRUTS_EXCLUDED_CLASSES, holderClass.getName() + "$ExcludedField");
loadButSet(properties);
vs = (OgnlValueStack) container.getInstance(ValueStackFactory.class).createValueStack();

T nonExcludedHolder = holderClass.getDeclaredConstructor().newInstance();
vs.push(nonExcludedHolder);

return nonExcludedHolder;
}

public void testSetFieldValueDontAssignWhenHolderClassAndFieldClassHaveOnlyPublicFields() throws Exception {
/* Case: to test setFieldValue without having set method
*
* NonExcludedHolder class
* - field: public
* ExcludeField class
* - field: public
*/
HolderWithPublicField holder = setUpClass(HolderWithPublicField.class);
vs.setValue("excludedField.excludedFieldString", "EXPLOITED");

assertNotEquals("EXPLOITED", holder.excludedField.excludedFieldString);
}

public void testSetMethodValueDontAssignWhenHolderAndFieldClassWithPublicMethodsAndPrivateFields() throws Exception {
/* Case: to test setMethodValue, so to make fields as private
*
* NonExcludedHolder class
* - field: private
* - method: public
* ExcludeField class
* - field: private
* - method: public
*/
HolderWithPublicMethod holder = setUpClass(HolderWithPublicMethod.class);
vs.setValue("excludedField.excludedFieldString", "EXPLOITED");

assertNotEquals("EXPLOITED", holder.excludedField.excludedFieldString);
}

public void testSetFieldValueDontAssignWhenHolderClassWithGetMethodAndFieldClassWithPublicField() throws Exception {
/* Case: to test setFieldValue when holder get method is public and field class set method is private so fallback to set field
*
* NonExcludedHolder class
* - field: private
* - method: public
* ExcludeField class
* - field: public
* - method: private
*/
HolderWhoseFieldWithPrivateMethod holder = setUpClass(HolderWhoseFieldWithPrivateMethod.class);
vs.setValue("excludedField.excludedFieldString", "EXPLOITED");

assertNotEquals("EXPLOITED", holder.excludedField.excludedFieldString);
}

public void testSetMethodValueDontAssignWhenHolderClassWithGetMethodAndFieldClassWithPublicMethod() throws Exception {
/* Case: to test setMethodValue when holder get method is public and field class field is private so only call to set method
*
* NonExcludedHolder class
* - field: private
* - method: public
* ExcludeField class
* - field: private
* - method: public
*/
HolderWhoseFieldWithPublicMethod holder = setUpClass(HolderWhoseFieldWithPublicMethod.class);
vs.setValue("excludedField.excludedFieldString", "EXPLOITED");

assertNotEquals("EXPLOITED", holder.excludedField.excludedFieldString);
}

public void testWriteMethodValueDontAssignWhenWriteMethodIsNotAccessible() throws Exception {
/* Case: to test invoke method from getWriteMethod when holder get method is public and field class field / set method is private so fallback to write method
*
* NonExcludedHolder class
* - field: private
* - method: public
* ExcludeField class
* - field: private
* - set method: private
* - write method: public
*/
HolderWhoseFieldWithPublicWriteMethod holder = setUpClass(HolderWhoseFieldWithPublicWriteMethod.class);
Method writeMethod = OgnlRuntime.getWriteMethod(HolderWhoseFieldWithPublicWriteMethod.ExcludedField.class, "excludedFieldString");
vs.setValue("excludedField.excludedFieldString", "EXPLOITED");

assertEquals("setexcludedfieldstring", writeMethod.getName());
assertNotEquals("EXPLOITED", holder.excludedField.excludedFieldString);
}

public void testWriteMethodValueDontAssignWhenPublicSetterDifferentFieldName() throws Exception {
/* Case: to test invoke method from getWriteMethod when holder get method is public and field class field / set method is of different name
*
* NonExcludedHolder class
* - field: private
* - method: public
* ExcludeField class
* - field: private
* - set method: public (but not matching with field name)
*/
HolderWhoseFieldWithPublicSetterDifferentFieldName holder = setUpClass(HolderWhoseFieldWithPublicSetterDifferentFieldName.class);
vs.setValue("excludedField.excludedFieldString", "EXPLOITED");

assertNotEquals("EXPLOITED", holder.excludedField.excludedFieldStringInternal);
}


public static class HolderWithPublicField {
Copy link
Member

@kusalk kusalk Apr 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lukaszlenart This is a PoC of the original public field bug I reported

public ExcludedField excludedField = new ExcludedField();

public static class ExcludedField {
public String excludedFieldString = "defaultValue";
}
}

public static class HolderWithPublicMethod {
private ExcludedField excludedField = new ExcludedField();

public ExcludedField getExcludedField() {
return excludedField;
}

public static class ExcludedField {
private String excludedFieldString = "defaultValue";

public void setExcludedFieldString(String value) {
this.excludedFieldString = value;
}
}
}

public static class HolderWhoseFieldWithPrivateMethod {
private ExcludedField excludedField = new ExcludedField();


public ExcludedField getExcludedField() {
return excludedField;
}

public static class ExcludedField {
public String excludedFieldString = "defaultValue";

private void setExcludedFieldString(String value) {
this.excludedFieldString = value;
}
}
}

public static class HolderWhoseFieldWithPublicMethod {
private ExcludedField excludedField = new ExcludedField();


public ExcludedField getExcludedField() {
return excludedField;
}

public static class ExcludedField {
public String excludedFieldString = "defaultValue";

private void setExcludedFieldString(String value) {
this.excludedFieldString = value;
}
}
}

public static class HolderWhoseFieldWithPublicWriteMethod {
private ExcludedField excludedField = new ExcludedField();


public ExcludedField getExcludedField() {
return excludedField;
}

public static class ExcludedField {
private String excludedFieldString = "defaultValue";

private void setExcludedFieldString(String value) {
this.excludedFieldString = value;
}

public void setexcludedfieldstring(String value) {
this.excludedFieldString = value;
}
}
}

public static class HolderWhoseFieldWithPublicSetterDifferentFieldName {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lukaszlenart This is a PoC of the new public setter bug discovered by @jefferyxhy

private ExcludedField excludedField = new ExcludedField();

public ExcludedField getExcludedField() {
return excludedField;
}

public static class ExcludedField {
private String excludedFieldStringInternal = "defaultValue";

public void setExcludedFieldString(String value) {
this.excludedFieldStringInternal = value;
}
}
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
<asm.version>9.6</asm.version>
<jackson.version>2.16.1</jackson.version>
<log4j2.version>2.23.1</log4j2.version>
<ognl.version>3.3.4</ognl.version>
<ognl.version>3.3.5</ognl.version>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lukaszlenart updated with 3.3.5. Thanks

<slf4j.version>2.0.12</slf4j.version>
<spring.platformVersion>5.3.31</spring.platformVersion>
<tiles.version>3.0.8</tiles.version>
Expand Down