]> WPIA git - gigi.git/blob - src/org/cacert/gigi/Gigi.java
Convert CSRF-Problems to Exceptions.
[gigi.git] / src / org / cacert / gigi / Gigi.java
1 package org.cacert.gigi;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.util.Calendar;
6 import java.util.HashMap;
7 import java.util.Map;
8 import java.util.Properties;
9
10 import javax.servlet.ServletException;
11 import javax.servlet.http.HttpServlet;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14 import javax.servlet.http.HttpSession;
15
16 import org.cacert.gigi.database.DatabaseConnection;
17 import org.cacert.gigi.email.EmailProvider;
18 import org.cacert.gigi.output.Menu;
19 import org.cacert.gigi.output.MenuItem;
20 import org.cacert.gigi.output.Outputable;
21 import org.cacert.gigi.output.Form.CSRFException;
22 import org.cacert.gigi.output.template.Template;
23 import org.cacert.gigi.pages.LoginPage;
24 import org.cacert.gigi.pages.MainPage;
25 import org.cacert.gigi.pages.Page;
26 import org.cacert.gigi.pages.TestSecure;
27 import org.cacert.gigi.pages.Verify;
28 import org.cacert.gigi.pages.account.ChangePasswordPage;
29 import org.cacert.gigi.pages.account.MailCertificateAdd;
30 import org.cacert.gigi.pages.account.MailCertificates;
31 import org.cacert.gigi.pages.account.MailOverview;
32 import org.cacert.gigi.pages.account.MyDetails;
33 import org.cacert.gigi.pages.error.PageNotFound;
34 import org.cacert.gigi.pages.main.RegisterPage;
35 import org.cacert.gigi.pages.wot.AssurePage;
36 import org.cacert.gigi.util.ServerConstants;
37
38 public class Gigi extends HttpServlet {
39         public static final String LOGGEDIN = "loggedin";
40         public static final String USER = "user";
41         private static final long serialVersionUID = -6386785421902852904L;
42         private Template baseTemplate;
43         private HashMap<String, Page> pages = new HashMap<String, Page>();
44         Menu m;
45
46         public Gigi(Properties conf) {
47                 EmailProvider.init(conf);
48                 DatabaseConnection.init(conf);
49         }
50
51         @Override
52         public void init() throws ServletException {
53                 pages.put("/error", new PageNotFound());
54                 pages.put("/login", new LoginPage("CACert - Login"));
55                 pages.put("/", new MainPage("CACert - Home"));
56                 pages.put("/secure", new TestSecure());
57                 pages.put(Verify.PATH, new Verify());
58                 pages.put(AssurePage.PATH + "/*", new AssurePage());
59                 pages.put(MailCertificates.PATH + "/*", new MailCertificates());
60                 pages.put(MyDetails.PATH, new MyDetails());
61                 pages.put(ChangePasswordPage.PATH, new ChangePasswordPage());
62                 pages.put(RegisterPage.PATH, new RegisterPage());
63                 pages.put(MailCertificateAdd.PATH, new MailCertificateAdd());
64                 pages.put(MailOverview.DEFAULT_PATH, new MailOverview("My email addresses"));
65                 baseTemplate = new Template(Gigi.class.getResource("Gigi.templ"));
66                 m = new Menu("Certificates", "cert", new MenuItem(MailOverview.DEFAULT_PATH, "Emails"), new MenuItem("",
67                         "Client Certificates"), new MenuItem("", "Domains"), new MenuItem("", "Server Certificates"));
68                 super.init();
69
70         }
71
72         @Override
73         protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException,
74                 IOException {
75                 addXSSHeaders(resp);
76                 // if (req.getHeader("Origin") != null) {
77                 // resp.getWriter().println("No cross domain access allowed.");
78                 // return;
79                 // }
80                 HttpSession hs = req.getSession();
81                 if (req.getPathInfo() != null && req.getPathInfo().equals("/logout")) {
82                         if (hs != null) {
83                                 hs.setAttribute(LOGGEDIN, null);
84                                 hs.invalidate();
85                         }
86                         resp.sendRedirect("/");
87                         return;
88                 }
89
90                 final Page p = getPage(req.getPathInfo());
91                 if (p != null) {
92
93                         if (p.needsLogin() && hs.getAttribute("loggedin") == null) {
94                                 String request = req.getPathInfo();
95                                 request = request.split("\\?")[0];
96                                 hs.setAttribute(LoginPage.LOGIN_RETURNPATH, request);
97                                 resp.sendRedirect("/login");
98                                 return;
99                         }
100                         if (p.beforeTemplate(req, resp)) {
101                                 return;
102                         }
103                         HashMap<String, Object> vars = new HashMap<String, Object>();
104
105                         resp.setContentType("text/html; charset=utf-8");
106                         Outputable content = new Outputable() {
107
108                                 @Override
109                                 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
110                                         try {
111                                                 if (req.getMethod().equals("POST")) {
112                                                         p.doPost(req, resp);
113                                                 } else {
114                                                         p.doGet(req, resp);
115                                                 }
116                                         } catch (CSRFException err) {
117                                                 try {
118                                                         resp.sendError(500, "CSRF invalid");
119                                                 } catch (IOException e) {
120                                                         e.printStackTrace();
121                                                 }
122                                         } catch (IOException e) {
123                                                 e.printStackTrace();
124                                         }
125
126                                 }
127                         };
128                         vars.put("menu", m);
129                         vars.put("title", p.getTitle());
130                         vars.put("static", ServerConstants.getStaticHostNamePort());
131                         vars.put("year", Calendar.getInstance().get(Calendar.YEAR));
132                         vars.put("content", content);
133                         baseTemplate.output(resp.getWriter(), Page.getLanguage(req), vars);
134                 } else {
135                         resp.sendError(404, "Page not found.");
136                 }
137
138         }
139
140         private Page getPage(String pathInfo) {
141                 if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
142                         pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
143                 }
144                 Page page = pages.get(pathInfo);
145                 if (page != null) {
146                         return page;
147                 }
148                 page = pages.get(pathInfo + "/*");
149                 if (page != null) {
150                         return page;
151                 }
152                 int idx = pathInfo.lastIndexOf('/');
153                 pathInfo = pathInfo.substring(0, idx);
154
155                 page = pages.get(pathInfo + "/*");
156                 if (page != null) {
157                         return page;
158                 }
159                 return null;
160
161         }
162
163         public static void addXSSHeaders(HttpServletResponse hsr) {
164                 hsr.addHeader("Access-Control-Allow-Origin", "https://" + ServerConstants.getWwwHostNamePort() + " https://"
165                         + ServerConstants.getSecureHostNamePort());
166                 hsr.addHeader("Access-Control-Max-Age", "60");
167
168                 hsr.addHeader("Content-Security-Policy", getDefaultCSP());
169                 hsr.addHeader("Strict-Transport-Security", "max-age=31536000");
170
171         }
172
173         private static String defaultCSP = null;
174
175         private static String getDefaultCSP() {
176                 if (defaultCSP == null) {
177                         StringBuffer csp = new StringBuffer();
178                         csp.append("default-src 'none';");
179                         csp.append("font-src https://" + ServerConstants.getStaticHostNamePort());
180                         csp.append(";img-src https://" + ServerConstants.getStaticHostNamePort());
181                         csp.append(";media-src 'none'; object-src 'none';");
182                         csp.append("script-src https://" + ServerConstants.getStaticHostNamePort());
183                         csp.append(";style-src https://" + ServerConstants.getStaticHostNamePort());
184                         csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://"
185                                 + ServerConstants.getWwwHostNamePort());
186                         csp.append("report-url https://api.cacert.org/security/csp/report");
187                         defaultCSP = csp.toString();
188                 }
189                 return defaultCSP;
190         }
191 }