]> WPIA git - gigi.git/commitdiff
fix: do not follow redirects when doing http-pings (+testCase)
authorFelix Dörre <felix@dogcraft.de>
Tue, 5 Jul 2016 07:07:48 +0000 (09:07 +0200)
committerFelix Dörre <felix@dogcraft.de>
Tue, 5 Jul 2016 14:25:27 +0000 (16:25 +0200)
fixes #61

Change-Id: Iae3589b50614d097f0705003836125aa955ca7d6

config/test.properties.template
src/org/cacert/gigi/Launcher.java
tests/org/cacert/gigi/ping/TestHTTP.java
tests/org/cacert/gigi/testUtils/PingTest.java

index b39f07b79569532b081fc1d90f38a7a46c44d4de..232f46e09afbf701eb8852db233106fd6793cd72 100644 (file)
@@ -32,6 +32,8 @@ domain.http=you-intstallation-for-the-textfiles
 domain.dnstest=the.dns.zone
 domain.testns=the.authorativ.ns.for.domain.dnstest
 domain.local=a.domain.that.resolves.to.localhost
 domain.dnstest=the.dns.zone
 domain.testns=the.authorativ.ns.for.domain.dnstest
 domain.local=a.domain.that.resolves.to.localhost
+#port that is 80 is redirected to
+domain.localHTTP=80
 
 
 email.address=somemail@yourdomain.org
 
 
 email.address=somemail@yourdomain.org
index 7bf5bd119554387739d8b07a2647342cde3f82df..a7730adcc9d249350fa555affeb949b109aa919c 100644 (file)
@@ -2,6 +2,7 @@ package org.cacert.gigi;
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
+import java.net.HttpURLConnection;
 import java.net.InetSocketAddress;
 import java.security.GeneralSecurityException;
 import java.security.Key;
 import java.net.InetSocketAddress;
 import java.security.GeneralSecurityException;
 import java.security.Key;
