-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SEC-1790: Reject redirect locations containing CR or LF.
- Loading branch information
Showing
3 changed files
with
67 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
web/src/main/java/org/springframework/security/web/firewall/FirewalledResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package org.springframework.security.web.firewall; | ||
|
|
||
| import javax.servlet.http.HttpServletResponse; | ||
| import javax.servlet.http.HttpServletResponseWrapper; | ||
| import java.io.IOException; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| /** | ||
| * @author Luke Taylor | ||
| */ | ||
| class FirewalledResponse extends HttpServletResponseWrapper { | ||
| Pattern CR_OR_LF = Pattern.compile("\\r|\\n"); | ||
|
|
||
| public FirewalledResponse(HttpServletResponse response) { | ||
| super(response); | ||
| } | ||
|
|
||
| @Override | ||
| public void sendRedirect(String location) throws IOException { | ||
| // TODO: implement pluggable validation, instead of simple blacklisting. | ||
| // SEC-1790. Prevent redirects containing CRLF | ||
| if (CR_OR_LF.matcher(location).find()) { | ||
| throw new IllegalArgumentException("Invalid characters (CR/LF) in redirect location"); | ||
| } | ||
| super.sendRedirect(location); | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
web/src/test/java/org/springframework/security/web/firewall/FirewalledResponseTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package org.springframework.security.web.firewall; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.fail; | ||
|
|
||
| import org.junit.*; | ||
| import org.springframework.mock.web.MockHttpServletResponse; | ||
|
|
||
| /** | ||
| * @author Luke Taylor | ||
| */ | ||
| public class FirewalledResponseTests { | ||
|
|
||
| @Test | ||
| public void rejectsRedirectLocationContaingCRLF() throws Exception { | ||
| MockHttpServletResponse response = new MockHttpServletResponse(); | ||
| FirewalledResponse fwResponse = new FirewalledResponse(response); | ||
|
|
||
| fwResponse.sendRedirect("/theURL"); | ||
| assertEquals("/theURL", response.getRedirectedUrl()); | ||
|
|
||
| try { | ||
| fwResponse.sendRedirect("/theURL\r\nsomething"); | ||
| fail(); | ||
| } catch (IllegalArgumentException expected) { | ||
| } | ||
| try { | ||
| fwResponse.sendRedirect("/theURL\rsomething"); | ||
| fail(); | ||
| } catch (IllegalArgumentException expected) { | ||
| } | ||
|
|
||
| try { | ||
| fwResponse.sendRedirect("/theURL\nsomething"); | ||
| fail(); | ||
| } catch (IllegalArgumentException expected) { | ||
| } | ||
| } | ||
| } |