Skip to content
This repository was archived by the owner on Jul 15, 2023. It is now read-only.

Commit 6cbe64b

Browse files
author
J Wyman
authored
Merge pull request #712 from whoisj/v1.17/more-bitbucket
bitbucket: fix data loss from token misuse
2 parents 55acfcb + 5609c07 commit 6cbe64b

File tree

3 files changed

+41
-31
lines changed

3 files changed

+41
-31
lines changed

Bitbucket.Authentication/Src/OAuth/OAuthAuthenticator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ private Token FindAccessToken(string responseText)
323323
&& tokenMatch.Groups.Count > 1)
324324
{
325325
string tokenText = tokenMatch.Groups[1].Value;
326-
return new Token(tokenText, TokenType.Personal);
326+
return new Token(tokenText, TokenType.BitbucketAccess);
327327
}
328328

329329
return null;

Microsoft.Alm.Authentication/Src/Network.cs

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -435,39 +435,49 @@ private HttpClient GetHttpClient(TargetUri targetUri, HttpMessageHandler handler
435435
switch (options.Authorization)
436436
{
437437
case Token token:
438+
{
439+
// Different types of tokens are packed differently.
440+
switch (token.Type)
438441
{
439-
// Different types of tokens are packed differently.
440-
switch (token.Type)
442+
case TokenType.AzureAccess:
443+
case TokenType.BitbucketAccess:
441444
{
442-
case TokenType.AzureAccess:
443-
case TokenType.BitbucketAccess:
444-
case TokenType.Personal:
445-
{
446-
// ADAL access tokens are packed into the Authorization header.
447-
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Value);
448-
}
449-
break;
450-
451-
case TokenType.AzureFederated:
452-
{
453-
// Federated authentication tokens are sent as cookie(s).
454-
httpClient.DefaultRequestHeaders.Add("Cookie", token.Value);
455-
}
456-
break;
457-
458-
default:
459-
Trace.WriteLine("! unsupported token type, not appending an authentication header to the request.");
460-
break;
445+
// ADAL access tokens are packed into the Authorization header.
446+
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Value);
461447
}
448+
break;
449+
450+
case TokenType.AzureFederated:
451+
{
452+
// Federated authentication tokens are sent as cookie(s).
453+
httpClient.DefaultRequestHeaders.Add("Cookie", token.Value);
454+
}
455+
break;
456+
457+
case TokenType.Personal:
458+
{
459+
// Personal access tokens are designed to treated like credentials,
460+
// so treat them like credentials.
461+
var credentials = (Credential)token;
462+
463+
// Credentials are packed into the 'Authorization' header as a base64 encoded pair.
464+
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials.ToBase64String());
465+
}
466+
break;
467+
468+
default:
469+
Trace.WriteLine("! unsupported token type, not appending an authentication header to the request.");
470+
break;
462471
}
463-
break;
472+
}
473+
break;
464474

465475
case Credential credentials:
466-
{
467-
// Credentials are packed into the 'Authorization' header as a base64 encoded pair.
468-
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials.ToBase64String());
469-
}
470-
break;
476+
{
477+
// Credentials are packed into the 'Authorization' header as a base64 encoded pair.
478+
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials.ToBase64String());
479+
}
480+
break;
471481
}
472482
}
473483
}
@@ -491,7 +501,7 @@ private static IWebProxy GetHttpWebProxy(TargetUri targetUri)
491501

492502
if (proxyUri != null)
493503
{
494-
WebProxy proxy = new WebProxy(proxyUri) { UseDefaultCredentials = true };
504+
var proxy = new WebProxy(proxyUri) { UseDefaultCredentials = true };
495505

496506
// check if the user has specified authentications (comes as UserInfo)
497507
if (!string.IsNullOrWhiteSpace(proxyUri.UserInfo) && proxyUri.UserInfo.Length > 1)
@@ -504,7 +514,7 @@ private static IWebProxy GetHttpWebProxy(TargetUri targetUri)
504514
string userName = proxyUri.UserInfo.Substring(0, tokenIndex);
505515
string password = proxyUri.UserInfo.Substring(tokenIndex + 1);
506516

507-
NetworkCredential proxyCreds = new NetworkCredential(userName, password);
517+
var proxyCreds = new NetworkCredential(userName, password);
508518

509519
proxy.UseDefaultCredentials = false;
510520
proxy.Credentials = proxyCreds;

Microsoft.Alm.Authentication/Src/Token.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ public static explicit operator Credential(Token token)
305305
if (token is null)
306306
return null;
307307

308-
if (token.Type != TokenType.Personal)
308+
if (token.Type != TokenType.Personal && token.Type != TokenType.BitbucketAccess)
309309
throw new InvalidCastException($"Cannot cast `{nameof(Token)}` of type '{token.Type}' to `{nameof(Credential)}`");
310310

311311
return new Credential(token.ToString(), token._value);

0 commit comments

Comments
 (0)