Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contributing back patches to 1DS #1266

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Contributing back patches to 1DS #1266

wants to merge 3 commits into from

Conversation

lakshmir124
Copy link

@lakshmir124 lakshmir124 commented Apr 22, 2024

This PR contributes back cpp-client-telemetry related patches used in the teams-client-native-shell repo.

@lakshmir124 lakshmir124 requested a review from a team as a code owner April 22, 2024 00:19

// [optional] Mandatory agents list. If not present in response, the payload
// is determined as bad
std::unordered_set<std::string> mandatoryAgents;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you miss adding any file, as mandatoryAgents is not used anywhere?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be used in the EXPCommonClient.cpp file in this PR - Pull 230 - Contributing back patches to 1DS modules. I think this PR will have to be merged first before we merged the private one.

@lalitb
Copy link
Contributor

lalitb commented Apr 22, 2024

Thanks @lakshmir124, can you please add more details to the PR description?

@lakshmir124 lakshmir124 changed the title Contributing back patch to 1DS Contributing back patches to 1DS Apr 22, 2024
@@ -22,6 +23,8 @@

namespace MAT_NS_BEGIN {

const std::string kProxyRegKeyPath = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
Copy link
Contributor

@mkoscumb mkoscumb Apr 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't create namespace scoped STL containers as these cause heap allocations which are never released until the containing binary is unloaded, which violates Pay For Play.
static const char* kProxyRegKeyPath = ...

LONG getSizeResult = RegGetValueA(hKey, subKey.c_str(), value.c_str(), RRF_RT_REG_SZ, NULL,
NULL, &stringSize);
if (getSizeResult != ERROR_SUCCESS) {
return "";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use the default string constructor, rather than the c-string constructor:
std::string{}

LONG getStringResult = RegGetValueA(hKey, subKey.c_str(), value.c_str(), RRF_RT_REG_SZ, NULL,
&data[0], &stringSize);
if (getStringResult != ERROR_SUCCESS) {
return "";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same.

LONG res = RegGetValueA(hKey, subKey.c_str(), value.c_str(), RRF_RT_REG_DWORD, NULL, &data,
&dataSize);
if (res != ERROR_SUCCESS) {
return 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does a caller distinguish between a zero in the Registry value and an error?

LONG getSizeResult = RegGetValueA(hKey, subKey.c_str(), value.c_str(), RRF_RT_REG_SZ, NULL,
NULL, &stringSize);
if (getSizeResult != ERROR_SUCCESS) {
return "";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does a caller distinguish between an empty string in the registry and an error?

@@ -136,6 +139,105 @@ class WinInetRequestWrapper
return true;
}

DWORD GetDWORDRegKey(HKEY hKey, const std::string& subKey, const std::string& value) {
Copy link
Contributor

@mkoscumb mkoscumb Apr 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If these arguments are always known constants, don't do unnecessary heap allocations, use const char* rather than std::string

Copy link
Contributor

@mkoscumb mkoscumb Apr 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::string_view would be ideal, but this repo is still not on C++17 :(

}

bool ProxyAuthRequired() {
return GetDWORDRegKey(HKEY_CURRENT_USER, kProxyRegKeyPath, "ProxyEnable");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't rely on implicit conversions.

GetDWORDRegKey(...) != 0

return;
}
wchar_t* proxyUser = cred->UserName;
wchar_t* proxyPass = (wchar_t*)cred->CredentialBlob;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No c-style casts in modern code, use static_cast<wchar_t*> or reinterpret_cast<wchar_t*>

static_cast<DWORD>(wcslen(proxyPass) + 1));
}

::CredFree(cred);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should either wrap PCREDENTIALW in a helper type whose destructor calls ::CredFree, or refactor 216-238 into a helper function which is marked noexcept. That way if at any point (including if someone adds new code which can raise exceptions) an exception is raised there's no chance cred is leaked.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The former is preferred as it's more resilient.

struct CredHolder final
{
    PCREDENTIALW cred;
    ~CredHolder() noexcept
    {
        ::CredFree(cred);
    }
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants