Skip to content
Open
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
1 change: 1 addition & 0 deletions instrumentation-security/apache-tomcat-10/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dependencies {
implementation("com.newrelic.agent.java:newrelic-api:${nrAPIVersion}")
implementation("org.apache.tomcat.embed:tomcat-embed-core:10.0.0")
implementation("org.apache.tomcat:tomcat-juli:10.0.0")
testImplementation("jakarta.faces:jakarta.faces-api:3.0.0")
}

jar {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,24 @@ public class HttpServletHelper {
public static void gatherURLMappings(ServletContext servletContext) {
try {
Map<String, ? extends ServletRegistration> servletRegistrations = servletContext.getServletRegistrations();
getJSPMappings(servletContext, SEPARATOR);

boolean isJSFSupported = false;
for (ServletRegistration servletReg : servletRegistrations.values()) {
String handlerName = servletReg.getClassName();
if (StringUtils.equalsAny(handlerName, URLMappingsHelper.JAVAX_FACES_WEBAPP_FACES_SERVLET, URLMappingsHelper.JAKARTA_FACES_WEBAPP_FACES_SERVLET)) {
isJSFSupported = true;
}
for (String mapping : servletReg.getMappings()) {
URLMappingsHelper.addApplicationURLMapping(new ApplicationURLMapping(WILDCARD, mapping, servletReg.getClassName()));
URLMappingsHelper.addApplicationURLMapping(new ApplicationURLMapping(WILDCARD, mapping, handlerName));
}
}
getJSPMappings(servletContext, SEPARATOR, isJSFSupported);

} catch (Exception e){
NewRelicSecurity.getAgent().log(LogLevel.WARNING, String.format(GenericHelper.ERROR_WHILE_GETTING_APP_ENDPOINTS, APACHE_TOMCAT_10, e.getMessage()), e, HttpServletHelper.class.getName());
}
}

private static void getJSPMappings(ServletContext servletContext, String dir) {
private static void getJSPMappings(ServletContext servletContext, String dir, boolean isJSFSupported) {
try {
if(dir.endsWith(SEPARATOR)){
Collection<String> resourcePaths = servletContext.getResourcePaths(dir);
Expand All @@ -43,9 +48,12 @@ private static void getJSPMappings(ServletContext servletContext, String dir) {
continue;
}
if(path.endsWith(SEPARATOR)) {
getJSPMappings(servletContext, path);
getJSPMappings(servletContext, path, isJSFSupported);
}
else if(StringUtils.endsWithAny(path, ".jsp", ".JSP", ".jspx", ".JSPX")) {
URLMappingsHelper.addApplicationURLMapping(new ApplicationURLMapping(WILDCARD, path));
}
else if(path.endsWith(".jsp") || path.endsWith(".jspx") || path.endsWith(".JSP") || path.endsWith(".JSPX")) {
else if (isJSFSupported && StringUtils.endsWithAny(path, ".xhtml", ".faces", ".jsf", ".XHTML", ".FACES", ".JSF")) {
URLMappingsHelper.addApplicationURLMapping(new ApplicationURLMapping(WILDCARD, path));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class APIEndpointTest {
public void setupEndpoints() {
expectedMappings.put("/servlet/*", HttpServletServer.class.getName()+"$1");
expectedMappings.put("/index.jsp", null);
expectedMappings.put("/index.xhtml", null);
}

@Test
Expand All @@ -40,7 +41,7 @@ public void testAPIEndpoint() throws Exception {

Set<ApplicationURLMapping> mappings = URLMappingsHelper.getApplicationURLMappings();
Assert.assertNotNull(mappings);
Assert.assertEquals(2, mappings.size());
Assert.assertEquals(3, mappings.size());
for (ApplicationURLMapping mapping : mappings) {
assertMappings(mapping);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package com.nr.agent.security.instrumentation.tomcat10;

import jakarta.faces.webapp.FacesServlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleState;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.webresources.TomcatURLStreamHandlerFactory;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.junit.rules.ExternalResource;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
Expand Down Expand Up @@ -65,7 +66,12 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
super.doGet(req, resp);
}
});
Tomcat.addServlet(context, "faces", new FacesServlet());
context.setAddWebinfClassesResources(true);
context.addServletMappingDecoded("/faces/*", "faces");
context.addServletMappingDecoded("*.jsf", "faces");
context.addServletMappingDecoded("*.faces", "faces");
context.addServletMappingDecoded("*.xhtml", "faces");
context.addWelcomeFile("/index.jsp");
context.addServletMappingDecoded("/servlet/*","servlet");

Expand All @@ -76,14 +82,21 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
public URI getEndPoint(String path) throws URISyntaxException {
return new URI("http://localhost:" + port + "/" + path);
}

private void createFile() {
File indexFile = new File(webappDirLocation + "index.jsp");
File indexJSFFile = new File(webappDirLocation + "index.xhtml");
try {
if (tmp.mkdir() && indexFile.createNewFile()) {
if (tmp.mkdir() && indexFile.createNewFile() && indexJSFFile.createNewFile()) {
BufferedWriter writer = new BufferedWriter(new FileWriter(indexFile));
writer.append("Hello World!");
writer.flush();
writer.close();

BufferedWriter writer1 = new BufferedWriter(new FileWriter(indexJSFFile));
writer1.append("Hello World!");
writer1.flush();
writer1.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down
1 change: 1 addition & 0 deletions instrumentation-security/apache-tomcat-7/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dependencies {
implementation("com.newrelic.agent.java:newrelic-api:${nrAPIVersion}")
implementation("org.apache.tomcat.embed:tomcat-embed-core:8.0.1")
implementation("org.apache.tomcat:tomcat-juli:8.0.1")
testImplementation("javax.faces:jsf-api:2.0")
}

jar {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,23 @@ public class HttpServletHelper {
public static void gatherURLMappings(ServletContext servletContext) {
try {
Map<String, ? extends ServletRegistration> servletRegistrations = servletContext.getServletRegistrations();
getJSPMappings(servletContext, SEPARATOR);

boolean isJSFSupported = false;
for (ServletRegistration servletReg : servletRegistrations.values()) {
String handlerName = servletReg.getClassName();
if (StringUtils.equalsAny(handlerName, URLMappingsHelper.JAVAX_FACES_WEBAPP_FACES_SERVLET, URLMappingsHelper.JAKARTA_FACES_WEBAPP_FACES_SERVLET)) {
isJSFSupported = true;
}
for (String mapping : servletReg.getMappings()) {
URLMappingsHelper.addApplicationURLMapping(new ApplicationURLMapping(WILDCARD, mapping, servletReg.getClassName()));
URLMappingsHelper.addApplicationURLMapping(new ApplicationURLMapping(WILDCARD, mapping, handlerName));
}
}
getJSPMappings(servletContext, SEPARATOR, isJSFSupported);
} catch (Exception e){
NewRelicSecurity.getAgent().log(LogLevel.WARNING, String.format(GenericHelper.ERROR_WHILE_GETTING_APP_ENDPOINTS, APACHE_TOMCAT_7, e.getMessage()), e, HttpServletHelper.class.getName());
}
}

private static void getJSPMappings(ServletContext servletContext, String dir) {
private static void getJSPMappings(ServletContext servletContext, String dir, boolean isJSFSupported) {
try {
if(dir.endsWith(SEPARATOR)){
Collection<String> resourcePaths = servletContext.getResourcePaths(dir);
Expand All @@ -42,9 +46,12 @@ private static void getJSPMappings(ServletContext servletContext, String dir) {
continue;
}
if(path.endsWith(SEPARATOR)) {
getJSPMappings(servletContext, path);
getJSPMappings(servletContext, path, isJSFSupported);
}
else if(StringUtils.endsWithAny(path, ".jsp", ".JSP", ".jspx", ".JSPX")) {
URLMappingsHelper.addApplicationURLMapping(new ApplicationURLMapping(WILDCARD, path));
}
else if(path.endsWith(".jsp") || path.endsWith(".jspx") || path.endsWith(".JSP") || path.endsWith(".JSPX")) {
else if (isJSFSupported && StringUtils.endsWithAny(path, ".xhtml", ".faces", ".jsf")) {
URLMappingsHelper.addApplicationURLMapping(new ApplicationURLMapping(WILDCARD, path));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class APIEndpointTest {
public void setupEndpoints() {
expectedMappings.put("/servlet/*", HttpServletServer.class.getName()+"$1");
expectedMappings.put("/index.jsp", null);
expectedMappings.put("/index.xhtml", null);
}

@Test
Expand All @@ -40,7 +41,7 @@ public void testAPIEndpoint() throws Exception {

Set<ApplicationURLMapping> mappings = URLMappingsHelper.getApplicationURLMappings();
Assert.assertNotNull(mappings);
Assert.assertEquals(2, mappings.size());
Assert.assertEquals(3, mappings.size());
for (ApplicationURLMapping mapping : mappings) {
assertMappings(mapping);
}
Expand All @@ -58,9 +59,10 @@ private void assertMappings(ApplicationURLMapping actualMapping) {
Assert.assertEquals(handler, actualMapping.getHandler());
Assert.assertEquals("*", actualMapping.getMethod());
}

@Trace(dispatcher = true)
private void service() throws IOException, URISyntaxException {
URL u = server.getEndPoint("").toURL();
URL u = server.getEndPoint("/test").toURL();
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setRequestProperty("content-type", "text/plain; charset=utf-8");
conn.connect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.junit.rules.ExternalResource;

import javax.faces.webapp.FacesServlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -65,7 +66,12 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
super.doGet(req, resp);
}
});
Tomcat.addServlet(context, "faces", new FacesServlet());
context.setAddWebinfClassesResources(true);
context.addServletMapping("/faces/*", "faces");
context.addServletMapping("*.jsf", "faces");
context.addServletMapping("*.faces", "faces");
context.addServletMapping("*.xhtml", "faces");
context.addWelcomeFile("/index.jsp");
context.addServletMapping("/servlet/*","servlet");

Expand All @@ -76,19 +82,27 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
public URI getEndPoint(String path) throws URISyntaxException {
return new URI("http://localhost:" + port + "/" + path);
}

private void createFile() {
File indexFile = new File(webappDirLocation + "index.jsp");
File indexJSFFile = new File(webappDirLocation + "index.xhtml");
try {
if (tmp.mkdir() && indexFile.createNewFile()) {
if (tmp.mkdir() && indexFile.createNewFile() && indexJSFFile.createNewFile()) {
BufferedWriter writer = new BufferedWriter(new FileWriter(indexFile));
writer.append("Hello World!");
writer.flush();
writer.close();

BufferedWriter writer1 = new BufferedWriter(new FileWriter(indexJSFFile));
writer1.append("Hello World!");
writer1.flush();
writer1.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private void stop() {
if (server.getServer() != null && server.getServer().getState() != LifecycleState.DESTROYED) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,21 +183,24 @@ public static void postProcessSecurityHook(HttpServletRequest request, HttpServl
}
public static void gatherURLMappings(ServletContext servletContext) {
try {
String contextPath = StringUtils.removeStart(StringUtils.removeEnd(servletContext.getContextPath(), SEPARATOR), StringUtils.SEPARATOR);
Map<String, ? extends ServletRegistration> servletRegistrations = servletContext.getServletRegistrations();
getJSPMappings(servletContext, SEPARATOR);

boolean isJSFSupported = false;
for (ServletRegistration servletReg : servletRegistrations.values()) {
String handlerName = servletReg.getClassName();
if (StringUtils.equalsAny(handlerName, URLMappingsHelper.JAVAX_FACES_WEBAPP_FACES_SERVLET, URLMappingsHelper.JAKARTA_FACES_WEBAPP_FACES_SERVLET)) {
isJSFSupported = true;
}
for (String mapping : servletReg.getMappings()) {
URLMappingsHelper.addApplicationURLMapping(new ApplicationURLMapping(WILDCARD, mapping, servletReg.getClassName()));
URLMappingsHelper.addApplicationURLMapping(new ApplicationURLMapping(WILDCARD, mapping, handlerName));
}
}
getJSPMappings(servletContext, SEPARATOR, isJSFSupported);
} catch (Exception e){
NewRelicSecurity.getAgent().log(LogLevel.WARNING, String.format(GenericHelper.ERROR_WHILE_GETTING_APP_ENDPOINTS, JETTY_11, e.getMessage()), e, HttpServletHelper.class.getName());
}
}

private static void getJSPMappings(ServletContext servletContext, String dir) {
private static void getJSPMappings(ServletContext servletContext, String dir, boolean isJSFSupported) {
try {
if(dir.endsWith(SEPARATOR)){
Collection<String> resourcePaths = servletContext.getResourcePaths(dir);
Expand All @@ -207,9 +210,12 @@ private static void getJSPMappings(ServletContext servletContext, String dir) {
continue;
}
if(path.endsWith(SEPARATOR)) {
getJSPMappings(servletContext, path);
getJSPMappings(servletContext, path, isJSFSupported);
}
else if(StringUtils.endsWithAny(path, ".jsp", ".JSP", ".jspx", ".JSPX")) {
URLMappingsHelper.addApplicationURLMapping(new ApplicationURLMapping(WILDCARD, path));
}
else if(path.endsWith(".jsp") || path.endsWith(".jspx") || path.endsWith(".JSP") || path.endsWith(".JSPX")) {
else if (isJSFSupported && StringUtils.endsWithAny(path, ".xhtml", ".faces", ".jsf", ".XHTML", ".FACES", ".JSF")) {
URLMappingsHelper.addApplicationURLMapping(new ApplicationURLMapping(WILDCARD, path));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,19 +186,23 @@ public static void postProcessSecurityHook(HttpServletRequest request, HttpServl
public static void gatherURLMappings(ServletContext servletContext) {
try {
Map<String, ? extends ServletRegistration> servletRegistrations = servletContext.getServletRegistrations();
getJSPMappings(servletContext, SEPARATOR);

boolean isJSFSupported = false;
for (ServletRegistration servletReg : servletRegistrations.values()) {
String handlerName = servletReg.getClassName();
if (StringUtils.equalsAny(handlerName, URLMappingsHelper.JAVAX_FACES_WEBAPP_FACES_SERVLET, URLMappingsHelper.JAKARTA_FACES_WEBAPP_FACES_SERVLET)) {
isJSFSupported = true;
}
for (String mapping : servletReg.getMappings()) {
URLMappingsHelper.addApplicationURLMapping(new ApplicationURLMapping(WILDCARD, mapping, servletReg.getClassName()));
URLMappingsHelper.addApplicationURLMapping(new ApplicationURLMapping(WILDCARD, mapping, handlerName));
}
}
getJSPMappings(servletContext, SEPARATOR, isJSFSupported);
} catch (Exception e){
NewRelicSecurity.getAgent().log(LogLevel.WARNING, String.format(GenericHelper.ERROR_WHILE_GETTING_APP_ENDPOINTS, JETTY_9, e.getMessage()), e, HttpServletHelper.class.getName());
}
}

private static void getJSPMappings(ServletContext servletContext, String dir) {
private static void getJSPMappings(ServletContext servletContext, String dir, boolean isJSFSupported) {
try {
if(dir.endsWith(SEPARATOR)){
Collection<String> resourcePaths = servletContext.getResourcePaths(dir);
Expand All @@ -208,9 +212,12 @@ private static void getJSPMappings(ServletContext servletContext, String dir) {
continue;
}
if(path.endsWith(SEPARATOR)) {
getJSPMappings(servletContext, path);
getJSPMappings(servletContext, path, isJSFSupported);
}
else if(StringUtils.endsWithAny(path, ".jsp", ".JSP", ".jspx", ".JSPX")) {
URLMappingsHelper.addApplicationURLMapping(new ApplicationURLMapping(WILDCARD, path));
}
else if(path.endsWith(".jsp") || path.endsWith(".jspx") || path.endsWith(".JSP") || path.endsWith(".JSPX")) {
else if (isJSFSupported && StringUtils.endsWithAny(path, ".xhtml", ".faces", ".jsf")) {
URLMappingsHelper.addApplicationURLMapping(new ApplicationURLMapping(WILDCARD, path));
}
}
Expand Down
1 change: 1 addition & 0 deletions instrumentation-security/servlet-3.0/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ dependencies {
implementation('jakarta.servlet:jakarta.servlet-api:4.0.4')

testImplementation('org.apache.tomcat.embed:tomcat-embed-core:9.0.70')
testImplementation("javax.faces:jsf-api:2.0")
}

jar {
Expand Down
Loading