Skip to content

Commit fa847b4

Browse files
committed
fix(common): make encryption key header output consistent with CreateHmacKeyResponse
1 parent fdcfe38 commit fa847b4

4 files changed

Lines changed: 377 additions & 0 deletions

File tree

‎google/cloud/internal/curl_wrappers.cc‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@
2525
#include <openssl/opensslv.h>
2626
#endif
2727
#include <algorithm>
28+
#include <array>
2829
#include <cctype>
2930
#include <csignal>
3031
#include <iostream>
32+
#include <optional>
3133
#include <string>
3234
#include <string_view>
3335
#include <thread>
@@ -188,7 +190,80 @@ std::string CleanupDebugData(char const* data, std::size_t size) {
188190
return text;
189191
}
190192

193+
struct CensoredHeaderMatch {
194+
std::size_t start;
195+
std::size_t colon;
196+
};
197+
198+
// Matches encryption key header names followed by ':' only at a header-name
199+
// boundary so sibling headers (-sha256, -algorithm) and prefixed names are
200+
// preserved verbatim.
201+
std::optional<CensoredHeaderMatch> FindCensoredHeader(std::string_view line) {
202+
static std::array<std::string_view, 2> constexpr kCensoredHeaderPrefixes = {
203+
"x-goog-encryption-key:",
204+
"x-goog-copy-source-encryption-key:",
205+
};
206+
for (std::size_t pos = 0; pos < line.size(); ++pos) {
207+
bool const at_boundary = pos == 0 || line[pos - 1] == '[' ||
208+
line[pos - 1] == ' ' || line[pos - 1] == '\t';
209+
if (!at_boundary) continue;
210+
std::string_view const suffix = line.substr(pos);
211+
for (std::string_view prefix : kCensoredHeaderPrefixes) {
212+
if (absl::StartsWithIgnoreCase(suffix, prefix)) {
213+
return CensoredHeaderMatch{pos, pos + prefix.size() - 1};
214+
}
215+
}
216+
}
217+
return std::nullopt;
218+
}
219+
220+
std::string CensorEncryptionKeyHeaders(std::string_view payload) {
221+
std::string out;
222+
out.reserve(payload.size());
223+
std::size_t pos = 0;
224+
while (pos < payload.size()) {
225+
std::size_t const nl_pos = payload.find('\n', pos);
226+
std::size_t const line_end =
227+
nl_pos == std::string_view::npos ? payload.size() : nl_pos;
228+
std::string_view line = payload.substr(pos, line_end - pos);
229+
std::string_view terminator;
230+
if (nl_pos != std::string_view::npos) {
231+
if (!line.empty() && line.back() == '\r') {
232+
line.remove_suffix(1);
233+
terminator = "\r\n";
234+
} else {
235+
terminator = "\n";
236+
}
237+
} else if (line.back() == '\r') {
238+
line.remove_suffix(1);
239+
terminator = "\r";
240+
}
241+
std::optional<CensoredHeaderMatch> const match = FindCensoredHeader(line);
242+
if (match.has_value()) {
243+
// libcurl formats HTTP/2 and HTTP/3 header traces inside brackets
244+
// ("[header: value]"). Preserve only a final closing bracket so embedded
245+
// ']' characters inside the value are still replaced.
246+
bool const bracketed = match->start > 0 && line[match->start - 1] == '[';
247+
std::string_view const closing =
248+
(bracketed && line.back() == ']') ? "]" : "";
249+
absl::StrAppend(&out, line.substr(0, match->colon + 1), " [censored]",
250+
closing, terminator);
251+
} else {
252+
absl::StrAppend(&out, line, terminator);
253+
}
254+
pos = nl_pos == std::string_view::npos ? payload.size() : nl_pos + 1;
255+
}
256+
return out;
257+
}
258+
191259
std::string DebugCensored(std::string_view msg, std::string_view payload) {
260+
// Censor encryption key headers before Bearer truncation so a key value
261+
// beginning with "Bearer " is replaced in full rather than truncated.
262+
std::optional<std::string> censored_payload;
263+
if (absl::StrContainsIgnoreCase(payload, "encryption-key:")) {
264+
censored_payload = CensorEncryptionKeyHeaders(payload);
265+
payload = *censored_payload;
266+
}
192267
// We want to truncate the portion of the payload following this ": Bearer" to
193268
// at most 32 characters, skipping everything else until any newline.
194269
auto const bearer = std::string_view{": Bearer "};

‎google/cloud/internal/curl_wrappers_test.cc‎

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "google/cloud/internal/curl_wrappers.h"
1616
#include "google/cloud/internal/curl_options.h"
1717
#include <gmock/gmock.h>
18+
#include <ostream>
1819
#include <string_view>
1920

2021
namespace google {
@@ -23,6 +24,7 @@ namespace rest_internal {
2324
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
2425
namespace {
2526

27+
using ::testing::Eq;
2628
using ::testing::Pair;
2729
using ::testing::UnorderedElementsAre;
2830

@@ -156,6 +158,211 @@ TEST(CurlWrappers, DebugInfo) {
156158
}
157159
}
158160

161+
struct DebugHeaderCase {
162+
std::string name;
163+
std::string input;
164+
std::string expected;
165+
};
166+
167+
void PrintTo(DebugHeaderCase const& c, std::ostream* os) { *os << c.name; }
168+
169+
class DebugSendHeaderEncryptionKeyHeadersTest
170+
: public ::testing::TestWithParam<DebugHeaderCase> {};
171+
172+
/// @test Verify DebugSendHeader formats encryption key header values as
173+
/// [censored] while preserving line endings, sibling headers, and Bearer
174+
/// truncation.
175+
TEST_P(DebugSendHeaderEncryptionKeyHeadersTest, FormatsLine) {
176+
DebugHeaderCase const& test = GetParam();
177+
EXPECT_THAT(DebugSendHeader(test.input.data(), test.input.size()),
178+
Eq(test.expected));
179+
}
180+
181+
INSTANTIATE_TEST_SUITE_P(
182+
CurlWrappers, DebugSendHeaderEncryptionKeyHeadersTest,
183+
::testing::Values(
184+
DebugHeaderCase{
185+
"EncryptionKeyNoNewline",
186+
"x-goog-encryption-key: "
187+
"MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=",
188+
">> curl(Send Header): x-goog-encryption-key: [censored]"},
189+
DebugHeaderCase{
190+
"EncryptionKeyLf",
191+
"x-goog-encryption-key: "
192+
"MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=\n",
193+
">> curl(Send Header): x-goog-encryption-key: [censored]\n"},
194+
DebugHeaderCase{
195+
"EncryptionKeyCrLf",
196+
"x-goog-encryption-key: "
197+
"MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=\r\n",
198+
">> curl(Send Header): x-goog-encryption-key: [censored]\r\n"},
199+
DebugHeaderCase{
200+
"CopySourceEncryptionKeyNoNewline",
201+
"x-goog-copy-source-encryption-key: "
202+
"QUJDRDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg=",
203+
">> curl(Send Header): x-goog-copy-source-encryption-key: "
204+
"[censored]"},
205+
DebugHeaderCase{
206+
"CopySourceEncryptionKeyLf",
207+
"x-goog-copy-source-encryption-key: "
208+
"QUJDRDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg=\n",
209+
">> curl(Send Header): x-goog-copy-source-encryption-key: "
210+
"[censored]\n"},
211+
DebugHeaderCase{
212+
"CopySourceEncryptionKeyCrLf",
213+
"x-goog-copy-source-encryption-key: "
214+
"QUJDRDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg=\r\n",
215+
">> curl(Send Header): x-goog-copy-source-encryption-key: "
216+
"[censored]\r\n"},
217+
DebugHeaderCase{
218+
"MixedCaseHeaders",
219+
"X-Goog-Encryption-Key: value1\r\n"
220+
"X-Goog-Copy-Source-Encryption-Key: value2\r\n",
221+
">> curl(Send Header): X-Goog-Encryption-Key: [censored]\r\n"
222+
"X-Goog-Copy-Source-Encryption-Key: [censored]\r\n"},
223+
DebugHeaderCase{
224+
"MultiLineWithBearerAndSiblings",
225+
"authorization: Bearer 012345678901234567890123456789123456\n"
226+
"x-goog-encryption-key: "
227+
"MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=\n"
228+
"x-goog-encryption-key-sha256: key-hash\n"
229+
"x-goog-copy-source-encryption-key: "
230+
"QUJDRDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg=\n"
231+
"x-goog-copy-source-encryption-key-sha256: source-hash\n"
232+
"host: storage.googleapis.com\n",
233+
">> curl(Send Header): authorization: Bearer "
234+
"01234567890123456789012345678912...<truncated>...\n"
235+
"x-goog-encryption-key: [censored]\n"
236+
"x-goog-encryption-key-sha256: key-hash\n"
237+
"x-goog-copy-source-encryption-key: [censored]\n"
238+
"x-goog-copy-source-encryption-key-sha256: source-hash\n"
239+
"host: storage.googleapis.com\n"},
240+
DebugHeaderCase{
241+
"DuplicateKeyLines",
242+
"x-goog-encryption-key: first\r\n"
243+
"x-goog-encryption-key: second\r\n",
244+
">> curl(Send Header): x-goog-encryption-key: [censored]\r\n"
245+
"x-goog-encryption-key: [censored]\r\n"},
246+
DebugHeaderCase{
247+
"NoSpaceAfterColon", "x-goog-encryption-key:value",
248+
">> curl(Send Header): x-goog-encryption-key: [censored]"},
249+
DebugHeaderCase{
250+
"EmptyValueWithSpace", "x-goog-encryption-key: \r\n",
251+
">> curl(Send Header): x-goog-encryption-key: [censored]\r\n"},
252+
DebugHeaderCase{
253+
"EmptyValueNoSpace", "x-goog-encryption-key:\r\n",
254+
">> curl(Send Header): x-goog-encryption-key: [censored]\r\n"},
255+
DebugHeaderCase{
256+
"ValueStartingWithBearer",
257+
"x-goog-encryption-key: Bearer "
258+
"012345678901234567890123456789123456\r\n",
259+
">> curl(Send Header): x-goog-encryption-key: [censored]\r\n"},
260+
DebugHeaderCase{
261+
"ValueStartingWithBearerBeforeAuthorization",
262+
"x-goog-encryption-key: Bearer "
263+
"012345678901234567890123456789123456\n"
264+
"authorization: Bearer 012345678901234567890123456789123456\n",
265+
">> curl(Send Header): x-goog-encryption-key: [censored]\n"
266+
"authorization: Bearer "
267+
"01234567890123456789012345678912...<truncated>...\n"},
268+
DebugHeaderCase{
269+
"LeftmostHeaderWinsOnSameLine",
270+
"x-goog-copy-source-encryption-key: val1 x-goog-encryption-key: "
271+
"val2\r\n",
272+
">> curl(Send Header): x-goog-copy-source-encryption-key: "
273+
"[censored]\r\n"},
274+
DebugHeaderCase{
275+
"TabPrecededHeader", "\tx-goog-encryption-key: value\r\n",
276+
">> curl(Send Header): \tx-goog-encryption-key: [censored]\r\n"},
277+
DebugHeaderCase{
278+
"BlankLinesPreservedLf", "x-goog-encryption-key: value\n\n",
279+
">> curl(Send Header): x-goog-encryption-key: [censored]\n\n"},
280+
DebugHeaderCase{"BlankLinesPreservedCrLf",
281+
"x-goog-encryption-key: value\r\n\r\n",
282+
">> curl(Send Header): x-goog-encryption-key: "
283+
"[censored]\r\n\r\n"},
284+
DebugHeaderCase{
285+
"TrailingCarriageReturnOnly", "x-goog-encryption-key: value\r",
286+
">> curl(Send Header): x-goog-encryption-key: [censored]\r"},
287+
DebugHeaderCase{
288+
"UnbracketedValueEndingInClosingBracket",
289+
"x-goog-encryption-key: value]\r\n",
290+
">> curl(Send Header): x-goog-encryption-key: [censored]\r\n"},
291+
DebugHeaderCase{"EmptyPayload", "", ">> curl(Send Header): "},
292+
DebugHeaderCase{
293+
"SiblingAndNearMissHeadersUnchanged",
294+
"x-goog-encryption-key-sha256: key-hash\r\n"
295+
"x-goog-copy-source-encryption-key-sha256: source-hash\r\n"
296+
"x-goog-encryption-algorithm: AES256\r\n"
297+
"my-x-goog-encryption-key: value\r\n",
298+
">> curl(Send Header): x-goog-encryption-key-sha256: key-hash\r\n"
299+
"x-goog-copy-source-encryption-key-sha256: source-hash\r\n"
300+
"x-goog-encryption-algorithm: AES256\r\n"
301+
"my-x-goog-encryption-key: value\r\n"}),
302+
[](::testing::TestParamInfo<DebugHeaderCase> const& info) {
303+
return info.param.name;
304+
});
305+
306+
class DebugInfoEncryptionKeyHeadersTest
307+
: public ::testing::TestWithParam<DebugHeaderCase> {};
308+
309+
/// @test Verify DebugInfo formats bracketed HTTP/2 and HTTP/3 encryption key
310+
/// header traces as [censored] while preserving non-matching headers.
311+
TEST_P(DebugInfoEncryptionKeyHeadersTest, FormatsLine) {
312+
DebugHeaderCase const& test = GetParam();
313+
EXPECT_THAT(DebugInfo(test.input.data(), test.input.size()),
314+
Eq(test.expected));
315+
}
316+
317+
INSTANTIATE_TEST_SUITE_P(
318+
CurlWrappers, DebugInfoEncryptionKeyHeadersTest,
319+
::testing::Values(
320+
DebugHeaderCase{"Http2EncryptionKey",
321+
"[HTTP/2] [1] [x-goog-encryption-key: "
322+
"MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=]\n",
323+
"== curl(Info): [HTTP/2] [1] [x-goog-encryption-key: "
324+
"[censored]]\n"},
325+
DebugHeaderCase{
326+
"Http2CopySourceEncryptionKey",
327+
"[HTTP/2] [1] [x-goog-copy-source-encryption-key: "
328+
"QUJDRDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg=]\n",
329+
"== curl(Info): [HTTP/2] [1] [x-goog-copy-source-encryption-key: "
330+
"[censored]]\n"},
331+
DebugHeaderCase{"Http3EncryptionKey",
332+
"[HTTP/3] [0] [x-goog-encryption-key: "
333+
"MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=]\n",
334+
"== curl(Info): [HTTP/3] [0] [x-goog-encryption-key: "
335+
"[censored]]\n"},
336+
DebugHeaderCase{
337+
"H2H3EncryptionKey",
338+
"h2h3 [x-goog-encryption-key: "
339+
"MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=]\n",
340+
"== curl(Info): h2h3 [x-goog-encryption-key: [censored]]\n"},
341+
DebugHeaderCase{
342+
"SpacePrecededTraceHeader",
343+
"[1] header: x-goog-encryption-key: "
344+
"MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=\n",
345+
"== curl(Info): [1] header: x-goog-encryption-key: [censored]\n"},
346+
DebugHeaderCase{
347+
"UnclosedBracketEncryptionKey",
348+
"[HTTP/2] [1] [x-goog-encryption-key: "
349+
"MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=",
350+
"== curl(Info): [HTTP/2] [1] [x-goog-encryption-key: [censored]"},
351+
DebugHeaderCase{"ValueContainingClosingBracket",
352+
"[HTTP/2] [1] [x-goog-encryption-key: part1]part2]\n",
353+
"== curl(Info): [HTTP/2] [1] [x-goog-encryption-key: "
354+
"[censored]]\n"},
355+
DebugHeaderCase{
356+
"SiblingAndNearMissHeadersUnchanged",
357+
"[HTTP/2] [1] [x-goog-encryption-key-sha256: key-hash]\n"
358+
"[HTTP/2] [1] [my-x-goog-encryption-key: value]\n",
359+
"== curl(Info): [HTTP/2] [1] [x-goog-encryption-key-sha256: "
360+
"key-hash]\n"
361+
"[HTTP/2] [1] [my-x-goog-encryption-key: value]\n"}),
362+
[](::testing::TestParamInfo<DebugHeaderCase> const& info) {
363+
return info.param.name;
364+
});
365+
159366
TEST(CurlWrappers, CurlInitializeOptions) {
160367
auto defaults = CurlInitializeOptions({});
161368
EXPECT_TRUE(defaults.get<EnableCurlSslLockingOption>());

‎google/cloud/internal/rest_opentelemetry.cc‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ class RestClientCarrier
5959
RestContext& context_;
6060
};
6161

62+
bool IsCensoredHeader(std::string_view name) {
63+
std::string_view constexpr kEncryptionKeyHeader = "x-goog-encryption-key";
64+
std::string_view constexpr kCopySourceEncryptionKeyHeader =
65+
"x-goog-copy-source-encryption-key";
66+
return absl::EqualsIgnoreCase(name, kEncryptionKeyHeader) ||
67+
absl::EqualsIgnoreCase(name, kCopySourceEncryptionKeyHeader);
68+
}
69+
6270
} // namespace
6371

6472
void InjectTraceContext(
@@ -91,6 +99,10 @@ opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> MakeSpanHttp(
9199
span->SetAttribute(name, kv.second.values().front().substr(0, 32));
92100
continue;
93101
}
102+
if (IsCensoredHeader(kv.first.name())) {
103+
span->SetAttribute(name, "[censored]");
104+
continue;
105+
}
94106
span->SetAttribute(name, kv.second.values().front());
95107
}
96108
return span;

0 commit comments

Comments
 (0)