From: Janis Streib Date: Wed, 27 Aug 2014 12:05:12 +0000 (+0200) Subject: ADD: !Configchange! http serve X-Git-Url: https://code.wpia.club/?p=gigi.git;a=commitdiff_plain;h=98410d964cfab49b45c5e56c4f9577f6527f400b ADD: !Configchange! http serve Config keys deleted: gigi.properties: port test.properties: serverPort Config keys added: gigi.properties: http.port gigi.properties: https.port test.properties: serverPort.http test.properties: serverPort.https --- diff --git a/config/gigi.properties.template b/config/gigi.properties.template index b091faf7..ef794f64 100644 --- a/config/gigi.properties.template +++ b/config/gigi.properties.template @@ -4,7 +4,8 @@ name.secure=secure.cacert.local name.www=www.cacert.local name.api=api.cacert.local -port=443 +https.port=443 +http.port=80 #emailProvider=org.cacert.gigi.email.Sendmail emailProvider=org.cacert.gigi.email.CommandlineEmailProvider sql.driver=com.mysql.jdbc.Driver diff --git a/config/test.properties.template b/config/test.properties.template index 97ce26b4..354b3f21 100644 --- a/config/test.properties.template +++ b/config/test.properties.template @@ -1,11 +1,13 @@ type=local -serverPort=443 +serverPort.https=443 +serverPort.http=80 mail=localhost:8474 # ==== OR === type=autonomous java=java -cp bin;/path/to/mysqlConnector.jar org.cacert.gigi.Launcher -serverPort=4443 +serverPort.https=4443 +serverPort.http=8098 mailPort=8473 diff --git a/src/org/cacert/gigi/Gigi.java b/src/org/cacert/gigi/Gigi.java index eab09850..53685824 100644 --- a/src/org/cacert/gigi/Gigi.java +++ b/src/org/cacert/gigi/Gigi.java @@ -126,18 +126,40 @@ public class Gigi extends HttpServlet { } + private static String staticTemplateVarHttp; + + private static String staticTemplateVarHttps; + + private static String getStaticTemplateVar(boolean https) { + if (https) { + if (staticTemplateVarHttps == null) { + staticTemplateVarHttps = "https://" + ServerConstants.getStaticHostNamePortSecure(); + } + return staticTemplateVarHttps; + } else { + if (staticTemplateVarHttp == null) { + staticTemplateVarHttp = "http://" + ServerConstants.getStaticHostNamePort(); + } + return staticTemplateVarHttp; + } + } + @Override protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { - addXSSHeaders(resp); + boolean isSecure = req.getServerPort() == ServerConstants.getSecurePort(); + addXSSHeaders(resp, isSecure); // if (req.getHeader("Origin") != null) { // resp.getWriter().println("No cross domain access allowed."); // return; // } HttpSession hs = req.getSession(); - final Page p = getPage(req.getPathInfo()); - if (p != null) { + if (p != null) { + if (!isSecure && (p.needsLogin() || p instanceof LoginPage || p instanceof RegisterPage)) { + resp.sendRedirect("https://" + ServerConstants.getWwwHostNamePortSecure() + req.getPathInfo()); + return; + } User currentPageUser = LoginPage.getUser(req); if ( !p.isPermitted(currentPageUser) && hs.getAttribute("loggedin") == null) { String request = req.getPathInfo(); @@ -180,7 +202,7 @@ public class Gigi extends HttpServlet { vars.put(Menu.USER_VALUE, currentPageUser); vars.put("menu", rootMenu); vars.put("title", Page.getLanguage(req).getTranslation(p.getTitle())); - vars.put("static", ServerConstants.getStaticHostNamePort()); + vars.put("static", getStaticTemplateVar(isSecure)); vars.put("year", Calendar.getInstance().get(Calendar.YEAR)); vars.put("content", content); baseTemplate.output(resp.getWriter(), Page.getLanguage(req), vars); @@ -213,31 +235,52 @@ public class Gigi extends HttpServlet { } - public static void addXSSHeaders(HttpServletResponse hsr) { - hsr.addHeader("Access-Control-Allow-Origin", "https://" + ServerConstants.getWwwHostNamePort() + " https://" + ServerConstants.getSecureHostNamePort()); + public static void addXSSHeaders(HttpServletResponse hsr, boolean doHttps) { + hsr.addHeader("Access-Control-Allow-Origin", "https://" + ServerConstants.getWwwHostNamePortSecure() + " https://" + ServerConstants.getSecureHostNamePort()); hsr.addHeader("Access-Control-Max-Age", "60"); - - hsr.addHeader("Content-Security-Policy", getDefaultCSP()); + if (doHttps) { + hsr.addHeader("Content-Security-Policy", getHttpsCSP()); + } else { + hsr.addHeader("Content-Security-Policy", getHttpCSP()); + } hsr.addHeader("Strict-Transport-Security", "max-age=31536000"); } - private static String defaultCSP = null; + private static String httpsCSP = null; + + private static String httpCSP = null; + + private static String getHttpsCSP() { + if (httpsCSP == null) { + StringBuffer csp = new StringBuffer(); + csp.append("default-src 'none'"); + csp.append(";font-src https://" + ServerConstants.getStaticHostNamePortSecure()); + csp.append(";img-src https://" + ServerConstants.getStaticHostNamePortSecure()); + csp.append(";media-src 'none'; object-src 'none'"); + csp.append(";script-src https://" + ServerConstants.getStaticHostNamePortSecure()); + csp.append(";style-src https://" + ServerConstants.getStaticHostNamePortSecure()); + csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePortSecure()); + csp.append(";report-url https://api.cacert.org/security/csp/report"); + httpsCSP = csp.toString(); + } + return httpsCSP; + } - private static String getDefaultCSP() { - if (defaultCSP == null) { + private static String getHttpCSP() { + if (httpCSP == null) { StringBuffer csp = new StringBuffer(); - csp.append("default-src 'none';"); - csp.append("font-src https://" + ServerConstants.getStaticHostNamePort()); - csp.append(";img-src https://" + ServerConstants.getStaticHostNamePort()); - csp.append(";media-src 'none'; object-src 'none';"); - csp.append("script-src https://" + ServerConstants.getStaticHostNamePort()); - csp.append(";style-src https://" + ServerConstants.getStaticHostNamePort()); + csp.append("default-src 'none'"); + csp.append(";font-src http://" + ServerConstants.getStaticHostNamePort()); + csp.append(";img-src http://" + ServerConstants.getStaticHostNamePort()); + csp.append(";media-src 'none'; object-src 'none'"); + csp.append(";script-src http://" + ServerConstants.getStaticHostNamePort()); + csp.append(";style-src http://" + ServerConstants.getStaticHostNamePort()); csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePort()); - csp.append("report-url https://api.cacert.org/security/csp/report"); - defaultCSP = csp.toString(); + csp.append(";report-url http://api.cacert.org/security/csp/report"); + httpCSP = csp.toString(); } - return defaultCSP; + return httpCSP; } public static String getPathByPage(Page p) { diff --git a/src/org/cacert/gigi/Gigi.templ b/src/org/cacert/gigi/Gigi.templ index 37858f74..4c156ad8 100644 --- a/src/org/cacert/gigi/Gigi.templ +++ b/src/org/cacert/gigi/Gigi.templ @@ -2,8 +2,8 @@ <?=$title?> - - + + @@ -11,7 +11,7 @@