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-5352 Introducing the StrutsParameter annotation #832

Merged
merged 20 commits into from
Jan 26, 2024
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
Expand Up @@ -24,9 +24,15 @@
import com.opensymphony.xwork2.Validateable;
import com.opensymphony.xwork2.util.ValueStack;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.parameter.StrutsParameter;

import java.io.File;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
*/
Expand Down Expand Up @@ -89,6 +95,7 @@ public List getLeftSideCartoonCharacters() {
return leftSideCartoonCharacters;
}

@StrutsParameter
public void setLeftSideCartoonCharacters(List leftSideCartoonCharacters) {
this.leftSideCartoonCharacters = leftSideCartoonCharacters;
}
Expand All @@ -98,6 +105,7 @@ public List getRightSideCartoonCharacters() {
return rightSideCartoonCharacters;
}

@StrutsParameter
public void setRightSideCartoonCharacters(List rightSideCartoonCharacters) {
this.rightSideCartoonCharacters = rightSideCartoonCharacters;
}
Expand All @@ -107,6 +115,7 @@ public String getFavouriteVehicalType() {
return favouriteVehicalType;
}

@StrutsParameter
public void setFavouriteVehicalType(String favouriteVehicalType) {
this.favouriteVehicalType = favouriteVehicalType;
}
Expand All @@ -115,6 +124,7 @@ public String getFavouriteVehicalSpecific() {
return favouriteVehicalSpecific;
}

@StrutsParameter
public void setFavouriteVehicalSpecific(String favouriteVehicalSpecific) {
this.favouriteVehicalSpecific = favouriteVehicalSpecific;
}
Expand Down Expand Up @@ -145,6 +155,7 @@ public String getName() {
return name;
}

@StrutsParameter
public void setName(String name) {
this.name = name;
}
Expand All @@ -153,6 +164,7 @@ public Date getBirthday() {
return birthday;
}

@StrutsParameter
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
Expand All @@ -161,6 +173,7 @@ public String getBio() {
return bio;
}

@StrutsParameter
public void setBio(String bio) {
this.bio = bio;
}
Expand All @@ -169,6 +182,7 @@ public String getFavouriteColor() {
return favouriteColor;
}

@StrutsParameter
public void setFavouriteColor(String favoriteColor) {
this.favouriteColor = favoriteColor;
}
Expand All @@ -177,6 +191,7 @@ public List getFriends() {
return friends;
}

@StrutsParameter
public void setFriends(List friends) {
this.friends = friends;
}
Expand All @@ -193,6 +208,7 @@ public boolean isLegalAge() {
return legalAge;
}

@StrutsParameter
public void setLegalAge(boolean legalAge) {
this.legalAge = legalAge;
}
Expand All @@ -201,6 +217,7 @@ public String getState() {
return state;
}

@StrutsParameter
public void setState(String state) {
this.state = state;
}
Expand All @@ -209,6 +226,7 @@ public String getRegion() {
return region;
}

@StrutsParameter
public void setRegion(String region) {
this.region = region;
}
Expand All @@ -229,6 +247,7 @@ public void setPictureFileName(String pictureFileName) {
this.pictureFileName = pictureFileName;
}

@StrutsParameter
public void setFavouriteLanguage(String favouriteLanguage) {
this.favouriteLanguage = favouriteLanguage;
}
Expand All @@ -237,7 +256,7 @@ public String getFavouriteLanguage() {
return favouriteLanguage;
}


@StrutsParameter
public void setThoughts(String thoughts) {
this.thoughts = thoughts;
}
Expand All @@ -250,6 +269,7 @@ public Date getWakeup() {
return wakeup;
}

