When the peer is behind a reverse proxy providing SSL/TLS encryption, the rendered absolute URLs should start with https when the user browser requested https : added limited support to the X-Forwarded-Proto HTTP header notably provided on Heroku platform. Also added some unit tests.pull/93/head
parent
75802dcf00
commit
731684105a
@ -0,0 +1,120 @@
|
||||
// YaCyDefaultServletTest.java
|
||||
// Copyright 2016 by luccioman; https://github.com/luccioman
|
||||
//
|
||||
// This is a part of YaCy, a peer-to-peer based web search engine
|
||||
//
|
||||
// LICENSE
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation; either version 2 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
package net.yacy.http.servlets;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.net.HttpHeaders;
|
||||
|
||||
import net.yacy.cora.protocol.HeaderFramework;
|
||||
import net.yacy.cora.protocol.RequestHeader;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link YaCyDefaultServlet} class.
|
||||
* @author luccioman
|
||||
*
|
||||
*/
|
||||
public class YaCyDefaultServletTest {
|
||||
|
||||
/**
|
||||
* getContext() should be able fallback to default value with null or empty parameters.
|
||||
*/
|
||||
@Test
|
||||
public void testGetContextEmptyParams() {
|
||||
assertEquals("http://localhost:8090", YaCyDefaultServlet.getContext(null, null));
|
||||
|
||||
RequestHeader header = new RequestHeader();
|
||||
assertEquals("http://localhost:8090", YaCyDefaultServlet.getContext(header, null));
|
||||
}
|
||||
|
||||
/**
|
||||
* getContext() : standard "Host" HTTP header is filled with host and port
|
||||
*/
|
||||
@Test
|
||||
public void testGetContextHostHeader() {
|
||||
RequestHeader header = new RequestHeader();
|
||||
header.put(HeaderFramework.HOST, "localhost:8090");
|
||||
assertEquals("http://localhost:8090", YaCyDefaultServlet.getContext(header, null));
|
||||
|
||||
header = new RequestHeader();
|
||||
header.put(HeaderFramework.HOST, "myhost.com:8090");
|
||||
assertEquals("http://myhost.com:8090", YaCyDefaultServlet.getContext(header, null));
|
||||
}
|
||||
|
||||
/**
|
||||
* getContext() : standard "Host" header is filled with hostname and port,
|
||||
* custom "X-YaCy-Request-Scheme" header indicates the protocol
|
||||
*/
|
||||
@Test
|
||||
public void testGetContextCustomProtocolHeader() {
|
||||
RequestHeader header = new RequestHeader();
|
||||
header.put(HeaderFramework.HOST, "myhost.com:8443");
|
||||
header.put(HeaderFramework.X_YACY_REQUEST_SCHEME, "https");
|
||||
assertEquals("https://myhost.com:8443", YaCyDefaultServlet.getContext(header, null));
|
||||
|
||||
header = new RequestHeader();
|
||||
header.put(HeaderFramework.HOST, "myhost.com:8090");
|
||||
header.put(HeaderFramework.X_YACY_REQUEST_SCHEME, "http");
|
||||
assertEquals("http://myhost.com:8090", YaCyDefaultServlet.getContext(header, null));
|
||||
}
|
||||
|
||||
/**
|
||||
* getContext() : standard "Host" header is filled only with hostname (default standard port),
|
||||
* custom "X-YaCy-Request-Scheme" indicates the protocol
|
||||
*/
|
||||
@Test
|
||||
public void testGetContextDefaultPortCustomProtocolHeader() {
|
||||
RequestHeader header = new RequestHeader();
|
||||
header.put(HeaderFramework.HOST, "myhost.com");
|
||||
header.put(HeaderFramework.X_YACY_REQUEST_SCHEME, "http");
|
||||
assertEquals("http://myhost.com", YaCyDefaultServlet.getContext(header, null));
|
||||
|
||||
header = new RequestHeader();
|
||||
header.put(HeaderFramework.HOST, "myhost.com");
|
||||
header.put(HeaderFramework.X_YACY_REQUEST_SCHEME, "https");
|
||||
assertEquals("https://myhost.com", YaCyDefaultServlet.getContext(header, null));
|
||||
}
|
||||
|
||||
/**
|
||||
* getContext() : reverse proxy serving HTTPS, YaCy serving HTTP
|
||||
*/
|
||||
@Test
|
||||
public void testGetContextReverseProxy() {
|
||||
/* Different protocols : HTTPS on proxy, HTTP on peer */
|
||||
RequestHeader header = new RequestHeader();
|
||||
header.put(HeaderFramework.HOST, "myhost.com");
|
||||
header.put(HeaderFramework.X_YACY_REQUEST_SCHEME, "http");
|
||||
header.put(HttpHeaders.X_FORWARDED_PROTO.toString(), "https");
|
||||
assertEquals("https://myhost.com", YaCyDefaultServlet.getContext(header, null));
|
||||
|
||||
/* Illegal X-Forwarded-Proto header value */
|
||||
header = new RequestHeader();
|
||||
header.put(HeaderFramework.HOST, "myhost.com:8090");
|
||||
header.put(HeaderFramework.X_YACY_REQUEST_SCHEME, "http");
|
||||
header.put(HttpHeaders.X_FORWARDED_PROTO.toString(), "http://attacker.com?query=");
|
||||
assertEquals("http://myhost.com:8090", YaCyDefaultServlet.getContext(header, null));
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in new issue