Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
75 changes: 75 additions & 0 deletions google/cloud/internal/curl_wrappers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
#include <openssl/opensslv.h>
#endif
#include <algorithm>
#include <array>
#include <cctype>
#include <csignal>
#include <iostream>
#include <optional>
#include <string>
#include <string_view>
#include <thread>
Expand Down Expand Up @@ -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<CensoredHeaderMatch> FindCensoredHeader(std::string_view line) {
static std::array<std::string_view, 2> 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<CensoredHeaderMatch> 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<std::string> 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 "};
Expand Down
207 changes: 207 additions & 0 deletions google/cloud/internal/curl_wrappers_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "google/cloud/internal/curl_wrappers.h"
#include "google/cloud/internal/curl_options.h"
#include <gmock/gmock.h>
#include <ostream>
#include <string_view>

namespace google {
Expand All @@ -23,6 +24,7 @@ namespace rest_internal {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
namespace {

using ::testing::Eq;
using ::testing::Pair;
using ::testing::UnorderedElementsAre;

Expand Down Expand Up @@ -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<DebugHeaderCase> {};

/// @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...<truncated>...\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...<truncated>...\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<DebugHeaderCase> const& info) {
return info.param.name;
});

class DebugInfoEncryptionKeyHeadersTest
: public ::testing::TestWithParam<DebugHeaderCase> {};

/// @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<DebugHeaderCase> const& info) {
return info.param.name;
});

TEST(CurlWrappers, CurlInitializeOptions) {
auto defaults = CurlInitializeOptions({});
EXPECT_TRUE(defaults.get<EnableCurlSslLockingOption>());
Expand Down
13 changes: 13 additions & 0 deletions google/cloud/internal/http_header.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <string_view>

namespace google {
namespace cloud {
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions google/cloud/internal/http_header.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <unordered_map>
#endif // UINTPTR_MAX == UINT64_MAX
#include <string>
#include <string_view>
#include <vector>

namespace google {
Expand Down Expand Up @@ -170,6 +171,9 @@ using HttpHeaders = absl::flat_hash_map<HttpHeaderName, HttpHeader>;
using HttpHeaders = std::unordered_map<HttpHeaderName, HttpHeader>;
#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
Expand Down
Loading
Loading