Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
a42fe41
Add failing WebSocket frame parser tests
entlein Nov 19, 2026
ea3ebbe
Parse WebSocket frames
entlein Nov 19, 2026
20b0b0c
Add failing WebSocket message stitcher tests
entlein Nov 19, 2026
a84af5b
Stitch WebSocket frames into messages
entlein Nov 19, 2026
46738f8
Name the WebSocket record type in stitcher tests
entlein Nov 19, 2026
5e23920
Add failing HTTP upgrade to WebSocket handover tests
entlein Nov 19, 2026
354b695
Hand an upgraded HTTP connection over to the WebSocket parser
entlein Nov 19, 2026
008eb2d
Add failing websocket_events table test
entlein Nov 19, 2026
89c7e50
Write WebSocket messages to websocket_events
entlein Nov 19, 2026
fdd58a6
Declare that WebSocket streams have no frame boundary to resync on
entlein Nov 19, 2026
855c788
Fix include order in the HTTP parser
entlein Nov 19, 2026
446f449
Expect websocket_events rows in timestamp order
entlein Nov 19, 2026
1cb1453
Format the WebSocket connector test
entlein Nov 19, 2026
8d171e6
Add failing test for escaped non-text WebSocket payloads
entlein Nov 19, 2026
bcc3138
Escape non-text WebSocket payloads
entlein Nov 19, 2026
a85554f
Test WebSocket subprotocol and Kubernetes exec session decoding
entlein Nov 19, 2026
d9138af
Decode Kubernetes exec sessions in WebSocket traces
entlein Nov 19, 2026
068f942
Test tar manifests and non-TTY stdin in Kubernetes exec sessions
entlein Nov 19, 2026
5eea991
Summarize tar streams and non-TTY stdin in Kubernetes exec sessions
entlein Nov 19, 2026
c53fc50
Initialize WebSocket records field by field
entlein Nov 19, 2026
188a368
format
entlein Nov 19, 2026
a65d8a2
Test bounded WebSocket payloads, resync and exec input redaction
entlein Nov 19, 2026
c64f1e9
Bound WebSocket payload capture and redact unechoed exec input
entlein Nov 19, 2026
e8124da
format
entlein Nov 19, 2026
43aa054
Add a WebSocket trace BPF test with plain and TLS echo sessions
entlein Nov 19, 2026
49ca006
Test flushing Kubernetes exec sessions that close without a status
entlein Nov 19, 2026
a75b459
Flush Kubernetes exec sessions that close without a status
entlein Nov 19, 2026
c8edd77
format
entlein Nov 19, 2026
1546037
Check the HTTP upgrade and trace the server in the WebSocket BPF test
entlein Nov 19, 2026
3ee7495
format
entlein Nov 19, 2026
d981d7d
Test that credentials are redacted before they reach tables
entlein Nov 20, 2026
403594a
Redact credentials in HTTP and WebSocket rows
entlein Nov 20, 2026
347b85f
Test that bearer tokens in WebSocket subprotocols are redacted
entlein Nov 20, 2026
3db81b1
Redact bearer tokens in WebSocket subprotocols
entlein Nov 20, 2026
cb2aadd
Test that HTTP2 bodies follow the body size limit
entlein Nov 20, 2026
d4d5ef1
Keep HTTP2 bodies up to the body size limit
entlein Nov 20, 2026
7d27812
Test that TLS handshakes report version, cipher, group and ALPN
entlein Nov 20, 2026
c19395d
Report TLS version, cipher suite, key share group and ALPN
entlein Nov 20, 2026
6c38eb8
Test that TLS handshakes hand over to the decrypted protocol
entlein Nov 20, 2026
e9d45f2
Hand TLS connections over to the decrypted protocol
entlein Nov 20, 2026
f4f0b85
Test that decrypted HTTP is kept when TLS tracing is on
entlein Nov 20, 2026
c1e3c8a
Test that ClientHellos report offered groups
entlein Nov 20, 2026
2e2c63c
Report the groups a TLS client offers
entlein Nov 20, 2026
4e1a6ea
Alias the TLS protocol namespace in conn tracker tests
entlein Nov 20, 2026
aa70605
Test that a TLS connection closed in one poll yields its decrypted HT…
entlein Nov 20, 2026
80c64c6
Transfer handed-over TLS connections in the same iteration
entlein Nov 20, 2026
44cb3e8
Send all TLS records to user space
entlein Nov 20, 2026
e9eb9e3
Format the socket tracer sources
entlein Nov 20, 2026
d9b74d0
Drop a redundant include from the HTTP parser
entlein Nov 20, 2026
d6c8416
Test that WebSocket records carry the request path
entlein Nov 20, 2026
75022c0
Add the request path to WebSocket records
entlein Nov 20, 2026
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
2 changes: 2 additions & 0 deletions src/carnot/funcs/protocols/protocols.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ inline std::string ProtocolName(int protocol) {
return "Kafka";
case px::protocols::Protocol::kAMQP:
return "AMQP";
case px::protocols::Protocol::kWebSocket:
return "WebSocket";
default:
return absl::StrCat("Invalid (", protocol, ")");
}
Expand Down
1 change: 1 addition & 0 deletions src/shared/protocols/protocols.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ enum class Protocol {
kMux = 11,
kAMQP = 12,
kTLS = 13,
kWebSocket = 14,
};

} // namespace protocols
Expand Down
15 changes: 15 additions & 0 deletions src/stirling/source_connectors/socket_tracer/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,21 @@ pl_cc_bpf_test(
],
)

