8000 Handle exceptions and null returns from CredentialsProvider. by chescock · Pull Request #1239 · libgit2/libgit2sharp · GitHub
[go: up one dir, main page]

Skip to content

Handle exceptions and null returns from CredentialsProvider. #1239

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

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions LibGit2Sharp/RemoteCallbacks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,21 @@ private int GitCredentialHandler(
types |= SupportedCredentialTypes.Default;
}

var cred = CredentialsProvider(url, username, types);

return cred.GitCredentialHandler(out ptr);
ptr = IntPtr.Zero;
try
{
var cred = CredentialsProvider(url, username, types);
if (cred == null)
Copy link
Member

Choose a reason for hiding this comment

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

Now that we get rid of a potential NullReferenceException, what would be the behavior from the user perspective of trying to fetch a (private) repository without passing a CredentialsProvider?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A LibGit2SharpException exception will be thrown.

When using WinHTTP, passing null for CredentialsProvider currently throws an exception with the message "Request failed with status code: 401". With this change and libgit2/libgit2#3528 together, passing a CredentialsProvider that returns a null Credentials will have the same message. With just this change, it will instead have the message "No error message has been provided by the native library". I think the other transports will have the message "authentication required but no callback set" in all of those cases.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for your answer. Considering the less than helpful message (from a user perspective), I'd rather wait for the libgit2 PR to be merged before merging this in.

/cc @carlosmn

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nulltoken I think the libgit2 changes were pulled in with #1264, so this will now give friendly messages in all cases.

{
return (int)GitErrorCode.PassThrough;
}
return cred.GitCredentialHandler(out ptr);
}
catch (Exception exception)
{
Proxy.giterr_set_str(GitErrorCategory.Callback, exception);
return (int)GitErrorCode.Error;
}
}

private int GitCertificateCheck(IntPtr certPtr, int valid, IntPtr cHostname, IntPtr payload)
Expand Down
0