Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reject requests using the CONNECT method ( Fixes #312) #327

Merged
merged 3 commits into from
Oct 29, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright (C) 2013-2018 Expedia Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.hotels.styx.proxy.interceptors;

import com.hotels.styx.api.Eventual;
import com.hotels.styx.api.HttpInterceptor;
import com.hotels.styx.api.HttpMethod;
import com.hotels.styx.api.HttpResponseStatus;
import com.hotels.styx.api.LiveHttpRequest;
import com.hotels.styx.api.LiveHttpResponse;
import com.hotels.styx.routing.config.HttpInterceptorFactory;
import com.hotels.styx.routing.config.RouteHandlerDefinition;

import static com.hotels.styx.api.LiveHttpResponse.response;

/**
* A built-in interceptor to reject connections that try to use the CONNECT method to establish a TCP tunnel
* (see issue #312 https://github.com/HotelsDotCom/styx/issues/312).
*/
public class TcpTunnelRequestRejector implements HttpInterceptor {



@Override
public Eventual<LiveHttpResponse> intercept(LiveHttpRequest request, Chain chain) {

if (HttpMethod.CONNECT.equals(request.method())) {
return Eventual.of(response(HttpResponseStatus.METHOD_NOT_ALLOWED).build());
} else {
return chain.proceed(request);
}
}


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this is what needs to be done. However please follow styx coding standards, specifically use static imports with METHOD_NOT_ALLOWED, and CONNECT. Also I would remove the extra empty lines.

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.hotels.styx.routing.handlers.HttpInterceptorPipeline;
import com.hotels.styx.routing.handlers.ProxyToBackend;
import com.hotels.styx.routing.handlers.StaticResponseHandler;
import com.hotels.styx.proxy.interceptors.TcpTunnelRequestRejector;
import com.hotels.styx.routing.interceptors.RewriteInterceptor;

import java.util.Map;
Expand Down Expand Up @@ -90,11 +91,12 @@ private static HttpHandler styxHttpPipeline(StyxConfig config, HttpHandler inter
builder.add(new HttpMessageLoggingInterceptor(longFormatEnabled));
}

builder.add(new ConfigurationContextResolverInterceptor(EMPTY_CONFIGURATION_CONTEXT_RESOLVER));
builder.add(new UnexpectedRequestContentLengthRemover());
builder.add(new ViaHeaderAppendingInterceptor());
builder.add(new HopByHopHeadersRemovingInterceptor());
builder.add(new RequestEnrichingInterceptor(config.styxHeaderConfig()));
builder.add(new TcpTunnelRequestRejector()).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems that you use different convention with the dot at the end instead of at the front

add(new ConfigurationContextResolverInterceptor(EMPTY_CONFIGURATION_CONTEXT_RESOLVER)).
add(new UnexpectedRequestContentLengthRemover()).
add(new ViaHeaderAppendingInterceptor()).
add(new HopByHopHeadersRemovingInterceptor()).
add(new RequestEnrichingInterceptor(config.styxHeaderConfig()));

return new HttpInterceptorPipeline(builder.build(), interceptorsPipeline);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import com.hotels.styx.api.LiveHttpResponse.response
import com.hotels.styx.api.HttpResponseStatus._
import com.hotels.styx.api.HttpVersion._
import com.hotels.styx.common.FreePorts._
import com.hotels.styx.api.HttpResponse
import com.hotels.styx.api.{HttpMethod, HttpResponse}
import com.hotels.styx.client.StyxHeaderConfig.STYX_INFO_DEFAULT
import com.hotels.styx.client.{HttpClient, StyxHttpClient}
import com.hotels.styx.support.backends.FakeHttpServer
Expand Down Expand Up @@ -245,4 +245,16 @@ class ProxySpec extends FunSpec
}
}

describe("Handling CONNECT method") {
it("Should reject CONNECT requests") {
val request = get("/connectTest")
.addHeader(HOST, styxServer.proxyHost)
.method(HttpMethod.CONNECT)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static import if possible.

.build();

assert(decodedRequest(request).status() == METHOD_NOT_ALLOWED)
}

}

}