/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/VulnerabilityTest.java

https://github.com/apache/struts-sandbox · Java · 100 lines · 61 code · 15 blank · 24 comment · 0 complexity · c46cc0b6b4b73f6f0541d4cfe78c86b2 MD5 · raw file

  1. /*
  2. * $Id$
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one
  5. * or more contributor license agreements. See the NOTICE file
  6. * distributed with this work for additional information
  7. * regarding copyright ownership. The ASF licenses this file
  8. * to you under the Apache License, Version 2.0 (the
  9. * "License"); you may not use this file except in compliance
  10. * with the License. You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing,
  15. * software distributed under the License is distributed on an
  16. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17. * KIND, either express or implied. See the License for the
  18. * specific language governing permissions and limitations
  19. * under the License.
  20. */
  21. package org.apache.struts2.uel;
  22. import com.opensymphony.xwork2.ActionContext;
  23. import com.opensymphony.xwork2.util.CompoundRoot;
  24. import com.opensymphony.xwork2.util.ValueStack;
  25. import com.opensymphony.xwork2.util.reflection.ReflectionContextState;
  26. import org.apache.struts2.StrutsTestCase;
  27. import javax.servlet.ServletContextEvent;
  28. import java.util.HashMap;
  29. import java.util.Map;
  30. public class VulnerabilityTest extends StrutsTestCase {
  31. public void testMethodsAreNotInvokedUnlessDenyMethodExecutionIsTrue() {
  32. CompoundRoot root = new CompoundRoot();
  33. ValueStack stack = new UELValueStack(container);
  34. TestObject obj = new TestObject();
  35. root.push(obj);
  36. Map contextMap = stack.getContext();
  37. ReflectionContextState.setCreatingNullObjects(contextMap, true);
  38. ReflectionContextState.setDenyMethodExecution(contextMap, true);
  39. ReflectionContextState.setReportingConversionErrors(contextMap, true);
  40. //simple
  41. stack.findValue("top.invoke()");
  42. assertFalse(obj.wasInvoked());
  43. //nested
  44. TestObject nested = new TestObject();
  45. obj.setInner(nested);
  46. stack.findValue("top.inner.invoke()");
  47. assertFalse(nested.wasInvoked());
  48. }
  49. public void testParametersDoesNotAffectSession() throws Exception {
  50. HashMap<String, Object> session = new HashMap<String, Object>();
  51. ValueStack stack = ActionContext.getContext().getValueStack();
  52. stack.getContext().put("session", session);
  53. assertSame(session, stack.findValue("#session"));
  54. //make sure that values can tb set in session
  55. stack.setValue("#session['clean']", "clean");
  56. assertEquals("clean", stack.findValue("#session['clean']"));
  57. Map<String, Object> params = new HashMap<String, Object>();
  58. params.put("bar", "123");
  59. params.put("#session.foo", "Foo");
  60. params.put("\u0023session[\'user\']", "0wn3d");
  61. params.put("\\u0023session[\'user\']", "0wn3d");
  62. params.put("\u0023session.user2", "0wn3d");
  63. params.put("\\u0023session.user2", "0wn3d");
  64. params.put("('\u0023'%20%2b%20'session[\'user3\']')(unused)", "0wn3d");
  65. params.put("('\\u0023' + 'session[\\'user4\\']')(unused)", "0wn3d");
  66. params.put("('\u0023'%2b'session[\'user5\']')(unused)", "0wn3d");
  67. params.put("('\\u0023'%2b'session[\'user5\']')(unused)", "0wn3d");
  68. request.setParameters(params);
  69. executeAction("/test/test.action");
  70. assertEquals(123, findValueAfterExecute("top.bar"));
  71. assertNull(session.get("foo"));
  72. assertNull(session.get("user"));
  73. assertNull(session.get("user2"));
  74. assertNull(session.get("user3"));
  75. assertNull(session.get("user4"));
  76. assertNull(session.get("user5"));
  77. }
  78. @Override
  79. protected void setUp() throws Exception {
  80. super.setUp();
  81. //simulate start up
  82. UELServletContextListener listener = new UELServletContextListener();
  83. listener.contextInitialized(new ServletContextEvent(servletContext));
  84. }
  85. }