Skip to content

Commit 20fcafd

Browse files
authored
Censor credentials in debug log messages. (#1434)
Censoring of credentials was added to debug log messages for Windows, Android and curl implementation. Resolves: OAM-2216 Signed-off-by: Yevhen Krasilnyk <[email protected]>
1 parent 58f9566 commit 20fcafd

File tree

8 files changed

+232
-14
lines changed

8 files changed

+232
-14
lines changed

olp-cpp-sdk-core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ set(OLP_SDK_PORTING_HEADERS
135135
set(OLP_SDK_UTILS_HEADERS
136136
./include/olp/core/utils/Base64.h
137137
./include/olp/core/utils/Config.h
138+
./include/olp/core/utils/Credentials.h
138139
./include/olp/core/utils/Dir.h
139140
./include/olp/core/utils/LruCache.h
140141
./include/olp/core/utils/Url.h
@@ -334,6 +335,7 @@ set(OLP_SDK_PLATFORM_SOURCES
334335
set(OLP_SDK_UTILS_SOURCES
335336
./src/utils/Base64.cpp
336337
./src/utils/BoostExceptionHandle.cpp
338+
./src/utils/Credentials.cpp
337339
./src/utils/Dir.cpp
338340
./src/utils/Url.cpp
339341
)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (C) 2023 HERE Europe B.V.
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+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
#pragma once
21+
22+
#include <string>
23+
24+
namespace olp {
25+
namespace utils {
26+
/**
27+
* Censores app_id and app_code parameter's values in the URL.
28+
* @param[in] url The URL to be censored.
29+
* @return An URL with censored app_id and app_code.
30+
*/
31+
std::string CensorCredentialsInUrl(std::string url);
32+
33+
} // namespace utils
34+
} // namespace olp

olp-cpp-sdk-core/src/http/android/NetworkAndroid.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424
#include <memory>
2525
#include <sstream>
2626
#include <string>
27+
#include <utility>
2728
#include <vector>
2829

2930
#include "olp/core/context/Context.h"
3031
#include "olp/core/http/HttpStatusCode.h"
3132
#include "olp/core/logging/Log.h"
3233
#include "olp/core/porting/make_unique.h"
34+
#include "olp/core/utils/Credentials.h"
3335

3436
#include "utils/JNIScopedLocalReference.h"
3537
#include "utils/JNIThreadBinder.h"
@@ -384,15 +386,20 @@ bool NetworkAndroid::Initialize() {
384386
if (!natives_registered) {
385387
JNINativeMethod methods[] = {
386388
{"headersCallback", "(J[Ljava/lang/String;)V",
387-
(void*)&Java_com_here_olp_network_HttpClient_headersCallback},
389+
reinterpret_cast<void*>(
390+
&Java_com_here_olp_network_HttpClient_headersCallback)},
388391
{"dateAndOffsetCallback", "(JJJ)V",
389-
(void*)&Java_com_here_olp_network_HttpClient_dateAndOffsetCallback},
392+
reinterpret_cast<void*>(
393+
&Java_com_here_olp_network_HttpClient_dateAndOffsetCallback)},
390394
{"dataCallback", "(J[BI)V",
391-
(void*)&Java_com_here_olp_network_HttpClient_dataCallback},
395+
reinterpret_cast<void*>(
396+
&Java_com_here_olp_network_HttpClient_dataCallback)},
392397
{"completeRequest", "(JIIILjava/lang/String;Ljava/lang/String;)V",
393-
(void*)&Java_com_here_olp_network_HttpClient_completeRequest},
398+
reinterpret_cast<void*>(
399+
&Java_com_here_olp_network_HttpClient_completeRequest)},
394400
{"resetRequest", "(J)V",
395-
(void*)&Java_com_here_olp_network_HttpClient_resetRequest}};
401+
reinterpret_cast<void*>(
402+
&Java_com_here_olp_network_HttpClient_resetRequest)}};
396403