@StrutsParameter
public void setWakeup(Date wakeup) {
this.wakeup = wakeup;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* 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.showcase.action;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.parameter.StrutsParameter;
import org.apache.struts2.showcase.model.MyDto;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;

/**
* This class supports {@link com.atlassian.confluence.stateless.webdriver.selenium3.security.StrutsParametersTest}
* which prevents critical security regressions. Do NOT modify without understanding the motivation behind the tests and
* the implications of any changes.
*/
public class ParamsAnnotationAction extends ActionSupport {

@StrutsParameter
Copy link
Member

Choose a reason for hiding this comment

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

Oh nice, I was going to ask about that, so this mechanism supports injecting incoming values directly into fields without a need to define setter, is that right? What about getter to fetch the value, is it still needed?

Copy link
Member Author

Choose a reason for hiding this comment

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

So this PR doesn't add any new injection capabilities - OGNL has always been able to set public fields without setters. It has also never required a getter to set a value, only a setter.

A getter is only used when you want to access a nested setter (or public field). For example, the parameter name kusal.lukasz translates to getKusal(), then on the returned object, setLukasz(_).

What this PR does is use annotations to clearly mark the code path OGNL will take to perform parameter injection. The ParametersInterceptor is predicting the field or method OGNL will invoke, and if it is not annotated, it will be stripped out of the acceptable parameters map before OGNL attempts injection. In this way, when inspecting the source of an Action class, it is perfectly clear which methods and fields are exposed to the internet. We are essentially making OGNL more intuitive for developers - because right now, attackers seem to understand OGNL better than the developers using it! 😂

Copy link
Member

Choose a reason for hiding this comment

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

Ah.. I missed the public scope, I was hopping on some magic :D Anyway, thanks for the detailed explanation.

public String varToPrint;

public String publicField = "no";

@StrutsParameter
public String publicFieldAnnotated = "no";

private String privateField = "no";

public int[] publicArray = new int[]{0};

@StrutsParameter(depth = 1)
public int[] publicArrayAnnotated = new int[]{0};

public List<String> publicList = new ArrayList<>(singletonList("no"));

@StrutsParameter(depth = 1)
public List<String> publicListAnnotated = new ArrayList<>(singletonList("no"));

private List<String> privateList = new ArrayList<>(singletonList("no"));

public Map<String, String> publicMap = new HashMap<>(singletonMap("key", "no"));

@StrutsParameter(depth = 1)
public Map<String, String> publicMapAnnotated = new HashMap<>(singletonMap("key", "no"));

public MyDto publicMyDto = new MyDto();

@StrutsParameter(depth = 2)
public MyDto publicMyDtoAnnotated = new MyDto();

@StrutsParameter(depth = 1)
public MyDto publicMyDtoAnnotatedDepthOne = new MyDto();

private MyDto privateMyDto = new MyDto();

public void setPrivateFieldMethod(String privateField) {
this.privateField = privateField;
}

@StrutsParameter
public void setPrivateFieldMethodAnnotated(String privateField) {
this.privateField = privateField;
}

public List<String> getPrivateListMethod() {
return privateList;
}

@StrutsParameter(depth = 1)
public List<String> getPrivateListMethodAnnotated() {
return privateList;
}

public MyDto getUnsafeMethodMyDto() {
return privateMyDto;
}

@StrutsParameter(depth = 2)
public MyDto getSafeMethodMyDto() {
return privateMyDto;
}

@StrutsParameter(depth = 1)
public MyDto getSafeMethodMyDtoDepthOne() {
return privateMyDto;
}

public String renderVarToPrint() throws ReflectiveOperationException {
if (varToPrint == null) {
return "null";
}
Field field = this.getClass().getDeclaredField(varToPrint);
field.setAccessible(true);
try {
return String.format("%s{%s}", varToPrint,
field.getType().isArray() ? stringifyArray(field.get(this)) : field.get(this));
} finally {
field.setAccessible(false);
}
}

private String stringifyArray(Object array) {
switch (array.getClass().getComponentType().getName()) {
case "int":
return Arrays.toString((int[]) array);
default:
return "TODO";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.opensymphony.xwork2.Preparable;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.interceptor.parameter.StrutsParameter;
import org.apache.struts2.showcase.dao.Dao;
import org.apache.struts2.showcase.dao.SkillDao;
import org.apache.struts2.showcase.model.Skill;
Expand Down Expand Up @@ -71,6 +72,7 @@ protected Dao getDao() {
return skillDao;
}

@StrutsParameter(depth = 1)
public Skill getCurrentSkill() {
return currentSkill;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.struts2.showcase.async;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.parameter.StrutsParameter;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -34,10 +35,12 @@ public class ChatRoomAction extends ActionSupport {

private static final List<String> messages = new ArrayList<>();

@StrutsParameter
public void setMessage(String message) {
this.message = message;
}

@StrutsParameter
public void setLastIndex(Integer lastIndex) {
this.lastIndex = lastIndex;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package org.apache.struts2.showcase.conversion;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.parameter.StrutsParameter;

import java.util.LinkedHashSet;
import java.util.Set;
Expand All @@ -30,7 +31,7 @@
*/
public class AddressAction extends ActionSupport {

private Set<Address> addresses = new LinkedHashSet<Address>();
private Set<Address> addresses = new LinkedHashSet<>();

public String input() throws Exception {
return SUCCESS;
Expand All @@ -41,6 +42,7 @@ public String submit() throws Exception {
return SUCCESS;
}

@StrutsParameter(depth = 2)
public Set<Address> getAddresses() {
return addresses;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package org.apache.struts2.showcase.conversion;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.parameter.StrutsParameter;

import java.util.Arrays;
import java.util.LinkedList;
Expand All @@ -47,6 +48,7 @@ public List<OperationsEnum> getSelectedOperations() {
return this.selectedOperations;
}

@StrutsParameter
public void setSelectedOperations(List<OperationsEnum> selectedOperations) {
this.selectedOperations = selectedOperations;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package org.apache.struts2.showcase.conversion;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.parameter.StrutsParameter;

import java.util.List;

Expand All @@ -36,6 +37,7 @@ public String submit() throws Exception {
return SUCCESS;
}

@StrutsParameter(depth = 2)
public List<Person> getPersons() {
return persons;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.opensymphony.xwork2.Action;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.parameter.StrutsParameter;

import java.io.InputStream;

Expand All @@ -38,6 +39,7 @@ public String execute() throws Exception {
return SUCCESS;
}

@StrutsParameter
public void setInputPath(String value) {
inputPath = sanitizeInputPath(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.action.UploadedFilesAware;
import org.apache.struts2.dispatcher.multipart.UploadedFile;
import org.apache.struts2.interceptor.parameter.StrutsParameter;

import java.util.List;

Expand Down Expand Up @@ -65,6 +66,7 @@ public String getCaption() {
return caption;
}

@StrutsParameter
public void setCaption(String caption) {
this.caption = caption;
}
Expand Down
Loading