brbzull0 commented on code in PR #13908:
URL: https://github.com/apache/trafficserver/pull/13908#discussion_r4548743480


##########
src/iocore/net/SNIActionPerformer.cc:
##########
@@ -299,10 +314,19 @@ int
 VerifyClient::SNIAction(SSL &ssl, const Context & /* ctx ATS_UNUSED */) const
 {
   auto snis   = TLSSNISupport::getInstance(&ssl);
-  auto ssl_vc = SSLNetVCAccess(&ssl);
-
-  if (snis == nullptr || ssl_vc == nullptr) {
-    return SSL_TLSEXT_ERR_OK;
+  auto ssl_vc = dynamic_cast<SSLNetVConnection *>(snis);
+
+  if (ssl_vc == nullptr) {
+    // The CA override is kept on the SSLNetVConnection for the certificate 
callback, and
+    // ssl_verify_client_callback() looks the connection up with 
SSLNetVCAccess(), so a client
+    // certificate can't be checked as configured on any other connection. 
Refuse rather than let the
+    // default verification stand in. NONE needs neither.
+    if (this->mode == 0) {

Review Comment:
   Agreed. It now refuses only when `verify_client_ca_certs` is set, and 
otherwise sets the level on the `SSL` object. The comment gives that reason.
   



##########
src/iocore/net/SNIActionPerformer.cc:
##########
@@ -185,9 +194,15 @@ TunnelDestination::SNIAction(SSL &ssl, const Context &ctx) 
const
   auto snis      = TLSSNISupport::getInstance(&ssl);
   auto tuns      = TLSTunnelSupport::getInstance(&ssl);
   auto alpns     = ALPNSupport::getInstance(&ssl);
-  auto ssl_netvc = SSLNetVCAccess(&ssl);
+  auto ssl_netvc = dynamic_cast<SSLNetVConnection *>(snis);
 
-  if (snis == nullptr || tuns == nullptr || alpns == nullptr || ssl_netvc == 
nullptr) {
+  if (ssl_netvc == nullptr) {
+    // Only an SSLNetVConnection can be tunnelled, so refuse rather than serve 
the name here.
+    Dbg(dbg_ctl_ssl_sni, "routing to %s needs an SSLNetVConnection - denying 
connection", destination.c_str());

Review Comment:
   The notes say these apply only to TLS over TCP, not that HTTP/3 is refused, 
because no HTTP/3 connection reaches the SNI actions yet. Today an HTTP/3 
connection for such a name runs none of the entry's actions, with or without 
this PR. The refusal takes effect once HTTP/3 runs them, which is a separate 
change, so I'll add that note there.
   
   Why HTTP/3 doesn't reach them, on current master:
   
   - OpenSSL QUIC: `perform_sni_action()` is called only from 
[`ssl_client_hello_callback()`](https://github.com/apache/trafficserver/blob/1991c3c1152231b8de8fc42e671b467dad136eb6/src/iocore/net/SSLUtils.cc#L570)
 and 
[`ssl_servername_callback()`](https://github.com/apache/trafficserver/blob/1991c3c1152231b8de8fc42e671b467dad136eb6/src/iocore/net/SSLUtils.cc#L779).
 
[`_set_handshake_callbacks()`](https://github.com/apache/trafficserver/blob/1991c3c1152231b8de8fc42e671b467dad136eb6/src/iocore/net/QUICMultiCertConfigLoader.cc#L282-L298)
 replaces both, and `ssl_cert_callback()`, with callbacks that only record the 
name and select the certificate.
   - quiche: keeps the default callbacks, but 
[`perform_sni_action()`](https://github.com/apache/trafficserver/blob/1991c3c1152231b8de8fc42e671b467dad136eb6/src/iocore/net/TLSSNISupport.cc#L94-L96)
 looks the name up together with the local port, and 
[`QUICNetVConnection::set_local_addr()`](https://github.com/apache/trafficserver/blob/1991c3c1152231b8de8fc42e671b467dad136eb6/src/iocore/net/QUICNetVConnection.cc#L128-L131)
 is empty, so the port is 0. `sni.yaml` port ranges start at 1 
([default](https://github.com/apache/trafficserver/blob/1991c3c1152231b8de8fc42e671b467dad136eb6/src/iocore/net/YamlSNIConfig.cc#L289),
 
[check](https://github.com/apache/trafficserver/blob/1991c3c1152231b8de8fc42e671b467dad136eb6/src/iocore/net/YamlSNIConfig.cc#L557)),
 so no entry matches.
   
   <details><summary>Checked on a quiche build</summary>
   
   Master at 0c2aaed489 and this branch give the same result. `sni.yaml` has 
`block.me.com` with `ip_allow: 192.168.10.1` and `allow.me.com` with `ip_allow: 
127.0.0.1`, and the client connects from 127.0.0.1.
   
   | | `allow.me.com` | `block.me.com` |
   |---|---|---|
   | TLS over TCP | 200 | handshake failure |
   | HTTP/3 | 200 | 200 |
   
   The debug log has `allow.me.com:0 not available in the map` and 
`block.me.com:0 not available in the map`.
   
   </details>
   



##########
src/iocore/net/unit_tests/test_SNIActionPerformer.cc:
##########
@@ -0,0 +1,529 @@
+/** @file
+
+  Catch based unit tests for the SNI actions that act on the connection.
+
+  @section license License
+
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you 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.
+ */
+
+#include "../SNIActionPerformer.h"
+#include "../P_SSLNetVConnection.h"
+#include "../P_SSLUtils.h"
+#include "../P_UnixNetVConnection.h"
+#include "iocore/net/TLSALPNSupport.h"
+#include "iocore/net/TLSSNISupport.h"
+#include "iocore/net/TLSTunnelSupport.h"
+#include "proxy/IPAllow.h"
+#include "records/RecHttp.h"
+#include "tscore/ink_inet.h"
+
+#if TS_USE_QUIC == 1
+#include "../P_QUICNetVConnection.h"
+#endif
+
+#include <catch2/catch_test_macros.hpp>
+
+#include <openssl/ssl.h>
+
+#include <algorithm>
+#include <initializer_list>
+#include <iterator>
+#include <memory>
+#include <string>
+#include <type_traits>
+#include <vector>
+
+#if TS_USE_QUIC == 1
+// The look-alike connections below model QUICNetVConnection, so 
QUICNetVConnection must keep this shape.
+static_assert(std::is_convertible_v<QUICNetVConnection *, TLSSNISupport *>);
+static_assert(std::is_convertible_v<QUICNetVConnection *, NetVConnection *>);
+static_assert(!std::is_base_of_v<SSLNetVConnection, QUICNetVConnection>);
+#endif
+
+namespace
+{
+IpEndpoint
+make_endpoint(const char *ip)
+{
+  IpEndpoint ep;
+
+  REQUIRE(ats_ip_pton(ip, &ep) == 0);
+  return ep;
+}
+
+// A connection that binds TLSSNISupport and ALPNSupport but is not an 
SSLNetVConnection, the way
+// QUICNetVConnection is.
+class NonSSLNetVConnection : public UnixNetVConnection, public ALPNSupport, 
public TLSSNISupport
+{
+public:
+  void
+  set_peer(const char *ip)
+  {
+    IpEndpoint ep = make_endpoint(ip);
+
+    set_remote_addr(&ep.sa);
+    got_remote_addr = true;
+  }
+
+  void
+  bind_to(SSL *ssl)
+  {
+    ALPNSupport::bind(ssl, this);
+    TLSSNISupport::bind(ssl, this);
+  }
+
+protected:
+  in_port_t
+  _get_local_port() override
+  {
+    return 443;
+  }
+};
+
+// The same, but also binding TLSTunnelSupport, so nothing but the connection 
type stops a tunnel.
+class NonSSLTunnelNetVConnection : public NonSSLNetVConnection, public 
TLSTunnelSupport
+{
+public:
+  void
+  bind_to(SSL *ssl)
+  {
+    NonSSLNetVConnection::bind_to(ssl);
+    TLSTunnelSupport::bind(ssl, this);
+  }
+};
+
+class PeerSSLNetVConnection : public SSLNetVConnection
+{
+public:
+  void
+  set_peer(const char *ip)
+  {
+    IpEndpoint ep = make_endpoint(ip);
+
+    set_remote_addr(&ep.sa);
+    got_remote_addr = true;
+  }
+
+  // What _bindSSLObject() binds for the slots the actions read.
+  void
+  bind_to(SSL *ssl)
+  {
+    this->ssl = ssl;
+    SSLNetVCAttach(ssl, this);
+    ALPNSupport::bind(ssl, this);
+    TLSSNISupport::bind(ssl, this);
+    TLSTunnelSupport::bind(ssl, this);
+  }
+
+  // The SSL object belongs to the test.
+  ~PeerSSLNetVConnection() override { this->ssl = nullptr; }
+};
+
+// TLSSNISupport bound on its own, with no connection behind it.
+class DetachedSNISupport : public TLSSNISupport
+{
+protected:
+  in_port_t
+  _get_local_port() override
+  {
+    return 443;
+  }
+};
+
+struct SSLDeleter {
+  void
+  operator()(SSL *ssl) const
+  {
+    SSL_free(ssl);
+  }
+};
+
+struct SSLCtxDeleter {
+  void
+  operator()(SSL_CTX *ctx) const
+  {
+    SSL_CTX_free(ctx);
+  }
+};
+
+// Declare the connection before this, so the connection outlives the SSL 
object that points at it.
+class SSLObject
+{
+public:
+  SSLObject()
+  {
+    // The support classes reserve their ex_data slots here, and they assert 
on a second call.
+    [[maybe_unused]] static bool const initialized = (SSLInitializeLibrary(), 
true);
+
+    _ctx.reset(SSL_CTX_new(TLS_server_method()));
+    REQUIRE(_ctx != nullptr);
+    _ssl.reset(SSL_new(_ctx.get()));
+    REQUIRE(_ssl != nullptr);
+  }
+
+  SSL *
+  get() const
+  {
+    return _ssl.get();
+  }
+
+  SSL &
+  operator*() const
+  {
+    return *_ssl;
+  }
+
+private:
+  std::unique_ptr<SSL_CTX, SSLCtxDeleter> _ctx;
+  std::unique_ptr<SSL, SSLDeleter>        _ssl;
+};
+
+// IpAllow::subjects is process-wide; set it for one test and put it back 
afterwards.
+class ScopedAclSubjects
+{
+public:
+  ScopedAclSubjects(std::initializer_list<uint8_t> subjects)
+  {
+    REQUIRE(subjects.size() <= IpAllow::Subject::MAX_SUBJECTS);
+    std::copy(std::begin(IpAllow::subjects), std::end(IpAllow::subjects), 
std::begin(_saved));
+    std::fill(std::begin(IpAllow::subjects), std::end(IpAllow::subjects), 
IpAllow::Subject::MAX_SUBJECTS);
+    std::copy(subjects.begin(), subjects.end(), std::begin(IpAllow::subjects));
+  }
+
+  ~ScopedAclSubjects() { std::copy(std::begin(_saved), std::end(_saved), 
std::begin(IpAllow::subjects)); }
+
+private:
+  uint8_t _saved[IpAllow::Subject::MAX_SUBJECTS];
+};
+
+const std::string         server_name{"allowed.example.com"};
+const ActionItem::Context no_context{};
+
+// TunnelDestination keeps a reference to its ALPN list, so the list has to 
outlive the action.
+const std::vector<int> no_alpn{};
+} // end anonymous namespace
+
+TEST_CASE("SNI_IpAllow checks the peer of a connection that is not an 
SSLNetVConnection", "[sni][ip_allow]")

Review Comment:
   QUIC doesn't reach the SNI actions yet 
([details](https://github.com/apache/trafficserver/pull/13908#discussion_r4514217347)),
 so that test would see both clients served. For the same reason these actions 
can't crash on master: none of them runs. I'd add the test with the change that 
makes HTTP/3 run the SNI actions. The new unit test uses a real 
`QUICNetVConnection`.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to