397404
env->RegisterNatives(java_self_class_, methods,
398405
sizeof(methods) / sizeof(methods[0]));
@@ -670,8 +677,9 @@ void NetworkAndroid::CompleteRequest(JNIEnv* env, RequestId request_id,
670677
OLP_SDK_LOG_DEBUG(
671678
kLogTag, "CompleteRequest, request_id="
672679
<< request_id << ", uploaded_bytes=" << uploaded_bytes
673-
<< ", downloaded_bytes=" << downloaded_bytes
674-
<< ", url=" << request_data->url << ", status=" << status);
680+
<< ", downloaded_bytes=" << downloaded_bytes << ", url="
681+
<< olp::utils::CensorCredentialsInUrl(request_data->url)
682+
<< ", status=" << status);
675683
// We don't need the object anymore
676684
env->DeleteGlobalRef(request_data->obj);
677685
request_data->obj = nullptr;
@@ -917,7 +925,8 @@ SendOutcome NetworkAndroid::Send(NetworkRequest request,
917925
env.GetEnv()->ExceptionClear();
918926
return SendOutcome(ErrorCode::IO_ERROR);
919927
}
920-
env.GetEnv()->SetByteArrayRegion(jbody, 0, size, (jbyte*)body_data);
928+
env.GetEnv()->SetByteArrayRegion(jbody, 0, size,
929+
reinterpret_cast<const jbyte*>(body_data));
921930
}
922931
utils::JNIScopedLocalReference body_ref(env.GetEnv(), jbody);
923932

olp-cpp-sdk-core/src/http/curl/NetworkCurl.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "olp/core/http/NetworkInitializationSettings.h"
4646
#include "olp/core/http/NetworkUtils.h"
4747
#include "olp/core/logging/Log.h"
48+
#include "olp/core/utils/Credentials.h"
4849

