|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.struts2.interceptor; |
| 20 | + |
| 21 | + |
| 22 | +import static org.apache.struts2.interceptor.ResourceIsolationPolicy.SEC_FETCH_DEST_HEADER; |
| 23 | +import static org.apache.struts2.interceptor.ResourceIsolationPolicy.SEC_FETCH_MODE_HEADER; |
| 24 | +import static org.apache.struts2.interceptor.ResourceIsolationPolicy.SEC_FETCH_SITE_HEADER; |
| 25 | +import static org.apache.struts2.interceptor.ResourceIsolationPolicy.VARY_HEADER; |
| 26 | +import static org.junit.Assert.assertNotEquals; |
| 27 | + |
| 28 | +import com.opensymphony.xwork2.ActionContext; |
| 29 | +import com.opensymphony.xwork2.XWorkTestCase; |
| 30 | +import com.opensymphony.xwork2.mock.MockActionInvocation; |
| 31 | +import org.apache.struts2.ServletActionContext; |
| 32 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 33 | +import org.springframework.mock.web.MockHttpServletResponse; |
| 34 | + |
| 35 | +import java.util.Arrays; |
| 36 | + |
| 37 | +public class FetchMetadataInterceptorTest extends XWorkTestCase { |
| 38 | + |
| 39 | + private final FetchMetadataInterceptor interceptor = new FetchMetadataInterceptor(); |
| 40 | + private final MockActionInvocation mai = new MockActionInvocation(); |
| 41 | + private final MockHttpServletRequest request = new MockHttpServletRequest(); |
| 42 | + private final MockHttpServletResponse response = new MockHttpServletResponse(); |
| 43 | + private static final String VARY_HEADER_VALUE = String.format( |
| 44 | + "%s,%s,%s", |
| 45 | + SEC_FETCH_DEST_HEADER, |
| 46 | + SEC_FETCH_SITE_HEADER, |
| 47 | + SEC_FETCH_MODE_HEADER |
| 48 | + ); |
| 49 | + |
| 50 | + @Override |
| 51 | + protected void setUp() throws Exception { |
| 52 | + super.setUp(); |
| 53 | + container.inject(interceptor); |
| 54 | + interceptor.setExemptedPaths("/foo,/bar"); |
| 55 | + ServletActionContext.setRequest(request); |
| 56 | + ServletActionContext.setResponse(response); |
| 57 | + ActionContext context = ServletActionContext.getActionContext(); |
| 58 | + mai.setInvocationContext(context); |
| 59 | + } |
| 60 | + |
| 61 | + public void testNoSite() throws Exception { |
| 62 | + request.removeHeader("sec-fetch-site"); |
| 63 | + |
| 64 | + assertNotEquals("Expected interceptor to accept this request", "403", |
| 65 | + interceptor.intercept(mai)); |
| 66 | + } |
| 67 | + |
| 68 | + public void testValidSite() throws Exception { |
| 69 | + for (String header : Arrays.asList("same-origin", "same-site", "none")){ |
| 70 | + request.addHeader("sec-fetch-site", header); |
| 71 | + |
| 72 | + assertNotEquals("Expected interceptor to accept this request", "403", |
| 73 | + interceptor.intercept(mai)); |
| 74 | + } |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + public void testValidTopLevelNavigation() throws Exception { |
| 79 | + request.addHeader("sec-fetch-mode", "navigate"); |
| 80 | + request.addHeader("sec-fetch-dest", "script"); |
| 81 | + request.setMethod("GET"); |
| 82 | + |
| 83 | + assertNotEquals("Expected interceptor to accept this request", "403", |
| 84 | + interceptor.intercept(mai)); |
| 85 | + } |
| 86 | + |
| 87 | + public void testInvalidTopLevelNavigation() throws Exception { |
| 88 | + for (String header : Arrays.asList("object", "embed")) { |
| 89 | + request.addHeader("sec-fetch-site", "foo"); |
| 90 | + request.addHeader("sec-fetch-mode", "navigate"); |
| 91 | + request.addHeader("sec-fetch-dest", header); |
| 92 | + request.setMethod("GET"); |
| 93 | + |
| 94 | + assertEquals("Expected interceptor to NOT accept this request", "403", interceptor.intercept(mai)); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + public void testPathInExemptedPaths() throws Exception { |
| 99 | + request.addHeader("sec-fetch-site", "foo"); |
| 100 | + request.setContextPath("/foo"); |
| 101 | + |
| 102 | + assertNotEquals("Expected interceptor to accept this request", "403", |
| 103 | + interceptor.intercept(mai)); |
| 104 | + } |
| 105 | + |
| 106 | + public void testPathNotInExemptedPaths() throws Exception { |
| 107 | + request.addHeader("sec-fetch-site", "foo"); |
| 108 | + request.setContextPath("/foobar"); |
| 109 | + |
| 110 | + assertEquals("Expected interceptor to NOT accept this request", "403", interceptor.intercept(mai)); |
| 111 | + } |
| 112 | + |
| 113 | + public void testVaryHeaderAcceptedReq() throws Exception { |
| 114 | + request.addHeader("sec-fetch-site", "foo"); |
| 115 | + request.setContextPath("/foo"); |
| 116 | + |
| 117 | + interceptor.intercept(mai); |
| 118 | + |
| 119 | + assertTrue("Expected vary header to be included", response.containsHeader(VARY_HEADER)); |
| 120 | + assertEquals("Expected different vary header value", response.getHeader(VARY_HEADER), VARY_HEADER_VALUE); |
| 121 | + } |
| 122 | + |
| 123 | + public void testVaryHeaderRejectedReq() throws Exception { |
| 124 | + request.addHeader("sec-fetch-site", "foo"); |
| 125 | + |
| 126 | + interceptor.intercept(mai); |
| 127 | + |
| 128 | + assertTrue("Expected vary header to be included", response.containsHeader(VARY_HEADER)); |
| 129 | + assertEquals("Expected different vary header value", response.getHeader(VARY_HEADER), VARY_HEADER_VALUE); |
| 130 | + } |
| 131 | +} |
0 commit comments