@@ -105,6 +106,7 @@ public class Launcher {
     public synchronized void boot() throws Exception {
         Locale.setDefault(Locale.ENGLISH);
         TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
     public synchronized void boot() throws Exception {
         Locale.setDefault(Locale.ENGLISH);
         TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
+        HttpURLConnection.setFollowRedirects(false);
 
         conf = GigiConfig.parse(System.in);
         ServerConstants.init(conf.getMainProps());
 
         conf = GigiConfig.parse(System.in);
         ServerConstants.init(conf.getMainProps());
index 31fecd942bb53e2ba4055aad9a182d77b953cbff..a1c5c7ae8e325700a20d2ab319f7c4965fb463d0 100644 (file)
@@ -4,8 +4,13 @@ import static org.hamcrest.CoreMatchers.*;
 import static org.junit.Assert.*;
 import static org.junit.Assume.*;
 
 import static org.junit.Assert.*;
 import static org.junit.Assume.*;
 
+import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.net.ServerSocket;
+import java.net.Socket;
 import java.net.URL;
 import java.net.URLEncoder;
 import java.sql.SQLException;
 import java.net.URL;
 import java.net.URLEncoder;
 import java.sql.SQLException;
@@ -106,4 +111,83 @@ public class TestHTTP extends PingTest {
         return IOUtils.readURL(new InputStreamReader(u.openStream(), "UTF-8")).trim();
 
     }
         return IOUtils.readURL(new InputStreamReader(u.openStream(), "UTF-8")).trim();
 
     }
+
+    @Test
+    public void testHttpRedirect() throws IOException, SQLException, InterruptedException {
+        try (ServerSocket s = openServer()) {
+            testHttpRedirect(s, true);
+        }
+    }
+
+    @Test
+    public void testHttpNoRedirect() throws IOException, SQLException, InterruptedException {
+        try (ServerSocket s = openServer()) {
+            testHttpRedirect(s, false);
+        }
+    }
+
+    private ServerSocket openServer() {
+        String localHTTP = getTestProps().getProperty("domain.localHTTP");
+        assumeNotNull(localHTTP);
+        try {
+            return new ServerSocket(Integer.parseInt(localHTTP));
+        } catch (IOException e) {
+            throw new Error("Requires a free port " + localHTTP);
+        }
+    }
+
+    public void testHttpRedirect(ServerSocket s, boolean doRedirect) throws IOException, SQLException, InterruptedException {
+        String test = getTestProps().getProperty("domain.local");
+        assumeNotNull(test);
+
+        Matcher m = initailizeDomainForm();
+
+        String content = "newdomain=" + URLEncoder.encode(test, "UTF-8") + //
+                "&emailType=y&email=2&HTTPType=y" + //
+                "&ssl-type-0=direct&ssl-port-0=" + //
+                "&ssl-type-1=direct&ssl-port-1=" + //
+                "&ssl-type-2=direct&ssl-port-2=" + //
+                "&ssl-type-3=direct&ssl-port-3=" + //
+                "&adddomain&csrf=" + csrf;
+        String p2 = sendDomainForm(content);
+        try (Socket s0 = s.accept()) {
+            BufferedReader br = new BufferedReader(new InputStreamReader(s0.getInputStream(), "UTF-8"));
+            String fst = br.readLine();
+            assertEquals("GET /cacert-" + m.group(1) + ".txt HTTP/1.1", fst);
+            while ( !br.readLine().equals("")) {
+            }
+            String res = m.group(2);
+            PrintWriter out = new PrintWriter(new OutputStreamWriter(s0.getOutputStream(), "UTF-8"));
+            if ( !doRedirect) {
+                out.println("HTTP/1.1 200 OK");
+                out.println("Content-length: " + res.length());
+                out.println();
+                out.print(res);
+            } else {
+                out.println("HTTP/1.1 302 Moved");
+                out.println("Location: /token");
+                out.println();
+            }
+            out.flush();
+        }
+        waitForPings(2);
+
+        TestMail mail = getMailReciever().receive();
+        mail.verify();
+
+        String newcontent = IOUtils.readURL(get(p2));
+        Pattern pat = Pattern.compile("<td>http</td>\\s*<td>success</td>");
+        pat = Pattern.compile("<td>http</td>\\s*<td>([^<]*)</td>\\s*<td>([^<]*)</td>\\s*<td>([^<]*)</td>");
+        Matcher m0 = pat.matcher(newcontent);
+        assertTrue(newcontent, m0.find());
+        if (doRedirect) {
+            assertEquals("failed", m0.group(1));
+            assertThat(m0.group(3), containsString("code 302"));
+        } else {
+            assertEquals("success", m0.group(1));
+        }
+        pat = Pattern.compile("<td>email</td>\\s*<td>success</td>");
+        assertTrue(newcontent, pat.matcher(newcontent).find());
+
+    }
 }
 }
index 0d15f2e86c3cf5807c3a969d647b83e061103c89..967fdb2ded062a95e998f640767bd77ecbee4cfc 100644 (file)
@@ -51,8 +51,9 @@ public abstract class PingTest extends ClientTest {
         openConnection.setDoOutput(true);
         openConnection.getOutputStream().write(content.getBytes("UTF-8"));
         openConnection.getHeaderField("Location");
         openConnection.setDoOutput(true);
         openConnection.getOutputStream().write(content.getBytes("UTF-8"));
         openConnection.getHeaderField("Location");
-        if (((HttpURLConnection) openConnection).getResponseCode() != 302) {
-            throw new Error(IOUtils.readURL(openConnection));
+        int code = ((HttpURLConnection) openConnection).getResponseCode();
+        if (code != 302) {
+            throw new Error("Code was: " + code + "\ncontent was: " + IOUtils.readURL(openConnection));
         }
 
         String newcontent = IOUtils.readURL(get(DomainOverview.PATH));
         }
 
         String newcontent = IOUtils.readURL(get(DomainOverview.PATH));