|
43 | 43 | import org.elasticsearch.rest.RestChannel;
|
44 | 44 | import org.elasticsearch.rest.RestRequest;
|
45 | 45 | import org.elasticsearch.rest.RestResponse;
|
| 46 | +import org.elasticsearch.rest.RestStatus; |
46 | 47 | import org.elasticsearch.test.rest.FakeRestRequest;
|
47 | 48 | import org.elasticsearch.threadpool.TestThreadPool;
|
48 | 49 | import org.elasticsearch.threadpool.ThreadPool;
|
@@ -681,4 +682,170 @@ public void dispatchBadRequest(final RestChannel channel, final ThreadContext th
|
681 | 682 | testThreadPool.shutdownNow();
|
682 | 683 | }
|
683 | 684 | }
|
| 685 | + |
| 686 | + public void testOptionsRequestsFailWith400AndNoAuthn() throws Exception { |
| 687 | + final Settings settings = Settings.builder().put(env.settings()).build(); |
| 688 | + AtomicReference<Throwable> badRequestCauseReference = new AtomicReference<>(); |
| 689 | + final HttpServerTransport.Dispatcher dispatcher = new HttpServerTransport.Dispatcher() { |
| 690 | + @Override |
| 691 | + public void dispatchRequest(final RestRequest request, final RestChannel channel, final ThreadContext threadContext) { |
| 692 | + logger.error("--> Unexpected dispatched request [" + FakeRestRequest.requestToString(channel.request()) + "]"); |
| 693 | + throw new AssertionError("Unexpected dispatched request"); |
| 694 | + } |
| 695 | + |
| 696 | + @Override |
| 697 | + public void dispatchBadRequest(final RestChannel channel, final ThreadContext threadContext, final Throwable cause) { |
| 698 | + badRequestCauseReference.set(cause); |
| 699 | + } |
| 700 | + }; |
| 701 | + final ThreadPool testThreadPool = new TestThreadPool(TEST_MOCK_TRANSPORT_THREAD_PREFIX); |
| 702 | + try ( |
| 703 | + Netty4HttpServerTransport transport = Security.getHttpServerTransportWithHeadersValidator( |
| 704 | + settings, |
| 705 | + new NetworkService(List.of()), |
| 706 | + testThreadPool, |
| 707 | + xContentRegistry(), |
| 708 | + dispatcher, |
| 709 | + randomClusterSettings(), |
| 710 | + new SharedGroupFactory(settings), |
| 711 | + Tracer.NOOP, |
| 712 | + TLSConfig.noTLS(), |
| 713 | + null, |
| 714 | + (httpPreRequest, channel, listener) -> { |
| 715 | + throw new AssertionError("should not be invoked for OPTIONS requests"); |
| 716 | + }, |
| 717 | + (httpPreRequest, channel, listener) -> { |
| 718 | + throw new AssertionError("should not be invoked for OPTIONS requests with a body"); |
| 719 | + } |
| 720 | + ) |
| 721 | + ) { |
| 722 | + final ChannelHandler handler = transport.configureServerChannelHandler(); |
| 723 | + final EmbeddedChannel ch = new EmbeddedChannel(handler); |
| 724 | + // OPTIONS request with fixed length content written in one chunk |
| 725 | + { |
| 726 | + ByteBuf buf = ch.alloc().buffer(); |
| 727 | + ByteBufUtil.copy(AsciiString.of("OPTIONS /url/whatever/fixed-length-single-chunk HTTP/1.1"), buf); |
| 728 | + buf.writeByte(HttpConstants.LF); |
| 729 | + if (randomBoolean()) { |
| 730 | + ByteBufUtil.copy(AsciiString.of("Host: localhost"), buf); |
| 731 | + buf.writeByte(HttpConstants.LF); |
| 732 | + } |
| 733 | + if (randomBoolean()) { |
| 734 | + ByteBufUtil.copy(AsciiString.of("Accept: */*"), buf); |
| 735 | + buf.writeByte(HttpConstants.LF); |
| 736 | + } |
| 737 | + if (randomBoolean()) { |
| 738 | + ByteBufUtil.copy(AsciiString.of("Content-Encoding: gzip"), buf); |
| 739 | + buf.writeByte(HttpConstants.LF); |
| 740 | + } |
| 741 | + if (randomBoolean()) { |
| 742 | + ByteBufUtil.copy( |
| 743 | + AsciiString.of("Content-Type: " + randomFrom("text/plain; charset=utf-8", "application/json; charset=utf-8")), |
| 744 | + buf |
| 745 | + ); |
| 746 | + buf.writeByte(HttpConstants.LF); |
| 747 | + } |
| 748 | + String content = randomAlphaOfLengthBetween(4, 1024); |
| 749 | + // having a "Content-Length" request header is what makes it "fixed length" |
| 750 | + ByteBufUtil.copy(AsciiString.of("Content-Length: " + content.length()), buf); |
| 751 | + buf.writeByte(HttpConstants.LF); |
| 752 | + // end of headers |
| 753 | + buf.writeByte(HttpConstants.LF); |
| 754 | + ByteBufUtil.copy(AsciiString.of(content), buf); |
| 755 | + // write everything in one single chunk |
| 756 | + testThreadPool.generic().submit(() -> { |
| 757 | + ch.writeInbound(buf); |
| 758 | + ch.flushInbound(); |
| 759 | + }).get(); |
| 760 | + ch.runPendingTasks(); |
| 761 | + Throwable badRequestCause = badRequestCauseReference.get(); |
| 762 | + assertThat(badRequestCause, instanceOf(HttpHeadersValidationException.class)); |
| 763 | + assertThat(badRequestCause.getCause(), instanceOf(ElasticsearchException.class)); |
| 764 | + assertThat(((ElasticsearchException) badRequestCause.getCause()).status(), is(RestStatus.BAD_REQUEST)); |
| 765 | + assertThat( |
| 766 | + ((ElasticsearchException) badRequestCause.getCause()).getDetailedMessage(), |
| 767 | + containsString("OPTIONS requests with a payload body are not supported") |
| 768 | + ); |
| 769 | + } |
| 770 | + { |
| 771 | + ByteBuf buf = ch.alloc().buffer(); |
| 772 | + ByteBufUtil.copy(AsciiString.of("OPTIONS /url/whatever/chunked-transfer?encoding HTTP/1.1"), buf); |
| 773 | + buf.writeByte(HttpConstants.LF); |
| 774 | + if (randomBoolean()) { |
| 775 | + ByteBufUtil.copy(AsciiString.of("Host: localhost"), buf); |
| 776 | + buf.writeByte(HttpConstants.LF); |
| 777 | + } |
| 778 | + if (randomBoolean()) { |
| 779 | + ByteBufUtil.copy(AsciiString.of("Accept: */*"), buf); |
| 780 | + buf.writeByte(HttpConstants.LF); |
| 781 | + } |
| 782 | + if (randomBoolean()) { |
| 783 | + ByteBufUtil.copy(AsciiString.of("Content-Encoding: gzip"), buf); |
| 784 | + buf.writeByte(HttpConstants.LF); |
| 785 | + } |
| 786 | + if (randomBoolean()) { |
| 787 | + ByteBufUtil.copy( |
| 788 | + AsciiString.of("Content-Type: " + randomFrom("text/plain; charset=utf-8", "application/json; charset=utf-8")), |
| 789 | + buf |
| 790 | + ); |
| 791 | + buf.writeByte(HttpConstants.LF); |
| 792 | + } |
| 793 | + // do not write a "Content-Length" header to make the request "variable length" |
| 794 | + if (randomBoolean()) { |
| 795 | + ByteBufUtil.copy(AsciiString.of("Transfer-Encoding: " + randomFrom("chunked", "gzip, chunked")), buf); |
| 796 | + } else { |
| 797 | + ByteBufUtil.copy(AsciiString.of("Transfer-Encoding: chunked"), buf); |
| 798 | + } |
| 799 | + buf.writeByte(HttpConstants.LF); |
| 800 | + buf.writeByte(HttpConstants.LF); |
| 801 | + // maybe append some chunks as well |
| 802 | + String[] contentParts = randomArray(0, 4, String[]::new, () -> randomAlphaOfLengthBetween(1, 64)); |
| 803 | + for (String content : contentParts) { |
| 804 | + ByteBufUtil.copy(AsciiString.of(Integer.toHexString(content.length())), buf); |
| 805 | + buf.writeByte(HttpConstants.CR); |
| 806 | + buf.writeByte(HttpConstants.LF); |
| 807 | + ByteBufUtil.copy(AsciiString.of(content), buf); |
| 808 | + buf.writeByte(HttpConstants.CR); |
| 809 | + buf.writeByte(HttpConstants.LF); |
| 810 | + } |
| 811 | + testThreadPool.generic().submit(() -> { |
| 812 | + ch.writeInbound(buf); |
| 813 | + ch.flushInbound(); |
| 814 | + }).get(); |
| 815 | + // append some more chunks as well |
| 816 | + ByteBuf buf2 = ch.alloc().buffer(); |
| 817 | + contentParts = randomArray(1, 4, String[]::new, () -> randomAlphaOfLengthBetween(1, 64)); |
| 818 | + for (String content : contentParts) { |
| 819 | + ByteBufUtil.copy(AsciiString.of(Integer.toHexString(content.length())), buf2); |
| 820 | + buf2.writeByte(HttpConstants.CR); |
| 821 | + buf2.writeByte(HttpConstants.LF); |
| 822 | + ByteBufUtil.copy(AsciiString.of(content), buf2); |
| 823 | + buf2.writeByte(HttpConstants.CR); |
| 824 | + buf2.writeByte(HttpConstants.LF); |
| 825 | + } |
| 826 | + // finish chunked request |
| 827 | + ByteBufUtil.copy(AsciiString.of("0"), buf2); |
| 828 | + buf2.writeByte(HttpConstants.CR); |
| 829 | + buf2.writeByte(HttpConstants.LF); |
| 830 | + buf2.writeByte(HttpConstants.CR); |
| 831 | + buf2.writeByte(HttpConstants.LF); |
| 832 | + testThreadPool.generic().submit(() -> { |
| 833 | + ch.writeInbound(buf2); |
| 834 | + ch.flushInbound(); |
| 835 | + }).get(); |
| 836 | + ch.runPendingTasks(); |
| 837 | + Throwable badRequestCause = badRequestCauseReference.get(); |
| 838 | + assertThat(badRequestCause, instanceOf(HttpHeadersValidationException.class)); |
| 839 | + assertThat(badRequestCause.getCause(), instanceOf(ElasticsearchException.class)); |
| 840 | + assertThat(((ElasticsearchException) badRequestCause.getCause()).status(), is(RestStatus.BAD_REQUEST)); |
| 841 | + assertThat( |
| 842 | + ((ElasticsearchException) badRequestCause.getCause()).getDetailedMessage(), |
| 843 | + containsString("OPTIONS requests with a payload body are not supported") |
| 844 | + ); |
| 845 | + } |
| 846 | + } finally { |
| 847 | + testThreadPool.shutdownNow(); |
| 848 | + } |
| 849 | + } |
| 850 | + |
684 | 851 | }
|
0 commit comments