1 //
2 // Copyright (C) 2023 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #include "host/libs/web/http_client/http_client_util.h"
17 
18 #include <regex>
19 #include <string>
20 
21 namespace cuttlefish {
22 
ScrubSecrets(const std::string & data)23 std::string ScrubSecrets(const std::string& data) {
24   std::string result = data;
25   // eg [<head>]Authorization: Bearer token_text[<tail>] ->
26   //    [<head>]Authorization: Bearer token_...[<tail>]
27   result = std::regex_replace(
28       result, std::regex("(Authorization:[ ]+\\S+[ ]+)(\\S{6})\\S*"),
29       "$1$2...");
30   // eg [<head>]client_secret=token_text[<tail>] ->
31   //    [<head>]client_secret=token_...[<tail>]
32   result = std::regex_replace(
33       result, std::regex("(client_secret=)(\\S{6})[^\\&\\s]*"), "$1$2...");
34   return result;
35 }
36 
37 }  // namespace cuttlefish
38