-
Notifications
You must be signed in to change notification settings - Fork 811
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
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
eca0666
WW-5352 Introduce StrutsParameter annotation
kusalk ad576f0
WW-5352 Introduce ThreadAllowlist bean
kusalk 4255da3
WW-5352 First draft implementation
kusalk bf3f407
WW-5352 Ensure allowlist is cleared if in unexpected state
kusalk 4c5f2b0
WW-5352 Add full unit test coverage
kusalk e9154b9
Merge branch 'master' into WW-5352-parameter-annotation-3
kusalk 5d79301
WW-5352 Fix missing curved bracket
kusalk 4c60f39
WW-5352 Enable annotations for showcase
kusalk b2c7542
WW-5352 Dispatcher should up thread allowlist
kusalk a57c288
WW-5352 Reinstate manual allowlist for generic types
kusalk 0a71e2c
WW-5352 Implement auto-allowlisting for Iterator component
kusalk 770d311
WW-5352 Mild optimisation
kusalk 6df8004
WW-5352 Auto allowlist parameterized types!
kusalk f106b20
WW-5352 Map-like type support
kusalk bf7737f
WW-5352 Add unit test coverage for generics
kusalk 56d8361
WW-5352 Implement transition mode
kusalk 49b9c0c
WW-5352 Ensure superclasses and interfaces allowlisted
kusalk 728d695
WW-5352 Add debug logging for parameter rejections
kusalk b506169
WW-5352 Acceptance test coverage
kusalk 71d77df
WW-5352 Normalise parameter name
kusalk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
apps/showcase/src/main/java/org/apache/struts2/showcase/action/ParamsAnnotationAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 togetKusal()
, 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! 😂There was a problem hiding this comment.
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.