diff --git a/google/cloud/internal/curl_wrappers.cc b/google/cloud/internal/curl_wrappers.cc index 58709f94555ba..138e37bc85753 100644 --- a/google/cloud/internal/curl_wrappers.cc +++ b/google/cloud/internal/curl_wrappers.cc @@ -25,9 +25,11 @@ #include #endif #include +#include #include #include #include +#include #include #include #include @@ -188,7 +190,80 @@ std::string CleanupDebugData(char const* data, std::size_t size) { return text; } +struct CensoredHeaderMatch { + std::size_t start; + std::size_t colon; +}; + +// Matches encryption key header names followed by ':' only at a header-name +// boundary so sibling headers (-sha256, -algorithm) and prefixed names are +// preserved verbatim. +std::optional FindCensoredHeader(std::string_view line) { + static std::array constexpr kCensoredHeaderPrefixes = { + "x-goog-encryption-key:", + "x-goog-copy-source-encryption-key:", + }; + for (std::size_t pos = 0; pos < line.size(); ++pos) { + bool const at_boundary = pos == 0 || line[pos - 1] == '[' || + line[pos - 1] == ' ' || line[pos - 1] == '\t'; + if (!at_boundary) continue; + std::string_view const suffix = line.substr(pos); + for (std::string_view prefix : kCensoredHeaderPrefixes) { + if (absl::StartsWithIgnoreCase(suffix, prefix)) { + return CensoredHeaderMatch{pos, pos + prefix.size() - 1}; + } + } + } + return std::nullopt; +} + +std::string CensorEncryptionKeyHeaders(std::string_view payload) { + std::string out; + out.reserve(payload.size()); + std::size_t pos = 0; + while (pos < payload.size()) { + std::size_t const nl_pos = payload.find('\n', pos); + std::size_t const line_end = + nl_pos == std::string_view::npos ? payload.size() : nl_pos; + std::string_view line = payload.substr(pos, line_end - pos); + std::string_view terminator; + if (nl_pos != std::string_view::npos) { + if (!line.empty() && line.back() == '\r') { + line.remove_suffix(1); + terminator = "\r\n"; + } else { + terminator = "\n"; + } + } else if (line.back() == '\r') { + line.remove_suffix(1); + terminator = "\r"; + } + std::optional const match = FindCensoredHeader(line); + if (match.has_value()) { + // libcurl formats HTTP/2 and HTTP/3 header traces inside brackets + // ("[header: value]"). Preserve only a final closing bracket so embedded + // ']' characters inside the value are still replaced. + bool const bracketed = match->start > 0 && line[match->start - 1] == '['; + std::string_view const closing = + (bracketed && line.back() == ']') ? "]" : ""; + absl::StrAppend(&out, line.substr(0, match->colon + 1), " [censored]", + closing, terminator); + } else { + absl::StrAppend(&out, line, terminator); + } + pos = nl_pos == std::string_view::npos ? payload.size() : nl_pos + 1; + } + return out; +} + std::string DebugCensored(std::string_view msg, std::string_view payload) { + // Censor encryption key headers before Bearer truncation so a key value + // beginning with "Bearer " is replaced in full rather than truncated. + std::optional censored_payload; + if (absl::StrContainsIgnoreCase(payload, "encryption-key:")) { + censored_payload = CensorEncryptionKeyHeaders(payload); + payload = *censored_payload; + } // We want to truncate the portion of the payload following this ": Bearer" to // at most 32 characters, skipping everything else until any newline. auto const bearer = std::string_view{": Bearer "}; diff --git a/google/cloud/internal/curl_wrappers_test.cc b/google/cloud/internal/curl_wrappers_test.cc index d08d70510145f..e32fa7c342141 100644 --- a/google/cloud/internal/curl_wrappers_test.cc +++ b/google/cloud/internal/curl_wrappers_test.cc @@ -15,6 +15,7 @@ #include "google/cloud/internal/curl_wrappers.h" #include "google/cloud/internal/curl_options.h" #include +#include #include namespace google { @@ -23,6 +24,7 @@ namespace rest_internal { GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN namespace { +using ::testing::Eq; using ::testing::Pair; using ::testing::UnorderedElementsAre; @@ -156,6 +158,211 @@ TEST(CurlWrappers, DebugInfo) { } } +struct DebugHeaderCase { + std::string name; + std::string input; + std::string expected; +}; + +void PrintTo(DebugHeaderCase const& c, std::ostream* os) { *os << c.name; } + +class DebugSendHeaderEncryptionKeyHeadersTest + : public ::testing::TestWithParam {}; + +/// @test Verify DebugSendHeader formats encryption key header values as +/// [censored] while preserving line endings, sibling headers, and Bearer +/// truncation. +TEST_P(DebugSendHeaderEncryptionKeyHeadersTest, FormatsLine) { + DebugHeaderCase const& test = GetParam(); + EXPECT_THAT(DebugSendHeader(test.input.data(), test.input.size()), + Eq(test.expected)); +} + +INSTANTIATE_TEST_SUITE_P( + CurlWrappers, DebugSendHeaderEncryptionKeyHeadersTest, + ::testing::Values( + DebugHeaderCase{ + "EncryptionKeyNoNewline", + "x-goog-encryption-key: " + "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=", + ">> curl(Send Header): x-goog-encryption-key: [censored]"}, + DebugHeaderCase{ + "EncryptionKeyLf", + "x-goog-encryption-key: " + "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=\n", + ">> curl(Send Header): x-goog-encryption-key: [censored]\n"}, + DebugHeaderCase{ + "EncryptionKeyCrLf", + "x-goog-encryption-key: " + "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=\r\n", + ">> curl(Send Header): x-goog-encryption-key: [censored]\r\n"}, + DebugHeaderCase{ + "CopySourceEncryptionKeyNoNewline", + "x-goog-copy-source-encryption-key: " + "QUJDRDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg=", + ">> curl(Send Header): x-goog-copy-source-encryption-key: " + "[censored]"}, + DebugHeaderCase{ + "CopySourceEncryptionKeyLf", + "x-goog-copy-source-encryption-key: " + "QUJDRDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg=\n", + ">> curl(Send Header): x-goog-copy-source-encryption-key: " + "[censored]\n"}, + DebugHeaderCase{ + "CopySourceEncryptionKeyCrLf", + "x-goog-copy-source-encryption-key: " + "QUJDRDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg=\r\n", + ">> curl(Send Header): x-goog-copy-source-encryption-key: " + "[censored]\r\n"}, + DebugHeaderCase{ + "MixedCaseHeaders", + "X-Goog-Encryption-Key: value1\r\n" + "X-Goog-Copy-Source-Encryption-Key: value2\r\n", + ">> curl(Send Header): X-Goog-Encryption-Key: [censored]\r\n" + "X-Goog-Copy-Source-Encryption-Key: [censored]\r\n"}, + DebugHeaderCase{ + "MultiLineWithBearerAndSiblings", + "authorization: Bearer 012345678901234567890123456789123456\n" + "x-goog-encryption-key: " + "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=\n" + "x-goog-encryption-key-sha256: key-hash\n" + "x-goog-copy-source-encryption-key: " + "QUJDRDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg=\n" + "x-goog-copy-source-encryption-key-sha256: source-hash\n" + "host: storage.googleapis.com\n", + ">> curl(Send Header): authorization: Bearer " + "01234567890123456789012345678912......\n" + "x-goog-encryption-key: [censored]\n" + "x-goog-encryption-key-sha256: key-hash\n" + "x-goog-copy-source-encryption-key: [censored]\n" + "x-goog-copy-source-encryption-key-sha256: source-hash\n" + "host: storage.googleapis.com\n"}, + DebugHeaderCase{ + "DuplicateKeyLines", + "x-goog-encryption-key: first\r\n" + "x-goog-encryption-key: second\r\n", + ">> curl(Send Header): x-goog-encryption-key: [censored]\r\n" + "x-goog-encryption-key: [censored]\r\n"}, + DebugHeaderCase{ + "NoSpaceAfterColon", "x-goog-encryption-key:value", + ">> curl(Send Header): x-goog-encryption-key: [censored]"}, + DebugHeaderCase{ + "EmptyValueWithSpace", "x-goog-encryption-key: \r\n", + ">> curl(Send Header): x-goog-encryption-key: [censored]\r\n"}, + DebugHeaderCase{ + "EmptyValueNoSpace", "x-goog-encryption-key:\r\n", + ">> curl(Send Header): x-goog-encryption-key: [censored]\r\n"}, + DebugHeaderCase{ + "ValueStartingWithBearer", + "x-goog-encryption-key: Bearer " + "012345678901234567890123456789123456\r\n", + ">> curl(Send Header): x-goog-encryption-key: [censored]\r\n"}, + DebugHeaderCase{ + "ValueStartingWithBearerBeforeAuthorization", + "x-goog-encryption-key: Bearer " + "012345678901234567890123456789123456\n" + "authorization: Bearer 012345678901234567890123456789123456\n", + ">> curl(Send Header): x-goog-encryption-key: [censored]\n" + "authorization: Bearer " + "01234567890123456789012345678912......\n"}, + DebugHeaderCase{ + "LeftmostHeaderWinsOnSameLine", + "x-goog-copy-source-encryption-key: val1 x-goog-encryption-key: " + "val2\r\n", + ">> curl(Send Header): x-goog-copy-source-encryption-key: " + "[censored]\r\n"}, + DebugHeaderCase{ + "TabPrecededHeader", "\tx-goog-encryption-key: value\r\n", + ">> curl(Send Header): \tx-goog-encryption-key: [censored]\r\n"}, + DebugHeaderCase{ + "BlankLinesPreservedLf", "x-goog-encryption-key: value\n\n", + ">> curl(Send Header): x-goog-encryption-key: [censored]\n\n"}, + DebugHeaderCase{"BlankLinesPreservedCrLf", + "x-goog-encryption-key: value\r\n\r\n", + ">> curl(Send Header): x-goog-encryption-key: " + "[censored]\r\n\r\n"}, + DebugHeaderCase{ + "TrailingCarriageReturnOnly", "x-goog-encryption-key: value\r", + ">> curl(Send Header): x-goog-encryption-key: [censored]\r"}, + DebugHeaderCase{ + "UnbracketedValueEndingInClosingBracket", + "x-goog-encryption-key: value]\r\n", + ">> curl(Send Header): x-goog-encryption-key: [censored]\r\n"}, + DebugHeaderCase{"EmptyPayload", "", ">> curl(Send Header): "}, + DebugHeaderCase{ + "SiblingAndNearMissHeadersUnchanged", + "x-goog-encryption-key-sha256: key-hash\r\n" + "x-goog-copy-source-encryption-key-sha256: source-hash\r\n" + "x-goog-encryption-algorithm: AES256\r\n" + "my-x-goog-encryption-key: value\r\n", + ">> curl(Send Header): x-goog-encryption-key-sha256: key-hash\r\n" + "x-goog-copy-source-encryption-key-sha256: source-hash\r\n" + "x-goog-encryption-algorithm: AES256\r\n" + "my-x-goog-encryption-key: value\r\n"}), + [](::testing::TestParamInfo const& info) { + return info.param.name; + }); + +class DebugInfoEncryptionKeyHeadersTest + : public ::testing::TestWithParam {}; + +/// @test Verify DebugInfo formats bracketed HTTP/2 and HTTP/3 encryption key +/// header traces as [censored] while preserving non-matching headers. +TEST_P(DebugInfoEncryptionKeyHeadersTest, FormatsLine) { + DebugHeaderCase const& test = GetParam(); + EXPECT_THAT(DebugInfo(test.input.data(), test.input.size()), + Eq(test.expected)); +} + +INSTANTIATE_TEST_SUITE_P( + CurlWrappers, DebugInfoEncryptionKeyHeadersTest, + ::testing::Values( + DebugHeaderCase{"Http2EncryptionKey", + "[HTTP/2] [1] [x-goog-encryption-key: " + "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=]\n", + "== curl(Info): [HTTP/2] [1] [x-goog-encryption-key: " + "[censored]]\n"}, + DebugHeaderCase{ + "Http2CopySourceEncryptionKey", + "[HTTP/2] [1] [x-goog-copy-source-encryption-key: " + "QUJDRDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg=]\n", + "== curl(Info): [HTTP/2] [1] [x-goog-copy-source-encryption-key: " + "[censored]]\n"}, + DebugHeaderCase{"Http3EncryptionKey", + "[HTTP/3] [0] [x-goog-encryption-key: " + "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=]\n", + "== curl(Info): [HTTP/3] [0] [x-goog-encryption-key: " + "[censored]]\n"}, + DebugHeaderCase{ + "H2H3EncryptionKey", + "h2h3 [x-goog-encryption-key: " + "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=]\n", + "== curl(Info): h2h3 [x-goog-encryption-key: [censored]]\n"}, + DebugHeaderCase{ + "SpacePrecededTraceHeader", + "[1] header: x-goog-encryption-key: " + "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=\n", + "== curl(Info): [1] header: x-goog-encryption-key: [censored]\n"}, + DebugHeaderCase{ + "UnclosedBracketEncryptionKey", + "[HTTP/2] [1] [x-goog-encryption-key: " + "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=", + "== curl(Info): [HTTP/2] [1] [x-goog-encryption-key: [censored]"}, + DebugHeaderCase{"ValueContainingClosingBracket", + "[HTTP/2] [1] [x-goog-encryption-key: part1]part2]\n", + "== curl(Info): [HTTP/2] [1] [x-goog-encryption-key: " + "[censored]]\n"}, + DebugHeaderCase{ + "SiblingAndNearMissHeadersUnchanged", + "[HTTP/2] [1] [x-goog-encryption-key-sha256: key-hash]\n" + "[HTTP/2] [1] [my-x-goog-encryption-key: value]\n", + "== curl(Info): [HTTP/2] [1] [x-goog-encryption-key-sha256: " + "key-hash]\n" + "[HTTP/2] [1] [my-x-goog-encryption-key: value]\n"}), + [](::testing::TestParamInfo const& info) { + return info.param.name; + }); + TEST(CurlWrappers, CurlInitializeOptions) { auto defaults = CurlInitializeOptions({}); EXPECT_TRUE(defaults.get()); diff --git a/google/cloud/internal/http_header.cc b/google/cloud/internal/http_header.cc index fbe8820eec808..ed9e2094b26c5 100644 --- a/google/cloud/internal/http_header.cc +++ b/google/cloud/internal/http_header.cc @@ -13,9 +13,11 @@ // limitations under the License. #include "google/cloud/internal/http_header.h" +#include "absl/strings/match.h" #include "absl/strings/str_cat.h" #include "absl/strings/str_join.h" #include "absl/strings/strip.h" +#include namespace google { namespace cloud { @@ -64,6 +66,9 @@ HttpHeader::operator std::string() const { std::string HttpHeader::DebugString() const { if (name_.empty()) return {}; if (values_.empty()) return absl::StrCat(name_.name(), ":"); + if (IsCensoredHeader(name_.name())) { + return absl::StrCat(name_.name(), ": [censored]"); + } return absl::StrCat( name_.name(), ": ", absl::StrJoin(values_, ",", [](std::string* out, std::string const& v) { @@ -84,6 +89,14 @@ HttpHeader& HttpHeader::MergeHeader(HttpHeader&& other) { return *this; } +bool IsCensoredHeader(std::string_view name) { + std::string_view constexpr kEncryptionKeyHeader = "x-goog-encryption-key"; + std::string_view constexpr kCopySourceEncryptionKeyHeader = + "x-goog-copy-source-encryption-key"; + return absl::EqualsIgnoreCase(name, kEncryptionKeyHeader) || + absl::EqualsIgnoreCase(name, kCopySourceEncryptionKeyHeader); +} + GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END } // namespace rest_internal } // namespace cloud diff --git a/google/cloud/internal/http_header.h b/google/cloud/internal/http_header.h index 06a70993b74d9..163b243e9c259 100644 --- a/google/cloud/internal/http_header.h +++ b/google/cloud/internal/http_header.h @@ -24,6 +24,7 @@ #include #endif // UINTPTR_MAX == UINT64_MAX #include +#include #include namespace google { @@ -170,6 +171,9 @@ using HttpHeaders = absl::flat_hash_map; using HttpHeaders = std::unordered_map; #endif // UINTPTR_MAX == UINT64_MAX +/// Returns true for header names whose values are formatted as `[censored]`. +bool IsCensoredHeader(std::string_view name); + GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END } // namespace rest_internal } // namespace cloud diff --git a/google/cloud/internal/http_header_test.cc b/google/cloud/internal/http_header_test.cc index 5bcc6bd8eccf8..599c79340334d 100644 --- a/google/cloud/internal/http_header_test.cc +++ b/google/cloud/internal/http_header_test.cc @@ -146,6 +146,41 @@ TEST(HttpHeader, DebugString) { EXPECT_THAT(long_value.DebugString(), Eq("key: valuelonge")); } +/// @test Verify DebugString formats encryption key header values as [censored] +/// while preserving headers with no values and sibling header truncation. +TEST(HttpHeader, DebugStringEncryptionKeyHeaders) { + EXPECT_THAT(HttpHeader("x-goog-encryption-key", + "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=") + .DebugString(), + Eq("x-goog-encryption-key: [censored]")); + EXPECT_THAT(HttpHeader("X-Goog-Encryption-Key", "short").DebugString(), + Eq("x-goog-encryption-key: [censored]")); + EXPECT_THAT(HttpHeader("x-goog-copy-source-encryption-key", + "QUJDRDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg=") + .DebugString(), + Eq("x-goog-copy-source-encryption-key: [censored]")); + EXPECT_THAT(HttpHeader("X-Goog-Copy-Source-Encryption-Key", {"k1", "k2"}) + .DebugString(), + Eq("x-goog-copy-source-encryption-key: [censored]")); + EXPECT_THAT(HttpHeader("x-goog-encryption-key", "").DebugString(), + Eq("x-goog-encryption-key: [censored]")); + EXPECT_THAT(HttpHeader("x-goog-encryption-key").DebugString(), + Eq("x-goog-encryption-key:")); + EXPECT_THAT(HttpHeader("x-goog-encryption-key-sha256", + "dGVzdC1zaGEyNTYtaGFzaC12YWx1ZS0wMTIzNDU2Nzg5") + .DebugString(), + Eq("x-goog-encryption-key-sha256: dGVzdC1zaG")); + EXPECT_THAT(HttpHeader("x-goog-copy-source-encryption-key-sha256", + "c291cmNlLXNoYTI1Ni1oYXNoLXZhbHVlLTAxMjM0NTY3") + .DebugString(), + Eq("x-goog-copy-source-encryption-key-sha256: c291cmNlLX")); + EXPECT_THAT(HttpHeader("x-goog-encryption-algorithm", "AES256").DebugString(), + Eq("x-goog-encryption-algorithm: AES256")); + EXPECT_THAT(HttpHeader("x-custom-x-goog-encryption-key", "customvalue123") + .DebugString(), + Eq("x-custom-x-goog-encryption-key: customvalu")); +} + TEST(HttpHeader, MergeHeader) { HttpHeader k1_v1("k1", "k1-value1"); HttpHeader k2_v1("k2", "k2-value1"); diff --git a/google/cloud/internal/rest_opentelemetry.cc b/google/cloud/internal/rest_opentelemetry.cc index 6264ac2d1e3ae..16b00e1bc4562 100644 --- a/google/cloud/internal/rest_opentelemetry.cc +++ b/google/cloud/internal/rest_opentelemetry.cc @@ -13,6 +13,7 @@ // limitations under the License. #include "google/cloud/internal/rest_opentelemetry.h" +#include "google/cloud/internal/http_header.h" #include "google/cloud/internal/opentelemetry.h" #include "google/cloud/internal/opentelemetry_semantic_convention_compatibility.h" #include "google/cloud/internal/rest_context.h" @@ -91,6 +92,10 @@ opentelemetry::nostd::shared_ptr MakeSpanHttp( span->SetAttribute(name, kv.second.values().front().substr(0, 32)); continue; } + if (IsCensoredHeader(kv.first.name())) { + span->SetAttribute(name, "[censored]"); + continue; + } span->SetAttribute(name, kv.second.values().front()); } return span; diff --git a/google/cloud/internal/rest_opentelemetry_test.cc b/google/cloud/internal/rest_opentelemetry_test.cc index eca7902602269..6c2e0576faa8c 100644 --- a/google/cloud/internal/rest_opentelemetry_test.cc +++ b/google/cloud/internal/rest_opentelemetry_test.cc @@ -17,6 +17,8 @@ #include "google/cloud/testing_util/opentelemetry_matchers.h" #include "google/cloud/testing_util/status_matchers.h" #include +#include +#include namespace google { namespace cloud { @@ -77,6 +79,87 @@ TEST(RestOpentelemetry, MakeSpanHttp) { secret.substr(0, 32)))))); } +/// @test Verify encryption key headers are formatted as [censored] on HTTP +/// spans while sibling headers and headers with no values keep their existing +/// attribute values. +TEST(RestOpentelemetry, MakeSpanHttpEncryptionKeyHeaders) { + std::shared_ptr const span_catcher = + InstallSpanCatcher(); + + std::string_view constexpr kUrl = + "https://storage.googleapis.com/storage/v1/b/my-bucket/o/my-object"; + std::string_view constexpr kKey = + "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI="; + std::string_view constexpr kSourceKey = + "QUJDRDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg="; + std::string_view constexpr kKeySha256 = + "dGVzdC1zaGEyNTYtaGFzaC12YWx1ZS0wMTIzNDU2Nzg5"; + std::string_view constexpr kSourceKeySha256 = + "c291cmNlLXNoYTI1Ni1oYXNoLXZhbHVlLTAxMjM0NTY3"; + + RestRequest lowercase_request(std::string{kUrl}); + lowercase_request.AddHeader("x-goog-encryption-algorithm", "AES256"); + lowercase_request.AddHeader("x-goog-encryption-key", std::string{kKey}); + lowercase_request.AddHeader("x-goog-encryption-key-sha256", + std::string{kKeySha256}); + lowercase_request.AddHeader("x-goog-copy-source-encryption-algorithm", + "AES256"); + lowercase_request.AddHeader("x-goog-copy-source-encryption-key", + std::string{kSourceKey}); + lowercase_request.AddHeader("x-goog-copy-source-encryption-key-sha256", + std::string{kSourceKeySha256}); + lowercase_request.AddHeader("x-custom-x-goog-encryption-key", "custom-value"); + MakeSpanHttp(lowercase_request, "GET")->End(); + + RestRequest mixed_case_request(std::string{kUrl}); + mixed_case_request.AddHeader("X-Goog-Encryption-Key", "k"); + mixed_case_request.AddHeader("X-Goog-Copy-Source-Encryption-Key", "s"); + MakeSpanHttp(mixed_case_request, "GET")->End(); + + RestRequest empty_string_request(std::string{kUrl}); + empty_string_request.AddHeader("x-goog-encryption-key", ""); + MakeSpanHttp(empty_string_request, "GET")->End(); + + RestRequest no_values_request( + std::string{kUrl}, HttpHeaders{{"x-goog-encryption-key", HttpHeader{}}}); + MakeSpanHttp(no_values_request, "GET")->End(); + + EXPECT_THAT( + span_catcher->GetSpans(), + ElementsAre( + SpanHasAttributes( + OTelAttribute( + "http.request.header.x-goog-encryption-algorithm", "AES256"), + OTelAttribute( + "http.request.header.x-goog-encryption-key", "[censored]"), + OTelAttribute( + "http.request.header.x-goog-encryption-key-sha256", + std::string{kKeySha256}), + OTelAttribute( + "http.request.header.x-goog-copy-source-encryption-algorithm", + "AES256"), + OTelAttribute( + "http.request.header.x-goog-copy-source-encryption-key", + "[censored]"), + OTelAttribute( + "http.request.header.x-goog-copy-source-encryption-key-" + "sha256", + std::string{kSourceKeySha256}), + OTelAttribute( + "http.request.header.x-custom-x-goog-encryption-key", + "custom-value")), + SpanHasAttributes( + OTelAttribute( + "http.request.header.x-goog-encryption-key", "[censored]"), + OTelAttribute( + "http.request.header.x-goog-copy-source-encryption-key", + "[censored]")), + SpanHasAttributes(OTelAttribute( + "http.request.header.x-goog-encryption-key", "[censored]")), + SpanHasAttributes(OTelAttribute( + "http.request.header.x-goog-encryption-key", "")))); +} + TEST(RestOpentelemetry, InjectTraceContext) { auto span_catcher = InstallSpanCatcher(); auto constexpr kUrl = "https://storage.googleapis.com/storage/v1/b/my-bucket"; diff --git a/google/cloud/internal/tracing_rest_client.cc b/google/cloud/internal/tracing_rest_client.cc index 7534afcd02f00..84d14cba50c97 100644 --- a/google/cloud/internal/tracing_rest_client.cc +++ b/google/cloud/internal/tracing_rest_client.cc @@ -13,6 +13,7 @@ // limitations under the License. #include "google/cloud/internal/tracing_rest_client.h" +#include "google/cloud/internal/http_header.h" #include "google/cloud/internal/opentelemetry.h" #include "google/cloud/internal/opentelemetry_semantic_convention_compatibility.h" #include "google/cloud/internal/rest_opentelemetry.h" @@ -85,6 +86,10 @@ StatusOr> EndResponseSpan( kv.second.values().front().substr(0, kApiKeyHintLength) + "..."); continue; } + if (IsCensoredHeader(kv.first.name())) { + span->SetAttribute(name, "[censored]"); + continue; + } span->SetAttribute(name, kv.second.values().front()); } if (!request_result || !(*request_result)) { diff --git a/google/cloud/internal/tracing_rest_client_test.cc b/google/cloud/internal/tracing_rest_client_test.cc index 2fab57b612db5..db29b99f97fec 100644 --- a/google/cloud/internal/tracing_rest_client_test.cc +++ b/google/cloud/internal/tracing_rest_client_test.cc @@ -22,6 +22,8 @@ #include #include #include +#include +#include namespace google { namespace cloud { @@ -313,6 +315,129 @@ TEST(TracingRestClient, CensorsAuthFields) { "ABCDEFGHIJKL...")))); } +/// @test Verify encryption key headers on RestContext are formatted as +/// [censored] on HTTP spans (including when also set on RestRequest), while +/// sibling headers and headers with no values keep their existing attribute +/// values. +TEST(TracingRestClient, EncryptionKeyHeaders) { + std::shared_ptr const span_catcher = + InstallSpanCatcher(); + + auto impl = std::make_unique(); + EXPECT_CALL(*impl, Delete) + .WillRepeatedly([](RestContext&, RestRequest const&) { + auto response = std::make_unique(); + EXPECT_CALL(*response, StatusCode) + .WillRepeatedly(Return(HttpStatusCode::kOk)); + EXPECT_CALL(*response, Headers).WillRepeatedly(Return(MockHeaders())); + EXPECT_CALL(std::move(*response), ExtractPayload).WillOnce([] { + return MakeMockHttpPayloadSuccess(MockContents()); + }); + return std::unique_ptr(std::move(response)); + }); + + std::string_view constexpr kUrl = + "https://storage.googleapis.com/storage/v1/b/my-bucket/o/my-object"; + std::string_view constexpr kKey = + "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI="; + std::string_view constexpr kSourceKey = + "QUJDRDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg="; + std::string_view constexpr kKeySha256 = + "dGVzdC1zaGEyNTYtaGFzaC12YWx1ZS0wMTIzNDU2Nzg5"; + std::string_view constexpr kSourceKeySha256 = + "c291cmNlLXNoYTI1Ni1oYXNoLXZhbHVlLTAxMjM0NTY3"; + + std::unique_ptr const client = + MakeTracingRestClient(std::move(impl)); + + // Set the header on both request and context so the context attribute keeps + // [censored]. + RestRequest request(std::string{kUrl}); + request.AddHeader("x-goog-encryption-key", std::string{kKey}); + + RestContext context; + context.AddHeader("x-goog-encryption-algorithm", "AES256"); + context.AddHeader("x-goog-encryption-key", std::string{kKey}); + context.AddHeader("x-goog-encryption-key-sha256", std::string{kKeySha256}); + context.AddHeader("x-goog-copy-source-encryption-algorithm", "AES256"); + context.AddHeader("x-goog-copy-source-encryption-key", + std::string{kSourceKey}); + context.AddHeader("x-goog-copy-source-encryption-key-sha256", + std::string{kSourceKeySha256}); + context.AddHeader("x-custom-x-goog-encryption-key", "custom-value"); + + StatusOr> r1 = client->Delete(context, request); + ASSERT_STATUS_OK(r1); + std::unique_ptr response1 = *std::move(r1); + ASSERT_THAT(response1, NotNull()); + EXPECT_THAT(ReadAll(std::move(*response1).ExtractPayload()), + IsOkAndHolds(MockContents())); + + RestContext mixed_case_context; + mixed_case_context.AddHeader("X-Goog-Encryption-Key", ""); + mixed_case_context.AddHeader("X-Goog-Copy-Source-Encryption-Key", "s"); + StatusOr> r2 = + client->Delete(mixed_case_context, RestRequest(std::string{kUrl})); + ASSERT_STATUS_OK(r2); + std::unique_ptr response2 = *std::move(r2); + ASSERT_THAT(response2, NotNull()); + EXPECT_THAT(ReadAll(std::move(*response2).ExtractPayload()), + IsOkAndHolds(MockContents())); + + RestContext no_values_context( + HttpHeaders{{"x-goog-encryption-key", HttpHeader{}}}); + StatusOr> r3 = + client->Delete(no_values_context, RestRequest(std::string{kUrl})); + ASSERT_STATUS_OK(r3); + std::unique_ptr response3 = *std::move(r3); + ASSERT_THAT(response3, NotNull()); + EXPECT_THAT(ReadAll(std::move(*response3).ExtractPayload()), + IsOkAndHolds(MockContents())); + + EXPECT_THAT( + span_catcher->GetSpans(), + UnorderedElementsAre( + AllOf(SpanNamed("HTTP/DELETE"), + SpanHasAttributes( + OTelAttribute( + "http.request.header.x-goog-encryption-algorithm", + "AES256"), + OTelAttribute( + "http.request.header.x-goog-encryption-key", + "[censored]"), + OTelAttribute( + "http.request.header.x-goog-encryption-key-sha256", + std::string{kKeySha256}), + OTelAttribute( + "http.request.header.x-goog-copy-source-encryption-" + "algorithm", + "AES256"), + OTelAttribute( + "http.request.header.x-goog-copy-source-encryption-key", + "[censored]"), + OTelAttribute( + "http.request.header.x-goog-copy-source-encryption-key-" + "sha256", + std::string{kSourceKeySha256}), + OTelAttribute( + "http.request.header.x-custom-x-goog-encryption-key", + "custom-value"))), + SpanNamed("SendRequest"), + AllOf(SpanNamed("HTTP/DELETE"), + SpanHasAttributes( + OTelAttribute( + "http.request.header.x-goog-encryption-key", + "[censored]"), + OTelAttribute( + "http.request.header.x-goog-copy-source-encryption-key", + "[censored]"))), + SpanNamed("SendRequest"), + AllOf(SpanNamed("HTTP/DELETE"), + SpanHasAttributes(OTelAttribute( + "http.request.header.x-goog-encryption-key", ""))), + SpanNamed("SendRequest"))); +} + TEST(TracingRestClient, CachedConnection) { auto span_catcher = InstallSpanCatcher();