Skip to content

Commit

Permalink
[SECURITY-1704]
Browse files Browse the repository at this point in the history
  • Loading branch information
Wadeck committed Jan 14, 2020
1 parent cd28a6d commit 639ade5
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 0 deletions.
4 changes: 4 additions & 0 deletions core/src/main/java/hudson/model/Api.java
Expand Up @@ -246,6 +246,10 @@ private boolean permit(StaplerRequest req) {
protected void setHeaders(StaplerResponse rsp) {
rsp.setHeader("X-Jenkins", Jenkins.VERSION);
rsp.setHeader("X-Jenkins-Session", Jenkins.SESSION_HASH);
// to be really defensive against dumb browsers not taking into consideration the content-type being set
rsp.setHeader("X-Content-Type-Options", "nosniff");
// recommended by OWASP: https://cheatsheetseries.owasp.org/cheatsheets/REST_Security_Cheat_Sheet.html#security-headers
rsp.setHeader("X-Frame-Options", "deny");
}

private static final Logger LOGGER = Logger.getLogger(Api.class.getName());
Expand Down
109 changes: 109 additions & 0 deletions test/src/test/java/hudson/model/ApiSEC1704Test.java
@@ -0,0 +1,109 @@
/*
* The MIT License
*
* Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Yahoo!, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.model;

import com.gargoylesoftware.htmlunit.WebResponse;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestExtension;
import org.kohsuke.stapler.export.ExportedBean;

import javax.annotation.CheckForNull;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

//TODO to be merged back to ApiTest after security release
/**
* @author Kohsuke Kawaguchi
*/
public class ApiSEC1704Test {

@Rule
public JenkinsRule j = new JenkinsRule();

@Test
@Issue("SECURITY-1704")
public void project_notExposedToIFrame() throws Exception {
FreeStyleProject p = j.createFreeStyleProject("p");
ensureXmlIsNotExposedToIFrame(p.getUrl());
ensureJsonIsNotExposedToIFrame(p.getUrl());
ensurePythonIsNotExposedToIFrame(p.getUrl());
}

@Test
@Issue("SECURITY-1704")
public void custom_notExposedToIFrame() throws Exception {
ensureXmlIsNotExposedToIFrame("custom/");
ensureJsonIsNotExposedToIFrame("custom/");
ensurePythonIsNotExposedToIFrame("custom/");
}

private void ensureXmlIsNotExposedToIFrame(String itemUrl) throws Exception {
WebResponse response = j.createWebClient().goTo(itemUrl + "api/xml", "application/xml").getWebResponse();
assertThat(response.getResponseHeaderValue("X-Frame-Options"), equalTo("deny"));
}

private void ensureJsonIsNotExposedToIFrame(String itemUrl) throws Exception {
WebResponse response = j.createWebClient().goTo(itemUrl + "api/json", "application/json").getWebResponse();
assertThat(response.getResponseHeaderValue("X-Frame-Options"), equalTo("deny"));
}

private void ensurePythonIsNotExposedToIFrame(String itemUrl) throws Exception {
WebResponse response = j.createWebClient().goTo(itemUrl + "api/python", "text/x-python").getWebResponse();
assertThat(response.getResponseHeaderValue("X-Frame-Options"), equalTo("deny"));
}

@TestExtension("custom_notExposedToIFrame")
public static class CustomObject implements RootAction {
@Override
public @CheckForNull String getIconFileName() {
return null;
}

@Override
public @CheckForNull String getDisplayName() {
return null;
}

@Override
public @CheckForNull String getUrlName() {
return "custom";
}

public Api getApi() {
return new Api(new CustomData("s3cr3t"));
}

@ExportedBean
class CustomData {
private String secret;
CustomData(String secret){
this.secret = secret;
}
}
}
}
@@ -0,0 +1,63 @@
/**
* Copyright (c) 2008-2010 Yahoo! Inc.
* All rights reserved.
* The copyrights to the contents of this file are licensed under the MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

package hudson.security.csrf;

import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebResponse;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import hudson.model.FreeStyleProject;
import hudson.model.User;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.JenkinsRule.WebClient;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
*
* @author dty
*/
//TODO merge back to DefaultCrumbIssuerTest
public class DefaultCrumbIssuerSEC1704Test {

@Rule public JenkinsRule r = new JenkinsRule();

@Before public void setIssuer() {
r.jenkins.setCrumbIssuer(new DefaultCrumbIssuer(false));
}

@Test
@Issue("SECURITY-1704")
public void custom_notExposedToIFrame() throws Exception {
ensureXmlIsNotExposedToIFrame("crumbIssuer/");
ensureJsonIsNotExposedToIFrame("crumbIssuer/");
ensurePythonIsNotExposedToIFrame("crumbIssuer/");
}

private void ensureXmlIsNotExposedToIFrame(String itemUrl) throws Exception {
WebResponse response = r.createWebClient().goTo(itemUrl + "api/xml", "application/xml").getWebResponse();
assertThat(response.getResponseHeaderValue("X-Frame-Options"), equalTo("deny"));
}

private void ensureJsonIsNotExposedToIFrame(String itemUrl) throws Exception {
WebResponse response = r.createWebClient().goTo(itemUrl + "api/json", "application/json").getWebResponse();
assertThat(response.getResponseHeaderValue("X-Frame-Options"), equalTo("deny"));
}

private void ensurePythonIsNotExposedToIFrame(String itemUrl) throws Exception {
WebResponse response = r.createWebClient().goTo(itemUrl + "api/python", "text/x-python").getWebResponse();
assertThat(response.getResponseHeaderValue("X-Frame-Options"), equalTo("deny"));
}
}

0 comments on commit 639ade5

Please sign in to comment.