1
0
mirror of synced 2026-08-04 09:17:02 +00:00

Add Referrer-Policy header support

Fixes gh-4110
This commit is contained in:
Eddú Meléndez
2016-10-29 12:00:56 -05:00
committed by Rob Winch
parent eb2870bf82
commit 23294c4c57
11 changed files with 475 additions and 3 deletions
@@ -0,0 +1,129 @@
/*
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.security.web.header.writers;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.web.header.HeaderWriter;
import org.springframework.util.Assert;
/**
* <p>
* Provides support for <a href="https://www.w3.org/TR/referrer-policy/">Referrer Policy</a>.
* </p>
*
* <p>
* The list of policies defined can be found at
* <a href="https://www.w3.org/TR/referrer-policy/#referrer-policies">Referrer Policies</a>.
* </p>
*
* <p>
* This implementation of {@link HeaderWriter} writes the following header:
* </p>
* <ul>
* <li>Referrer-Policy</li>
* </ul>
*
* <p>
* By default, the Referrer-Policy header is not included in the response.
* Policy <b>no-referrer</b> is used by default if no {@link ReferrerPolicy} is set.
* </p>
*
* @author Eddú Meléndez
* @since 4.2
*/
public class ReferrerPolicyHeaderWriter implements HeaderWriter {
private final String REFERER_POLICY_HEADER = "Referrer-Policy";
private ReferrerPolicy policy;
/**
* Creates a new instance. Default value: no-referrer.
*/
public ReferrerPolicyHeaderWriter() {
this(ReferrerPolicy.NO_REFERRER);
}
/**
* Creates a new instance.
*
* @param policy
* @throws IllegalArgumentException if policyDirectives is null or empty
*/
public ReferrerPolicyHeaderWriter(ReferrerPolicy policy) {
setPolicy(policy);
}
/**
* Sets the policy to be used in the response header.
* @param policy
*/
public void setPolicy(ReferrerPolicy policy) {
Assert.notNull(policy, "policy can not be null");
this.policy = policy;
}
/**
* @see org.springframework.security.web.header.HeaderWriter#writeHeaders(HttpServletRequest, HttpServletResponse)
*/
@Override
public void writeHeaders(HttpServletRequest request, HttpServletResponse response) {
response.setHeader(REFERER_POLICY_HEADER, this.policy.getPolicy());
}
public enum ReferrerPolicy {
NO_REFERRER("no-referrer"),
NO_REFERRER_WHEN_DOWNGRADE("no-referrer-when-downgrade"),
SAME_ORIGIN("same-origin"),
ORIGIN("origin"),
STRICT_ORIGIN("strict-origin"),
ORIGIN_WHEN_CROSS_ORIGIN("origin-when-cross-origin"),
STRICT_ORIGIN_WHEN_CROSS_ORIGIN("strict-origin-when-cross-origin"),
UNSAFE_URL("unsafe-url");
private static Map<String, ReferrerPolicy> REFERRER_POLICIES;
static {
Map<String, ReferrerPolicy> referrerPolicies = new HashMap<String, ReferrerPolicy>();
for (ReferrerPolicy referrerPolicy : values()) {
referrerPolicies.put(referrerPolicy.getPolicy(), referrerPolicy);
}
REFERRER_POLICIES = Collections.unmodifiableMap(referrerPolicies);
}
private String policy;
private ReferrerPolicy(String policy) {
this.policy = policy;
}
public String getPolicy() {
return this.policy;
}
public static ReferrerPolicy get(String referrerPolicy) {
return REFERRER_POLICIES.get(referrerPolicy);
}
}
}
@@ -0,0 +1,68 @@
/*
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.security.web.header.writers;
import org.junit.Before;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.security.web.header.writers.ReferrerPolicyHeaderWriter.*;
/**
* @author Eddú Meléndez
*/
public class ReferrerPolicyHeaderWriterTests {
private final String DEFAULT_REFERRER_POLICY = "no-referrer";
private MockHttpServletRequest request;
private MockHttpServletResponse response;
private ReferrerPolicyHeaderWriter writer;
@Before
public void setup() {
this.request = new MockHttpServletRequest();
this.request.setSecure(true);
this.response = new MockHttpServletResponse();
this.writer = new ReferrerPolicyHeaderWriter();
}
@Test
public void writeHeadersReferrerPolicyDefault() {
this.writer.writeHeaders(this.request, this.response);
assertThat(this.response.getHeaderNames().size()).isEqualTo(1);
assertThat(this.response.getHeader("Referrer-Policy")).isEqualTo(DEFAULT_REFERRER_POLICY);
}
@Test
public void writeHeadersReferrerPolicyCustom() {
this.writer = new ReferrerPolicyHeaderWriter(ReferrerPolicy.SAME_ORIGIN);
this.writer.writeHeaders(this.request, this.response);
assertThat(this.response.getHeaderNames().size()).isEqualTo(1);
assertThat(this.response.getHeader("Referrer-Policy")).isEqualTo("same-origin");
}
@Test(expected = IllegalArgumentException.class)
public void writeHeaderReferrerPolicyInvalid() {
this.writer = new ReferrerPolicyHeaderWriter(null);
}
}