|
28 | 28 | import javax.servlet.http.HttpServletRequest; |
29 | 29 |
|
30 | 30 | import static org.apache.shiro.ee.util.JakartaTransformer.jakartify; |
| 31 | +import static org.assertj.core.api.Assertions.assertThat; |
31 | 32 | import static org.junit.jupiter.api.Assertions.assertEquals; |
32 | 33 | import static org.junit.jupiter.api.Assertions.assertFalse; |
33 | | -import static org.junit.jupiter.api.Assertions.assertNull; |
34 | 34 | import static org.junit.jupiter.api.Assertions.assertThrows; |
35 | 35 | import static org.junit.jupiter.api.Assertions.assertTrue; |
36 | 36 |
|
|
46 | 46 | * Resubmit forms support |
47 | 47 | */ |
48 | 48 | @ExtendWith(MockitoExtension.class) |
49 | | -public class FormSupportTest { |
| 49 | +class FormSupportTest { |
50 | 50 | @Mock |
51 | 51 | private HttpServletRequest request; |
52 | 52 |
|
53 | 53 | @Test |
54 | 54 | void nullReferer() { |
55 | 55 | when(request.getHeader("referer")).thenReturn(null); |
56 | | - assertNull(getReferer(request)); |
| 56 | + assertThat(getReferer(request)).isNull(); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void blankReferer() { |
| 61 | + when(request.getHeader("referer")).thenReturn(" "); |
| 62 | + assertThat(getReferer(request)).isNull(); |
57 | 63 | } |
58 | 64 |
|
59 | 65 | @Test |
60 | 66 | void plainStringReferer() { |
61 | 67 | when(request.getHeader("referer")).thenReturn("hello"); |
62 | | - assertEquals("hello", getReferer(request)); |
| 68 | + when(request.getContextPath()).thenReturn("/myapp"); |
| 69 | + assertThat(getReferer(request)).isNull(); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void malformedReferer() { |
| 74 | + when(request.getHeader("referer")).thenReturn("http://exa mple.com"); |
| 75 | + assertThat(getReferer(request)).isNull(); |
63 | 76 | } |
64 | 77 |
|
65 | 78 | @Test |
66 | | - void switchToHttps() { |
67 | | - when(request.getHeader("referer")).thenReturn("http://example.com"); |
68 | | - assertEquals("https://example.com", getReferer(request)); |
| 79 | + void refererWithinContextPath() { |
| 80 | + when(request.getHeader("referer")).thenReturn("https://example.com/myapp/login.xhtml"); |
| 81 | + when(request.getContextPath()).thenReturn("/myapp"); |
| 82 | + |
| 83 | + assertThat(getReferer(request)).isEqualTo("/myapp/login.xhtml"); |
69 | 84 | } |
70 | 85 |
|
71 | 86 | @Test |
72 | | - void dontSwitchToHttpsWhenCustomPort() { |
73 | | - when(request.getHeader("referer")).thenReturn("http://example.com:8080/"); |
74 | | - assertEquals("http://example.com:8080/", getReferer(request)); |
| 87 | + void refererWithinContextPathWithQuery() { |
| 88 | + when(request.getHeader("referer")).thenReturn("https://example.com/myapp/login.xhtml?a=1&b=2"); |
| 89 | + when(request.getContextPath()).thenReturn("/myapp"); |
| 90 | + |
| 91 | + assertThat(getReferer(request)).isEqualTo("/myapp/login.xhtml?a=1&b=2"); |
75 | 92 | } |
76 | 93 |
|
77 | 94 | @Test |
78 | | - void dontSwitchToHttpsWhenCustomPortNoTrailingSlash() { |
79 | | - when(request.getHeader("referer")).thenReturn("http://example.com:8080"); |
80 | | - assertEquals("http://example.com:8080", getReferer(request)); |
| 95 | + void refererEqualToContextPathBecomesRoot() { |
| 96 | + when(request.getHeader("referer")).thenReturn("https://example.com/myapp"); |
| 97 | + when(request.getContextPath()).thenReturn("/myapp"); |
| 98 | + |
| 99 | + assertThat(getReferer(request)).isEqualTo("/myapp"); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void refererOutsideContextPathIsRejected() { |
| 104 | + when(request.getHeader("referer")).thenReturn("https://example.com/otherapp/login.xhtml"); |
| 105 | + when(request.getContextPath()).thenReturn("/myapp"); |
| 106 | + |
| 107 | + assertThat(getReferer(request)).isNull(); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + void rootContextKeepsPath() { |
| 112 | + when(request.getHeader("referer")).thenReturn("https://example.com/login.xhtml"); |
| 113 | + when(request.getContextPath()).thenReturn(""); |
| 114 | + |
| 115 | + assertThat(getReferer(request)).isEqualTo("/login.xhtml"); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + void rootContextKeepsPathWithQuery() { |
| 120 | + when(request.getHeader("referer")).thenReturn("https://example.com/login.xhtml?x=1"); |
| 121 | + when(request.getContextPath()).thenReturn(""); |
| 122 | + |
| 123 | + assertThat(getReferer(request)).isEqualTo("/login.xhtml?x=1"); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + void normalizedPathWithinContextIsAccepted() { |
| 128 | + when(request.getHeader("referer")).thenReturn("https://example.com/myapp//foo/./bar.xhtml"); |
| 129 | + when(request.getContextPath()).thenReturn("/myapp"); |
| 130 | + |
| 131 | + assertThat(getReferer(request)).isEqualTo("/myapp/foo/bar.xhtml"); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + void normalizedPathEscapingContextIsRejected() { |
| 136 | + when(request.getHeader("referer")).thenReturn("https://example.com/myapp/../otherapp/page.xhtml"); |
| 137 | + when(request.getContextPath()).thenReturn("/myapp"); |
| 138 | + |
| 139 | + assertThat(getReferer(request)).isNull(); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + void opaqueUriRefererIsRejected() { |
| 144 | + when(request.getHeader("referer")).thenReturn("mailto:test@example.com"); |
| 145 | + when(request.getContextPath()).thenReturn("/myapp"); |
| 146 | + |
| 147 | + assertThat(getReferer(request)).isNull(); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + void javascriptUriRefererIsRejected() { |
| 152 | + when(request.getHeader("referer")).thenReturn("javascript:alert(1)"); |
| 153 | + when(request.getContextPath()).thenReturn("/myapp"); |
| 154 | + |
| 155 | + assertThat(getReferer(request)).isNull(); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + void contextPathPrefixMatchRequiresPathBoundary() { |
| 160 | + when(request.getHeader("referer")).thenReturn("https://example.com/myapplication/page.xhtml"); |
| 161 | + when(request.getContextPath()).thenReturn("/myapp"); |
| 162 | + |
| 163 | + assertThat(getReferer(request)).isNull(); |
| 164 | + } |
| 165 | + |
| 166 | + @Test |
| 167 | + void refererWithFragmentDropsFragmentAndKeepsQueryOnly() { |
| 168 | + when(request.getHeader("referer")).thenReturn("https://example.com/myapp/page.xhtml?a=1#frag"); |
| 169 | + when(request.getContextPath()).thenReturn("/myapp"); |
| 170 | + |
| 171 | + assertThat(getReferer(request)).isEqualTo("/myapp/page.xhtml?a=1"); |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + void externalHostWithMatchingContextCurrentlyPasses() { |
| 176 | + when(request.getHeader("referer")).thenReturn("https://attacker.example/myapp/login.xhtml"); |
| 177 | + when(request.getContextPath()).thenReturn("/myapp"); |
| 178 | + |
| 179 | + assertThat(getReferer(request)).isEqualTo("/myapp/login.xhtml"); |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + void encodedPathTraversalRefererIsRejected() { |
| 184 | + when(request.getHeader("referer")) |
| 185 | + .thenReturn("https://example.com/myapp/%2e%2e/otherapp/page.xhtml"); |
| 186 | + when(request.getContextPath()).thenReturn("/myapp"); |
| 187 | + |
| 188 | + assertThat(getReferer(request)).isNull(); |
| 189 | + } |
| 190 | + |
| 191 | + @Test |
| 192 | + void encodedPathTraversalWithEncodedSlashesRefererIsRejected() { |
| 193 | + when(request.getHeader("referer")) |
| 194 | + .thenReturn("https://example.com/myapp/%2e%2e%2fotherapp%2fpage.xhtml"); |
| 195 | + when(request.getContextPath()).thenReturn("/myapp"); |
| 196 | + |
| 197 | + assertThat(getReferer(request)).isNull(); |
81 | 198 | } |
82 | 199 |
|
83 | 200 | @Test |
|
0 commit comments