pl_cc_bpf_test(
name = "websocket_trace_bpf_test",
timeout = "moderate",
srcs = ["websocket_trace_bpf_test.cc"],
tags = ["requires_bpf"],
deps = [
":cc_library",
"//src/common/testing/test_utils:cc_library",
"//src/stirling/source_connectors/socket_tracer/testing:cc_library",
"//src/stirling/source_connectors/socket_tracer/testing/container_images:go_websocket_client_container",
"//src/stirling/source_connectors/socket_tracer/testing/container_images:go_websocket_server_container",
"//src/stirling/testing:cc_library",
],
)

pl_cc_bpf_test(
name = "amqp_trace_bpf_test",
timeout = "moderate",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,10 @@ static __inline void process_data(const bool vecs, struct pt_regs* ctx, uint64_t

// Only process plaintext data.
if (conn_info->ssl == ssl) {
if (ENABLE_TLS_TRACING && ssl && conn_info->protocol == kProtocolTLS) {
conn_info->protocol = kProtocolUnknown;
}

// TODO(yzhao): Split the interface such that the singular buf case and multiple bufs in msghdr
// are handled separately without mixed interface. The plan is to factor out helper functions
// for lower-level functionalities, and call them separately for each case.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ enum traffic_protocol_t {
kProtocolMux = 11,
kProtocolAMQP = 12,
kProtocolTLS = 13,
kProtocolWebSocket = 14,
// We use magic enum to iterate through protocols in C++ land,
// and don't want the C-enum-size trick to show up there.
#ifndef __cplusplus
Expand Down
79 changes: 76 additions & 3 deletions src/stirling/source_connectors/socket_tracer/conn_tracker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ void ConnTracker::AddConnCloseEvent(const socket_control_event_t& event) {
}
state_ptr->global.conn_closed = true;
}
if (protocol() == traffic_protocol_t::kProtocolWebSocket) {
auto* state_ptr = protocol_state<protocols::websocket::StateWrapper>();
if (state_ptr != nullptr) {
state_ptr->global.conn_closed = true;
}
}

send_data_.set_conn_closed();
recv_data_.set_conn_closed();
Expand All @@ -146,6 +152,13 @@ void ConnTracker::AddDataEvent(std::unique_ptr<SocketDataEvent> event) {
SetProtocol(event->attr.protocol, "inferred from data_event");
SetSSL(event->attr.ssl, event->attr.ssl_source, "inferred from data_event");

if (protocol_ == kProtocolTLS && event->attr.ssl && event->attr.protocol != kProtocolTLS &&
event->attr.protocol != kProtocolUnknown) {
tls_handover_protocol_ = event->attr.protocol;
tls_handover_events_.push_back(std::move(event));
return;
}

CheckTracker();
UpdateTimestamps(event->attr.timestamp_ns);
UpdateDataStats(*event);
Expand All @@ -158,7 +171,7 @@ void ConnTracker::AddDataEvent(std::unique_ptr<SocketDataEvent> event) {
return;
}

if (event->attr.protocol != protocol_) {
if (event->attr.protocol != protocol_ && event->attr.protocol != upgraded_from_) {
return;
}

Expand Down Expand Up @@ -245,6 +258,11 @@ endpoint_role_t InferHTTP2Role(bool write_event, const std::unique_ptr<HTTP2Head
}

void ConnTracker::AddHTTP2Header(std::unique_ptr<HTTP2HeaderEvent> hdr) {
if (protocol_ == kProtocolTLS) {
tls_handover_protocol_ = kProtocolHTTP2;
tls_handover_events_.push_back(std::move(hdr));
return;
}
SetProtocol(kProtocolHTTP2, "inferred from http2 headers");

if (protocol_ != kProtocolHTTP2) {
Expand Down Expand Up @@ -322,6 +340,11 @@ void ConnTracker::AddHTTP2Header(std::unique_ptr<HTTP2HeaderEvent> hdr) {
}

void ConnTracker::AddHTTP2Data(std::unique_ptr<HTTP2DataEvent> data) {
if (protocol_ == kProtocolTLS) {
tls_handover_protocol_ = kProtocolHTTP2;
tls_handover_events_.push_back(std::move(data));
return;
}
SetProtocol(kProtocolHTTP2, "inferred from http2 data");

if (protocol_ != kProtocolHTTP2) {
Expand Down Expand Up @@ -376,7 +399,7 @@ void ConnTracker::AddHTTP2Data(std::unique_ptr<HTTP2DataEvent> data) {
::ToString(data->attr.conn_id));
}

half_stream_ptr->AddData(data->payload);
half_stream_ptr->AddData(data->payload, FLAGS_max_body_bytes);
if (data->attr.end_stream) {
half_stream_ptr->AddEndStream();
}
Expand Down Expand Up @@ -674,7 +697,8 @@ auto CreateTraceRoles() {
res.Set(kProtocolKafka, {kRoleServer});
res.Set(kProtocolMux, {kRoleServer});
res.Set(kProtocolAMQP, {kRoleServer});
res.Set(kProtocolTLS, {kRoleServer});
res.Set(kProtocolTLS, {kRoleClient, kRoleServer});
res.Set(kProtocolWebSocket, {kRoleServer});

DCHECK(res.AreAllKeysSet());
return res;
Expand Down Expand Up @@ -812,6 +836,55 @@ void ConnTracker::IterationPreTick(
UpdateState(cluster_cidrs);
}

bool ConnTracker::MaybeUpgradeToWebSocket() {
if (protocol_ != kProtocolHTTP) {
return false;
}
auto* state = protocol_state<protocols::http::StateWrapper>();
if (state == nullptr || !state->global.websocket_upgrade_accepted) {
return false;
}

protocols::websocket::StateWrapper ws_state;
ws_state.global.path = std::move(state->global.websocket_path);
ws_state.global.subprotocol = std::move(state->global.websocket_subprotocol);
ws_state.global.conn_closed = state->global.conn_closed;

CONN_TRACE(1) << "Protocol upgraded: HTTP->WebSocket";
upgraded_from_ = protocol_;
protocol_ = kProtocolWebSocket;
protocol_state_ = std::move(ws_state);
send_data_.SwitchProtocol(protocol_);
recv_data_.SwitchProtocol(protocol_);
return true;
}

bool ConnTracker::MaybeHandOverFromTLS() {
if (protocol_ != kProtocolTLS || tls_handover_protocol_ == kProtocolUnknown) {
return false;
}

CONN_TRACE(1) << absl::Substitute("Protocol handed over: TLS->$0",
magic_enum::enum_name(tls_handover_protocol_));
protocol_ = tls_handover_protocol_;
tls_handover_protocol_ = kProtocolUnknown;
send_data_.SwitchProtocol(protocol_);
recv_data_.SwitchProtocol(protocol_);

auto events = std::move(tls_handover_events_);
tls_handover_events_.clear();
for (auto& event : events) {
if (auto* data = std::get_if<std::unique_ptr<SocketDataEvent>>(&event)) {
AddDataEvent(std::move(*data));
} else if (auto* hdr = std::get_if<std::unique_ptr<HTTP2HeaderEvent>>(&event)) {
AddHTTP2Header(std::move(*hdr));
} else {
AddHTTP2Data(std::move(std::get<std::unique_ptr<HTTP2DataEvent>>(event)));
}
}
return true;
}

void ConnTracker::IterationPostTick() {
if (death_countdown_ > 0) {
--death_countdown_;
Expand Down
11 changes: 11 additions & 0 deletions src/stirling/source_connectors/socket_tracer/conn_tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "src/stirling/utils/stat_counter.h"

DECLARE_bool(treat_loopback_as_in_cluster);
DECLARE_uint64(max_body_bytes);
constexpr int64_t kUnsetPIDFD = -1;
DECLARE_int64(stirling_conn_trace_pid);
DECLARE_int64(stirling_conn_trace_fd);
Expand Down Expand Up @@ -327,6 +328,8 @@ class ConnTracker : NotCopyMoveable {
}

const conn_id_t& conn_id() const { return conn_id_; }
bool MaybeHandOverFromTLS();

traffic_protocol_t protocol() const { return protocol_; }
endpoint_role_t role() const { return role_; }
bool ssl() const { return ssl_; }
Expand Down Expand Up @@ -481,6 +484,8 @@ class ConnTracker : NotCopyMoveable {
*/
void IterationPostTick();

bool MaybeUpgradeToWebSocket();

/**
* Sets the duration after which a connection is deemed to be inactive.
* After becoming inactive, the connection may either (1) have its buffers purged,
Expand Down Expand Up @@ -703,6 +708,12 @@ class ConnTracker : NotCopyMoveable {
bool is_tracked_upid_ = false;

traffic_protocol_t protocol_ = kProtocolUnknown;
traffic_protocol_t upgraded_from_ = kProtocolUnknown;

traffic_protocol_t tls_handover_protocol_ = kProtocolUnknown;
std::vector<std::variant<std::unique_ptr<SocketDataEvent>, std::unique_ptr<HTTP2HeaderEvent>,
std::unique_ptr<HTTP2DataEvent>>>
tls_handover_events_;
endpoint_role_t role_ = kRoleUnknown;
bool ssl_ = false;
ssl_source_t ssl_source_ = kSSLNone;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <string>

#include "src/common/base/types.h"
#include "src/common/testing/test_environment.h"
#include "src/stirling/source_connectors/socket_tracer/testing/http2_stream_generator.h"

namespace px {
Expand Down Expand Up @@ -84,6 +87,42 @@ TEST_F(ConnTrackerHTTP2Test, BasicData) {
EXPECT_EQ(records[0].recv.data(), "Response");
}

TEST_F(ConnTrackerHTTP2Test, DataIsKeptUpToTheBodyLimit) {
PX_SET_FOR_SCOPE(FLAGS_max_body_bytes, 16384);
const std::string kOrder(2000, 'o');
auto frame_generator = testing::StreamEventGenerator(&mock_clock_, kConnID, 7);

tracker_.AddHTTP2Header(frame_generator.GenHeader<kHeaderEventWrite>(":method", "POST"));
tracker_.AddHTTP2Data(frame_generator.GenDataFrame<kDataFrameEventWrite>(kOrder, true));
tracker_.AddHTTP2Data(frame_generator.GenDataFrame<kDataFrameEventRead>("ok", true));
tracker_.AddHTTP2Header(frame_generator.GenHeader<kHeaderEventRead>(":status", "200"));

std::vector<http2::Record> records = tracker_.ProcessToRecords<http2::ProtocolTraits>();

ASSERT_EQ(records.size(), 1);
EXPECT_EQ(records[0].send.data(), kOrder);
EXPECT_FALSE(records[0].send.data_truncated());
}

TEST_F(ConnTrackerHTTP2Test, DataBeyondTheBodyLimitIsTruncated) {
PX_SET_FOR_SCOPE(FLAGS_max_body_bytes, 100);
auto frame_generator = testing::StreamEventGenerator(&mock_clock_, kConnID, 7);

tracker_.AddHTTP2Header(frame_generator.GenHeader<kHeaderEventWrite>(":method", "POST"));
tracker_.AddHTTP2Data(frame_generator.GenDataFrame<kDataFrameEventWrite>(std::string(60, 'a')));
tracker_.AddHTTP2Data(
frame_generator.GenDataFrame<kDataFrameEventWrite>(std::string(90, 'b'), true));
tracker_.AddHTTP2Data(frame_generator.GenDataFrame<kDataFrameEventRead>("ok", true));
tracker_.AddHTTP2Header(frame_generator.GenHeader<kHeaderEventRead>(":status", "200"));

std::vector<http2::Record> records = tracker_.ProcessToRecords<http2::ProtocolTraits>();

ASSERT_EQ(records.size(), 1);
EXPECT_EQ(records[0].send.data(), std::string(60, 'a') + std::string(40, 'b'));
EXPECT_TRUE(records[0].send.data_truncated());
EXPECT_EQ(records[0].send.original_data_size(), 150);
}

TEST_F(ConnTrackerHTTP2Test, BasicHeader) {
const int kStreamID = 7;
auto frame_generator = testing::StreamEventGenerator(&mock_clock_, kConnID, kStreamID);
Expand Down Expand Up @@ -361,20 +400,20 @@ TEST_F(ConnTrackerHTTP2Test, StreamIDJumpAhead) {
expected_record0.send.AddHeader(":method", "post");
expected_record0.send.AddHeader(":host", "pixie.ai");
expected_record0.send.AddHeader(":path", "/magic");
expected_record0.send.AddData("Request");
expected_record0.send.AddData("Request", FLAGS_max_body_bytes);
expected_record0.send.AddEndStream();
expected_record0.recv.AddHeader(":status", "200");
expected_record0.recv.AddData("Response");
expected_record0.recv.AddData("Response", FLAGS_max_body_bytes);
expected_record0.recv.AddEndStream();

http2::Record expected_record1;
expected_record1.send.AddHeader(":method", "post");
expected_record1.send.AddHeader(":host", "pixie.ai");
expected_record1.send.AddHeader(":path", "/wormhole");
expected_record1.send.AddData("Request");
expected_record1.send.AddData("Request", FLAGS_max_body_bytes);
expected_record1.send.AddEndStream();
expected_record1.recv.AddHeader(":status", "200");
expected_record1.recv.AddData("Long ways from home");
expected_record1.recv.AddData("Long ways from home", FLAGS_max_body_bytes);
expected_record1.recv.AddEndStream();

EXPECT_THAT(records, UnorderedElementsAre(EqHTTP2Record(expected_record0),
Expand Down Expand Up @@ -451,20 +490,20 @@ TEST_F(ConnTrackerHTTP2Test, StreamIDJumpBack) {
expected_record0.send.AddHeader(":method", "post");
expected_record0.send.AddHeader(":host", "pixie.ai");
expected_record0.send.AddHeader(":path", "/magic");
expected_record0.send.AddData("Request");
expected_record0.send.AddData("Request", FLAGS_max_body_bytes);
expected_record0.send.AddEndStream();
expected_record0.recv.AddHeader(":status", "200");
expected_record0.recv.AddData("Response");
expected_record0.recv.AddData("Response", FLAGS_max_body_bytes);
expected_record0.recv.AddEndStream();

http2::Record expected_record1;
expected_record1.send.AddHeader(":method", "post");
expected_record1.send.AddHeader(":host", "pixie.ai");
expected_record1.send.AddHeader(":path", "/wormhole");
expected_record1.send.AddData("Request");
expected_record1.send.AddData("Request", FLAGS_max_body_bytes);
expected_record1.send.AddEndStream();
expected_record1.recv.AddHeader(":status", "200");
expected_record1.recv.AddData("Long ways from home");
expected_record1.recv.AddData("Long ways from home", FLAGS_max_body_bytes);
expected_record1.recv.AddEndStream();

EXPECT_THAT(records, UnorderedElementsAre(EqHTTP2Record(expected_record0),
Expand Down
Loading
Loading