GeezerVison is showing github.com — CORS: Correctly handle origin · netty/netty@afca14b
File tree
Expand file treeCollapse file tree
Open diff view settings
main/java/io/netty/handler/codec/http/cors test/java/io/netty/handler/codec/http/cors
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/cors/CorsHandler.java +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -155,7 +155,7 @@ private CorsConfig getForOrigin(String requestOrigin) { |
155 | 155 | if (corsConfig.origins().contains(requestOrigin)) { |
156 | 156 | return corsConfig; |
157 | 157 | } |
158 | | - if (corsConfig.isNullOriginAllowed() || NULL_ORIGIN.equals(requestOrigin)) { |
| 158 | + if (corsConfig.isNullOriginAllowed() && NULL_ORIGIN.equals(requestOrigin)) { |
159 | 159 | return corsConfig; |
160 | 160 | } |
161 | 161 | } |
|
Collapse file
Copy file name to clipboardExpand all lines: codec-http/src/test/java/io/netty/handler/codec/http/cors/CorsHandlerTest.java +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -665,6 +665,24 @@ public void preflightEmptyLastDiscardedThenNewRequestForwarded() { |
665 | 665 | assertFalse(ch.finish()); |
666 | 666 | } |
667 | 667 |
|
| 668 | + @Test |
| 669 | + public void shortCircuitWithNullOriginNotAllowedShouldBeForbidden() { |
| 670 | + final CorsConfig config = forOrigin("http://localhost:8080").shortCircuit().build(); |
| 671 | + final HttpResponse response = simpleRequest(config, "null"); |
| 672 | + assertEquals(FORBIDDEN, response.status()); |
| 673 | + assertEquals("0", response.headers().get(CONTENT_LENGTH)); |
| 674 | + assertTrue(ReferenceCountUtil.release(response)); |
| 675 | + } |
| 676 | + |
| 677 | + @Test |
| 678 | + public void shortCircuitWithNullOriginAllowedShouldSucceed() { |
| 679 | + final CorsConfig config = forOrigin("http://localhost:8080").allowNullOrigin().shortCircuit().build(); |
| 680 | + final HttpResponse response = simpleRequest(config, "null"); |
| 681 | + assertEquals(OK, response.status()); |
| 682 | + assertEquals("null", response.headers().get(ACCESS_CONTROL_ALLOW_ORIGIN)); |
| 683 | + assertTrue(ReferenceCountUtil.release(response)); |
| 684 | + } |
| 685 | +g |
668 | 686 | private static HttpResponse simpleRequest(final CorsConfig config, final String origin) { |
669 | 687 | return simpleRequest(config, origin, null); |
670 | 688 | } |
|