|
5 | 5 | */ |
6 | 6 | package io.jooby.internal.pac4j; |
7 | 7 |
|
| 8 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 9 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
8 | 10 | import static org.mockito.Mockito.*; |
9 | 11 |
|
10 | 12 | import org.junit.jupiter.api.BeforeEach; |
11 | 13 | import org.junit.jupiter.api.Test; |
| 14 | +import org.mockito.ArgumentCaptor; |
12 | 15 | import org.pac4j.core.client.Clients; |
13 | 16 | import org.pac4j.core.config.Config; |
| 17 | +import org.pac4j.core.context.WebContext; |
14 | 18 | import org.pac4j.core.http.url.UrlResolver; |
15 | 19 |
|
16 | 20 | import io.jooby.Context; |
@@ -98,4 +102,52 @@ void testApplyWithMissingQueryData() throws Exception { |
98 | 102 | && html.contains("action=\"/callback?client_name=FormClient\"") |
99 | 103 | && html.contains("value=\"\""))); |
100 | 104 | } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void shouldEscapeXssPayloads() throws Exception { |
| 108 | + Config config = mock(Config.class); |
| 109 | + Clients clients = mock(Clients.class); |
| 110 | + UrlResolver urlResolver = mock(UrlResolver.class); |
| 111 | + |
| 112 | + when(config.getClients()).thenReturn(clients); |
| 113 | + when(clients.getUrlResolver()).thenReturn(urlResolver); |
| 114 | + when(urlResolver.compute(anyString(), any(WebContext.class))).thenReturn("/callback"); |
| 115 | + |
| 116 | + Context ctx = mock(Context.class); |
| 117 | + Value errorNode = mock(Value.class); |
| 118 | + Value userNode = mock(Value.class); |
| 119 | + |
| 120 | + String xssError = "<script>alert('error')</script>"; |
| 121 | + String xssUser = "\"><script>alert('user')</script>"; |
| 122 | + |
| 123 | + when(ctx.query("error")).thenReturn(errorNode); |
| 124 | + when(errorNode.value("")).thenReturn(xssError); |
| 125 | + |
| 126 | + when(ctx.query("username")).thenReturn(userNode); |
| 127 | + when(userNode.value("")).thenReturn(xssUser); |
| 128 | + |
| 129 | + // Mock response chain |
| 130 | + when(ctx.setResponseType(MediaType.html)).thenReturn(ctx); |
| 131 | + |
| 132 | + // Capture the HTML argument passed to ctx.send() and return the ctx mock |
| 133 | + ArgumentCaptor<String> htmlCaptor = ArgumentCaptor.forClass(String.class); |
| 134 | + when(ctx.send(htmlCaptor.capture())).thenReturn(ctx); |
| 135 | + |
| 136 | + DevLoginForm form = new DevLoginForm(config, "/callback"); |
| 137 | + form.apply(ctx); |
| 138 | + |
| 139 | + // Retrieve the captured HTML |
| 140 | + String html = htmlCaptor.getValue(); |
| 141 | + |
| 142 | + // Assert inputs were sanitized |
| 143 | + assertFalse(html.contains(xssError), "Error payload was rendered unescaped!"); |
| 144 | + assertFalse(html.contains(xssUser), "Username payload was rendered unescaped!"); |
| 145 | + |
| 146 | + // Verify HTML entity encoding occurred (checking < and >) |
| 147 | + assertTrue(html.contains("<script>"), "Expected escaped <script> tag"); |
| 148 | + |
| 149 | + // Verify quotes were escaped in the value attribute injection |
| 150 | + assertTrue( |
| 151 | + html.contains("">") || html.contains("">"), "Expected escaped double quote"); |
| 152 | + } |
101 | 153 | } |
0 commit comments