Repository: microsoft/terminal
File: src/cascadia/TerminalConnection/AzureConnection.cpp (C++)

Code before:
        // Return the uri
        return winrt::hstring{ std::wstring{ cloudShell.GetNamedObject(L"properties").GetNamedString(L"uri") } + L"/" };
    }

    // Method description:
    // - helper function to request for a terminal
    // Return value:
    // - the uri for the terminal
    winrt::hstring AzureConnection::_GetTerminal(const winrt::hstring& shellType)
    {
        auto uri{ fmt::format(L"{}terminals?cols={}&rows={}&version=2019-01-01&shell={}", _cloudShellUri, _initialCols, _initialRows, shellType) };

        WWH::HttpStringContent content{
            L"{}",
            WSS::UnicodeEncoding::Utf8,
            // LOAD-BEARING. the API returns "'content-type' should be 'application/json' or 'multipart/form-data'"
            L"application/json"
        };

        const auto terminalResponse = _SendRequestReturningJson(uri, content, WWH::HttpMethod::Post(), Windows::Foundation::Uri(_cloudShellUri));
        _terminalID = terminalResponse.GetNamedString(L"id");

        auto socketUri = terminalResponse.GetNamedString(L"socketUri");

        // we have to do some post-handling to get the proper socket endpoint
        // at this point, socketUri is of the form: wss://ccon-prod-westus-aci-03.servicebus.windows.net/cc-AAAA-AAAAAAAA//aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
        // we need it to become:                    wss://ccon-prod-westus-aci-03.servicebus.windows.net/$hc/cc-AAAA-AAAAAAAA/terminals/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
        // (the "aaaaaa..." part at the end is just the _terminalID, that we already have stored)
        std::wstring wSocketUri{ socketUri };

        // get the substring up until the ".net"
        const auto dotNetPos = wSocketUri.find(L".net") + 4;
        const auto wSocketUriBody = wSocketUri.substr(0, dotNetPos);

        // get the portion between the ".net" and the "//" (this is the cc-AAAA-AAAAAAAA part)
        const auto lastDoubleSlashPos = wSocketUri.find_last_of(L"//");
        const auto wSocketUriMiddle = wSocketUri.substr(dotNetPos, lastDoubleSlashPos - (dotNetPos));

        // piece together the final uri, adding in the "$hc" and "terminals" where needed
        auto finalSocketUri{ fmt::format(L"{}/$hc{}terminals/{}", wSocketUriBody, wSocketUriMiddle, _terminalID) };

        // Return the uri
        return winrt::hstring(finalSocketUri);
    }

    // Method description:
    // - helper function to store the credentials
    // - we store the display name, tenant ID, access/refresh tokens, and token expiry
    void AzureConnection::_StoreCredential()
    {
        WDJ::JsonObject userName;

Review comment (security):
Are you sure it always contains `//`? This PR should also close #15513 and there the issue author shows that they get a response that properly includes `/terminals/`. Should we protect against that? See: https://github.com/microsoft/terminal/issues/15513#issuecomment-1580932516

Code after:
        // Return the uri
        return winrt::hstring{ std::wstring{ cloudShell.GetNamedObject(L"properties").GetNamedString(L"uri") } + L"/" };
    }

    // Method description:
    // - helper function to request for a terminal
    // Return value:
    // - the uri for the terminal
    winrt::hstring AzureConnection::_GetTerminal(const winrt::hstring& shellType)
    {
        auto uri{ fmt::format(L"{}terminals?cols={}&rows={}&version=2019-01-01&shell={}", _cloudShellUri, _initialCols, _initialRows, shellType) };

        WWH::HttpStringContent content{
            L"{}",
            WSS::UnicodeEncoding::Utf8,
            // LOAD-BEARING. the API returns "'content-type' should be 'application/json' or 'multipart/form-data'"
            L"application/json"
        };

        const auto terminalResponse = _SendRequestReturningJson(uri, content, WWH::HttpMethod::Post(), Windows::Foundation::Uri(_cloudShellUri));
        _terminalID = terminalResponse.GetNamedString(L"id");

        // we have to do some post-handling to get the proper socket endpoint
        // the logic here is based on the way the cloud shell team itself does it
        winrt::hstring finalSocketUri;
        const std::wstring_view wCloudShellUri{ _cloudShellUri };

        if (wCloudShellUri.find(L"servicebus") == std::wstring::npos)
        {
            // wCloudShellUri does not contain the word "servicebus", we can just use it to make the final URI

            // remove the "https" from the cloud shell URI
            const auto uriWithoutProtocol = wCloudShellUri.substr(5);

            finalSocketUri = fmt::format(FMT_COMPILE(L"wss{}terminals/{}"), uriWithoutProtocol, _terminalID);
        }
        else
        {
            // if wCloudShellUri contains the word "servicebus", that means the returned socketUri is of the form
            // wss://ccon-prod-westus-aci-03.servicebus.windows.net/cc-AAAA-AAAAAAAA//aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
            // we need to change it to:
            // wss://ccon-prod-westus-aci-03.servicebus.windows.net/$hc/cc-AAAA-AAAAAAAA/terminals/aaaaaaaaaaaaaaaaaaaaaa

            const auto socketUri = terminalResponse.GetNamedString(L"socketUri");
            const std::wstring_view wSocketUri{ socketUri };

            // get the substring up until the ".net"
            const auto dotNetStart = wSocketUri.find(L".net");
            THROW_HR_IF(E_UNEXPECTED, dotNetStart == std::wstring::npos);
            const auto dotNetEnd = dotNetStart + 4;
            const auto wSocketUriBody = wSocketUri.substr(0, dotNetEnd);