]> source.dussan.org Git - sonarqube.git/commitdiff
SSF-10 Prevent Clickjacking
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 31 Dec 2014 07:26:05 +0000 (08:26 +0100)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 31 Dec 2014 07:26:05 +0000 (08:26 +0100)
server/sonar-server/src/main/java/org/sonar/server/platform/SecurityServletFilter.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/platform/SecurityServletFilterTest.java [new file with mode: 0644]
server/sonar-web/src/main/webapp/WEB-INF/web.xml

diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/SecurityServletFilter.java b/server/sonar-server/src/main/java/org/sonar/server/platform/SecurityServletFilter.java
new file mode 100644 (file)
index 0000000..33d1c9d
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.platform;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletResponse;
+
+import java.io.IOException;
+
+public class SecurityServletFilter implements Filter {
+
+  @Override
+  public void init(FilterConfig filterConfig) throws ServletException {
+    // nothing
+  }
+
+  @Override
+  public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
+    chain.doFilter(req, resp);
+
+    // Clickjacking protection
+    // See https://www.owasp.org/index.php/Clickjacking_Protection_for_Java_EE
+    HttpServletResponse httpResponse = (HttpServletResponse) resp;
+    httpResponse.addHeader("X-Frame-Options", "SAMEORIGIN");
+  }
+
+  @Override
+  public void destroy() {
+    // nothing
+  }
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/SecurityServletFilterTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/SecurityServletFilterTest.java
new file mode 100644 (file)
index 0000000..8bc547a
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.platform;
+
+import org.junit.Test;
+
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+public class SecurityServletFilterTest {
+
+  @Test
+  public void set_secured_headers() throws Exception {
+    SecurityServletFilter filter = new SecurityServletFilter();
+    filter.init(mock(FilterConfig.class));
+
+    HttpServletRequest request = mock(HttpServletRequest.class);
+    HttpServletResponse response = mock(HttpServletResponse.class);
+    FilterChain chain = mock(FilterChain.class);
+    filter.doFilter(request, response, chain);
+
+    // Clickjacking
+    verify(response).addHeader("X-Frame-Options", "SAMEORIGIN");
+
+    filter.destroy();
+  }
+}
index b4896f15c5666835603c75e544f995c70303b3f2..bb2cc8d743b2623fa93c7db039bc5bcff04090b6 100644 (file)
     <filter-name>RackFilter</filter-name>
     <filter-class>org.jruby.rack.RackFilter</filter-class>
   </filter>
+  <filter>
+    <filter-name>SecurityFilter</filter-name>
+    <filter-class>org.sonar.server.platform.SecurityServletFilter</filter-class>
+  </filter>
   <filter>
     <filter-name>ProfilingFilter</filter-name>
     <filter-class>org.sonar.server.platform.ProfilingFilter</filter-class>
     <filter-name>ServletFilters</filter-name>
     <url-pattern>/*</url-pattern>
   </filter-mapping>
+  <filter-mapping>
+    <filter-name>SecurityFilter</filter-name>
+    <url-pattern>/*</url-pattern>
+  </filter-mapping>
   <filter-mapping>
     <filter-name>RackFilter</filter-name>
     <url-pattern>/*</url-pattern>