4950
namespace olp {
5051
namespace http {
@@ -526,8 +527,10 @@ ErrorCode NetworkCurl::SendImplementation(
526527
return ErrorCode::NETWORK_OVERLOAD_ERROR;
527528
}
528529

529-
OLP_SDK_LOG_DEBUG(
530-
kLogTag, "Send request with url=" << request.GetUrl() << ", id=" << id);
530+
OLP_SDK_LOG_DEBUG(kLogTag,
531+
"Send request with url="
532+
<< utils::CensorCredentialsInUrl(request.GetUrl())
533+
<< ", id=" << id);
531534

532535
handle->ignore_offset = false; // request.IgnoreOffset();
533536
handle->skip_content = false; // config->SkipContentWhenError();
@@ -982,7 +985,8 @@ void NetworkCurl::CompleteMessage(CURL* handle, CURLcode result) {
982985

983986
OLP_SDK_LOG_DEBUG(kLogTag,
984987
"Message completed, id="
985-
<< rhandle.id << ", url='" << url << "', status=("
988+
<< rhandle.id << ", url='"
989+
<< utils::CensorCredentialsInUrl(url) << "', status=("
986990
<< status << ") " << error
987991
<< ", time=" << GetElapsedTime(rhandle.send_time)
988992
<< "ms, bytes=" << download_bytes + upload_bytes);

olp-cpp-sdk-core/src/http/winhttp/NetworkWinHttp.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "olp/core/http/NetworkUtils.h"
3333
#include "olp/core/logging/Log.h"
3434
#include "olp/core/porting/make_unique.h"
35+
#include "olp/core/utils/Credentials.h"
3536

3637
namespace {
3738

@@ -410,7 +411,8 @@ SendOutcome NetworkWinHttp::Send(NetworkRequest request,
410411
payload, request);
411412
if (handle == nullptr) {
412413
OLP_SDK_LOG_DEBUG(kLogTag,
413-
"All handles are in use, url=" << request.GetUrl());
414+
"All handles are in use, url="
415+
<< utils::CensorCredentialsInUrl(request.GetUrl()));
414416
return SendOutcome(ErrorCode::NETWORK_OVERLOAD_ERROR);
415417
}
416418
}
@@ -591,8 +593,10 @@ SendOutcome NetworkWinHttp::Send(NetworkRequest request,
591593

592594
handle->result_data->bytes_uploaded += content_length + headers.size();
593595

594-
OLP_SDK_LOG_DEBUG(kLogTag,
595-
"Send request, url=" << request.GetUrl() << ", id=" << id);
596+
OLP_SDK_LOG_DEBUG(
597+
kLogTag,
598+
"Send request, url=" << utils::CensorCredentialsInUrl(request.GetUrl())
599+
<< ", id=" << id);
596600

597601
return SendOutcome(id);
598602
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (C) 2023 HERE Europe B.V.
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+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
#include <olp/core/utils/Credentials.h>
21+
22+
namespace {
23+
void CensorCredentialsPart(std::string& url, std::size_t arguments_start,
24+
const std::string& credentials_part_name) {
25+
const auto credentials_part_argument_start =
26+
url.find(credentials_part_name, arguments_start);
27+
28+
if (credentials_part_argument_start == std::string::npos) {
29+
return;
30+
}
31+
32+
const auto credentials_part_value_start =
33+
credentials_part_argument_start + credentials_part_name.length();
34+
const auto credentials_part_argument_end =
35+
url.find('&', credentials_part_value_start);
36+
const auto credentials_part_value_end =
37+
credentials_part_argument_end != std::string::npos
38+
? credentials_part_argument_end
39+
: url.size();
40+
41+
const auto credentials_part_value_begin_it =
42+
std::next(url.begin(), credentials_part_value_start);
43+
const auto credentials_part_value_end_it =
44+
std::next(url.begin(), credentials_part_value_end);
45+
std::fill(credentials_part_value_begin_it, credentials_part_value_end_it,
46+
'*');
47+
}
48+
} // anonymous namespace
49+
50+
namespace olp {
51+
namespace utils {
52+
std::string CensorCredentialsInUrl(std::string url) {
53+
const auto arguments_start_pos = url.find('?');
54+
55+
if (arguments_start_pos == std::string::npos) {
56+
return url;
57+
}
58+
59+
static const std::string app_id_name = "app_id=";
60+
CensorCredentialsPart(url, arguments_start_pos, app_id_name);
61+
62+
static const std::string app_code_name = "app_code=";
63+
CensorCredentialsPart(url, arguments_start_pos, app_code_name);
64+
65+
static const std::string api_key_name = "apiKey=";
66+
CensorCredentialsPart(url, arguments_start_pos, api_key_name);
67+
68+
return url;
69+
}
70+
71+
} // namespace utils
72+
} // namespace olp

olp-cpp-sdk-core/tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ set(OLP_CPP_SDK_CORE_TESTS_SOURCES
6767

6868
./http/NetworkSettingsTest.cpp
6969
./http/NetworkUtils.cpp
70+
71+
./utils/UtilsTest.cpp
7072
)
7173

7274
if (ANDROID OR IOS)
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright (C) 2023 HERE Europe B.V.
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+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
#include <gtest/gtest.h>
21+
22+
#include <string>
23+
24+
#include <olp/core/utils/Credentials.h>
25+
26+
namespace {
27+
28+
using UtilsTest = testing::Test;
29+
30+
TEST(UtilsTest, Credentials) {
31+
{
32+
SCOPED_TRACE("Empty url");
33+
34+
EXPECT_TRUE(olp::utils::CensorCredentialsInUrl("").empty());
35+
}
36+
37+
{
38+
SCOPED_TRACE("Nothing to censor");
39+
40+
const std::string url{
41+
"https://sab.metadata.data.api.platform.here.com/metadata/v1/"
42+
"catalogs/"
43+
"hrn:here:data::olp-here:ocm-patch/"
44+
"versions?endVersion=46&startVersion=0"};
45+
46+
EXPECT_EQ(olp::utils::CensorCredentialsInUrl(url), url);
47+
}
48+
49+
{
50+
SCOPED_TRACE("Censoring app_id, app_code");
51+
52+
const std::string app_id{"2ARQ22QED2TMaSsPlC88DO"};
53+
54+
const std::string app_code{
55+
"9849asdasdasYiukljbnSIUYAGlhbLASYJDgljkhjblhbuhblkSABLhb12312312321231"
56+
"12"
57+
"321312l;kasjdf"};
58+
59+
const std::string url_with_credentials{
60+
"https://api-lookup.data.api.platform.here.com/lookup/v1/resources/"
61+
"hrn:here:data::olp-here:ocm-patch/"
62+
"apis?app_id=" +
63+
app_id + "&app_code=" + app_code};
64+
65+
const auto result =
66+
olp::utils::CensorCredentialsInUrl(url_with_credentials);
67+
68+
EXPECT_EQ(url_with_credentials.size(), result.size());
69+
EXPECT_EQ(result.find(app_id, 0), std::string::npos);
70+
EXPECT_EQ(result.find(app_code, 0), std::string::npos);
71+
}
72+
73+
{
74+
SCOPED_TRACE("Censoring apiKey");
75+
76+
const std::string appKey{"SomeApiKey"};
77+
78+
const std::string url_with_credentials{
79+
"https://api-lookup.data.api.platform.here.com/lookup/v1/resources/"
80+
"hrn:here:data::olp-here:ocm-patch/"
81+
"apis?apiKey=" +
82+
appKey};
83+
84+
const auto result =
85+
olp::utils::CensorCredentialsInUrl(url_with_credentials);
86+
87+
EXPECT_EQ(url_with_credentials.size(), result.size());
88+
EXPECT_EQ(result.find(appKey, 0), std::string::npos);
89+
}
90+
}
91+
} // namespace

0 commit comments

Comments
 (0)