Skip to content

Commit

Permalink
Added Servlet.getRequestURL instrumentation (#6031)
Browse files Browse the repository at this point in the history
  • Loading branch information
DDJavierSantos committed Oct 27, 2023
1 parent 848d377 commit 29753c5
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,6 @@ public void onGetPathInfo(@Nullable String s) {
onNamed(Collections.singleton(s), SourceTypes.REQUEST_PATH);
}

@Override
public void onGetRequestURI(@Nullable String s) {
onNamed(Collections.singleton(s), SourceTypes.REQUEST_URI);
}

private static void onNamed(@Nullable final Iterable<String> names, final byte source) {
if (names == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,28 @@ public static void beforeRequestDispatcher(@CallSite.Argument final String path)
}
}

@Source(SourceTypes.REQUEST_URI)
@CallSite.After("java.lang.StringBuffer javax.servlet.http.HttpServletRequest.getRequestURL()")
public static StringBuffer afterGetRequestURL(
@CallSite.This final HttpServletRequest self, @CallSite.Return final StringBuffer retValue) {
if (null != retValue && retValue.length() > 0) {
final PropagationModule module = InstrumentationBridge.PROPAGATION;
if (module != null) {
try {
module.taintObject(SourceTypes.REQUEST_URI, null, retValue.toString(), retValue);
} catch (final Throwable e) {
module.onUnexpectedException("afterGetRequestURL threw", e);
}
}
}
return retValue;
}

@Source(SourceTypes.REQUEST_PATH)
@CallSite.After("java.lang.String javax.servlet.http.HttpServletRequest.getRequestURI()")
@CallSite.After("java.lang.String javax.servlet.http.HttpServletRequest.getPathInfo()")
@CallSite.After("java.lang.String javax.servlet.http.HttpServletRequest.getPathTranslated()")
public static String afterGetPathInfo(
public static String afterGetPath(
@CallSite.This final HttpServletRequest self, @CallSite.Return final String retValue) {
if (null != retValue && !retValue.isEmpty()) {
final WebModule module = InstrumentationBridge.WEB;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class HttpServletRequestCallSiteTest extends AgentTestRunner {
setup:
final iastModule = Mock(WebModule)
InstrumentationBridge.registerIastModule(iastModule)
final mock = Mock(HttpServletRequest){
final mock = Mock(clazz){
getRequestURI() >> 'retValue'
}
final testSuite = new TestHttpServletRequestCallSiteSuite(mock)
Expand All @@ -199,6 +199,29 @@ class HttpServletRequestCallSiteTest extends AgentTestRunner {
HttpServletRequestWrapper | _
}

void 'test getRequestURL'() {
setup:
final module = Mock(PropagationModule)
final retValue = new StringBuffer("retValue")
InstrumentationBridge.registerIastModule(module)
final mock = Mock(clazz){
getRequestURL() >> retValue
}
final testSuite = new TestHttpServletRequestCallSiteSuite(mock)

when:
testSuite.getRequestURL()

then:
1 * module.taintObject(_,_,_,_)

where:
clazz | _
HttpServletRequest | _
HttpServletRequestWrapper | _
}


void 'test getPathInfo'() {
setup:
final iastModule = Mock(WebModule)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public String getHeader(final String headerName) {
return request.getHeader(headerName);
}

public StringBuffer getRequestURL() {
return request.getRequestURL();
}

public Enumeration<?> getHeaders(final String headerName) {
return request.getHeaders(headerName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,12 @@ String pathInfo(HttpServletRequest request) {
return String.format("Request.getRequestURI returns %s", pathInfo);
}

@GetMapping("/getrequesturl")
String requestURL(HttpServletRequest request) {
StringBuffer requestURL = request.getRequestURL();
return String.format("Request.getRequestURL returns %s", requestURL);
}

private void withProcess(final Operation<Process> op) {
Process process = null;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,21 @@ abstract class AbstractIastSpringBootTest extends AbstractIastServerSmokeTest {
}
}

void 'getRequestURL taints its output'() {
setup:
String url = "http://localhost:${httpPort}/getrequesturl"
def request = new Request.Builder().url(url).get().build()

when:
client.newCall(request).execute()

then:
hasTainted { tainted ->
tainted.value == url &&
tainted.ranges[0].source.origin == 'http.request.uri'
}
}

void 'request header taint string'() {
setup:
String url = "http://localhost:${httpPort}/request_header/test"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,4 @@ void onMultipartValues(
@Nullable String headerName, @Nullable final Collection<String> headerValues);

void onGetPathInfo(@Nullable String s);

void onGetRequestURI(@Nullable String s);
}

0 comments on commit 29753c5

Please sign in to comment.