GitLabApiClient icon indicating copy to clipboard operation
GitLabApiClient copied to clipboard

BranchClient does not escape special characters in branch names

Open Khubajsn opened this issue 2 years ago • 1 comments

When working with branch names that contain special characters (including but not limited to slashes), BranchClient performs incorrect HTTP requests.

URL encoding for the branchName parameter should be added to the following methods:

  • GetAsync
  • DeleteBranch
  • GetProtectedBranchesAsync
  • UnprotectBranchAsync

Khubajsn avatar Jun 13 '22 08:06 Khubajsn

Hi @Khubajsn i think you have to URL-encoded the branch name link this: ... = System.Web.HttpUtility.UrlEncode(branchName)

GitLab documentation: File path, branches, and tags name encoding

But there is also a problem with the parser UriParser class in .NET 4.x if you use %2f in the URL

Workaround .. without warranty

        public async Task<T> Get<T>(string url)
        {
            System.Uri tmpUri = null;

            if (!url.ToLower().Contains("%2f"))
            {
                tmpUri = new System.Uri(url, System.UriKind.Relative);
            }
            else
            {
                string absoluteUri = string.Format("{0}{1}", _client.BaseAddress.AbsoluteUri, url);
                tmpUri = new System.Uri(absoluteUri);

                string paq = tmpUri.PathAndQuery; // you have to access to the PathAndQuery property
                System.Reflection.FieldInfo fieldInfoFlags = typeof(Uri).GetField("m_Flags", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
                ulong flags = (ulong)fieldInfoFlags.GetValue(tmpUri);
                flags &= ~((ulong)0x30); // Flags.PathNotCanonical (0x10) | Flags.QueryNotCanonical (0x20)
                fieldInfoFlags.SetValue(tmpUri, flags);
            }

            var responseMessage = await _client.GetAsync(tmpUri);
            await EnsureSuccessStatusCode(responseMessage);
            return await ReadResponse<T>(responseMessage);
        }

Shineson1001 avatar Jun 22 '22 17:06 Shineson1001