Skip to content

Commit

Permalink
Merge pull request #104 from brucehoff/IT-3558
Browse files Browse the repository at this point in the history
IT-3558: Set Strict-Transport-Security on HTTPS responses, as requested by Pentester
  • Loading branch information
brucehoff authored Apr 24, 2024
2 parents a27940c + 4f596fa commit b59bf9f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/main/java/synapseawsconsolelogin/Auth.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@
public class Auth extends HttpServlet {
private static Logger logger = Logger.getLogger("Auth");

private static final String StrictTransportSecurityHeaderName = "Strict-Transport-Security";
private static final String StrictTransportSecurityHeaderValue = "max-age=31536000; includeSubDomains";

private static final String TEAM_CLAIM_NAME = "team";

// templates for constructing the 'claims' part of the OIDC authorization request
Expand Down Expand Up @@ -368,6 +371,9 @@ public String getAuthorizeUrl(String state) {
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
if (req.isSecure()) {
resp.setHeader(StrictTransportSecurityHeaderName, StrictTransportSecurityHeaderValue);
}
resp.setContentType("text/plain");
try (ServletOutputStream os=resp.getOutputStream()) {
os.println("Not found.");
Expand Down Expand Up @@ -405,11 +411,14 @@ static int synapseExceptionStatus(SynapseServerException e) {
// and we don't expect others. For the rest we'll just return 500.
return 500;
}

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
try {
if (req.isSecure()) {
resp.setHeader(StrictTransportSecurityHeaderName, StrictTransportSecurityHeaderValue);
}
doGetIntern(req, resp);
} catch (Exception e) {
handleException(e, resp);
Expand Down
60 changes: 57 additions & 3 deletions src/test/java/synapseawsconsolelogin/AuthTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -669,4 +670,57 @@ public void testDoGet_PersonalAccessToken() throws Exception {
verify(mockHttpResponse).setContentType("application/force-download");
}

private static void hstsIsSet(HttpServletResponse mockHttpResponse) {
verify(mockHttpResponse).setHeader("Strict-Transport-Security", "max-age=31536000; includeSubDomains");
}

private static void hstsIsNOTSet(HttpServletResponse mockHttpResponse) {
verify(mockHttpResponse, never()).setHeader(eq("Strict-Transport-Security"), anyString());
}

@Test
public void testHSTSSecureGet() throws Exception {
mockIncomingUrl("https://www.foo.com", "/unknown");
when (mockHttpRequest.isSecure()).thenReturn(true);

// method under test
auth.doGet(mockHttpRequest, mockHttpResponse);

hstsIsSet(mockHttpResponse);
}

@Test
public void testHSTSSecurePost() throws Exception {
mockIncomingUrl("https://www.foo.com", "/unknown");
when (mockHttpRequest.isSecure()).thenReturn(true);

// method under test
auth.doPost(mockHttpRequest, mockHttpResponse);

hstsIsSet(mockHttpResponse);
}

@Test
public void testHSTSInsecureGet() throws Exception {
mockIncomingUrl("http://www.foo.com", "/unknown");
when (mockHttpRequest.isSecure()).thenReturn(false);

// method under test
auth.doGet(mockHttpRequest, mockHttpResponse);

hstsIsNOTSet(mockHttpResponse);
}

@Test
public void testHSTSInsecurePost() throws Exception {
mockIncomingUrl("http://www.foo.com", "/unknown");
when (mockHttpRequest.isSecure()).thenReturn(false);

// method under test
auth.doPost(mockHttpRequest, mockHttpResponse);

hstsIsNOTSet(mockHttpResponse);
}


}

0 comments on commit b59bf9f

Please sign in to comment.