From 3db4c89c6172f1a4acacb66f92fc90feeda20336 Mon Sep 17 00:00:00 2001 From: lovesh-ap Date: Fri, 5 Apr 2024 16:23:33 +0530 Subject: [PATCH] NR-256007 - Fix NoClassDefFoundError raised in javax packages --- .../javax/naming/Context_Instrumentation.java | 1 - .../jndi => javax/naming}/JNDIUtils.java | 2 +- .../javax/jndi/ContextRmiTest.java | 2 +- .../javax/jndi/ContextTest.java | 2 +- .../directory/DirContext_Instrumentation.java | 1 - .../naming/directory}/LDAPUtils.java | 2 +- .../javax/ldap/DirContextTest.java | 2 +- .../xpath/javax/XPATHUtils.java | 10 --------- .../xpath/internal/XPath_Instrumentation.java | 15 +++++++------ .../xml/xpath/XPath_Instrumentation.java | 21 +++++++++---------- .../xpath/javax/XPathTest.java | 19 +++++++++-------- 11 files changed, 32 insertions(+), 45 deletions(-) rename instrumentation-security/javax-jndi/src/main/java/{com/newrelic/agent/security/instrumentation/javax/jndi => javax/naming}/JNDIUtils.java (97%) rename instrumentation-security/javax-ldap/src/main/java/{com/newrelic/agent/security/instrumentation/javax/ldap => javax/naming/directory}/LDAPUtils.java (78%) delete mode 100644 instrumentation-security/javax-xpath/src/main/java/com/newrelic/agent/security/instrumentation/xpath/javax/XPATHUtils.java diff --git a/instrumentation-security/javax-jndi/src/main/java/javax/naming/Context_Instrumentation.java b/instrumentation-security/javax-jndi/src/main/java/javax/naming/Context_Instrumentation.java index 7b011bdcb..507e695cd 100644 --- a/instrumentation-security/javax-jndi/src/main/java/javax/naming/Context_Instrumentation.java +++ b/instrumentation-security/javax-jndi/src/main/java/javax/naming/Context_Instrumentation.java @@ -10,7 +10,6 @@ import com.newrelic.api.agent.weaver.MatchType; import com.newrelic.api.agent.weaver.Weave; import com.newrelic.api.agent.weaver.Weaver; -import com.newrelic.agent.security.instrumentation.javax.jndi.JNDIUtils; import java.util.Enumeration; import java.util.List; diff --git a/instrumentation-security/javax-jndi/src/main/java/com/newrelic/agent/security/instrumentation/javax/jndi/JNDIUtils.java b/instrumentation-security/javax-jndi/src/main/java/javax/naming/JNDIUtils.java similarity index 97% rename from instrumentation-security/javax-jndi/src/main/java/com/newrelic/agent/security/instrumentation/javax/jndi/JNDIUtils.java rename to instrumentation-security/javax-jndi/src/main/java/javax/naming/JNDIUtils.java index a50035bfc..b4c4576ce 100644 --- a/instrumentation-security/javax-jndi/src/main/java/com/newrelic/agent/security/instrumentation/javax/jndi/JNDIUtils.java +++ b/instrumentation-security/javax-jndi/src/main/java/javax/naming/JNDIUtils.java @@ -1,4 +1,4 @@ -package com.newrelic.agent.security.instrumentation.javax.jndi; +package javax.naming; import com.newrelic.api.agent.security.NewRelicSecurity; import com.newrelic.api.agent.security.instrumentation.helpers.GenericHelper; diff --git a/instrumentation-security/javax-jndi/src/test/java/com/nr/agent/security/instrumentation/javax/jndi/ContextRmiTest.java b/instrumentation-security/javax-jndi/src/test/java/com/nr/agent/security/instrumentation/javax/jndi/ContextRmiTest.java index 6f0e71eae..a2d2a52ed 100644 --- a/instrumentation-security/javax-jndi/src/test/java/com/nr/agent/security/instrumentation/javax/jndi/ContextRmiTest.java +++ b/instrumentation-security/javax-jndi/src/test/java/com/nr/agent/security/instrumentation/javax/jndi/ContextRmiTest.java @@ -6,7 +6,7 @@ import com.newrelic.api.agent.security.schema.AbstractOperation; import com.newrelic.api.agent.security.schema.VulnerabilityCaseType; import com.newrelic.api.agent.security.schema.operation.SSRFOperation; -import com.newrelic.agent.security.instrumentation.javax.jndi.JNDIUtils; +import javax.naming.JNDIUtils; import org.junit.Assert; import org.junit.FixMethodOrder; import org.junit.Test; diff --git a/instrumentation-security/javax-jndi/src/test/java/com/nr/agent/security/instrumentation/javax/jndi/ContextTest.java b/instrumentation-security/javax-jndi/src/test/java/com/nr/agent/security/instrumentation/javax/jndi/ContextTest.java index b4295dc77..e21071234 100644 --- a/instrumentation-security/javax-jndi/src/test/java/com/nr/agent/security/instrumentation/javax/jndi/ContextTest.java +++ b/instrumentation-security/javax-jndi/src/test/java/com/nr/agent/security/instrumentation/javax/jndi/ContextTest.java @@ -6,7 +6,7 @@ import com.newrelic.api.agent.security.schema.AbstractOperation; import com.newrelic.api.agent.security.schema.VulnerabilityCaseType; import com.newrelic.api.agent.security.schema.operation.SSRFOperation; -import com.newrelic.agent.security.instrumentation.javax.jndi.JNDIUtils; +import javax.naming.JNDIUtils; import com.unboundid.ldap.sdk.LDAPException; import org.junit.Assert; import org.junit.BeforeClass; diff --git a/instrumentation-security/javax-ldap/src/main/java/javax/naming/directory/DirContext_Instrumentation.java b/instrumentation-security/javax-ldap/src/main/java/javax/naming/directory/DirContext_Instrumentation.java index 310909107..a2f263627 100644 --- a/instrumentation-security/javax-ldap/src/main/java/javax/naming/directory/DirContext_Instrumentation.java +++ b/instrumentation-security/javax-ldap/src/main/java/javax/naming/directory/DirContext_Instrumentation.java @@ -10,7 +10,6 @@ import com.newrelic.api.agent.weaver.MatchType; import com.newrelic.api.agent.weaver.Weave; import com.newrelic.api.agent.weaver.Weaver; -import com.newrelic.agent.security.instrumentation.javax.ldap.LDAPUtils; import javax.naming.Context; import javax.naming.Name; diff --git a/instrumentation-security/javax-ldap/src/main/java/com/newrelic/agent/security/instrumentation/javax/ldap/LDAPUtils.java b/instrumentation-security/javax-ldap/src/main/java/javax/naming/directory/LDAPUtils.java similarity index 78% rename from instrumentation-security/javax-ldap/src/main/java/com/newrelic/agent/security/instrumentation/javax/ldap/LDAPUtils.java rename to instrumentation-security/javax-ldap/src/main/java/javax/naming/directory/LDAPUtils.java index 749dabd50..1da7ed112 100644 --- a/instrumentation-security/javax-ldap/src/main/java/com/newrelic/agent/security/instrumentation/javax/ldap/LDAPUtils.java +++ b/instrumentation-security/javax-ldap/src/main/java/javax/naming/directory/LDAPUtils.java @@ -1,4 +1,4 @@ -package com.newrelic.agent.security.instrumentation.javax.ldap; +package javax.naming.directory; public class LDAPUtils { diff --git a/instrumentation-security/javax-ldap/src/test/java/com/nr/agent/security/instrumentation/javax/ldap/DirContextTest.java b/instrumentation-security/javax-ldap/src/test/java/com/nr/agent/security/instrumentation/javax/ldap/DirContextTest.java index 4d50e493c..e5e9ea75b 100644 --- a/instrumentation-security/javax-ldap/src/test/java/com/nr/agent/security/instrumentation/javax/ldap/DirContextTest.java +++ b/instrumentation-security/javax-ldap/src/test/java/com/nr/agent/security/instrumentation/javax/ldap/DirContextTest.java @@ -31,7 +31,7 @@ import java.util.List; @RunWith(SecurityInstrumentationTestRunner.class) -@InstrumentationTestConfig(includePrefixes = { "javax.naming", "com.newrelic.agent.security.instrumentation.javax.ldap.LDAPUtils" } ) +@InstrumentationTestConfig(includePrefixes = { "javax.naming", "javax.naming.directory.LDAPUtils" } ) @FixMethodOrder(MethodSorters.NAME_ASCENDING) //FIXME: after instrumentation works @Ignore diff --git a/instrumentation-security/javax-xpath/src/main/java/com/newrelic/agent/security/instrumentation/xpath/javax/XPATHUtils.java b/instrumentation-security/javax-xpath/src/main/java/com/newrelic/agent/security/instrumentation/xpath/javax/XPATHUtils.java deleted file mode 100644 index 7f2b1ed9b..000000000 --- a/instrumentation-security/javax-xpath/src/main/java/com/newrelic/agent/security/instrumentation/xpath/javax/XPATHUtils.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.newrelic.agent.security.instrumentation.xpath.javax; - -public class XPATHUtils { - - public static final String NR_SEC_CUSTOM_ATTRIB_NAME = "XPATH_OPERATION_LOCK_JAVAXPATH-"; - public static final String METHOD_EVALUATE = "evaluate"; - - public static final String METHOD_EXECUTE = "execute"; - public static final Object JAVAX_XPATH = "JAVAX-XPATH"; -} diff --git a/instrumentation-security/javax-xpath/src/main/java/com/sun/org/apache/xpath/internal/XPath_Instrumentation.java b/instrumentation-security/javax-xpath/src/main/java/com/sun/org/apache/xpath/internal/XPath_Instrumentation.java index 7b335a93f..777896065 100644 --- a/instrumentation-security/javax-xpath/src/main/java/com/sun/org/apache/xpath/internal/XPath_Instrumentation.java +++ b/instrumentation-security/javax-xpath/src/main/java/com/sun/org/apache/xpath/internal/XPath_Instrumentation.java @@ -10,7 +10,6 @@ import com.newrelic.api.agent.weaver.MatchType; import com.newrelic.api.agent.weaver.Weave; import com.newrelic.api.agent.weaver.Weaver; -import com.newrelic.agent.security.instrumentation.xpath.javax.XPATHUtils; import com.sun.org.apache.xml.internal.utils.PrefixResolver; import com.sun.org.apache.xpath.internal.objects.XObject; @@ -26,7 +25,7 @@ public XObject execute( boolean isLockAcquired = acquireLockIfPossible(); AbstractOperation operation = null; if(isLockAcquired) { - operation = preprocessSecurityHook(getPatternString(), XPATHUtils.METHOD_EXECUTE); + operation = preprocessSecurityHook(getPatternString(), "execute"); } XObject returnVal = null; @@ -49,7 +48,7 @@ public XObject execute( boolean isLockAcquired = acquireLockIfPossible(); AbstractOperation operation = null; if(isLockAcquired) { - operation = preprocessSecurityHook(getPatternString(), XPATHUtils.METHOD_EXECUTE); + operation = preprocessSecurityHook(getPatternString(), "execute"); } XObject returnVal = null; @@ -87,24 +86,24 @@ private AbstractOperation preprocessSecurityHook (String patternString, String m return xPathOperation; } catch (Throwable e) { if (e instanceof NewRelicSecurityException) { - NewRelicSecurity.getAgent().log(LogLevel.WARNING, String.format(GenericHelper.SECURITY_EXCEPTION_MESSAGE, XPATHUtils.JAVAX_XPATH, e.getMessage()), e, this.getClass().getName()); + NewRelicSecurity.getAgent().log(LogLevel.WARNING, String.format(GenericHelper.SECURITY_EXCEPTION_MESSAGE, "JAVAX-XPATH", e.getMessage()), e, this.getClass().getName()); throw e; } - NewRelicSecurity.getAgent().log(LogLevel.SEVERE, String.format(GenericHelper.REGISTER_OPERATION_EXCEPTION_MESSAGE, XPATHUtils.JAVAX_XPATH, e.getMessage()), e, this.getClass().getName()); - NewRelicSecurity.getAgent().reportIncident(LogLevel.SEVERE , String.format(GenericHelper.REGISTER_OPERATION_EXCEPTION_MESSAGE, XPATHUtils.JAVAX_XPATH, e.getMessage()), e, this.getClass().getName()); + NewRelicSecurity.getAgent().log(LogLevel.SEVERE, String.format(GenericHelper.REGISTER_OPERATION_EXCEPTION_MESSAGE, "JAVAX-XPATH", e.getMessage()), e, this.getClass().getName()); + NewRelicSecurity.getAgent().reportIncident(LogLevel.SEVERE , String.format(GenericHelper.REGISTER_OPERATION_EXCEPTION_MESSAGE, "JAVAX-XPATH", e.getMessage()), e, this.getClass().getName()); } return null; } private void releaseLock() { try { - GenericHelper.releaseLock(XPATHUtils.NR_SEC_CUSTOM_ATTRIB_NAME); + GenericHelper.releaseLock("XPATH_OPERATION_LOCK_JAVAXPATH-"); } catch (Throwable ignored) {} } private boolean acquireLockIfPossible() { try { - return GenericHelper.acquireLockIfPossible(XPATHUtils.NR_SEC_CUSTOM_ATTRIB_NAME); + return GenericHelper.acquireLockIfPossible("XPATH_OPERATION_LOCK_JAVAXPATH-"); } catch (Throwable ignored) {} return false; } diff --git a/instrumentation-security/javax-xpath/src/main/java/javax/xml/xpath/XPath_Instrumentation.java b/instrumentation-security/javax-xpath/src/main/java/javax/xml/xpath/XPath_Instrumentation.java index 04742b312..ce65bb97b 100644 --- a/instrumentation-security/javax-xpath/src/main/java/javax/xml/xpath/XPath_Instrumentation.java +++ b/instrumentation-security/javax-xpath/src/main/java/javax/xml/xpath/XPath_Instrumentation.java @@ -10,7 +10,6 @@ import com.newrelic.api.agent.weaver.MatchType; import com.newrelic.api.agent.weaver.Weave; import com.newrelic.api.agent.weaver.Weaver; -import com.newrelic.agent.security.instrumentation.xpath.javax.XPATHUtils; import org.xml.sax.InputSource; import javax.xml.namespace.QName; @@ -23,7 +22,7 @@ public String evaluate(String expression, InputSource source) boolean isLockAcquired = acquireLockIfPossible(); AbstractOperation operation = null; if(isLockAcquired) { - operation = preprocessSecurityHook(expression, XPATHUtils.METHOD_EVALUATE); + operation = preprocessSecurityHook(expression, "evaluate"); } String returnVal = null; @@ -46,7 +45,7 @@ public Object evaluate( boolean isLockAcquired = acquireLockIfPossible(); AbstractOperation operation = null; if(isLockAcquired) { - operation = preprocessSecurityHook(expression, XPATHUtils.METHOD_EVALUATE); + operation = preprocessSecurityHook(expression, "evaluate"); } Object returnVal = null; @@ -66,7 +65,7 @@ public String evaluate(String expression, Object item) boolean isLockAcquired = acquireLockIfPossible(); AbstractOperation operation = null; if(isLockAcquired) { - operation = preprocessSecurityHook(expression, XPATHUtils.METHOD_EVALUATE); + operation = preprocessSecurityHook(expression, "evaluate"); } String returnVal = null; @@ -86,7 +85,7 @@ public Object evaluate(String expression, Object item, QName returnType) boolean isLockAcquired = acquireLockIfPossible(); AbstractOperation operation = null; if(isLockAcquired) { - operation = preprocessSecurityHook(expression, XPATHUtils.METHOD_EVALUATE); + operation = preprocessSecurityHook(expression, "evaluate"); } Object returnVal = null; @@ -110,7 +109,7 @@ private void registerExitOperation(boolean isProcessingAllowed, AbstractOperatio } NewRelicSecurity.getAgent().registerExitEvent(operation); } catch (Throwable ignored){ - NewRelicSecurity.getAgent().log(LogLevel.FINEST, String.format(GenericHelper.EXIT_OPERATION_EXCEPTION_MESSAGE, XPATHUtils.JAVAX_XPATH, ignored.getMessage()), ignored, this.getClass().getName()); + NewRelicSecurity.getAgent().log(LogLevel.FINEST, String.format(GenericHelper.EXIT_OPERATION_EXCEPTION_MESSAGE, "JAVAX-XPATH", ignored.getMessage()), ignored, this.getClass().getName()); } } @@ -126,24 +125,24 @@ private AbstractOperation preprocessSecurityHook (String patternString, String m return xPathOperation; } catch (Throwable e) { if (e instanceof NewRelicSecurityException) { - NewRelicSecurity.getAgent().log(LogLevel.WARNING, String.format(GenericHelper.SECURITY_EXCEPTION_MESSAGE, XPATHUtils.JAVAX_XPATH, e.getMessage()), e, this.getClass().getName()); + NewRelicSecurity.getAgent().log(LogLevel.WARNING, String.format(GenericHelper.SECURITY_EXCEPTION_MESSAGE, "JAVAX-XPATH", e.getMessage()), e, this.getClass().getName()); throw e; } - NewRelicSecurity.getAgent().log(LogLevel.SEVERE, String.format(GenericHelper.REGISTER_OPERATION_EXCEPTION_MESSAGE, XPATHUtils.JAVAX_XPATH, e.getMessage()), e, this.getClass().getName()); - NewRelicSecurity.getAgent().reportIncident(LogLevel.SEVERE , String.format(GenericHelper.REGISTER_OPERATION_EXCEPTION_MESSAGE, XPATHUtils.JAVAX_XPATH, e.getMessage()), e, this.getClass().getName()); + NewRelicSecurity.getAgent().log(LogLevel.SEVERE, String.format(GenericHelper.REGISTER_OPERATION_EXCEPTION_MESSAGE, "JAVAX-XPATH", e.getMessage()), e, this.getClass().getName()); + NewRelicSecurity.getAgent().reportIncident(LogLevel.SEVERE , String.format(GenericHelper.REGISTER_OPERATION_EXCEPTION_MESSAGE, "JAVAX-XPATH", e.getMessage()), e, this.getClass().getName()); } return null; } private void releaseLock() { try { - GenericHelper.releaseLock(XPATHUtils.NR_SEC_CUSTOM_ATTRIB_NAME); + GenericHelper.releaseLock("XPATH_OPERATION_LOCK_JAVAXPATH-"); } catch (Throwable ignored) {} } private boolean acquireLockIfPossible() { try { - return GenericHelper.acquireLockIfPossible(XPATHUtils.NR_SEC_CUSTOM_ATTRIB_NAME); + return GenericHelper.acquireLockIfPossible("XPATH_OPERATION_LOCK_JAVAXPATH-"); } catch (Throwable ignored) {} return false; } diff --git a/instrumentation-security/javax-xpath/src/test/java/com/nr/agent/security/instrumentation/xpath/javax/XPathTest.java b/instrumentation-security/javax-xpath/src/test/java/com/nr/agent/security/instrumentation/xpath/javax/XPathTest.java index 37701dd21..7089a53a0 100644 --- a/instrumentation-security/javax-xpath/src/test/java/com/nr/agent/security/instrumentation/xpath/javax/XPathTest.java +++ b/instrumentation-security/javax-xpath/src/test/java/com/nr/agent/security/instrumentation/xpath/javax/XPathTest.java @@ -6,7 +6,6 @@ import com.newrelic.api.agent.security.schema.AbstractOperation; import com.newrelic.api.agent.security.schema.VulnerabilityCaseType; import com.newrelic.api.agent.security.schema.operation.XPathOperation; -import com.newrelic.agent.security.instrumentation.xpath.javax.XPATHUtils; import com.newrelic.security.test.marker.Java17IncompatibleTest; import org.junit.Assert; import org.junit.FixMethodOrder; @@ -33,6 +32,8 @@ public class XPathTest { private final String XML_DOC = "src/test/resources/Customer.xml"; private final String EXPRESSION = "/Customers/Customer"; + public static final String METHOD_EVALUATE = "evaluate"; + @Test public void testEvaluate() throws Exception { InputSource source = new InputSource(XML_DOC); @@ -46,7 +47,7 @@ public void testEvaluate() throws Exception { XPathOperation operation = (XPathOperation) operations.get(0); Assert.assertEquals("Invalid event category.", VulnerabilityCaseType.XPATH, operation.getCaseType()); - Assert.assertEquals("Invalid executed method name.", XPATHUtils.METHOD_EVALUATE, operation.getMethodName()); + Assert.assertEquals("Invalid executed method name.", METHOD_EVALUATE, operation.getMethodName()); Assert.assertEquals("Invalid expression", EXPRESSION, operation.getExpression()); } @@ -63,7 +64,7 @@ public void testEvaluate1() throws Exception { XPathOperation operation = (XPathOperation) operations.get(0); Assert.assertEquals("Invalid event category.", VulnerabilityCaseType.XPATH, operation.getCaseType()); - Assert.assertEquals("Invalid executed method name.", XPATHUtils.METHOD_EVALUATE, operation.getMethodName()); + Assert.assertEquals("Invalid executed method name.", METHOD_EVALUATE, operation.getMethodName()); Assert.assertEquals("Invalid expression", EXPRESSION, operation.getExpression()); } @@ -80,7 +81,7 @@ public void testEvaluate2() throws Exception { XPathOperation operation = (XPathOperation) operations.get(0); Assert.assertEquals("Invalid event category.", VulnerabilityCaseType.XPATH, operation.getCaseType()); - Assert.assertEquals("Invalid executed method name.", XPATHUtils.METHOD_EVALUATE, operation.getMethodName()); + Assert.assertEquals("Invalid executed method name.", METHOD_EVALUATE, operation.getMethodName()); Assert.assertEquals("Invalid expression", EXPRESSION, operation.getExpression()); } @@ -97,7 +98,7 @@ public void testEvaluate3() throws Exception { XPathOperation operation = (XPathOperation) operations.get(0); Assert.assertEquals("Invalid event category.", VulnerabilityCaseType.XPATH, operation.getCaseType()); - Assert.assertEquals("Invalid executed method name.", XPATHUtils.METHOD_EVALUATE, operation.getMethodName()); + Assert.assertEquals("Invalid executed method name.", METHOD_EVALUATE, operation.getMethodName()); Assert.assertEquals("Invalid expression", EXPRESSION, operation.getExpression()); } @Test @@ -114,7 +115,7 @@ public void testCompile() throws Exception { XPathOperation operation = (XPathOperation) operations.get(0); Assert.assertEquals("Invalid event category.", VulnerabilityCaseType.XPATH, operation.getCaseType()); - Assert.assertEquals("Invalid executed method name.", XPATHUtils.METHOD_EVALUATE, operation.getMethodName()); + Assert.assertEquals("Invalid executed method name.", METHOD_EVALUATE, operation.getMethodName()); Assert.assertEquals("Invalid expression", EXPRESSION, operation.getExpression()); } @@ -132,7 +133,7 @@ public void testCompile1() throws Exception { XPathOperation operation = (XPathOperation) operations.get(0); Assert.assertEquals("Invalid event category.", VulnerabilityCaseType.XPATH, operation.getCaseType()); - Assert.assertEquals("Invalid executed method name.", XPATHUtils.METHOD_EVALUATE, operation.getMethodName()); + Assert.assertEquals("Invalid executed method name.", METHOD_EVALUATE, operation.getMethodName()); Assert.assertEquals("Invalid expression", EXPRESSION, operation.getExpression()); } @@ -150,7 +151,7 @@ public void testCompile2() throws Exception { XPathOperation operation = (XPathOperation) operations.get(0); Assert.assertEquals("Invalid event category.", VulnerabilityCaseType.XPATH, operation.getCaseType()); - Assert.assertEquals("Invalid executed method name.", XPATHUtils.METHOD_EVALUATE, operation.getMethodName()); + Assert.assertEquals("Invalid executed method name.", METHOD_EVALUATE, operation.getMethodName()); Assert.assertEquals("Invalid expression", EXPRESSION, operation.getExpression()); } @@ -168,7 +169,7 @@ public void testCompile3() throws Exception { XPathOperation operation = (XPathOperation) operations.get(0); Assert.assertEquals("Invalid event category.", VulnerabilityCaseType.XPATH, operation.getCaseType()); - Assert.assertEquals("Invalid executed method name.", XPATHUtils.METHOD_EVALUATE, operation.getMethodName()); + Assert.assertEquals("Invalid executed method name.", METHOD_EVALUATE, operation.getMethodName()); Assert.assertEquals("Invalid expression", EXPRESSION, operation.getExpression()); } }