use of com.zimbra.common.util.RemoteIP in project zm-mailbox by Zimbra.
the class TestRemoteIP method testNoXForwardedHeaders.
@Test
public void testNoXForwardedHeaders() throws UnsupportedEncodingException, MalformedURLException {
HashMap<String, String> headers = new HashMap<String, String>();
MockHttpServletRequest req = new MockHttpServletRequest("test".getBytes("UTF-8"), new URL("http://localhost:7070/service/FooRequest"), "", 80, "192.168.1.1", headers);
RemoteIP remoteIp = new RemoteIP(req, new RemoteIP.TrustedIPs(new String[] { "192.168.1.1" }));
assertNull("originating IP should be null", remoteIp.getOrigIP());
assertNull("originating port should be null", remoteIp.getOrigPort());
assertNull("originating protocol should be null", remoteIp.getOrigProto());
assertEquals("wrong request IP", "192.168.1.1", remoteIp.getRequestIP());
assertEquals("wrong request port", "80", remoteIp.getRequestPort().toString());
assertEquals("wrong client IP", "192.168.1.1", remoteIp.getClientIP());
assertEquals("wrong client port", "80", remoteIp.getClientPort().toString());
}
use of com.zimbra.common.util.RemoteIP in project zm-mailbox by Zimbra.
the class TestRemoteIP method testNonTrustedIPLogString.
@Test
public void testNonTrustedIPLogString() throws Exception {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put(RemoteIP.X_ORIGINATING_IP_HEADER, "172.16.150.11");
headers.put(RemoteIP.X_ORIGINATING_PORT_HEADER, "8080");
headers.put(RemoteIP.X_ORIGINATING_PROTOCOL_HEADER, "IMAP");
MockHttpServletRequest req = new MockHttpServletRequest("test".getBytes("UTF-8"), new URL("http://localhost:7070/service/FooRequest"), "", 80, "10.10.1.1", headers);
RemoteIP remoteIp = new RemoteIP(req, new RemoteIP.TrustedIPs(new String[] { "192.168.1.1" }));
remoteIp.addToLoggingContext();
String updatedLogContext = ZimbraLog.getContextString();
// we should ignore X-Forwarded-XXX headers from non-trusted clients
assertTrue(updatedLogContext.indexOf("oip=172.16.150.11") == -1);
assertTrue(updatedLogContext.indexOf("oport=8080") == -1);
assertTrue(updatedLogContext.indexOf("ip=10.10.1.1") > -1);
assertTrue(updatedLogContext.indexOf("port=80") > -1);
assertTrue(updatedLogContext.indexOf("oproto=IMAP") == -1);
}
use of com.zimbra.common.util.RemoteIP in project zm-mailbox by Zimbra.
the class ThrottlingFilter method doFilter.
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
HttpServletRequest hreq = (HttpServletRequest) req;
HttpServletResponse hresp = (HttpServletResponse) resp;
HttpSession session = hreq.getSession(false);
// always get the latest value from LC
int max = LC.servlet_max_concurrent_requests_per_session.intValue();
if (session == null || max <= 0) {
// don't throttle if no session or disabled
chain.doFilter(req, resp);
return;
}
Semaphore tracker = sid2tracker.get(session.getId());
if (tracker == null) {
tracker = new Semaphore(max);
Semaphore exist = sid2tracker.putIfAbsent(session.getId(), tracker);
if (exist == null) {
// absent
session.setAttribute(getClass().getName(), new SessionBindingListener());
} else {
tracker = exist;
}
}
if (tracker.tryAcquire()) {
try {
chain.doFilter(req, resp);
} finally {
tracker.release();
}
} else {
new RemoteIP(hreq, ZimbraServlet.getTrustedIPs()).addToLoggingContext();
ZimbraLog.addToContext("jsessionid", session.getId());
ZimbraLog.misc.warn("too many concurrent HTTP requests");
ZimbraLog.clearContext();
hresp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "too many concurrent HTTP requests");
}
}
use of com.zimbra.common.util.RemoteIP in project zm-mailbox by Zimbra.
the class TestRemoteIP method testTrustedIPLogString.
@Test
public void testTrustedIPLogString() throws Exception {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put(RemoteIP.X_ORIGINATING_IP_HEADER, "172.16.150.11");
headers.put(RemoteIP.X_ORIGINATING_PORT_HEADER, "8080");
headers.put(RemoteIP.X_ORIGINATING_PROTOCOL_HEADER, "IMAP");
MockHttpServletRequest req = new MockHttpServletRequest("test".getBytes("UTF-8"), new URL("http://localhost:7070/service/FooRequest"), "", 80, "192.168.1.1", headers);
RemoteIP remoteIp = new RemoteIP(req, new RemoteIP.TrustedIPs(new String[] { "192.168.1.1" }));
remoteIp.addToLoggingContext();
String updatedLogContext = ZimbraLog.getContextString();
assertTrue(updatedLogContext.indexOf("oip=172.16.150.11") > -1);
assertTrue(updatedLogContext.indexOf("oport=8080") > -1);
assertTrue(updatedLogContext.indexOf("ip=172.16.150.11") > -1);
assertTrue(updatedLogContext.indexOf("port=8080") > -1);
assertTrue(updatedLogContext.indexOf("oproto=IMAP") > -1);
}
use of com.zimbra.common.util.RemoteIP in project zm-mailbox by Zimbra.
the class TestRemoteIP method testNonTrustedClientIPRemoteIP.
@Test
public void testNonTrustedClientIPRemoteIP() throws UnsupportedEncodingException, MalformedURLException {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put(RemoteIP.X_ORIGINATING_PROTOCOL_HEADER, "IMAP");
MockHttpServletRequest req = new MockHttpServletRequest("test".getBytes("UTF-8"), new URL("http://localhost:7070/service/FooRequest"), "", 80, "10.10.1.1", headers);
RemoteIP remoteIp = new RemoteIP(req, new RemoteIP.TrustedIPs(new String[] { "192.168.1.1" }));
// we should ignore X-Forwarded-XXX headers from non-trusted clients
assertNull("originating IP should be null", remoteIp.getOrigIP());
assertNull("originating port should be null", remoteIp.getOrigPort());
assertNull("originating protocol should be null", remoteIp.getOrigProto());
assertEquals("wrong request IP", "10.10.1.1", remoteIp.getRequestIP());
assertEquals("wrong request port", "80", remoteIp.getRequestPort().toString());
assertEquals("wrong client IP", "10.10.1.1", remoteIp.getClientIP());
assertEquals("wrong client port", "80", remoteIp.getClientPort().toString());
}
Aggregations