-
Notifications
You must be signed in to change notification settings - Fork 816
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-5429 Log parameter annotation issues at ERROR level when in DevMode #969
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,7 @@ private ErrorMessageBuilder() { | |
} | ||
|
||
public ErrorMessageBuilder errorSettingExpressionWithValue(String expr, Object value) { | ||
appenExpression(expr); | ||
appendExpression(expr); | ||
if (value instanceof Object[]) { | ||
appendValueAsArray((Object[]) value, message); | ||
} else { | ||
|
@@ -42,7 +42,7 @@ public ErrorMessageBuilder errorSettingExpressionWithValue(String expr, Object v | |
return this; | ||
} | ||
|
||
private void appenExpression(String expr) { | ||
private void appendExpression(String expr) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed typo |
||
message.append("Error setting expression '"); | ||
message.append(expr); | ||
message.append("' with value "); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* 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.util; | ||
|
||
import com.opensymphony.xwork2.TextProvider; | ||
import com.opensymphony.xwork2.interceptor.ValidationAware; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
/** | ||
* @since 6.5.0 | ||
*/ | ||
public final class DebugUtils { | ||
|
||
public static void notifyDeveloperOfError(Logger log, Object action, String message) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extracted this method out of |
||
if (action instanceof TextProvider) { | ||
TextProvider tp = (TextProvider) action; | ||
message = tp.getText("devmode.notification", "Developer Notification:\n{0}", new String[]{message}); | ||
} | ||
log.error(message); | ||
if (action instanceof ValidationAware) { | ||
ValidationAware validationAware = (ValidationAware) action; | ||
validationAware.addActionError(message); | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,15 +116,17 @@ public void testInsecureParameters() throws Exception { | |
pi.setParameters(action, vs, HttpParameters.create(params).build()); | ||
|
||
// then | ||
assertEquals(3, action.getActionMessages().size()); | ||
assertEquals(3, action.getActionErrors().size()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using Action errors instead of Action messages to communicate developer errors (only impacts DevMode) |
||
|
||
String msg1 = action.getActionMessage(0); | ||
String msg2 = action.getActionMessage(1); | ||
String msg3 = action.getActionMessage(2); | ||
List<String> actionErrors = new ArrayList<>(action.getActionErrors()); | ||
|
||
assertEquals("Error setting expression 'expression' with value '#f=#_memberAccess.getClass().getDeclaredField('allowStaticMethodAccess'),#f.setAccessible(true),#f.set(#_memberAccess,true),#req=@org.apache.struts2.ServletActionContext@getRequest(),#resp=@org.apache.struts2.ServletActionContext@getResponse().getWriter(),#resp.println(#req.getRealPath('/')),#resp.close()'", msg1); | ||
assertEquals("Error setting expression 'name' with value '(#context[\"xwork.MethodAccessor.denyMethodExecution\"]= new java.lang.Boolean(false), #_memberAccess[\"allowStaticMethodAccess\"]= new java.lang.Boolean(true), @java.lang.Runtime@getRuntime().exec('mkdir /tmp/PWNAGE'))(meh)'", msg2); | ||
assertEquals("Error setting expression 'top['name'](0)' with value 'true'", msg3); | ||
String msg1 = actionErrors.get(0); | ||
String msg2 = actionErrors.get(1); | ||
String msg3 = actionErrors.get(2); | ||
|
||
assertEquals("Unexpected Exception caught setting 'expression' on 'class org.apache.struts2.interceptor.parameter.ValidateAction: Error setting expression 'expression' with value '#f=#_memberAccess.getClass().getDeclaredField('allowStaticMethodAccess'),#f.setAccessible(true),#f.set(#_memberAccess,true),#req=@org.apache.struts2.ServletActionContext@getRequest(),#resp=@org.apache.struts2.ServletActionContext@getResponse().getWriter(),#resp.println(#req.getRealPath('/')),#resp.close()'", msg1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These messages now include both the context message as well as the exception message. Whilst they are very similar in this test example, it's not guaranteed to be the case |
||
assertEquals("Unexpected Exception caught setting 'name' on 'class org.apache.struts2.interceptor.parameter.ValidateAction: Error setting expression 'name' with value '(#context[\"xwork.MethodAccessor.denyMethodExecution\"]= new java.lang.Boolean(false), #_memberAccess[\"allowStaticMethodAccess\"]= new java.lang.Boolean(true), @java.lang.Runtime@getRuntime().exec('mkdir /tmp/PWNAGE'))(meh)'", msg2); | ||
assertEquals("Unexpected Exception caught setting 'top['name'](0)' on 'class org.apache.struts2.interceptor.parameter.ValidateAction: Error setting expression 'top['name'](0)' with value 'true'", msg3); | ||
assertNull(action.getName()); | ||
} | ||
|
||
|
@@ -201,15 +203,16 @@ protected boolean isExcluded(String paramName) { | |
pi.setParameters(action, vs, HttpParameters.create(params).build()); | ||
|
||
// then | ||
assertEquals(3, action.getActionMessages().size()); | ||
assertEquals(3, action.getActionErrors().size()); | ||
|
||
String msg1 = action.getActionMessage(0); | ||
String msg2 = action.getActionMessage(1); | ||
String msg3 = action.getActionMessage(2); | ||
List<String> actionErrors = new ArrayList<>(action.getActionErrors()); | ||
String msg1 = actionErrors.get(0); | ||
String msg2 = actionErrors.get(1); | ||
String msg3 = actionErrors.get(2); | ||
|
||
assertEquals("Error setting expression 'class.classLoader.defaultAssertionStatus' with value 'true'", msg1); | ||
assertEquals("Error setting expression 'class.classLoader.jarPath' with value 'bad'", msg2); | ||
assertEquals("Error setting expression 'model.class.classLoader.jarPath' with value 'very bad'", msg3); | ||
assertEquals("Unexpected Exception caught setting 'class.classLoader.defaultAssertionStatus' on 'class org.apache.struts2.interceptor.parameter.ValidateAction: Error setting expression 'class.classLoader.defaultAssertionStatus' with value 'true'", msg1); | ||
assertEquals("Unexpected Exception caught setting 'class.classLoader.jarPath' on 'class org.apache.struts2.interceptor.parameter.ValidateAction: Error setting expression 'class.classLoader.jarPath' with value 'bad'", msg2); | ||
assertEquals("Unexpected Exception caught setting 'model.class.classLoader.jarPath' on 'class org.apache.struts2.interceptor.parameter.ValidateAction: Error setting expression 'model.class.classLoader.jarPath' with value 'very bad'", msg3); | ||
|
||
assertFalse(excluded.get(pollution1)); | ||
assertFalse(excluded.get(pollution2)); | ||
|
@@ -582,8 +585,8 @@ public void testNonexistentParametersGetLoggedInDevMode() throws Exception { | |
container.inject(config.getInterceptors().get(0).getInterceptor()); | ||
ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.PARAM_INTERCEPTOR_ACTION_NAME, null, extraContext.getContextMap()); | ||
proxy.execute(); | ||
final String actionMessage = "" + ((SimpleAction) proxy.getAction()).getActionMessages().toArray()[0]; | ||
assertTrue(actionMessage.contains("Error setting expression 'not_a_property' with value 'There is no action property named like this'")); | ||
final String actionError = "" + ((SimpleAction) proxy.getAction()).getActionErrors().toArray()[0]; | ||
assertTrue(actionError.contains("Error setting expression 'not_a_property' with value 'There is no action property named like this'")); | ||
} | ||
|
||
public void testNonexistentParametersAreIgnoredInProductionMode() throws Exception { | ||
|
@@ -1014,59 +1017,65 @@ protected void setUp() throws Exception { | |
class ValidateAction implements ValidationAware { | ||
|
||
private final List<String> messages = new LinkedList<>(); | ||
private final List<String> errors = new LinkedList<>(); | ||
private String name; | ||
|
||
@Override | ||
public void setActionErrors(Collection<String> errorMessages) { | ||
} | ||
|
||
@Override | ||
public Collection<String> getActionErrors() { | ||
return null; | ||
return errors; | ||
} | ||
|
||
@Override | ||
public void setActionMessages(Collection<String> messages) { | ||
} | ||
|
||
@Override | ||
public Collection<String> getActionMessages() { | ||
return messages; | ||
} | ||
|
||
@Override | ||
public void setFieldErrors(Map<String, List<String>> errorMap) { | ||
} | ||
|
||
@Override | ||
public Map<String, List<String>> getFieldErrors() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public void addActionError(String anErrorMessage) { | ||
errors.add(anErrorMessage); | ||
} | ||
|
||
@Override | ||
public void addActionMessage(String aMessage) { | ||
messages.add(aMessage); | ||
} | ||
|
||
@Override | ||
public void addFieldError(String fieldName, String errorMessage) { | ||
} | ||
|
||
@Override | ||
public boolean hasActionErrors() { | ||
return false; | ||
return !errors.isEmpty(); | ||
} | ||
|
||
@Override | ||
public boolean hasActionMessages() { | ||
return !messages.isEmpty(); | ||
} | ||
|
||
public boolean hasErrors() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean hasFieldErrors() { | ||
return false; | ||
} | ||
|
||
public String getActionMessage(int index) { | ||
return messages.get(index); | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
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.
Added default implementation which matches the JavaDoc, makes implementing this class simpler